wiggle_generate/types/
mod.rs1mod error;
3mod flags;
4mod handle;
5mod record;
6mod variant;
7
8use crate::codegen_settings::ErrorType;
9use crate::lifetimes::LifetimeExt;
10use crate::names;
11
12use proc_macro2::TokenStream;
13use quote::quote;
14
15pub fn define_datatype(namedtype: &witx::NamedType, error: Option<&ErrorType>) -> TokenStream {
16 match &namedtype.tref {
17 witx::TypeRef::Name(alias_to) => define_alias(&namedtype.name, &alias_to),
18 witx::TypeRef::Value(v) => match &**v {
19 witx::Type::Record(r) => match r.bitflags_repr() {
20 Some(repr) => flags::define_flags(&namedtype.name, repr, &r),
21 None => record::define_struct(&namedtype.name, &r),
22 },
23 witx::Type::Variant(v) => match error {
24 Some(ErrorType::Generated(error)) => {
25 let d = variant::define_variant(&namedtype.name, &v, true);
26 let e = error::define_error(&namedtype.name, &v, error);
27 quote!( #d #e )
28 }
29 _ => variant::define_variant(&namedtype.name, &v, false),
30 },
31 witx::Type::Handle(h) => handle::define_handle(&namedtype.name, &h),
32 witx::Type::Builtin(b) => define_builtin(&namedtype.name, *b),
33 witx::Type::Pointer(p) => {
34 define_witx_pointer(&namedtype.name, quote!(wiggle::GuestPtr), p)
35 }
36 witx::Type::ConstPointer(p) => {
37 define_witx_pointer(&namedtype.name, quote!(wiggle::GuestPtr), p)
38 }
39 witx::Type::List(arr) => define_witx_list(&namedtype.name, &arr),
40 },
41 }
42}
43
44fn define_alias(name: &witx::Id, to: &witx::NamedType) -> TokenStream {
45 let ident = names::type_(name);
46 let rhs = names::type_(&to.name);
47 if to.tref.needs_lifetime() {
48 quote!(pub type #ident<'a> = #rhs<'a>;)
49 } else {
50 quote!(pub type #ident = #rhs;)
51 }
52}
53
54fn define_builtin(name: &witx::Id, builtin: witx::BuiltinType) -> TokenStream {
55 let ident = names::type_(name);
56 let built = names::builtin_type(builtin);
57 quote!(pub type #ident = #built;)
58}
59
60fn define_witx_pointer(
61 name: &witx::Id,
62 pointer_type: TokenStream,
63 pointee: &witx::TypeRef,
64) -> TokenStream {
65 let ident = names::type_(name);
66 let pointee_type = names::type_ref(pointee, quote!('a));
67
68 quote!(pub type #ident<'a> = #pointer_type<'a, #pointee_type>;)
69}
70
71fn define_witx_list(name: &witx::Id, arr_raw: &witx::TypeRef) -> TokenStream {
72 let ident = names::type_(name);
73 let pointee_type = names::type_ref(arr_raw, quote!());
74 quote!(pub type #ident = wiggle::GuestPtr<[#pointee_type]>;)
75}
76
77pub fn int_repr_tokens(int_repr: witx::IntRepr) -> TokenStream {
78 match int_repr {
79 witx::IntRepr::U8 => quote!(u8),
80 witx::IntRepr::U16 => quote!(u16),
81 witx::IntRepr::U32 => quote!(u32),
82 witx::IntRepr::U64 => quote!(u64),
83 }
84}
85
86pub trait WiggleType {
87 fn impls_display(&self) -> bool;
88}
89
90impl WiggleType for witx::TypeRef {
91 fn impls_display(&self) -> bool {
92 match self {
93 witx::TypeRef::Name(alias_to) => (&*alias_to).impls_display(),
94 witx::TypeRef::Value(v) => (&*v).impls_display(),
95 }
96 }
97}
98
99impl WiggleType for witx::NamedType {
100 fn impls_display(&self) -> bool {
101 self.tref.impls_display()
102 }
103}
104
105impl WiggleType for witx::Type {
106 fn impls_display(&self) -> bool {
107 match self {
108 witx::Type::Record(x) => x.impls_display(),
109 witx::Type::Variant(x) => x.impls_display(),
110 witx::Type::Handle(x) => x.impls_display(),
111 witx::Type::Builtin(x) => x.impls_display(),
112 witx::Type::Pointer { .. }
113 | witx::Type::ConstPointer { .. }
114 | witx::Type::List { .. } => false,
115 }
116 }
117}
118
119impl WiggleType for witx::BuiltinType {
120 fn impls_display(&self) -> bool {
121 true
122 }
123}
124
125impl WiggleType for witx::InterfaceFuncParam {
126 fn impls_display(&self) -> bool {
127 self.tref.impls_display()
128 }
129}