File: struct_builder.cpp

package info (click to toggle)
cbmc 6.6.0-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,852 kB
  • sloc: cpp: 386,459; ansic: 114,466; java: 28,405; python: 6,003; yacc: 4,552; makefile: 4,041; lex: 2,487; xml: 2,388; sh: 2,050; perl: 557; pascal: 184; javascript: 163; ada: 36
file content (92 lines) | stat: -rw-r--r-- 2,649 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*******************************************************************\

 Module: Unit tests helpers for structs

 Author: Jez Higgins, jez@jezuk.co.uk

\*******************************************************************/
#include <analyses/variable-sensitivity/full_struct_abstract_object/struct_builder.h>
#include <util/arith_tools.h>
#include <util/mathematical_types.h>

full_struct_abstract_objectt::constant_struct_pointert build_struct(
  const struct_exprt &starting_value,
  abstract_environmentt &environment,
  const namespacet &ns)
{
  auto result = std::make_shared<const full_struct_abstract_objectt>(
    starting_value, environment, ns);

  auto struct_type = to_struct_type(starting_value.type());
  size_t comp_index = 0;

  auto nil_with_type = nil_exprt();
  nil_with_type.type() = struct_type;

  for(const exprt &op : starting_value.operands())
  {
    const auto &component = struct_type.components()[comp_index];
    auto new_result = result->write(
      environment,
      ns,
      std::stack<exprt>(),
      member_exprt(nil_with_type, component.get_name(), component.type()),
      environment.eval(op, ns),
      false);

    result =
      std::dynamic_pointer_cast<const full_struct_abstract_objectt>(new_result);

    ++comp_index;
  }

  return result;
}

full_struct_abstract_objectt::constant_struct_pointert build_struct(
  const std::map<std::string, int> &members,
  abstract_environmentt &environment,
  const namespacet &ns)
{
  struct_typet struct_type;
  exprt::operandst val1_op;

  for(auto &member : members)
  {
    auto component =
      struct_union_typet::componentt(member.first, integer_typet());
    struct_type.components().push_back(component);

    if(member.second != TOP_MEMBER)
      val1_op.push_back(from_integer(member.second, integer_typet()));
    else
      val1_op.push_back(nil_exprt());
  }

  auto val1 = struct_exprt(val1_op, struct_type);
  return build_struct(val1, environment, ns);
}

full_struct_abstract_objectt::constant_struct_pointert build_top_struct()
{
  struct_typet struct_type;
  return std::make_shared<full_struct_abstract_objectt>(
    struct_type, true, false);
}

full_struct_abstract_objectt::constant_struct_pointert build_bottom_struct()
{
  struct_typet struct_type;
  return std::make_shared<full_struct_abstract_objectt>(
    struct_type, false, true);
}

exprt read_component(
  full_struct_abstract_objectt::constant_struct_pointert struct_object,
  const member_exprt &component,
  const abstract_environmentt &environment,
  const namespacet &ns)
{
  return struct_object->expression_transform(component, {}, environment, ns)
    ->to_constant();
}