CBMC
boolbv_let.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "boolbv.h"
10 
11 #include <util/range.h>
12 #include <util/replace_symbol.h>
13 #include <util/std_expr.h>
14 
16 {
17  const auto &variables = expr.variables();
18  const auto &values = expr.values();
19 
21  expr.type() == expr.where().type(),
22  "let must have the type of the 'where' operand");
23 
24  // Check the types.
25  for(auto &binding : make_range(variables).zip(values))
26  {
28  binding.first.type() == binding.second.type(),
29  "let value must have the type of the let symbol");
30  }
31 
32  // A let expression can bind multiple symbols simultaneously.
33  // This is a 'concurrent' binding, i.e., the symbols are not yet
34  // visible when evaluating the values. SMT-LIB also has this
35  // semantics. We therefore first convert all values,
36  // and only then assign them.
37  std::vector<bvt> converted_values;
38  converted_values.reserve(variables.size());
39 
40  for(auto &value : values)
41  {
42  if(!bv_width.get_width_opt(value.type()).has_value())
43  converted_values.emplace_back();
44  else
45  converted_values.push_back(convert_bv(value));
46  }
47 
48  // get fresh bound symbols
49  auto fresh_variables = fresh_binding(expr.binding());
50 
51  // Now assign
52  for(const auto &binding : make_range(fresh_variables).zip(converted_values))
53  {
54  const auto &identifier = binding.first.get_identifier();
55 
56  // make the symbol visible
57  if(binding.first.is_boolean())
58  {
59  DATA_INVARIANT(binding.second.size() == 1, "boolean values have one bit");
60  symbols[identifier] = binding.second[0];
61  }
62  else
63  map.set_literals(identifier, binding.first.type(), binding.second);
64  }
65 
66  // for renaming the bound symbols
67  replace_symbolt replace_symbol;
68 
69  for(const auto &pair : make_range(variables).zip(fresh_variables))
70  replace_symbol.insert(pair.first, pair.second);
71 
72  // rename the bound symbols in 'where'
73  exprt where_renamed = expr.where();
74  replace_symbol(where_renamed);
75 
76  // recursive call
77  bvt result_bv = convert_bv(where_renamed);
78 
79  // the mapping can now be deleted
80  for(const auto &entry : fresh_variables)
81  {
82  const auto &type = entry.type();
83  if(type.id() == ID_bool)
84  symbols.erase(entry.get_identifier());
85  else
86  map.erase_literals(entry.get_identifier(), type);
87  }
88 
89  return result_bv;
90 }
void erase_literals(const irep_idt &identifier, const typet &type)
Definition: boolbv_map.cpp:118
void set_literals(const irep_idt &identifier, const typet &type, const bvt &literals)
Definition: boolbv_map.cpp:75
virtual std::optional< std::size_t > get_width_opt(const typet &type) const
Definition: boolbv_width.h:31
virtual const bvt & convert_bv(const exprt &expr, const std::optional< std::size_t > expected_width={})
Convert expression to vector of literalts, using an internal cache to speed up conversion if availabl...
Definition: boolbv.cpp:39
virtual bvt convert_let(const let_exprt &)
Definition: boolbv_let.cpp:15
binding_exprt::variablest fresh_binding(const binding_exprt &)
create new, unique variables for the given binding
Definition: boolbv.cpp:552
boolbv_widtht bv_width
Definition: boolbv.h:116
boolbv_mapt map
Definition: boolbv.h:123
Base class for all expressions.
Definition: expr.h:56
typet & type()
Return the type of the expression.
Definition: expr.h:84
A let expression.
Definition: std_expr.h:3204
exprt & where()
convenience accessor for binding().where()
Definition: std_expr.h:3297
binding_exprt::variablest & variables()
convenience accessor for binding().variables()
Definition: std_expr.h:3285
binding_exprt & binding()
Definition: std_expr.h:3232
operandst & values()
Definition: std_expr.h:3274
Replace a symbol expression by a given expression.
void insert(const class symbol_exprt &old_expr, const exprt &new_expr)
Sets old_expr to be replaced by new_expr if we don't already have a replacement; otherwise does nothi...
std::vector< literalt > bvt
Definition: literal.h:201
Ranges: pair of begin and end iterators, which can be initialized from containers,...
ranget< iteratort > make_range(iteratort begin, iteratort end)
Definition: range.h:522
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:534
API to expression classes.