Merge pull request #34 from dsmiller95/main

Support code inside Module blocks
This commit is contained in:
Yoshifumi Kawai 2023-05-22 17:32:18 +09:00 committed by GitHub
commit 7f3e57eada
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 110 additions and 5 deletions

View File

@ -91,6 +91,14 @@ fn main() -> Result<(), Box<dyn Error>> {
.generate_csharp_file("../dotnet-sandbox/NativeMethods.cs")
.unwrap();
csbindgen::Builder::default()
.input_extern_file("src/nested_module_test.rs")
.csharp_class_name("NestedModuleTests")
.csharp_dll_name("csbindgen_tests_nested_module")
.csharp_use_function_pointer(true)
.generate_csharp_file("../dotnet-sandbox/NestedModuleTests.cs")
.unwrap();
csbindgen::Builder::new()
.input_bindgen_file("src/zstd.rs")
.method_filter(|x| x.starts_with("ZSTD_"))

View File

@ -0,0 +1,31 @@
mod nested_mod {
#[repr(C)]
pub struct NumberStruct {
pub num: i32,
}
type NumberStructAlias = NumberStruct;
#[no_mangle]
pub extern "C" fn triple_input(input: NumberStruct) -> i32 {
input.num * 3
}
#[no_mangle]
pub extern "C" fn triple_input_aliased(input: NumberStructAlias) -> i32 {
input.num * 3
}
#[repr(u8)]
pub enum NumberEnum {
One,
Two,
Three,
}
#[no_mangle]
pub extern "C" fn number_map(input: NumberEnum) -> i32 {
match input {
NumberEnum::One => 1,
NumberEnum::Two => 2,
NumberEnum::Three => 3,
_ => -1
}
}
}

View File

@ -8,12 +8,34 @@ enum FnItem {
Item(syn::ItemFn),
}
/// build a Vec of all Items, unless the Item is a Item::Mod, then append the Item contents of the vect
/// Do this recursively.
/// This is not memory-efficient, would work better with an iterator, but does not seem performance critical.
fn depth_first_module_walk<'a>(ast: &'a Vec<Item>) -> Vec<&'a syn::Item>{
let mut unwrapped_items : Vec<&syn::Item> = vec![];
for item in ast {
match item {
Item::Mod(m) => match &m.content {
Some((_, items)) => {
unwrapped_items.extend(depth_first_module_walk(items));
}
_ => {}
},
_ => {
unwrapped_items.push(item);
}
}
}
unwrapped_items
}
pub fn collect_foreign_method(
ast: &syn::File,
options: &BindgenOptions,
list: &mut Vec<ExternMethod>,
) {
for item in ast.items.iter() {
for item in depth_first_module_walk(&ast.items) {
if let Item::ForeignMod(m) = item {
for item in m.items.iter() {
if let ForeignItem::Fn(m) = item {
@ -32,7 +54,7 @@ pub fn collect_extern_method(
options: &BindgenOptions,
list: &mut Vec<ExternMethod>,
) {
for item in ast.items.iter() {
for item in depth_first_module_walk(&ast.items) {
if let Item::Fn(m) = item {
if m.sig.abi.is_some() {
// has extern
@ -112,7 +134,7 @@ fn parse_method(item: FnItem, options: &BindgenOptions) -> Option<ExternMethod>
}
pub fn collect_type_alias(ast: &syn::File, result: &mut AliasMap) {
for item in ast.items.iter() {
for item in depth_first_module_walk(&ast.items) {
if let Item::Type(t) = item {
let name = t.ident.to_string();
let alias = parse_type(&t.ty);
@ -137,7 +159,7 @@ pub fn collect_type_alias(ast: &syn::File, result: &mut AliasMap) {
pub fn collect_struct(ast: &syn::File, result: &mut Vec<RustStruct>) {
// collect union or struct
for item in ast.items.iter() {
for item in depth_first_module_walk(&ast.items) {
if let Item::Union(t) = item {
let struct_name = t.ident.to_string();
let fields = collect_fields(&t.fields);
@ -211,7 +233,7 @@ fn collect_fields_unnamed(fields: &syn::FieldsUnnamed) -> Vec<FieldMember> {
}
pub fn collect_enum(ast: &syn::File, result: &mut Vec<RustEnum>) {
for item in ast.items.iter() {
for item in depth_first_module_walk(&ast.items) {
if let Item::Enum(t) = item {
let mut repr = None;
for attr in &t.attrs {

44
dotnet-sandbox/NestedModuleTests.cs vendored Normal file
View File

@ -0,0 +1,44 @@
// <auto-generated>
// This code is generated by csbindgen.
// DON'T CHANGE THIS DIRECTLY.
// </auto-generated>
#pragma warning disable CS8500
#pragma warning disable CS8981
using System;
using System.Runtime.InteropServices;
namespace CsBindgen
{
internal static unsafe partial class NestedModuleTests
{
const string __DllName = "csbindgen_tests_nested_module";
[DllImport(__DllName, EntryPoint = "triple_input", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int triple_input(NumberStruct input);
[DllImport(__DllName, EntryPoint = "triple_input_aliased", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int triple_input_aliased(NumberStruct input);
[DllImport(__DllName, EntryPoint = "number_map", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern int number_map(NumberEnum input);
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe partial struct NumberStruct
{
public int num;
}
internal enum NumberEnum : byte
{
One,
Two,
Three,
}
}