Skip to content

Added "delayed" generation of delegate signatures (follow-up) #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions csbindgen/src/emitter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use crate::alias_map::AliasMap;
use crate::builder::BindgenOptions;
use crate::type_meta::ExportSymbolNaming::{ExportName, NoMangle};
Expand Down Expand Up @@ -88,6 +90,7 @@ pub fn emit_csharp(
let class_name = &options.csharp_class_name;
let method_prefix = &options.csharp_method_prefix;
let accessibility = &options.csharp_class_accessibility;
let mut forward_decls: HashSet<String> = HashSet::new();

let mut dll_name = match options.csharp_if_symbol.as_str() {
"" => format!(
Expand Down Expand Up @@ -126,6 +129,7 @@ pub fn emit_csharp(
aliases,
method_name,
&"return".to_string(),
&mut forward_decls,
) {
method_list_string.push_str(
format!(" [UnmanagedFunctionPointer(CallingConvention.Cdecl)]\n")
Expand All @@ -143,6 +147,7 @@ pub fn emit_csharp(
aliases,
method_name,
&p.name,
&mut forward_decls,
) {
method_list_string.push_str(
format!(" [UnmanagedFunctionPointer(CallingConvention.Cdecl)]\n")
Expand All @@ -164,7 +169,7 @@ pub fn emit_csharp(
};
let return_type = match &item.return_type {
Some(x) => {
x.to_csharp_string(options, aliases, false, method_name, &"return".to_string())
x.to_csharp_string(options, aliases, false, method_name, &"return".to_string(), &mut forward_decls)
}
None => "void".to_string(),
};
Expand All @@ -175,7 +180,7 @@ pub fn emit_csharp(
.map(|p| {
let mut type_name =
p.rust_type
.to_csharp_string(options, aliases, false, method_name, &p.name);
.to_csharp_string(options, aliases, false, method_name, &p.name, &mut forward_decls);
if type_name == "bool" {
type_name = "[MarshalAs(UnmanagedType.U1)] bool".to_string();
}
Expand Down Expand Up @@ -226,6 +231,7 @@ pub fn emit_csharp(
true,
&"".to_string(),
&"".to_string(),
&mut forward_decls,
);
let attr = if type_name == "bool" {
"[MarshalAs(UnmanagedType.U1)] ".to_string()
Expand Down Expand Up @@ -304,6 +310,7 @@ pub fn emit_csharp(
false,
&"".to_string(),
&"".to_string(),
&mut forward_decls,
);

// special case for string, char, ByteStr
Expand Down Expand Up @@ -347,6 +354,8 @@ pub fn emit_csharp(
let class_string = if method_list_string.is_empty() && const_string.is_empty() {
String::new()
} else {
let forward_decls_string = forward_decls.drain().collect::<Vec<_>>().join("\n");

format!(
"{accessibility} static unsafe partial class {class_name}
{{
Expand All @@ -355,6 +364,8 @@ pub fn emit_csharp(
{const_string}

{method_list_string}

{forward_decls_string}
}}"
)
};
Expand Down
25 changes: 24 additions & 1 deletion csbindgen/src/type_meta.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use crate::{alias_map::AliasMap, builder::BindgenOptions};

pub fn escape_csharp_name(str: &str) -> String {
Expand Down Expand Up @@ -228,6 +230,7 @@ impl RustType {
emit_from_struct: bool,
method_name: &String,
parameter_name: &String,
forward_decls: &mut HashSet<String>,
) -> String {
fn convert_type_name(type_name: &str, options: &BindgenOptions) -> String {
let temp_string: String;
Expand Down Expand Up @@ -308,6 +311,7 @@ impl RustType {
emit_from_struct,
method_name,
parameter_name,
forward_decls,
)
} else {
convert_type_name(use_type.type_name.as_str(), options).to_string()
Expand Down Expand Up @@ -347,6 +351,7 @@ impl RustType {
emit_from_struct,
method_name,
parameter_name,
forward_decls,
));
sb.push_str(", ");
}
Expand All @@ -358,6 +363,7 @@ impl RustType {
emit_from_struct,
method_name,
parameter_name,
forward_decls,
));
}
None => {
Expand All @@ -366,7 +372,15 @@ impl RustType {
};
sb.push('>');
} else {
sb.push_str(build_method_delegate_name(method_name, parameter_name).as_str());
let delegate_name = build_method_delegate_name(method_name, parameter_name);

sb.push_str(&delegate_name);

let decl = build_method_delegate_if_required(self, options, alias_map, method_name, parameter_name, forward_decls);

if let Some(decl) = decl {
forward_decls.insert(format!(" [UnmanagedFunctionPointer(CallingConvention.Cdecl)]\n public {decl};"));
}
}
}
TypeKind::Option(inner) => {
Expand All @@ -379,6 +393,7 @@ impl RustType {
emit_from_struct,
method_name,
parameter_name,
forward_decls,
)
.as_str(),
);
Expand All @@ -393,6 +408,7 @@ impl RustType {
method_name: &String,
parameter_name: &String,
emit_inner: bool,
forward_decls: &mut HashSet<String>,
) -> bool {
use PointerType::*;
if let TypeKind::Pointer(p, inner) = &rust_type.type_kind {
Expand All @@ -405,6 +421,7 @@ impl RustType {
emit_from_struct,
method_name,
parameter_name,
forward_decls,
)
.as_str(),
);
Expand Down Expand Up @@ -436,6 +453,7 @@ impl RustType {
method_name,
parameter_name,
emit_inner,
forward_decls,
) {
sb.push_str(type_csharp_string.as_str());
}
Expand All @@ -452,6 +470,7 @@ impl RustType {
method_name,
parameter_name,
emit_inner,
forward_decls,
) {
if emit_inner {
sb.push_str(type_csharp_string.as_str());
Expand All @@ -470,6 +489,7 @@ pub fn build_method_delegate_if_required(
alias_map: &AliasMap,
method_name: &String,
parameter_name: &String,
forward_decls: &mut HashSet<String>,
) -> Option<String> {
let emit_from_struct = false;

Expand All @@ -487,6 +507,7 @@ pub fn build_method_delegate_if_required(
emit_from_struct,
method_name,
parameter_name,
forward_decls,
),
None => "void".to_string(),
};
Expand All @@ -501,6 +522,7 @@ pub fn build_method_delegate_if_required(
emit_from_struct,
method_name,
parameter_name,
forward_decls,
);
let parameter_name = if p.name == "" {
format!("arg{}", index + 1)
Expand All @@ -525,6 +547,7 @@ pub fn build_method_delegate_if_required(
alias_map,
method_name,
parameter_name,
forward_decls,
),
_ => None,
}
Expand Down
2 changes: 2 additions & 0 deletions dotnet-sandbox/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ internal static unsafe partial class NativeMethods
internal static extern void free_treat_as_empty_struct_context(TreatAsEmptyStruct* _src);




}

[StructLayout(LayoutKind.Sequential)]
Expand Down
2 changes: 2 additions & 0 deletions dotnet-sandbox/NestedModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ internal static unsafe partial class NestedModuleTests
internal static extern int number_map(NumberEnum input);




}

[StructLayout(LayoutKind.Sequential)]
Expand Down
2 changes: 2 additions & 0 deletions dotnet-sandbox/bullet3_bindgen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,8 @@ internal static unsafe partial class LibBullet3
internal static extern void b3RotateVector(double* quat, double* vec, double* vecOut);




}

[StructLayout(LayoutKind.Sequential)]
Expand Down
2 changes: 2 additions & 0 deletions dotnet-sandbox/libphysx_csbindgen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15540,6 +15540,8 @@ internal static unsafe partial class LibPhysx
internal static extern PxPvdTransport* phys_PxDefaultPvdFileTransportCreate(byte* name);




}

[StructLayout(LayoutKind.Sequential)]
Expand Down
2 changes: 2 additions & 0 deletions dotnet-sandbox/libpng16_csbindgen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,8 @@ internal static unsafe partial class LibPng16
internal static extern int png_set_option(png_struct_def* png_ptr, int option, int onoff);




}

[StructLayout(LayoutKind.Sequential)]
Expand Down
2 changes: 2 additions & 0 deletions dotnet-sandbox/lz4_bindgen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,8 @@ public static unsafe partial class LibLz4
public static extern void LZ4F_resetDecompressionContext(LZ4F_dctx_s* dctx);




}

[StructLayout(LayoutKind.Sequential)]
Expand Down
2 changes: 2 additions & 0 deletions dotnet-sandbox/quiche_bindgen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ internal static unsafe partial class LibQuiche
internal static extern void quiche_h3_conn_free(quiche_h3_conn* conn);




}

[StructLayout(LayoutKind.Sequential)]
Expand Down
2 changes: 2 additions & 0 deletions dotnet-sandbox/zstd_bindgen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,8 @@ internal static unsafe partial class LibZstd
internal static extern nuint ZSTD_sizeof_DDict(ZSTD_DDict_s* ddict);




}

[StructLayout(LayoutKind.Sequential)]
Expand Down