| 1 |
//! A helper, non public, module to assist with cloning items within a |
| 2 |
//! `Resolve`. |
| 3 |
//! |
| 4 |
//! Cloning items is not as simple as calling `Clone` due to the nature of how |
| 5 |
//! `TypeId` tracks relationships between interfaces and types. A full deep |
| 6 |
//! clone requires walking the full structure and allocating new id links. This |
| 7 |
//! is akin to, for example, creating a deep copy of an `Rc<T>` by calling |
| 8 |
//! `Clone for T`. |
| 9 |
//! |
| 10 |
//! This is currently used when merging worlds together to help copy anonymously |
| 11 |
//! named items from one world to another. |
| 12 |
//! |
| 13 |
//! The general structure of this module is that each method takes a mutable |
| 14 |
//! reference to an AST item and updates it as necessary internally, delegating |
| 15 |
//! to other methods for internal AST items. |
| 16 |
//! |
| 17 |
//! This module does not at the time of this writing have full support for |
| 18 |
//! cloning everything within a `Resolve`. |
| 19 |
|
| 20 |
use crate::*; |
| 21 |
use std::collections::HashMap; |
| 22 |
|
| 23 |
pub struct Cloner<'a> { |
| 24 |
pub resolve: &'a mut Resolve, |
| 25 |
prev_owner: TypeOwner, |
| 26 |
new_owner: TypeOwner, |
| 27 |
|
| 28 |
/// This map keeps track, in the current scope of types, of all copied |
| 29 |
/// types. This deduplicates copying types to ensure that they're only |
| 30 |
/// copied at most once. |
| 31 |
types: HashMap<TypeId, TypeId>, |
| 32 |
|
| 33 |
/// If `None` then it's inferred from `self.new_owner`. |
| 34 |
pub new_package: Option<PackageId>, |
| 35 |
} |
| 36 |
|
| 37 |
impl<'a> Cloner<'a> { |
| 38 |
pub fn new( |
| 39 |
resolve: &'a mut Resolve, |
| 40 |
prev_owner: TypeOwner, |
| 41 |
new_owner: TypeOwner, |
| 42 |
) -> Cloner<'a> { |
| 43 |
Cloner { |
| 44 |
prev_owner, |
| 45 |
new_owner, |
| 46 |
resolve, |
| 47 |
types: Default::default(), |
| 48 |
new_package: None, |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
pub fn register_world_type_overlap(&mut self, from: WorldId, into: WorldId) { |
| 53 |
let into = &self.resolve.worlds[into]; |
| 54 |
let from = &self.resolve.worlds[from]; |
| 55 |
for (name, into_import) in into.imports.iter() { |
| 56 |
let WorldKey::Name(_) = name else { continue }; |
| 57 |
let WorldItem::Type(into_id) = into_import else { |
| 58 |
continue; |
| 59 |
}; |
| 60 |
let Some(WorldItem::Type(from_id)) = from.imports.get(name) else { |
| 61 |
continue; |
| 62 |
}; |
| 63 |
self.types.insert(*from_id, *into_id); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
pub fn world_item(&mut self, key: &WorldKey, item: &mut WorldItem) { |
| 68 |
match key { |
| 69 |
WorldKey::Name(_) => {} |
| 70 |
WorldKey::Interface(_) => return, |
| 71 |
} |
| 72 |
|
| 73 |
match item { |
| 74 |
WorldItem::Type(t) => { |
| 75 |
self.type_id(t); |
| 76 |
} |
| 77 |
WorldItem::Function(f) => { |
| 78 |
self.function(f); |
| 79 |
} |
| 80 |
WorldItem::Interface { id, .. } => { |
| 81 |
self.interface(id); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
fn type_id(&mut self, ty: &mut TypeId) { |
| 87 |
if !self.types.contains_key(ty) { |
| 88 |
let mut new = self.resolve.types[*ty].clone(); |
| 89 |
self.type_def(&mut new); |
| 90 |
let id = self.resolve.types.alloc(new); |
| 91 |
self.types.insert(*ty, id); |
| 92 |
} |
| 93 |
*ty = self.types[ty]; |
| 94 |
} |
| 95 |
|
| 96 |
fn type_def(&mut self, def: &mut TypeDef) { |
| 97 |
if def.owner != TypeOwner::None { |
| 98 |
assert_eq!(def.owner, self.prev_owner); |
| 99 |
def.owner = self.new_owner; |
| 100 |
} |
| 101 |
match &mut def.kind { |
| 102 |
TypeDefKind::Type(Type::Id(id)) => { |
| 103 |
if self.resolve.types[*id].owner == self.prev_owner { |
| 104 |
self.type_id(id); |
| 105 |
} else { |
| 106 |
// .. |
| 107 |
} |
| 108 |
} |
| 109 |
TypeDefKind::Type(_) |
| 110 |
| TypeDefKind::Resource |
| 111 |
| TypeDefKind::Flags(_) |
| 112 |
| TypeDefKind::Enum(_) => {} |
| 113 |
TypeDefKind::Handle(Handle::Own(ty) | Handle::Borrow(ty)) => { |
| 114 |
self.type_id(ty); |
| 115 |
} |
| 116 |
TypeDefKind::Option(ty) |
| 117 |
| TypeDefKind::List(ty) |
| 118 |
| TypeDefKind::FixedSizeList(ty, ..) => { |
| 119 |
self.ty(ty); |
| 120 |
} |
| 121 |
TypeDefKind::Tuple(list) => { |
| 122 |
for ty in list.types.iter_mut() { |
| 123 |
self.ty(ty); |
| 124 |
} |
| 125 |
} |
| 126 |
TypeDefKind::Record(r) => { |
| 127 |
for field in r.fields.iter_mut() { |
| 128 |
self.ty(&mut field.ty); |
| 129 |
} |
| 130 |
} |
| 131 |
TypeDefKind::Variant(r) => { |
| 132 |
for case in r.cases.iter_mut() { |
| 133 |
if let Some(ty) = &mut case.ty { |
| 134 |
self.ty(ty); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
TypeDefKind::Result(r) => { |
| 139 |
if let Some(ok) = &mut r.ok { |
| 140 |
self.ty(ok); |
| 141 |
} |
| 142 |
if let Some(err) = &mut r.err { |
| 143 |
self.ty(err); |
| 144 |
} |
| 145 |
} |
| 146 |
TypeDefKind::Future(ty) | TypeDefKind::Stream(ty) => { |
| 147 |
if let Some(ty) = ty { |
| 148 |
self.ty(ty); |
| 149 |
} |
| 150 |
} |
| 151 |
TypeDefKind::Unknown => {} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
fn ty(&mut self, ty: &mut Type) { |
| 156 |
match ty { |
| 157 |
Type::Id(id) => self.type_id(id), |
| 158 |
_ => {} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
fn function(&mut self, func: &mut Function) { |
| 163 |
if let Some(id) = func.kind.resource_mut() { |
| 164 |
self.type_id(id); |
| 165 |
} |
| 166 |
for (_, ty) in func.params.iter_mut() { |
| 167 |
self.ty(ty); |
| 168 |
} |
| 169 |
if let Some(ty) = &mut func.result { |
| 170 |
self.ty(ty); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
fn interface(&mut self, id: &mut InterfaceId) { |
| 175 |
let mut new = self.resolve.interfaces[*id].clone(); |
| 176 |
let next_id = self.resolve.interfaces.next_id(); |
| 177 |
let mut clone = Cloner::new( |
| 178 |
self.resolve, |
| 179 |
TypeOwner::Interface(*id), |
| 180 |
TypeOwner::Interface(next_id), |
| 181 |
); |
| 182 |
for id in new.types.values_mut() { |
| 183 |
clone.type_id(id); |
| 184 |
} |
| 185 |
for func in new.functions.values_mut() { |
| 186 |
clone.function(func); |
| 187 |
} |
| 188 |
new.package = Some(self.new_package.unwrap_or_else(|| match self.new_owner { |
| 189 |
TypeOwner::Interface(id) => self.resolve.interfaces[id].package.unwrap(), |
| 190 |
TypeOwner::World(id) => self.resolve.worlds[id].package.unwrap(), |
| 191 |
TypeOwner::None => unreachable!(), |
| 192 |
})); |
| 193 |
*id = self.resolve.interfaces.alloc(new); |
| 194 |
assert_eq!(*id, next_id); |
| 195 |
} |
| 196 |
} |