Skip to content

Commit

Permalink
simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Jan 8, 2025
1 parent 9b0ea24 commit 6e75816
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 33 deletions.
10 changes: 5 additions & 5 deletions crates/libs/bindgen/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use super::*;
pub struct Derive(HashMap<TypeName, Vec<String>>);

impl Derive {
pub fn new(reader: &Reader, types: &TypeMap, derive: &[&str]) -> Self {
pub fn new(types: &TypeMap, derive: &[&str]) -> Self {
let mut map = HashMap::new();

for derive in derive {
let Some((name, derive)) = derive.split_once('=') else {
panic!("`--derive` must be `<type name>=Comma,Separated,List");
};

let tn = get_type_name(reader, name);
let tn = get_type_name(name);

if !types.contains_key(&tn) {
panic!("type not included: `{name}`");
Expand All @@ -33,15 +33,15 @@ impl Derive {
}
}

fn get_type_name(reader: &Reader, path: &str) -> TypeName {
fn get_type_name(path: &str) -> TypeName {
if let Some((namespace, name)) = path.rsplit_once('.') {
if let Some((namespace, types)) = reader.get_key_value(namespace) {
if let Some((namespace, types)) = reader().get_key_value(namespace) {
if let Some((name, _)) = types.get_key_value(name) {
return TypeName(namespace, name);
}
}
} else {
for (namespace, types) in reader.iter() {
for (namespace, types) in reader().iter() {
if let Some((name, _)) = types.get_key_value(path) {
return TypeName(namespace, name);
}
Expand Down
10 changes: 6 additions & 4 deletions crates/libs/bindgen/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use super::*;
pub struct Filter(Vec<(String, bool)>);

impl Filter {
pub fn new(reader: &Reader, include: &[&str], exclude: &[&str]) -> Self {
pub fn new(include: &[&str], exclude: &[&str]) -> Self {
let mut rules = vec![];

for filter in include {
push_filter(reader, &mut rules, filter, true);
push_filter(&mut rules, filter, true);
}

for filter in exclude {
push_filter(reader, &mut rules, filter, false)
push_filter(&mut rules, filter, false)
}

debug_assert!(!rules.is_empty());
Expand Down Expand Up @@ -68,7 +68,9 @@ impl Filter {
}
}

fn push_filter(reader: &Reader, rules: &mut Vec<(String, bool)>, filter: &str, include: bool) {
fn push_filter(rules: &mut Vec<(String, bool)>, filter: &str, include: bool) {
let reader = reader();

if reader.contains_key(filter) {
rules.push((filter.to_string(), include));
return;
Expand Down
10 changes: 5 additions & 5 deletions crates/libs/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ where
panic!("at least one `--filter` required");
}

let reader = Reader::new(expand_input(&input));
let filter = Filter::new(reader, &include, &exclude);
let references = References::new(reader, references);
let types = TypeMap::filter(reader, &filter, &references);
let derive = Derive::new(reader, &types, &derive);
Reader::init(expand_input(&input));
let filter = Filter::new(&include, &exclude);
let references = References::new(references);
let types = TypeMap::filter(&filter, &references);
let derive = Derive::new(&types, &derive);

let config = Box::leak(Box::new(Config {
types,
Expand Down
20 changes: 7 additions & 13 deletions crates/libs/bindgen/src/libraries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ pub enum CallingConvention {
// Returns the libraries and their function and stack sizes used by the gnu and msvc tools to build the umbrella libs.
#[doc(hidden)]
pub fn libraries() -> BTreeMap<String, BTreeMap<String, CallingConvention>> {
let mut libraries = BTreeMap::new();
Reader::init(expand_input(&["default"]));
let mut result = BTreeMap::<String, BTreeMap<String, CallingConvention>>::new();

let reader = Reader::new(expand_input(&["default"]));
combine_libraries(reader, &mut libraries);
libraries
}

fn combine_libraries(
reader: &Reader,
libraries: &mut BTreeMap<String, BTreeMap<String, CallingConvention>>,
) {
for types in reader.values() {
for types in reader().values() {
for ty in types.values() {
let Some(ty) = cpp_fn(ty) else {
continue;
Expand All @@ -40,12 +32,12 @@ fn combine_libraries(
0
};

libraries
result
.entry(library)
.or_default()
.insert(name, CallingConvention::Stdcall(params));
} else if flags.contains(PInvokeAttributes::CallConvCdecl) {
libraries
result
.entry(library)
.or_default()
.insert(name, CallingConvention::Cdecl);
Expand All @@ -54,6 +46,8 @@ fn combine_libraries(
}
}
}

result
}

fn cpp_fn(types: &[Type]) -> Option<CppFn> {
Expand Down
6 changes: 3 additions & 3 deletions crates/libs/bindgen/src/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ pub struct Reference {
pub struct References(Vec<Reference>);

impl References {
pub fn new(reader: &'static Reader, stage: Vec<ReferenceStage>) -> Self {
pub fn new(stage: Vec<ReferenceStage>) -> Self {
Self(
stage
.into_iter()
.map(|stage| {
let filter = Filter::new(reader, &[&stage.path], &[]);
let types = TypeMap::filter(reader, &filter, &References::default());
let filter = Filter::new(&[&stage.path], &[]);
let types = TypeMap::filter(&filter, &References::default());

Reference {
name: stage.name,
Expand Down
3 changes: 2 additions & 1 deletion crates/libs/bindgen/src/type_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ impl TypeMap {
}

#[track_caller]
pub fn filter(reader: &'static Reader, filter: &Filter, references: &References) -> Self {
pub fn filter(filter: &Filter, references: &References) -> Self {
let mut dependencies = Self::new();
let reader = reader();

for namespace in reader.keys() {
if filter.includes_namespace(namespace) {
Expand Down
3 changes: 1 addition & 2 deletions crates/libs/bindgen/src/winmd/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl std::ops::Deref for Reader {
}

impl Reader {
pub fn new(files: Vec<File>) -> &'static Self {
pub fn init(files: Vec<File>) {
let reader = Box::leak(Box::new(Self(HashMap::new())));

for file in files {
Expand Down Expand Up @@ -180,7 +180,6 @@ impl Reader {
}

READER.store(reader, Ordering::Relaxed);
reader
}

#[track_caller]
Expand Down

0 comments on commit 6e75816

Please sign in to comment.