Skip to content

Generate C compatible types, functions, and header #1480

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

Merged
merged 20 commits into from
Jun 3, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
even more clippy lints
  • Loading branch information
kczimm committed May 22, 2024
commit efbceed7571db3b55b9fe9abf0590373a52e330c
73 changes: 10 additions & 63 deletions pgml-sdks/rust-bridge/rust-bridge-macros/src/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,8 @@ pub fn generate_c_methods(
}
let method_ident = method.method_ident.clone();

let (
go_function_arguments,
go_arguments_prep,
mut c_function_arguments,
c_argument_prep,
rust_function_arguments,
) = get_method_arguments(&wrapped_type_ident, &name_ident, &method);
let (mut c_function_arguments, c_argument_prep, rust_function_arguments) =
get_method_arguments(&wrapped_type_ident, &name_ident, &method);

let method_name = format_ident!(
"pgml_{}_{}",
Expand Down Expand Up @@ -203,11 +198,7 @@ fn get_method_arguments(
proc_macro2::TokenStream,
proc_macro2::TokenStream,
proc_macro2::TokenStream,
proc_macro2::TokenStream,
proc_macro2::TokenStream,
) {
let mut go_function_arguments = Vec::new();
let mut go_arguments_prep = Vec::new();
let mut c_function_arguments = Vec::new();
let mut c_argument_prep = Vec::new();
let mut rust_function_arguments = Vec::new();
Expand All @@ -227,8 +218,6 @@ fn get_method_arguments(
for (argument_name, argument_type) in &method.method_arguments {
let argument_name_without_mut = argument_name.replacen("mut", "", 1);
let (
go_function_arguments_,
go_arguments_prep_,
c_function_arguments_,
c_function_argument_types,
c_argument_prep_,
Expand All @@ -242,16 +231,12 @@ fn get_method_arguments(
.collect::<Vec<String>>()
.join(",");

go_function_arguments.push(go_function_arguments_);
go_arguments_prep.push(go_arguments_prep_);
c_function_arguments.push(c_function_arguments_);
c_argument_prep.push(c_argument_prep_);
rust_function_arguments.push(rust_function_arguments_);
}

(
proc_macro2::TokenStream::from_str(&go_function_arguments.join("\n")).unwrap(),
proc_macro2::TokenStream::from_str(&go_arguments_prep.join("\n")).unwrap(),
proc_macro2::TokenStream::from_str(&c_function_arguments.join(",")).unwrap(),
proc_macro2::TokenStream::from_str(&c_argument_prep.join("\n")).unwrap(),
proc_macro2::TokenStream::from_str(&rust_function_arguments.join(",")).unwrap(),
Expand All @@ -269,89 +254,61 @@ fn get_method_arguments(
fn get_c_types(
argument_name: &str,
ty: &SupportedType,
) -> (String, String, Vec<String>, Vec<String>, String, String) {
) -> (Vec<String>, Vec<String>, String, String) {
let t = ty.to_language_string(&None);
let c_to_rust = format!("let {argument_name}: {t} = {argument_name}.custom_into();");
match ty {
SupportedType::Reference(r) => {
let (
go_function_arguments,
go_argument_prep,
c_function_arguments,
c_function_argument_types,
c_argument_prep,
rust_function_arguments,
) = get_c_types(argument_name, &r.ty);
let (c_function_arguments, c_function_argument_types, _, _) =
get_c_types(argument_name, &r.ty);
(
"".to_string(),
"".to_string(),
c_function_arguments,
c_function_argument_types,
c_to_rust,
argument_name.to_string(),
)
}
SupportedType::str | SupportedType::String => (
"".to_string(),
"".to_string(),
vec![format!("{argument_name}")],
vec!["*mut std::ffi::c_char".to_string()],
c_to_rust,
argument_name.to_string(),
),
SupportedType::Option(r) => {
let (
go_function_arguments,
go_argument_prep,
mut c_function_arguments,
mut c_function_argument_types,
c_argument_prep,
rust_function_arguments,
) = get_c_types(argument_name, &r);
let (c_function_arguments, mut c_function_argument_types, _, _) =
get_c_types(argument_name, r);

let v = c_function_argument_types.last_mut().unwrap();
if !v.starts_with('*') {
*v = format!("*mut {v}");
}

(
"".to_string(),
"".to_string(),
c_function_arguments,
c_function_argument_types,
c_to_rust,
argument_name.to_string(),
)
}
SupportedType::bool => (
"".to_string(),
"".to_string(),
vec![format!("{argument_name}")],
vec!["bool".to_string()],
"".to_string(),
argument_name.to_string(),
),
SupportedType::Vec(v) => {
let (
go_function_arguments,
go_argument_prep,
mut c_function_arguments,
mut c_function_argument_types,
mut c_argument_prep,
rust_function_arguments,
) = get_c_types(argument_name, v);
let (mut c_function_arguments, mut c_function_argument_types, _, _) =
get_c_types(argument_name, v);

let v = c_function_argument_types.last_mut().unwrap();
*v = v.replacen("*mut", "*mut *mut", 1);
c_function_arguments.push("v_size".to_string());
c_function_argument_types.push("std::ffi::c_ulong".to_string());
c_argument_prep = "let v_size: usize = v_size as usize;".to_string();
let c_argument_prep = "let v_size: usize = v_size as usize;".to_string();
let c_to_rust =
format!("{c_argument_prep}\nlet {argument_name}: {t} = {argument_name}.custom_into_vec(v_size);");

(
"".to_string(),
"".to_string(),
c_function_arguments,
c_function_argument_types,
c_to_rust,
Expand All @@ -362,40 +319,30 @@ fn get_c_types(
SupportedType::Tuple(_) => panic!("Tuple arguments not supported in c"),
SupportedType::S => unreachable!(),
SupportedType::i64 => (
"".to_string(),
"".to_string(),
vec![format!("{argument_name}")],
vec!["std::ffi::c_long".to_string()],
format!("let {argument_name}: {t} = {argument_name} as {t};"),
argument_name.to_string(),
),
SupportedType::u64 => (
"".to_string(),
"".to_string(),
vec![format!("{argument_name}")],
vec!["std::ffi::c_ulong".to_string()],
format!("let {argument_name}: {t} = {argument_name} as {t};"),
argument_name.to_string(),
),
SupportedType::i32 => (
"".to_string(),
"".to_string(),
vec![format!("{argument_name}")],
vec!["std::ffi::c_int".to_string()],
format!("let {argument_name}: {t} = {argument_name} as {t};"),
argument_name.to_string(),
),
SupportedType::f64 => (
"".to_string(),
"".to_string(),
vec![format!("{argument_name}")],
vec!["std::ffi::c_double".to_string()],
format!("let {argument_name}: {t} = {argument_name} as {t};"),
argument_name.to_string(),
),
SupportedType::CustomType(s) => (
"".to_string(),
"".to_string(),
vec![format!("{argument_name}")],
vec![format!("*mut {s}C")],
c_to_rust,
Expand Down