| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
//! Sort definitions so that dependencies come first |
| 6 |
//! |
| 7 |
//! This is needed for languages like Python that will throw errors if the dependent type is |
| 8 |
//! defined by its dependency. |
| 9 |
|
| 10 |
use super::*; |
| 11 |
|
| 12 |
pub fn pass(module: &mut Module) -> Result<()> { |
| 13 |
let ffi_dep_sorter = DependencySorter::new( |
| 14 |
module.ffi_definitions.drain(..), |
| 15 |
FfiDefinitionDependencyLogic, |
| 16 |
); |
| 17 |
module.ffi_definitions = ffi_dep_sorter.sort(); |
| 18 |
|
| 19 |
let type_sorter = DependencySorter::new( |
| 20 |
module.type_definitions.drain(..), |
| 21 |
TypeDefinitionDependencyLogic, |
| 22 |
); |
| 23 |
module.type_definitions = type_sorter.sort(); |
| 24 |
Ok(()) |
| 25 |
} |
| 26 |
|
| 27 |
// Generalized dependency sort using a version of depth-first topological sort: |
| 28 |
// |
| 29 |
// https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search |
| 30 |
// |
| 31 |
// Basically, we do a depth first search into the dependency graph, which ensures that we get dependencies |
| 32 |
// first. |
| 33 |
struct DependencySorter<L: DependencyLogic> { |
| 34 |
logic: L, |
| 35 |
unsorted: IndexMap<String, L::Item>, |
| 36 |
sorted: Vec<L::Item>, |
| 37 |
} |
| 38 |
|
| 39 |
impl<L: DependencyLogic> DependencySorter<L> { |
| 40 |
fn new(items: impl IntoIterator<Item = L::Item>, logic: L) -> Self { |
| 41 |
let unsorted: IndexMap<_, _> = items |
| 42 |
.into_iter() |
| 43 |
.map(|i| (logic.item_name(&i), i)) |
| 44 |
.collect(); |
| 45 |
Self { |
| 46 |
unsorted, |
| 47 |
sorted: vec![], |
| 48 |
logic, |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
fn sort(mut self) -> Vec<L::Item> { |
| 53 |
while let Some(name) = self.unsorted.keys().next() { |
| 54 |
self.recurse(name.clone()); |
| 55 |
} |
| 56 |
self.sorted |
| 57 |
} |
| 58 |
|
| 59 |
fn recurse(&mut self, current_name: String) { |
| 60 |
let Some(current_item) = self.unsorted.shift_remove(¤t_name) else { |
| 61 |
// If `current_name` is not in unsorted, then we've already processed the item |
| 62 |
return; |
| 63 |
}; |
| 64 |
// Add all dependents first |
| 65 |
for name in self.logic.dependency_names(¤t_item) { |
| 66 |
self.recurse(name); |
| 67 |
} |
| 68 |
// Then add the current item |
| 69 |
self.sorted.push(current_item); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/// Logic for a particular dependency sort |
| 74 |
trait DependencyLogic { |
| 75 |
// What are we sorting? |
| 76 |
type Item; |
| 77 |
|
| 78 |
// Get the name of an item |
| 79 |
fn item_name(&self, item: &Self::Item) -> String; |
| 80 |
|
| 81 |
// Get the names of an item's dependencies |
| 82 |
fn dependency_names(&self, item: &Self::Item) -> Vec<String>; |
| 83 |
} |
| 84 |
|
| 85 |
struct FfiDefinitionDependencyLogic; |
| 86 |
|
| 87 |
impl DependencyLogic for FfiDefinitionDependencyLogic { |
| 88 |
type Item = FfiDefinition; |
| 89 |
|
| 90 |
fn item_name(&self, ffi_def: &FfiDefinition) -> String { |
| 91 |
ffi_def.name().to_string() |
| 92 |
} |
| 93 |
|
| 94 |
fn dependency_names(&self, ffi_def: &FfiDefinition) -> Vec<String> { |
| 95 |
match ffi_def { |
| 96 |
FfiDefinition::Struct(ffi_struct) => ffi_struct |
| 97 |
.fields |
| 98 |
.iter() |
| 99 |
.filter_map(|f| Self::type_dependency_name(&f.ty)) |
| 100 |
.collect(), |
| 101 |
FfiDefinition::RustFunction(func) => func |
| 102 |
.arguments |
| 103 |
.iter() |
| 104 |
.map(|a| &a.ty) |
| 105 |
.chain(&func.return_type.ty) |
| 106 |
.filter_map(Self::type_dependency_name) |
| 107 |
.collect(), |
| 108 |
FfiDefinition::FunctionType(func_type) => func_type |
| 109 |
.arguments |
| 110 |
.iter() |
| 111 |
.map(|a| &a.ty) |
| 112 |
.chain(&func_type.return_type.ty) |
| 113 |
.filter_map(Self::type_dependency_name) |
| 114 |
.collect(), |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
impl FfiDefinitionDependencyLogic { |
| 120 |
fn type_dependency_name(ffi_type: &FfiType) -> Option<String> { |
| 121 |
match &ffi_type { |
| 122 |
FfiType::Struct(name) => Some(name.0.clone()), |
| 123 |
FfiType::Function(name) => Some(name.0.clone()), |
| 124 |
FfiType::Reference(inner) | FfiType::MutReference(inner) => { |
| 125 |
Self::type_dependency_name(inner) |
| 126 |
} |
| 127 |
_ => None, |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
struct TypeDefinitionDependencyLogic; |
| 133 |
|
| 134 |
impl DependencyLogic for TypeDefinitionDependencyLogic { |
| 135 |
type Item = TypeDefinition; |
| 136 |
|
| 137 |
fn item_name(&self, type_def: &TypeDefinition) -> String { |
| 138 |
match type_def { |
| 139 |
TypeDefinition::Simple(self_type) |
| 140 |
| TypeDefinition::Optional(OptionalType { self_type, .. }) |
| 141 |
| TypeDefinition::Sequence(SequenceType { self_type, .. }) |
| 142 |
| TypeDefinition::Map(MapType { self_type, .. }) |
| 143 |
| TypeDefinition::Record(Record { self_type, .. }) |
| 144 |
| TypeDefinition::Enum(Enum { self_type, .. }) |
| 145 |
| TypeDefinition::Interface(Interface { self_type, .. }) |
| 146 |
| TypeDefinition::CallbackInterface(CallbackInterface { self_type, .. }) |
| 147 |
| TypeDefinition::Custom(CustomType { self_type, .. }) |
| 148 |
| TypeDefinition::External(ExternalType { self_type, .. }) => { |
| 149 |
self_type.canonical_name.clone() |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
fn dependency_names(&self, type_def: &TypeDefinition) -> Vec<String> { |
| 155 |
match type_def { |
| 156 |
TypeDefinition::Simple(_) => vec![], |
| 157 |
TypeDefinition::Optional(OptionalType { inner, .. }) |
| 158 |
| TypeDefinition::Sequence(SequenceType { inner, .. }) => { |
| 159 |
vec![inner.canonical_name.clone()] |
| 160 |
} |
| 161 |
TypeDefinition::Map(MapType { key, value, .. }) => { |
| 162 |
vec![key.canonical_name.clone(), value.canonical_name.clone()] |
| 163 |
} |
| 164 |
TypeDefinition::Record(r) => r |
| 165 |
.fields |
| 166 |
.iter() |
| 167 |
.map(|f| f.ty.canonical_name.clone()) |
| 168 |
.collect(), |
| 169 |
TypeDefinition::Enum(e) => e |
| 170 |
.variants |
| 171 |
.iter() |
| 172 |
.flat_map(|v| v.fields.iter().map(|f| f.ty.canonical_name.clone())) |
| 173 |
.collect(), |
| 174 |
TypeDefinition::Interface(i) => { |
| 175 |
i.trait_impls |
| 176 |
.iter() |
| 177 |
.map(|i| format!("Type{}", i.trait_name)) |
| 178 |
.chain( |
| 179 |
i.methods |
| 180 |
.iter() |
| 181 |
.map(|meth| &meth.callable) |
| 182 |
.chain(i.vtable.iter().flat_map(|vtable| { |
| 183 |
vtable.methods.iter().map(|meth| &meth.callable) |
| 184 |
})) |
| 185 |
.flat_map(|callable| { |
| 186 |
callable |
| 187 |
.arguments |
| 188 |
.iter() |
| 189 |
.map(|a| &a.ty) |
| 190 |
.chain(&callable.return_type.ty) |
| 191 |
.chain(&callable.throws_type.ty) |
| 192 |
.map(|ty| ty.canonical_name.clone()) |
| 193 |
}), |
| 194 |
) |
| 195 |
.collect() |
| 196 |
} |
| 197 |
TypeDefinition::CallbackInterface(c) => c |
| 198 |
.vtable |
| 199 |
.methods |
| 200 |
.iter() |
| 201 |
.flat_map(|m| { |
| 202 |
m.callable |
| 203 |
.arguments |
| 204 |
.iter() |
| 205 |
.map(|a| &a.ty) |
| 206 |
.chain(&m.callable.return_type.ty) |
| 207 |
.chain(&m.callable.throws_type.ty) |
| 208 |
}) |
| 209 |
.map(|ty| ty.canonical_name.clone()) |
| 210 |
.collect(), |
| 211 |
TypeDefinition::Custom(custom) => { |
| 212 |
vec![custom.builtin.canonical_name.clone()] |
| 213 |
} |
| 214 |
TypeDefinition::External(_) => vec![], |
| 215 |
} |
| 216 |
} |
| 217 |
} |