From 1f2102ccf07255fbcac4da63f5ee0d719556060f Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Tue, 2 Jan 2018 14:01:48 +0000 Subject: [PATCH] Add code_typet::get_this This simple utility saves a double-lookup compared to has_this followed by parameters()[0]. --- src/goto-programs/remove_virtual_functions.cpp | 5 +++-- src/util/std_types.h | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/goto-programs/remove_virtual_functions.cpp b/src/goto-programs/remove_virtual_functions.cpp index d4bc305929b..58864e11e2d 100644 --- a/src/goto-programs/remove_virtual_functions.cpp +++ b/src/goto-programs/remove_virtual_functions.cpp @@ -181,10 +181,11 @@ void remove_virtual_functionst::remove_virtual_function( // Cast the `this` pointer to the correct type for the new callee: const auto &callee_type= to_code_type(ns.lookup(fun.symbol_expr.get_identifier()).type); + const code_typet::parametert *this_param = callee_type.get_this(); INVARIANT( - callee_type.has_this(), + this_param != nullptr, "Virtual function callees must have a `this` argument"); - typet need_type=callee_type.parameters()[0].type(); + typet need_type=this_param->type(); if(!type_eq(newcall.arguments()[0].type(), need_type, ns)) newcall.arguments()[0].make_typecast(need_type); } diff --git a/src/util/std_types.h b/src/util/std_types.h index 726c210e52e..71fe3511606 100644 --- a/src/util/std_types.h +++ b/src/util/std_types.h @@ -817,9 +817,17 @@ class code_typet:public typet } bool has_this() const + { + return get_this() != nullptr; + } + + const parametert *get_this() const { const parameterst &p=parameters(); - return !p.empty() && p.front().get_this(); + if(!p.empty() && p.front().get_this()) + return &p.front(); + else + return nullptr; } bool is_KnR() const