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