|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: analyses variable-sensitivity |
| 4 | +
|
| 5 | + Author: Jez Higgins, [email protected] |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +/// \file |
| 10 | +/// Common behaviour for abstract objects modelling aggregate values - |
| 11 | +/// arrays, structs, etc. |
| 12 | +#ifndef CBMC_ABSTRACT_AGGREGATE_OBJECT_H |
| 13 | +#define CBMC_ABSTRACT_AGGREGATE_OBJECT_H |
| 14 | + |
| 15 | +#include <analyses/variable-sensitivity/abstract_environment.h> |
| 16 | +#include <analyses/variable-sensitivity/abstract_object.h> |
| 17 | +#include <stack> |
| 18 | + |
| 19 | +class abstract_aggregate_tag |
| 20 | +{ |
| 21 | +}; |
| 22 | + |
| 23 | +template <class aggregate_typet, class aggregate_traitst> |
| 24 | +class abstract_aggregate_objectt : public abstract_objectt, |
| 25 | + public abstract_aggregate_tag |
| 26 | +{ |
| 27 | +public: |
| 28 | + explicit abstract_aggregate_objectt(const typet &type) |
| 29 | + : abstract_objectt(type) |
| 30 | + { |
| 31 | + PRECONDITION(type.id() == aggregate_traitst::TYPE_ID()); |
| 32 | + } |
| 33 | + |
| 34 | + abstract_aggregate_objectt(const typet &type, bool tp, bool bttm) |
| 35 | + : abstract_objectt(type, tp, bttm) |
| 36 | + { |
| 37 | + PRECONDITION(type.id() == aggregate_traitst::TYPE_ID()); |
| 38 | + } |
| 39 | + |
| 40 | + abstract_aggregate_objectt( |
| 41 | + const exprt &expr, |
| 42 | + const abstract_environmentt &environment, |
| 43 | + const namespacet &ns) |
| 44 | + : abstract_objectt(expr, environment, ns) |
| 45 | + { |
| 46 | + PRECONDITION(ns.follow(expr.type()).id() == aggregate_traitst::TYPE_ID()); |
| 47 | + } |
| 48 | + |
| 49 | + abstract_object_pointert expression_transform( |
| 50 | + const exprt &expr, |
| 51 | + const std::vector<abstract_object_pointert> &operands, |
| 52 | + const abstract_environmentt &environment, |
| 53 | + const namespacet &ns) const override |
| 54 | + { |
| 55 | + if(expr.id() == aggregate_traitst::ACCESS_EXPR_ID()) |
| 56 | + return read_component(environment, expr, ns); |
| 57 | + |
| 58 | + return abstract_objectt::expression_transform( |
| 59 | + expr, operands, environment, ns); |
| 60 | + } |
| 61 | + |
| 62 | + abstract_object_pointert write( |
| 63 | + abstract_environmentt &environment, |
| 64 | + const namespacet &ns, |
| 65 | + const std::stack<exprt> &stack, |
| 66 | + const exprt &specifier, |
| 67 | + const abstract_object_pointert &value, |
| 68 | + bool merging_write) const override |
| 69 | + { |
| 70 | + return write_component( |
| 71 | + environment, ns, stack, specifier, value, merging_write); |
| 72 | + } |
| 73 | + |
| 74 | + void get_statistics( |
| 75 | + abstract_object_statisticst &statistics, |
| 76 | + abstract_object_visitedt &visited, |
| 77 | + const abstract_environmentt &env, |
| 78 | + const namespacet &ns) const override |
| 79 | + { |
| 80 | + abstract_objectt::get_statistics(statistics, visited, env, ns); |
| 81 | + aggregate_traitst::get_statistics(statistics, visited, env, ns); |
| 82 | + this->statistics(statistics, visited, env, ns); |
| 83 | + } |
| 84 | + |
| 85 | +protected: |
| 86 | + virtual abstract_object_pointert read_component( |
| 87 | + const abstract_environmentt &environment, |
| 88 | + const exprt &expr, |
| 89 | + const namespacet &ns) const |
| 90 | + { |
| 91 | + // If we are bottom then so are the components |
| 92 | + // otherwise the components could be anything |
| 93 | + return environment.abstract_object_factory( |
| 94 | + aggregate_traitst::read_type(expr.type(), type()), |
| 95 | + ns, |
| 96 | + !is_bottom(), |
| 97 | + is_bottom()); |
| 98 | + } |
| 99 | + |
| 100 | + virtual abstract_object_pointert write_component( |
| 101 | + abstract_environmentt &environment, |
| 102 | + const namespacet &ns, |
| 103 | + const std::stack<exprt> &stack, |
| 104 | + const exprt &expr, |
| 105 | + const abstract_object_pointert &value, |
| 106 | + bool merging_write) const |
| 107 | + { |
| 108 | + if(is_top() || is_bottom()) |
| 109 | + { |
| 110 | + return shared_from_this(); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + return std::make_shared<aggregate_typet>(type(), true, false); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + virtual void statistics( |
| 119 | + abstract_object_statisticst &statistics, |
| 120 | + abstract_object_visitedt &visited, |
| 121 | + const abstract_environmentt &env, |
| 122 | + const namespacet &ns) const = 0; |
| 123 | + |
| 124 | + template <class keyt, typename hash> |
| 125 | + static bool merge_shared_maps( |
| 126 | + const sharing_mapt<keyt, abstract_object_pointert, false, hash> &map1, |
| 127 | + const sharing_mapt<keyt, abstract_object_pointert, false, hash> &map2, |
| 128 | + sharing_mapt<keyt, abstract_object_pointert, false, hash> &out_map) |
| 129 | + { |
| 130 | + bool modified = false; |
| 131 | + |
| 132 | + typename sharing_mapt<keyt, abstract_object_pointert, false, hash>:: |
| 133 | + delta_viewt delta_view; |
| 134 | + map1.get_delta_view(map2, delta_view, true); |
| 135 | + |
| 136 | + for(auto &item : delta_view) |
| 137 | + { |
| 138 | + bool changes = false; |
| 139 | + abstract_object_pointert v_new = |
| 140 | + abstract_objectt::merge(item.m, item.get_other_map_value(), changes); |
| 141 | + if(changes) |
| 142 | + { |
| 143 | + modified = true; |
| 144 | + out_map.replace(item.k, v_new); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return modified; |
| 149 | + } |
| 150 | +}; |
| 151 | + |
| 152 | +struct array_aggregate_typet |
| 153 | +{ |
| 154 | + static const irep_idt TYPE_ID() |
| 155 | + { |
| 156 | + return ID_array; |
| 157 | + } |
| 158 | + static const irep_idt ACCESS_EXPR_ID() |
| 159 | + { |
| 160 | + return ID_index; |
| 161 | + } |
| 162 | + static typet read_type(const typet &, const typet &object_type) |
| 163 | + { |
| 164 | + array_typet array_type(to_array_type(object_type)); |
| 165 | + return array_type.subtype(); |
| 166 | + } |
| 167 | + |
| 168 | + static void get_statistics( |
| 169 | + abstract_object_statisticst &statistics, |
| 170 | + abstract_object_visitedt &visited, |
| 171 | + const abstract_environmentt &env, |
| 172 | + const namespacet &ns) |
| 173 | + { |
| 174 | + ++statistics.number_of_arrays; |
| 175 | + } |
| 176 | +}; |
| 177 | + |
| 178 | +struct struct_aggregate_typet |
| 179 | +{ |
| 180 | + static const irep_idt &TYPE_ID() |
| 181 | + { |
| 182 | + return ID_struct; |
| 183 | + } |
| 184 | + static const irep_idt &ACCESS_EXPR_ID() |
| 185 | + { |
| 186 | + return ID_member; |
| 187 | + } |
| 188 | + static const typet &read_type(const typet &expr_type, const typet &) |
| 189 | + { |
| 190 | + return expr_type; |
| 191 | + } |
| 192 | + |
| 193 | + static void get_statistics( |
| 194 | + abstract_object_statisticst &statistics, |
| 195 | + abstract_object_visitedt &visited, |
| 196 | + const abstract_environmentt &env, |
| 197 | + const namespacet &ns) |
| 198 | + { |
| 199 | + ++statistics.number_of_structs; |
| 200 | + } |
| 201 | +}; |
| 202 | + |
| 203 | +struct union_aggregate_typet |
| 204 | +{ |
| 205 | + static const irep_idt TYPE_ID() |
| 206 | + { |
| 207 | + return ID_union; |
| 208 | + } |
| 209 | + static const irep_idt ACCESS_EXPR_ID() |
| 210 | + { |
| 211 | + return ID_member; |
| 212 | + } |
| 213 | + static typet read_type(const typet &, const typet &object_type) |
| 214 | + { |
| 215 | + return object_type; |
| 216 | + } |
| 217 | + |
| 218 | + static void get_statistics( |
| 219 | + abstract_object_statisticst &statistics, |
| 220 | + abstract_object_visitedt &visited, |
| 221 | + const abstract_environmentt &env, |
| 222 | + const namespacet &ns) |
| 223 | + { |
| 224 | + } |
| 225 | +}; |
| 226 | + |
| 227 | +#endif //CBMC_ABSTRACT_AGGREGATE_OBJECT_H |
0 commit comments