Skip to content

Commit 1f2102c

Browse files
committed
Add code_typet::get_this
This simple utility saves a double-lookup compared to has_this followed by parameters()[0].
1 parent a22dd1c commit 1f2102c

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/goto-programs/remove_virtual_functions.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,11 @@ void remove_virtual_functionst::remove_virtual_function(
181181
// Cast the `this` pointer to the correct type for the new callee:
182182
const auto &callee_type=
183183
to_code_type(ns.lookup(fun.symbol_expr.get_identifier()).type);
184+
const code_typet::parametert *this_param = callee_type.get_this();
184185
INVARIANT(
185-
callee_type.has_this(),
186+
this_param != nullptr,
186187
"Virtual function callees must have a `this` argument");
187-
typet need_type=callee_type.parameters()[0].type();
188+
typet need_type=this_param->type();
188189
if(!type_eq(newcall.arguments()[0].type(), need_type, ns))
189190
newcall.arguments()[0].make_typecast(need_type);
190191
}

src/util/std_types.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,9 +817,17 @@ class code_typet:public typet
817817
}
818818

819819
bool has_this() const
820+
{
821+
return get_this() != nullptr;
822+
}
823+
824+
const parametert *get_this() const
820825
{
821826
const parameterst &p=parameters();
822-
return !p.empty() && p.front().get_this();
827+
if(!p.empty() && p.front().get_this())
828+
return &p.front();
829+
else
830+
return nullptr;
823831
}
824832

825833
bool is_KnR() const

0 commit comments

Comments
 (0)