+ lastund = False
+ lastchar = char
+ if char.isnumeric():
+ lastund = True
+ return (ret + lastchar.lower()).strip("_")
+
+def doc_to_field_nullable(doc):
+ if doc is None:
+ return False
+ for line in doc.splitlines():
+ if "Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None" in line:
+ return True
+ return False
+
+def doc_to_params_ret_nullable(doc):
+ if doc is None:
+ return (set(), False)
+ params = set()
+ ret_null = False
+ for line in doc.splitlines():
+ if "may be NULL or all-0s to represent None" not in line:
+ continue
+ if "Note that the return value" in line:
+ ret_null = True
+ elif "Note that " in line:
+ param = line.split("Note that ")[1].split(" ")[0]
+ params.add(param)
+ return (params, ret_null)
+
+unitary_enums = set()
+# Map from enum name to "contains trait object"
+complex_enums = {}
+opaque_structs = set()
+trait_structs = {}
+result_types = set()
+tuple_types = {}
+
+var_is_arr_regex = re.compile("\(\* ?([A-za-z0-9_]*)\)\[([a-z0-9]*)\]")
+var_ty_regex = re.compile("([A-za-z_0-9]*)(.*)")
+java_c_types_none_allowed = True # Unset when we do the real pass that populates the above sets
+def java_c_types(fn_arg, ret_arr_len):
+ fn_arg = fn_arg.strip()
+ if fn_arg.startswith("MUST_USE_RES "):
+ fn_arg = fn_arg[13:]
+ is_const = False
+ if fn_arg.startswith("const "):
+ fn_arg = fn_arg[6:]
+ is_const = True
+ if fn_arg.startswith("struct "):
+ fn_arg = fn_arg[7:]
+ if fn_arg.startswith("enum "):
+ fn_arg = fn_arg[5:]
+ nonnull_ptr = "NONNULL_PTR" in fn_arg
+ fn_arg = fn_arg.replace("NONNULL_PTR", "")
+
+ is_ptr = False
+ take_by_ptr = False
+ rust_obj = None
+ arr_access = None
+ java_hu_ty = None
+ if fn_arg.startswith("LDKThirtyTwoBytes"):
+ fn_arg = "uint8_t (*" + fn_arg[18:] + ")[32]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKThirtyTwoBytes"
+ arr_access = "data"
+ elif fn_arg.startswith("LDKThirtyTwoU16s"):
+ fn_arg = "uint16_t (*" + fn_arg[17:] + ")[32]"
+ assert var_is_arr_regex.match(fn_arg[9:])
+ rust_obj = "LDKThirtyTwoU16s"
+ arr_access = "data"
+ elif fn_arg.startswith("LDKU128"):
+ if fn_arg == "LDKU128":
+ fn_arg = "LDKU128 arg"
+ if fn_arg.startswith("LDKU128*") or fn_arg.startswith("LDKU128 *"):
+ fn_arg = "uint8_t (" + fn_arg[8:] + ")[16]"
+ else:
+ fn_arg = "uint8_t (*" + fn_arg[8:] + ")[16]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKU128"
+ arr_access = "le_bytes"
+ elif fn_arg.startswith("LDKPublicKey"):
+ fn_arg = "uint8_t (*" + fn_arg[13:] + ")[33]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKPublicKey"
+ arr_access = "compressed_form"
+ elif fn_arg.startswith("LDKTweakedPublicKey"):
+ fn_arg = "uint8_t (*" + fn_arg[21:] + ")[32]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKTweakedPublicKey"
+ arr_access = "x_coordinate"
+ elif fn_arg.startswith("LDKSecretKey"):
+ fn_arg = "uint8_t (*" + fn_arg[13:] + ")[32]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKSecretKey"
+ arr_access = "bytes"
+ elif fn_arg.startswith("LDKECDSASignature"):
+ fn_arg = "uint8_t (*" + fn_arg[18:] + ")[64]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKECDSASignature"
+ arr_access = "compact_form"
+ elif fn_arg.startswith("LDKSchnorrSignature"):
+ fn_arg = "uint8_t (*" + fn_arg[20:] + ")[64]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKSchnorrSignature"
+ arr_access = "compact_form"
+ elif fn_arg.startswith("LDKRecoverableSignature"):
+ fn_arg = "uint8_t (*" + fn_arg[24:] + ")[68]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKRecoverableSignature"
+ arr_access = "serialized_form"
+ elif fn_arg.startswith("LDKThreeBytes"):
+ fn_arg = "uint8_t (*" + fn_arg[14:] + ")[3]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKThreeBytes"
+ arr_access = "data"
+ elif fn_arg.startswith("LDKFourBytes"):
+ fn_arg = "uint8_t (*" + fn_arg[13:] + ")[4]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKFourBytes"
+ arr_access = "data"
+ elif fn_arg.startswith("LDKSixteenBytes"):
+ fn_arg = "uint8_t (*" + fn_arg[16:] + ")[16]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKSixteenBytes"
+ arr_access = "data"
+ elif fn_arg.startswith("LDKTwentyBytes"):
+ fn_arg = "uint8_t (*" + fn_arg[15:] + ")[20]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKTwentyBytes"
+ arr_access = "data"
+ elif fn_arg.startswith("LDKTwelveBytes"):
+ fn_arg = "uint8_t (*" + fn_arg[15:] + ")[12]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKTwelveBytes"
+ arr_access = "data"
+ elif fn_arg.startswith("LDKu8slice"):
+ fn_arg = "uint8_t (*" + fn_arg[11:] + ")[datalen]"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ rust_obj = "LDKu8slice"
+ arr_access = "data"
+ elif fn_arg.startswith("LDKCVec_u8Z"):
+ fn_arg = "uint8_t (*" + fn_arg[12:] + ")[datalen]"
+ rust_obj = "LDKCVec_u8Z"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ arr_access = "data"
+ elif fn_arg.startswith("LDKTransaction ") or fn_arg == "LDKTransaction":
+ fn_arg = "uint8_t (*" + fn_arg[15:] + ")[datalen]"
+ rust_obj = "LDKTransaction"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ arr_access = "data"
+ elif fn_arg.startswith("LDKTransactionOutputs "):
+ fn_arg = "C2Tuple_ThirtyTwoBytesCVec_C2Tuple_u32TxOutZZZ"
+ rust_obj = "C2Tuple_ThirtyTwoBytesCVec_C2Tuple_u32TxOutZZZ"
+ elif fn_arg.startswith("LDKWitness ") or fn_arg == "LDKWitness":
+ if len(fn_arg) > 12 and fn_arg[11] == "*":
+ fn_arg = "uint8_t (" + fn_arg[11:] + ")[datalen]"
+ else:
+ fn_arg = "uint8_t (*" + fn_arg[11:] + ")[datalen]"
+ rust_obj = "LDKWitness"
+ assert var_is_arr_regex.match(fn_arg[8:])
+ arr_access = "data"
+ elif fn_arg.startswith("LDKCVec_"):
+ is_ptr = False
+ if "*" in fn_arg:
+ fn_arg = fn_arg.replace("*", "")
+ is_ptr = True
+
+ tyn = fn_arg[8:].split(" ")
+ assert tyn[0].endswith("Z")
+ if tyn[0] == "u64Z":
+ new_arg = "uint64_t"
+ else:
+ new_arg = "LDK" + tyn[0][:-1]
+ for a in tyn[1:]:
+ new_arg = new_arg + " " + a
+ res = java_c_types(new_arg, ret_arr_len)
+ if res is None:
+ assert java_c_types_none_allowed
+ return None
+ if is_ptr:
+ res.pass_by_ref = True
+ java_ty = consts.java_arr_ty_str(res.java_ty)
+ if res.is_native_primitive or res.passed_as_ptr:
+ return TypeInfo(rust_obj=fn_arg.split(" ")[0], java_ty=java_ty, java_hu_ty=res.java_hu_ty + "[]",
+ java_fn_ty_arg="[" + res.java_fn_ty_arg, c_ty=res.c_ty + "Array", passed_as_ptr=False, is_ptr=is_ptr,
+ nonnull_ptr=nonnull_ptr, is_const=is_const,
+ var_name=res.var_name, arr_len="datalen", arr_access="data", subty=res, is_native_primitive=False)
+ else:
+ return TypeInfo(rust_obj=fn_arg.split(" ")[0], java_ty=java_ty, java_hu_ty=res.java_hu_ty + "[]",
+ java_fn_ty_arg="[" + res.java_fn_ty_arg, c_ty=consts.ptr_arr, passed_as_ptr=False, is_ptr=is_ptr,
+ nonnull_ptr=nonnull_ptr, is_const=is_const,
+ var_name=res.var_name, arr_len="datalen", arr_access="data", subty=res, is_native_primitive=False)
+
+ is_primitive = False
+ contains_trait = False
+ arr_len = None
+ mapped_type = []
+ java_type_plural = None
+ arr_ty = None
+ if fn_arg.startswith("void"):
+ java_ty = "void"
+ c_ty = "void"
+ fn_ty_arg = "V"
+ fn_arg = fn_arg[4:].strip()
+ is_primitive = True
+ elif fn_arg.startswith("bool"):
+ java_ty = consts.c_type_map['bool'][0]
+ c_ty = "jboolean"
+ fn_ty_arg = "Z"
+ arr_ty = "bool"
+ fn_arg = fn_arg[4:].strip()
+ is_primitive = True
+ elif fn_arg.startswith("uint8_t"):
+ mapped_type = consts.c_type_map['uint8_t']
+ java_ty = mapped_type[0]
+ c_ty = "int8_t"
+ fn_ty_arg = "B"
+ arr_ty = "uint8_t"
+ fn_arg = fn_arg[7:].strip()
+ is_primitive = True
+ elif fn_arg.startswith("LDKU5") or fn_arg.startswith("LDKWitnessVersion"):
+ java_ty = consts.c_type_map['uint8_t'][0]
+ if fn_arg.startswith("LDKU5"):
+ java_hu_ty = "UInt5"
+ rust_obj = "LDKU5"
+ fn_arg = fn_arg[6:].strip()
+ else:
+ java_hu_ty = "WitnessVersion"
+ rust_obj = "LDKWitnessVersion"
+ fn_arg = fn_arg[18:].strip()
+ c_ty = "int8_t"
+ arr_ty = "uint8_t"
+ fn_ty_arg = "B"
+ elif fn_arg.startswith("uint16_t"):
+ mapped_type = consts.c_type_map['uint16_t']
+ java_ty = mapped_type[0]
+ c_ty = "int16_t"
+ arr_ty = "uint16_t"
+ fn_ty_arg = "S"
+ fn_arg = fn_arg[8:].strip()
+ is_primitive = True
+ elif fn_arg.startswith("uint32_t"):
+ mapped_type = consts.c_type_map['uint32_t']
+ java_ty = mapped_type[0]
+ c_ty = "int32_t"
+ arr_ty = "uint32_t"
+ fn_ty_arg = "I"
+ fn_arg = fn_arg[8:].strip()
+ is_primitive = True
+ elif fn_arg.startswith("int64_t"):
+ mapped_type = consts.c_type_map['int64_t']
+ java_ty = mapped_type[0]
+ c_ty = "int64_t"
+ arr_ty = "int64_t"
+ fn_ty_arg = "J"
+ fn_arg = fn_arg[7:].strip()
+ is_primitive = True
+ elif fn_arg.startswith("double"):
+ mapped_type = consts.c_type_map['double']
+ java_ty = mapped_type[0]
+ c_ty = "double"
+ arr_ty = "double"
+ fn_ty_arg = "D"
+ fn_arg = fn_arg[6:].strip()
+ is_primitive = True
+ elif fn_arg.startswith("uint64_t") or fn_arg.startswith("uintptr_t"):
+ # TODO: uintptr_t is arch-dependent :(
+ mapped_type = consts.c_type_map['uint64_t']
+ java_ty = mapped_type[0]
+ fn_ty_arg = "J"
+ if fn_arg.startswith("uint64_t"):
+ c_ty = "int64_t"
+ arr_ty = "uint64_t"
+ fn_arg = fn_arg[8:].strip()
+ else:
+ java_ty = consts.usize_native_ty
+ c_ty = consts.usize_c_ty
+ arr_ty = "uintptr_t"
+ rust_obj = "uintptr_t"
+ fn_arg = fn_arg[9:].strip()
+ is_primitive = True
+ elif is_const and fn_arg.startswith("char *"):
+ java_ty = consts.java_type_map["String"]
+ java_hu_ty = consts.java_hu_type_map["String"]
+ c_ty = "const char*"
+ arr_ty = "LDKStr"
+ fn_ty_arg = "Ljava/lang/String;"
+ fn_arg = fn_arg[6:].strip()
+ elif fn_arg.startswith("LDKStr") or fn_arg.startswith("LDKAddress"):
+ rust_obj = "LDKStr"
+ arr_ty = "LDKStr"
+ java_ty = consts.java_type_map["String"]
+ java_hu_ty = consts.java_hu_type_map["String"]
+ c_ty = "jstring"
+ fn_ty_arg = "Ljava/lang/String;"
+ if fn_arg.startswith("LDKAddress"):
+ fn_arg = fn_arg[10:].strip()
+ else:
+ fn_arg = fn_arg[6:].strip()
+ arr_access = "chars"
+ arr_len = "len"
+ elif fn_arg.startswith("LDKError ") or fn_arg == "LDKError":
+ java_ty = consts.c_type_map['uint32_t'][0]
+ java_hu_ty = "UnqualifiedError"
+ rust_obj = "LDKError"
+ c_ty = "int32_t"
+ arr_ty = "uint32_t"
+ fn_ty_arg = "I"
+ fn_arg = fn_arg[8:].strip()
+ else:
+ ma = var_ty_regex.match(fn_arg)
+ arr_ty = ma.group(1).strip()
+ if ma.group(1).strip() in unitary_enums:
+ assert ma.group(1).strip().startswith("LDK")
+ java_ty = ma.group(1).strip()[3:]
+ java_hu_ty = java_ty
+ c_ty = consts.unitary_enum_c_ty
+ fn_ty_arg = "Lorg/ldk/enums/" + java_ty + ";"
+ fn_arg = ma.group(2).strip()
+ rust_obj = ma.group(1).strip()
+ else:
+ c_ty = consts.ptr_c_ty
+ java_ty = consts.ptr_native_ty
+ java_hu_ty = ma.group(1).strip()
+ java_hu_ty = java_hu_ty.replace("LDKCOption", "Option")
+ java_hu_ty = java_hu_ty.replace("LDKCResult", "Result")
+ java_hu_ty = java_hu_ty.replace("LDKC2Tuple", "TwoTuple")
+ java_hu_ty = java_hu_ty.replace("LDKC3Tuple", "ThreeTuple")
+ java_hu_ty = java_hu_ty.replace("LDKC4Tuple", "FourTuple")
+ java_hu_ty = java_hu_ty.replace("LDK", "")
+ fn_ty_arg = "J"
+ fn_arg = ma.group(2).strip()
+ rust_obj = ma.group(1).strip()
+ if rust_obj in trait_structs:
+ contains_trait = True
+ elif rust_obj in complex_enums:
+ contains_trait = complex_enums[rust_obj]
+ take_by_ptr = True
+
+ if fn_arg.startswith(" *") or fn_arg.startswith("*"):
+ fn_arg = fn_arg.replace("*", "").strip()
+ is_ptr = True
+ c_ty = consts.ptr_c_ty
+ java_ty = consts.ptr_native_ty
+ fn_ty_arg = "J"
+
+ var_is_arr = var_is_arr_regex.match(fn_arg)
+ subty = None
+ if var_is_arr is not None or ret_arr_len is not None:
+ assert(not take_by_ptr)
+ assert(not is_ptr)
+ # is there a special case for plurals?
+ if len(mapped_type) == 3:
+ java_ty = mapped_type[1]
+ java_hu_ty = mapped_type[2]
+ else:
+ java_ty = java_ty + "[]"
+ java_hu_ty = java_ty
+ if rust_obj == "LDKU128":
+ java_hu_ty = consts.u128_native_ty
+ c_ty = c_ty + "Array"
+
+ subty = java_c_types(arr_ty, None)
+ if subty is None:
+ assert java_c_types_none_allowed
+ return None
+ if is_ptr:
+ subty.pass_by_ref = True
+
+ 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_hu_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", subty=subty,
+ arr_len=var_is_arr.group(2), arr_access=arr_access, is_native_primitive=False, contains_trait=contains_trait)
+ 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, is_const=is_const,
+ passed_as_ptr=False, is_ptr=False, nonnull_ptr=nonnull_ptr, var_name=var_is_arr.group(1), subty=subty,
+ arr_len=var_is_arr.group(2), arr_access=arr_access, is_native_primitive=False, contains_trait=contains_trait)
+
+ 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,
+ contains_trait=contains_trait, subty=subty)
+
+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\n")
+
+
+write_c("static inline void* untag_ptr(uint64_t ptr) {\n")
+write_c("\tif (ptr < 4096) return (void*)ptr;\n")
+write_c("\tif (sizeof(void*) == 4) {\n")
+write_c("\t\t// For 32-bit systems, store pointers as 64-bit ints and use the 31st bit\n")
+write_c("\t\treturn (void*)(uintptr_t)ptr;\n")
+write_c("\t} else {\n")
+write_c("\t\t// For 64-bit systems, assume the top byte is used for tagging, then\n")
+write_c("\t\t// use bit 9 ^ bit 10.\n")
+write_c("\t\tuint64_t tenth_bit = (((uintptr_t)ptr) & (1ULL << 54)) >> 54;\n")
+write_c("\t\tuintptr_t p = (ptr & ~(1ULL << 55)) | (tenth_bit << 55);\n")
+write_c("#ifdef LDK_DEBUG_BUILD\n")
+write_c("\t\t// On debug builds we also use the 11th bit as a debug flag\n")
+write_c("\t\tuintptr_t eleventh_bit = (((uintptr_t)ptr) & (1ULL << 53)) >> 53;\n")
+write_c("\t\tCHECK(tenth_bit != eleventh_bit);\n")
+write_c("\t\tp ^= 1ULL << 53;\n")
+write_c("#endif\n")
+write_c("\t\treturn (void*)p;\n")
+write_c("\t}\n")
+write_c("}\n")
+
+write_c("static inline bool ptr_is_owned(uint64_t ptr) {\n")
+write_c("\tif(ptr < 4096) return true;\n")
+write_c("\tif (sizeof(void*) == 4) {\n")
+write_c("\t\treturn ptr & (1ULL << 32);\n")
+write_c("\t} else {\n")
+write_c("\t\tuintptr_t ninth_bit = (((uintptr_t)ptr) & (1ULL << 55)) >> 55;\n")
+write_c("\t\tuintptr_t tenth_bit = (((uintptr_t)ptr) & (1ULL << 54)) >> 54;\n")
+write_c("#ifdef LDK_DEBUG_BUILD\n")
+write_c("\t\t// On debug builds we also use the 11th bit as a debug flag\n")
+write_c("\t\tuintptr_t eleventh_bit = (((uintptr_t)ptr) & (1ULL << 53)) >> 53;\n")
+write_c("\t\tCHECK(tenth_bit != eleventh_bit);\n")
+write_c("#endif\n")
+write_c("\t\treturn (ninth_bit ^ tenth_bit) ? true : false;\n")
+write_c("\t}\n")
+write_c("}\n")
+
+write_c("static inline uint64_t tag_ptr(const void* ptr, bool is_owned) {\n")
+write_c("\tif ((uintptr_t)ptr < 4096) return (uint64_t)ptr;\n")
+write_c("\tif (sizeof(void*) == 4) {\n")
+write_c("\t\treturn (((uint64_t)ptr) | ((is_owned ? 1ULL : 0) << 32));\n")
+write_c("\t} else {\n")
+write_c("\t\tCHECK(sizeof(uintptr_t) == 8);\n")
+write_c("\t\tuintptr_t tenth_bit = (((uintptr_t)ptr) & (1ULL << 54)) >> 54;\n")
+write_c("\t\tuintptr_t t = (((uintptr_t)ptr) | (((is_owned ? 1ULL : 0ULL) ^ tenth_bit) << 55));\n")
+write_c("#ifdef LDK_DEBUG_BUILD\n")
+write_c("\t\tuintptr_t ninth_bit = (((uintptr_t)ptr) & (1ULL << 55)) >> 55;\n")
+write_c("\t\tuintptr_t eleventh_bit = (((uintptr_t)ptr) & (1ULL << 53)) >> 53;\n")
+write_c("\t\tCHECK(ninth_bit == tenth_bit);\n")
+write_c("\t\tCHECK(ninth_bit == eleventh_bit);\n")
+write_c("\t\tt ^= 1ULL << 53;\n")
+write_c("#endif\n")
+write_c("\t\tCHECK(ptr_is_owned(t) == is_owned);\n")
+write_c("\t\tCHECK(untag_ptr(t) == ptr);\n")
+#write_c("\t\tCHECK(untag_ptr((uintptr_t)untag_ptr(t)) == ptr);\n")
+write_c("\t\treturn t;\n")
+write_c("\t}\n")
+write_c("}\n\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(f"{sys.argv[2]}/bindings{consts.file_ext}", "w") as out_java:
+ # Map a top-level function
+ def map_fn(line, re_match, ret_arr_len, c_call_string, doc_comment):
+ map_fn_with_ref_option(line, re_match, ret_arr_len, c_call_string, doc_comment, False)
+ def map_fn_with_ref_option(line, re_match, ret_arr_len, c_call_string, doc_comment, force_holds_ref):
+ 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(',')
+
+ if method_name.startswith("__"):
+ return
+
+ 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"
+ expected_struct = "LDKC" + struct_meth
+ struct_meth_name = method_name[len(struct_meth) + 1:].strip("_")
+ elif method_name.startswith("C2Tuple") or method_name.startswith("C3Tuple") or method_name.startswith("C4Tuple"):
+ tuple_name = method_name.rsplit("Z", 1)[0][2:] + "Z"
+ if method_name.startswith("C2Tuple"):
+ struct_meth = "Two" + tuple_name
+ expected_struct = "LDKC2" + tuple_name
+ elif method_name.startswith("C3Tuple"):
+ struct_meth = "Three" + tuple_name
+ expected_struct = "LDKC3" + tuple_name