Debian Codegraph @

crates/wast/src/component/func.rs

raw
1 use crate::component::*;
2 use crate::kw;
3 use crate::parser::{Cursor, Lookahead1, Parse, Parser, Peek, Result};
4 use crate::token::{Id, Index, LParen, NameAnnotation, Span};
5
6 /// A declared core function.
7 ///
8 /// This is a member of both the core alias and canon sections.
9 #[derive(Debug)]
10 pub struct CoreFunc<'a> {
11 /// Where this `core func` was defined.
12 pub span: Span,
13 /// An identifier that this function is resolved with (optionally) for name
14 /// resolution.
15 pub id: Option<Id<'a>>,
16 /// An optional name for this function stored in the custom `name` section.
17 pub name: Option<NameAnnotation<'a>>,
18 /// The kind of core function.
19 pub kind: CoreFuncKind<'a>,
20 }
21
22 impl<'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 /// Represents the kind of core functions.
40 #[derive(Debug)]
41 #[allow(missing_docs)]
42 pub enum CoreFuncKind<'a> {
43 /// The core function is defined in terms of lowering a component function.
44 ///
45 /// The core function is actually a member of the canon section.
46 Lower(CanonLower<'a>),
47 /// The core function is defined in terms of aliasing a module instance export.
48 ///
49 /// The core function is actually a member of the core alias section.
50 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 BackpressureSet,
58 BackpressureInc,
59 BackpressureDec,
60 TaskReturn(CanonTaskReturn<'a>),
61 TaskCancel,
62 ContextGet(u32),
63 ContextSet(u32),
64 ThreadYield(CanonThreadYield),
65 SubtaskDrop,
66 SubtaskCancel(CanonSubtaskCancel),
67 StreamNew(CanonStreamNew<'a>),
68 StreamRead(CanonStreamRead<'a>),
69 StreamWrite(CanonStreamWrite<'a>),
70 StreamCancelRead(CanonStreamCancelRead<'a>),
71 StreamCancelWrite(CanonStreamCancelWrite<'a>),
72 StreamDropReadable(CanonStreamDropReadable<'a>),
73 StreamDropWritable(CanonStreamDropWritable<'a>),
74 FutureNew(CanonFutureNew<'a>),
75 FutureRead(CanonFutureRead<'a>),
76 FutureWrite(CanonFutureWrite<'a>),
77 FutureCancelRead(CanonFutureCancelRead<'a>),
78 FutureCancelWrite(CanonFutureCancelWrite<'a>),
79 FutureDropReadable(CanonFutureDropReadable<'a>),
80 FutureDropWritable(CanonFutureDropWritable<'a>),
81 ErrorContextNew(CanonErrorContextNew<'a>),
82 ErrorContextDebugMessage(CanonErrorContextDebugMessage<'a>),
83 ErrorContextDrop,
84 WaitableSetNew,
85 WaitableSetWait(CanonWaitableSetWait<'a>),
86 WaitableSetPoll(CanonWaitableSetPoll<'a>),
87 WaitableSetDrop,
88 WaitableJoin,
89 ThreadIndex,
90 ThreadNewIndirect(CanonThreadNewIndirect<'a>),
91 ThreadSwitchTo(CanonThreadSwitchTo),
92 ThreadSuspend(CanonThreadSuspend),
93 ThreadResumeLater,
94 ThreadYieldTo(CanonThreadYieldTo),
95 }
96
97 impl<'a> Parse<'a> for CoreFuncKind<'a> {
98 fn parse(parser: Parser<'a>) -> Result<Self> {
99 let mut l = parser.lookahead1();
100 if l.peek::<kw::canon>()? {
101 parser.parse::<kw::canon>()?;
102 } else if l.peek::<kw::alias>()? {
103 return Ok(Self::Alias(parser.parse()?));
104 } else {
105 return Err(l.error());
106 }
107
108 CoreFuncKind::parse_lookahead(parser.lookahead1())
109 }
110 }
111
112 impl<'a> CoreFuncKind<'a> {
113 fn parse_lookahead(mut l: Lookahead1<'a>) -> Result<CoreFuncKind<'a>> {
114 let parser = l.parser();
115 if l.peek::<kw::lower>()? {
116 Ok(CoreFuncKind::Lower(parser.parse()?))
117 } else if l.peek::<kw::resource_new>()? {
118 Ok(CoreFuncKind::ResourceNew(parser.parse()?))
119 } else if l.peek::<kw::resource_drop>()? {
120 Ok(CoreFuncKind::ResourceDrop(parser.parse()?))
121 } else if l.peek::<kw::resource_rep>()? {
122 Ok(CoreFuncKind::ResourceRep(parser.parse()?))
123 } else if l.peek::<kw::thread_spawn_ref>()? {
124 Ok(CoreFuncKind::ThreadSpawnRef(parser.parse()?))
125 } else if l.peek::<kw::thread_spawn_indirect>()? {
126 Ok(CoreFuncKind::ThreadSpawnIndirect(parser.parse()?))
127 } else if l.peek::<kw::thread_available_parallelism>()? {
128 Ok(CoreFuncKind::ThreadAvailableParallelism(parser.parse()?))
129 } else if l.peek::<kw::backpressure_set>()? {
130 parser.parse::<kw::backpressure_set>()?;
131 Ok(CoreFuncKind::BackpressureSet)
132 } else if l.peek::<kw::backpressure_inc>()? {
133 parser.parse::<kw::backpressure_inc>()?;
134 Ok(CoreFuncKind::BackpressureInc)
135 } else if l.peek::<kw::backpressure_dec>()? {
136 parser.parse::<kw::backpressure_dec>()?;
137 Ok(CoreFuncKind::BackpressureDec)
138 } else if l.peek::<kw::task_return>()? {
139 Ok(CoreFuncKind::TaskReturn(parser.parse()?))
140 } else if l.peek::<kw::task_cancel>()? {
141 parser.parse::<kw::task_cancel>()?;
142 Ok(CoreFuncKind::TaskCancel)
143 } else if l.peek::<kw::context_get>()? {
144 parser.parse::<kw::context_get>()?;
145 parser.parse::<kw::i32>()?;
146 Ok(CoreFuncKind::ContextGet(parser.parse()?))
147 } else if l.peek::<kw::context_set>()? {
148 parser.parse::<kw::context_set>()?;
149 parser.parse::<kw::i32>()?;
150 Ok(CoreFuncKind::ContextSet(parser.parse()?))
151 } else if l.peek::<kw::thread_yield>()? {
152 Ok(CoreFuncKind::ThreadYield(parser.parse()?))
153 } else if l.peek::<kw::subtask_drop>()? {
154 parser.parse::<kw::subtask_drop>()?;
155 Ok(CoreFuncKind::SubtaskDrop)
156 } else if l.peek::<kw::subtask_cancel>()? {
157 Ok(CoreFuncKind::SubtaskCancel(parser.parse()?))
158 } else if l.peek::<kw::stream_new>()? {
159 Ok(CoreFuncKind::StreamNew(parser.parse()?))
160 } else if l.peek::<kw::stream_read>()? {
161 Ok(CoreFuncKind::StreamRead(parser.parse()?))
162 } else if l.peek::<kw::stream_write>()? {
163 Ok(CoreFuncKind::StreamWrite(parser.parse()?))
164 } else if l.peek::<kw::stream_cancel_read>()? {
165 Ok(CoreFuncKind::StreamCancelRead(parser.parse()?))
166 } else if l.peek::<kw::stream_cancel_write>()? {
167 Ok(CoreFuncKind::StreamCancelWrite(parser.parse()?))
168 } else if l.peek::<kw::stream_drop_readable>()? {
169 Ok(CoreFuncKind::StreamDropReadable(parser.parse()?))
170 } else if l.peek::<kw::stream_drop_writable>()? {
171 Ok(CoreFuncKind::StreamDropWritable(parser.parse()?))
172 } else if l.peek::<kw::future_new>()? {
173 Ok(CoreFuncKind::FutureNew(parser.parse()?))
174 } else if l.peek::<kw::future_read>()? {
175 Ok(CoreFuncKind::FutureRead(parser.parse()?))
176 } else if l.peek::<kw::future_write>()? {
177 Ok(CoreFuncKind::FutureWrite(parser.parse()?))
178 } else if l.peek::<kw::future_cancel_read>()? {
179 Ok(CoreFuncKind::FutureCancelRead(parser.parse()?))
180 } else if l.peek::<kw::future_cancel_write>()? {
181 Ok(CoreFuncKind::FutureCancelWrite(parser.parse()?))
182 } else if l.peek::<kw::future_drop_readable>()? {
183 Ok(CoreFuncKind::FutureDropReadable(parser.parse()?))
184 } else if l.peek::<kw::future_drop_writable>()? {
185 Ok(CoreFuncKind::FutureDropWritable(parser.parse()?))
186 } else if l.peek::<kw::error_context_new>()? {
187 Ok(CoreFuncKind::ErrorContextNew(parser.parse()?))
188 } else if l.peek::<kw::error_context_debug_message>()? {
189 Ok(CoreFuncKind::ErrorContextDebugMessage(parser.parse()?))
190 } else if l.peek::<kw::error_context_drop>()? {
191 parser.parse::<kw::error_context_drop>()?;
192 Ok(CoreFuncKind::ErrorContextDrop)
193 } else if l.peek::<kw::waitable_set_new>()? {
194 parser.parse::<kw::waitable_set_new>()?;
195 Ok(CoreFuncKind::WaitableSetNew)
196 } else if l.peek::<kw::waitable_set_wait>()? {
197 Ok(CoreFuncKind::WaitableSetWait(parser.parse()?))
198 } else if l.peek::<kw::waitable_set_poll>()? {
199 Ok(CoreFuncKind::WaitableSetPoll(parser.parse()?))
200 } else if l.peek::<kw::waitable_set_drop>()? {
201 parser.parse::<kw::waitable_set_drop>()?;
202 Ok(CoreFuncKind::WaitableSetDrop)
203 } else if l.peek::<kw::waitable_join>()? {
204 parser.parse::<kw::waitable_join>()?;
205 Ok(CoreFuncKind::WaitableJoin)
206 } else if l.peek::<kw::thread_index>()? {
207 parser.parse::<kw::thread_index>()?;
208 Ok(CoreFuncKind::ThreadIndex)
209 } else if l.peek::<kw::thread_new_indirect>()? {
210 Ok(CoreFuncKind::ThreadNewIndirect(parser.parse()?))
211 } else if l.peek::<kw::thread_switch_to>()? {
212 Ok(CoreFuncKind::ThreadSwitchTo(parser.parse()?))
213 } else if l.peek::<kw::thread_suspend>()? {
214 Ok(CoreFuncKind::ThreadSuspend(parser.parse()?))
215 } else if l.peek::<kw::thread_resume_later>()? {
216 parser.parse::<kw::thread_resume_later>()?;
217 Ok(CoreFuncKind::ThreadResumeLater)
218 } else if l.peek::<kw::thread_yield_to>()? {
219 Ok(CoreFuncKind::ThreadYieldTo(parser.parse()?))
220 } else {
221 Err(l.error())
222 }
223 }
224 }
225
226 /// A declared component function.
227 ///
228 /// This may be a member of the import, alias, or canon sections.
229 #[derive(Debug)]
230 pub struct Func<'a> {
231 /// Where this `func` was defined.
232 pub span: Span,
233 /// An identifier that this function is resolved with (optionally) for name
234 /// resolution.
235 pub id: Option<Id<'a>>,
236 /// An optional name for this function stored in the custom `name` section.
237 pub name: Option<NameAnnotation<'a>>,
238 /// If present, inline export annotations which indicate names this
239 /// definition should be exported under.
240 pub exports: InlineExport<'a>,
241 /// The kind of function.
242 pub kind: FuncKind<'a>,
243 }
244
245 impl<'a> Parse<'a> for Func<'a> {
246 fn parse(parser: Parser<'a>) -> Result<Self> {
247 let span = parser.parse::<kw::func>()?.0;
248 let id = parser.parse()?;
249 let name = parser.parse()?;
250 let exports = parser.parse()?;
251 let kind = parser.parse()?;
252
253 Ok(Self {
254 span,
255 id,
256 name,
257 exports,
258 kind,
259 })
260 }
261 }
262
263 /// Represents the kind of component functions.
264 #[derive(Debug)]
265 pub enum FuncKind<'a> {
266 /// A function which is actually defined as an import, such as:
267 ///
268 /// ```text
269 /// (func (import "foo") (param string))
270 /// ```
271 Import {
272 /// The import name of this import.
273 import: InlineImport<'a>,
274 /// The type that this function will have.
275 ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>,
276 },
277 /// The function is defined in terms of lifting a core function.
278 ///
279 /// The function is actually a member of the canon section.
280 Lift {
281 /// The lifted function's type.
282 ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>,
283 /// Information relating to the lifting of the core function.
284 info: CanonLift<'a>,
285 },
286 /// The function is defined in terms of aliasing a component instance export.
287 ///
288 /// The function is actually a member of the alias section.
289 Alias(InlineExportAlias<'a, false>),
290 }
291
292 impl<'a> Parse<'a> for FuncKind<'a> {
293 fn parse(parser: Parser<'a>) -> Result<Self> {
294 if let Some(import) = parser.parse()? {
295 Ok(Self::Import {
296 import,
297 ty: parser.parse()?,
298 })
299 } else if parser.peek::<LParen>()? && parser.peek2::<kw::alias>()? {
300 parser.parens(|parser| Ok(Self::Alias(parser.parse()?)))
301 } else {
302 Ok(Self::Lift {
303 ty: parser.parse()?,
304 info: parser.parens(|parser| {
305 parser.parse::<kw::canon>()?;
306 parser.parse()
307 })?,
308 })
309 }
310 }
311 }
312
313 /// A WebAssembly canonical function to be inserted into a component.
314 ///
315 /// This is a member of the canonical section.
316 #[derive(Debug)]
317 pub struct CanonicalFunc<'a> {
318 /// Where this `func` was defined.
319 pub span: Span,
320 /// An identifier that this function is resolved with (optionally) for name
321 /// resolution.
322 pub id: Option<Id<'a>>,
323 /// An optional name for this function stored in the custom `name` section.
324 pub name: Option<NameAnnotation<'a>>,
325 /// What kind of function this is, be it a lowered or lifted function.
326 pub kind: CanonicalFuncKind<'a>,
327 }
328
329 impl<'a> Parse<'a> for CanonicalFunc<'a> {
330 fn parse(parser: Parser<'a>) -> Result<Self> {
331 let span = parser.parse::<kw::canon>()?.0;
332 let mut l = parser.lookahead1();
333
334 if l.peek::<kw::lift>()? {
335 let info = parser.parse()?;
336 let (id, name, ty) = parser.parens(|parser| {
337 parser.parse::<kw::func>()?;
338 let id = parser.parse()?;
339 let name = parser.parse()?;
340 let ty = parser.parse()?;
341 Ok((id, name, ty))
342 })?;
343
344 Ok(Self {
345 span,
346 id,
347 name,
348 kind: CanonicalFuncKind::Lift { info, ty },
349 })
350 } else {
351 let kind = CoreFuncKind::parse_lookahead(l)?;
352 let (id, name) = parser.parens(|parser| {
353 parser.parse::<kw::core>()?;
354 parser.parse::<kw::func>()?;
355 let id = parser.parse()?;
356 let name = parser.parse()?;
357 Ok((id, name))
358 })?;
359
360 Ok(Self {
361 span,
362 id,
363 name,
364 kind: CanonicalFuncKind::Core(kind),
365 })
366 }
367 }
368 }
369
370 /// Possible ways to define a canonical function in the text format.
371 #[derive(Debug)]
372 #[allow(missing_docs)]
373 pub enum CanonicalFuncKind<'a> {
374 /// A canonical function that is defined in terms of lifting a core function.
375 Lift {
376 /// The lifted function's type.
377 ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>,
378 /// Information relating to the lifting of the core function.
379 info: CanonLift<'a>,
380 },
381
382 /// A canonical function that defines a core function, whose variants are
383 /// delegated to `CoreFuncKind`.
384 Core(CoreFuncKind<'a>),
385 }
386
387 /// Information relating to lifting a core function.
388 #[derive(Debug)]
389 pub struct CanonLift<'a> {
390 /// The core function being lifted.
391 pub func: CoreItemRef<'a, kw::func>,
392 /// The canonical options for the lifting.
393 pub opts: Vec<CanonOpt<'a>>,
394 }
395
396 impl<'a> Parse<'a> for CanonLift<'a> {
397 fn parse(parser: Parser<'a>) -> Result<Self> {
398 parser.parse::<kw::lift>()?;
399
400 Ok(Self {
401 func: parser.parens(|parser| {
402 parser.parse::<kw::core>()?;
403 parser.parse()
404 })?,
405 opts: parser.parse()?,
406 })
407 }
408 }
409
410 impl Default for CanonLift<'_> {
411 fn default() -> Self {
412 let span = Span::from_offset(0);
413 Self {
414 func: CoreItemRef {
415 kind: kw::func(span),
416 idx: Index::Num(0, span),
417 export_name: None,
418 },
419 opts: Vec::new(),
420 }
421 }
422 }
423
424 /// Information relating to lowering a component function.
425 #[derive(Debug)]
426 pub struct CanonLower<'a> {
427 /// The function being lowered.
428 pub func: ItemRef<'a, kw::func>,
429 /// The canonical options for the lowering.
430 pub opts: Vec<CanonOpt<'a>>,
431 }
432
433 impl<'a> Parse<'a> for CanonLower<'a> {
434 fn parse(parser: Parser<'a>) -> Result<Self> {
435 parser.parse::<kw::lower>()?;
436
437 Ok(Self {
438 func: parser.parens(|parser| parser.parse())?,
439 opts: parser.parse()?,
440 })
441 }
442 }
443
444 impl Default for CanonLower<'_> {
445 fn default() -> Self {
446 let span = Span::from_offset(0);
447 Self {
448 func: ItemRef {
449 kind: kw::func(span),
450 idx: Index::Num(0, span),
451 export_names: Vec::new(),
452 },
453 opts: Vec::new(),
454 }
455 }
456 }
457
458 /// Information relating to the `resource.new` intrinsic.
459 #[derive(Debug)]
460 pub struct CanonResourceNew<'a> {
461 /// The resource type that this intrinsic creates an owned reference to.
462 pub ty: Index<'a>,
463 }
464
465 impl<'a> Parse<'a> for CanonResourceNew<'a> {
466 fn parse(parser: Parser<'a>) -> Result<Self> {
467 parser.parse::<kw::resource_new>()?;
468
469 Ok(Self {
470 ty: parser.parse()?,
471 })
472 }
473 }
474
475 /// Information relating to the `resource.drop` intrinsic.
476 #[derive(Debug)]
477 pub struct CanonResourceDrop<'a> {
478 /// The resource type that this intrinsic is dropping.
479 pub ty: Index<'a>,
480 /// Whether or not this function is async
481 pub async_: bool,
482 }
483
484 impl<'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 async_: parser.parse::<Option<kw::r#async>>()?.is_some(),
491 })
492 }
493 }
494
495 /// Information relating to the `resource.rep` intrinsic.
496 #[derive(Debug)]
497 pub struct CanonResourceRep<'a> {
498 /// The resource type that this intrinsic is accessing.
499 pub ty: Index<'a>,
500 }
501
502 impl<'a> Parse<'a> for CanonResourceRep<'a> {
503 fn parse(parser: Parser<'a>) -> Result<Self> {
504 parser.parse::<kw::resource_rep>()?;
505
506 Ok(Self {
507 ty: parser.parse()?,
508 })
509 }
510 }
511
512 /// Information relating to the `thread.spawn_ref` intrinsic.
513 #[derive(Debug)]
514 pub struct CanonThreadSpawnRef<'a> {
515 /// The function type that is being spawned.
516 pub ty: Index<'a>,
517 }
518
519 impl<'a> Parse<'a> for CanonThreadSpawnRef<'a> {
520 fn parse(parser: Parser<'a>) -> Result<Self> {
521 parser.parse::<kw::thread_spawn_ref>()?;
522
523 Ok(Self {
524 ty: parser.parse()?,
525 })
526 }
527 }
528
529 /// Information relating to the `thread.spawn_indirect` intrinsic.
530 ///
531 /// This should look quite similar to parsing of `CallIndirect`.
532 #[derive(Debug)]
533 pub struct CanonThreadSpawnIndirect<'a> {
534 /// The function type that is being spawned.
535 pub ty: Index<'a>,
536 /// The table that this spawn is going to be indexing.
537 pub table: CoreItemRef<'a, kw::table>,
538 }
539
540 impl<'a> Parse<'a> for CanonThreadSpawnIndirect<'a> {
541 fn parse(parser: Parser<'a>) -> Result<Self> {
542 parser.parse::<kw::thread_spawn_indirect>()?;
543 let ty = parser.parse()?;
544 let table = parser.parens(|p| p.parse())?;
545 Ok(Self { ty, table })
546 }
547 }
548
549 /// Information relating to the `thread.spawn` intrinsic.
550 #[derive(Debug)]
551 pub struct CanonThreadAvailableParallelism;
552
553 impl<'a> Parse<'a> for CanonThreadAvailableParallelism {
554 fn parse(parser: Parser<'a>) -> Result<Self> {
555 parser.parse::<kw::thread_available_parallelism>()?;
556 Ok(Self)
557 }
558 }
559
560 /// Information relating to the `task.return` intrinsic.
561 #[derive(Debug)]
562 pub struct CanonTaskReturn<'a> {
563 /// The type of the result which may be returned with this intrinsic.
564 pub result: Option<ComponentValType<'a>>,
565 /// The canonical options for storing values.
566 pub opts: Vec<CanonOpt<'a>>,
567 }
568
569 impl<'a> Parse<'a> for CanonTaskReturn<'a> {
570 fn parse(parser: Parser<'a>) -> Result<Self> {
571 parser.parse::<kw::task_return>()?;
572
573 Ok(Self {
574 result: if parser.peek2::<kw::result>()? {
575 Some(parser.parens(|p| {
576 p.parse::<kw::result>()?.0;
577 p.parse()
578 })?)
579 } else {
580 None
581 },
582 opts: parser.parse()?,
583 })
584 }
585 }
586
587 /// Information relating to the `waitable-set.wait` intrinsic.
588 #[derive(Debug)]
589 pub struct CanonWaitableSetWait<'a> {
590 /// If true, the component instance may be reentered during a call to this
591 /// intrinsic.
592 pub async_: bool,
593 /// The memory to use when returning an event to the caller.
594 pub memory: CoreItemRef<'a, kw::memory>,
595 }
596
597 impl<'a> Parse<'a> for CanonWaitableSetWait<'a> {
598 fn parse(parser: Parser<'a>) -> Result<Self> {
599 parser.parse::<kw::waitable_set_wait>()?;
600 let async_ = parser.parse::<Option<kw::cancellable>>()?.is_some();
601 let memory = parser.parens(|p| p.parse())?;
602
603 Ok(Self { async_, memory })
604 }
605 }
606
607 /// Information relating to the `waitable-set.poll` intrinsic.
608 #[derive(Debug)]
609 pub struct CanonWaitableSetPoll<'a> {
610 /// If true, the component instance may be reentered during a call to this
611 /// intrinsic.
612 pub async_: bool,
613 /// The memory to use when returning an event to the caller.
614 pub memory: CoreItemRef<'a, kw::memory>,
615 }
616
617 impl<'a> Parse<'a> for CanonWaitableSetPoll<'a> {
618 fn parse(parser: Parser<'a>) -> Result<Self> {
619 parser.parse::<kw::waitable_set_poll>()?;
620 let async_ = parser.parse::<Option<kw::cancellable>>()?.is_some();
621 let memory = parser.parens(|p| p.parse())?;
622
623 Ok(Self { async_, memory })
624 }
625 }
626
627 /// Information relating to the `thread.yield` intrinsic.
628 #[derive(Debug)]
629 pub struct CanonThreadYield {
630 /// If true, the component instance may be reentered during a call to this
631 /// intrinsic.
632 pub cancellable: bool,
633 }
634
635 impl<'a> Parse<'a> for CanonThreadYield {
636 fn parse(parser: Parser<'a>) -> Result<Self> {
637 parser.parse::<kw::thread_yield>()?;
638 let cancellable = parser.parse::<Option<kw::cancellable>>()?.is_some();
639
640 Ok(Self { cancellable })
641 }
642 }
643
644 /// Information relating to the `subtask.cancel` intrinsic.
645 #[derive(Debug)]
646 pub struct CanonSubtaskCancel {
647 /// If false, block until cancel is finished; otherwise return BLOCKED if
648 /// necessary.
649 pub async_: bool,
650 }
651
652 impl<'a> Parse<'a> for CanonSubtaskCancel {
653 fn parse(parser: Parser<'a>) -> Result<Self> {
654 parser.parse::<kw::subtask_cancel>()?;
655 let async_ = parser.parse::<Option<kw::r#async>>()?.is_some();
656
657 Ok(Self { async_ })
658 }
659 }
660
661 /// Information relating to the `stream.new` intrinsic.
662 #[derive(Debug)]
663 pub struct CanonStreamNew<'a> {
664 /// The stream type to instantiate.
665 pub ty: Index<'a>,
666 }
667
668 impl<'a> Parse<'a> for CanonStreamNew<'a> {
669 fn parse(parser: Parser<'a>) -> Result<Self> {
670 parser.parse::<kw::stream_new>()?;
671
672 Ok(Self {
673 ty: parser.parse()?,
674 })
675 }
676 }
677
678 /// Information relating to the `stream.read` intrinsic.
679 #[derive(Debug)]
680 pub struct CanonStreamRead<'a> {
681 /// The stream type to instantiate.
682 pub ty: Index<'a>,
683 /// The canonical options for storing values.
684 pub opts: Vec<CanonOpt<'a>>,
685 }
686
687 impl<'a> Parse<'a> for CanonStreamRead<'a> {
688 fn parse(parser: Parser<'a>) -> Result<Self> {
689 parser.parse::<kw::stream_read>()?;
690
691 Ok(Self {
692 ty: parser.parse()?,
693 opts: parser.parse()?,
694 })
695 }
696 }
697
698 /// Information relating to the `stream.write` intrinsic.
699 #[derive(Debug)]
700 pub struct CanonStreamWrite<'a> {
701 /// The stream type to instantiate.
702 pub ty: Index<'a>,
703 /// The canonical options for loading values.
704 pub opts: Vec<CanonOpt<'a>>,
705 }
706
707 impl<'a> Parse<'a> for CanonStreamWrite<'a> {
708 fn parse(parser: Parser<'a>) -> Result<Self> {
709 parser.parse::<kw::stream_write>()?;
710
711 Ok(Self {
712 ty: parser.parse()?,
713 opts: parser.parse()?,
714 })
715 }
716 }
717
718 /// Information relating to the `stream.cancel-read` intrinsic.
719 #[derive(Debug)]
720 pub struct CanonStreamCancelRead<'a> {
721 /// The stream type to instantiate.
722 pub ty: Index<'a>,
723 /// If false, block until cancel is finished; otherwise return BLOCKED if
724 /// necessary.
725 pub async_: bool,
726 }
727
728 impl<'a> Parse<'a> for CanonStreamCancelRead<'a> {
729 fn parse(parser: Parser<'a>) -> Result<Self> {
730 parser.parse::<kw::stream_cancel_read>()?;
731
732 Ok(Self {
733 ty: parser.parse()?,
734 async_: parser.parse::<Option<kw::r#async>>()?.is_some(),
735 })
736 }
737 }
738
739 /// Information relating to the `stream.cancel-write` intrinsic.
740 #[derive(Debug)]
741 pub struct CanonStreamCancelWrite<'a> {
742 /// The stream type to instantiate.
743 pub ty: Index<'a>,
744 /// If false, block until cancel is finished; otherwise return BLOCKED if
745 /// necessary.
746 pub async_: bool,
747 }
748
749 impl<'a> Parse<'a> for CanonStreamCancelWrite<'a> {
750 fn parse(parser: Parser<'a>) -> Result<Self> {
751 parser.parse::<kw::stream_cancel_write>()?;
752
753 Ok(Self {
754 ty: parser.parse()?,
755 async_: parser.parse::<Option<kw::r#async>>()?.is_some(),
756 })
757 }
758 }
759
760 /// Information relating to the `stream.drop-readable` intrinsic.
761 #[derive(Debug)]
762 pub struct CanonStreamDropReadable<'a> {
763 /// The stream type to drop.
764 pub ty: Index<'a>,
765 }
766
767 impl<'a> Parse<'a> for CanonStreamDropReadable<'a> {
768 fn parse(parser: Parser<'a>) -> Result<Self> {
769 parser.parse::<kw::stream_drop_readable>()?;
770
771 Ok(Self {
772 ty: parser.parse()?,
773 })
774 }
775 }
776
777 /// Information relating to the `stream.drop-writable` intrinsic.
778 #[derive(Debug)]
779 pub struct CanonStreamDropWritable<'a> {
780 /// The stream type to drop.
781 pub ty: Index<'a>,
782 }
783
784 impl<'a> Parse<'a> for CanonStreamDropWritable<'a> {
785 fn parse(parser: Parser<'a>) -> Result<Self> {
786 parser.parse::<kw::stream_drop_writable>()?;
787
788 Ok(Self {
789 ty: parser.parse()?,
790 })
791 }
792 }
793
794 /// Information relating to the `future.new` intrinsic.
795 #[derive(Debug)]
796 pub struct CanonFutureNew<'a> {
797 /// The future type to instantiate.
798 pub ty: Index<'a>,
799 }
800
801 impl<'a> Parse<'a> for CanonFutureNew<'a> {
802 fn parse(parser: Parser<'a>) -> Result<Self> {
803 parser.parse::<kw::future_new>()?;
804
805 Ok(Self {
806 ty: parser.parse()?,
807 })
808 }
809 }
810
811 /// Information relating to the `future.read` intrinsic.
812 #[derive(Debug)]
813 pub struct CanonFutureRead<'a> {
814 /// The future type to instantiate.
815 pub ty: Index<'a>,
816 /// The canonical options for storing values.
817 pub opts: Vec<CanonOpt<'a>>,
818 }
819
820 impl<'a> Parse<'a> for CanonFutureRead<'a> {
821 fn parse(parser: Parser<'a>) -> Result<Self> {
822 parser.parse::<kw::future_read>()?;
823
824 Ok(Self {
825 ty: parser.parse()?,
826 opts: parser.parse()?,
827 })
828 }
829 }
830
831 /// Information relating to the `future.write` intrinsic.
832 #[derive(Debug)]
833 pub struct CanonFutureWrite<'a> {
834 /// The future type to instantiate.
835 pub ty: Index<'a>,
836 /// The canonical options for loading values.
837 pub opts: Vec<CanonOpt<'a>>,
838 }
839
840 impl<'a> Parse<'a> for CanonFutureWrite<'a> {
841 fn parse(parser: Parser<'a>) -> Result<Self> {
842 parser.parse::<kw::future_write>()?;
843
844 Ok(Self {
845 ty: parser.parse()?,
846 opts: parser.parse()?,
847 })
848 }
849 }
850
851 /// Information relating to the `future.cancel-read` intrinsic.
852 #[derive(Debug)]
853 pub struct CanonFutureCancelRead<'a> {
854 /// The future type to instantiate.
855 pub ty: Index<'a>,
856 /// If false, block until cancel is finished; otherwise return BLOCKED if
857 /// necessary.
858 pub async_: bool,
859 }
860
861 impl<'a> Parse<'a> for CanonFutureCancelRead<'a> {
862 fn parse(parser: Parser<'a>) -> Result<Self> {
863 parser.parse::<kw::future_cancel_read>()?;
864
865 Ok(Self {
866 ty: parser.parse()?,
867 async_: parser.parse::<Option<kw::r#async>>()?.is_some(),
868 })
869 }
870 }
871
872 /// Information relating to the `future.cancel-write` intrinsic.
873 #[derive(Debug)]
874 pub struct CanonFutureCancelWrite<'a> {
875 /// The future type to instantiate.
876 pub ty: Index<'a>,
877 /// If false, block until cancel is finished; otherwise return BLOCKED if
878 /// necessary.
879 pub async_: bool,
880 }
881
882 impl<'a> Parse<'a> for CanonFutureCancelWrite<'a> {
883 fn parse(parser: Parser<'a>) -> Result<Self> {
884 parser.parse::<kw::future_cancel_write>()?;
885
886 Ok(Self {
887 ty: parser.parse()?,
888 async_: parser.parse::<Option<kw::r#async>>()?.is_some(),
889 })
890 }
891 }
892
893 /// Information relating to the `future.drop-readable` intrinsic.
894 #[derive(Debug)]
895 pub struct CanonFutureDropReadable<'a> {
896 /// The future type to drop.
897 pub ty: Index<'a>,
898 }
899
900 impl<'a> Parse<'a> for CanonFutureDropReadable<'a> {
901 fn parse(parser: Parser<'a>) -> Result<Self> {
902 parser.parse::<kw::future_drop_readable>()?;
903
904 Ok(Self {
905 ty: parser.parse()?,
906 })
907 }
908 }
909
910 /// Information relating to the `future.drop-writable` intrinsic.
911 #[derive(Debug)]
912 pub struct CanonFutureDropWritable<'a> {
913 /// The future type to drop.
914 pub ty: Index<'a>,
915 }
916
917 impl<'a> Parse<'a> for CanonFutureDropWritable<'a> {
918 fn parse(parser: Parser<'a>) -> Result<Self> {
919 parser.parse::<kw::future_drop_writable>()?;
920
921 Ok(Self {
922 ty: parser.parse()?,
923 })
924 }
925 }
926
927 /// Information relating to the `error-context.new` intrinsic.
928 #[derive(Debug)]
929 pub struct CanonErrorContextNew<'a> {
930 /// The canonical options for loading the debug message.
931 pub opts: Vec<CanonOpt<'a>>,
932 }
933
934 impl<'a> Parse<'a> for CanonErrorContextNew<'a> {
935 fn parse(parser: Parser<'a>) -> Result<Self> {
936 parser.parse::<kw::error_context_new>()?;
937
938 Ok(Self {
939 opts: parser.parse()?,
940 })
941 }
942 }
943
944 /// Information relating to the `error-context.debug-message` intrinsic.
945 #[derive(Debug)]
946 pub struct CanonErrorContextDebugMessage<'a> {
947 /// The canonical options for storing the debug message.
948 pub opts: Vec<CanonOpt<'a>>,
949 }
950
951 impl<'a> Parse<'a> for CanonErrorContextDebugMessage<'a> {
952 fn parse(parser: Parser<'a>) -> Result<Self> {
953 parser.parse::<kw::error_context_debug_message>()?;
954
955 Ok(Self {
956 opts: parser.parse()?,
957 })
958 }
959 }
960
961 /// Information relating to the `thread.new_indirect` intrinsic.
962 #[derive(Debug)]
963 pub struct CanonThreadNewIndirect<'a> {
964 /// The function type for the thread start function.
965 pub ty: Index<'a>,
966 /// The table to index.
967 pub table: CoreItemRef<'a, kw::table>,
968 }
969
970 impl<'a> Parse<'a> for CanonThreadNewIndirect<'a> {
971 fn parse(parser: Parser<'a>) -> Result<Self> {
972 parser.parse::<kw::thread_new_indirect>()?;
973 let ty = parser.parse()?;
974 let table = parser.parens(|p| p.parse())?;
975 Ok(Self { ty, table })
976 }
977 }
978
979 /// Information relating to the `thread.switch-to` intrinsic.
980 #[derive(Debug)]
981 pub struct CanonThreadSwitchTo {
982 /// Whether the thread can be cancelled while suspended at this point.
983 pub cancellable: bool,
984 }
985
986 impl<'a> Parse<'a> for CanonThreadSwitchTo {
987 fn parse(parser: Parser<'a>) -> Result<Self> {
988 parser.parse::<kw::thread_switch_to>()?;
989 let cancellable = parser.parse::<Option<kw::cancellable>>()?.is_some();
990 Ok(Self { cancellable })
991 }
992 }
993
994 /// Information relating to the `thread.suspend` intrinsic.
995 #[derive(Debug)]
996 pub struct CanonThreadSuspend {
997 /// Whether the thread can be cancelled while suspended at this point.
998 pub cancellable: bool,
999 }
1000 impl<'a> Parse<'a> for CanonThreadSuspend {
1001 fn parse(parser: Parser<'a>) -> Result<Self> {
1002 parser.parse::<kw::thread_suspend>()?;
1003 let cancellable = parser.parse::<Option<kw::cancellable>>()?.is_some();
1004 Ok(Self { cancellable })
1005 }
1006 }
1007
1008 /// Information relating to the `thread.yield-to` intrinsic.
1009 #[derive(Debug)]
1010 pub struct CanonThreadYieldTo {
1011 /// Whether the thread can be cancelled while yielding at this point.
1012 pub cancellable: bool,
1013 }
1014 impl<'a> Parse<'a> for CanonThreadYieldTo {
1015 fn parse(parser: Parser<'a>) -> Result<Self> {
1016 parser.parse::<kw::thread_yield_to>()?;
1017 let cancellable = parser.parse::<Option<kw::cancellable>>()?.is_some();
1018 Ok(Self { cancellable })
1019 }
1020 }
1021
1022 #[derive(Debug)]
1023 /// Canonical ABI options.
1024 pub enum CanonOpt<'a> {
1025 /// Encode strings as UTF-8.
1026 StringUtf8,
1027 /// Encode strings as UTF-16.
1028 StringUtf16,
1029 /// Encode strings as "compact UTF-16".
1030 StringLatin1Utf16,
1031 /// Use the specified memory for canonical ABI memory access.
1032 Memory(CoreItemRef<'a, kw::memory>),
1033 /// Use the specified reallocation function for memory allocations.
1034 Realloc(CoreItemRef<'a, kw::func>),
1035 /// Call the specified function after the lifted function has returned.
1036 PostReturn(CoreItemRef<'a, kw::func>),
1037 /// Use the async ABI for lifting or lowering.
1038 Async,
1039 /// Use the specified function to deliver async events to stackless coroutines.
1040 Callback(CoreItemRef<'a, kw::func>),
1041 /// Lower this component function into the specified core function type.
1042 CoreType(CoreItemRef<'a, kw::r#type>),
1043 /// Use the GC variant of the canonical ABI.
1044 Gc,
1045 }
1046
1047 impl Default for kw::r#type {
1048 fn default() -> Self {
1049 Self(Span::from_offset(0))
1050 }
1051 }
1052
1053 impl<'a> Parse<'a> for CanonOpt<'a> {
1054 fn parse(parser: Parser<'a>) -> Result<Self> {
1055 let mut l = parser.lookahead1();
1056 if l.peek::<kw::string_utf8>()? {
1057 parser.parse::<kw::string_utf8>()?;
1058 Ok(Self::StringUtf8)
1059 } else if l.peek::<kw::string_utf16>()? {
1060 parser.parse::<kw::string_utf16>()?;
1061 Ok(Self::StringUtf16)
1062 } else if l.peek::<kw::string_latin1_utf16>()? {
1063 parser.parse::<kw::string_latin1_utf16>()?;
1064 Ok(Self::StringLatin1Utf16)
1065 } else if l.peek::<kw::r#async>()? {
1066 parser.parse::<kw::r#async>()?;
1067 Ok(Self::Async)
1068 } else if l.peek::<kw::gc>()? {
1069 parser.parse::<kw::gc>()?;
1070 Ok(Self::Gc)
1071 } else if l.peek::<LParen>()? {
1072 parser.parens(|parser| {
1073 let mut l = parser.lookahead1();
1074 if l.peek::<kw::memory>()? {
1075 Ok(CanonOpt::Memory(parser.parse()?))
1076 } else if l.peek::<kw::realloc>()? {
1077 parser.parse::<kw::realloc>()?;
1078 Ok(CanonOpt::Realloc(
1079 parser.parse::<IndexOrCoreRef<'_, _>>()?.0,
1080 ))
1081 } else if l.peek::<kw::post_return>()? {
1082 parser.parse::<kw::post_return>()?;
1083 Ok(CanonOpt::PostReturn(
1084 parser.parse::<IndexOrCoreRef<'_, _>>()?.0,
1085 ))
1086 } else if l.peek::<kw::callback>()? {
1087 parser.parse::<kw::callback>()?;
1088 Ok(CanonOpt::Callback(
1089 parser.parse::<IndexOrCoreRef<'_, _>>()?.0,
1090 ))
1091 } else if l.peek::<kw::core_type>()? {
1092 parser.parse::<kw::core_type>()?;
1093 Ok(CanonOpt::CoreType(
1094 parser.parse::<IndexOrCoreRef<'_, _>>()?.0,
1095 ))
1096 } else {
1097 Err(l.error())
1098 }
1099 })
1100 } else {
1101 Err(l.error())
1102 }
1103 }
1104 }
1105
1106 impl Peek for CanonOpt<'_> {
1107 fn peek(cursor: Cursor<'_>) -> Result<bool> {
1108 Ok(kw::string_utf8::peek(cursor)?
1109 || kw::string_utf16::peek(cursor)?
1110 || kw::string_latin1_utf16::peek(cursor)?
1111 || kw::r#async::peek(cursor)?
1112 || kw::gc::peek(cursor)?
1113 || match cursor.lparen()? {
1114 Some(next) => {
1115 kw::memory::peek(next)?
1116 || kw::realloc::peek(next)?
1117 || kw::post_return::peek(next)?
1118 || kw::callback::peek(next)?
1119 || kw::core_type::peek(next)?
1120 }
1121 None => false,
1122 })
1123 }
1124
1125 fn display() -> &'static str {
1126 "canonical option"
1127 }
1128 }
1129
1130 impl<'a> Parse<'a> for Vec<CanonOpt<'a>> {
1131 fn parse(parser: Parser<'a>) -> Result<Self> {
1132 let mut funcs = Vec::new();
1133 while parser.peek::<CanonOpt<'_>>()? {
1134 funcs.push(parser.parse()?);
1135 }
1136 Ok(funcs)
1137 }
1138 }