Skip to content

Commit 071b5fa

Browse files
committed
Use uintptr_t in generic conversion when we're talking about pointers
This may make it harder to support 32-bit Java platforms in the future if we have a reason to do so, but that seems unlikely and its possible we can simply redefine the type.
1 parent b0ba503 commit 071b5fa

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

gen_type_mapping.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -303,23 +303,23 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
303303
"// Thus, after this call, " + ty_info.var_name + " is reset to null and is now a dummy object.\n" + self.consts.set_null_skip_free(ty_info.var_name))
304304

305305
opaque_ret_conv_suf = ";\n"
306-
opaque_ret_conv_suf += "uint64_t " + ty_info.var_name + "_ref = 0;\n"
306+
opaque_ret_conv_suf += "uintptr_t " + ty_info.var_name + "_ref = 0;\n"
307307
indent = ""
308308
if is_nullable:
309-
opaque_ret_conv_suf += "if ((uint64_t)" + ty_info.var_name + "_var.inner > 4096) {\n"
309+
opaque_ret_conv_suf += "if ((uintptr_t)" + ty_info.var_name + "_var.inner > 4096) {\n"
310310
indent = "\t"
311311
if not holds_ref and ty_info.is_ptr and (ty_info.rust_obj.replace("LDK", "") + "_clone") in self.clone_fns: # is_ptr, not holds_ref implies passing a pointed-to value to java, which needs copied
312312
opaque_ret_conv_suf += indent + ty_info.var_name + "_var = " + ty_info.rust_obj.replace("LDK", "") + "_clone(" + ty_info.var_name + ");\n"
313313
elif not holds_ref and ty_info.is_ptr:
314314
opaque_ret_conv_suf += indent + "// Warning: we may need a move here but no clone is available for " + ty_info.rust_obj + "\n"
315315

316-
opaque_ret_conv_suf += indent + "CHECK((((uint64_t)" + ty_info.var_name + "_var.inner) & 1) == 0); // We rely on a free low bit, malloc guarantees this.\n"
317-
opaque_ret_conv_suf += indent + "CHECK((((uint64_t)&" + ty_info.var_name + "_var) & 1) == 0); // We rely on a free low bit, pointer alignment guarantees this.\n"
316+
opaque_ret_conv_suf += indent + "CHECK((((uintptr_t)" + ty_info.var_name + "_var.inner) & 1) == 0); // We rely on a free low bit, malloc guarantees this.\n"
317+
opaque_ret_conv_suf += indent + "CHECK((((uintptr_t)&" + ty_info.var_name + "_var) & 1) == 0); // We rely on a free low bit, pointer alignment guarantees this.\n"
318318
opaque_ret_conv_suf += "CHECK_INNER_FIELD_ACCESS_OR_NULL(" + ty_info.var_name + "_var);\n"
319319
if holds_ref:
320-
opaque_ret_conv_suf += indent + ty_info.var_name + "_ref = (uint64_t)" + ty_info.var_name + "_var.inner & ~1;"
320+
opaque_ret_conv_suf += indent + ty_info.var_name + "_ref = (uintptr_t)" + ty_info.var_name + "_var.inner & ~1;"
321321
else:
322-
opaque_ret_conv_suf += indent + ty_info.var_name + "_ref = (uint64_t)" + ty_info.var_name + "_var.inner;\n"
322+
opaque_ret_conv_suf += indent + ty_info.var_name + "_ref = (uintptr_t)" + ty_info.var_name + "_var.inner;\n"
323323
opaque_ret_conv_suf += indent + "if (" + ty_info.var_name + "_var.is_owned) {\n"
324324
opaque_ret_conv_suf += indent + "\t" + ty_info.var_name + "_ref |= 1;\n"
325325
opaque_ret_conv_suf += indent + "}"
@@ -357,7 +357,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
357357
arg_conv_cleanup = None,
358358
ret_conv = (ty_info.c_ty + " " + ty_info.var_name + "_conv = " + ret_pfx, ret_sfx + ";"),
359359
ret_conv_name = ty_info.var_name + "_conv", to_hu_conv = None, to_hu_conv_name = None, from_hu_conv = None)
360-
base_conv = "void* " + ty_info.var_name + "_ptr = (void*)(((uint64_t)" + ty_info.var_name + ") & ~1);\n"
360+
base_conv = "void* " + ty_info.var_name + "_ptr = (void*)(((uintptr_t)" + ty_info.var_name + ") & ~1);\n"
361361
base_conv += "CHECK_ACCESS(" + ty_info.var_name + "_ptr);\n"
362362
base_conv += ty_info.rust_obj + " " + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)(" + ty_info.var_name + "_ptr);"
363363
from_hu_conv = None
@@ -392,7 +392,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
392392
from_hu_conv = (from_hu_conv[0], self.consts.add_ref("this", ty_info.var_name))
393393
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
394394
arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
395-
ret_conv = ret_conv, ret_conv_name = "(uint64_t)" + ty_info.var_name + "_ret",
395+
ret_conv = ret_conv, ret_conv_name = "(uintptr_t)" + ty_info.var_name + "_ret",
396396
to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, "ret_hu_conv", "new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ")") + ";\n" + self.consts.add_ref("ret_hu_conv", "this") + ";",
397397
to_hu_conv_name = "ret_hu_conv", from_hu_conv = from_hu_conv)
398398
needs_full_clone = not is_free and (not ty_info.is_ptr or ty_info.requires_clone == True) and ty_info.requires_clone != False
@@ -405,7 +405,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
405405
# In the second case, we need to clone before returning to C (as once we return the GC can free the object),
406406
# whereas in the first we prefer to clone in C to avoid additional Java code as much as possible.
407407
if holds_ref:
408-
base_conv += "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone((" + ty_info.rust_obj + "*)(((uint64_t)" + ty_info.var_name + ") & ~1));"
408+
base_conv += "\n" + ty_info.var_name + "_conv = " + ty_info.rust_obj.replace("LDK", "") + "_clone((" + ty_info.rust_obj + "*)(((uintptr_t)" + ty_info.var_name + ") & ~1));"
409409
else:
410410
from_hu_conv = (ty_info.var_name + " == null ? 0 : " + ty_info.var_name + ".clone_ptr()", "")
411411
base_conv += "\n" + "FREE((void*)" + ty_info.var_name + ");"
@@ -431,11 +431,11 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
431431
base_conv += self.consts.trait_struct_inc_refcnt(optional_ty_info).\
432432
replace("\n", "\n\t").replace(ty_info.var_name + "_conv", ty_info.var_name + "_conv.some")
433433
base_conv += "\n}"
434-
ret_conv = ("uint64_t " + ty_info.var_name + "_ref = ((uint64_t)&", ") | 1;")
434+
ret_conv = ("uintptr_t " + ty_info.var_name + "_ref = ((uintptr_t)&", ") | 1;")
435435
if not holds_ref:
436436
ret_conv = (ty_info.rust_obj + " *" + ty_info.var_name + "_copy = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n", "")
437437
ret_conv = (ret_conv[0] + "*" + ty_info.var_name + "_copy = ", "")
438-
ret_conv = (ret_conv[0], ";\nuint64_t " + ty_info.var_name + "_ref = (uint64_t)" + ty_info.var_name + "_copy;")
438+
ret_conv = (ret_conv[0], ";\nuintptr_t " + ty_info.var_name + "_ref = (uintptr_t)" + ty_info.var_name + "_copy;")
439439
if from_hu_conv is None:
440440
from_hu_conv = (self.consts.get_ptr(ty_info.var_name), "")
441441
from_hu_conv = (from_hu_conv[0], to_hu_conv_sfx)
@@ -456,11 +456,11 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
456456
from_hu_conv = (ty_info.var_name + " != null ? " + self.consts.get_ptr(ty_info.var_name) + " : 0", "")
457457
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
458458
arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
459-
ret_conv = ret_conv, ret_conv_name = "(uint64_t)" + ty_info.var_name + "_conv",
459+
ret_conv = ret_conv, ret_conv_name = "(uintptr_t)" + ty_info.var_name + "_conv",
460460
to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_hu_conv", ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ")") + ";",
461461
to_hu_conv_name = ty_info.var_name + "_hu_conv", from_hu_conv = from_hu_conv)
462462
if ty_info.rust_obj in self.tuple_types:
463-
ret_conv_name = "((uint64_t)" + ty_info.var_name + "_conv)"
463+
ret_conv_name = "((uintptr_t)" + ty_info.var_name + "_conv)"
464464
if holds_ref:
465465
# If we're trying to return a ref, we have to clone.
466466
if (ty_info.rust_obj.replace("LDK", "") + "_clone") not in self.clone_fns:
@@ -497,16 +497,16 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
497497
if not ty_info.is_ptr and not holds_ref:
498498
ret_conv = ("LDKTxOut* " + ty_info.var_name + "_ref = MALLOC(sizeof(LDKTxOut), \"LDKTxOut\");\n*" + ty_info.var_name + "_ref = ", ";")
499499
else:
500-
ret_conv = ("uint64_t " + ty_info.var_name + "_ref = ((uint64_t)&", ") | 1;")
500+
ret_conv = ("uintptr_t " + ty_info.var_name + "_ref = ((uintptr_t)&", ") | 1;")
501501
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
502502
arg_conv = base_conv, arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
503-
ret_conv = ret_conv, ret_conv_name = "(uint64_t)" + ty_info.var_name + "_ref",
503+
ret_conv = ret_conv, ret_conv_name = "(uintptr_t)" + ty_info.var_name + "_ref",
504504
to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_conv", "new " +ty_info.java_hu_ty + "(null, " + ty_info.var_name + ")") + ";",
505505
to_hu_conv_name = ty_info.var_name + "_conv", from_hu_conv = (self.consts.get_ptr(ty_info.var_name), ""))
506506
elif ty_info.is_ptr:
507507
assert(not is_free)
508508
if ty_info.rust_obj in self.complex_enums:
509-
ret_conv = ("uint64_t ret_" + ty_info.var_name + " = (uint64_t)", " | 1; // Warning: We should clone here!")
509+
ret_conv = ("uintptr_t ret_" + ty_info.var_name + " = (uintptr_t)", " | 1; // Warning: We should clone here!")
510510
from_hu_sfx = self.consts.add_ref("this", ty_info.var_name)
511511
if ty_info.rust_obj.replace("LDK", "") + "_clone" in self.clone_fns:
512512
ret_conv_pfx = ty_info.rust_obj + " *ret_" + ty_info.var_name + " = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + " ret conversion\");\n"
@@ -516,13 +516,13 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
516516
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
517517
arg_conv = ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + ";",
518518
arg_conv_name = ty_info.var_name + "_conv", arg_conv_cleanup = None,
519-
ret_conv = ret_conv, ret_conv_name = "(uint64_t)ret_" + ty_info.var_name,
519+
ret_conv = ret_conv, ret_conv_name = "(uintptr_t)ret_" + ty_info.var_name,
520520
to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, ty_info.var_name + "_hu_conv", ty_info.java_hu_ty + ".constr_from_ptr(" + ty_info.var_name + ")") + ";",
521521
to_hu_conv_name = ty_info.var_name + "_hu_conv",
522522
from_hu_conv = (ty_info.var_name + " == null ? 0 : " + self.consts.get_ptr(ty_info.var_name) + " & ~1", from_hu_sfx))
523523
elif ty_info.rust_obj in self.trait_structs:
524524
if ty_info.nonnull_ptr:
525-
arg_conv = "void* " + ty_info.var_name + "_ptr = (void*)(((uint64_t)" + ty_info.var_name + ") & ~1);\n"
525+
arg_conv = "void* " + ty_info.var_name + "_ptr = (void*)(((uintptr_t)" + ty_info.var_name + ") & ~1);\n"
526526
arg_conv += "if (!(" + ty_info.var_name + " & 1)) { CHECK_ACCESS(" + ty_info.var_name + "_ptr); }\n"
527527
arg_conv += ty_info.rust_obj + "* " + ty_info.var_name + "_conv = (" + ty_info.rust_obj + "*)" + ty_info.var_name + "_ptr;"
528528
arg_conv_name = ty_info.var_name + "_conv"
@@ -532,7 +532,7 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
532532
arg_conv = ty_info.rust_obj + " *" + ty_info.var_name + "_conv_ptr = NULL;\n"
533533
arg_conv += "if (" + ty_info.var_name + " != 0) {\n"
534534
arg_conv += "\t" + ty_info.rust_obj + " " + ty_info.var_name + "_conv;\n"
535-
arg_conv += "void* " + ty_info.var_name + "_ptr = (void*)(((uint64_t)" + ty_info.var_name + ") & ~1);\n"
535+
arg_conv += "void* " + ty_info.var_name + "_ptr = (void*)(((uintptr_t)" + ty_info.var_name + ") & ~1);\n"
536536
arg_conv += "CHECK_ACCESS(" + ty_info.var_name + "_ptr);\n"
537537
arg_conv += "\t" + ty_info.var_name + "_conv = *(" + ty_info.rust_obj + "*)" + ty_info.var_name + "_ptr;"
538538
arg_conv += self.consts.trait_struct_inc_refcnt(ty_info).replace("\n", "\n\t")
@@ -545,19 +545,19 @@ def _do_map_type_with_info(self, ty_info, print_void, ret_arr_len, is_free, hold
545545
arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
546546
ret_conv = (ty_info.rust_obj + " *" + ty_info.var_name + "_clone = MALLOC(sizeof(" + ty_info.rust_obj + "), \"" + ty_info.rust_obj + "\");\n" +
547547
"*" + ty_info.var_name + "_clone = " + ty_info.rust_obj.replace("LDK", "") + "_clone(", ");"),
548-
ret_conv_name = "(uint64_t)" + ty_info.var_name + "_clone",
548+
ret_conv_name = "(uintptr_t)" + ty_info.var_name + "_clone",
549549
to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, "ret_hu_conv", "new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ")") + ";\n" + self.consts.add_ref("ret_hu_conv", "this") + ";",
550550
to_hu_conv_name = "ret_hu_conv",
551551
from_hu_conv = (ty_info.var_name + " == null ? 0 : " + self.consts.get_ptr(ty_info.var_name), ""))
552552
else:
553553
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,
554554
arg_conv = arg_conv, arg_conv_name = arg_conv_name, arg_conv_cleanup = None,
555-
ret_conv = ("// WARNING: This object doesn't live past this scope, needs clone!\nuint64_t ret_" + ty_info.var_name + " = ((uint64_t)", ") | 1;"),
555+
ret_conv = ("// WARNING: This object doesn't live past this scope, needs clone!\nuintptr_t ret_" + ty_info.var_name + " = ((uintptr_t)", ") | 1;"),
556556
ret_conv_name = "ret_" + ty_info.var_name,
557557
to_hu_conv = self.consts.var_decl_statement(ty_info.java_hu_ty, "ret_hu_conv", "new " + ty_info.java_hu_ty + "(null, " + ty_info.var_name + ")") + ";\n" + self.consts.add_ref("ret_hu_conv", "this") + ";",
558558
to_hu_conv_name = "ret_hu_conv",
559559
from_hu_conv = (ty_info.var_name + " == null ? 0 : " + self.consts.get_ptr(ty_info.var_name), self.consts.add_ref("this", ty_info.var_name)))
560-
ret_conv = ("uint64_t ret_" + ty_info.var_name + " = (uint64_t)", ";")
560+
ret_conv = ("uintptr_t ret_" + ty_info.var_name + " = (uintptr_t)", ";")
561561
if holds_ref:
562562
ret_conv = (ret_conv[0], " | 1;")
563563
return ConvInfo(ty_info = ty_info, arg_name = ty_info.var_name,

0 commit comments

Comments
 (0)