summaryrefslogtreecommitdiffstats
path: root/src/bindgen/dependencies.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-18 05:39:07 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-18 05:39:07 +0000
commitaf6b8ed095f88f1df2116cdc7a9d44872cfa6074 (patch)
tree1f2df671c1f8033d5ed83f056167a0911f8d2a57 /src/bindgen/dependencies.rs
parentInitial commit. (diff)
downloadrust-cbindgen-upstream/0.26.0.tar.xz
rust-cbindgen-upstream/0.26.0.zip
Adding upstream version 0.26.0.upstream/0.26.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/bindgen/dependencies.rs')
-rw-r--r--src/bindgen/dependencies.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/bindgen/dependencies.rs b/src/bindgen/dependencies.rs
new file mode 100644
index 0000000..6a98738
--- /dev/null
+++ b/src/bindgen/dependencies.rs
@@ -0,0 +1,46 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+use std::cmp::Ordering;
+use std::collections::HashSet;
+
+use crate::bindgen::ir::{ItemContainer, Path};
+
+/// A dependency list is used for gathering what order to output the types.
+#[derive(Default)]
+pub struct Dependencies {
+ pub order: Vec<ItemContainer>,
+ pub items: HashSet<Path>,
+}
+
+impl Dependencies {
+ pub fn new() -> Dependencies {
+ Dependencies {
+ order: Vec::new(),
+ items: HashSet::new(),
+ }
+ }
+
+ pub fn sort(&mut self) {
+ // Sort untagged enums and opaque structs into their own layers because they don't
+ // depend on each other or anything else.
+ let ordering = |a: &ItemContainer, b: &ItemContainer| match (a, b) {
+ (ItemContainer::Enum(x), ItemContainer::Enum(y))
+ if x.tag.is_none() && y.tag.is_none() =>
+ {
+ x.path.cmp(&y.path)
+ }
+ (ItemContainer::Enum(x), _) if x.tag.is_none() => Ordering::Less,
+ (_, ItemContainer::Enum(x)) if x.tag.is_none() => Ordering::Greater,
+
+ (ItemContainer::OpaqueItem(x), ItemContainer::OpaqueItem(y)) => x.path.cmp(&y.path),
+ (&ItemContainer::OpaqueItem(_), _) => Ordering::Less,
+ (_, &ItemContainer::OpaqueItem(_)) => Ordering::Greater,
+
+ _ => Ordering::Equal,
+ };
+
+ self.order.sort_by(ordering);
+ }
+}