support tuple struct

This commit is contained in:
neuecc 2023-04-15 20:38:44 +09:00
parent 168936d9ff
commit 32af3f29e0
3 changed files with 60 additions and 5 deletions

View File

@ -124,6 +124,16 @@ pub extern "C" fn comment_one() {
pub extern "C" fn long_jpn_comment() { pub extern "C" fn long_jpn_comment() {
} }
#[repr(C)]
pub struct my_int_vec3(i32,i32,i32);
pub extern "C" fn use_vec3(_v3: my_int_vec3) {
}
#[repr(C)] #[repr(C)]
pub struct NfcCard { pub struct NfcCard {
pub delegate: unsafe extern "C" fn(ByteArray) -> ByteArray pub delegate: unsafe extern "C" fn(ByteArray) -> ByteArray

View File

@ -1,6 +1,6 @@
use crate::{alias_map::AliasMap, builder::BindgenOptions, field_map::FieldMap, type_meta::*}; use crate::{alias_map::AliasMap, builder::BindgenOptions, field_map::FieldMap, type_meta::*};
use regex::Regex; use regex::Regex;
use std::{collections::HashSet, fmt::format}; use std::{collections::HashSet};
use syn::{ForeignItem, Item, Pat, ReturnType}; use syn::{ForeignItem, Item, Pat, ReturnType};
enum FnItem { enum FnItem {
@ -156,7 +156,15 @@ pub fn collect_struct(ast: &syn::File, result: &mut Vec<RustStruct>) {
fields, fields,
is_union: false, is_union: false,
}); });
}; } else if let syn::Fields::Unnamed(f) = &t.fields {
let struct_name = t.ident.to_string();
let fields = collect_fields_unnamed(f);
result.push(RustStruct {
struct_name,
fields,
is_union: false,
});
}
} }
} }
} }
@ -177,6 +185,23 @@ fn collect_fields(fields: &syn::FieldsNamed) -> Vec<FieldMember> {
result result
} }
fn collect_fields_unnamed(fields: &syn::FieldsUnnamed) -> Vec<FieldMember> {
let mut result = Vec::new();
let mut i = 0;
for field in &fields.unnamed {
i += 1;
let name = format!("Item{i}");
let t = parse_type(&field.ty);
result.push(FieldMember {
name: name,
rust_type: t,
});
}
result
}
pub fn collect_enum(ast: &syn::File, result: &mut Vec<RustEnum>) { pub fn collect_enum(ast: &syn::File, result: &mut Vec<RustEnum>) {
for item in ast.items.iter() { for item in ast.items.iter() {
if let Item::Enum(t) = item { if let Item::Enum(t) = item {
@ -208,7 +233,7 @@ pub fn collect_enum(ast: &syn::File, result: &mut Vec<RustEnum>) {
enum_name, enum_name,
fields, fields,
repr, repr,
is_flags: false is_flags: false,
}); });
} else if let Item::Macro(t) = item { } else if let Item::Macro(t) = item {
let last_segment = t.mac.path.segments.last().unwrap(); let last_segment = t.mac.path.segments.last().unwrap();
@ -238,7 +263,16 @@ pub fn collect_enum(ast: &syn::File, result: &mut Vec<RustEnum>) {
.map(|x| { .map(|x| {
( (
x.get(1).unwrap().as_str().to_string(), x.get(1).unwrap().as_str().to_string(),
Some(x.get(2).unwrap().as_str().to_string().replace("Self :: ", "").replace(" . bits", "").trim().to_string()), Some(
x.get(2)
.unwrap()
.as_str()
.to_string()
.replace("Self :: ", "")
.replace(" . bits", "")
.trim()
.to_string(),
),
) )
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -247,7 +281,7 @@ pub fn collect_enum(ast: &syn::File, result: &mut Vec<RustEnum>) {
enum_name, enum_name,
fields, fields,
repr, repr,
is_flags: true is_flags: true,
}); });
} }
} }

View File

@ -24,6 +24,9 @@ namespace CsBindgen
[DllImport(__DllName, EntryPoint = "long_jpn_comment", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [DllImport(__DllName, EntryPoint = "long_jpn_comment", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void long_jpn_comment(); public static extern void long_jpn_comment();
[DllImport(__DllName, EntryPoint = "use_vec3", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void use_vec3(my_int_vec3 _v3);
[DllImport(__DllName, EntryPoint = "other_2", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [DllImport(__DllName, EntryPoint = "other_2", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void other_2(NfcCard _hoge); public static extern void other_2(NfcCard _hoge);
@ -157,6 +160,14 @@ namespace CsBindgen
public fixed byte png_name[5]; public fixed byte png_name[5];
} }
[StructLayout(LayoutKind.Sequential)]
internal unsafe partial struct my_int_vec3
{
public int Item1;
public int Item2;
public int Item3;
}
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
internal unsafe partial struct NfcCard internal unsafe partial struct NfcCard
{ {