Debian Codegraph @

crates/wast/src/component/types.rs

raw
1 use crate::component::*;
2 use crate::core;
3 use crate::kw;
4 use crate::parser::Lookahead1;
5 use crate::parser::Peek;
6 use crate::parser::{Parse, Parser, Result};
7 use crate::token::Index;
8 use crate::token::LParen;
9 use crate::token::{Id, NameAnnotation, Span};
10
11 /// A core type declaration.
12 #[derive(Debug)]
13 pub struct CoreType<'a> {
14 /// Where this type was defined.
15 pub span: Span,
16 /// An optional identifier to refer to this `core type` by as part of name
17 /// resolution.
18 pub id: Option<Id<'a>>,
19 /// An optional name for this type stored in the custom `name` section.
20 pub name: Option<NameAnnotation<'a>>,
21 /// The core type's definition.
22 pub def: CoreTypeDef<'a>,
23 }
24
25 impl<'a> Parse<'a> for CoreType<'a> {
26 fn parse(parser: Parser<'a>) -> Result<Self> {
27 let span = parser.parse::<kw::core>()?.0;
28 parser.parse::<kw::r#type>()?;
29 let id = parser.parse()?;
30 let name = parser.parse()?;
31 let def = parser.parens(|p| p.parse())?;
32
33 Ok(Self {
34 span,
35 id,
36 name,
37 def,
38 })
39 }
40 }
41
42 /// Represents a core type definition.
43 ///
44 /// In the future this may be removed when module types are a part of
45 /// a core module.
46 #[derive(Debug)]
47 pub enum CoreTypeDef<'a> {
48 /// The type definition is one of the core types.
49 Def(core::TypeDef<'a>),
50 /// The type definition is a module type.
51 Module(ModuleType<'a>),
52 }
53
54 impl<'a> Parse<'a> for CoreTypeDef<'a> {
55 fn parse(parser: Parser<'a>) -> Result<Self> {
56 if parser.peek::<kw::module>()? {
57 parser.parse::<kw::module>()?;
58 Ok(Self::Module(parser.parse()?))
59 } else {
60 Ok(Self::Def(parser.parse()?))
61 }
62 }
63 }
64
65 /// A type definition for a core module.
66 #[derive(Debug)]
67 pub struct ModuleType<'a> {
68 /// The declarations of the module type.
69 pub decls: Vec<ModuleTypeDecl<'a>>,
70 }
71
72 impl<'a> Parse<'a> for ModuleType<'a> {
73 fn parse(parser: Parser<'a>) -> Result<Self> {
74 parser.depth_check()?;
75 Ok(Self {
76 decls: parser.parse()?,
77 })
78 }
79 }
80
81 /// The declarations of a [`ModuleType`].
82 #[derive(Debug)]
83 pub enum ModuleTypeDecl<'a> {
84 /// A core type.
85 Type(core::Type<'a>),
86 /// A core recursion group.
87 Rec(core::Rec<'a>),
88 /// An alias local to the component type.
89 Alias(Alias<'a>),
90 /// An import.
91 Import(core::Import<'a>),
92 /// An export.
93 Export(&'a str, core::ItemSig<'a>),
94 }
95
96 impl<'a> Parse<'a> for ModuleTypeDecl<'a> {
97 fn parse(parser: Parser<'a>) -> Result<Self> {
98 let mut l = parser.lookahead1();
99 if l.peek::<kw::r#type>()? {
100 Ok(Self::Type(parser.parse()?))
101 } else if l.peek::<kw::rec>()? {
102 Ok(Self::Rec(parser.parse()?))
103 } else if l.peek::<kw::alias>()? {
104 Ok(Self::Alias(Alias::parse_outer_core_type_alias(parser)?))
105 } else if l.peek::<kw::import>()? {
106 Ok(Self::Import(parser.parse()?))
107 } else if l.peek::<kw::export>()? {
108 parser.parse::<kw::export>()?;
109 let name = parser.parse()?;
110 let et = parser.parens(|parser| parser.parse())?;
111 Ok(Self::Export(name, et))
112 } else {
113 Err(l.error())
114 }
115 }
116 }
117
118 impl<'a> Parse<'a> for Vec<ModuleTypeDecl<'a>> {
119 fn parse(parser: Parser<'a>) -> Result<Self> {
120 let mut decls = Vec::new();
121 while !parser.is_empty() {
122 decls.push(parser.parens(|parser| parser.parse())?);
123 }
124 Ok(decls)
125 }
126 }
127
128 /// A type declaration in a component.
129 #[derive(Debug)]
130 pub struct Type<'a> {
131 /// Where this type was defined.
132 pub span: Span,
133 /// An optional identifier to refer to this `type` by as part of name
134 /// resolution.
135 pub id: Option<Id<'a>>,
136 /// An optional name for this type stored in the custom `name` section.
137 pub name: Option<NameAnnotation<'a>>,
138 /// If present, inline export annotations which indicate names this
139 /// definition should be exported under.
140 pub exports: InlineExport<'a>,
141 /// The type definition.
142 pub def: TypeDef<'a>,
143 }
144
145 impl<'a> Type<'a> {
146 /// Parses a `Type` while allowing inline `(export "...")` names to be
147 /// defined.
148 pub fn parse_maybe_with_inline_exports(parser: Parser<'a>) -> Result<Self> {
149 Type::parse(parser, true)
150 }
151
152 fn parse_no_inline_exports(parser: Parser<'a>) -> Result<Self> {
153 Type::parse(parser, false)
154 }
155
156 fn parse(parser: Parser<'a>, allow_inline_exports: bool) -> Result<Self> {
157 let span = parser.parse::<kw::r#type>()?.0;
158 let id = parser.parse()?;
159 let name = parser.parse()?;
160 let exports = if allow_inline_exports {
161 parser.parse()?
162 } else {
163 Default::default()
164 };
165 let def = parser.parse()?;
166
167 Ok(Self {
168 span,
169 id,
170 name,
171 exports,
172 def,
173 })
174 }
175 }
176
177 /// A definition of a component type.
178 #[derive(Debug)]
179 pub enum TypeDef<'a> {
180 /// A defined value type.
181 Defined(ComponentDefinedType<'a>),
182 /// A component function type.
183 Func(ComponentFunctionType<'a>),
184 /// A component type.
185 Component(ComponentType<'a>),
186 /// An instance type.
187 Instance(InstanceType<'a>),
188 /// A resource type.
189 Resource(ResourceType<'a>),
190 }
191
192 impl<'a> Parse<'a> for TypeDef<'a> {
193 fn parse(parser: Parser<'a>) -> Result<Self> {
194 if parser.peek::<LParen>()? {
195 parser.parens(|parser| {
196 let mut l = parser.lookahead1();
197 if l.peek::<kw::func>()? {
198 parser.parse::<kw::func>()?;
199 Ok(Self::Func(parser.parse()?))
200 } else if l.peek::<kw::component>()? {
201 parser.parse::<kw::component>()?;
202 Ok(Self::Component(parser.parse()?))
203 } else if l.peek::<kw::instance>()? {
204 parser.parse::<kw::instance>()?;
205 Ok(Self::Instance(parser.parse()?))
206 } else if l.peek::<kw::resource>()? {
207 parser.parse::<kw::resource>()?;
208 Ok(Self::Resource(parser.parse()?))
209 } else {
210 Ok(Self::Defined(ComponentDefinedType::parse_non_primitive(
211 parser, l,
212 )?))
213 }
214 })
215 } else {
216 // Only primitive types have no parens
217 Ok(Self::Defined(ComponentDefinedType::Primitive(
218 parser.parse()?,
219 )))
220 }
221 }
222 }
223
224 /// A primitive value type.
225 #[allow(missing_docs)]
226 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
227 pub enum PrimitiveValType {
228 Bool,
229 S8,
230 U8,
231 S16,
232 U16,
233 S32,
234 U32,
235 S64,
236 U64,
237 F32,
238 F64,
239 Char,
240 String,
241 ErrorContext,
242 }
243
244 impl<'a> Parse<'a> for PrimitiveValType {
245 fn parse(parser: Parser<'a>) -> Result<Self> {
246 let mut l = parser.lookahead1();
247 if l.peek::<kw::bool_>()? {
248 parser.parse::<kw::bool_>()?;
249 Ok(Self::Bool)
250 } else if l.peek::<kw::s8>()? {
251 parser.parse::<kw::s8>()?;
252 Ok(Self::S8)
253 } else if l.peek::<kw::u8>()? {
254 parser.parse::<kw::u8>()?;
255 Ok(Self::U8)
256 } else if l.peek::<kw::s16>()? {
257 parser.parse::<kw::s16>()?;
258 Ok(Self::S16)
259 } else if l.peek::<kw::u16>()? {
260 parser.parse::<kw::u16>()?;
261 Ok(Self::U16)
262 } else if l.peek::<kw::s32>()? {
263 parser.parse::<kw::s32>()?;
264 Ok(Self::S32)
265 } else if l.peek::<kw::u32>()? {
266 parser.parse::<kw::u32>()?;
267 Ok(Self::U32)
268 } else if l.peek::<kw::s64>()? {
269 parser.parse::<kw::s64>()?;
270 Ok(Self::S64)
271 } else if l.peek::<kw::u64>()? {
272 parser.parse::<kw::u64>()?;
273 Ok(Self::U64)
274 } else if l.peek::<kw::f32>()? {
275 parser.parse::<kw::f32>()?;
276 Ok(Self::F32)
277 } else if l.peek::<kw::f64>()? {
278 parser.parse::<kw::f64>()?;
279 Ok(Self::F64)
280 } else if l.peek::<kw::float32>()? {
281 parser.parse::<kw::float32>()?;
282 Ok(Self::F32)
283 } else if l.peek::<kw::float64>()? {
284 parser.parse::<kw::float64>()?;
285 Ok(Self::F64)
286 } else if l.peek::<kw::char>()? {
287 parser.parse::<kw::char>()?;
288 Ok(Self::Char)
289 } else if l.peek::<kw::string>()? {
290 parser.parse::<kw::string>()?;
291 Ok(Self::String)
292 } else if l.peek::<kw::error_context>()? {
293 parser.parse::<kw::error_context>()?;
294 Ok(Self::ErrorContext)
295 } else {
296 Err(l.error())
297 }
298 }
299 }
300
301 impl Peek for PrimitiveValType {
302 fn peek(cursor: crate::parser::Cursor<'_>) -> Result<bool> {
303 Ok(matches!(
304 cursor.keyword()?,
305 Some(("bool", _))
306 | Some(("s8", _))
307 | Some(("u8", _))
308 | Some(("s16", _))
309 | Some(("u16", _))
310 | Some(("s32", _))
311 | Some(("u32", _))
312 | Some(("s64", _))
313 | Some(("u64", _))
314 | Some(("f32", _))
315 | Some(("f64", _))
316 | Some(("float32", _))
317 | Some(("float64", _))
318 | Some(("char", _))
319 | Some(("string", _))
320 | Some(("error-context", _))
321 ))
322 }
323
324 fn display() -> &'static str {
325 "primitive value type"
326 }
327 }
328
329 /// A component value type.
330 #[allow(missing_docs)]
331 #[derive(Debug)]
332 pub enum ComponentValType<'a> {
333 /// The value type is an inline defined type.
334 Inline(ComponentDefinedType<'a>),
335 /// The value type is an index reference to a defined type.
336 Ref(Index<'a>),
337 }
338
339 impl<'a> Parse<'a> for ComponentValType<'a> {
340 fn parse(parser: Parser<'a>) -> Result<Self> {
341 if parser.peek::<Index<'_>>()? {
342 Ok(Self::Ref(parser.parse()?))
343 } else {
344 Ok(Self::Inline(InlineComponentValType::parse(parser)?.0))
345 }
346 }
347 }
348
349 impl Peek for ComponentValType<'_> {
350 fn peek(cursor: crate::parser::Cursor<'_>) -> Result<bool> {
351 Ok(Index::peek(cursor)? || ComponentDefinedType::peek(cursor)?)
352 }
353
354 fn display() -> &'static str {
355 "component value type"
356 }
357 }
358
359 /// An inline-only component value type.
360 ///
361 /// This variation does not parse type indexes.
362 #[allow(missing_docs)]
363 #[derive(Debug)]
364 pub struct InlineComponentValType<'a>(ComponentDefinedType<'a>);
365
366 impl<'a> Parse<'a> for InlineComponentValType<'a> {
367 fn parse(parser: Parser<'a>) -> Result<Self> {
368 if parser.peek::<LParen>()? {
369 parser.parens(|parser| {
370 Ok(Self(ComponentDefinedType::parse_non_primitive(
371 parser,
372 parser.lookahead1(),
373 )?))
374 })
375 } else {
376 Ok(Self(ComponentDefinedType::Primitive(parser.parse()?)))
377 }
378 }
379 }
380
381 // A component defined type.
382 #[allow(missing_docs)]
383 #[derive(Debug)]
384 pub enum ComponentDefinedType<'a> {
385 Primitive(PrimitiveValType),
386 Record(Record<'a>),
387 Variant(Variant<'a>),
388 List(List<'a>),
389 FixedSizeList(FixedSizeList<'a>),
390 Tuple(Tuple<'a>),
391 Flags(Flags<'a>),
392 Enum(Enum<'a>),
393 Option(OptionType<'a>),
394 Result(ResultType<'a>),
395 Own(Index<'a>),
396 Borrow(Index<'a>),
397 Stream(Stream<'a>),
398 Future(Future<'a>),
399 }
400
401 impl<'a> ComponentDefinedType<'a> {
402 fn parse_non_primitive(parser: Parser<'a>, mut l: Lookahead1<'a>) -> Result<Self> {
403 parser.depth_check()?;
404 if l.peek::<kw::record>()? {
405 Ok(Self::Record(parser.parse()?))
406 } else if l.peek::<kw::variant>()? {
407 Ok(Self::Variant(parser.parse()?))
408 } else if l.peek::<kw::list>()? {
409 parse_list(parser)
410 } else if l.peek::<kw::tuple>()? {
411 Ok(Self::Tuple(parser.parse()?))
412 } else if l.peek::<kw::flags>()? {
413 Ok(Self::Flags(parser.parse()?))
414 } else if l.peek::<kw::enum_>()? {
415 Ok(Self::Enum(parser.parse()?))
416 } else if l.peek::<kw::option>()? {
417 Ok(Self::Option(parser.parse()?))
418 } else if l.peek::<kw::result>()? {
419 Ok(Self::Result(parser.parse()?))
420 } else if l.peek::<kw::own>()? {
421 parser.parse::<kw::own>()?;
422 Ok(Self::Own(parser.parse()?))
423 } else if l.peek::<kw::borrow>()? {
424 parser.parse::<kw::borrow>()?;
425 Ok(Self::Borrow(parser.parse()?))
426 } else if l.peek::<kw::stream>()? {
427 Ok(Self::Stream(parser.parse()?))
428 } else if l.peek::<kw::future>()? {
429 Ok(Self::Future(parser.parse()?))
430 } else {
431 Err(l.error())
432 }
433 }
434 }
435
436 impl Default for ComponentDefinedType<'_> {
437 fn default() -> Self {
438 Self::Primitive(PrimitiveValType::Bool)
439 }
440 }
441
442 impl Peek for ComponentDefinedType<'_> {
443 fn peek(cursor: crate::parser::Cursor<'_>) -> Result<bool> {
444 if PrimitiveValType::peek(cursor)? {
445 return Ok(true);
446 }
447
448 Ok(match cursor.lparen()? {
449 Some(cursor) => matches!(
450 cursor.keyword()?,
451 Some(("record", _))
452 | Some(("variant", _))
453 | Some(("list", _))
454 | Some(("tuple", _))
455 | Some(("flags", _))
456 | Some(("enum", _))
457 | Some(("option", _))
458 | Some(("result", _))
459 | Some(("own", _))
460 | Some(("borrow", _))
461 ),
462 None => false,
463 })
464 }
465
466 fn display() -> &'static str {
467 "component defined type"
468 }
469 }
470
471 /// A record defined type.
472 #[derive(Debug)]
473 pub struct Record<'a> {
474 /// The fields of the record.
475 pub fields: Vec<RecordField<'a>>,
476 }
477
478 impl<'a> Parse<'a> for Record<'a> {
479 fn parse(parser: Parser<'a>) -> Result<Self> {
480 parser.parse::<kw::record>()?;
481 let mut fields = Vec::new();
482 while !parser.is_empty() {
483 fields.push(parser.parens(|p| p.parse())?);
484 }
485 Ok(Self { fields })
486 }
487 }
488
489 /// A record type field.
490 #[derive(Debug)]
491 pub struct RecordField<'a> {
492 /// The name of the field.
493 pub name: &'a str,
494 /// The type of the field.
495 pub ty: ComponentValType<'a>,
496 }
497
498 impl<'a> Parse<'a> for RecordField<'a> {
499 fn parse(parser: Parser<'a>) -> Result<Self> {
500 parser.parse::<kw::field>()?;
501 Ok(Self {
502 name: parser.parse()?,
503 ty: parser.parse()?,
504 })
505 }
506 }
507
508 /// A variant defined type.
509 #[derive(Debug)]
510 pub struct Variant<'a> {
511 /// The cases of the variant type.
512 pub cases: Vec<VariantCase<'a>>,
513 }
514
515 impl<'a> Parse<'a> for Variant<'a> {
516 fn parse(parser: Parser<'a>) -> Result<Self> {
517 parser.parse::<kw::variant>()?;
518 let mut cases = Vec::new();
519 while !parser.is_empty() {
520 cases.push(parser.parens(|p| p.parse())?);
521 }
522 Ok(Self { cases })
523 }
524 }
525
526 /// A case of a variant type.
527 #[derive(Debug)]
528 pub struct VariantCase<'a> {
529 /// Where this `case` was defined
530 pub span: Span,
531 /// An optional identifier to refer to this case by as part of name
532 /// resolution.
533 pub id: Option<Id<'a>>,
534 /// The name of the case.
535 pub name: &'a str,
536 /// The optional type of the case.
537 pub ty: Option<ComponentValType<'a>>,
538 /// The optional refinement.
539 pub refines: Option<Refinement<'a>>,
540 }
541
542 impl<'a> Parse<'a> for VariantCase<'a> {
543 fn parse(parser: Parser<'a>) -> Result<Self> {
544 let span = parser.parse::<kw::case>()?.0;
545 let id = parser.parse()?;
546 let name = parser.parse()?;
547 let ty = parser.parse()?;
548 let refines = if !parser.is_empty() {
549 Some(parser.parse()?)
550 } else {
551 None
552 };
553 Ok(Self {
554 span,
555 id,
556 name,
557 ty,
558 refines,
559 })
560 }
561 }
562
563 /// A refinement for a variant case.
564 #[derive(Debug)]
565 pub enum Refinement<'a> {
566 /// The refinement is referenced by index.
567 Index(Span, Index<'a>),
568 /// The refinement has been resolved to an index into
569 /// the cases of the variant.
570 Resolved(u32),
571 }
572
573 impl<'a> Parse<'a> for Refinement<'a> {
574 fn parse(parser: Parser<'a>) -> Result<Self> {
575 parser.parens(|parser| {
576 let span = parser.parse::<kw::refines>()?.0;
577 let id = parser.parse()?;
578 Ok(Self::Index(span, id))
579 })
580 }
581 }
582
583 /// A list type.
584 #[derive(Debug)]
585 pub struct List<'a> {
586 /// The element type of the array.
587 pub element: Box<ComponentValType<'a>>,
588 }
589
590 /// A fixed size list type.
591 #[derive(Debug)]
592 pub struct FixedSizeList<'a> {
593 /// The element type of the array.
594 pub element: Box<ComponentValType<'a>>,
595 /// Number of Elements
596 pub elements: u32,
597 }
598
599 fn parse_list<'a>(parser: Parser<'a>) -> Result<ComponentDefinedType<'a>> {
600 parser.parse::<kw::list>()?;
601 let tp = parser.parse()?;
602 let elements = parser.parse::<Option<u32>>()?;
603 if let Some(elements) = elements {
604 Ok(ComponentDefinedType::FixedSizeList(FixedSizeList {
605 element: Box::new(tp),
606 elements,
607 }))
608 } else {
609 Ok(ComponentDefinedType::List(List {
610 element: Box::new(tp),
611 }))
612 }
613 }
614
615 /// A tuple type.
616 #[derive(Debug)]
617 pub struct Tuple<'a> {
618 /// The types of the fields of the tuple.
619 pub fields: Vec<ComponentValType<'a>>,
620 }
621
622 impl<'a> Parse<'a> for Tuple<'a> {
623 fn parse(parser: Parser<'a>) -> Result<Self> {
624 parser.parse::<kw::tuple>()?;
625 let mut fields = Vec::new();
626 while !parser.is_empty() {
627 fields.push(parser.parse()?);
628 }
629 Ok(Self { fields })
630 }
631 }
632
633 /// A flags type.
634 #[derive(Debug)]
635 pub struct Flags<'a> {
636 /// The names of the individual flags.
637 pub names: Vec<&'a str>,
638 }
639
640 impl<'a> Parse<'a> for Flags<'a> {
641 fn parse(parser: Parser<'a>) -> Result<Self> {
642 parser.parse::<kw::flags>()?;
643 let mut names = Vec::new();
644 while !parser.is_empty() {
645 names.push(parser.parse()?);
646 }
647 Ok(Self { names })
648 }
649 }
650
651 /// An enum type.
652 #[derive(Debug)]
653 pub struct Enum<'a> {
654 /// The tag names of the enum.
655 pub names: Vec<&'a str>,
656 }
657
658 impl<'a> Parse<'a> for Enum<'a> {
659 fn parse(parser: Parser<'a>) -> Result<Self> {
660 parser.parse::<kw::enum_>()?;
661 let mut names = Vec::new();
662 while !parser.is_empty() {
663 names.push(parser.parse()?);
664 }
665 Ok(Self { names })
666 }
667 }
668
669 /// An optional type.
670 #[derive(Debug)]
671 pub struct OptionType<'a> {
672 /// The type of the value, when a value is present.
673 pub element: Box<ComponentValType<'a>>,
674 }
675
676 impl<'a> Parse<'a> for OptionType<'a> {
677 fn parse(parser: Parser<'a>) -> Result<Self> {
678 parser.parse::<kw::option>()?;
679 Ok(Self {
680 element: Box::new(parser.parse()?),
681 })
682 }
683 }
684
685 /// A result type.
686 #[derive(Debug)]
687 pub struct ResultType<'a> {
688 /// The type on success.
689 pub ok: Option<Box<ComponentValType<'a>>>,
690 /// The type on failure.
691 pub err: Option<Box<ComponentValType<'a>>>,
692 }
693
694 impl<'a> Parse<'a> for ResultType<'a> {
695 fn parse(parser: Parser<'a>) -> Result<Self> {
696 parser.parse::<kw::result>()?;
697
698 let ok: Option<ComponentValType> = parser.parse()?;
699 let err: Option<ComponentValType> = if parser.peek::<LParen>()? {
700 Some(parser.parens(|parser| {
701 parser.parse::<kw::error>()?;
702 parser.parse()
703 })?)
704 } else {
705 None
706 };
707
708 Ok(Self {
709 ok: ok.map(Box::new),
710 err: err.map(Box::new),
711 })
712 }
713 }
714
715 /// A stream type.
716 #[derive(Debug)]
717 pub struct Stream<'a> {
718 /// The element type of the stream.
719 pub element: Option<Box<ComponentValType<'a>>>,
720 }
721
722 impl<'a> Parse<'a> for Stream<'a> {
723 fn parse(parser: Parser<'a>) -> Result<Self> {
724 parser.parse::<kw::stream>()?;
725 Ok(Self {
726 element: parser.parse::<Option<ComponentValType>>()?.map(Box::new),
727 })
728 }
729 }
730
731 /// A future type.
732 #[derive(Debug)]
733 pub struct Future<'a> {
734 /// The element type of the future, if any.
735 pub element: Option<Box<ComponentValType<'a>>>,
736 }
737
738 impl<'a> Parse<'a> for Future<'a> {
739 fn parse(parser: Parser<'a>) -> Result<Self> {
740 parser.parse::<kw::future>()?;
741 Ok(Self {
742 element: parser.parse::<Option<ComponentValType>>()?.map(Box::new),
743 })
744 }
745 }
746
747 /// A component function type with parameters and result.
748 #[derive(Debug)]
749 pub struct ComponentFunctionType<'a> {
750 /// The parameters of a function, optionally each having an identifier for
751 /// name resolution and a name for the custom `name` section.
752 pub params: Box<[ComponentFunctionParam<'a>]>,
753 /// The result of a function.
754 pub result: Option<ComponentValType<'a>>,
755 }
756
757 impl<'a> Parse<'a> for ComponentFunctionType<'a> {
758 fn parse(parser: Parser<'a>) -> Result<Self> {
759 let mut params: Vec<ComponentFunctionParam> = Vec::new();
760 while parser.peek2::<kw::param>()? {
761 params.push(parser.parens(|p| p.parse())?);
762 }
763
764 let result = if parser.peek2::<kw::result>()? {
765 Some(parser.parens(|p| {
766 p.parse::<kw::result>()?;
767 p.parse()
768 })?)
769 } else {
770 None
771 };
772
773 Ok(Self {
774 params: params.into(),
775 result,
776 })
777 }
778 }
779
780 /// A parameter of a [`ComponentFunctionType`].
781 #[derive(Debug)]
782 pub struct ComponentFunctionParam<'a> {
783 /// The name of the parameter
784 pub name: &'a str,
785 /// The type of the parameter.
786 pub ty: ComponentValType<'a>,
787 }
788
789 impl<'a> Parse<'a> for ComponentFunctionParam<'a> {
790 fn parse(parser: Parser<'a>) -> Result<Self> {
791 parser.parse::<kw::param>()?;
792 Ok(Self {
793 name: parser.parse()?,
794 ty: parser.parse()?,
795 })
796 }
797 }
798
799 /// A result of a [`ComponentFunctionType`].
800 #[derive(Debug)]
801 pub struct ComponentFunctionResult<'a> {
802 /// An optionally-specified name of this result
803 pub name: Option<&'a str>,
804 /// The type of the result.
805 pub ty: ComponentValType<'a>,
806 }
807
808 impl<'a> Parse<'a> for ComponentFunctionResult<'a> {
809 fn parse(parser: Parser<'a>) -> Result<Self> {
810 parser.parse::<kw::result>()?;
811 Ok(Self {
812 name: parser.parse()?,
813 ty: parser.parse()?,
814 })
815 }
816 }
817
818 /// The type of an exported item from an component or instance type.
819 #[derive(Debug)]
820 pub struct ComponentExportType<'a> {
821 /// Where this export was defined.
822 pub span: Span,
823 /// The name of this export.
824 pub name: ComponentExternName<'a>,
825 /// The signature of the item.
826 pub item: ItemSig<'a>,
827 }
828
829 impl<'a> Parse<'a> for ComponentExportType<'a> {
830 fn parse(parser: Parser<'a>) -> Result<Self> {
831 let span = parser.parse::<kw::export>()?.0;
832 let name = parser.parse()?;
833 let item = parser.parens(|p| p.parse())?;
834 Ok(Self { span, name, item })
835 }
836 }
837
838 /// A type definition for a component type.
839 #[derive(Debug, Default)]
840 pub struct ComponentType<'a> {
841 /// The declarations of the component type.
842 pub decls: Vec<ComponentTypeDecl<'a>>,
843 }
844
845 impl<'a> Parse<'a> for ComponentType<'a> {
846 fn parse(parser: Parser<'a>) -> Result<Self> {
847 parser.depth_check()?;
848 Ok(Self {
849 decls: parser.parse()?,
850 })
851 }
852 }
853
854 /// A declaration of a component type.
855 #[derive(Debug)]
856 pub enum ComponentTypeDecl<'a> {
857 /// A core type definition local to the component type.
858 CoreType(CoreType<'a>),
859 /// A type definition local to the component type.
860 Type(Type<'a>),
861 /// An alias local to the component type.
862 Alias(Alias<'a>),
863 /// An import of the component type.
864 Import(ComponentImport<'a>),
865 /// An export of the component type.
866 Export(ComponentExportType<'a>),
867 }
868
869 impl<'a> Parse<'a> for ComponentTypeDecl<'a> {
870 fn parse(parser: Parser<'a>) -> Result<Self> {
871 let mut l = parser.lookahead1();
872 if l.peek::<kw::core>()? {
873 Ok(Self::CoreType(parser.parse()?))
874 } else if l.peek::<kw::r#type>()? {
875 Ok(Self::Type(Type::parse_no_inline_exports(parser)?))
876 } else if l.peek::<kw::alias>()? {
877 Ok(Self::Alias(parser.parse()?))
878 } else if l.peek::<kw::import>()? {
879 Ok(Self::Import(parser.parse()?))
880 } else if l.peek::<kw::export>()? {
881 Ok(Self::Export(parser.parse()?))
882 } else {
883 Err(l.error())
884 }
885 }
886 }
887
888 impl<'a> Parse<'a> for Vec<ComponentTypeDecl<'a>> {
889 fn parse(parser: Parser<'a>) -> Result<Self> {
890 let mut decls = Vec::new();
891 while !parser.is_empty() {
892 decls.push(parser.parens(|parser| parser.parse())?);
893 }
894 Ok(decls)
895 }
896 }
897
898 /// A type definition for an instance type.
899 #[derive(Debug)]
900 pub struct InstanceType<'a> {
901 /// The declarations of the instance type.
902 pub decls: Vec<InstanceTypeDecl<'a>>,
903 }
904
905 impl<'a> Parse<'a> for InstanceType<'a> {
906 fn parse(parser: Parser<'a>) -> Result<Self> {
907 parser.depth_check()?;
908 Ok(Self {
909 decls: parser.parse()?,
910 })
911 }
912 }
913
914 /// A declaration of an instance type.
915 #[derive(Debug)]
916 pub enum InstanceTypeDecl<'a> {
917 /// A core type definition local to the component type.
918 CoreType(CoreType<'a>),
919 /// A type definition local to the instance type.
920 Type(Type<'a>),
921 /// An alias local to the instance type.
922 Alias(Alias<'a>),
923 /// An export of the instance type.
924 Export(ComponentExportType<'a>),
925 }
926
927 impl<'a> Parse<'a> for InstanceTypeDecl<'a> {
928 fn parse(parser: Parser<'a>) -> Result<Self> {
929 let mut l = parser.lookahead1();
930 if l.peek::<kw::core>()? {
931 Ok(Self::CoreType(parser.parse()?))
932 } else if l.peek::<kw::r#type>()? {
933 Ok(Self::Type(Type::parse_no_inline_exports(parser)?))
934 } else if l.peek::<kw::alias>()? {
935 Ok(Self::Alias(parser.parse()?))
936 } else if l.peek::<kw::export>()? {
937 Ok(Self::Export(parser.parse()?))
938 } else {
939 Err(l.error())
940 }
941 }
942 }
943
944 impl<'a> Parse<'a> for Vec<InstanceTypeDecl<'a>> {
945 fn parse(parser: Parser<'a>) -> Result<Self> {
946 let mut decls = Vec::new();
947 while !parser.is_empty() {
948 decls.push(parser.parens(|parser| parser.parse())?);
949 }
950 Ok(decls)
951 }
952 }
953
954 /// A type definition for an instance type.
955 #[derive(Debug)]
956 pub struct ResourceType<'a> {
957 /// Representation, in core WebAssembly, of this resource.
958 pub rep: core::ValType<'a>,
959 /// The declarations of the instance type.
960 pub dtor: Option<CoreItemRef<'a, kw::func>>,
961 }
962
963 impl<'a> Parse<'a> for ResourceType<'a> {
964 fn parse(parser: Parser<'a>) -> Result<Self> {
965 let rep = parser.parens(|p| {
966 p.parse::<kw::rep>()?;
967 p.parse()
968 })?;
969 let dtor = if parser.is_empty() {
970 None
971 } else {
972 Some(parser.parens(|p| {
973 p.parse::<kw::dtor>()?;
974 p.parens(|p| p.parse())
975 })?)
976 };
977 Ok(Self { rep, dtor })
978 }
979 }
980
981 /// A value type declaration used for values in import signatures.
982 #[derive(Debug)]
983 pub struct ComponentValTypeUse<'a>(pub ComponentValType<'a>);
984
985 impl<'a> Parse<'a> for ComponentValTypeUse<'a> {
986 fn parse(parser: Parser<'a>) -> Result<Self> {
987 match ComponentTypeUse::<'a, InlineComponentValType<'a>>::parse(parser)? {
988 ComponentTypeUse::Ref(i) => Ok(Self(ComponentValType::Ref(i.idx))),
989 ComponentTypeUse::Inline(t) => Ok(Self(ComponentValType::Inline(t.0))),
990 }
991 }
992 }
993
994 /// A reference to a core type defined in this component.
995 ///
996 /// This is the same as `TypeUse`, but accepts `$T` as shorthand for
997 /// `(type $T)`.
998 #[derive(Debug, Clone)]
999 pub enum CoreTypeUse<'a, T> {
1000 /// The type that we're referencing.
1001 Ref(CoreItemRef<'a, kw::r#type>),
1002 /// The inline type.
1003 Inline(T),
1004 }
1005
1006 impl<'a, T: Parse<'a>> Parse<'a> for CoreTypeUse<'a, T> {
1007 fn parse(parser: Parser<'a>) -> Result<Self> {
1008 // Here the core context is assumed, so no core prefix is expected
1009 if parser.peek::<LParen>()? && parser.peek2::<CoreItemRef<'a, kw::r#type>>()? {
1010 Ok(Self::Ref(parser.parens(|parser| parser.parse())?))
1011 } else {
1012 Ok(Self::Inline(parser.parse()?))
1013 }
1014 }
1015 }
1016
1017 impl<T> Default for CoreTypeUse<'_, T> {
1018 fn default() -> Self {
1019 let span = Span::from_offset(0);
1020 Self::Ref(CoreItemRef {
1021 idx: Index::Num(0, span),
1022 kind: kw::r#type(span),
1023 export_name: None,
1024 })
1025 }
1026 }
1027
1028 /// A reference to a type defined in this component.
1029 ///
1030 /// This is the same as `TypeUse`, but accepts `$T` as shorthand for
1031 /// `(type $T)`.
1032 #[derive(Debug, Clone)]
1033 pub enum ComponentTypeUse<'a, T> {
1034 /// The type that we're referencing.
1035 Ref(ItemRef<'a, kw::r#type>),
1036 /// The inline type.
1037 Inline(T),
1038 }
1039
1040 impl<'a, T: Parse<'a>> Parse<'a> for ComponentTypeUse<'a, T> {
1041 fn parse(parser: Parser<'a>) -> Result<Self> {
1042 if parser.peek::<LParen>()? && parser.peek2::<ItemRef<'a, kw::r#type>>()? {
1043 Ok(Self::Ref(parser.parens(|parser| parser.parse())?))
1044 } else {
1045 Ok(Self::Inline(parser.parse()?))
1046 }
1047 }
1048 }
1049
1050 impl<T> Default for ComponentTypeUse<'_, T> {
1051 fn default() -> Self {
1052 let span = Span::from_offset(0);
1053 Self::Ref(ItemRef {
1054 idx: Index::Num(0, span),
1055 kind: kw::r#type(span),
1056 export_names: Vec::new(),
1057 })
1058 }
1059 }