Go to the documentation of this file.
45 typedef std::function<
104 const auto &callee_parameters =
109 callee_parameters.size() == call_args.size(),
110 "function overrides must have matching argument counts");
112 for(std::size_t i = 0; i < call_args.size(); ++i)
114 const typet &need_type = callee_parameters[i].type();
116 if(call_args[i].type() != need_type)
122 call_args[i].type().
id() == ID_pointer,
123 "where overriding function argument types differ, "
124 "those arguments must be pointer-typed");
145 const exprt &argument_for_this,
152 instr_it = std::prev(instr_it);
154 if(instr_it->type ==
ASSERT)
159 if(instr_it->type !=
ASSUME)
164 exprt guard = instr_it->get_condition();
166 bool changed =
false;
170 if(*expr_it == argument_for_this)
172 expr_it.mutate() = temp_var_for_this;
180 checks_directly_preceding_function_call.
instructions.cbegin(),
185 return checks_directly_preceding_function_call;
203 exprt &argument_for_this,
214 argument_for_this.
type(),
222 new_code_for_this_argument.
add(
224 new_code_for_this_argument.
add(
226 temp_var_for_this, argument_for_this, vcall_source_loc));
230 goto_program, target, argument_for_this, temp_var_for_this);
233 checks_directly_preceding_function_call);
235 argument_for_this = temp_var_for_this;
266 target->is_function_call(),
267 "remove_virtual_function must target a FUNCTION_CALL instruction");
272 if(functions.empty())
274 target->turn_into_skip();
280 if(functions.size()==1 &&
283 if(!functions.front().symbol_expr.has_value())
285 target->turn_into_skip();
290 auto c = target->get_function_call();
292 target->set_function_call(c);
297 const auto &vcall_source_loc=target->source_location;
309 new_code_for_this_argument);
325 this_expr.
type().
id() == ID_pointer,
"this parameter must be a pointer");
328 "this parameter must not be a void pointer");
332 exprt this_class_identifier =
344 INVARIANT(!functions.empty(),
"Function dispatch table cannot be empty.");
345 const auto &last_function_symbol = functions.back().symbol_expr;
347 std::map<irep_idt, goto_programt::targett> calls;
349 for(
auto it=functions.crbegin(), itend=functions.crend(); it!=itend; ++it)
352 irep_idt id_or_empty = fun.symbol_expr.has_value()
353 ? fun.symbol_expr->get_identifier()
360 if(fun.symbol_expr.has_value())
363 auto new_call = code;
370 insertit.first->second = t1;
378 t1->source_location.set_comment(
379 "cannot find calls for " +
383 insertit.first->second = t1;
395 fun.symbol_expr.has_value() == last_function_symbol.has_value() &&
396 (!fun.symbol_expr.has_value() ||
397 *fun.symbol_expr == *last_function_symbol))
405 fun_class_identifier, this_class_identifier);
410 it != functions.crbegin() &&
411 std::prev(it)->symbol_expr.has_value() == fun.symbol_expr.has_value() &&
412 (!fun.symbol_expr.has_value() ||
413 *(std::prev(it)->symbol_expr) == *fun.symbol_expr))
416 !new_code_gotos.
empty(),
417 "a dispatch table entry has been processed already, "
418 "which should have created a GOTO");
425 insertit.first->second, class_id_test, vcall_source_loc));
441 const irep_idt property_class=it->source_location.get_property_class();
443 it->source_location=target->source_location;
444 if(!property_class.
empty())
445 it->source_location.set_property_class(property_class);
447 it->source_location.set_comment(
comment);
453 target->turn_into_skip();
480 "remove_virtual_function must take a function call instruction whose "
481 "function is a class method descriptor ");
484 "virtual function calls must have at least a this-argument");
523 for(
const auto &child : findit->second.children)
527 auto it = entry_map.find(child);
529 it != entry_map.end() &&
530 (!it->second.symbol_expr.has_value() ||
532 id2string(it->second.symbol_expr->get_identifier()),
533 "java::java.lang.Object")))
542 function.symbol_expr->set(ID_C_class, child);
546 function.symbol_expr=last_method_defn;
548 if(!
function.symbol_expr.has_value())
554 function.class_id = resolved_call->get_class_identifier();
556 resolved_call->get_full_component_identifier());
559 function.symbol_expr->
set(
560 ID_C_class, resolved_call->get_class_identifier());
563 functions.push_back(
function);
564 entry_map.emplace(child,
function);
567 child,
function.symbol_expr, component_name, functions, entry_map);
576 const exprt &
function,
580 const irep_idt class_id=
function.get(ID_C_class);
581 const std::string class_id_string(
id2string(class_id));
582 const irep_idt function_name =
function.get(ID_component_name);
583 const std::string function_name_string(
id2string(function_name));
584 INVARIANT(!class_id.
empty(),
"All virtual functions must have a class");
594 root_function.
class_id = resolved_call->get_class_identifier();
601 ID_C_class, resolved_call->get_class_identifier());
607 class_id, root_function.
symbol_expr, function_name, functions, entry_map);
610 functions.push_back(root_function);
620 irep_idt a_id = a.symbol_expr.has_value()
621 ? a.symbol_expr->get_identifier()
623 irep_idt b_id = b.symbol_expr.has_value()
624 ? b.symbol_expr->get_identifier()
627 if(has_prefix(id2string(a_id),
"java::java.lang.Object"))
629 else if(has_prefix(id2string(b_id),
"java::java.lang.Object"))
633 int cmp = a_id.compare(b_id);
635 return a.class_id < b.class_id;
649 const irep_idt &component_name)
const
653 class_id, component_name);
669 bool did_something=
false;
671 for(goto_programt::instructionst::iterator
676 if(target->is_function_call())
694 return did_something;
701 bool did_something=
false;
703 for(goto_functionst::function_mapt::iterator f_it=
708 const irep_idt &function_id = f_it->first;
728 class_hierarchy(symbol_table);
777 class_hierarchy(
function.get_symbol_table());
780 function.get_function_id(),
function.get_goto_function().body);
796 function.get_function_id(),
function.get_goto_function().body);
856 const exprt &
function,
862 get_callees.get_functions(
function, overridden_functions);
#define Forall_goto_program_instructions(it, program)
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
@ CALL_LAST_FUNCTION
When no callee type matches, call the last passed function, which is expected to be some safe default...
virtual_dispatch_fallback_actiont
Specifies remove_virtual_function's behaviour when the actual supplied parameter does not match any o...
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
const typet & subtype() const
bool has_subexpr(const exprt &expr, const std::function< bool(const exprt &)> &pred)
returns true if the expression has a subexpression that satisfies pred
Non-graph-based representation of the class hierarchy.
std::set< irep_idt > get_callees(const call_grapht::directed_grapht &graph, const irep_idt &function)
Get functions directly callable from a given function.
optionalt< resolve_inherited_componentt::inherited_componentt > get_inherited_method_implementation(const irep_idt &call_basename, const irep_idt &classname, const symbol_tablet &symbol_table)
Given a class and a component, identify the concrete method it is resolved to.
depth_iteratort depth_begin()
@ ASSUME_FALSE
When no callee type matches, ASSUME false, thus preventing any complete trace from using this path.
void get_child_functions_rec(const irep_idt &, const optionalt< symbol_exprt > &, const irep_idt &, dispatch_table_entriest &, dispatch_table_entries_mapt &) const
Used by get_functions to track the most-derived parent that provides an override of a given function.
The type of an expression, extends irept.
Fresh auxiliary symbol creation.
void remove_skip(goto_programt &goto_program, goto_programt::targett begin, goto_programt::targett end)
remove unnecessary skip statements
static void create_static_function_call(code_function_callt &call, const symbol_exprt &function_symbol, const namespacet &ns)
Create a concrete function call to replace a virtual one.
exprt get_class_identifier_field(const exprt &this_expr_in, const struct_tag_typet &suggested_type, const namespacet &ns)
void update()
Update all indices.
Functions for replacing virtual function call with a static function calls in functions,...
targett add(instructiont &&instruction)
Adds a given instruction at the end.
void remove_virtual_functions(symbol_table_baset &symbol_table, goto_functionst &goto_functions)
Remove virtual function calls from all functions in the specified list and replace them with their mo...
void compute_location_numbers()
bool empty() const
Is the program empty?
Base class for all expressions.
A struct tag type, i.e., struct_typet with an identifier.
std::vector< dispatch_table_entryt > dispatch_table_entriest
remove_virtual_functionst(symbol_table_baset &_symbol_table, const class_hierarchyt &_class_hierarchy)
std::function< optionalt< resolve_inherited_componentt::inherited_componentt > const irep_idt &, const irep_idt &)> function_call_resolvert
function_mapt function_map
Expression to hold a symbol (variable)
static instructiont make_decl(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
static goto_programt::targett replace_virtual_function_with_dispatch_table(symbol_table_baset &symbol_table, const irep_idt &function_id, goto_programt &goto_program, goto_programt::targett target, const dispatch_table_entriest &functions, virtual_dispatch_fallback_actiont fallback_action)
Replace virtual function call with a static function call Achieved by substituting a virtual function...
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
static instructiont make_goto(targett _target, const source_locationt &l=source_locationt::nil())
Given a class and a component (either field or method), find the closest parent that defines that com...
bool remove_virtual_functions(const irep_idt &function_id, goto_programt &goto_program)
Remove all virtual function calls in a GOTO program and replace them with calls to their most derived...
void get_functions(const exprt &, dispatch_table_entriest &) const
Used to get dispatch entries to call for the given function.
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
static instructiont make_function_call(const code_function_callt &_code, const source_locationt &l=source_locationt::nil())
Create a function call instruction.
goto_programt::targett remove_virtual_function(const irep_idt &function_id, goto_programt &goto_program, goto_programt::targett target)
Replace specified virtual function call with a static call to its most derived implementation.
typet & type()
Return the type of the expression.
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
codet representation of a function call statement.
targett insert_before(const_targett target)
Insertion before the instruction pointed-to by the given instruction iterator target.
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
const class_hierarchyt & class_hierarchy
static instructiont make_assertion(const exprt &g, const source_locationt &l=source_locationt::nil())
irep_idt mode
Language mode.
void destructive_insert(const_targett target, goto_programt &p)
Inserts the given program p before target.
const std::string & id2string(const irep_idt &d)
get_virtual_calleest(const symbol_table_baset &_symbol_table, const class_hierarchyt &_class_hierarchy)
static instructiont make_skip(const source_locationt &l=source_locationt::nil())
const irep_idt & get_identifier() const
The symbol table base class interface.
const symbol_table_baset & symbol_table
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
optionalt< symbol_exprt > symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
const irep_idt & id() const
std::map< irep_idt, dispatch_table_entryt > dispatch_table_entries_mapt
The Boolean constant false.
const parameterst & parameters() const
nonstd::optional< T > optionalt
void destructive_append(goto_programt &p)
Appends the given program p to *this. p is destroyed.
goto_programt::targett remove_virtual_function(symbol_tablet &symbol_table, const irep_idt &function_id, goto_programt &goto_program, goto_programt::targett instruction, const dispatch_table_entriest &dispatch_table, virtual_dispatch_fallback_actiont fallback_action)
Replace virtual function call with a static function call Achieved by substituting a virtual function...
instructionst instructions
The list of instructions in the goto program.
Deprecated expression utility functions.
A collection of goto functions.
static void process_this_argument(const irep_idt &function_id, const goto_programt &goto_program, const goto_programt::targett target, exprt &argument_for_this, symbol_table_baset &symbol_table, const source_locationt &vcall_source_loc, goto_programt &new_code_for_this_argument)
If argument_for_this contains a dereference then create a temporary variable for it and use that inst...
Forward depth-first search iterators These iterators' copy operations are expensive,...
goto_functionst goto_functions
GOTO functions.
symbol_table_baset & symbol_table
const irep_idt & get(const irep_namet &name) const
bool can_cast_expr< class_method_descriptor_exprt >(const exprt &base)
void set(const irep_namet &name, const irep_idt &value)
A generic container class for the GOTO intermediate representation of one function.
Extract class identifier.
static goto_programt analyse_checks_directly_preceding_function_call(const goto_programt &goto_program, goto_programt::const_targett instr_it, const exprt &argument_for_this, const symbol_exprt &temp_var_for_this)
Duplicate ASSUME instructions involving argument_for_this for temp_var_for_this.
instructionst::const_iterator const_targett
bool has_prefix(const std::string &s, const std::string &prefix)
static instructiont make_assumption(const exprt &g, const source_locationt &l=source_locationt::nil())
static irep_idt build_full_component_identifier(const irep_idt &class_name, const irep_idt &component_name)
Build a component name as found in a GOTO symbol table equivalent to the name of a concrete component...
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Semantic type conversion.
The Boolean constant true.
A constant literal expression.
depth_iteratort depth_end()
void collect_virtual_function_callees(const exprt &function, const symbol_table_baset &symbol_table, const class_hierarchyt &class_hierarchy, dispatch_table_entriest &overridden_functions)
Given a function expression representing a virtual method of a class, the function computes all overr...
static std::string comment(const rw_set_baset::entryt &entry, bool write)
symbol_tablet symbol_table
Symbol table.
symbolt & get_fresh_aux_symbol(const typet &type, const std::string &name_prefix, const std::string &basename_prefix, const source_locationt &source_location, const irep_idt &symbol_mode, const namespacet &ns, symbol_table_baset &symbol_table)
Installs a fresh-named symbol with respect to the given namespace ns with the requested name pattern ...
exprt get_method(const irep_idt &class_id, const irep_idt &component_name) const
Returns symbol pointing to a specified method in a specified class.
Interface providing access to a single function in a GOTO model, plus its associated symbol table.
const class_hierarchyt & class_hierarchy
instructionst::iterator targett
void operator()(goto_functionst &functions)
Remove virtual function calls from all functions in the specified list and replace them with their mo...