CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
string_constraint_generator_code_points.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Generates string constraints for Java functions dealing with
4 code points
5
6Author: Romain Brenguier, romain.brenguier@diffblue.com
7
8\*******************************************************************/
9
12
14
16
22std::pair<exprt, string_constraintst>
25 const exprt &code_point)
26{
27 string_constraintst constraints;
28 const typet &char_type = to_type_with_subtype(res.content().type()).subtype();
29 const typet &type = code_point.type();
30 PRECONDITION(type.id() == ID_signedbv);
31
32 // We add axioms:
33 // a1 : code_point<0x010000 => |res|=1
34 // a2 : code_point>=0x010000 => |res|=2
35 // a3 : code_point<0x010000 => res[0]=code_point
36 // a4 : code_point>=0x010000 => res[0]=0xD800+(code_point-0x10000)/0x0400
37 // a5 : code_point>=0x010000 => res[1]=0xDC00+(code_point-0x10000)/0x0400
38 // For more explenations about this conversion, see:
39 // https://en.wikipedia.org/wiki/UTF-16
40
41 exprt hex010000 = from_integer(0x010000, type);
42 exprt hexD800 = from_integer(0xD800, type);
43 exprt hexDC00 = from_integer(0xDC00, type);
44 exprt hex0400 = from_integer(0x0400, type);
45
48 constraints.existential.push_back(a1);
49
52 constraints.existential.push_back(a2);
53
56 constraints.existential.push_back(a3);
57
63 constraints.existential.push_back(a4);
64
69 constraints.existential.push_back(a5);
70
71 return {from_integer(0, get_return_code_type()), constraints};
72}
73
81{
82 return and_exprt(
85}
86
94{
95 return and_exprt(
98}
99
110{
111 exprt hex010000 = from_integer(0x010000, return_type);
112 exprt hex0800 = from_integer(0x0800, return_type);
113 exprt hex0400 = from_integer(0x0400, return_type);
117}
118
123std::pair<exprt, string_constraintst>
126{
127 string_constraintst constraints;
128 const typet &return_type = f.type();
129 PRECONDITION(return_type.id() == ID_signedbv);
130 PRECONDITION(f.arguments().size() == 2);
132 const exprt &pos = f.arguments()[1];
133
134 const symbol_exprt result = fresh_symbol("char", return_type);
135 const exprt index1 = from_integer(1, str.length_type());
136 const exprt &char1 = str[pos];
137 const exprt &char2 = str[plus_exprt(pos, index1)];
138 const typecast_exprt char1_as_int(char1, return_type);
139 const typecast_exprt char2_as_int(char2, return_type);
140 const exprt pair = pair_value(char1_as_int, char2_as_int, return_type);
143
144 constraints.existential.push_back(
146 constraints.existential.push_back(
148 return {result, constraints};
149}
150
155std::pair<exprt, string_constraintst>
158{
160 PRECONDITION(args.size() == 2);
161 typet return_type = f.type();
162 PRECONDITION(return_type.id() == ID_signedbv);
163 symbol_exprt result = fresh_symbol("char", return_type);
165 string_constraintst constraints;
166
167 const exprt &char1 = str[minus_exprt(
168 args[1], from_integer(2, array_pool.get_or_create_length(str).type()))];
169 const exprt &char2 = str[minus_exprt(
170 args[1], from_integer(1, array_pool.get_or_create_length(str).type()))];
171 const typecast_exprt char1_as_int(char1, return_type);
172 const typecast_exprt char2_as_int(char2, return_type);
173
174 const exprt pair = pair_value(char1_as_int, char2_as_int, return_type);
177
178 constraints.existential.push_back(
180 constraints.existential.push_back(
182 return {result, constraints};
183}
184
190std::pair<exprt, string_constraintst>
193{
194 PRECONDITION(f.arguments().size() == 3);
195 string_constraintst constraints;
196 const exprt &begin = f.arguments()[1];
197 const exprt &end = f.arguments()[2];
198 const typet &return_type = f.type();
199 const symbol_exprt result = fresh_symbol("code_point_count", return_type);
200 const minus_exprt length(end, begin);
201 const div_exprt minimum(length, from_integer(2, length.type()));
202 constraints.existential.push_back(
203 binary_relation_exprt(result, ID_le, length));
204 constraints.existential.push_back(
206
207 return {result, constraints};
208}
209
216std::pair<exprt, string_constraintst>
219{
220 PRECONDITION(f.arguments().size() == 3);
221 string_constraintst constraints;
222 const exprt &index = f.arguments()[1];
223 const exprt &offset = f.arguments()[2];
224 const typet &return_type = f.type();
225 const symbol_exprt result = fresh_symbol("offset_by_code_point", return_type);
226
227 const exprt minimum = plus_exprt(index, offset);
228 const exprt maximum = plus_exprt(minimum, offset);
229 constraints.existential.push_back(
231 constraints.existential.push_back(
233
234 return {result, constraints};
235}
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
array_string_exprt get_string_expr(array_poolt &array_pool, const exprt &expr)
Fetch the string_exprt corresponding to the given refined_string_exprt.
bitvector_typet char_type()
Definition c_types.cpp:106
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
Boolean AND.
Definition std_expr.h:2125
exprt get_or_create_length(const array_string_exprt &s)
Get the length of an array_string_exprt from the array_pool.
const typet & length_type() const
Definition string_expr.h:70
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition std_expr.h:762
Division.
Definition std_expr.h:1157
Equality.
Definition std_expr.h:1366
Base class for all expressions.
Definition expr.h:56
typet & type()
Return the type of the expression.
Definition expr.h:84
Application of (mathematical) function.
Boolean implication.
Definition std_expr.h:2225
const irep_idt & id() const
Definition irep.h:388
Binary minus.
Definition std_expr.h:1061
Modulo defined as lhs-(rhs * truncate(lhs/rhs)).
Definition std_expr.h:1228
Binary multiplication Associativity is not specified.
Definition std_expr.h:1107
Boolean negation.
Definition std_expr.h:2454
The plus expression Associativity is not specified.
Definition std_expr.h:1002
std::pair< exprt, string_constraintst > add_axioms_for_offset_by_code_point(const function_application_exprt &f)
Add axioms corresponding the String.offsetByCodePointCount java function.
std::pair< exprt, string_constraintst > add_axioms_for_code_point(const array_string_exprt &res, const exprt &code_point)
add axioms for the conversion of an integer representing a java code point to a utf-16 string
std::pair< exprt, string_constraintst > add_axioms_for_code_point_count(const function_application_exprt &f)
Add axioms corresponding the String.codePointCount java function.
std::pair< exprt, string_constraintst > add_axioms_for_code_point_at(const function_application_exprt &f)
add axioms corresponding to the String.codePointAt java function
std::pair< exprt, string_constraintst > add_axioms_for_code_point_before(const function_application_exprt &f)
add axioms corresponding to the String.codePointBefore java function
Expression to hold a symbol (variable)
Definition std_expr.h:131
Semantic type conversion.
Definition std_expr.h:2073
The type of an expression, extends irept.
Definition type.h:29
literalt pos(literalt a)
Definition literal.h:194
API to expression classes for 'mathematical' expressions.
#define PRECONDITION(CONDITION)
Definition invariant.h:463
Generates string constraints to link results from string functions with their arguments.
exprt maximum(const exprt &a, const exprt &b)
exprt minimum(const exprt &a, const exprt &b)
signedbv_typet get_return_code_type()
static exprt is_low_surrogate(const exprt &chr)
the output is true when the character is a low surrogate for UTF-16 encoding, see https://en....
static exprt is_high_surrogate(const exprt &chr)
the output is true when the character is a high surrogate for UTF-16 encoding, see https://en....
exprt pair_value(exprt char1, exprt char2, typet return_type)
the output corresponds to the unicode character given by the pair of characters of inputs assuming it...
equal_exprt equal_to(exprt lhs, exprt rhs)
Definition string_expr.h:55
Collection of constraints of different types: existential formulas, universal formulas,...
std::vector< exprt > existential
const type_with_subtypet & to_type_with_subtype(const typet &type)
Definition type.h:208