wasmer_interface_types/ast.rs
1//! Represents the WIT language as a tree. This is the central
2//! representation of the language.
3
4use crate::{
5 interpreter::Instruction,
6 types::{InterfaceType, RecordType},
7};
8use std::str;
9
10/// Represents the kind of type.
11#[derive(PartialEq, Debug)]
12pub enum TypeKind {
13 /// A function type.
14 Function,
15
16 /// A record type.
17 Record,
18}
19
20/// Represents a type.
21#[derive(PartialEq, Debug)]
22pub enum Type {
23 /// A function type, like:
24 ///
25 /// ```wasm,ignore
26 /// (@interface type (func (param i32 i32) (result string)))
27 /// ```
28 Function {
29 /// Types for the parameters (`(param …)`).
30 inputs: Vec<InterfaceType>,
31
32 /// Types for the results (`(result …)`).
33 outputs: Vec<InterfaceType>,
34 },
35
36 /// A record type, like:
37 ///
38 /// ```wasm,ignore
39 /// (@interface type (record string i32))
40 /// ```
41 Record(RecordType),
42}
43
44/// Represents an imported function.
45#[derive(PartialEq, Debug)]
46pub struct Import<'input> {
47 /// The function namespace.
48 pub namespace: &'input str,
49
50 /// The function name.
51 pub name: &'input str,
52
53 /// The type signature.
54 pub signature_type: u32,
55}
56
57/// Represents an exported function signature.
58#[derive(PartialEq, Debug)]
59pub struct Export<'input> {
60 /// The export name.
61 pub name: &'input str,
62
63 /// The WIT function type being exported.
64 pub function_type: u32,
65}
66
67/// Represents an adapter.
68#[derive(PartialEq, Debug)]
69pub struct Adapter {
70 /// The adapter function type.
71 pub function_type: u32,
72
73 /// The instructions.
74 pub instructions: Vec<Instruction>,
75}
76
77/// Represents an implementation.
78#[derive(PartialEq, Debug)]
79pub struct Implementation {
80 /// The core function type.
81 pub core_function_type: u32,
82
83 /// The adapter function type.
84 pub adapter_function_type: u32,
85}
86
87/// Represents the kind of interface.
88#[derive(PartialEq, Debug)]
89pub(crate) enum InterfaceKind {
90 /// A type.
91 Type,
92
93 /// An imported function.
94 Import,
95
96 /// An adapter.
97 Adapter,
98
99 /// An exported function.
100 Export,
101
102 /// An implementation.
103 Implementation,
104}
105
106/// Represents a set of interfaces, i.e. it entirely describes a WIT
107/// definition.
108#[derive(PartialEq, Default, Debug)]
109pub struct Interfaces<'input> {
110 /// All the types.
111 pub types: Vec<Type>,
112
113 /// All the imported functions.
114 pub imports: Vec<Import<'input>>,
115
116 /// All the adapters.
117 pub adapters: Vec<Adapter>,
118
119 /// All the exported functions.
120 pub exports: Vec<Export<'input>>,
121
122 /// All the implementations.
123 pub implementations: Vec<Implementation>,
124}