Skip to content
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

Consolidate type generation #2903

Merged
merged 2 commits into from
Feb 29, 2024
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
4 changes: 4 additions & 0 deletions crates/libs/bindgen/src/rust/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use super::*;
use metadata::HasAttributes;

pub fn writer(writer: &Writer, def: metadata::TypeDef) -> TokenStream {
if !def.flags().contains(metadata::TypeAttributes::WindowsRuntime) {
return quote! {};
}

if writer.sys {
if def.interface_impls().next().is_some() {
let name = to_ident(def.name());
Expand Down
38 changes: 1 addition & 37 deletions crates/libs/bindgen/src/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,36 +177,7 @@ fn namespace(writer: &Writer, tree: &Tree) -> String {
if writer.reader.core_types().any(|(x, _)| x == &type_name) {
continue;
}
let name = type_name.name;
let mut kind = def.kind();

let tokens = match kind {
metadata::TypeKind::Class => {
if def.flags().contains(metadata::TypeAttributes::WindowsRuntime) {
classes::writer(writer, def)
} else {
quote! {}
}
}
metadata::TypeKind::Interface => interfaces::writer(writer, def),
metadata::TypeKind::Enum => enums::writer(writer, def),
metadata::TypeKind::Struct => {
if let Some(guid) = clsid(def) {
kind = metadata::TypeKind::Class;
let ident = to_ident(name);
let value = writer.guid(&guid);
let guid = writer.type_name(&metadata::Type::GUID);
quote! {
pub const #ident: #guid = #value;
}
} else {
structs::writer(writer, def)
}
}
metadata::TypeKind::Delegate => delegates::writer(writer, def),
};

types.entry(kind).or_default().entry(name).or_default().combine(&tokens);
types.entry(def.kind()).or_default().entry(type_name.name).or_default().combine(&writer.type_def(def));
}
metadata::Item::Fn(def, namespace) => {
let name = def.name();
Expand Down Expand Up @@ -236,13 +207,6 @@ fn namespace(writer: &Writer, tree: &Tree) -> String {
tokens.into_string()
}

fn clsid(def: metadata::TypeDef) -> Option<metadata::Guid> {
if def.fields().next().is_none() {
return metadata::type_def_guid(def);
}
None
}

fn namespace_impl(writer: &Writer, tree: &Tree) -> String {
let writer = &mut writer.clone();
writer.namespace = tree.namespace;
Expand Down
34 changes: 1 addition & 33 deletions crates/libs/bindgen/src/rust/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,39 +85,7 @@ pub fn standalone_imp(writer: &Writer) -> String {
);
}
metadata::Type::TypeDef(def, _) => {
let kind = def.kind();
match kind {
metadata::TypeKind::Class => {
sorted.insert(def.name(), classes::writer(writer, def));
}
metadata::TypeKind::Interface => {
sorted.insert(def.name(), interfaces::writer(writer, def));
}
metadata::TypeKind::Enum => {
sorted.insert(def.name(), enums::writer(writer, def));
}
metadata::TypeKind::Struct => {
let name = def.name();
if def.fields().next().is_none() {
if let Some(guid) = metadata::type_def_guid(def) {
let ident = to_ident(name);
let value = writer.guid(&guid);
let guid = writer.type_name(&metadata::Type::GUID);
sorted.insert(
name,
quote! {
pub const #ident: #guid = #value;
},
);
continue;
}
}
sorted.insert(name, structs::writer(writer, def));
}
metadata::TypeKind::Delegate => {
sorted.insert(def.name(), delegates::writer(writer, def));
}
}
sorted.insert(def.name(), writer.type_def(def));
}
_ => {}
}
Expand Down
16 changes: 16 additions & 0 deletions crates/libs/bindgen/src/rust/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ pub fn writer(writer: &Writer, def: metadata::TypeDef) -> TokenStream {
return handles::writer(writer, def);
}

if let Some(guid) = clsid(def) {
let ident = to_ident(def.name());
let value = writer.guid(&guid);
let guid = writer.type_name(&metadata::Type::GUID);
return quote! {
pub const #ident: #guid = #value;
};
}

gen_struct_with_name(writer, def, def.name(), &cfg::Cfg::default())
}

Expand Down Expand Up @@ -285,3 +294,10 @@ fn gen_struct_constants(writer: &Writer, def: metadata::TypeDef, struct_name: &T

tokens
}

fn clsid(def: metadata::TypeDef) -> Option<metadata::Guid> {
if def.fields().next().is_none() {
return metadata::type_def_guid(def);
}
None
}
9 changes: 9 additions & 0 deletions crates/libs/bindgen/src/rust/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ impl Writer {
}
}
}
pub fn type_def(&self, def: metadata::TypeDef) -> TokenStream {
match def.kind() {
metadata::TypeKind::Class => classes::writer(self, def),
metadata::TypeKind::Interface => interfaces::writer(self, def),
metadata::TypeKind::Enum => enums::writer(self, def),
metadata::TypeKind::Struct => structs::writer(self, def),
metadata::TypeKind::Delegate => delegates::writer(self, def),
}
}

//
// Type
Expand Down
20 changes: 10 additions & 10 deletions crates/libs/sys/src/Windows/Win32/Devices/Enumeration/Pnp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,6 @@ pub const UPNP_E_URLBASE_PRESENT: ::windows_sys::core::HRESULT = 0x8004A029_u32
pub const UPNP_E_VALUE_TOO_LONG: ::windows_sys::core::HRESULT = 0x8004A030_u32 as _;
pub const UPNP_E_VARIABLE_VALUE_UNKNOWN: ::windows_sys::core::HRESULT = 0x80040212_u32 as _;
pub const UPNP_SERVICE_DELAY_SCPD_AND_SUBSCRIPTION: u32 = 1u32;
pub const UPnPDescriptionDocument: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0x1d8a9b47_3a28_4ce2_8a4b_bd34e45bceeb);
pub const UPnPDescriptionDocumentEx: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0x33fd0563_d81a_4393_83cc_0195b1da2f91);
pub const UPnPDevice: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0xa32552c5_ba61_457a_b59a_a2561e125e33);
pub const UPnPDeviceFinder: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0xe2085f28_feb7_404a_b8e7_e659bdeaaa02);
pub const UPnPDeviceFinderEx: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0x181b54fc_380b_4a75_b3f1_4ac45e9605b0);
pub const UPnPDevices: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0xb9e84ffd_ad3c_40a4_b835_0882ebcbaaa8);
pub const UPnPRegistrar: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0x204810b9_73b2_11d4_bf42_00b0d0118b56);
pub const UPnPRemoteEndpointInfo: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0x2e5e84e9_4049_4244_b728_2d24227157c7);
pub const UPnPService: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0xc624ba95_fbcb_4409_8c03_8cceec533ef1);
pub const UPnPServices: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0xc0bc4b4a_a406_4efc_932f_b8546b8100cc);
pub type SW_DEVICE_CAPABILITIES = i32;
pub type SW_DEVICE_LIFETIME = i32;
pub type HSWDEVICE = isize;
Expand All @@ -100,4 +90,14 @@ impl ::core::clone::Clone for SW_DEVICE_CREATE_INFO {
*self
}
}
pub const UPnPDescriptionDocument: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0x1d8a9b47_3a28_4ce2_8a4b_bd34e45bceeb);
pub const UPnPDescriptionDocumentEx: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0x33fd0563_d81a_4393_83cc_0195b1da2f91);
pub const UPnPDevice: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0xa32552c5_ba61_457a_b59a_a2561e125e33);
pub const UPnPDeviceFinder: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0xe2085f28_feb7_404a_b8e7_e659bdeaaa02);
pub const UPnPDeviceFinderEx: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0x181b54fc_380b_4a75_b3f1_4ac45e9605b0);
pub const UPnPDevices: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0xb9e84ffd_ad3c_40a4_b835_0882ebcbaaa8);
pub const UPnPRegistrar: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0x204810b9_73b2_11d4_bf42_00b0d0118b56);
pub const UPnPRemoteEndpointInfo: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0x2e5e84e9_4049_4244_b728_2d24227157c7);
pub const UPnPService: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0xc624ba95_fbcb_4409_8c03_8cceec533ef1);
pub const UPnPServices: ::windows_sys::core::GUID = ::windows_sys::core::GUID::from_u128(0xc0bc4b4a_a406_4efc_932f_b8546b8100cc);
pub type SW_DEVICE_CREATE_CALLBACK = ::core::option::Option<unsafe extern "system" fn(hswdevice: HSWDEVICE, createresult: ::windows_sys::core::HRESULT, pcontext: *const ::core::ffi::c_void, pszdeviceinstanceid: ::windows_sys::core::PCWSTR)>;
Loading