Skip to content

Add standalone code generation support for the Rust Standard Library #2439

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 5 commits into from
Apr 12, 2023
Merged
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
5 changes: 4 additions & 1 deletion crates/libs/bindgen/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ pub fn gen(gen: &Gen, def: Field) -> TokenStream {
} else {
let kind = gen.type_default_name(&ty);
let value = gen.value(&gen.reader.constant_value(constant));
let underlying_type = gen.reader.type_underlying_type(&ty);

let value = if gen.reader.type_underlying_type(&ty) == constant_type {
let value = if underlying_type == constant_type {
value
// TODO: workaround for https://github.com/microsoft/win32metadata/issues/1029
} else if ty == Type::PCWSTR && value.0.starts_with('-') {
quote! { #value as u16 as _ }
} else if gen.std && underlying_type == Type::ISize {
quote! { ::core::ptr::invalid_mut(#value as _) }
} else {
quote! { #value as _ }
};
Expand Down
42 changes: 22 additions & 20 deletions crates/libs/bindgen/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ fn gen_sys_function(gen: &Gen, def: MethodDef) -> TokenStream {
let features = gen.cfg_features(&cfg);
let return_type = gen.return_sig(&signature);
let abi = gen.reader.method_def_extern_abi(def);
let impl_map = gen
.reader
.method_def_impl_map(def)
.expect("ImplMap not found");
let scope = gen.reader.impl_map_scope(impl_map);
let link = gen.reader.module_ref_name(scope).to_lowercase();
let link = gen.reader.method_def_module_name(def);

// TODO: skip inline functions for now.
if link == "forceinline" {
Expand All @@ -35,14 +30,26 @@ fn gen_sys_function(gen: &Gen, def: MethodDef) -> TokenStream {
});

let mut tokens = features;
tokens.combine(&gen_link(
&link,
abi,
doc.as_str(),
name.as_str(),
params,
return_type.as_str(),
));

if gen.std {
let link = link.trim_end_matches(".dll");
tokens.combine(&quote! {
#[link(name = #link)]
extern #abi {
pub fn #name(#(#params),*) #return_type;
}
});
} else {
tokens.combine(&gen_link(
&link,
abi,
doc.as_str(),
name.as_str(),
params,
return_type.as_str(),
));
}

tokens
}

Expand Down Expand Up @@ -96,12 +103,7 @@ fn gen_win_function(gen: &Gen, def: MethodDef) -> TokenStream {
}
}
} else {
let impl_map = gen
.reader
.method_def_impl_map(def)
.expect("ImplMap not found");
let scope = gen.reader.impl_map_scope(impl_map);
let link = gen.reader.module_ref_name(scope).to_lowercase();
let link = gen.reader.method_def_module_name(def);

// TODO: skip inline functions for now.
if link == "forceinline" {
Expand Down
2 changes: 2 additions & 0 deletions crates/libs/bindgen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct Gen<'a> {
pub doc: bool,
pub component: bool,
pub standalone: bool,
pub std: bool,
}

impl<'a> Gen<'a> {
Expand All @@ -20,6 +21,7 @@ impl<'a> Gen<'a> {
doc: false,
component: false,
standalone: false,
std: false,
}
}

Expand Down
19 changes: 16 additions & 3 deletions crates/libs/bindgen/src/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@ pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream {

pub fn gen_sys_handle(gen: &Gen, def: TypeDef) -> TokenStream {
let ident = to_ident(gen.reader.type_def_name(def));
let signature = gen.type_default_name(&gen.reader.type_def_underlying_type(def));
match gen.reader.type_def_underlying_type(def) {
Type::ISize if gen.std => quote! {
pub type #ident = *mut ::core::ffi::c_void;
},
Type::USize if gen.std => quote! {
#[cfg(target_pointer_width = "32")]
pub type #ident = u32;
#[cfg(target_pointer_width = "64")]
pub type #ident = u64;
},
underlying_type => {
let signature = gen.type_default_name(&underlying_type);

quote! {
pub type #ident = #signature;
quote! {
pub type #ident = #signature;
}
}
}
}

Expand Down
Loading