| 1 |
use crate::component::*; |
| 2 |
use crate::core; |
| 3 |
use crate::gensym; |
| 4 |
use crate::kw; |
| 5 |
use crate::token::Id; |
| 6 |
use crate::token::{Index, Span}; |
| 7 |
use std::collections::HashMap; |
| 8 |
use std::mem; |
| 9 |
|
| 10 |
/// Performs an AST "expansion" pass over the component fields provided. |
| 11 |
/// |
| 12 |
/// This expansion is intended to desugar the AST from various parsed constructs |
| 13 |
/// to bits and bobs amenable for name resolution as well as binary encoding. |
| 14 |
/// For example `(import "i" (func))` is split into a type definition followed by |
| 15 |
/// the import referencing that type definition. |
| 16 |
/// |
| 17 |
/// Most forms of AST expansion happen in this file and afterwards the AST will |
| 18 |
/// be handed to the name resolution pass which will convert `Index::Id` to |
| 19 |
/// `Index::Num` wherever it's found. |
| 20 |
pub fn expand(fields: &mut Vec<ComponentField<'_>>) { |
| 21 |
Expander::default().expand_component_fields(fields) |
| 22 |
} |
| 23 |
|
| 24 |
enum AnyType<'a> { |
| 25 |
Core(CoreType<'a>), |
| 26 |
Component(Type<'a>), |
| 27 |
} |
| 28 |
|
| 29 |
impl<'a> From<AnyType<'a>> for ComponentTypeDecl<'a> { |
| 30 |
fn from(t: AnyType<'a>) -> Self { |
| 31 |
match t { |
| 32 |
AnyType::Core(t) => Self::CoreType(t), |
| 33 |
AnyType::Component(t) => Self::Type(t), |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
impl<'a> From<AnyType<'a>> for InstanceTypeDecl<'a> { |
| 39 |
fn from(t: AnyType<'a>) -> Self { |
| 40 |
match t { |
| 41 |
AnyType::Core(t) => Self::CoreType(t), |
| 42 |
AnyType::Component(t) => Self::Type(t), |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
impl<'a> From<AnyType<'a>> for ComponentField<'a> { |
| 48 |
fn from(t: AnyType<'a>) -> Self { |
| 49 |
match t { |
| 50 |
AnyType::Core(t) => Self::CoreType(t), |
| 51 |
AnyType::Component(t) => Self::Type(t), |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
#[derive(Default)] |
| 57 |
struct Expander<'a> { |
| 58 |
/// Fields, during processing, which should be prepended to the |
| 59 |
/// currently-being-processed field. This should always be empty after |
| 60 |
/// processing is complete. |
| 61 |
types_to_prepend: Vec<AnyType<'a>>, |
| 62 |
component_fields_to_prepend: Vec<ComponentField<'a>>, |
| 63 |
|
| 64 |
/// Fields that are appended to the end of the module once everything has |
| 65 |
/// finished. |
| 66 |
component_fields_to_append: Vec<ComponentField<'a>>, |
| 67 |
} |
| 68 |
|
| 69 |
impl<'a> Expander<'a> { |
| 70 |
fn expand_component_fields(&mut self, fields: &mut Vec<ComponentField<'a>>) { |
| 71 |
let mut cur = 0; |
| 72 |
while cur < fields.len() { |
| 73 |
self.expand_field(&mut fields[cur]); |
| 74 |
let amt = self.types_to_prepend.len() + self.component_fields_to_prepend.len(); |
| 75 |
fields.splice(cur..cur, self.component_fields_to_prepend.drain(..)); |
| 76 |
fields.splice(cur..cur, self.types_to_prepend.drain(..).map(Into::into)); |
| 77 |
cur += 1 + amt; |
| 78 |
} |
| 79 |
fields.append(&mut self.component_fields_to_append); |
| 80 |
} |
| 81 |
|
| 82 |
fn expand_decls<T>(&mut self, decls: &mut Vec<T>, expand: fn(&mut Self, &mut T)) |
| 83 |
where |
| 84 |
T: From<AnyType<'a>>, |
| 85 |
{ |
| 86 |
let mut cur = 0; |
| 87 |
while cur < decls.len() { |
| 88 |
expand(self, &mut decls[cur]); |
| 89 |
assert!(self.component_fields_to_prepend.is_empty()); |
| 90 |
assert!(self.component_fields_to_append.is_empty()); |
| 91 |
let amt = self.types_to_prepend.len(); |
| 92 |
decls.splice(cur..cur, self.types_to_prepend.drain(..).map(From::from)); |
| 93 |
cur += 1 + amt; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
fn expand_field(&mut self, item: &mut ComponentField<'a>) { |
| 98 |
let expanded = match item { |
| 99 |
ComponentField::CoreModule(m) => self.expand_core_module(m), |
| 100 |
ComponentField::CoreInstance(i) => { |
| 101 |
self.expand_core_instance(i); |
| 102 |
None |
| 103 |
} |
| 104 |
ComponentField::CoreType(t) => { |
| 105 |
self.expand_core_type(t); |
| 106 |
None |
| 107 |
} |
| 108 |
ComponentField::CoreRec(_) => None, |
| 109 |
ComponentField::Component(c) => self.expand_nested_component(c), |
| 110 |
ComponentField::Instance(i) => self.expand_instance(i), |
| 111 |
ComponentField::Type(t) => { |
| 112 |
self.expand_type(t); |
| 113 |
None |
| 114 |
} |
| 115 |
ComponentField::CanonicalFunc(f) => { |
| 116 |
self.expand_canonical_func(f); |
| 117 |
None |
| 118 |
} |
| 119 |
ComponentField::CoreFunc(f) => self.expand_core_func(f), |
| 120 |
ComponentField::Func(f) => self.expand_func(f), |
| 121 |
ComponentField::Import(i) => { |
| 122 |
self.expand_item_sig(&mut i.item); |
| 123 |
None |
| 124 |
} |
| 125 |
ComponentField::Export(e) => { |
| 126 |
if let Some(sig) = &mut e.ty { |
| 127 |
self.expand_item_sig(&mut sig.0); |
| 128 |
} |
| 129 |
None |
| 130 |
} |
| 131 |
ComponentField::Start(_) |
| 132 |
| ComponentField::Alias(_) |
| 133 |
| ComponentField::Custom(_) |
| 134 |
| ComponentField::Producers(_) => None, |
| 135 |
}; |
| 136 |
|
| 137 |
if let Some(expanded) = expanded { |
| 138 |
*item = expanded; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
fn expand_core_module(&mut self, module: &mut CoreModule<'a>) -> Option<ComponentField<'a>> { |
| 143 |
for name in module.exports.names.drain(..) { |
| 144 |
let id = gensym::fill(module.span, &mut module.id); |
| 145 |
self.component_fields_to_append |
| 146 |
.push(ComponentField::Export(ComponentExport { |
| 147 |
span: module.span, |
| 148 |
id: None, |
| 149 |
debug_name: None, |
| 150 |
name, |
| 151 |
kind: ComponentExportKind::module(module.span, id), |
| 152 |
ty: None, |
| 153 |
})); |
| 154 |
} |
| 155 |
match &mut module.kind { |
| 156 |
// inline modules are expanded later during resolution |
| 157 |
CoreModuleKind::Inline { .. } => None, |
| 158 |
CoreModuleKind::Import { import, ty } => { |
| 159 |
let idx = self.expand_core_type_use(ty); |
| 160 |
Some(ComponentField::Import(ComponentImport { |
| 161 |
span: module.span, |
| 162 |
name: import.name, |
| 163 |
item: ItemSig { |
| 164 |
span: module.span, |
| 165 |
id: module.id, |
| 166 |
name: None, |
| 167 |
kind: ItemSigKind::CoreModule(CoreTypeUse::Ref(idx)), |
| 168 |
}, |
| 169 |
})) |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
fn expand_core_instance(&mut self, instance: &mut CoreInstance<'a>) { |
| 175 |
match &mut instance.kind { |
| 176 |
CoreInstanceKind::Instantiate { args, .. } => { |
| 177 |
for arg in args { |
| 178 |
self.expand_core_instantiation_arg(&mut arg.kind); |
| 179 |
} |
| 180 |
} |
| 181 |
CoreInstanceKind::BundleOfExports { .. } => {} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
fn expand_nested_component( |
| 186 |
&mut self, |
| 187 |
component: &mut NestedComponent<'a>, |
| 188 |
) -> Option<ComponentField<'a>> { |
| 189 |
for name in component.exports.names.drain(..) { |
| 190 |
let id = gensym::fill(component.span, &mut component.id); |
| 191 |
self.component_fields_to_append |
| 192 |
.push(ComponentField::Export(ComponentExport { |
| 193 |
span: component.span, |
| 194 |
id: None, |
| 195 |
debug_name: None, |
| 196 |
name, |
| 197 |
kind: ComponentExportKind::component(component.span, id), |
| 198 |
ty: None, |
| 199 |
})); |
| 200 |
} |
| 201 |
match &mut component.kind { |
| 202 |
NestedComponentKind::Inline(fields) => { |
| 203 |
expand(fields); |
| 204 |
None |
| 205 |
} |
| 206 |
NestedComponentKind::Import { import, ty } => { |
| 207 |
let idx = self.expand_component_type_use(ty); |
| 208 |
Some(ComponentField::Import(ComponentImport { |
| 209 |
span: component.span, |
| 210 |
name: import.name, |
| 211 |
item: ItemSig { |
| 212 |
span: component.span, |
| 213 |
id: component.id, |
| 214 |
name: None, |
| 215 |
kind: ItemSigKind::Component(ComponentTypeUse::Ref(idx)), |
| 216 |
}, |
| 217 |
})) |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
fn expand_instance(&mut self, instance: &mut Instance<'a>) -> Option<ComponentField<'a>> { |
| 223 |
for name in instance.exports.names.drain(..) { |
| 224 |
let id = gensym::fill(instance.span, &mut instance.id); |
| 225 |
self.component_fields_to_append |
| 226 |
.push(ComponentField::Export(ComponentExport { |
| 227 |
span: instance.span, |
| 228 |
id: None, |
| 229 |
debug_name: None, |
| 230 |
name, |
| 231 |
kind: ComponentExportKind::instance(instance.span, id), |
| 232 |
ty: None, |
| 233 |
})); |
| 234 |
} |
| 235 |
match &mut instance.kind { |
| 236 |
InstanceKind::Import { import, ty } => { |
| 237 |
let idx = self.expand_component_type_use(ty); |
| 238 |
Some(ComponentField::Import(ComponentImport { |
| 239 |
span: instance.span, |
| 240 |
name: import.name, |
| 241 |
item: ItemSig { |
| 242 |
span: instance.span, |
| 243 |
id: instance.id, |
| 244 |
name: None, |
| 245 |
kind: ItemSigKind::Instance(ComponentTypeUse::Ref(idx)), |
| 246 |
}, |
| 247 |
})) |
| 248 |
} |
| 249 |
InstanceKind::Instantiate { args, .. } => { |
| 250 |
for arg in args { |
| 251 |
self.expand_instantiation_arg(&mut arg.kind); |
| 252 |
} |
| 253 |
None |
| 254 |
} |
| 255 |
InstanceKind::BundleOfExports { .. } => None, |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
fn expand_canonical_func(&mut self, func: &mut CanonicalFunc<'a>) { |
| 260 |
match &mut func.kind { |
| 261 |
CanonicalFuncKind::Lift { ty, .. } => { |
| 262 |
self.expand_component_type_use(ty); |
| 263 |
} |
| 264 |
CanonicalFuncKind::Lower(_) |
| 265 |
| CanonicalFuncKind::ResourceNew(_) |
| 266 |
| CanonicalFuncKind::ResourceRep(_) |
| 267 |
| CanonicalFuncKind::ResourceDrop(_) |
| 268 |
| CanonicalFuncKind::ThreadSpawn(_) |
| 269 |
| CanonicalFuncKind::ThreadHwConcurrency(_) => {} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
fn expand_core_func(&mut self, func: &mut CoreFunc<'a>) -> Option<ComponentField<'a>> { |
| 274 |
match &mut func.kind { |
| 275 |
CoreFuncKind::Alias(a) => Some(ComponentField::Alias(Alias { |
| 276 |
span: func.span, |
| 277 |
id: func.id, |
| 278 |
name: func.name, |
| 279 |
target: AliasTarget::CoreExport { |
| 280 |
instance: a.instance, |
| 281 |
name: a.name, |
| 282 |
kind: core::ExportKind::Func, |
| 283 |
}, |
| 284 |
})), |
| 285 |
CoreFuncKind::Lower(info) => Some(ComponentField::CanonicalFunc(CanonicalFunc { |
| 286 |
span: func.span, |
| 287 |
id: func.id, |
| 288 |
name: func.name, |
| 289 |
kind: CanonicalFuncKind::Lower(mem::take(info)), |
| 290 |
})), |
| 291 |
CoreFuncKind::ResourceNew(info) => Some(ComponentField::CanonicalFunc(CanonicalFunc { |
| 292 |
span: func.span, |
| 293 |
id: func.id, |
| 294 |
name: func.name, |
| 295 |
kind: CanonicalFuncKind::ResourceNew(mem::take(info)), |
| 296 |
})), |
| 297 |
CoreFuncKind::ResourceDrop(info) => { |
| 298 |
Some(ComponentField::CanonicalFunc(CanonicalFunc { |
| 299 |
span: func.span, |
| 300 |
id: func.id, |
| 301 |
name: func.name, |
| 302 |
kind: CanonicalFuncKind::ResourceDrop(mem::take(info)), |
| 303 |
})) |
| 304 |
} |
| 305 |
CoreFuncKind::ResourceRep(info) => Some(ComponentField::CanonicalFunc(CanonicalFunc { |
| 306 |
span: func.span, |
| 307 |
id: func.id, |
| 308 |
name: func.name, |
| 309 |
kind: CanonicalFuncKind::ResourceRep(mem::take(info)), |
| 310 |
})), |
| 311 |
CoreFuncKind::ThreadSpawn(info) => Some(ComponentField::CanonicalFunc(CanonicalFunc { |
| 312 |
span: func.span, |
| 313 |
id: func.id, |
| 314 |
name: func.name, |
| 315 |
kind: CanonicalFuncKind::ThreadSpawn(mem::take(info)), |
| 316 |
})), |
| 317 |
CoreFuncKind::ThreadHwConcurrency(info) => { |
| 318 |
Some(ComponentField::CanonicalFunc(CanonicalFunc { |
| 319 |
span: func.span, |
| 320 |
id: func.id, |
| 321 |
name: func.name, |
| 322 |
kind: CanonicalFuncKind::ThreadHwConcurrency(mem::take(info)), |
| 323 |
})) |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
fn expand_func(&mut self, func: &mut Func<'a>) -> Option<ComponentField<'a>> { |
| 329 |
for name in func.exports.names.drain(..) { |
| 330 |
let id = gensym::fill(func.span, &mut func.id); |
| 331 |
self.component_fields_to_append |
| 332 |
.push(ComponentField::Export(ComponentExport { |
| 333 |
span: func.span, |
| 334 |
id: None, |
| 335 |
debug_name: None, |
| 336 |
name, |
| 337 |
kind: ComponentExportKind::func(func.span, id), |
| 338 |
ty: None, |
| 339 |
})); |
| 340 |
} |
| 341 |
match &mut func.kind { |
| 342 |
FuncKind::Import { import, ty } => { |
| 343 |
let idx = self.expand_component_type_use(ty); |
| 344 |
Some(ComponentField::Import(ComponentImport { |
| 345 |
span: func.span, |
| 346 |
name: import.name, |
| 347 |
item: ItemSig { |
| 348 |
span: func.span, |
| 349 |
id: func.id, |
| 350 |
name: None, |
| 351 |
kind: ItemSigKind::Func(ComponentTypeUse::Ref(idx)), |
| 352 |
}, |
| 353 |
})) |
| 354 |
} |
| 355 |
FuncKind::Lift { ty, info } => { |
| 356 |
let idx = self.expand_component_type_use(ty); |
| 357 |
Some(ComponentField::CanonicalFunc(CanonicalFunc { |
| 358 |
span: func.span, |
| 359 |
id: func.id, |
| 360 |
name: func.name, |
| 361 |
kind: CanonicalFuncKind::Lift { |
| 362 |
ty: ComponentTypeUse::Ref(idx), |
| 363 |
info: mem::take(info), |
| 364 |
}, |
| 365 |
})) |
| 366 |
} |
| 367 |
FuncKind::Alias(a) => Some(ComponentField::Alias(Alias { |
| 368 |
span: func.span, |
| 369 |
id: func.id, |
| 370 |
name: func.name, |
| 371 |
target: AliasTarget::Export { |
| 372 |
instance: a.instance, |
| 373 |
name: a.name, |
| 374 |
kind: ComponentExportAliasKind::Func, |
| 375 |
}, |
| 376 |
})), |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
fn expand_core_type(&mut self, field: &mut CoreType<'a>) { |
| 381 |
match &mut field.def { |
| 382 |
CoreTypeDef::Def(_) => {} |
| 383 |
CoreTypeDef::Module(m) => self.expand_module_ty(m), |
| 384 |
} |
| 385 |
|
| 386 |
let id = gensym::fill(field.span, &mut field.id); |
| 387 |
let index = Index::Id(id); |
| 388 |
match &field.def { |
| 389 |
CoreTypeDef::Def(_) => {} |
| 390 |
CoreTypeDef::Module(t) => t.key().insert(self, index), |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
fn expand_type(&mut self, field: &mut Type<'a>) { |
| 395 |
match &mut field.def { |
| 396 |
TypeDef::Defined(d) => self.expand_defined_ty(d), |
| 397 |
TypeDef::Func(f) => self.expand_func_ty(f), |
| 398 |
TypeDef::Component(c) => self.expand_component_ty(c), |
| 399 |
TypeDef::Instance(i) => self.expand_instance_ty(i), |
| 400 |
TypeDef::Resource(_) => {} |
| 401 |
} |
| 402 |
|
| 403 |
let id = gensym::fill(field.span, &mut field.id); |
| 404 |
let index = Index::Id(id); |
| 405 |
match &field.def { |
| 406 |
TypeDef::Defined(t) => t.key().insert(self, index), |
| 407 |
TypeDef::Func(t) => t.key().insert(self, index), |
| 408 |
TypeDef::Component(t) => t.key().insert(self, index), |
| 409 |
TypeDef::Instance(t) => t.key().insert(self, index), |
| 410 |
TypeDef::Resource(_) => {} |
| 411 |
} |
| 412 |
for name in field.exports.names.drain(..) { |
| 413 |
self.component_fields_to_append |
| 414 |
.push(ComponentField::Export(ComponentExport { |
| 415 |
span: field.span, |
| 416 |
id: None, |
| 417 |
debug_name: None, |
| 418 |
name, |
| 419 |
kind: ComponentExportKind::ty(field.span, id), |
| 420 |
ty: None, |
| 421 |
})); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
fn expand_func_ty(&mut self, ty: &mut ComponentFunctionType<'a>) { |
| 426 |
for param in ty.params.iter_mut() { |
| 427 |
self.expand_component_val_ty(&mut param.ty); |
| 428 |
} |
| 429 |
|
| 430 |
for result in ty.results.iter_mut() { |
| 431 |
self.expand_component_val_ty(&mut result.ty); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
fn expand_module_ty(&mut self, ty: &mut ModuleType<'a>) { |
| 436 |
use crate::core::resolve::types::{FuncKey, TypeKey, TypeReference}; |
| 437 |
|
| 438 |
// Note that this is a custom implementation from everything else in |
| 439 |
// this file since this is using core wasm types instead of component |
| 440 |
// types, so a small part of the core wasm expansion process is |
| 441 |
// inlined here to handle the `TypeUse` from core wasm. |
| 442 |
|
| 443 |
let mut func_type_to_idx = HashMap::new(); |
| 444 |
let mut to_prepend = Vec::new(); |
| 445 |
let mut i = 0; |
| 446 |
while i < ty.decls.len() { |
| 447 |
match &mut ty.decls[i] { |
| 448 |
ModuleTypeDecl::Type(ty) => match &ty.def.kind { |
| 449 |
core::InnerTypeKind::Func(f) => { |
| 450 |
let id = gensym::fill(ty.span, &mut ty.id); |
| 451 |
func_type_to_idx.insert(f.key(), Index::Id(id)); |
| 452 |
} |
| 453 |
core::InnerTypeKind::Struct(_) => {} |
| 454 |
core::InnerTypeKind::Array(_) => {} |
| 455 |
core::InnerTypeKind::Cont(_) => {} |
| 456 |
}, |
| 457 |
ModuleTypeDecl::Rec(_) => {} |
| 458 |
ModuleTypeDecl::Alias(_) => {} |
| 459 |
ModuleTypeDecl::Import(ty) => { |
| 460 |
expand_sig(&mut ty.item, &mut to_prepend, &mut func_type_to_idx); |
| 461 |
} |
| 462 |
ModuleTypeDecl::Export(_, item) => { |
| 463 |
expand_sig(item, &mut to_prepend, &mut func_type_to_idx); |
| 464 |
} |
| 465 |
} |
| 466 |
ty.decls.splice(i..i, to_prepend.drain(..)); |
| 467 |
i += 1; |
| 468 |
} |
| 469 |
|
| 470 |
fn expand_sig<'a>( |
| 471 |
item: &mut core::ItemSig<'a>, |
| 472 |
to_prepend: &mut Vec<ModuleTypeDecl<'a>>, |
| 473 |
func_type_to_idx: &mut HashMap<FuncKey<'a>, Index<'a>>, |
| 474 |
) { |
| 475 |
match &mut item.kind { |
| 476 |
core::ItemKind::Func(t) | core::ItemKind::Tag(core::TagType::Exception(t)) => { |
| 477 |
// If the index is already filled in then this is skipped. |
| 478 |
if t.index.is_some() { |
| 479 |
return; |
| 480 |
} |
| 481 |
|
| 482 |
// Otherwise the inline type information is used to |
| 483 |
// generate a type into this module if necessary. If the |
| 484 |
// function type already exists we reuse the same key, |
| 485 |
// otherwise a fresh type definition is created and we use |
| 486 |
// that one instead. |
| 487 |
let ty = t.inline.take().unwrap_or_default(); |
| 488 |
let key = ty.key(); |
| 489 |
if let Some(idx) = func_type_to_idx.get(&key) { |
| 490 |
t.index = Some(*idx); |
| 491 |
return; |
| 492 |
} |
| 493 |
let id = gensym::gen(item.span); |
| 494 |
to_prepend.push(ModuleTypeDecl::Type(core::Type { |
| 495 |
span: item.span, |
| 496 |
id: Some(id), |
| 497 |
name: None, |
| 498 |
// Currently, there is no way in the WebAssembly text |
| 499 |
// format to mark a function `shared` inline; a |
| 500 |
// `shared` function must use an explicit type index, |
| 501 |
// e.g., `(func (type $ft))`. |
| 502 |
def: key.to_def(item.span, /* shared = */ false), |
| 503 |
})); |
| 504 |
let idx = Index::Id(id); |
| 505 |
t.index = Some(idx); |
| 506 |
} |
| 507 |
core::ItemKind::Global(_) |
| 508 |
| core::ItemKind::Table(_) |
| 509 |
| core::ItemKind::Memory(_) => {} |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
fn expand_component_ty(&mut self, ty: &mut ComponentType<'a>) { |
| 515 |
Expander::default().expand_decls(&mut ty.decls, |e, decl| match decl { |
| 516 |
ComponentTypeDecl::CoreType(t) => e.expand_core_type(t), |
| 517 |
ComponentTypeDecl::Type(t) => e.expand_type(t), |
| 518 |
ComponentTypeDecl::Alias(_) => {} |
| 519 |
ComponentTypeDecl::Export(t) => e.expand_item_sig(&mut t.item), |
| 520 |
ComponentTypeDecl::Import(t) => e.expand_item_sig(&mut t.item), |
| 521 |
}) |
| 522 |
} |
| 523 |
|
| 524 |
fn expand_instance_ty(&mut self, ty: &mut InstanceType<'a>) { |
| 525 |
Expander::default().expand_decls(&mut ty.decls, |e, decl| match decl { |
| 526 |
InstanceTypeDecl::CoreType(t) => e.expand_core_type(t), |
| 527 |
InstanceTypeDecl::Type(t) => e.expand_type(t), |
| 528 |
InstanceTypeDecl::Alias(_) => {} |
| 529 |
InstanceTypeDecl::Export(t) => e.expand_item_sig(&mut t.item), |
| 530 |
}) |
| 531 |
} |
| 532 |
|
| 533 |
fn expand_item_sig(&mut self, ext: &mut ItemSig<'a>) { |
| 534 |
match &mut ext.kind { |
| 535 |
ItemSigKind::CoreModule(t) => { |
| 536 |
self.expand_core_type_use(t); |
| 537 |
} |
| 538 |
ItemSigKind::Func(t) => { |
| 539 |
self.expand_component_type_use(t); |
| 540 |
} |
| 541 |
ItemSigKind::Component(t) => { |
| 542 |
self.expand_component_type_use(t); |
| 543 |
} |
| 544 |
ItemSigKind::Instance(t) => { |
| 545 |
self.expand_component_type_use(t); |
| 546 |
} |
| 547 |
ItemSigKind::Value(t) => { |
| 548 |
self.expand_component_val_ty(&mut t.0); |
| 549 |
} |
| 550 |
ItemSigKind::Type(_) => {} |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
fn expand_defined_ty(&mut self, ty: &mut ComponentDefinedType<'a>) { |
| 555 |
match ty { |
| 556 |
ComponentDefinedType::Primitive(_) |
| 557 |
| ComponentDefinedType::Flags(_) |
| 558 |
| ComponentDefinedType::Enum(_) => {} |
| 559 |
ComponentDefinedType::Record(r) => { |
| 560 |
for field in r.fields.iter_mut() { |
| 561 |
self.expand_component_val_ty(&mut field.ty); |
| 562 |
} |
| 563 |
} |
| 564 |
ComponentDefinedType::Variant(v) => { |
| 565 |
for case in v.cases.iter_mut() { |
| 566 |
if let Some(ty) = &mut case.ty { |
| 567 |
self.expand_component_val_ty(ty); |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
ComponentDefinedType::List(t) => { |
| 572 |
self.expand_component_val_ty(&mut t.element); |
| 573 |
} |
| 574 |
ComponentDefinedType::Tuple(t) => { |
| 575 |
for field in t.fields.iter_mut() { |
| 576 |
self.expand_component_val_ty(field); |
| 577 |
} |
| 578 |
} |
| 579 |
ComponentDefinedType::Option(t) => { |
| 580 |
self.expand_component_val_ty(&mut t.element); |
| 581 |
} |
| 582 |
ComponentDefinedType::Result(r) => { |
| 583 |
if let Some(ty) = &mut r.ok { |
| 584 |
self.expand_component_val_ty(ty); |
| 585 |
} |
| 586 |
|
| 587 |
if let Some(ty) = &mut r.err { |
| 588 |
self.expand_component_val_ty(ty); |
| 589 |
} |
| 590 |
} |
| 591 |
ComponentDefinedType::Own(_) | ComponentDefinedType::Borrow(_) => {} |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
fn expand_component_val_ty(&mut self, ty: &mut ComponentValType<'a>) { |
| 596 |
let inline = match ty { |
| 597 |
ComponentValType::Inline(ComponentDefinedType::Primitive(_)) |
| 598 |
| ComponentValType::Ref(_) => return, |
| 599 |
ComponentValType::Inline(inline) => { |
| 600 |
self.expand_defined_ty(inline); |
| 601 |
mem::take(inline) |
| 602 |
} |
| 603 |
}; |
| 604 |
// If this inline type has already been defined within this context |
| 605 |
// then reuse the previously defined type to avoid injecting too many |
| 606 |
// types into the type index space. |
| 607 |
if let Some(idx) = inline.key().lookup(self) { |
| 608 |
*ty = ComponentValType::Ref(idx); |
| 609 |
return; |
| 610 |
} |
| 611 |
|
| 612 |
// And if this type isn't already defined we append it to the index |
| 613 |
// space with a fresh and unique name. |
| 614 |
let span = Span::from_offset(0); // FIXME(#613): don't manufacture |
| 615 |
let id = gensym::gen(span); |
| 616 |
|
| 617 |
self.types_to_prepend.push(inline.into_any_type(span, id)); |
| 618 |
|
| 619 |
let idx = Index::Id(id); |
| 620 |
*ty = ComponentValType::Ref(idx); |
| 621 |
} |
| 622 |
|
| 623 |
fn expand_core_type_use<T>( |
| 624 |
&mut self, |
| 625 |
item: &mut CoreTypeUse<'a, T>, |
| 626 |
) -> CoreItemRef<'a, kw::r#type> |
| 627 |
where |
| 628 |
T: TypeReference<'a>, |
| 629 |
{ |
| 630 |
let span = Span::from_offset(0); // FIXME(#613): don't manufacture |
| 631 |
let mut inline = match mem::take(item) { |
| 632 |
// If this type-use was already a reference to an existing type |
| 633 |
// then we put it back the way it was and return the corresponding |
| 634 |
// index. |
| 635 |
CoreTypeUse::Ref(idx) => { |
| 636 |
*item = CoreTypeUse::Ref(idx.clone()); |
| 637 |
return idx; |
| 638 |
} |
| 639 |
|
| 640 |
// ... otherwise with an inline type definition we go into |
| 641 |
// processing below. |
| 642 |
CoreTypeUse::Inline(inline) => inline, |
| 643 |
}; |
| 644 |
inline.expand(self); |
| 645 |
|
| 646 |
// If this inline type has already been defined within this context |
| 647 |
// then reuse the previously defined type to avoid injecting too many |
| 648 |
// types into the type index space. |
| 649 |
if let Some(idx) = inline.key().lookup(self) { |
| 650 |
let ret = CoreItemRef { |
| 651 |
idx, |
| 652 |
kind: kw::r#type(span), |
| 653 |
export_name: None, |
| 654 |
}; |
| 655 |
*item = CoreTypeUse::Ref(ret.clone()); |
| 656 |
return ret; |
| 657 |
} |
| 658 |
|
| 659 |
// And if this type isn't already defined we append it to the index |
| 660 |
// space with a fresh and unique name. |
| 661 |
let id = gensym::gen(span); |
| 662 |
|
| 663 |
self.types_to_prepend.push(inline.into_any_type(span, id)); |
| 664 |
|
| 665 |
let idx = Index::Id(id); |
| 666 |
let ret = CoreItemRef { |
| 667 |
idx, |
| 668 |
kind: kw::r#type(span), |
| 669 |
export_name: None, |
| 670 |
}; |
| 671 |
|
| 672 |
*item = CoreTypeUse::Ref(ret.clone()); |
| 673 |
ret |
| 674 |
} |
| 675 |
|
| 676 |
fn expand_component_type_use<T>( |
| 677 |
&mut self, |
| 678 |
item: &mut ComponentTypeUse<'a, T>, |
| 679 |
) -> ItemRef<'a, kw::r#type> |
| 680 |
where |
| 681 |
T: TypeReference<'a>, |
| 682 |
{ |
| 683 |
let span = Span::from_offset(0); // FIXME(#613): don't manufacture |
| 684 |
let mut inline = match mem::take(item) { |
| 685 |
// If this type-use was already a reference to an existing type |
| 686 |
// then we put it back the way it was and return the corresponding |
| 687 |
// index. |
| 688 |
ComponentTypeUse::Ref(idx) => { |
| 689 |
*item = ComponentTypeUse::Ref(idx.clone()); |
| 690 |
return idx; |
| 691 |
} |
| 692 |
|
| 693 |
// ... otherwise with an inline type definition we go into |
| 694 |
// processing below. |
| 695 |
ComponentTypeUse::Inline(inline) => inline, |
| 696 |
}; |
| 697 |
inline.expand(self); |
| 698 |
|
| 699 |
// If this inline type has already been defined within this context |
| 700 |
// then reuse the previously defined type to avoid injecting too many |
| 701 |
// types into the type index space. |
| 702 |
if let Some(idx) = inline.key().lookup(self) { |
| 703 |
let ret = ItemRef { |
| 704 |
idx, |
| 705 |
kind: kw::r#type(span), |
| 706 |
export_names: Vec::new(), |
| 707 |
}; |
| 708 |
*item = ComponentTypeUse::Ref(ret.clone()); |
| 709 |
return ret; |
| 710 |
} |
| 711 |
|
| 712 |
// And if this type isn't already defined we append it to the index |
| 713 |
// space with a fresh and unique name. |
| 714 |
let id = gensym::gen(span); |
| 715 |
|
| 716 |
self.types_to_prepend.push(inline.into_any_type(span, id)); |
| 717 |
|
| 718 |
let idx = Index::Id(id); |
| 719 |
let ret = ItemRef { |
| 720 |
idx, |
| 721 |
kind: kw::r#type(span), |
| 722 |
export_names: Vec::new(), |
| 723 |
}; |
| 724 |
|
| 725 |
*item = ComponentTypeUse::Ref(ret.clone()); |
| 726 |
ret |
| 727 |
} |
| 728 |
|
| 729 |
fn expand_core_instantiation_arg(&mut self, arg: &mut CoreInstantiationArgKind<'a>) { |
| 730 |
let (span, exports) = match arg { |
| 731 |
CoreInstantiationArgKind::Instance(_) => return, |
| 732 |
CoreInstantiationArgKind::BundleOfExports(span, exports) => (*span, mem::take(exports)), |
| 733 |
}; |
| 734 |
let id = gensym::gen(span); |
| 735 |
self.component_fields_to_prepend |
| 736 |
.push(ComponentField::CoreInstance(CoreInstance { |
| 737 |
span, |
| 738 |
id: Some(id), |
| 739 |
name: None, |
| 740 |
kind: CoreInstanceKind::BundleOfExports(exports), |
| 741 |
})); |
| 742 |
*arg = CoreInstantiationArgKind::Instance(CoreItemRef { |
| 743 |
kind: kw::instance(span), |
| 744 |
idx: Index::Id(id), |
| 745 |
export_name: None, |
| 746 |
}); |
| 747 |
} |
| 748 |
|
| 749 |
fn expand_instantiation_arg(&mut self, arg: &mut InstantiationArgKind<'a>) { |
| 750 |
let (span, exports) = match arg { |
| 751 |
InstantiationArgKind::Item(_) => return, |
| 752 |
InstantiationArgKind::BundleOfExports(span, exports) => (*span, mem::take(exports)), |
| 753 |
}; |
| 754 |
let id = gensym::gen(span); |
| 755 |
self.component_fields_to_prepend |
| 756 |
.push(ComponentField::Instance(Instance { |
| 757 |
span, |
| 758 |
id: Some(id), |
| 759 |
name: None, |
| 760 |
exports: Default::default(), |
| 761 |
kind: InstanceKind::BundleOfExports(exports), |
| 762 |
})); |
| 763 |
*arg = InstantiationArgKind::Item(ComponentExportKind::instance(span, id)); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
trait TypeReference<'a> { |
| 768 |
type Key: TypeKey<'a>; |
| 769 |
fn key(&self) -> Self::Key; |
| 770 |
fn expand(&mut self, cx: &mut Expander<'a>); |
| 771 |
fn into_any_type(self, span: Span, id: Id<'a>) -> AnyType<'a>; |
| 772 |
} |
| 773 |
|
| 774 |
impl<'a> TypeReference<'a> for ComponentDefinedType<'a> { |
| 775 |
type Key = Todo; // FIXME(#598): should implement this |
| 776 |
|
| 777 |
fn key(&self) -> Self::Key { |
| 778 |
Todo |
| 779 |
} |
| 780 |
|
| 781 |
fn expand(&mut self, cx: &mut Expander<'a>) { |
| 782 |
cx.expand_defined_ty(self) |
| 783 |
} |
| 784 |
|
| 785 |
fn into_any_type(self, span: Span, id: Id<'a>) -> AnyType<'a> { |
| 786 |
AnyType::Component(Type { |
| 787 |
span, |
| 788 |
id: Some(id), |
| 789 |
name: None, |
| 790 |
exports: Default::default(), |
| 791 |
def: TypeDef::Defined(self), |
| 792 |
}) |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
impl<'a> TypeReference<'a> for ComponentType<'a> { |
| 797 |
type Key = Todo; // FIXME(#598): should implement this |
| 798 |
|
| 799 |
fn key(&self) -> Self::Key { |
| 800 |
Todo |
| 801 |
} |
| 802 |
|
| 803 |
fn expand(&mut self, cx: &mut Expander<'a>) { |
| 804 |
cx.expand_component_ty(self) |
| 805 |
} |
| 806 |
|
| 807 |
fn into_any_type(self, span: Span, id: Id<'a>) -> AnyType<'a> { |
| 808 |
AnyType::Component(Type { |
| 809 |
span, |
| 810 |
id: Some(id), |
| 811 |
name: None, |
| 812 |
exports: Default::default(), |
| 813 |
def: TypeDef::Component(self), |
| 814 |
}) |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
impl<'a> TypeReference<'a> for ModuleType<'a> { |
| 819 |
type Key = Todo; // FIXME(#598): should implement this |
| 820 |
|
| 821 |
fn key(&self) -> Self::Key { |
| 822 |
Todo |
| 823 |
} |
| 824 |
|
| 825 |
fn expand(&mut self, cx: &mut Expander<'a>) { |
| 826 |
cx.expand_module_ty(self) |
| 827 |
} |
| 828 |
|
| 829 |
fn into_any_type(self, span: Span, id: Id<'a>) -> AnyType<'a> { |
| 830 |
AnyType::Core(CoreType { |
| 831 |
span, |
| 832 |
id: Some(id), |
| 833 |
name: None, |
| 834 |
def: CoreTypeDef::Module(self), |
| 835 |
}) |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
impl<'a> TypeReference<'a> for InstanceType<'a> { |
| 840 |
type Key = Todo; // FIXME(#598): should implement this |
| 841 |
|
| 842 |
fn key(&self) -> Self::Key { |
| 843 |
Todo |
| 844 |
} |
| 845 |
|
| 846 |
fn expand(&mut self, cx: &mut Expander<'a>) { |
| 847 |
cx.expand_instance_ty(self) |
| 848 |
} |
| 849 |
|
| 850 |
fn into_any_type(self, span: Span, id: Id<'a>) -> AnyType<'a> { |
| 851 |
AnyType::Component(Type { |
| 852 |
span, |
| 853 |
id: Some(id), |
| 854 |
name: None, |
| 855 |
exports: Default::default(), |
| 856 |
def: TypeDef::Instance(self), |
| 857 |
}) |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
impl<'a> TypeReference<'a> for ComponentFunctionType<'a> { |
| 862 |
type Key = Todo; // FIXME(#598): should implement this |
| 863 |
|
| 864 |
fn key(&self) -> Self::Key { |
| 865 |
Todo |
| 866 |
} |
| 867 |
|
| 868 |
fn expand(&mut self, cx: &mut Expander<'a>) { |
| 869 |
cx.expand_func_ty(self) |
| 870 |
} |
| 871 |
|
| 872 |
fn into_any_type(self, span: Span, id: Id<'a>) -> AnyType<'a> { |
| 873 |
AnyType::Component(Type { |
| 874 |
span, |
| 875 |
id: Some(id), |
| 876 |
name: None, |
| 877 |
exports: Default::default(), |
| 878 |
def: TypeDef::Func(self), |
| 879 |
}) |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
trait TypeKey<'a> { |
| 884 |
fn lookup(&self, cx: &Expander<'a>) -> Option<Index<'a>>; |
| 885 |
fn insert(&self, cx: &mut Expander<'a>, index: Index<'a>); |
| 886 |
} |
| 887 |
|
| 888 |
struct Todo; |
| 889 |
|
| 890 |
impl<'a> TypeKey<'a> for Todo { |
| 891 |
fn lookup(&self, _cx: &Expander<'a>) -> Option<Index<'a>> { |
| 892 |
None |
| 893 |
} |
| 894 |
|
| 895 |
fn insert(&self, _cx: &mut Expander<'a>, _index: Index<'a>) {} |
| 896 |
} |