Skip to content

Add support for getting components by name to struct_exprt #2710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/util/std_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Author: Daniel Kroening, [email protected]

#include "std_expr.h"

#include <util/namespace.h>

#include <cassert>

#include "arith_tools.h"
Expand Down Expand Up @@ -167,3 +169,31 @@ address_of_exprt::address_of_exprt(const exprt &_op):
unary_exprt(ID_address_of, _op, pointer_type(_op.type()))
{
}

// Implementation of struct_exprt::component for const / non const overloads.
template <typename T>
auto component(T &struct_expr, const irep_idt &name, const namespacet &ns)
-> decltype(struct_expr.op0())
{
static_assert(
std::is_base_of<struct_exprt, T>::value, "T must be a struct_exprt.");
const auto index =
to_struct_type(ns.follow(struct_expr.type())).component_number(name);
DATA_INVARIANT(
index < struct_expr.operands().size(),
"component matching index should exist");
return struct_expr.operands()[index];
}

/// \return The expression for a named component of this struct.
exprt &struct_exprt::component(const irep_idt &name, const namespacet &ns)
{
return ::component(*this, name, ns);
}

/// \return The expression for a named component of this struct.
const exprt &
struct_exprt::component(const irep_idt &name, const namespacet &ns) const
{
return ::component(*this, name, ns);
}
3 changes: 3 additions & 0 deletions src/util/std_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,9 @@ class struct_exprt:public exprt
exprt(ID_struct, _type)
{
}

exprt &component(const irep_idt &name, const namespacet &ns);
const exprt &component(const irep_idt &name, const namespacet &ns) const;
};

/*! \brief Cast a generic exprt to a \ref struct_exprt
Expand Down