Skip to content

Commit

Permalink
Resolve the order of dependencies in the clike language backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviojs committed Jan 26, 2025
1 parent bd78bbe commit f265f9e
Show file tree
Hide file tree
Showing 17 changed files with 923 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Specify the minimum supported Rust version
msrv = "1.70"
msrv = "1.74"
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ exclude = [
clap = { version = "4.3", optional = true }
indexmap = "2.1.0"
log = "0.4"
multimap = "0.10"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
serde_json = "1.0"
tempfile = "3"
Expand Down
3 changes: 3 additions & 0 deletions src/bindgen/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub enum ItemContainer {
Union(Union),
Enum(Enum),
Typedef(Typedef),
/// Generated by the language backend to resolve the order of dependencies.
Declaration(Box<ItemContainer>),
}

impl ItemContainer {
Expand All @@ -76,6 +78,7 @@ impl ItemContainer {
ItemContainer::Union(ref x) => x,
ItemContainer::Enum(ref x) => x,
ItemContainer::Typedef(ref x) => x,
ItemContainer::Declaration(ref x) => x.deref(),
}
}
}
Expand Down
42 changes: 39 additions & 3 deletions src/bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ impl Type {
}

pub fn simplify_standard_types(&mut self, config: &Config) {
self.visit_types(|ty| ty.simplify_standard_types(config));
self.visit_types_mut(|ty| ty.simplify_standard_types(config));
if let Some(ty) = self.simplified_type(config) {
*self = ty;
}
Expand All @@ -578,10 +578,33 @@ impl Type {
if let Type::Path(ref mut generic_path) = *self {
generic_path.replace_self_with(self_ty);
}
self.visit_types(|ty| ty.replace_self_with(self_ty))
self.visit_types_mut(|ty| ty.replace_self_with(self_ty))
}

fn visit_types(&mut self, mut visitor: impl FnMut(&mut Type)) {
fn visit_types(&self, mut visitor: impl FnMut(&Type)) {
match *self {
Type::Array(ref ty, ..) | Type::Ptr { ref ty, .. } => visitor(ty),
Type::Path(ref path) => {
for generic in path.generics() {
match *generic {
GenericArgument::Type(ref ty) => visitor(ty),
GenericArgument::Const(_) => {}
}
}
}
Type::Primitive(..) => {}
Type::FuncPtr {
ref ret, ref args, ..
} => {
visitor(ret);
for arg in args {
visitor(&arg.1)
}
}
}
}

fn visit_types_mut(&mut self, mut visitor: impl FnMut(&mut Type)) {
match *self {
Type::Array(ref mut ty, ..) | Type::Ptr { ref mut ty, .. } => visitor(ty),
Type::Path(ref mut path) => {
Expand Down Expand Up @@ -627,6 +650,19 @@ impl Type {
}
}

/// Visit root paths.
/// Includes self and inner types.
pub fn visit_root_paths(&self, mut visitor: impl FnMut(Path)) {
if let Some(path) = self.get_root_path() {
visitor(path);
}
self.visit_types(|ty| {
if let Some(path) = ty.get_root_path() {
visitor(path);
}
});
}

pub fn specialize(&self, mappings: &[(&Path, &GenericArgument)]) -> Type {
match *self {
Type::Ptr {
Expand Down
Loading

0 comments on commit f265f9e

Please sign in to comment.