Skip to content

Commit ad93636

Browse files
committed
Deduplicate overloads of struct_exprt::component
This commit uses a template to implement both the const and non-const overloads of `struct_exprt::component`. This reduces the overhead of maintaining 2 identical versions and means that both overloads must be implemented in the same way.
1 parent 711c9f7 commit ad93636

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/util/std_expr.cpp

+17-8
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,30 @@ address_of_exprt::address_of_exprt(const exprt &_op):
170170
{
171171
}
172172

173+
// Implementation of struct_exprt::component for const / non const overloads.
174+
template <typename T>
175+
auto component(T &struct_expr, const irep_idt &name, const namespacet &ns)
176+
-> decltype(struct_expr.op0())
177+
{
178+
static_assert(
179+
std::is_base_of<struct_exprt, T>::value, "T must be a struct_exprt.");
180+
const auto index =
181+
to_struct_type(ns.follow(struct_expr.type())).component_number(name);
182+
DATA_INVARIANT(
183+
index < struct_expr.operands().size(),
184+
"component matching index should exist");
185+
return struct_expr.operands()[index];
186+
}
187+
173188
/// \return The expression for a named component of this struct.
174189
exprt &struct_exprt::component(const irep_idt &name, const namespacet &ns)
175190
{
176-
const auto index = to_struct_type(ns.follow(type())).component_number(name);
177-
DATA_INVARIANT(
178-
index < operands().size(), "component matching index should exist");
179-
return operands()[index];
191+
return ::component(*this, name, ns);
180192
}
181193

182194
/// \return The expression for a named component of this struct.
183195
const exprt &
184196
struct_exprt::component(const irep_idt &name, const namespacet &ns) const
185197
{
186-
const auto index = to_struct_type(ns.follow(type())).component_number(name);
187-
DATA_INVARIANT(
188-
index < operands().size(), "component matching index should exist");
189-
return operands()[index];
198+
return ::component(*this, name, ns);
190199
}

0 commit comments

Comments
 (0)