|
1 | | -mod xml; |
| 1 | +pub mod cdecl; |
| 2 | +pub mod items; |
| 3 | +pub mod xml; |
2 | 4 |
|
3 | | -use std::{fs, path::Path}; |
| 5 | +use crate::items::Origin; |
| 6 | +use items::{structures::Structure, Items, RequiredBy}; |
| 7 | +use std::{ |
| 8 | + collections::HashMap, |
| 9 | + fmt::{self, Display}, |
| 10 | + fs, |
| 11 | + path::{Path, PathBuf}, |
| 12 | +}; |
4 | 13 | use tracing::{debug, error_span}; |
| 14 | +use xml::UnwrapBorrowed; |
5 | 15 |
|
6 | 16 | #[derive(Debug)] |
7 | 17 | pub struct Analysis { |
8 | | - pub vk: Library, |
9 | | - pub video: Library, |
| 18 | + vk: Library, |
| 19 | + video: Library, |
| 20 | + items: Items, |
10 | 21 | } |
11 | 22 |
|
12 | 23 | impl Analysis { |
13 | 24 | pub fn new(vulkan_headers_path: impl AsRef<Path>) -> Analysis { |
| 25 | + let vk = Library::new(LibraryId::Vk, &vulkan_headers_path); |
| 26 | + let video = Library::new(LibraryId::Video, &vulkan_headers_path); |
| 27 | + |
| 28 | + let mut items = Items::default(); |
| 29 | + vk.collect_into(&mut items); |
| 30 | + video.collect_into(&mut items); |
| 31 | + |
| 32 | + Analysis { vk, video, items } |
| 33 | + } |
| 34 | + |
| 35 | + pub fn vk_xml(&self) -> &xml::Registry { |
| 36 | + &self.vk.xml |
| 37 | + } |
| 38 | + |
| 39 | + pub fn video_xml(&self) -> &xml::Registry { |
| 40 | + &self.video.xml |
| 41 | + } |
| 42 | + |
| 43 | + pub fn items(&self) -> &Items { |
| 44 | + &self.items |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] |
| 49 | +pub enum LibraryId { |
| 50 | + Vk, |
| 51 | + Video, |
| 52 | +} |
| 53 | + |
| 54 | +impl LibraryId { |
| 55 | + fn xml_path(&self, vulkan_headers_path: impl AsRef<Path>) -> PathBuf { |
14 | 56 | let vulkan_headers_path = vulkan_headers_path.as_ref(); |
15 | | - Analysis { |
16 | | - vk: Library::new(vulkan_headers_path.join("registry/vk.xml")), |
17 | | - video: Library::new(vulkan_headers_path.join("registry/video.xml")), |
| 57 | + match self { |
| 58 | + LibraryId::Vk => vulkan_headers_path.join("registry/vk.xml"), |
| 59 | + LibraryId::Video => vulkan_headers_path.join("registry/video.xml"), |
18 | 60 | } |
19 | 61 | } |
20 | 62 | } |
21 | 63 |
|
| 64 | +impl Display for LibraryId { |
| 65 | + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 66 | + formatter.write_str(match self { |
| 67 | + LibraryId::Vk => "vk", |
| 68 | + LibraryId::Video => "video", |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
22 | 73 | #[derive(Debug)] |
23 | 74 | pub struct Library { |
24 | | - _xml: xml::Registry, |
| 75 | + id: LibraryId, |
| 76 | + xml: xml::Registry, |
25 | 77 | } |
26 | 78 |
|
27 | 79 | impl Library { |
28 | | - fn new(xml_path: impl AsRef<Path>) -> Library { |
29 | | - let xml = error_span!("xml", path = %xml_path.as_ref().display()).in_scope(|| { |
| 80 | + fn new(id: LibraryId, vulkan_headers_path: impl AsRef<Path>) -> Library { |
| 81 | + let xml = error_span!("xml", library_id = %id).in_scope(|| { |
| 82 | + let path = id.xml_path(vulkan_headers_path); |
30 | 83 | // We leak the input string here for convenience, to avoid explicit lifetimes. |
31 | | - let xml_input = Box::leak(fs::read_to_string(xml_path).unwrap().into_boxed_str()); |
| 84 | + let input = Box::leak(fs::read_to_string(path).unwrap().into_boxed_str()); |
32 | 85 | debug!("parsing xml"); |
33 | | - xml::Registry::parse(xml_input, "vulkan") |
| 86 | + xml::Registry::parse(input, "vulkan") |
34 | 87 | }); |
35 | 88 |
|
36 | | - Library { _xml: xml } |
| 89 | + Library { id, xml } |
| 90 | + } |
| 91 | + |
| 92 | + pub fn xml(&self) -> &xml::Registry { |
| 93 | + &self.xml |
| 94 | + } |
| 95 | + |
| 96 | + fn collect_into(&self, items: &mut Items) { |
| 97 | + let mut types_required_by = HashMap::new(); |
| 98 | + |
| 99 | + for feature in &self.xml.features { |
| 100 | + let required_by = RequiredBy::Feature { |
| 101 | + major: feature.major, |
| 102 | + minor: feature.minor, |
| 103 | + }; |
| 104 | + |
| 105 | + for require in &feature.requires { |
| 106 | + for require_type in &require.types { |
| 107 | + types_required_by.insert(require_type.name.unwrap_borrowed(), required_by); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + for extension in &self.xml.extensions { |
| 113 | + let required_by = RequiredBy::Extension { |
| 114 | + name: extension.name.unwrap_borrowed(), |
| 115 | + }; |
| 116 | + |
| 117 | + for require in &extension.requires { |
| 118 | + for require_type in &require.types { |
| 119 | + types_required_by.insert(require_type.name.unwrap_borrowed(), required_by); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + for structure in &self.xml.structs { |
| 125 | + let name = structure.name.unwrap_borrowed(); |
| 126 | + let Some(&required_by) = types_required_by.get(name) else { |
| 127 | + continue; |
| 128 | + }; |
| 129 | + |
| 130 | + let origin = Origin { |
| 131 | + library_id: self.id, |
| 132 | + required_by, |
| 133 | + }; |
| 134 | + |
| 135 | + let structure = Structure::new(origin, structure); |
| 136 | + assert!(items.structures.insert(name, structure).is_none()); |
| 137 | + } |
37 | 138 | } |
38 | 139 | } |
0 commit comments