Debian Codegraph @

crates/wast/src/component/expand.rs

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