CBMC
string_expr.h
Go to the documentation of this file.
1 /******************************************************************\
2 
3 Module: String expressions for the string solver
4 
5 Author: Romain Brenguier, romain.brenguier@diffblue.com
6 
7 \*******************************************************************/
8 
11 
12 #ifndef CPROVER_UTIL_STRING_EXPR_H
13 #define CPROVER_UTIL_STRING_EXPR_H
14 
15 #include "arith_tools.h"
16 #include "refined_string_type.h"
17 #include "std_expr.h"
18 
20 {
21  PRECONDITION(rhs.type() == lhs.type());
22  return binary_relation_exprt(std::move(lhs), ID_ge, std::move(rhs));
23 }
24 
26 {
27  PRECONDITION(rhs.type() == lhs.type());
28  return binary_relation_exprt(std::move(rhs), ID_lt, std::move(lhs));
29 }
30 
32 {
33  return binary_relation_exprt(from_integer(i, lhs.type()), ID_lt, lhs);
34 }
35 
37 {
38  PRECONDITION(rhs.type() == lhs.type());
39  return binary_relation_exprt(std::move(lhs), ID_le, std::move(rhs));
40 }
41 
44 {
45  return binary_relation_exprt(lhs, ID_le, from_integer(i, lhs.type()));
46 }
47 
49 {
50  PRECONDITION(rhs.type() == lhs.type());
51  return binary_relation_exprt(std::move(lhs), ID_lt, std::move(rhs));
52 }
53 
54 inline equal_exprt equal_to(exprt lhs, exprt rhs)
55 {
56  PRECONDITION(rhs.type() == lhs.type());
57  return equal_exprt(std::move(lhs), std::move(rhs));
58 }
59 
60 inline equal_exprt equal_to(const exprt &lhs, mp_integer i)
61 {
62  return equal_exprt(lhs, from_integer(i, lhs.type()));
63 }
64 
65 // Representation of strings as arrays
66 class array_string_exprt : public exprt
67 {
68 public:
69  const typet &length_type() const
70  {
71  return to_array_type(type()).size().type();
72  }
73 
75  {
76  return *this;
77  }
78 
79  const exprt &content() const
80  {
81  return *this;
82  }
83 
84  exprt operator[](const exprt &i) const
85  {
86  return index_exprt(content(), i);
87  }
88 
89  index_exprt operator[](int i) const
90  {
92  }
93 };
94 
96 {
97  PRECONDITION(expr.type().id() == ID_array);
98  return static_cast<array_string_exprt &>(expr);
99 }
100 
102 {
103  PRECONDITION(expr.type().id() == ID_array);
104  return static_cast<const array_string_exprt &>(expr);
105 }
106 
107 // Represent strings as a struct with a length field and a content field
109 {
110 public:
112  const exprt &_length,
113  const exprt &_content,
114  const typet &type)
115  : struct_exprt({_length, _content}, type)
116  {
117  }
118 
119  refined_string_exprt(const exprt &_length, const exprt &_content)
121  _length,
122  _content,
123  refined_string_typet(_length.type(), to_pointer_type(_content.type())))
124  {
125  }
126 
127  // Expression corresponding to the length of the string
128  const exprt &length() const
129  {
130  return op0();
131  }
133  {
134  return op0();
135  }
136 
137  // Expression corresponding to the content (array of characters) of the string
138  const exprt &content() const
139  {
140  return op1();
141  }
143  {
144  return op1();
145  }
146 
147  friend inline refined_string_exprt &to_string_expr(exprt &expr);
148 };
149 
151 {
152  PRECONDITION(expr.id()==ID_struct);
153  PRECONDITION(expr.operands().size()==2);
154  return static_cast<refined_string_exprt &>(expr);
155 }
156 
157 inline const refined_string_exprt &to_string_expr(const exprt &expr)
158 {
159  PRECONDITION(expr.id()==ID_struct);
160  PRECONDITION(expr.operands().size()==2);
161  return static_cast<const refined_string_exprt &>(expr);
162 }
163 
164 template <>
166 {
167  return base.id() == ID_struct && base.operands().size() == 2 &&
169 }
170 
171 inline void validate_expr(const refined_string_exprt &x)
172 {
173  INVARIANT(x.id() == ID_struct, "refined string exprs are struct");
174  validate_operands(x, 2, "refined string expr has length and content fields");
175  INVARIANT(
176  x.length().type().id() == ID_signedbv, "length should be a signed int");
177  INVARIANT(x.content().type().id() == ID_array, "content should be an array");
178 }
179 
180 #endif
array_string_exprt::length_type
const typet & length_type() const
Definition: string_expr.h:69
array_string_exprt::operator[]
exprt operator[](const exprt &i) const
Definition: string_expr.h:84
mp_integer
BigInt mp_integer
Definition: smt_terms.h:17
greater_or_equal_to
binary_relation_exprt greater_or_equal_to(exprt lhs, exprt rhs)
Definition: string_expr.h:19
arith_tools.h
refined_string_exprt::refined_string_exprt
refined_string_exprt(const exprt &_length, const exprt &_content, const typet &type)
Definition: string_expr.h:111
to_string_expr
refined_string_exprt & to_string_expr(exprt &expr)
Definition: string_expr.h:150
typet
The type of an expression, extends irept.
Definition: type.h:28
validate_operands
void validate_operands(const exprt &value, exprt::operandst::size_type number, const char *message, bool allow_more=false)
Definition: expr_cast.h:250
refined_string_typet
Definition: refined_string_type.h:26
validate_expr
void validate_expr(const refined_string_exprt &x)
Definition: string_expr.h:171
exprt
Base class for all expressions.
Definition: expr.h:55
array_string_exprt::content
exprt & content()
Definition: string_expr.h:74
can_cast_expr< refined_string_exprt >
bool can_cast_expr< refined_string_exprt >(const exprt &base)
Definition: string_expr.h:165
array_string_exprt::content
const exprt & content() const
Definition: string_expr.h:79
equal_exprt
Equality.
Definition: std_expr.h:1305
refined_string_exprt
Definition: string_expr.h:108
refined_string_exprt::length
const exprt & length() const
Definition: string_expr.h:128
struct_exprt
Struct constructor from list of elements.
Definition: std_expr.h:1818
array_typet::size
const exprt & size() const
Definition: std_types.h:800
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:84
multi_ary_exprt::op1
exprt & op1()
Definition: std_expr.h:883
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
less_than_or_equal_to
binary_relation_exprt less_than_or_equal_to(exprt lhs, exprt rhs)
Definition: string_expr.h:36
to_pointer_type
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: pointer_expr.h:79
refined_string_exprt::refined_string_exprt
refined_string_exprt(const exprt &_length, const exprt &_content)
Definition: string_expr.h:119
irept::id
const irep_idt & id() const
Definition: irep.h:396
to_array_string_expr
array_string_exprt & to_array_string_expr(exprt &expr)
Definition: string_expr.h:95
refined_string_exprt::content
exprt & content()
Definition: string_expr.h:142
refined_string_exprt::content
const exprt & content() const
Definition: string_expr.h:138
refined_string_type.h
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:100
binary_relation_exprt
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition: std_expr.h:706
refined_string_exprt::length
exprt & length()
Definition: string_expr.h:132
to_array_type
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:844
is_refined_string_type
bool is_refined_string_type(const typet &type)
Definition: refined_string_type.h:52
equal_to
equal_exprt equal_to(exprt lhs, exprt rhs)
Definition: string_expr.h:54
exprt::operands
operandst & operands()
Definition: expr.h:94
index_exprt
Array index operator.
Definition: std_expr.h:1409
INVARIANT
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
greater_than
binary_relation_exprt greater_than(exprt lhs, exprt rhs)
Definition: string_expr.h:25
array_string_exprt::operator[]
index_exprt operator[](int i) const
Definition: string_expr.h:89
multi_ary_exprt::op0
exprt & op0()
Definition: std_expr.h:877
std_expr.h
less_than
binary_relation_exprt less_than(exprt lhs, exprt rhs)
Definition: string_expr.h:48
array_string_exprt
Definition: string_expr.h:66
refined_string_exprt::to_string_expr
friend refined_string_exprt & to_string_expr(exprt &expr)
Definition: string_expr.h:150