+ rty = java_c_types(reg_fn.group(1), None)
+ if rty is not None and not rty.is_native_primitive and reg_fn.group(2) == rty.java_hu_ty + "_new":
+ constructor_fns[rty.rust_obj] = reg_fn.group(3)
+ continue
+ arr_fn = fn_ret_arr_regex.match(line)
+ if arr_fn is not None:
+ if arr_fn.group(2).endswith("_clone"):
+ clone_fns.add(arr_fn.group(2))
+ # No object constructors return arrays, as then they wouldn't be an object constructor
+ continue
+
+# Define some manual clones...
+clone_fns.add("ThirtyTwoBytes_clone")
+write_c("static inline struct LDKThirtyTwoBytes ThirtyTwoBytes_clone(const struct LDKThirtyTwoBytes *orig) { struct LDKThirtyTwoBytes ret; memcpy(ret.data, orig->data, 32); return ret; }\n")
+
+java_c_types_none_allowed = False # C structs created by cbindgen are declared in dependency order
+
+with open(f"{sys.argv[3]}/structs/UtilMethods{consts.file_ext}", "a") as util:
+ util.write(consts.util_fn_pfx)
+
+with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java:
+ # Map a top-level function
+ def map_fn(line, re_match, ret_arr_len, c_call_string):
+ method_return_type = re_match.group(1)
+ method_name = re_match.group(2)
+ method_comma_separated_arguments = re_match.group(3)
+ method_arguments = method_comma_separated_arguments.split(',')
+
+ is_free = method_name.endswith("_free")
+ struct_meth = method_name.split("_")[0]
+
+ return_type_info = type_mapping_generator.map_type(method_return_type, True, ret_arr_len, False, False)
+
+ argument_types = []
+ default_constructor_args = {}
+ takes_self = False
+ args_known = True
+
+ for argument_index, argument in enumerate(method_arguments):
+ argument_conversion_info = type_mapping_generator.map_type(argument, False, None, is_free, True)
+ if argument_index == 0 and argument_conversion_info.java_hu_ty == struct_meth:
+ takes_self = True
+ if argument_conversion_info.arg_conv is not None and "Warning" in argument_conversion_info.arg_conv:
+ if argument_conversion_info.rust_obj in constructor_fns:
+ assert not is_free
+ for explode_arg in constructor_fns[argument_conversion_info.rust_obj].split(','):
+ explode_arg_conv = type_mapping_generator.map_type(explode_arg, False, None, False, True)
+ if explode_arg_conv.c_ty == "void":
+ # We actually want to handle this case, but for now its only used in NetGraphMsgHandler::new()
+ # which ends up resulting in a redundant constructor - both without arguments for the NetworkGraph.
+ args_known = False
+ pass
+ if not argument_conversion_info.arg_name in default_constructor_args:
+ default_constructor_args[argument_conversion_info.arg_name] = []
+ default_constructor_args[argument_conversion_info.arg_name].append(explode_arg_conv)
+ argument_types.append(argument_conversion_info)
+
+ out_java.write("\t// " + line)
+ (out_java_delta, out_c_delta, out_java_struct_delta) = consts.map_function(argument_types, c_call_string, method_name, return_type_info, struct_meth, default_constructor_args, takes_self, args_known, type_mapping_generator)
+ out_java.write(out_java_delta)
+
+ if is_free:
+ assert len(argument_types) == 1
+ assert return_type_info.c_ty == "void"
+ write_c(consts.c_fn_ty_pfx + "void " + consts.c_fn_name_define_pfx(method_name, True) + argument_types[0].c_ty + " " + argument_types[0].ty_info.var_name + ") {\n")
+ if argument_types[0].ty_info.passed_as_ptr and not argument_types[0].ty_info.rust_obj in opaque_structs:
+ write_c("\tif ((" + argument_types[0].ty_info.var_name + " & 1) != 0) return;\n")
+
+ for info in argument_types:
+ if info.arg_conv is not None:
+ write_c("\t" + info.arg_conv.replace('\n', "\n\t") + "\n")
+ assert c_call_string is None
+ write_c("\t" + method_name + "(")
+ if argument_types[0].arg_conv_name is not None:
+ write_c(argument_types[0].arg_conv_name)
+ write_c(");")
+ for info in argument_types:
+ if info.arg_conv_cleanup is not None:
+ write_c("\n\t" + info.arg_conv_cleanup.replace("\n", "\n\t"))
+ write_c("\n}\n\n")
+ else:
+ write_c(out_c_delta)
+
+ out_java_struct = None
+ if ("LDK" + struct_meth in opaque_structs or "LDK" + struct_meth in trait_structs) and not is_free:
+ out_java_struct = open(f"{sys.argv[3]}/structs/{struct_meth}{consts.file_ext}", "a")
+ elif method_name.startswith("C2Tuple_") and method_name.endswith("_read"):
+ struct_meth = method_name.rsplit("_", 1)[0]
+ out_java_struct = open(f"{sys.argv[3]}/structs/UtilMethods{consts.file_ext}", "a")
+ if out_java_struct is not None:
+ out_java_struct.write(out_java_struct_delta)
+
+ def map_unitary_enum(struct_name, field_lines):
+ with open(f"{sys.argv[3]}/enums/{struct_name}{consts.file_ext}", "w") as out_java_enum:
+ unitary_enums.add(struct_name)
+ for idx, struct_line in enumerate(field_lines):
+ if idx == 0:
+ assert(struct_line == "typedef enum %s {" % struct_name)
+ elif idx == len(field_lines) - 3:
+ assert(struct_line.endswith("_Sentinel,"))
+ elif idx == len(field_lines) - 2:
+ assert(struct_line == "} %s;" % struct_name)
+ elif idx == len(field_lines) - 1:
+ assert(struct_line == "")
+ (c_out, native_file_out, native_out) = consts.native_c_unitary_enum_map(struct_name, [x.strip().strip(",") for x in field_lines[1:-3]])
+ write_c(c_out)
+ out_java_enum.write(native_file_out)
+ out_java.write(native_out)
+
+ def map_complex_enum(struct_name, union_enum_items):
+ java_hu_type = struct_name.replace("LDK", "")
+ complex_enums.add(struct_name)
+
+ enum_variants = []
+ tag_field_lines = union_enum_items["field_lines"]
+ for idx, struct_line in enumerate(tag_field_lines):
+ if idx == 0:
+ assert(struct_line == "typedef enum %s_Tag {" % struct_name)
+ elif idx == len(tag_field_lines) - 3:
+ assert(struct_line.endswith("_Sentinel,"))
+ elif idx == len(tag_field_lines) - 2:
+ assert(struct_line == "} %s_Tag;" % struct_name)
+ elif idx == len(tag_field_lines) - 1:
+ assert(struct_line == "")