+ c_ty = c_ty + "Array"
+ if var_is_arr is not None:
+ if var_is_arr.group(1) == "":
+ return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_hu_ty=java_ty, java_fn_ty_arg="[" + fn_ty_arg, c_ty=c_ty, is_const=is_const,
+ passed_as_ptr=False, is_ptr=False, nonnull_ptr=nonnull_ptr, var_name="arg",
+ arr_len=var_is_arr.group(2), arr_access=arr_access, is_native_primitive=False)
+ return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_hu_ty=java_ty, java_fn_ty_arg="[" + fn_ty_arg, c_ty=c_ty, is_const=is_const,
+ passed_as_ptr=False, is_ptr=False, nonnull_ptr=nonnull_ptr, var_name=var_is_arr.group(1),
+ arr_len=var_is_arr.group(2), arr_access=arr_access, is_native_primitive=False)
+
+ if java_hu_ty is None:
+ java_hu_ty = java_ty
+ return TypeInfo(rust_obj=rust_obj, java_ty=java_ty, java_hu_ty=java_hu_ty, java_fn_ty_arg=fn_ty_arg, c_ty=c_ty, passed_as_ptr=is_ptr or take_by_ptr,
+ is_const=is_const, is_ptr=is_ptr, nonnull_ptr=nonnull_ptr, var_name=fn_arg, arr_len=arr_len, arr_access=arr_access, is_native_primitive=is_primitive)
+
+fn_ptr_regex = re.compile("^extern const ([A-Za-z_0-9\* ]*) \(\*(.*)\)\((.*)\);$")
+fn_ret_arr_regex = re.compile("(.*) \(\*(.*)\((.*)\)\)\[([0-9]*)\];$")
+reg_fn_regex = re.compile("([A-Za-z_0-9\* ]* \*?)([a-zA-Z_0-9]*)\((.*)\);$")
+clone_fns = set()
+constructor_fns = {}
+
+from gen_type_mapping import TypeMappingGenerator
+type_mapping_generator = TypeMappingGenerator(java_c_types, consts, opaque_structs, clone_fns, unitary_enums, trait_structs, complex_enums, result_types, tuple_types)
+
+with open(sys.argv[1]) as in_h:
+ for line in in_h:
+ reg_fn = reg_fn_regex.match(line)
+ if reg_fn is not None:
+ if reg_fn.group(2).endswith("_clone"):
+ clone_fns.add(reg_fn.group(2))
+ else:
+ 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, doc_comment):
+ method_return_type = re_match.group(1)
+ method_name = re_match.group(2)
+ orig_method_name = str(method_name)
+ method_comma_separated_arguments = re_match.group(3)
+ method_arguments = method_comma_separated_arguments.split(',')
+
+ is_free = method_name.endswith("_free")
+ if method_name.startswith("COption") or method_name.startswith("CResult"):
+ struct_meth = method_name.rsplit("Z", 1)[0][1:] + "Z"
+ else:
+ struct_meth = method_name.split("_")[0]
+
+ return_type_info = type_mapping_generator.map_type(method_return_type.strip() + " ret", True, ret_arr_len, False, False)
+
+ argument_types = []
+ default_constructor_args = {}
+ takes_self = False
+ takes_self_ptr = 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.ty_info.is_ptr:
+ takes_self_ptr = 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)
+ if not takes_self and return_type_info.java_hu_ty != struct_meth:
+ if not return_type_info.java_hu_ty.startswith("Result_" + struct_meth):
+ method_name = orig_method_name
+ struct_meth = ""
+
+ 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, takes_self_ptr, args_known, type_mapping_generator, doc_comment)
+ 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
+ expected_struct = "LDK" + struct_meth
+ expected_cstruct = "LDKC" + struct_meth
+ if (expected_struct in opaque_structs or expected_struct in trait_structs
+ or expected_struct in complex_enums or expected_cstruct in complex_enums
+ or expected_cstruct in result_types) and not is_free:
+ out_java_struct = open(f"{sys.argv[3]}/structs/{struct_meth}{consts.file_ext}", "a")
+ out_java_struct.write(out_java_struct_delta)
+ elif (not is_free and not method_name.endswith("_clone") and
+ not method_name.startswith("_") and
+ method_name != "check_platform" and method_name != "Result_read" and
+ not expected_struct in unitary_enums and
+ ((not method_name.startswith("C2Tuple_") and not method_name.startswith("C3Tuple_"))
+ or method_name.endswith("_read"))):
+ out_java_struct = open(f"{sys.argv[3]}/structs/UtilMethods{consts.file_ext}", "a")
+ for line in out_java_struct_delta.splitlines():
+ if not line.strip().startswith("this."):
+ out_java_struct.write(line + "\n")
+ else:
+ out_java_struct.write("\t\t// " + line.strip() + "\n")
+
+ def map_unitary_enum(struct_name, field_lines, enum_doc_comment):
+ assert struct_name.startswith("LDK")
+ with open(f"{sys.argv[3]}/enums/{struct_name[3:]}{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 == "")
+ assert struct_name.startswith("LDK")
+ (c_out, native_file_out, native_out) = consts.native_c_unitary_enum_map(struct_name[3:], [x.strip().strip(",") for x in field_lines[1:-3]], enum_doc_comment)
+ 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, inline_enum_variants, enum_doc_comment):
+ java_hu_type = struct_name.replace("LDK", "").replace("COption", "Option")
+ 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 == "")