cprover
lambda_synthesis.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Java lambda code synthesis
4 
5 Author: Diffblue Ltd.
6 
7 \*******************************************************************/
8 
11 
12 #include "lambda_synthesis.h"
13 
16 #include "java_types.h"
17 #include "java_utils.h"
18 #include "synthetic_methods_map.h"
19 
20 #include <util/message.h>
21 #include <util/namespace.h>
22 #include <util/prefix.h>
23 #include <util/symbol_table.h>
24 
25 #include <string.h>
26 
27 static std::string escape_symbol_special_chars(std::string input)
28 {
29  for(auto &c : input)
30  {
31  if(c == '$' || c == ':' || c == '.')
32  c = '_';
33  }
34  return input;
35 }
36 
38  const irep_idt &method_identifier,
39  std::size_t instruction_address)
40 {
41  return "java::lambda_synthetic_class$" +
43  id2string(strip_java_namespace_prefix(method_identifier))) +
44  "$" + std::to_string(instruction_address);
45 }
46 
56  const symbol_table_baset &symbol_table,
57  const java_class_typet::java_lambda_method_handlest &lambda_method_handles,
58  const size_t index)
59 {
60  // Check if we don't have enough bootstrap methods to satisfy the requested
61  // lambda. This could happen if we fail to parse one of the methods, or if
62  // the class type is partly or entirely synthetic, such as the types created
63  // internally by the string solver.
64  if(index >= lambda_method_handles.size())
65  return {};
66  const auto &lambda_method_handle = lambda_method_handles.at(index);
67  // If the lambda method handle has an unknown type, it does not refer to
68  // any symbol (it has an empty identifier)
69  if(
70  lambda_method_handle.get_handle_kind() !=
72  return lambda_method_handle;
73  return {};
74 }
75 
78  const symbol_tablet &symbol_table,
79  const irep_idt &method_identifier,
80  const java_method_typet &dynamic_method_type)
81 {
82  const namespacet ns{symbol_table};
83  const auto &method_symbol = ns.lookup(method_identifier);
84  const auto &declaring_class_symbol =
85  ns.lookup(*declaring_class(method_symbol));
86 
87  const auto &class_type = to_java_class_type(declaring_class_symbol.type);
88  const auto &lambda_method_handles = class_type.lambda_method_handles();
89  auto lambda_handle_index =
90  dynamic_method_type.get_int(ID_java_lambda_method_handle_index);
92  symbol_table, lambda_method_handles, lambda_handle_index);
93 }
94 
95 class no_unique_unimplemented_method_exceptiont : public std::exception
96 {
97 public:
98  explicit no_unique_unimplemented_method_exceptiont(const std::string &s)
99  : message(s)
100  {
101  }
102  const std::string message;
103 };
104 
106 {
108  const java_class_typet::methodt *a,
109  const java_class_typet::methodt *b) const
110  {
111  return a->get_base_name() == b->get_base_name()
112  ? (a->get_descriptor() == b->get_descriptor()
113  ? 0
114  : a->get_descriptor() < b->get_descriptor())
115  : a->get_base_name() < b->get_base_name();
116  }
117 };
118 
121 typedef std::map<
123  bool,
126 
135 get_interface_methods(const irep_idt &interface_id, const namespacet &ns)
136 {
137  static const irep_idt jlo = "java::java.lang.Object";
138  // Terminate recursion at Object; any other base of an interface must
139  // itself be an interface.
140  if(jlo == interface_id)
141  return {};
142 
143  const java_class_typet &interface =
144  to_java_class_type(ns.lookup(interface_id).type);
145 
146  if(interface.get_is_stub())
147  {
149  "produces a type that inherits the stub type " + id2string(interface_id));
150  }
151 
153 
154  // First accumulate definitions from child types:
155  for(const auto &base : interface.bases())
156  {
157  const methods_by_name_and_descriptort base_methods =
158  get_interface_methods(base.type().get_identifier(), ns);
159  for(const auto &base_method : base_methods)
160  {
161  if(base_method.second)
162  {
163  // Any base definition fills any abstract definition from another base:
164  all_methods[base_method.first] = true;
165  }
166  else
167  {
168  // An abstract method incoming from a base falls to any existing
169  // definition, so only insert if not present:
170  all_methods.emplace(base_method.first, false);
171  }
172  }
173  }
174 
175  // Now insert defintions from this class:
176  for(const auto &method : interface.methods())
177  {
178  static const irep_idt equals = "equals";
179  static const irep_idt equals_descriptor = "(Ljava/lang/Object;)Z";
180  static const irep_idt hashCode = "hashCode";
181  static const irep_idt hashCode_descriptor = "()I";
182  if(
183  (method.get_base_name() == equals &&
184  method.get_descriptor() == equals_descriptor) ||
185  (method.get_base_name() == hashCode &&
186  method.get_descriptor() == hashCode_descriptor))
187  {
188  // Ignore any uses of functions that are certainly defined on
189  // java.lang.Object-- even if explicitly made abstract, they can't be the
190  // implemented method of a functional interface.
191  continue;
192  }
193 
194  // Note unlike inherited definitions, an abstract definition here *does*
195  // wipe out a non-abstract definition (i.e. a default method) from a parent
196  // type.
197  all_methods[&method] =
198  !ns.lookup(method.get_name()).type.get_bool(ID_C_abstract);
199  }
200 
201  return all_methods;
202 }
203 
205  const symbol_tablet &symbol_table,
206  const struct_tag_typet &functional_interface_tag,
207  const irep_idt &method_identifier,
208  const int instruction_address,
209  const messaget &log)
210 {
211  const namespacet ns{symbol_table};
212  try
213  {
214  const methods_by_name_and_descriptort all_methods =
215  get_interface_methods(functional_interface_tag.get_identifier(), ns);
216 
217  const java_class_typet::methodt *method_and_descriptor_to_implement =
218  nullptr;
219 
220  for(const auto &entry : all_methods)
221  {
222  if(!entry.second)
223  {
224  if(method_and_descriptor_to_implement != nullptr)
225  {
227  "produces a type with at least two unimplemented methods");
228  }
229  method_and_descriptor_to_implement = entry.first;
230  }
231  }
232 
233  if(!method_and_descriptor_to_implement)
234  {
236  "produces a type with no unimplemented methods");
237  }
238  return method_and_descriptor_to_implement;
239  }
241  {
242  log.debug() << "ignoring invokedynamic at " << method_identifier
243  << " address " << instruction_address << " with type "
244  << functional_interface_tag.get_identifier() << " which "
245  << e.message << "." << messaget::eom;
246  return {};
247  }
248 }
249 
251  const irep_idt &synthetic_class_name,
253  const struct_tag_typet &functional_interface_tag,
254  const java_method_typet &dynamic_method_type)
255 {
256  java_class_typet synthetic_class_type;
257  // Tag = name without 'java::' prefix, matching the convention used by
258  // java_bytecode_convert_class.cpp
259  synthetic_class_type.set_tag(
260  strip_java_namespace_prefix(synthetic_class_name));
261  synthetic_class_type.set_name(synthetic_class_name);
262  synthetic_class_type.set_synthetic(true);
263  synthetic_class_type.set(ID_java_lambda_method_handle, lambda_method_handle);
264  struct_tag_typet base_tag("java::java.lang.Object");
265  synthetic_class_type.add_base(base_tag);
266  synthetic_class_type.add_base(functional_interface_tag);
267 
268  // Add the class fields:
269 
270  {
271  java_class_typet::componentt base_field;
272  const irep_idt base_field_name("@java.lang.Object");
273  base_field.set_name(base_field_name);
274  base_field.set_base_name(base_field_name);
275  base_field.set_pretty_name(base_field_name);
276  base_field.set_access(ID_private);
277  base_field.type() = base_tag;
278  synthetic_class_type.components().emplace_back(std::move(base_field));
279 
280  std::size_t field_idx = 0;
281  for(const auto &param : dynamic_method_type.parameters())
282  {
283  irep_idt field_basename = "capture_" + std::to_string(field_idx++);
284 
286  new_field.set_name(field_basename);
287  new_field.set_base_name(field_basename);
288  new_field.set_pretty_name(field_basename);
289  new_field.set_access(ID_private);
290  new_field.type() = param.type();
291  synthetic_class_type.components().emplace_back(std::move(new_field));
292  }
293  }
294 
295  symbolt synthetic_class_symbol = type_symbolt{synthetic_class_type};
296  synthetic_class_symbol.name = synthetic_class_name;
297  synthetic_class_symbol.mode = ID_java;
298  return synthetic_class_symbol;
299 }
300 
302  synthetic_methods_mapt &synthetic_methods,
303  const irep_idt &synthetic_class_name,
304  java_method_typet constructor_type) // dynamic_method_type
305 {
307  irep_idt constructor_name = id2string(synthetic_class_name) + ".<init>";
308  constructor_symbol.name = constructor_name;
310  constructor_symbol.base_name = "<init>";
311  constructor_symbol.mode = ID_java;
312 
313  synthetic_methods[constructor_name] =
314  synthetic_method_typet::INVOKEDYNAMIC_CAPTURE_CONSTRUCTOR;
315 
316  constructor_type.set_is_constructor();
317  constructor_type.return_type() = empty_typet();
318 
319  size_t field_idx = 0;
320  for(auto &param : constructor_type.parameters())
321  {
322  irep_idt param_basename = "param_" + std::to_string(field_idx++);
323  param.set_base_name(param_basename);
324  param.set_identifier(
325  id2string(constructor_name) + "::" + id2string(param_basename));
326  }
327 
328  java_method_typet::parametert constructor_this_param(
329  java_reference_type(struct_tag_typet(synthetic_class_name)));
330  constructor_this_param.set_this();
331  constructor_this_param.set_base_name("this");
332  constructor_this_param.set_identifier(id2string(constructor_name) + "::this");
333 
334  constructor_type.parameters().insert(
335  constructor_type.parameters().begin(), constructor_this_param);
336 
337  constructor_symbol.type = constructor_type;
338  set_declaring_class(constructor_symbol, synthetic_class_name);
339  return constructor_symbol;
340 }
341 
343  synthetic_methods_mapt &synthetic_methods,
344  const java_class_typet::methodt &method_to_implement,
345  const irep_idt &synthetic_class_name)
346 {
347  const std::string implemented_method_name =
348  id2string(synthetic_class_name) + "." +
349  id2string(method_to_implement.get_base_name()) + ":" +
350  id2string(method_to_implement.get_descriptor());
351 
353  implemented_method_symbol.name = implemented_method_name;
354  synthetic_methods[implemented_method_symbol.name] =
355  synthetic_method_typet::INVOKEDYNAMIC_METHOD;
357  implemented_method_symbol.base_name = method_to_implement.get_base_name();
359  implemented_method_symbol.type = method_to_implement.type();
360  auto &implemented_method_type = to_code_type(implemented_method_symbol.type);
361  implemented_method_type.parameters()[0].type() =
362  java_reference_type(struct_tag_typet(synthetic_class_name));
363 
364  size_t field_idx = 0;
365  for(auto &param : implemented_method_type.parameters())
366  {
367  irep_idt param_basename =
368  field_idx == 0 ? "this" : "param_" + std::to_string(field_idx);
369  param.set_base_name(param_basename);
370  param.set_identifier(
371  id2string(implemented_method_name) + "::" + id2string(param_basename));
372 
373  ++field_idx;
374  }
375 
376  set_declaring_class(implemented_method_symbol, synthetic_class_name);
378 }
379 
380 // invokedynamic will be called with operands that should be stored in a
381 // synthetic object implementing the interface type that it returns. For
382 // example, "invokedynamic f(a, b, c) -> MyInterface" should result in the
383 // creation of the synthetic class:
384 // public class SyntheticCapture implements MyInterface {
385 // private int a;
386 // private float b;
387 // private Other c;
388 // public SyntheticCapture(int a, float b, Other c) {
389 // this.a = a; this.b = b; this.c = c;
390 // }
391 // public void myInterfaceMethod(int d) {
392 // f(a, b, c, d);
393 // }
394 // }
395 // This method just creates the outline; the methods will be populated on
396 // demand via java_bytecode_languaget::convert_lazy_method.
397 
398 // Check that we understand the lambda method handle; if we don't then
399 // we will not create a synthetic class at all, and the corresponding
400 // invoke instruction will return null when eventually converted by
401 // java_bytecode_convert_method.
403  const irep_idt &method_identifier,
405  symbol_tablet &symbol_table,
406  synthetic_methods_mapt &synthetic_methods,
407  message_handlert &message_handler)
408 {
409  const messaget log{message_handler};
410 
411  for(const auto &instruction : instructions)
412  {
413  if(strcmp(bytecode_info[instruction.bytecode].mnemonic, "invokedynamic"))
414  continue;
415  const auto &dynamic_method_type =
416  to_java_method_type(instruction.args.at(0).type());
417  const auto lambda_handle = lambda_method_handle(
418  symbol_table, method_identifier, dynamic_method_type);
419  if(!lambda_handle)
420  {
421  log.debug() << "ignoring invokedynamic at " << method_identifier
422  << " address " << instruction.address
423  << " with unknown handle type" << messaget::eom;
424  continue;
425  }
426  const auto &functional_interface_tag = to_struct_tag_type(
427  to_java_reference_type(dynamic_method_type.return_type()).subtype());
428  const auto unimplemented_method = try_get_unique_unimplemented_method(
429  symbol_table,
430  functional_interface_tag,
431  method_identifier,
432  instruction.address,
433  log);
434  if(!unimplemented_method)
435  continue;
436  log.debug() << "identified invokedynamic at " << method_identifier
437  << " address " << instruction.address << " for lambda: "
438  << lambda_handle->get_lambda_method_identifier()
439  << messaget::eom;
440  const irep_idt synthetic_class_name =
441  lambda_synthetic_class_name(method_identifier, instruction.address);
442  symbol_table.add(constructor_symbol(
443  synthetic_methods, synthetic_class_name, dynamic_method_type));
444  symbol_table.add(implemented_method_symbol(
445  synthetic_methods, *unimplemented_method, synthetic_class_name));
446  symbol_table.add(synthetic_class_symbol(
447  synthetic_class_name,
448  *lambda_handle,
449  functional_interface_tag,
450  dynamic_method_type));
451  }
452 }
453 
455  const irep_idt &identifier,
456  const irep_idt &base_name,
457  const irep_idt &pretty_name,
458  const typet &type,
459  const irep_idt &declaring_class,
460  symbol_table_baset &symbol_table,
461  message_handlert &log)
462 {
463  const auto *existing_symbol = symbol_table.lookup(identifier);
464  if(existing_symbol)
465  return *existing_symbol;
466 
468  identifier,
469  base_name,
470  pretty_name,
471  type,
473  symbol_table,
474  log);
475  return symbol_table.lookup_ref(identifier);
476 }
477 
479  const irep_idt &function_id,
480  symbol_table_baset &symbol_table,
481  message_handlert &message_handler)
482 {
483  code_blockt result;
484  namespacet ns(symbol_table);
485 
486  const symbolt &function_symbol = ns.lookup(function_id);
487  const auto &parameters = to_code_type(function_symbol.type).parameters();
488 
489  const symbolt &class_symbol = ns.lookup(*declaring_class(function_symbol));
490  const class_typet &class_type = to_class_type(class_symbol.type);
491 
492  const symbol_exprt this_param(
493  parameters.at(0).get_identifier(), parameters.at(0).type());
494  const dereference_exprt deref_this(this_param);
495 
496  // Call super-constructor (always java.lang.Object):
497  const irep_idt jlo("java::java.lang.Object");
498  const irep_idt jlo_constructor(id2string(jlo) + ".<init>:()V");
499  const auto jlo_reference = java_reference_type(struct_tag_typet(jlo));
500  code_typet::parametert jlo_this_param{jlo_reference};
501  jlo_this_param.set_this();
502 
503  java_method_typet jlo_constructor_type(
504  code_typet::parameterst{jlo_this_param}, empty_typet());
505  const auto &jlo_constructor_symbol = get_or_create_method_symbol(
506  jlo_constructor,
507  "<init>",
508  jlo_constructor,
509  jlo_constructor_type,
510  jlo,
511  symbol_table,
512  message_handler);
513  code_function_callt super_constructor_call(
514  jlo_constructor_symbol.symbol_expr(),
515  code_function_callt::argumentst{typecast_exprt(this_param, jlo_reference)});
516  result.add(super_constructor_call);
517 
518  // Store captured parameters:
519  auto field_iterator = std::next(class_type.components().begin());
520  for(const auto &parameter : parameters)
521  {
522  // Give the parameter its symbol:
523  parameter_symbolt param_symbol;
524  param_symbol.name = parameter.get_identifier();
525  param_symbol.base_name = parameter.get_base_name();
526  param_symbol.mode = ID_java;
527  param_symbol.type = parameter.type();
528  symbol_table.add(param_symbol);
529 
530  if(parameter.get_this())
531  continue;
532 
533  code_assignt assign_field(
534  member_exprt(deref_this, field_iterator->get_name(), parameter.type()),
535  symbol_exprt(parameter.get_identifier(), parameter.type()));
536  result.add(assign_field);
537 
538  ++field_iterator;
539  }
540 
541  return std::move(result);
542 }
543 
545  const irep_idt &function_id,
546  const irep_idt &basename,
547  const typet &type,
548  symbol_table_baset &symbol_table,
549  code_blockt &method)
550 {
551  irep_idt new_var_name = id2string(function_id) + "::" + id2string(basename);
552  auxiliary_symbolt new_instance_var_symbol;
553  new_instance_var_symbol.name = new_var_name;
554  new_instance_var_symbol.base_name = basename;
555  new_instance_var_symbol.mode = ID_java;
556  new_instance_var_symbol.type = type;
557  bool add_failed = symbol_table.add(new_instance_var_symbol);
558  POSTCONDITION(!add_failed);
559  symbol_exprt new_instance_var = new_instance_var_symbol.symbol_expr();
560  method.add(code_declt{new_instance_var});
561 
562  return new_instance_var;
563 }
564 
576  const irep_idt &function_id,
577  const symbolt &lambda_method_symbol,
578  symbol_table_baset &symbol_table,
579  code_blockt &result)
580 {
581  // We must instantiate the object, then call the requested constructor
582  const auto &method_type = to_code_type(lambda_method_symbol.type);
583  INVARIANT(
584  method_type.get_bool(ID_constructor),
585  "REF_NewInvokeSpecial lambda must refer to a constructor");
586  const auto &created_type = method_type.parameters().at(0).type();
587  irep_idt created_class =
588  to_struct_tag_type(created_type.subtype()).get_identifier();
589 
590  // Call static init if it exists:
591  irep_idt static_init_name = clinit_wrapper_name(created_class);
592  if(const auto *static_init_symbol = symbol_table.lookup(static_init_name))
593  {
594  result.add(code_function_callt{static_init_symbol->symbol_expr(), {}});
595  }
596 
597  // Make a local to hold the new instance:
598  symbol_exprt new_instance_var = create_and_declare_local(
599  function_id,
600  "newinvokespecial_instance",
601  created_type,
602  symbol_table,
603  result);
604 
605  // Instantiate the object:
606  side_effect_exprt java_new_expr(ID_java_new, created_type, {});
607  result.add(code_assignt{new_instance_var, java_new_expr});
608 
609  return new_instance_var;
610 }
611 
614 static optionalt<irep_idt> get_unboxing_method(const typet &maybe_boxed_type)
615 {
616  const irep_idt &boxed_type_id =
617  to_struct_tag_type(maybe_boxed_type.subtype()).get_identifier();
618  const java_boxed_type_infot *boxed_type_info =
619  get_boxed_type_info_by_name(boxed_type_id);
620  return boxed_type_info ? boxed_type_info->unboxing_function_name
622 }
623 
628  const symbolt &function_symbol,
629  const symbol_tablet &symbol_table)
630 {
631  const auto &method_type = to_java_method_type(function_symbol.type);
632  if(!method_type.has_this())
633  return function_symbol.symbol_expr();
634  const irep_idt &declared_on_class_id =
635  to_struct_tag_type(method_type.get_this()->type().subtype())
636  .get_identifier();
637  const auto &this_symbol = symbol_table.lookup_ref(declared_on_class_id);
638  if(to_java_class_type(this_symbol.type).get_final())
639  return function_symbol.symbol_expr();
640 
641  // Neither final nor static; make a class_method_descriptor_exprt that will
642  // trigger remove_virtual_functions to produce a virtual dispatch table:
643 
644  const std::string &function_name = id2string(function_symbol.name);
645  const auto method_name_start_idx = function_name.rfind('.');
646  const irep_idt mangled_method_name =
647  function_name.substr(method_name_start_idx + 1);
648 
649  return class_method_descriptor_exprt{function_symbol.type,
650  mangled_method_name,
651  declared_on_class_id,
652  function_symbol.base_name};
653 }
654 
685  exprt expr,
686  const typet &required_type,
687  code_blockt &code_block,
688  symbol_table_baset &symbol_table,
689  const irep_idt &function_id,
690  const std::string &role)
691 {
692  const typet &original_type = expr.type();
693  const bool original_is_pointer = can_cast_type<pointer_typet>(original_type);
694  const bool required_is_pointer = can_cast_type<pointer_typet>(required_type);
695 
696  if(original_is_pointer == required_is_pointer)
697  {
698  return expr;
699  }
700 
701  // One is a pointer, the other a primitive -- box or unbox as necessary, and
702  // check the types are consistent:
703 
704  const auto *primitive_type_info = get_java_primitive_type_info(
705  original_is_pointer ? required_type : original_type);
706  INVARIANT(
707  primitive_type_info != nullptr,
708  "A Java non-pointer type involved in a type disagreement should"
709  " be a primitive");
710 
711  const irep_idt fresh_local_name =
712  role + (original_is_pointer ? "_unboxed" : "_boxed");
713 
714  const symbol_exprt fresh_local = create_and_declare_local(
715  function_id, fresh_local_name, required_type, symbol_table, code_block);
716 
717  const irep_idt transform_function_id =
718  original_is_pointer
719  ? get_unboxing_method(original_type) // Use static type if known
720  .value_or(primitive_type_info->unboxing_function_name)
721  : primitive_type_info->boxed_type_factory_method;
722 
723  const symbolt &transform_function_symbol =
724  symbol_table.lookup_ref(transform_function_id);
725 
726  const typet &transform_function_param_type =
727  to_code_type(transform_function_symbol.type).parameters()[0].type();
728  const exprt cast_expr =
729  typecast_exprt::conditional_cast(expr, transform_function_param_type);
730 
731  code_block.add(code_function_callt{
732  fresh_local,
733  make_function_expr(transform_function_symbol, symbol_table),
734  {expr}});
735 
736  return std::move(fresh_local);
737 }
738 
743  exprt expr,
744  const typet &required_type,
745  code_blockt &code_block,
746  symbol_table_baset &symbol_table,
747  const irep_idt &function_id,
748  const std::string &role)
749 {
752  expr, required_type, code_block, symbol_table, function_id, role),
753  required_type);
754 }
755 
773  const irep_idt &function_id,
774  symbol_table_baset &symbol_table,
775  message_handlert &message_handler)
776 {
777  // Call the bound method with the capture parameters, then the actual
778  // parameters. Note one of the capture params might be the `this` parameter
779  // of a virtual call -- that depends on whether the callee is a static or an
780  // instance method.
781 
782  code_blockt result;
783  namespacet ns(symbol_table);
784 
785  const symbolt &function_symbol = ns.lookup(function_id);
786  const auto &function_type = to_code_type(function_symbol.type);
787  const auto &parameters = function_type.parameters();
788 
789  const symbolt &class_symbol = ns.lookup(*declaring_class(function_symbol));
790  const java_class_typet &class_type = to_java_class_type(class_symbol.type);
791 
792  const symbol_exprt this_param(
793  parameters.at(0).get_identifier(), parameters.at(0).type());
794  const dereference_exprt deref_this(this_param);
795 
796  code_function_callt::argumentst lambda_method_args;
797  for(const auto &field : class_type.components())
798  {
799  if(field.get_name() == "@java.lang.Object")
800  continue;
801  lambda_method_args.push_back(
802  member_exprt(deref_this, field.get_name(), field.type()));
803  }
804 
805  for(const auto &parameter : parameters)
806  {
807  // Give the parameter its symbol:
808  parameter_symbolt param_symbol;
809  param_symbol.name = parameter.get_identifier();
810  param_symbol.base_name = parameter.get_base_name();
811  param_symbol.mode = ID_java;
812  param_symbol.type = parameter.type();
813  symbol_table.add(param_symbol);
814 
815  if(parameter.get_this())
816  continue;
817 
818  lambda_method_args.push_back(param_symbol.symbol_expr());
819  }
820 
821  const auto &lambda_method_handle =
823  class_type.find(ID_java_lambda_method_handle));
824 
825  const auto &lambda_method_symbol =
826  ns.lookup(lambda_method_handle.get_lambda_method_identifier());
827  const auto handle_type = lambda_method_handle.get_handle_kind();
828  const auto is_constructor_lambda =
829  handle_type ==
831  const auto use_virtual_dispatch =
832  handle_type ==
834 
835  if(is_constructor_lambda)
836  {
837  auto new_instance_var = instantiate_new_object(
838  function_id, lambda_method_symbol, symbol_table, result);
839 
840  // Prepend the newly created object to the lambda arg list:
841  lambda_method_args.insert(lambda_method_args.begin(), new_instance_var);
842  }
843 
844  const auto &lambda_method_descriptor =
845  lambda_method_handle.get_lambda_method_descriptor();
846  exprt callee;
847  if(use_virtual_dispatch)
848  callee = lambda_method_descriptor;
849  else
850  callee = lambda_method_symbol.symbol_expr();
851 
852  // Adjust boxing if required:
853  const code_typet &callee_type = to_code_type(lambda_method_symbol.type);
854  const auto &callee_parameters = callee_type.parameters();
855  const auto &callee_return_type = callee_type.return_type();
856  INVARIANT(
857  callee_parameters.size() == lambda_method_args.size(),
858  "should have args for every parameter");
859  for(unsigned i = 0; i < callee_parameters.size(); ++i)
860  {
861  lambda_method_args[i] = adjust_type_if_necessary(
862  std::move(lambda_method_args[i]),
863  callee_parameters[i].type(),
864  result,
865  symbol_table,
866  function_id,
867  "param" + std::to_string(i));
868  }
869 
870  if(function_type.return_type() != empty_typet() && !is_constructor_lambda)
871  {
872  symbol_exprt result_local = create_and_declare_local(
873  function_id, "return_value", callee_return_type, symbol_table, result);
874  result.add(code_function_callt(result_local, callee, lambda_method_args));
875  exprt adjusted_local = adjust_type_if_necessary(
876  result_local,
877  function_type.return_type(),
878  result,
879  symbol_table,
880  function_id,
881  "retval");
882  result.add(code_returnt{adjusted_local});
883  }
884  else
885  {
886  result.add(code_function_callt(callee, lambda_method_args));
887  }
888 
889  if(is_constructor_lambda)
890  {
891  // Return the newly-created object.
893  lambda_method_args.at(0), function_type.return_type())});
894  }
895 
896  return std::move(result);
897 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
tag_typet::get_identifier
const irep_idt & get_identifier() const
Definition: std_types.h:459
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:142
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:170
typecast_exprt::conditional_cast
static exprt conditional_cast(const exprt &expr, const typet &type)
Definition: std_expr.h:1789
irept::get_int
signed int get_int(const irep_namet &name) const
Definition: irep.cpp:69
symbol_tablet
The symbol table.
Definition: symbol_table.h:20
java_class_typet::componentt
Definition: java_types.h:202
symbol_table_baset::lookup_ref
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Definition: symbol_table_base.h:104
typet::subtype
const typet & subtype() const
Definition: type.h:47
class_typet
Class type.
Definition: std_types.h:320
invokedynamic_synthetic_method
codet invokedynamic_synthetic_method(const irep_idt &function_id, symbol_table_baset &symbol_table, message_handlert &message_handler)
Create the body for the synthetic method implementing an invokedynamic method.
Definition: lambda_synthesis.cpp:772
bytecode_infot::mnemonic
const char * mnemonic
Definition: bytecode_info.h:46
java_class_typet::method_handle_kindt::LAMBDA_VIRTUAL_METHOD_HANDLE
@ LAMBDA_VIRTUAL_METHOD_HANDLE
Virtual call to the given interface or method.
java_reference_type
reference_typet java_reference_type(const typet &subtype)
Definition: java_types.cpp:89
java_class_typet::set_synthetic
void set_synthetic(bool synthetic)
marks class synthetic
Definition: java_types.h:436
set_declaring_class
void set_declaring_class(symbolt &symbol, const irep_idt &declaring_class)
Sets the identifier of the class which declared a given symbol to declaring_class.
Definition: java_utils.cpp:577
typet
The type of an expression, extends irept.
Definition: type.h:28
code_typet::parameterst
std::vector< parametert > parameterst
Definition: std_types.h:746
java_bytecode_parse_treet::methodt::instructionst
std::vector< instructiont > instructionst
Definition: java_bytecode_parse_tree.h:92
to_class_type
const class_typet & to_class_type(const typet &type)
Cast a typet to a class_typet.
Definition: std_types.h:376
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
dereference_exprt
Operator to dereference a pointer.
Definition: pointer_expr.h:256
compare_base_name_and_descriptort
Definition: lambda_synthesis.cpp:106
lambda_method_handle
static optionalt< java_class_typet::java_lambda_method_handlet > lambda_method_handle(const symbol_tablet &symbol_table, const irep_idt &method_identifier, const java_method_typet &dynamic_method_type)
Definition: lambda_synthesis.cpp:77
code_typet::parametert::set_identifier
void set_identifier(const irep_idt &identifier)
Definition: std_types.h:785
struct_typet::add_base
void add_base(const struct_tag_typet &base)
Add a base class/struct.
Definition: std_types.cpp:102
get_interface_methods
static const methods_by_name_and_descriptort get_interface_methods(const irep_idt &interface_id, const namespacet &ns)
Find all methods defined by this method and its parent types, returned as a map from const java_class...
Definition: lambda_synthesis.cpp:135
create_invokedynamic_synthetic_classes
void create_invokedynamic_synthetic_classes(const irep_idt &method_identifier, const java_bytecode_parse_treet::methodt::instructionst &instructions, symbol_tablet &symbol_table, synthetic_methods_mapt &synthetic_methods, message_handlert &message_handler)
Definition: lambda_synthesis.cpp:402
declaring_class
optionalt< irep_idt > declaring_class(const symbolt &symbol)
Gets the identifier of the class which declared a given symbol.
Definition: java_utils.cpp:571
irept::find
const irept & find(const irep_namet &name) const
Definition: irep.cpp:103
prefix.h
java_class_typet::methodt
Definition: java_types.h:243
get_unboxing_method
static optionalt< irep_idt > get_unboxing_method(const typet &maybe_boxed_type)
If maybe_boxed_type is a boxed primitive return its unboxing method; otherwise return empty.
Definition: lambda_synthesis.cpp:614
code_declt
A codet representing the declaration of a local variable.
Definition: std_code.h:402
exprt
Base class for all expressions.
Definition: expr.h:54
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
struct_tag_typet
A struct tag type, i.e., struct_typet with an identifier.
Definition: std_types.h:498
no_unique_unimplemented_method_exceptiont::message
const std::string message
Definition: lambda_synthesis.cpp:102
create_and_declare_local
static symbol_exprt create_and_declare_local(const irep_idt &function_id, const irep_idt &basename, const typet &type, symbol_table_baset &symbol_table, code_blockt &method)
Definition: lambda_synthesis.cpp:544
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:55
messaget::eom
static eomt eom
Definition: message.h:297
auxiliary_symbolt
Internally generated symbol table entryThis is a symbol generated as part of translation to or modifi...
Definition: symbol.h:147
synthetic_methods_mapt
std::unordered_map< irep_idt, synthetic_method_typet > synthetic_methods_mapt
Maps method names on to a synthetic method kind.
Definition: synthetic_methods_map.h:53
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:81
namespace.h
symbolt::pretty_name
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:52
java_class_typet
Definition: java_types.h:199
class_method_descriptor_exprt
An expression describing a method on a class.
Definition: std_expr.h:3111
java_class_typet::java_lambda_method_handlet
Represents a lambda call to a method.
Definition: java_types.h:484
strip_java_namespace_prefix
irep_idt strip_java_namespace_prefix(const irep_idt &to_strip)
Strip java:: prefix from given identifier.
Definition: java_utils.cpp:410
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
methods_by_name_and_descriptort
std::map< const java_class_typet::methodt *, bool, compare_base_name_and_descriptort > methods_by_name_and_descriptort
Map from method, indexed by name and descriptor but not defining class, onto defined-ness (i....
Definition: lambda_synthesis.cpp:125
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:82
java_class_typet::methodt::type
const java_method_typet & type() const
Definition: java_types.h:252
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:140
code_function_callt
codet representation of a function call statement.
Definition: std_code.h:1215
java_boxed_type_infot
Return type for get_boxed_type_info_by_name.
Definition: java_utils.h:55
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:949
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
java_reference_typet::subtype
struct_tag_typet & subtype()
Definition: java_types.h:614
empty_typet
The empty type.
Definition: std_types.h:46
no_unique_unimplemented_method_exceptiont::no_unique_unimplemented_method_exceptiont
no_unique_unimplemented_method_exceptiont(const std::string &s)
Definition: lambda_synthesis.cpp:98
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
struct_union_typet::set_tag
void set_tag(const irep_idt &tag)
Definition: std_types.h:164
java_class_typet::java_lambda_method_handlest
std::vector< java_lambda_method_handlet > java_lambda_method_handlest
Definition: java_types.h:517
code_typet::set_is_constructor
void set_is_constructor()
Definition: std_types.h:895
java_bytecode_parse_tree.h
symbol_table_baset
The symbol table base class interface.
Definition: symbol_table_base.h:22
to_java_reference_type
const java_reference_typet & to_java_reference_type(const typet &type)
Definition: java_types.h:632
symbolt::symbol_expr
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:122
java_class_typet::method_handle_kindt::LAMBDA_CONSTRUCTOR_HANDLE
@ LAMBDA_CONSTRUCTOR_HANDLE
Instantiate the needed type then call a constructor.
synthetic_class_symbol
symbolt synthetic_class_symbol(const irep_idt &synthetic_class_name, const java_class_typet::java_lambda_method_handlet &lambda_method_handle, const struct_tag_typet &functional_interface_tag, const java_method_typet &dynamic_method_type)
Definition: lambda_synthesis.cpp:250
code_typet
Base type of functions.
Definition: std_types.h:744
message_handlert
Definition: message.h:28
create_method_stub_symbol
void create_method_stub_symbol(const irep_idt &identifier, const irep_idt &base_name, const irep_idt &pretty_name, const typet &type, const irep_idt &declaring_class, symbol_table_baset &symbol_table, message_handlert &message_handler)
Definition: java_bytecode_convert_method.cpp:100
java_bytecode_convert_method.h
JAVA Bytecode Language Conversion.
java_class_typet::method_handle_kindt::UNKNOWN_HANDLE
@ UNKNOWN_HANDLE
Can't be called.
to_struct_tag_type
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition: std_types.h:523
code_function_callt::argumentst
exprt::operandst argumentst
Definition: std_code.h:1224
code_blockt::add
void add(const codet &code)
Definition: std_code.h:208
code_typet::parameters
const parameterst & parameters() const
Definition: std_types.h:860
java_class_typet::get_final
bool get_final() const
Definition: java_types.h:385
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
try_get_unique_unimplemented_method
static const java_class_typet::methodt * try_get_unique_unimplemented_method(const symbol_tablet &symbol_table, const struct_tag_typet &functional_interface_tag, const irep_idt &method_identifier, const int instruction_address, const messaget &log)
Definition: lambda_synthesis.cpp:204
escape_symbol_special_chars
static std::string escape_symbol_special_chars(std::string input)
Definition: lambda_synthesis.cpp:27
code_typet::parametert::set_base_name
void set_base_name(const irep_idt &name)
Definition: std_types.h:790
box_or_unbox_type_if_necessary
exprt box_or_unbox_type_if_necessary(exprt expr, const typet &required_type, code_blockt &code_block, symbol_table_baset &symbol_table, const irep_idt &function_id, const std::string &role)
If expr needs (un)boxing to satisfy required_type, add the required symbols to symbol_table and code ...
Definition: lambda_synthesis.cpp:684
code_typet::parametert::set_this
void set_this()
Definition: std_types.h:810
no_unique_unimplemented_method_exceptiont
Definition: lambda_synthesis.cpp:96
invokedynamic_synthetic_constructor
codet invokedynamic_synthetic_constructor(const irep_idt &function_id, symbol_table_baset &symbol_table, message_handlert &message_handler)
Create invokedynamic synthetic constructor.
Definition: lambda_synthesis.cpp:478
adjust_type_if_necessary
exprt adjust_type_if_necessary(exprt expr, const typet &required_type, code_blockt &code_block, symbol_table_baset &symbol_table, const irep_idt &function_id, const std::string &role)
Box or unbox expr as per box_or_unbox_type_if_necessary, then cast the result to required_type.
Definition: lambda_synthesis.cpp:742
can_cast_type< pointer_typet >
bool can_cast_type< pointer_typet >(const typet &type)
Check whether a reference to a typet is a pointer_typet.
Definition: std_types.h:1520
symbol_table_baset::add
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
Definition: symbol_table_base.cpp:18
member_exprt
Extract member of struct or union.
Definition: std_expr.h:2528
lambda_synthesis.h
Java lambda code synthesis.
java_class_typet::components
const componentst & components() const
Definition: java_types.h:226
java_class_typet::methodt::get_descriptor
const irep_idt & get_descriptor() const
Gets the method's descriptor – the mangled form of its type.
Definition: java_types.h:288
POSTCONDITION
#define POSTCONDITION(CONDITION)
Definition: invariant.h:480
make_function_expr
exprt make_function_expr(const symbolt &function_symbol, const symbol_tablet &symbol_table)
Produce a class_method_descriptor_exprt or symbol_exprt for function_symbol depending on whether virt...
Definition: lambda_synthesis.cpp:627
code_returnt
codet representation of a "return from a function" statement.
Definition: std_code.h:1342
symbolt
Symbol table entry.
Definition: symbol.h:28
irept::set
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:431
compare_base_name_and_descriptort::operator()
int operator()(const java_class_typet::methodt *a, const java_class_typet::methodt *b) const
Definition: lambda_synthesis.cpp:107
code_typet::parametert
Definition: std_types.h:761
java_class_typet::set_name
void set_name(const irep_idt &name)
Set the name of the struct, which can be used to look up its symbol in the symbol table.
Definition: java_types.h:566
type_symbolt
Symbol table entry describing a data typeThis is a symbol generated as part of type checking.
Definition: symbol.h:133
clinit_wrapper_name
irep_idt clinit_wrapper_name(const irep_idt &class_name)
Get the Java static initializer wrapper name for a given class (the wrapper checks if static initiali...
Definition: java_static_initializers.cpp:65
java_boxed_type_infot::unboxing_function_name
const irep_idt unboxing_function_name
Name of the function defined on the boxed type that returns the boxed value.
Definition: java_utils.h:58
symbol_table_baset::lookup
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Definition: symbol_table_base.h:95
code_typet::return_type
const typet & return_type() const
Definition: std_types.h:850
constructor_symbol
static symbolt constructor_symbol(synthetic_methods_mapt &synthetic_methods, const irep_idt &synthetic_class_name, java_method_typet constructor_type)
Definition: lambda_synthesis.cpp:301
get_or_create_method_symbol
static const symbolt & get_or_create_method_symbol(const irep_idt &identifier, const irep_idt &base_name, const irep_idt &pretty_name, const typet &type, const irep_idt &declaring_class, symbol_table_baset &symbol_table, message_handlert &log)
Definition: lambda_synthesis.cpp:454
to_java_class_type
const java_class_typet & to_java_class_type(const typet &type)
Definition: java_types.h:584
messaget::debug
mstreamt & debug() const
Definition: message.h:429
parameter_symbolt
Symbol table entry of function parameterThis is a symbol generated as part of type checking.
Definition: symbol.h:171
get_boxed_type_info_by_name
const java_boxed_type_infot * get_boxed_type_info_by_name(const irep_idt &type_name)
If type_name is a Java boxed type tag, return information about it, otherwise return null.
Definition: java_utils.cpp:35
INVARIANT
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:424
java_types.h
instantiate_new_object
static symbol_exprt instantiate_new_object(const irep_idt &function_id, const symbolt &lambda_method_symbol, symbol_table_baset &symbol_table, code_blockt &result)
Instantiates an object suitable for calling a given constructor (but does not actually call it).
Definition: lambda_synthesis.cpp:575
get_lambda_method_handle
static optionalt< java_class_typet::java_lambda_method_handlet > get_lambda_method_handle(const symbol_table_baset &symbol_table, const java_class_typet::java_lambda_method_handlest &lambda_method_handles, const size_t index)
Retrieves the symbol of the lambda method associated with the given lambda method handle (bootstrap m...
Definition: lambda_synthesis.cpp:55
to_java_method_type
const java_method_typet & to_java_method_type(const typet &type)
Definition: java_types.h:186
symbol_table.h
Author: Diffblue Ltd.
bytecode_info
struct bytecode_infot const bytecode_info[]
Definition: bytecode_info.cpp:16
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:295
message.h
implemented_method_symbol
static symbolt implemented_method_symbol(synthetic_methods_mapt &synthetic_methods, const java_class_typet::methodt &method_to_implement, const irep_idt &synthetic_class_name)
Definition: lambda_synthesis.cpp:342
java_utils.h
java_method_typet
Definition: java_types.h:103
lambda_synthetic_class_name
irep_idt lambda_synthetic_class_name(const irep_idt &method_identifier, std::size_t instruction_address)
Definition: lambda_synthesis.cpp:37
get_java_primitive_type_info
const java_primitive_type_infot * get_java_primitive_type_info(const typet &maybe_primitive_type)
If primitive_type is a Java primitive type, return information about it, otherwise return null.
Definition: java_utils.cpp:62
synthetic_methods_map.h
Synthetic methods are particular methods internally generated by the Java frontend,...
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
side_effect_exprt
An expression containing a side effect.
Definition: std_code.h:1898
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code.h:35