|
15 | 15 | #include "c_types.h"
|
16 | 16 | #include "expr_util.h"
|
17 | 17 | #include "mathematical_types.h"
|
| 18 | +#include "namespace.h" |
18 | 19 | #include "pointer_offset_size.h"
|
19 | 20 | #include "simplify_expr.h"
|
20 | 21 |
|
@@ -226,3 +227,43 @@ const exprt &object_descriptor_exprt::root_object() const
|
226 | 227 |
|
227 | 228 | return *p;
|
228 | 229 | }
|
| 230 | + |
| 231 | +/// Check that the member expression has the right number of operands, refers |
| 232 | +/// to a component that exists on its underlying compound type, and uses the |
| 233 | +/// same type as is declared on that compound type. Throws or raises an |
| 234 | +/// invariant if not, according to validation mode. |
| 235 | +/// \param expr: expression to validate |
| 236 | +/// \param ns: global namespace |
| 237 | +/// \param vm: validation mode (see \ref exprt::validate) |
| 238 | +void member_exprt::validate( |
| 239 | + const exprt &expr, |
| 240 | + const namespacet &ns, |
| 241 | + const validation_modet vm) |
| 242 | +{ |
| 243 | + check(expr, vm); |
| 244 | + |
| 245 | + const auto &member_expr = to_member_expr(expr); |
| 246 | + |
| 247 | + const typet &compound_type = ns.follow(member_expr.compound().type()); |
| 248 | + const auto *struct_union_type = |
| 249 | + type_try_dynamic_cast<struct_union_typet>(compound_type); |
| 250 | + DATA_CHECK( |
| 251 | + vm, |
| 252 | + struct_union_type != nullptr, |
| 253 | + "member must address a struct, union or compatible type"); |
| 254 | + |
| 255 | + const auto &component = |
| 256 | + struct_union_type->get_component(member_expr.get_component_name()); |
| 257 | + |
| 258 | + DATA_CHECK( |
| 259 | + vm, |
| 260 | + component.is_not_nil(), |
| 261 | + "member component '" + id2string(member_expr.get_component_name()) + |
| 262 | + "' must exist on addressed type"); |
| 263 | + |
| 264 | + DATA_CHECK( |
| 265 | + vm, |
| 266 | + component.type() == member_expr.type(), |
| 267 | + "member expression's type must match the addressed struct or union " |
| 268 | + "component"); |
| 269 | +} |
0 commit comments