From f260d8260953cf89c606008d95f126c9e80c3d93 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Mon, 30 May 2022 11:01:34 +0200 Subject: [PATCH 1/2] Make all Vulkan types fundamental --- Cargo.lock | 40 ++++++++++---------- src/analysis/conversion_type.rs | 1 + src/analysis/ffi_type.rs | 2 +- src/analysis/rust_type.rs | 5 ++- src/codegen/sys/ffi_type.rs | 3 +- src/library.rs | 66 +++++++++++++++++++++++++++++++-- 6 files changed, 90 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0bf9764ae..9b5a06a81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,7 +41,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" dependencies = [ - "log 0.4.16", + "log 0.4.17", ] [[package]] @@ -71,7 +71,7 @@ dependencies = [ "fix-getters-rules", "getopts", "hprof", - "log 0.4.16", + "log 0.4.17", "once_cell", "regex", "rustdoc-stripper", @@ -97,9 +97,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.8.1" +version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f647032dfaa1f8b6dc29bd3edb7bbef4861b8b8007ebb118d6db284fd59f6ee" +checksum = "e6012d540c5baa3589337a98ce73408de9b5a25ec9fc2c6fd6be8f0d39e0ca5a" dependencies = [ "autocfg", "hashbrown", @@ -111,35 +111,35 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" dependencies = [ - "log 0.4.16", + "log 0.4.17", ] [[package]] name = "log" -version = "0.4.16" +version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6389c490849ff5bc16be905ae24bc913a9c8892e19b2341dbc175e14c341c2b8" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ "cfg-if", ] [[package]] name = "memchr" -version = "2.4.1" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "once_cell" -version = "1.10.0" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" +checksum = "7709cef83f0c1f58f666e746a08b21e0085f7440fa6a29cc194d68aac97a4225" [[package]] name = "regex" -version = "1.5.5" +version = "1.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286" +checksum = "d83f127d94bdbcda4c8cc2e50f6f84f4b611f69c902699ca385a39c3a75f9ff1" dependencies = [ "aho-corasick", "memchr", @@ -148,26 +148,26 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.25" +version = "0.6.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64" [[package]] name = "rustdoc-stripper" version = "0.1.18" -source = "git+https://github.com/GuillaumeGomez/rustdoc-stripper#f6643dd300a71c876625260f190c63a5be41f331" +source = "git+https://github.com/GuillaumeGomez/rustdoc-stripper#4b222d816418eefd481528d9e5756b497fb0b28b" [[package]] name = "serde" -version = "1.0.136" +version = "1.0.137" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1" [[package]] name = "toml" -version = "0.5.8" +version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" dependencies = [ "indexmap", "serde", diff --git a/src/analysis/conversion_type.rs b/src/analysis/conversion_type.rs index 14cabc608..860736284 100644 --- a/src/analysis/conversion_type.rs +++ b/src/analysis/conversion_type.rs @@ -78,6 +78,7 @@ impl ConversionType { IntPtr => ConversionType::Direct, UIntPtr => ConversionType::Direct, Bool => ConversionType::Direct, + Vulkan(_) => ConversionType::Direct, Unsupported => ConversionType::Unknown, }, Alias(alias) if alias.c_identifier == "GQuark" => ConversionType::Scalar, diff --git a/src/analysis/ffi_type.rs b/src/analysis/ffi_type.rs index 1c35dab07..55ddd3da1 100644 --- a/src/analysis/ffi_type.rs +++ b/src/analysis/ffi_type.rs @@ -68,7 +68,7 @@ pub fn ffi_type(env: &Env, tid: TypeId, c_type: &str) -> Result { fn ffi_inner(env: &Env, tid: TypeId, inner: &str) -> Result { let typ = env.library.type_(tid); match *typ { - Type::Basic(fund) => { + Type::Basic(ref fund) => { use crate::library::Basic::*; let inner = match fund { None => "libc::c_void", diff --git a/src/analysis/rust_type.rs b/src/analysis/rust_type.rs index d7bfe53de..a6fe248bd 100644 --- a/src/analysis/rust_type.rs +++ b/src/analysis/rust_type.rs @@ -251,7 +251,7 @@ impl<'env> RustTypeBuilder<'env> { let mut skip_option = false; let type_ = self.env.library.type_(self.type_id); let mut rust_type = match *type_ { - Basic(fund) => { + Basic(ref fund) => { match fund { None => err("()"), Boolean | Bool => ok("bool"), @@ -268,7 +268,7 @@ impl<'env> RustTypeBuilder<'env> { UInt => ok("u32"), //maybe dependent on target system Short => ok_and_use("libc::c_short"), //depends of target system - UShort => ok_and_use("libc::c_ushort"), //depends o f target system + UShort => ok_and_use("libc::c_ushort"), //depends of target system Long => ok_and_use("libc::c_long"), //depends of target system ULong => ok_and_use("libc::c_ulong"), //depends of target system @@ -303,6 +303,7 @@ impl<'env> RustTypeBuilder<'env> { Type => ok_and_use(&use_glib_type(self.env, "types::Type")), Char => ok_and_use(&use_glib_type(self.env, "Char")), UChar => ok_and_use(&use_glib_type(self.env, "UChar")), + Vulkan(name) => ok_and_use(&format!("ash::vk::{}", name)), Unsupported => err("Unsupported"), _ => err(&format!("Basic: {:?}", fund)), } diff --git a/src/codegen/sys/ffi_type.rs b/src/codegen/sys/ffi_type.rs index 4fa74e587..0f0720811 100644 --- a/src/codegen/sys/ffi_type.rs +++ b/src/codegen/sys/ffi_type.rs @@ -69,7 +69,7 @@ fn ffi_inner(env: &Env, tid: library::TypeId, mut inner: String) -> Result { let typ = env.library.type_(tid); let res = match *typ { - Type::Basic(fund) => { + Type::Basic(ref fund) => { use crate::library::Basic::*; let inner = match fund { None => "c_void", @@ -110,6 +110,7 @@ fn ffi_inner(env: &Env, tid: library::TypeId, mut inner: String) -> Result { UIntPtr => "uintptr_t", Bool => "bool", Unsupported => return Err(TypeError::Unimplemented(inner)), + Vulkan(v) => return Ok(format!("ash::vk::{}", v).into()), VarArgs => panic!("Should not reach here"), }; Ok(inner.into()) diff --git a/src/library.rs b/src/library.rs index e834887f4..f6f30b8da 100644 --- a/src/library.rs +++ b/src/library.rs @@ -203,7 +203,7 @@ impl Default for Concurrency { } } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub enum Basic { None, Boolean, @@ -240,6 +240,7 @@ pub enum Basic { OsString, Bool, Unsupported, + Vulkan(String), } impl Basic { @@ -267,6 +268,7 @@ impl Basic { | Self::Float | Self::Double | Self::Bool + | Self::Vulkan(_) ) } } @@ -1037,6 +1039,8 @@ impl Namespace { pub const INTERNAL_NAMESPACE_NAME: &str = "*"; pub const INTERNAL_NAMESPACE: u16 = 0; pub const MAIN_NAMESPACE: u16 = 1; +pub const VULKAN_NAMESPACE_NAME: &str = "Vulkan"; +pub const VULKAN_NAMESPACE: u16 = 2; #[derive(Debug)] pub struct Library { @@ -1054,11 +1058,67 @@ impl Library { INTERNAL_NAMESPACE, library.add_namespace(INTERNAL_NAMESPACE_NAME) ); - for &(name, t) in BASIC { - library.add_type(INTERNAL_NAMESPACE, name, Type::Basic(t)); + for (name, t) in BASIC { + library.add_type(INTERNAL_NAMESPACE, name, Type::Basic(t.clone())); } assert_eq!(MAIN_NAMESPACE, library.add_namespace(main_namespace_name)); + assert_eq!( + VULKAN_NAMESPACE, + library.add_namespace(VULKAN_NAMESPACE_NAME) + ); + // TODO: This should be parseable from gir-files/Vulkan-1.0.gir! + const VULKAN: &[&str] = &[ + "AccessFlags", + "Buffer", + "BufferUsageFlags", + "CommandBuffer", + "CommandBufferLevel", + "CommandPool", + "DescriptorPool", + "DescriptorSet", + "Device", + "DeviceMemory", + "DeviceSize", + "Fence", + "Format", + "Image", + "ImageCreateInfo", + "ImageFormatProperties", + "ImageLayout", + "ImageSubresourceRange", + "ImageTiling", + "ImageUsageFlags", + "ImageView", + "ImageViewCreateInfo", + "Instance", + "MemoryAllocateInfo", + "MemoryHeapFlags", + "MemoryPropertyFlags", + "MemoryRequirements", + "PhysicalDevice", + "PhysicalDeviceFeatures", + "PhysicalDeviceMemoryProperties", + "PhysicalDeviceProperties", + "PhysicalDeviceType", + "PipelineStageFlags", + "PresentModeKHR", + "Queue", + "QueueFamilyProperties", + "QueueFlags", + "Result", + "SampleCountFlags", + "Semaphore", + "SurfaceKHR", + ]; + for v in VULKAN { + library.add_type( + VULKAN_NAMESPACE, + v, + Type::Basic(Basic::Vulkan(v.to_string())), + ); + } + //For string_type override Type::c_array(&mut library, TypeId::tid_utf8(), None, None); Type::c_array(&mut library, TypeId::tid_filename(), None, None); From 44c1a4edffe1ee3716382f84fefd96bc819226e1 Mon Sep 17 00:00:00 2001 From: Philippe Renon Date: Fri, 3 Jun 2022 19:25:09 +0200 Subject: [PATCH 2/2] Add Vulkan namespace preprocessor also rename the new Basic::Vulkan enum value to Basic::Typedef --- src/analysis/conversion_type.rs | 2 +- src/analysis/rust_type.rs | 2 +- src/codegen/sys/ffi_type.rs | 2 +- src/custom_vulkan_namespace.rs | 19 ++++++++++ src/lib.rs | 1 + src/library.rs | 62 ++------------------------------- src/library_preprocessing.rs | 1 + 7 files changed, 26 insertions(+), 63 deletions(-) create mode 100644 src/custom_vulkan_namespace.rs diff --git a/src/analysis/conversion_type.rs b/src/analysis/conversion_type.rs index 860736284..a57335a71 100644 --- a/src/analysis/conversion_type.rs +++ b/src/analysis/conversion_type.rs @@ -78,7 +78,7 @@ impl ConversionType { IntPtr => ConversionType::Direct, UIntPtr => ConversionType::Direct, Bool => ConversionType::Direct, - Vulkan(_) => ConversionType::Direct, + Typedef(_) => ConversionType::Direct, Unsupported => ConversionType::Unknown, }, Alias(alias) if alias.c_identifier == "GQuark" => ConversionType::Scalar, diff --git a/src/analysis/rust_type.rs b/src/analysis/rust_type.rs index a6fe248bd..249a29d62 100644 --- a/src/analysis/rust_type.rs +++ b/src/analysis/rust_type.rs @@ -303,7 +303,7 @@ impl<'env> RustTypeBuilder<'env> { Type => ok_and_use(&use_glib_type(self.env, "types::Type")), Char => ok_and_use(&use_glib_type(self.env, "Char")), UChar => ok_and_use(&use_glib_type(self.env, "UChar")), - Vulkan(name) => ok_and_use(&format!("ash::vk::{}", name)), + Typedef(name) => ok_and_use(name), Unsupported => err("Unsupported"), _ => err(&format!("Basic: {:?}", fund)), } diff --git a/src/codegen/sys/ffi_type.rs b/src/codegen/sys/ffi_type.rs index 0f0720811..4977e8d71 100644 --- a/src/codegen/sys/ffi_type.rs +++ b/src/codegen/sys/ffi_type.rs @@ -110,7 +110,7 @@ fn ffi_inner(env: &Env, tid: library::TypeId, mut inner: String) -> Result { UIntPtr => "uintptr_t", Bool => "bool", Unsupported => return Err(TypeError::Unimplemented(inner)), - Vulkan(v) => return Ok(format!("ash::vk::{}", v).into()), + Typedef(name) => return Ok(name.into()), VarArgs => panic!("Should not reach here"), }; Ok(inner.into()) diff --git a/src/custom_vulkan_namespace.rs b/src/custom_vulkan_namespace.rs new file mode 100644 index 000000000..70a908457 --- /dev/null +++ b/src/custom_vulkan_namespace.rs @@ -0,0 +1,19 @@ +use crate::library::*; + +pub const VULKAN_NAMESPACE_NAME: &str = "Vulkan"; + +impl Library { + pub fn tweak_vulkan_namespace(&mut self) { + if let Some(ns_id) = self.find_namespace(VULKAN_NAMESPACE_NAME) { + let ns = self.namespace_mut(ns_id); + for typ in &mut ns.types { + if let Some(Type::Record(rec)) = typ { + *typ = Some(Type::Basic(Basic::Typedef(format!( + "ash::vk::{}", + rec.name + )))); + } + } + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 5b909e094..397607399 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,7 @@ mod codegen; mod config; mod consts; mod custom_type_glib_priority; +mod custom_vulkan_namespace; mod env; mod file_saver; pub mod fmt; diff --git a/src/library.rs b/src/library.rs index f6f30b8da..44173a35c 100644 --- a/src/library.rs +++ b/src/library.rs @@ -240,7 +240,7 @@ pub enum Basic { OsString, Bool, Unsupported, - Vulkan(String), + Typedef(String), } impl Basic { @@ -268,7 +268,7 @@ impl Basic { | Self::Float | Self::Double | Self::Bool - | Self::Vulkan(_) + | Self::Typedef(_) ) } } @@ -1039,8 +1039,6 @@ impl Namespace { pub const INTERNAL_NAMESPACE_NAME: &str = "*"; pub const INTERNAL_NAMESPACE: u16 = 0; pub const MAIN_NAMESPACE: u16 = 1; -pub const VULKAN_NAMESPACE_NAME: &str = "Vulkan"; -pub const VULKAN_NAMESPACE: u16 = 2; #[derive(Debug)] pub struct Library { @@ -1063,62 +1061,6 @@ impl Library { } assert_eq!(MAIN_NAMESPACE, library.add_namespace(main_namespace_name)); - assert_eq!( - VULKAN_NAMESPACE, - library.add_namespace(VULKAN_NAMESPACE_NAME) - ); - // TODO: This should be parseable from gir-files/Vulkan-1.0.gir! - const VULKAN: &[&str] = &[ - "AccessFlags", - "Buffer", - "BufferUsageFlags", - "CommandBuffer", - "CommandBufferLevel", - "CommandPool", - "DescriptorPool", - "DescriptorSet", - "Device", - "DeviceMemory", - "DeviceSize", - "Fence", - "Format", - "Image", - "ImageCreateInfo", - "ImageFormatProperties", - "ImageLayout", - "ImageSubresourceRange", - "ImageTiling", - "ImageUsageFlags", - "ImageView", - "ImageViewCreateInfo", - "Instance", - "MemoryAllocateInfo", - "MemoryHeapFlags", - "MemoryPropertyFlags", - "MemoryRequirements", - "PhysicalDevice", - "PhysicalDeviceFeatures", - "PhysicalDeviceMemoryProperties", - "PhysicalDeviceProperties", - "PhysicalDeviceType", - "PipelineStageFlags", - "PresentModeKHR", - "Queue", - "QueueFamilyProperties", - "QueueFlags", - "Result", - "SampleCountFlags", - "Semaphore", - "SurfaceKHR", - ]; - for v in VULKAN { - library.add_type( - VULKAN_NAMESPACE, - v, - Type::Basic(Basic::Vulkan(v.to_string())), - ); - } - //For string_type override Type::c_array(&mut library, TypeId::tid_utf8(), None, None); Type::c_array(&mut library, TypeId::tid_filename(), None, None); diff --git a/src/library_preprocessing.rs b/src/library_preprocessing.rs index f60a07b3b..0ab5634d1 100644 --- a/src/library_preprocessing.rs +++ b/src/library_preprocessing.rs @@ -3,5 +3,6 @@ use crate::{config::WorkMode, library::*}; impl Library { pub fn preprocessing(&mut self, work_mode: WorkMode) { self.add_glib_priority(work_mode); + self.tweak_vulkan_namespace(); } }