1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
use std::collections::HashMap;
use wit_parser::*;
#[derive(Default)]
pub struct Types {
type_info: HashMap<TypeId, TypeInfo>,
}
#[derive(Default, Clone, Copy, Debug, PartialEq)]
pub struct TypeInfo {
/// Whether or not this type is ever used (transitively) within a borrowed
/// context, or a parameter to an export function.
pub borrowed: bool,
/// Whether or not this type is ever used (transitively) within an owned
/// context, such as the result of an exported function or in the params or
/// results of an imported function.
pub owned: bool,
/// Whether or not this type is ever used (transitively) within the
/// error case in the result of a function.
pub error: bool,
/// Whether or not this type (transitively) has a list.
pub has_list: bool,
/// Whether or not this type (transitively) has a handle.
pub has_handle: bool,
}
impl std::ops::BitOrAssign for TypeInfo {
fn bitor_assign(&mut self, rhs: Self) {
self.borrowed |= rhs.borrowed;
self.owned |= rhs.owned;
self.error |= rhs.error;
self.has_list |= rhs.has_list;
self.has_handle |= rhs.has_handle;
}
}
impl Types {
pub fn analyze(&mut self, resolve: &Resolve, world: WorldId) {
// Build up all type information first which is inherited through types,
// such as properties of borrows/lists/etc.
for (t, _) in resolve.types.iter() {
self.type_id_info(resolve, t);
}
// ... next handle borrowed/owned flags which aren't inherited through
// types.
let world = &resolve.worlds[world];
for (import, (_, item)) in world
.imports
.iter()
.map(|i| (true, i))
.chain(world.exports.iter().map(|i| (false, i)))
{
match item {
WorldItem::Function(f) => self.type_info_func(resolve, f, import),
WorldItem::Interface { id, .. } => {
let iface = &resolve.interfaces[*id];
for (_, t) in iface.types.iter() {
self.type_id_info(resolve, *t);
}
for (_, f) in iface.functions.iter() {
self.type_info_func(resolve, f, import);
}
}
WorldItem::Type(id) => {
self.type_id_info(resolve, *id);
}
}
}
}
fn type_info_func(&mut self, resolve: &Resolve, func: &Function, import: bool) {
let mut live = LiveTypes::default();
for (_, ty) in func.params.iter() {
self.type_info(resolve, ty);
live.add_type(resolve, ty);
}
for id in live.iter() {
if resolve.types[id].name.is_some() {
let info = self.type_info.get_mut(&id).unwrap();
if import {
info.owned = true;
} else {
info.borrowed = true;
}
}
}
let mut live = LiveTypes::default();
for ty in func.results.iter_types() {
self.type_info(resolve, ty);
live.add_type(resolve, ty);
}
for id in live.iter() {
if resolve.types[id].name.is_some() {
self.type_info.get_mut(&id).unwrap().owned = true;
}
}
for ty in func.results.iter_types() {
let id = match ty {
Type::Id(id) => *id,
_ => continue,
};
let err = match &resolve.types[id].kind {
TypeDefKind::Result(Result_ { err, .. }) => err,
_ => continue,
};
if let Some(Type::Id(id)) = err {
let id = super::resolve_type_definition_id(resolve, *id);
self.type_info.get_mut(&id).unwrap().error = true;
}
}
}
pub fn get(&self, id: TypeId) -> TypeInfo {
self.type_info[&id]
}
fn type_id_info(&mut self, resolve: &Resolve, ty: TypeId) -> TypeInfo {
if let Some(info) = self.type_info.get(&ty) {
return *info;
}
let mut info = TypeInfo::default();
match &resolve.types[ty].kind {
TypeDefKind::Record(r) => {
for field in r.fields.iter() {
info |= self.type_info(resolve, &field.ty);
}
}
TypeDefKind::Tuple(t) => {
for ty in t.types.iter() {
info |= self.type_info(resolve, ty);
}
}
TypeDefKind::Flags(_) => {}
TypeDefKind::Enum(_) => {}
TypeDefKind::Variant(v) => {
for case in v.cases.iter() {
info |= self.optional_type_info(resolve, case.ty.as_ref());
}
}
TypeDefKind::List(ty) => {
info = self.type_info(resolve, ty);
info.has_list = true;
}
TypeDefKind::Type(ty) => {
info = self.type_info(resolve, ty);
}
TypeDefKind::Option(ty) => {
info = self.type_info(resolve, ty);
}
TypeDefKind::Result(r) => {
info = self.optional_type_info(resolve, r.ok.as_ref());
info |= self.optional_type_info(resolve, r.err.as_ref());
}
TypeDefKind::Future(ty) => {
info = self.optional_type_info(resolve, ty.as_ref());
}
TypeDefKind::Stream(stream) => {
info = self.optional_type_info(resolve, stream.element.as_ref());
info |= self.optional_type_info(resolve, stream.end.as_ref());
}
TypeDefKind::Handle(_) => info.has_handle = true,
TypeDefKind::Resource => {}
TypeDefKind::Unknown => unreachable!(),
}
self.type_info.insert(ty, info);
info
}
fn type_info(&mut self, resolve: &Resolve, ty: &Type) -> TypeInfo {
let mut info = TypeInfo::default();
match ty {
Type::String => info.has_list = true,
Type::Id(id) => return self.type_id_info(resolve, *id),
_ => {}
}
info
}
fn optional_type_info(&mut self, resolve: &Resolve, ty: Option<&Type>) -> TypeInfo {
match ty {
Some(ty) => self.type_info(resolve, ty),
None => TypeInfo::default(),
}
}
}
impl TypeInfo {
pub fn is_copy(&self) -> bool {
!self.has_list && !self.has_handle
}
pub fn is_clone(&self) -> bool {
!self.has_handle
}
}
|