Skip to content

Commit 3a2e210

Browse files
Moving is_java_string_type functions to string_preprocess
This avoid dependencies between goto-programs and solver. We also removed function is_unrefined_string_type which should not be needed in the string solver. Removed also mention of java string types and unrefined types in the solver. These mentions should not be necessary, since we are not supposed to do anything java specific. The solver should only have to deal with refined string types and possibly char arrays.
1 parent 1aede78 commit 3a2e210

8 files changed

+166
-153
lines changed

src/goto-programs/string_refine_preprocess.cpp

+130-16
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,125 @@ Date: September 2016
2222

2323
#include "string_refine_preprocess.h"
2424

25-
/******************************************************************* \
25+
/*******************************************************************\
26+
27+
Function: string_refine_preprocesst::is_java_string_pointer_type
28+
29+
Inputs: a type
30+
31+
Outputs: Boolean telling whether the type is that of java string pointers
32+
33+
\*******************************************************************/
34+
35+
bool string_refine_preprocesst::is_java_string_pointer_type(const typet &type)
36+
{
37+
if(type.id()==ID_pointer)
38+
{
39+
const pointer_typet &pt=to_pointer_type(type);
40+
const typet &subtype=pt.subtype();
41+
return is_java_string_type(subtype);
42+
}
43+
return false;
44+
}
45+
46+
/*******************************************************************\
47+
48+
Function: string_refine_preprocesst::is_java_string_type
49+
50+
Inputs: a type
51+
52+
Outputs: Boolean telling whether the type is that of java string
53+
54+
\*******************************************************************/
55+
56+
bool string_refine_preprocesst::is_java_string_type(const typet &type)
57+
{
58+
if(type.id()==ID_symbol)
59+
{
60+
const irep_idt &tag=to_symbol_type(type).get_identifier();
61+
return tag=="java::java.lang.String";
62+
}
63+
else if(type.id()==ID_struct)
64+
{
65+
const irep_idt &tag=to_struct_type(type).get_tag();
66+
return tag=="java.lang.String";
67+
}
68+
return false;
69+
}
70+
71+
/*******************************************************************\
72+
73+
Function: string_refine_preprocesst::is_java_string_builder_type
74+
75+
Inputs: a type
76+
77+
Outputs: Boolean telling whether the type is that of java string builder
78+
79+
\*******************************************************************/
80+
81+
bool string_refine_preprocesst::is_java_string_builder_type(const typet &type)
82+
{
83+
if(type.id()==ID_pointer)
84+
{
85+
const pointer_typet &pt=to_pointer_type(type);
86+
const typet &subtype=pt.subtype();
87+
if(subtype.id()==ID_struct)
88+
{
89+
const irep_idt &tag=to_struct_type(subtype).get_tag();
90+
return tag=="java.lang.StringBuilder";
91+
}
92+
}
93+
return false;
94+
}
95+
96+
/*******************************************************************\
97+
98+
Function: string_refine_preprocesst::is_java_string_builder_pointer_type
99+
100+
Inputs: a type
101+
102+
Outputs: Boolean telling whether the type is that of java StringBuilder
103+
pointers
104+
105+
\*******************************************************************/
106+
107+
bool string_refine_preprocesst::is_java_string_builder_pointer_type(
108+
const typet &type)
109+
{
110+
if(type.id()==ID_pointer)
111+
{
112+
const pointer_typet &pt=to_pointer_type(type);
113+
const typet &subtype=pt.subtype();
114+
return is_java_string_builder_type(subtype);
115+
}
116+
return false;
117+
}
118+
/*******************************************************************\
119+
120+
Function: string_refine_preprocesst::is_java_char_sequence_type
121+
122+
Inputs: a type
123+
124+
Outputs: Boolean telling whether the type is that of java char sequence
125+
126+
\*******************************************************************/
127+
128+
bool string_refine_preprocesst::is_java_char_sequence_type(const typet &type)
129+
{
130+
if(type.id()==ID_pointer)
131+
{
132+
const pointer_typet &pt=to_pointer_type(type);
133+
const typet &subtype=pt.subtype();
134+
if(subtype.id()==ID_struct)
135+
{
136+
const irep_idt &tag=to_struct_type(subtype).get_tag();
137+
return tag=="java.lang.CharSequence";
138+
}
139+
}
140+
return false;
141+
}
142+
143+
/*******************************************************************\
26144
27145
Function: string_refine_preprocesst::fresh_array
28146
@@ -114,8 +232,8 @@ Function: string_refine_preprocesst::get_data_and_length_type_of_string
114232
void string_refine_preprocesst::get_data_and_length_type_of_string(
115233
const exprt &expr, typet &data_type, typet &length_type)
116234
{
117-
assert(refined_string_typet::is_java_string_type(expr.type()) ||
118-
refined_string_typet::is_java_string_builder_type(expr.type()));
235+
assert(is_java_string_type(expr.type()) ||
236+
is_java_string_builder_type(expr.type()));
119237
typet object_type=ns.follow(expr.type());
120238
const struct_typet &struct_type=to_struct_type(object_type);
121239
for(const auto &component : struct_type.components())
@@ -146,7 +264,7 @@ exprt string_refine_preprocesst::make_cprover_string_assign(
146264
const exprt &rhs,
147265
const source_locationt &location)
148266
{
149-
if(refined_string_typet::is_java_string_pointer_type(rhs.type()))
267+
if(is_java_string_pointer_type(rhs.type()))
150268
{
151269
auto pair=java_to_cprover_strings.insert(
152270
std::pair<exprt, exprt>(rhs, nil_exprt()));
@@ -183,7 +301,7 @@ exprt string_refine_preprocesst::make_cprover_string_assign(
183301
return pair.first->second;
184302
}
185303
else if(rhs.id()==ID_typecast &&
186-
refined_string_typet::is_java_string_pointer_type(rhs.op0().type()))
304+
is_java_string_pointer_type(rhs.op0().type()))
187305
{
188306
exprt new_rhs=make_cprover_string_assign(
189307
goto_program, target, rhs.op0(), location);
@@ -303,8 +421,7 @@ void string_refine_preprocesst::make_string_assign(
303421
const source_locationt &location,
304422
const std::string &signature)
305423
{
306-
assert(refined_string_typet::is_java_string_pointer_type(
307-
function_type.return_type()));
424+
assert(is_java_string_pointer_type(function_type.return_type()));
308425
dereference_exprt deref(lhs, lhs.type().subtype());
309426
typet object_type=ns.follow(deref.type());
310427
exprt object_size=size_of_expr(object_type, ns);
@@ -371,8 +488,7 @@ void string_refine_preprocesst::make_assign(
371488
const source_locationt &loc,
372489
const std::string &sig)
373490
{
374-
if(refined_string_typet::is_java_string_pointer_type(
375-
function_type.return_type()))
491+
if(is_java_string_pointer_type(function_type.return_type()))
376492
make_string_assign(
377493
goto_program, target, lhs, function_type, function_name, arg, loc, sig);
378494
else
@@ -404,8 +520,8 @@ void string_refine_preprocesst::make_string_copy(
404520
const source_locationt &location)
405521
{
406522
// TODO : treat CharSequence and StringBuffer
407-
assert(refined_string_typet::is_java_string_pointer_type(lhs.type()) ||
408-
refined_string_typet::is_java_string_builder_pointer_type(lhs.type()));
523+
assert(is_java_string_pointer_type(lhs.type()) ||
524+
is_java_string_builder_pointer_type(lhs.type()));
409525
exprt deref=dereference_exprt(lhs, lhs.type().subtype());
410526

411527
typet length_type, data_type;
@@ -451,8 +567,7 @@ void string_refine_preprocesst::make_string_function(
451567
const source_locationt &location,
452568
const std::string &signature)
453569
{
454-
if(refined_string_typet::is_java_string_pointer_type(
455-
function_type.return_type()))
570+
if(is_java_string_pointer_type(function_type.return_type()))
456571
make_string_assign(
457572
goto_program,
458573
target,
@@ -633,8 +748,7 @@ void string_refine_preprocesst::make_to_char_array_function(
633748

634749
assert(!function_call.arguments().empty());
635750
const exprt &string_argument=function_call.arguments()[0];
636-
assert(refined_string_typet::is_java_string_pointer_type(
637-
string_argument.type()));
751+
assert(is_java_string_pointer_type(string_argument.type()));
638752

639753
dereference_exprt deref(string_argument, string_argument.type().subtype());
640754
typet length_type, data_type;
@@ -870,7 +984,7 @@ exprt::operandst string_refine_preprocesst::process_arguments(
870984
{
871985
while(arg.id()==ID_typecast)
872986
arg=arg.op0();
873-
if(!refined_string_typet::is_java_string_type(arg.type()))
987+
if(!is_java_string_type(arg.type()))
874988
arg=typecast_exprt(arg, jls_ptr);
875989
}
876990
exprt arg2=make_cprover_string_assign(

src/goto-programs/string_refine_preprocess.h

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ class string_refine_preprocesst:public messaget
5151

5252
void initialize_string_function_table();
5353

54+
static bool is_java_string_pointer_type(const typet &type);
55+
56+
static bool is_java_string_type(const typet &type);
57+
58+
static bool is_java_string_builder_type(const typet &type);
59+
60+
static bool is_java_string_builder_pointer_type(const typet &type);
61+
62+
static bool is_java_char_sequence_type(const typet &type);
63+
5464
symbol_exprt fresh_array(
5565
const typet &type, const source_locationt &location);
5666
symbol_exprt fresh_string(

src/solvers/refinement/refined_string_type.cpp

-95
Original file line numberDiff line numberDiff line change
@@ -48,98 +48,3 @@ bool refined_string_typet::is_c_string_type(const typet &type)
4848
to_struct_type(type).get_tag()==CPROVER_PREFIX"string";
4949
}
5050

51-
/*******************************************************************\
52-
53-
Function: refined_string_typet::is_java_string_pointer_type
54-
55-
Inputs: a type
56-
57-
Outputs: Boolean telling whether the type is that of java string pointers
58-
59-
\*******************************************************************/
60-
61-
bool refined_string_typet::is_java_string_pointer_type(const typet &type)
62-
{
63-
if(type.id()==ID_pointer)
64-
{
65-
const pointer_typet &pt=to_pointer_type(type);
66-
const typet &subtype=pt.subtype();
67-
return is_java_string_type(subtype);
68-
}
69-
return false;
70-
}
71-
72-
/*******************************************************************\
73-
74-
Function: refined_string_typet::is_java_string_type
75-
76-
Inputs: a type
77-
78-
Outputs: Boolean telling whether the type is that of java string
79-
80-
\*******************************************************************/
81-
82-
bool refined_string_typet::is_java_string_type(const typet &type)
83-
{
84-
if(type.id()==ID_symbol)
85-
{
86-
irep_idt tag=to_symbol_type(type).get_identifier();
87-
return tag=="java::java.lang.String";
88-
}
89-
else if(type.id()==ID_struct)
90-
{
91-
irep_idt tag=to_struct_type(type).get_tag();
92-
return tag=="java.lang.String";
93-
}
94-
return false;
95-
}
96-
97-
/*******************************************************************\
98-
99-
Function: refined_string_typet::is_java_string_builder_type
100-
101-
Inputs: a type
102-
103-
Outputs: Boolean telling whether the type is that of java string builder
104-
105-
\*******************************************************************/
106-
107-
bool refined_string_typet::is_java_string_builder_type(const typet &type)
108-
{
109-
if(type.id()==ID_pointer)
110-
{
111-
const pointer_typet &pt=to_pointer_type(type);
112-
const typet &subtype=pt.subtype();
113-
if(subtype.id()==ID_struct)
114-
{
115-
irep_idt tag=to_struct_type(subtype).get_tag();
116-
return tag=="java.lang.StringBuilder";
117-
}
118-
}
119-
return false;
120-
}
121-
122-
/*******************************************************************\
123-
124-
Function: refined_string_typet::is_java_char_sequence_type
125-
126-
Inputs: a type
127-
128-
Outputs: Boolean telling whether the type is that of java char sequence
129-
130-
\*******************************************************************/
131-
132-
bool refined_string_typet::is_java_char_sequence_type(const typet &type)
133-
{
134-
if(type.id()==ID_pointer)
135-
{
136-
const pointer_typet &pt=to_pointer_type(type);
137-
const typet &subtype=pt.subtype();
138-
if(subtype.id()==ID_struct)
139-
{
140-
const irep_idt &tag=to_struct_type(subtype).get_tag();
141-
return tag=="java.lang.CharSequence";
142-
}
143-
}
144-
return false;
145-
}

src/solvers/refinement/refined_string_type.h

+8-28
Original file line numberDiff line numberDiff line change
@@ -33,51 +33,31 @@ class refined_string_typet: public struct_typet
3333

3434
const typet &get_char_type() const
3535
{
36-
assert(components().size()==2);
37-
return components()[0].type();
36+
return get_content_type().subtype();
3837
}
3938

4039
const typet &get_index_type() const
4140
{
42-
return get_content_type().size().type();
41+
assert(components().size()==2);
42+
return components()[0].type();
4343
}
4444

45-
// For C the unrefined string type is __CPROVER_string, for java it is a
46-
// pointer to a struct with tag java.lang.String
45+
// For C the unrefined string type is __CPROVER_string
4746

4847
static bool is_c_string_type(const typet &type);
4948

50-
static bool is_java_string_pointer_type(const typet &type);
51-
52-
static bool is_java_string_type(const typet &type);
53-
54-
static bool is_java_string_builder_type(const typet &type);
55-
56-
static bool is_java_char_sequence_type(const typet &type);
57-
58-
static bool is_unrefined_string_type(const typet &type)
59-
{
60-
return (
61-
is_c_string_type(type) ||
62-
is_java_string_pointer_type(type) ||
63-
is_java_string_builder_type(type) ||
64-
is_java_char_sequence_type(type));
65-
}
66-
67-
static bool is_unrefined_string(const exprt &expr)
68-
{
69-
return (is_unrefined_string_type(expr.type()));
70-
}
49+
static bool is_refined_string_type(const typet &type);
7150

7251
constant_exprt index_of_int(int i) const
7352
{
7453
return from_integer(i, get_index_type());
7554
}
7655
};
7756

78-
const refined_string_typet &to_refined_string_type(const typet &type)
57+
extern inline const refined_string_typet &to_refined_string_type(
58+
const typet &type)
7959
{
80-
assert(type.id()==ID_struct);
60+
assert(refined_string_typet::is_refined_string_type(type));
8161
return static_cast<const refined_string_typet &>(type);
8262
}
8363

0 commit comments

Comments
 (0)