Skip to content

Commit

Permalink
Improved build macro parsing (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed May 6, 2021
1 parent da9cf84 commit aa3b3dd
Show file tree
Hide file tree
Showing 18 changed files with 553 additions and 549 deletions.
9 changes: 3 additions & 6 deletions crates/crates/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ windows = {{ version = "{}", default-features = false }}
"// This file was generated by the `windows` crate - do not edit by hand!\n\n".as_bytes(),
)?;

let mut limits = gen::TypeLimits::new(reader);
let mut imports = std::collections::BTreeMap::new();

for namespace in reader.namespaces() {
if module == "Windows.UI" && namespace.starts_with("Windows.UI.Xaml") {
Expand All @@ -109,14 +109,11 @@ windows = {{ version = "{}", default-features = false }}
if namespace == module || namespace.starts_with(&format!("{}.", module)) {
println!("- {}", namespace);

limits.insert(gen::NamespaceTypes {
namespace,
limit: gen::TypeLimit::All,
});
imports.insert(namespace, gen::ImportLimit::All);
}
}

let tree = gen::TypeTree::from_limits(reader, &limits);
let tree = gen::TypeTree::from_imports(reader, &imports);

let ts = tree
.gen(&tree)
Expand Down
13 changes: 13 additions & 0 deletions crates/gen/src/import_limit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::collections::BTreeSet;

#[derive(Debug)]
pub enum ImportLimit {
All,
Some(BTreeSet<&'static str>),
}

impl ImportLimit {
pub fn none() -> Self {
Self::Some(BTreeSet::new())
}
}
4 changes: 2 additions & 2 deletions crates/gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ pub use std::iter::FromIterator;
mod r#async;
mod gen;
mod guid;
mod import_limit;
mod iterator;
mod object;
mod parser;
mod squote;
pub mod tables;
mod to_ident;
mod type_limits;
mod type_tree;
pub mod types;
mod workspace;

pub use gen::*;
pub use guid::*;
pub use import_limit::*;
pub use iterator::*;
pub use object::*;
pub use parser::*;
pub use r#async::*;
pub use squote::*;
pub use to_ident::*;
pub use type_limits::*;
pub use type_tree::*;
pub use workspace::*;

Expand Down
22 changes: 22 additions & 0 deletions crates/gen/src/parser/type_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,28 @@ impl TypeReader {
panic!("Could not find type `{}.{}`", namespace, name);
}

pub fn get_namespace(&'static self, namespace: &str) -> Option<&'static str> {
if let Some((namespace, _)) = self.types.get_key_value(namespace) {
Some(namespace)
} else {
None
}
}

pub fn get_type_name(
&'static self,
namespace: &str,
name: &str,
) -> Option<(&'static str, &'static str)> {
if let Some((namespace, types)) = self.types.get_key_value(namespace) {
if let Some((name, _)) = types.get_key_value(trim_tick(name)) {
return Some((namespace, name));
}
}

None
}

fn to_element_type(&'static self, row: &TypeRow) -> ElementType {
match row {
TypeRow::TypeDef(row) => ElementType::from_type_def(*row, Vec::new()),
Expand Down
37 changes: 0 additions & 37 deletions crates/gen/src/type_limits.rs

This file was deleted.

36 changes: 19 additions & 17 deletions crates/gen/src/type_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@ impl TypeTree {
}
}

pub fn from_limits(reader: &'static TypeReader, limits: &TypeLimits) -> Self {
pub fn from_imports(
reader: &'static TypeReader,
imports: &BTreeMap<&'static str, ImportLimit>,
) -> Self {
let mut root = Self::from_namespace("");

let mut set = BTreeSet::new();

for limit in limits.limits() {
match &limit.limit {
TypeLimit::All => {
for def in reader.namespace_types(&limit.namespace) {
root.insert_if(reader, &limit.namespace, &mut set, &def);
for (namespace, limit) in imports {
match limit {
ImportLimit::All => {
for def in reader.namespace_types(namespace) {
root.insert_if(reader, namespace, &mut set, &def);
}
}
TypeLimit::Some(types) => {
for name in types {
ImportLimit::Some(names) => {
for name in names {
root.insert_if(
reader,
&limit.namespace,
namespace,
&mut set,
&reader.resolve_type(&limit.namespace, name),
&reader.resolve_type(namespace, name),
);
}
}
Expand Down Expand Up @@ -147,14 +149,14 @@ mod tests {
#[test]
fn test_tree() {
let reader = TypeReader::get();
let mut limits = TypeLimits::new(reader);
let mut imports = BTreeMap::new();

let mut single = BTreeSet::new();
single.insert("FILE_ACCESS_FLAGS");

limits.insert(NamespaceTypes {
namespace: "Windows.Win32.FileSystem",
limit: TypeLimit::Some(vec!["FILE_ACCESS_FLAGS".to_string()]),
});
imports.insert("Windows.Win32.FileSystem", ImportLimit::Some(single));

let tree = TypeTree::from_limits(reader, &limits);
let tree = TypeTree::from_imports(reader, &imports);

assert_eq!(tree.namespace, "");
assert_eq!(tree.types.len(), 0);
Expand Down
140 changes: 0 additions & 140 deletions crates/macros/src/build_limits.rs

This file was deleted.

Loading

0 comments on commit aa3b3dd

Please sign in to comment.