1use crate::component::*;
2use crate::kw;
3use crate::parser::{Cursor, Lookahead1, Parse, Parser, Peek, Result};
4use crate::token::{Id, Index, LParen, NameAnnotation, Span};
5
6#[derive(Debug)]
10pub struct CoreFunc<'a> {
11 pub span: Span,
13 pub id: Option<Id<'a>>,
16 pub name: Option<NameAnnotation<'a>>,
18 pub kind: CoreFuncKind<'a>,
20}
21
22impl<'a> Parse<'a> for CoreFunc<'a> {
23 fn parse(parser: Parser<'a>) -> Result<Self> {
24 let span = parser.parse::<kw::core>()?.0;
25 parser.parse::<kw::func>()?;
26 let id = parser.parse()?;
27 let name = parser.parse()?;
28 let kind = parser.parens(|p| p.parse())?;
29
30 Ok(Self {
31 span,
32 id,
33 name,
34 kind,
35 })
36 }
37}
38
39#[derive(Debug)]
41#[allow(missing_docs)]
42pub enum CoreFuncKind<'a> {
43 Lower(CanonLower<'a>),
47 Alias(InlineExportAlias<'a, true>),
51 ResourceNew(CanonResourceNew<'a>),
52 ResourceDrop(CanonResourceDrop<'a>),
53 ResourceRep(CanonResourceRep<'a>),
54 ThreadSpawnRef(CanonThreadSpawnRef<'a>),
55 ThreadSpawnIndirect(CanonThreadSpawnIndirect<'a>),
56 ThreadAvailableParallelism(CanonThreadAvailableParallelism),
57 BackpressureInc,
58 BackpressureDec,
59 TaskReturn(CanonTaskReturn<'a>),
60 TaskCancel,
61 ContextGet(crate::core::ValType<'a>, u32),
62 ContextSet(crate::core::ValType<'a>, u32),
63 SubtaskDrop,
64 SubtaskCancel(CanonSubtaskCancel),
65 StreamNew(CanonStreamNew<'a>),
66 StreamRead(CanonStreamRead<'a>),
67 StreamWrite(CanonStreamWrite<'a>),
68 StreamCancelRead(CanonStreamCancelRead<'a>),
69 StreamCancelWrite(CanonStreamCancelWrite<'a>),
70 StreamDropReadable(CanonStreamDropReadable<'a>),
71 StreamDropWritable(CanonStreamDropWritable<'a>),
72 FutureNew(CanonFutureNew<'a>),
73 FutureRead(CanonFutureRead<'a>),
74 FutureWrite(CanonFutureWrite<'a>),
75 FutureCancelRead(CanonFutureCancelRead<'a>),
76 FutureCancelWrite(CanonFutureCancelWrite<'a>),
77 FutureDropReadable(CanonFutureDropReadable<'a>),
78 FutureDropWritable(CanonFutureDropWritable<'a>),
79 ErrorContextNew(CanonErrorContextNew<'a>),
80 ErrorContextDebugMessage(CanonErrorContextDebugMessage<'a>),
81 ErrorContextDrop,
82 WaitableSetNew,
83 WaitableSetWait(CanonWaitableSetWait<'a>),
84 WaitableSetPoll(CanonWaitableSetPoll<'a>),
85 WaitableSetDrop,
86 WaitableJoin,
87 ThreadIndex,
88 ThreadNewIndirect(CanonThreadNewIndirect<'a>),
89 ThreadResumeLater,
90 ThreadSuspend(CanonThreadSuspend),
91 ThreadYield(CanonThreadYield),
92 ThreadSuspendThenResume(CanonThreadSuspendThenResume),
93 ThreadYieldThenResume(CanonThreadYieldThenResume),
94 ThreadSuspendThenPromote(CanonThreadSuspendThenPromote),
95 ThreadYieldThenPromote(CanonThreadYieldThenPromote),
96}
97
98impl<'a> Parse<'a> for CoreFuncKind<'a> {
99 fn parse(parser: Parser<'a>) -> Result<Self> {
100 let mut l = parser.lookahead1();
101 if l.peek::<kw::canon>()? {
102 parser.parse::<kw::canon>()?;
103 } else if l.peek::<kw::alias>()? {
104 return Ok(Self::Alias(parser.parse()?));
105 } else {
106 return Err(l.error());
107 }
108
109 CoreFuncKind::parse_lookahead(parser.lookahead1())
110 }
111}
112
113impl<'a> CoreFuncKind<'a> {
114 fn parse_lookahead(mut l: Lookahead1<'a>) -> Result<CoreFuncKind<'a>> {
115 let parser = l.parser();
116 if l.peek::<kw::lower>()? {
117 Ok(CoreFuncKind::Lower(parser.parse()?))
118 } else if l.peek::<kw::resource_new>()? {
119 Ok(CoreFuncKind::ResourceNew(parser.parse()?))
120 } else if l.peek::<kw::resource_drop>()? {
121 Ok(CoreFuncKind::ResourceDrop(parser.parse()?))
122 } else if l.peek::<kw::resource_rep>()? {
123 Ok(CoreFuncKind::ResourceRep(parser.parse()?))
124 } else if l.peek::<kw::thread_spawn_ref>()? {
125 Ok(CoreFuncKind::ThreadSpawnRef(parser.parse()?))
126 } else if l.peek::<kw::thread_spawn_indirect>()? {
127 Ok(CoreFuncKind::ThreadSpawnIndirect(parser.parse()?))
128 } else if l.peek::<kw::thread_available_parallelism>()? {
129 Ok(CoreFuncKind::ThreadAvailableParallelism(parser.parse()?))
130 } else if l.peek::<kw::backpressure_inc>()? {
131 parser.parse::<kw::backpressure_inc>()?;
132 Ok(CoreFuncKind::BackpressureInc)
133 } else if l.peek::<kw::backpressure_dec>()? {
134 parser.parse::<kw::backpressure_dec>()?;
135 Ok(CoreFuncKind::BackpressureDec)
136 } else if l.peek::<kw::task_return>()? {
137 Ok(CoreFuncKind::TaskReturn(parser.parse()?))
138 } else if l.peek::<kw::task_cancel>()? {
139 parser.parse::<kw::task_cancel>()?;
140 Ok(CoreFuncKind::TaskCancel)
141 } else if l.peek::<kw::context_get>()? {
142 parser.parse::<kw::context_get>()?;
143 let ty = parser.parse()?;
144 Ok(CoreFuncKind::ContextGet(ty, parser.parse()?))
145 } else if l.peek::<kw::context_set>()? {
146 parser.parse::<kw::context_set>()?;
147 let ty = parser.parse()?;
148 Ok(CoreFuncKind::ContextSet(ty, parser.parse()?))
149 } else if l.peek::<kw::subtask_drop>()? {
150 parser.parse::<kw::subtask_drop>()?;
151 Ok(CoreFuncKind::SubtaskDrop)
152 } else if l.peek::<kw::subtask_cancel>()? {
153 Ok(CoreFuncKind::SubtaskCancel(parser.parse()?))
154 } else if l.peek::<kw::stream_new>()? {
155 Ok(CoreFuncKind::StreamNew(parser.parse()?))
156 } else if l.peek::<kw::stream_read>()? {
157 Ok(CoreFuncKind::StreamRead(parser.parse()?))
158 } else if l.peek::<kw::stream_write>()? {
159 Ok(CoreFuncKind::StreamWrite(parser.parse()?))
160 } else if l.peek::<kw::stream_cancel_read>()? {
161 Ok(CoreFuncKind::StreamCancelRead(parser.parse()?))
162 } else if l.peek::<kw::stream_cancel_write>()? {
163 Ok(CoreFuncKind::StreamCancelWrite(parser.parse()?))
164 } else if l.peek::<kw::stream_drop_readable>()? {
165 Ok(CoreFuncKind::StreamDropReadable(parser.parse()?))
166 } else if l.peek::<kw::stream_drop_writable>()? {
167 Ok(CoreFuncKind::StreamDropWritable(parser.parse()?))
168 } else if l.peek::<kw::future_new>()? {
169 Ok(CoreFuncKind::FutureNew(parser.parse()?))
170 } else if l.peek::<kw::future_read>()? {
171 Ok(CoreFuncKind::FutureRead(parser.parse()?))
172 } else if l.peek::<kw::future_write>()? {
173 Ok(CoreFuncKind::FutureWrite(parser.parse()?))
174 } else if l.peek::<kw::future_cancel_read>()? {
175 Ok(CoreFuncKind::FutureCancelRead(parser.parse()?))
176 } else if l.peek::<kw::future_cancel_write>()? {
177 Ok(CoreFuncKind::FutureCancelWrite(parser.parse()?))
178 } else if l.peek::<kw::future_drop_readable>()? {
179 Ok(CoreFuncKind::FutureDropReadable(parser.parse()?))
180 } else if l.peek::<kw::future_drop_writable>()? {
181 Ok(CoreFuncKind::FutureDropWritable(parser.parse()?))
182 } else if l.peek::<kw::error_context_new>()? {
183 Ok(CoreFuncKind::ErrorContextNew(parser.parse()?))
184 } else if l.peek::<kw::error_context_debug_message>()? {
185 Ok(CoreFuncKind::ErrorContextDebugMessage(parser.parse()?))
186 } else if l.peek::<kw::error_context_drop>()? {
187 parser.parse::<kw::error_context_drop>()?;
188 Ok(CoreFuncKind::ErrorContextDrop)
189 } else if l.peek::<kw::waitable_set_new>()? {
190 parser.parse::<kw::waitable_set_new>()?;
191 Ok(CoreFuncKind::WaitableSetNew)
192 } else if l.peek::<kw::waitable_set_wait>()? {
193 Ok(CoreFuncKind::WaitableSetWait(parser.parse()?))
194 } else if l.peek::<kw::waitable_set_poll>()? {
195 Ok(CoreFuncKind::WaitableSetPoll(parser.parse()?))
196 } else if l.peek::<kw::waitable_set_drop>()? {
197 parser.parse::<kw::waitable_set_drop>()?;
198 Ok(CoreFuncKind::WaitableSetDrop)
199 } else if l.peek::<kw::waitable_join>()? {
200 parser.parse::<kw::waitable_join>()?;
201 Ok(CoreFuncKind::WaitableJoin)
202 } else if l.peek::<kw::thread_index>()? {
203 parser.parse::<kw::thread_index>()?;
204 Ok(CoreFuncKind::ThreadIndex)
205 } else if l.peek::<kw::thread_new_indirect>()? {
206 Ok(CoreFuncKind::ThreadNewIndirect(parser.parse()?))
207 } else if l.peek::<kw::thread_resume_later>()? {
208 parser.parse::<kw::thread_resume_later>()?;
209 Ok(CoreFuncKind::ThreadResumeLater)
210 } else if l.peek::<kw::thread_suspend>()? {
211 Ok(CoreFuncKind::ThreadSuspend(parser.parse()?))
212 } else if l.peek::<kw::thread_yield>()? {
213 Ok(CoreFuncKind::ThreadYield(parser.parse()?))
214 } else if l.peek::<kw::thread_suspend_then_resume>()? {
215 Ok(CoreFuncKind::ThreadSuspendThenResume(parser.parse()?))
216 } else if l.peek::<kw::thread_yield_then_resume>()? {
217 Ok(CoreFuncKind::ThreadYieldThenResume(parser.parse()?))
218 } else if l.peek::<kw::thread_suspend_then_promote>()? {
219 Ok(CoreFuncKind::ThreadSuspendThenPromote(parser.parse()?))
220 } else if l.peek::<kw::thread_yield_then_promote>()? {
221 Ok(CoreFuncKind::ThreadYieldThenPromote(parser.parse()?))
222 } else {
223 Err(l.error())
224 }
225 }
226}
227
228#[derive(Debug)]
232pub struct Func<'a> {
233 pub span: Span,
235 pub id: Option<Id<'a>>,
238 pub name: Option<NameAnnotation<'a>>,
240 pub exports: InlineExport<'a>,
243 pub kind: FuncKind<'a>,
245}
246
247impl<'a> Parse<'a> for Func<'a> {
248 fn parse(parser: Parser<'a>) -> Result<Self> {
249 let span = parser.parse::<kw::func>()?.0;
250 let id = parser.parse()?;
251 let name = parser.parse()?;
252 let exports = parser.parse()?;
253 let kind = parser.parse()?;
254
255 Ok(Self {
256 span,
257 id,
258 name,
259 exports,
260 kind,
261 })
262 }
263}
264
265#[derive(Debug)]
267pub enum FuncKind<'a> {
268 Import {
274 import: InlineImport<'a>,
276 ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>,
278 },
279 Lift {
283 ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>,
285 info: CanonLift<'a>,
287 },
288 Alias(InlineExportAlias<'a, false>),
292}
293
294impl<'a> Parse<'a> for FuncKind<'a> {
295 fn parse(parser: Parser<'a>) -> Result<Self> {
296 if let Some(import) = parser.parse()? {
297 Ok(Self::Import {
298 import,
299 ty: parser.parse()?,
300 })
301 } else if parser.peek::<LParen>()? && parser.peek2::<kw::alias>()? {
302 parser.parens(|parser| Ok(Self::Alias(parser.parse()?)))
303 } else {
304 Ok(Self::Lift {
305 ty: parser.parse()?,
306 info: parser.parens(|parser| {
307 parser.parse::<kw::canon>()?;
308 parser.parse()
309 })?,
310 })
311 }
312 }
313}
314
315#[derive(Debug)]
319pub struct CanonicalFunc<'a> {
320 pub span: Span,
322 pub id: Option<Id<'a>>,
325 pub name: Option<NameAnnotation<'a>>,
327 pub kind: CanonicalFuncKind<'a>,
329}
330
331impl<'a> Parse<'a> for CanonicalFunc<'a> {
332 fn parse(parser: Parser<'a>) -> Result<Self> {
333 let span = parser.parse::<kw::canon>()?.0;
334 let mut l = parser.lookahead1();
335
336 if l.peek::<kw::lift>()? {
337 let info = parser.parse()?;
338 let (id, name, ty) = parser.parens(|parser| {
339 parser.parse::<kw::func>()?;
340 let id = parser.parse()?;
341 let name = parser.parse()?;
342 let ty = parser.parse()?;
343 Ok((id, name, ty))
344 })?;
345
346 Ok(Self {
347 span,
348 id,
349 name,
350 kind: CanonicalFuncKind::Lift { info, ty },
351 })
352 } else {
353 let kind = CoreFuncKind::parse_lookahead(l)?;
354 let (id, name) = parser.parens(|parser| {
355 parser.parse::<kw::core>()?;
356 parser.parse::<kw::func>()?;
357 let id = parser.parse()?;
358 let name = parser.parse()?;
359 Ok((id, name))
360 })?;
361
362 Ok(Self {
363 span,
364 id,
365 name,
366 kind: CanonicalFuncKind::Core(kind),
367 })
368 }
369 }
370}
371
372#[derive(Debug)]
374#[allow(missing_docs)]
375pub enum CanonicalFuncKind<'a> {
376 Lift {
378 ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>,
380 info: CanonLift<'a>,
382 },
383
384 Core(CoreFuncKind<'a>),
387}
388
389#[derive(Debug)]
391pub struct CanonLift<'a> {
392 pub func: CoreItemRef<'a, kw::func>,
394 pub opts: Vec<CanonOpt<'a>>,
396}
397
398impl<'a> Parse<'a> for CanonLift<'a> {
399 fn parse(parser: Parser<'a>) -> Result<Self> {
400 parser.parse::<kw::lift>()?;
401
402 Ok(Self {
403 func: parser.parens(|parser| {
404 parser.parse::<kw::core>()?;
405 parser.parse()
406 })?,
407 opts: parser.parse()?,
408 })
409 }
410}
411
412impl Default for CanonLift<'_> {
413 fn default() -> Self {
414 let span = Span::from_offset(0);
415 Self {
416 func: CoreItemRef {
417 kind: kw::func(span),
418 idx: Index::Num(0, span),
419 export_name: None,
420 },
421 opts: Vec::new(),
422 }
423 }
424}
425
426#[derive(Debug)]
428pub struct CanonLower<'a> {
429 pub func: ItemRef<'a, kw::func>,
431 pub opts: Vec<CanonOpt<'a>>,
433}
434
435impl<'a> Parse<'a> for CanonLower<'a> {
436 fn parse(parser: Parser<'a>) -> Result<Self> {
437 parser.parse::<kw::lower>()?;
438
439 Ok(Self {
440 func: parser.parens(|parser| parser.parse())?,
441 opts: parser.parse()?,
442 })
443 }
444}
445
446impl Default for CanonLower<'_> {
447 fn default() -> Self {
448 let span = Span::from_offset(0);
449 Self {
450 func: ItemRef {
451 kind: kw::func(span),
452 idx: Index::Num(0, span),
453 export_names: Vec::new(),
454 },
455 opts: Vec::new(),
456 }
457 }
458}
459
460#[derive(Debug)]
462pub struct CanonResourceNew<'a> {
463 pub ty: Index<'a>,
465}
466
467impl<'a> Parse<'a> for CanonResourceNew<'a> {
468 fn parse(parser: Parser<'a>) -> Result<Self> {
469 parser.parse::<kw::resource_new>()?;
470
471 Ok(Self {
472 ty: parser.parse()?,
473 })
474 }
475}
476
477#[derive(Debug)]
479pub struct CanonResourceDrop<'a> {
480 pub ty: Index<'a>,
482}
483
484impl<'a> Parse<'a> for CanonResourceDrop<'a> {
485 fn parse(parser: Parser<'a>) -> Result<Self> {
486 parser.parse::<kw::resource_drop>()?;
487
488 Ok(Self {
489 ty: parser.parse()?,
490 })
491 }
492}
493
494#[derive(Debug)]
496pub struct CanonResourceRep<'a> {
497 pub ty: Index<'a>,
499}
500
501impl<'a> Parse<'a> for CanonResourceRep<'a> {
502 fn parse(parser: Parser<'a>) -> Result<Self> {
503 parser.parse::<kw::resource_rep>()?;
504
505 Ok(Self {
506 ty: parser.parse()?,
507 })
508 }
509}
510
511#[derive(Debug)]
513pub struct CanonThreadSpawnRef<'a> {
514 pub ty: Index<'a>,
516}
517
518impl<'a> Parse<'a> for CanonThreadSpawnRef<'a> {
519 fn parse(parser: Parser<'a>) -> Result<Self> {
520 parser.parse::<kw::thread_spawn_ref>()?;
521
522 Ok(Self {
523 ty: parser.parse()?,
524 })
525 }
526}
527
528#[derive(Debug)]
532pub struct CanonThreadSpawnIndirect<'a> {
533 pub ty: Index<'a>,
535 pub table: CoreItemRef<'a, kw::table>,
537}
538
539impl<'a> Parse<'a> for CanonThreadSpawnIndirect<'a> {
540 fn parse(parser: Parser<'a>) -> Result<Self> {
541 parser.parse::<kw::thread_spawn_indirect>()?;
542 let ty = parser.parse()?;
543 let table = parser.parens(|p| p.parse())?;
544 Ok(Self { ty, table })
545 }
546}
547
548#[derive(Debug)]
550pub struct CanonThreadAvailableParallelism;
551
552impl<'a> Parse<'a> for CanonThreadAvailableParallelism {
553 fn parse(parser: Parser<'a>) -> Result<Self> {
554 parser.parse::<kw::thread_available_parallelism>()?;
555 Ok(Self)
556 }
557}
558
559#[derive(Debug)]
561pub struct CanonTaskReturn<'a> {
562 pub result: Option<ComponentValType<'a>>,
564 pub opts: Vec<CanonOpt<'a>>,
566}
567
568impl<'a> Parse<'a> for CanonTaskReturn<'a> {
569 fn parse(parser: Parser<'a>) -> Result<Self> {
570 parser.parse::<kw::task_return>()?;
571
572 Ok(Self {
573 result: if parser.peek2::<kw::result>()? {
574 Some(parser.parens(|p| {
575 p.parse::<kw::result>()?.0;
576 p.parse()
577 })?)
578 } else {
579 None
580 },
581 opts: parser.parse()?,
582 })
583 }
584}
585
586#[derive(Debug)]
588pub struct CanonWaitableSetWait<'a> {
589 pub async_: bool,
592 pub memory: CoreItemRef<'a, kw::memory>,
594}
595
596impl<'a> Parse<'a> for CanonWaitableSetWait<'a> {
597 fn parse(parser: Parser<'a>) -> Result<Self> {
598 parser.parse::<kw::waitable_set_wait>()?;
599 let async_ = parser.parse::<Option<kw::cancellable>>()?.is_some();
600 let memory = parser.parens(|p| p.parse())?;
601
602 Ok(Self { async_, memory })
603 }
604}
605
606#[derive(Debug)]
608pub struct CanonWaitableSetPoll<'a> {
609 pub async_: bool,
612 pub memory: CoreItemRef<'a, kw::memory>,
614}
615
616impl<'a> Parse<'a> for CanonWaitableSetPoll<'a> {
617 fn parse(parser: Parser<'a>) -> Result<Self> {
618 parser.parse::<kw::waitable_set_poll>()?;
619 let async_ = parser.parse::<Option<kw::cancellable>>()?.is_some();
620 let memory = parser.parens(|p| p.parse())?;
621
622 Ok(Self { async_, memory })
623 }
624}
625
626#[derive(Debug)]
628pub struct CanonThreadYield {
629 pub cancellable: bool,
632}
633
634impl<'a> Parse<'a> for CanonThreadYield {
635 fn parse(parser: Parser<'a>) -> Result<Self> {
636 parser.parse::<kw::thread_yield>()?;
637 let cancellable = parser.parse::<Option<kw::cancellable>>()?.is_some();
638
639 Ok(Self { cancellable })
640 }
641}
642
643#[derive(Debug)]
645pub struct CanonSubtaskCancel {
646 pub async_: bool,
649}
650
651impl<'a> Parse<'a> for CanonSubtaskCancel {
652 fn parse(parser: Parser<'a>) -> Result<Self> {
653 parser.parse::<kw::subtask_cancel>()?;
654 let async_ = parser.parse::<Option<kw::r#async>>()?.is_some();
655
656 Ok(Self { async_ })
657 }
658}
659
660#[derive(Debug)]
662pub struct CanonStreamNew<'a> {
663 pub ty: Index<'a>,
665}
666
667impl<'a> Parse<'a> for CanonStreamNew<'a> {
668 fn parse(parser: Parser<'a>) -> Result<Self> {
669 parser.parse::<kw::stream_new>()?;
670
671 Ok(Self {
672 ty: parser.parse()?,
673 })
674 }
675}
676
677#[derive(Debug)]
679pub struct CanonStreamRead<'a> {
680 pub ty: Index<'a>,
682 pub opts: Vec<CanonOpt<'a>>,
684}
685
686impl<'a> Parse<'a> for CanonStreamRead<'a> {
687 fn parse(parser: Parser<'a>) -> Result<Self> {
688 parser.parse::<kw::stream_read>()?;
689
690 Ok(Self {
691 ty: parser.parse()?,
692 opts: parser.parse()?,
693 })
694 }
695}
696
697#[derive(Debug)]
699pub struct CanonStreamWrite<'a> {
700 pub ty: Index<'a>,
702 pub opts: Vec<CanonOpt<'a>>,
704}
705
706impl<'a> Parse<'a> for CanonStreamWrite<'a> {
707 fn parse(parser: Parser<'a>) -> Result<Self> {
708 parser.parse::<kw::stream_write>()?;
709
710 Ok(Self {
711 ty: parser.parse()?,
712 opts: parser.parse()?,
713 })
714 }
715}
716
717#[derive(Debug)]
719pub struct CanonStreamCancelRead<'a> {
720 pub ty: Index<'a>,
722 pub async_: bool,
725}
726
727impl<'a> Parse<'a> for CanonStreamCancelRead<'a> {
728 fn parse(parser: Parser<'a>) -> Result<Self> {
729 parser.parse::<kw::stream_cancel_read>()?;
730
731 Ok(Self {
732 ty: parser.parse()?,
733 async_: parser.parse::<Option<kw::r#async>>()?.is_some(),
734 })
735 }
736}
737
738#[derive(Debug)]
740pub struct CanonStreamCancelWrite<'a> {
741 pub ty: Index<'a>,
743 pub async_: bool,
746}
747
748impl<'a> Parse<'a> for CanonStreamCancelWrite<'a> {
749 fn parse(parser: Parser<'a>) -> Result<Self> {
750 parser.parse::<kw::stream_cancel_write>()?;
751
752 Ok(Self {
753 ty: parser.parse()?,
754 async_: parser.parse::<Option<kw::r#async>>()?.is_some(),
755 })
756 }
757}
758
759#[derive(Debug)]
761pub struct CanonStreamDropReadable<'a> {
762 pub ty: Index<'a>,
764}
765
766impl<'a> Parse<'a> for CanonStreamDropReadable<'a> {
767 fn parse(parser: Parser<'a>) -> Result<Self> {
768 parser.parse::<kw::stream_drop_readable>()?;
769
770 Ok(Self {
771 ty: parser.parse()?,
772 })
773 }
774}
775
776#[derive(Debug)]
778pub struct CanonStreamDropWritable<'a> {
779 pub ty: Index<'a>,
781}
782
783impl<'a> Parse<'a> for CanonStreamDropWritable<'a> {
784 fn parse(parser: Parser<'a>) -> Result<Self> {
785 parser.parse::<kw::stream_drop_writable>()?;
786
787 Ok(Self {
788 ty: parser.parse()?,
789 })
790 }
791}
792
793#[derive(Debug)]
795pub struct CanonFutureNew<'a> {
796 pub ty: Index<'a>,
798}
799
800impl<'a> Parse<'a> for CanonFutureNew<'a> {
801 fn parse(parser: Parser<'a>) -> Result<Self> {
802 parser.parse::<kw::future_new>()?;
803
804 Ok(Self {
805 ty: parser.parse()?,
806 })
807 }
808}
809
810#[derive(Debug)]
812pub struct CanonFutureRead<'a> {
813 pub ty: Index<'a>,
815 pub opts: Vec<CanonOpt<'a>>,
817}
818
819impl<'a> Parse<'a> for CanonFutureRead<'a> {
820 fn parse(parser: Parser<'a>) -> Result<Self> {
821 parser.parse::<kw::future_read>()?;
822
823 Ok(Self {
824 ty: parser.parse()?,
825 opts: parser.parse()?,
826 })
827 }
828}
829
830#[derive(Debug)]
832pub struct CanonFutureWrite<'a> {
833 pub ty: Index<'a>,
835 pub opts: Vec<CanonOpt<'a>>,
837}
838
839impl<'a> Parse<'a> for CanonFutureWrite<'a> {
840 fn parse(parser: Parser<'a>) -> Result<Self> {
841 parser.parse::<kw::future_write>()?;
842
843 Ok(Self {
844 ty: parser.parse()?,
845 opts: parser.parse()?,
846 })
847 }
848}
849
850#[derive(Debug)]
852pub struct CanonFutureCancelRead<'a> {
853 pub ty: Index<'a>,
855 pub async_: bool,
858}
859
860impl<'a> Parse<'a> for CanonFutureCancelRead<'a> {
861 fn parse(parser: Parser<'a>) -> Result<Self> {
862 parser.parse::<kw::future_cancel_read>()?;
863
864 Ok(Self {
865 ty: parser.parse()?,
866 async_: parser.parse::<Option<kw::r#async>>()?.is_some(),
867 })
868 }
869}
870
871#[derive(Debug)]
873pub struct CanonFutureCancelWrite<'a> {
874 pub ty: Index<'a>,
876 pub async_: bool,
879}
880
881impl<'a> Parse<'a> for CanonFutureCancelWrite<'a> {
882 fn parse(parser: Parser<'a>) -> Result<Self> {
883 parser.parse::<kw::future_cancel_write>()?;
884
885 Ok(Self {
886 ty: parser.parse()?,
887 async_: parser.parse::<Option<kw::r#async>>()?.is_some(),
888 })
889 }
890}
891
892#[derive(Debug)]
894pub struct CanonFutureDropReadable<'a> {
895 pub ty: Index<'a>,
897}
898
899impl<'a> Parse<'a> for CanonFutureDropReadable<'a> {
900 fn parse(parser: Parser<'a>) -> Result<Self> {
901 parser.parse::<kw::future_drop_readable>()?;
902
903 Ok(Self {
904 ty: parser.parse()?,
905 })
906 }
907}
908
909#[derive(Debug)]
911pub struct CanonFutureDropWritable<'a> {
912 pub ty: Index<'a>,
914}
915
916impl<'a> Parse<'a> for CanonFutureDropWritable<'a> {
917 fn parse(parser: Parser<'a>) -> Result<Self> {
918 parser.parse::<kw::future_drop_writable>()?;
919
920 Ok(Self {
921 ty: parser.parse()?,
922 })
923 }
924}
925
926#[derive(Debug)]
928pub struct CanonErrorContextNew<'a> {
929 pub opts: Vec<CanonOpt<'a>>,
931}
932
933impl<'a> Parse<'a> for CanonErrorContextNew<'a> {
934 fn parse(parser: Parser<'a>) -> Result<Self> {
935 parser.parse::<kw::error_context_new>()?;
936
937 Ok(Self {
938 opts: parser.parse()?,
939 })
940 }
941}
942
943#[derive(Debug)]
945pub struct CanonErrorContextDebugMessage<'a> {
946 pub opts: Vec<CanonOpt<'a>>,
948}
949
950impl<'a> Parse<'a> for CanonErrorContextDebugMessage<'a> {
951 fn parse(parser: Parser<'a>) -> Result<Self> {
952 parser.parse::<kw::error_context_debug_message>()?;
953
954 Ok(Self {
955 opts: parser.parse()?,
956 })
957 }
958}
959
960#[derive(Debug)]
962pub struct CanonThreadNewIndirect<'a> {
963 pub ty: Index<'a>,
965 pub table: CoreItemRef<'a, kw::table>,
967}
968
969impl<'a> Parse<'a> for CanonThreadNewIndirect<'a> {
970 fn parse(parser: Parser<'a>) -> Result<Self> {
971 parser.parse::<kw::thread_new_indirect>()?;
972 let ty = parser.parse()?;
973 let table = parser.parens(|p| p.parse())?;
974 Ok(Self { ty, table })
975 }
976}
977
978#[derive(Debug)]
980pub struct CanonThreadSuspend {
981 pub cancellable: bool,
983}
984impl<'a> Parse<'a> for CanonThreadSuspend {
985 fn parse(parser: Parser<'a>) -> Result<Self> {
986 parser.parse::<kw::thread_suspend>()?;
987 let cancellable = parser.parse::<Option<kw::cancellable>>()?.is_some();
988 Ok(Self { cancellable })
989 }
990}
991
992#[derive(Debug)]
994pub struct CanonThreadSuspendThenResume {
995 pub cancellable: bool,
997}
998impl<'a> Parse<'a> for CanonThreadSuspendThenResume {
999 fn parse(parser: Parser<'a>) -> Result<Self> {
1000 parser.parse::<kw::thread_suspend_then_resume>()?;
1001 let cancellable = parser.parse::<Option<kw::cancellable>>()?.is_some();
1002 Ok(Self { cancellable })
1003 }
1004}
1005
1006#[derive(Debug)]
1008pub struct CanonThreadYieldThenResume {
1009 pub cancellable: bool,
1011}
1012impl<'a> Parse<'a> for CanonThreadYieldThenResume {
1013 fn parse(parser: Parser<'a>) -> Result<Self> {
1014 parser.parse::<kw::thread_yield_then_resume>()?;
1015 let cancellable = parser.parse::<Option<kw::cancellable>>()?.is_some();
1016 Ok(Self { cancellable })
1017 }
1018}
1019
1020#[derive(Debug)]
1022pub struct CanonThreadSuspendThenPromote {
1023 pub cancellable: bool,
1025}
1026impl<'a> Parse<'a> for CanonThreadSuspendThenPromote {
1027 fn parse(parser: Parser<'a>) -> Result<Self> {
1028 parser.parse::<kw::thread_suspend_then_promote>()?;
1029 let cancellable = parser.parse::<Option<kw::cancellable>>()?.is_some();
1030 Ok(Self { cancellable })
1031 }
1032}
1033
1034#[derive(Debug)]
1036pub struct CanonThreadYieldThenPromote {
1037 pub cancellable: bool,
1039}
1040impl<'a> Parse<'a> for CanonThreadYieldThenPromote {
1041 fn parse(parser: Parser<'a>) -> Result<Self> {
1042 parser.parse::<kw::thread_yield_then_promote>()?;
1043 let cancellable = parser.parse::<Option<kw::cancellable>>()?.is_some();
1044 Ok(Self { cancellable })
1045 }
1046}
1047
1048#[derive(Debug)]
1049pub enum CanonOpt<'a> {
1051 StringUtf8,
1053 StringUtf16,
1055 StringLatin1Utf16,
1057 Memory(CoreItemRef<'a, kw::memory>),
1059 Realloc(CoreItemRef<'a, kw::func>),
1061 PostReturn(CoreItemRef<'a, kw::func>),
1063 Async,
1065 Callback(CoreItemRef<'a, kw::func>),
1067 CoreType(CoreItemRef<'a, kw::r#type>),
1069 Gc,
1071}
1072
1073impl Default for kw::r#type {
1074 fn default() -> Self {
1075 Self(Span::from_offset(0))
1076 }
1077}
1078
1079impl<'a> Parse<'a> for CanonOpt<'a> {
1080 fn parse(parser: Parser<'a>) -> Result<Self> {
1081 let mut l = parser.lookahead1();
1082 if l.peek::<kw::string_utf8>()? {
1083 parser.parse::<kw::string_utf8>()?;
1084 Ok(Self::StringUtf8)
1085 } else if l.peek::<kw::string_utf16>()? {
1086 parser.parse::<kw::string_utf16>()?;
1087 Ok(Self::StringUtf16)
1088 } else if l.peek::<kw::string_latin1_utf16>()? {
1089 parser.parse::<kw::string_latin1_utf16>()?;
1090 Ok(Self::StringLatin1Utf16)
1091 } else if l.peek::<kw::r#async>()? {
1092 parser.parse::<kw::r#async>()?;
1093 Ok(Self::Async)
1094 } else if l.peek::<kw::gc>()? {
1095 parser.parse::<kw::gc>()?;
1096 Ok(Self::Gc)
1097 } else if l.peek::<LParen>()? {
1098 parser.parens(|parser| {
1099 let mut l = parser.lookahead1();
1100 if l.peek::<kw::memory>()? {
1101 Ok(CanonOpt::Memory(parser.parse()?))
1102 } else if l.peek::<kw::realloc>()? {
1103 parser.parse::<kw::realloc>()?;
1104 Ok(CanonOpt::Realloc(
1105 parser.parse::<IndexOrCoreRef<'_, _>>()?.0,
1106 ))
1107 } else if l.peek::<kw::post_return>()? {
1108 parser.parse::<kw::post_return>()?;
1109 Ok(CanonOpt::PostReturn(
1110 parser.parse::<IndexOrCoreRef<'_, _>>()?.0,
1111 ))
1112 } else if l.peek::<kw::callback>()? {
1113 parser.parse::<kw::callback>()?;
1114 Ok(CanonOpt::Callback(
1115 parser.parse::<IndexOrCoreRef<'_, _>>()?.0,
1116 ))
1117 } else if l.peek::<kw::core_type>()? {
1118 parser.parse::<kw::core_type>()?;
1119 Ok(CanonOpt::CoreType(
1120 parser.parse::<IndexOrCoreRef<'_, _>>()?.0,
1121 ))
1122 } else {
1123 Err(l.error())
1124 }
1125 })
1126 } else {
1127 Err(l.error())
1128 }
1129 }
1130}
1131
1132impl Peek for CanonOpt<'_> {
1133 fn peek(cursor: Cursor<'_>) -> Result<bool> {
1134 Ok(kw::string_utf8::peek(cursor)?
1135 || kw::string_utf16::peek(cursor)?
1136 || kw::string_latin1_utf16::peek(cursor)?
1137 || kw::r#async::peek(cursor)?
1138 || kw::gc::peek(cursor)?
1139 || match cursor.lparen()? {
1140 Some(next) => {
1141 kw::memory::peek(next)?
1142 || kw::realloc::peek(next)?
1143 || kw::post_return::peek(next)?
1144 || kw::callback::peek(next)?
1145 || kw::core_type::peek(next)?
1146 }
1147 None => false,
1148 })
1149 }
1150
1151 fn display() -> &'static str {
1152 "canonical option"
1153 }
1154}
1155
1156impl<'a> Parse<'a> for Vec<CanonOpt<'a>> {
1157 fn parse(parser: Parser<'a>) -> Result<Self> {
1158 let mut funcs = Vec::new();
1159 while parser.peek::<CanonOpt<'_>>()? {
1160 funcs.push(parser.parse()?);
1161 }
1162 Ok(funcs)
1163 }
1164}