Debian Codegraph @

third_party/rust/wast/src/component/func.rs

raw
1 use crate::component::*;
2 use crate::kw;
3 use crate::parser::{Parse, Parser, 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.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 ThreadSpawn(CanonThreadSpawn<'a>),
55 ThreadHwConcurrency(CanonThreadHwConcurrency),
56 }
57
58 impl<'a> Parse<'a> for CoreFuncKind<'a> {
59 fn parse(parser: Parser<'a>) -> Result<Self> {
60 parser.parens(|parser| {
61 let mut l = parser.lookahead1();
62 if l.peek::<kw::canon>()? {
63 parser.parse::<kw::canon>()?;
64 } else if l.peek::<kw::alias>()? {
65 return Ok(Self::Alias(parser.parse()?));
66 } else {
67 return Err(l.error());
68 }
69 let mut l = parser.lookahead1();
70 if l.peek::<kw::lower>()? {
71 Ok(CoreFuncKind::Lower(parser.parse()?))
72 } else if l.peek::<kw::resource_new>()? {
73 Ok(CoreFuncKind::ResourceNew(parser.parse()?))
74 } else if l.peek::<kw::resource_drop>()? {
75 Ok(CoreFuncKind::ResourceDrop(parser.parse()?))
76 } else if l.peek::<kw::resource_rep>()? {
77 Ok(CoreFuncKind::ResourceRep(parser.parse()?))
78 } else if l.peek::<kw::thread_spawn>()? {
79 Ok(CoreFuncKind::ThreadSpawn(parser.parse()?))
80 } else if l.peek::<kw::thread_hw_concurrency>()? {
81 Ok(CoreFuncKind::ThreadHwConcurrency(parser.parse()?))
82 } else {
83 Err(l.error())
84 }
85 })
86 }
87 }
88
89 /// A declared component function.
90 ///
91 /// This may be a member of the import, alias, or canon sections.
92 #[derive(Debug)]
93 pub struct Func<'a> {
94 /// Where this `func` was defined.
95 pub span: Span,
96 /// An identifier that this function is resolved with (optionally) for name
97 /// resolution.
98 pub id: Option<Id<'a>>,
99 /// An optional name for this function stored in the custom `name` section.
100 pub name: Option<NameAnnotation<'a>>,
101 /// If present, inline export annotations which indicate names this
102 /// definition should be exported under.
103 pub exports: InlineExport<'a>,
104 /// The kind of function.
105 pub kind: FuncKind<'a>,
106 }
107
108 impl<'a> Parse<'a> for Func<'a> {
109 fn parse(parser: Parser<'a>) -> Result<Self> {
110 let span = parser.parse::<kw::func>()?.0;
111 let id = parser.parse()?;
112 let name = parser.parse()?;
113 let exports = parser.parse()?;
114 let kind = parser.parse()?;
115
116 Ok(Self {
117 span,
118 id,
119 name,
120 exports,
121 kind,
122 })
123 }
124 }
125
126 /// Represents the kind of component functions.
127 #[derive(Debug)]
128 pub enum FuncKind<'a> {
129 /// A function which is actually defined as an import, such as:
130 ///
131 /// ```text
132 /// (func (import "foo") (param string))
133 /// ```
134 Import {
135 /// The import name of this import.
136 import: InlineImport<'a>,
137 /// The type that this function will have.
138 ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>,
139 },
140 /// The function is defined in terms of lifting a core function.
141 ///
142 /// The function is actually a member of the canon section.
143 Lift {
144 /// The lifted function's type.
145 ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>,
146 /// Information relating to the lifting of the core function.
147 info: CanonLift<'a>,
148 },
149 /// The function is defined in terms of aliasing a component instance export.
150 ///
151 /// The function is actually a member of the alias section.
152 Alias(InlineExportAlias<'a, false>),
153 }
154
155 impl<'a> Parse<'a> for FuncKind<'a> {
156 fn parse(parser: Parser<'a>) -> Result<Self> {
157 if let Some(import) = parser.parse()? {
158 Ok(Self::Import {
159 import,
160 ty: parser.parse()?,
161 })
162 } else if parser.peek::<LParen>()? && parser.peek2::<kw::alias>()? {
163 parser.parens(|parser| Ok(Self::Alias(parser.parse()?)))
164 } else {
165 Ok(Self::Lift {
166 ty: parser.parse()?,
167 info: parser.parens(|parser| {
168 parser.parse::<kw::canon>()?;
169 parser.parse()
170 })?,
171 })
172 }
173 }
174 }
175
176 /// A WebAssembly canonical function to be inserted into a component.
177 ///
178 /// This is a member of the canonical section.
179 #[derive(Debug)]
180 pub struct CanonicalFunc<'a> {
181 /// Where this `func` was defined.
182 pub span: Span,
183 /// An identifier that this function is resolved with (optionally) for name
184 /// resolution.
185 pub id: Option<Id<'a>>,
186 /// An optional name for this function stored in the custom `name` section.
187 pub name: Option<NameAnnotation<'a>>,
188 /// What kind of function this is, be it a lowered or lifted function.
189 pub kind: CanonicalFuncKind<'a>,
190 }
191
192 impl<'a> Parse<'a> for CanonicalFunc<'a> {
193 fn parse(parser: Parser<'a>) -> Result<Self> {
194 let span = parser.parse::<kw::canon>()?.0;
195
196 if parser.peek::<kw::lift>()? {
197 let info = parser.parse()?;
198 let (id, name, ty) = parser.parens(|parser| {
199 parser.parse::<kw::func>()?;
200 let id = parser.parse()?;
201 let name = parser.parse()?;
202 let ty = parser.parse()?;
203 Ok((id, name, ty))
204 })?;
205
206 Ok(Self {
207 span,
208 id,
209 name,
210 kind: CanonicalFuncKind::Lift { info, ty },
211 })
212 } else if parser.peek::<kw::lower>()? {
213 Self::parse_core_func(span, parser, CanonicalFuncKind::Lower)
214 } else if parser.peek::<kw::resource_new>()? {
215 Self::parse_core_func(span, parser, CanonicalFuncKind::ResourceNew)
216 } else if parser.peek::<kw::resource_drop>()? {
217 Self::parse_core_func(span, parser, CanonicalFuncKind::ResourceDrop)
218 } else if parser.peek::<kw::resource_rep>()? {
219 Self::parse_core_func(span, parser, CanonicalFuncKind::ResourceRep)
220 } else {
221 Err(parser.error("expected `canon lift` or `canon lower`"))
222 }
223 }
224 }
225
226 impl<'a> CanonicalFunc<'a> {
227 fn parse_core_func<T>(
228 span: Span,
229 parser: Parser<'a>,
230 variant: fn(T) -> CanonicalFuncKind<'a>,
231 ) -> Result<Self>
232 where
233 T: Parse<'a>,
234 {
235 let info = parser.parse()?;
236 let (id, name) = parser.parens(|parser| {
237 parser.parse::<kw::core>()?;
238 parser.parse::<kw::func>()?;
239 let id = parser.parse()?;
240 let name = parser.parse()?;
241 Ok((id, name))
242 })?;
243
244 Ok(Self {
245 span,
246 id,
247 name,
248 kind: variant(info),
249 })
250 }
251 }
252
253 /// Possible ways to define a canonical function in the text format.
254 #[derive(Debug)]
255 #[allow(missing_docs)]
256 pub enum CanonicalFuncKind<'a> {
257 /// A canonical function that is defined in terms of lifting a core function.
258 Lift {
259 /// The lifted function's type.
260 ty: ComponentTypeUse<'a, ComponentFunctionType<'a>>,
261 /// Information relating to the lifting of the core function.
262 info: CanonLift<'a>,
263 },
264 /// A canonical function that is defined in terms of lowering a component function.
265 Lower(CanonLower<'a>),
266
267 ResourceNew(CanonResourceNew<'a>),
268 ResourceDrop(CanonResourceDrop<'a>),
269 ResourceRep(CanonResourceRep<'a>),
270
271 ThreadSpawn(CanonThreadSpawn<'a>),
272 ThreadHwConcurrency(CanonThreadHwConcurrency),
273 }
274
275 /// Information relating to lifting a core function.
276 #[derive(Debug)]
277 pub struct CanonLift<'a> {
278 /// The core function being lifted.
279 pub func: CoreItemRef<'a, kw::func>,
280 /// The canonical options for the lifting.
281 pub opts: Vec<CanonOpt<'a>>,
282 }
283
284 impl<'a> Parse<'a> for CanonLift<'a> {
285 fn parse(parser: Parser<'a>) -> Result<Self> {
286 parser.parse::<kw::lift>()?;
287
288 Ok(Self {
289 func: parser.parens(|parser| {
290 parser.parse::<kw::core>()?;
291 parser.parse()
292 })?,
293 opts: parser.parse()?,
294 })
295 }
296 }
297
298 impl Default for CanonLift<'_> {
299 fn default() -> Self {
300 let span = Span::from_offset(0);
301 Self {
302 func: CoreItemRef {
303 kind: kw::func(span),
304 idx: Index::Num(0, span),
305 export_name: None,
306 },
307 opts: Vec::new(),
308 }
309 }
310 }
311
312 /// Information relating to lowering a component function.
313 #[derive(Debug)]
314 pub struct CanonLower<'a> {
315 /// The function being lowered.
316 pub func: ItemRef<'a, kw::func>,
317 /// The canonical options for the lowering.
318 pub opts: Vec<CanonOpt<'a>>,
319 }
320
321 impl<'a> Parse<'a> for CanonLower<'a> {
322 fn parse(parser: Parser<'a>) -> Result<Self> {
323 parser.parse::<kw::lower>()?;
324
325 Ok(Self {
326 func: parser.parens(|parser| parser.parse())?,
327 opts: parser.parse()?,
328 })
329 }
330 }
331
332 impl Default for CanonLower<'_> {
333 fn default() -> Self {
334 let span = Span::from_offset(0);
335 Self {
336 func: ItemRef {
337 kind: kw::func(span),
338 idx: Index::Num(0, span),
339 export_names: Vec::new(),
340 },
341 opts: Vec::new(),
342 }
343 }
344 }
345
346 /// Information relating to the `resource.new` intrinsic.
347 #[derive(Debug)]
348 pub struct CanonResourceNew<'a> {
349 /// The resource type that this intrinsic creates an owned reference to.
350 pub ty: Index<'a>,
351 }
352
353 impl<'a> Parse<'a> for CanonResourceNew<'a> {
354 fn parse(parser: Parser<'a>) -> Result<Self> {
355 parser.parse::<kw::resource_new>()?;
356
357 Ok(Self {
358 ty: parser.parse()?,
359 })
360 }
361 }
362
363 impl Default for CanonResourceNew<'_> {
364 fn default() -> Self {
365 CanonResourceNew {
366 ty: Index::Num(0, Span::from_offset(0)),
367 }
368 }
369 }
370
371 /// Information relating to the `resource.drop` intrinsic.
372 #[derive(Debug)]
373 pub struct CanonResourceDrop<'a> {
374 /// The resource type that this intrinsic is dropping.
375 pub ty: Index<'a>,
376 }
377
378 impl<'a> Parse<'a> for CanonResourceDrop<'a> {
379 fn parse(parser: Parser<'a>) -> Result<Self> {
380 parser.parse::<kw::resource_drop>()?;
381
382 Ok(Self {
383 ty: parser.parse()?,
384 })
385 }
386 }
387
388 impl Default for CanonResourceDrop<'_> {
389 fn default() -> Self {
390 CanonResourceDrop {
391 ty: Index::Num(0, Span::from_offset(0)),
392 }
393 }
394 }
395
396 /// Information relating to the `resource.rep` intrinsic.
397 #[derive(Debug)]
398 pub struct CanonResourceRep<'a> {
399 /// The resource type that this intrinsic is accessing.
400 pub ty: Index<'a>,
401 }
402
403 impl<'a> Parse<'a> for CanonResourceRep<'a> {
404 fn parse(parser: Parser<'a>) -> Result<Self> {
405 parser.parse::<kw::resource_rep>()?;
406
407 Ok(Self {
408 ty: parser.parse()?,
409 })
410 }
411 }
412
413 impl Default for CanonResourceRep<'_> {
414 fn default() -> Self {
415 CanonResourceRep {
416 ty: Index::Num(0, Span::from_offset(0)),
417 }
418 }
419 }
420
421 /// Information relating to the `thread.spawn` intrinsic.
422 #[derive(Debug)]
423 pub struct CanonThreadSpawn<'a> {
424 /// The function type that is being spawned.
425 pub ty: Index<'a>,
426 }
427
428 impl<'a> Parse<'a> for CanonThreadSpawn<'a> {
429 fn parse(parser: Parser<'a>) -> Result<Self> {
430 parser.parse::<kw::thread_spawn>()?;
431
432 Ok(Self {
433 ty: parser.parse()?,
434 })
435 }
436 }
437
438 impl Default for CanonThreadSpawn<'_> {
439 fn default() -> Self {
440 CanonThreadSpawn {
441 ty: Index::Num(0, Span::from_offset(0)),
442 }
443 }
444 }
445
446 /// Information relating to the `thread.spawn` intrinsic.
447 #[derive(Debug)]
448 pub struct CanonThreadHwConcurrency;
449
450 impl<'a> Parse<'a> for CanonThreadHwConcurrency {
451 fn parse(parser: Parser<'a>) -> Result<Self> {
452 parser.parse::<kw::thread_hw_concurrency>()?;
453 Ok(Self)
454 }
455 }
456
457 impl Default for CanonThreadHwConcurrency {
458 fn default() -> Self {
459 Self
460 }
461 }
462
463 #[derive(Debug)]
464 /// Canonical ABI options.
465 pub enum CanonOpt<'a> {
466 /// Encode strings as UTF-8.
467 StringUtf8,
468 /// Encode strings as UTF-16.
469 StringUtf16,
470 /// Encode strings as "compact UTF-16".
471 StringLatin1Utf16,
472 /// Use the specified memory for canonical ABI memory access.
473 Memory(CoreItemRef<'a, kw::memory>),
474 /// Use the specified reallocation function for memory allocations.
475 Realloc(CoreItemRef<'a, kw::func>),
476 /// Call the specified function after the lifted function has returned.
477 PostReturn(CoreItemRef<'a, kw::func>),
478 }
479
480 impl<'a> Parse<'a> for CanonOpt<'a> {
481 fn parse(parser: Parser<'a>) -> Result<Self> {
482 let mut l = parser.lookahead1();
483 if l.peek::<kw::string_utf8>()? {
484 parser.parse::<kw::string_utf8>()?;
485 Ok(Self::StringUtf8)
486 } else if l.peek::<kw::string_utf16>()? {
487 parser.parse::<kw::string_utf16>()?;
488 Ok(Self::StringUtf16)
489 } else if l.peek::<kw::string_latin1_utf16>()? {
490 parser.parse::<kw::string_latin1_utf16>()?;
491 Ok(Self::StringLatin1Utf16)
492 } else if l.peek::<LParen>()? {
493 parser.parens(|parser| {
494 let mut l = parser.lookahead1();
495 if l.peek::<kw::memory>()? {
496 let span = parser.parse::<kw::memory>()?.0;
497 Ok(CanonOpt::Memory(parse_trailing_item_ref(
498 kw::memory(span),
499 parser,
500 )?))
501 } else if l.peek::<kw::realloc>()? {
502 parser.parse::<kw::realloc>()?;
503 Ok(CanonOpt::Realloc(
504 parser.parse::<IndexOrCoreRef<'_, _>>()?.0,
505 ))
506 } else if l.peek::<kw::post_return>()? {
507 parser.parse::<kw::post_return>()?;
508 Ok(CanonOpt::PostReturn(
509 parser.parse::<IndexOrCoreRef<'_, _>>()?.0,
510 ))
511 } else {
512 Err(l.error())
513 }
514 })
515 } else {
516 Err(l.error())
517 }
518 }
519 }
520
521 fn parse_trailing_item_ref<T>(kind: T, parser: Parser) -> Result<CoreItemRef<T>> {
522 Ok(CoreItemRef {
523 kind,
524 idx: parser.parse()?,
525 export_name: parser.parse()?,
526 })
527 }
528
529 impl<'a> Parse<'a> for Vec<CanonOpt<'a>> {
530 fn parse(parser: Parser<'a>) -> Result<Self> {
531 let mut funcs = Vec::new();
532 while !parser.is_empty() {
533 funcs.push(parser.parse()?);
534 }
535 Ok(funcs)
536 }
537 }