self.docs = docs
class ComplexEnumVariantInfo:
- def __init__(self, var_name, fields):
+ def __init__(self, var_name, fields, tuple_variant):
self.var_name = var_name
self.fields = fields
+ self.tuple_variant = tuple_variant
out_java_enum.write(native_file_out)
out_java.write(native_out)
- def map_complex_enum(struct_name, union_enum_items, enum_doc_comment):
+ 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)
for idx, field in enumerate(enum_var_lines):
if idx != 0 and idx < len(enum_var_lines) - 2:
fields.append(type_mapping_generator.map_type(field.strip(' ;'), False, None, False, True))
- else:
- # TODO: Assert line format
- pass
+ enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, False))
+ elif camel_to_snake(variant_name) in inline_enum_variants:
+ fields.append(type_mapping_generator.map_type(inline_enum_variants[camel_to_snake(variant_name)] + " " + camel_to_snake(variant_name), False, None, False, True))
+ enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, True))
else:
- # TODO: Assert line format
- pass
- enum_variants.append(ComplexEnumVariantInfo(variant_name, fields))
+ enum_variants.append(ComplexEnumVariantInfo(variant_name, fields, True))
with open(f"{sys.argv[3]}/structs/{java_hu_type}{consts.file_ext}", "w") as out_java_enum:
(out_java_addendum, out_java_enum_addendum, out_c_addendum) = consts.map_complex_enum(struct_name, enum_variants, camel_to_snake, enum_doc_comment)
enum_var_name = struct_name.split("_")
union_enum_items[enum_var_name[0]][enum_var_name[1]] = field_lines
elif struct_name in union_enum_items:
- map_complex_enum(struct_name, union_enum_items[struct_name], last_block_comment)
+ tuple_variants = {}
+ elem_items = -1
+ for line in field_lines:
+ if line == " struct {":
+ elem_items = 0
+ elif line == " };":
+ elem_items = -1
+ elif elem_items > -1:
+ line = line.strip()
+ if line.startswith("struct "):
+ line = line[7:]
+ split = line.split(" ")
+ assert len(split) == 2
+ tuple_variants[split[1].strip(";")] = split[0]
+ elem_items += 1
+ if elem_items > 1:
+ # We don't currently support tuple variant with more than one element
+ assert False
+ map_complex_enum(struct_name, union_enum_items[struct_name], tuple_variants, last_block_comment)
last_block_comment = None
elif is_unitary_enum:
map_unitary_enum(struct_name, field_lines, last_block_comment)
out_c += (self.c_complex_enum_pfx(struct_name, [x.var_name for x in variant_list], init_meth_jty_strs))
out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + " " + self.c_fn_name_define_pfx(struct_name + "_ref_from_ptr", True) + self.ptr_c_ty + " ptr) {\n")
- out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n")
+ out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)(ptr & ~1);\n")
out_c += ("\tswitch(obj->tag) {\n")
for var in variant_list:
out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n")
for idx, field_map in enumerate(var.fields):
if field_map.ret_conv is not None:
out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
- out_c += ("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
+ if var.tuple_variant:
+ out_c += "obj->" + camel_to_snake(var.var_name)
+ else:
+ out_c += "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name
out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
c_params.append(field_map.ret_conv_name)
else:
- c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
+ if var.tuple_variant:
+ c_params.append("obj->" + camel_to_snake(var.var_name))
+ else:
+ c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var.var_name, c_params) + ";\n")
out_c += ("\t\t}\n")
out_c += ("\t\tdefault: abort();\n")
out_java += ("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n");
out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + self.c_fn_name_define_pfx(struct_name + "_ref_from_ptr", True) + self.ptr_c_ty + " ptr) {\n")
- out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n")
+ out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)(ptr & ~1);\n")
out_c += ("\tswitch(obj->tag) {\n")
for var in variant_list:
out_c += ("\t\tcase " + struct_name + "_" + var.var_name + ": {\n")
for idx, field_map in enumerate(var.fields):
if field_map.ret_conv is not None:
out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t"))
- out_c += ("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
+ if var.tuple_variant:
+ out_c += "obj->" + camel_to_snake(var.var_name)
+ else:
+ out_c += "obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name
out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n")
c_params.append(field_map.ret_conv_name)
else:
- c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
+ if var.tuple_variant:
+ c_params.append("obj->" + camel_to_snake(var.var_name))
+ else:
+ c_params.append("obj->" + camel_to_snake(var.var_name) + "." + field_map.arg_name)
out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var.var_name, c_params) + ";\n")
out_c += ("\t\t}\n")
out_c += ("\t\tdefault: abort();\n")