1 // mod r#enum;
2 mod error;
3 mod flags;
4 mod handle;
5 mod record;
6 mod variant;
7
8 use crate::codegen_settings::ErrorType;
9 use crate::lifetimes::LifetimeExt;
10 use crate::names;
11
12 use proc_macro2::TokenStream;
13 use quote::quote;
14
define_datatype(namedtype: &witx::NamedType, error: Option<&ErrorType>) -> TokenStream15 pub 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
define_alias(name: &witx::Id, to: &witx::NamedType) -> TokenStream44 fn 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
define_builtin(name: &witx::Id, builtin: witx::BuiltinType) -> TokenStream54 fn 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
define_witx_pointer( name: &witx::Id, pointer_type: TokenStream, pointee: &witx::TypeRef, ) -> TokenStream60 fn 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
define_witx_list(name: &witx::Id, arr_raw: &witx::TypeRef) -> TokenStream71 fn 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
int_repr_tokens(int_repr: witx::IntRepr) -> TokenStream77 pub 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
86 pub trait WiggleType {
impls_display(&self) -> bool87 fn impls_display(&self) -> bool;
88 }
89
90 impl WiggleType for witx::TypeRef {
impls_display(&self) -> bool91 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
99 impl WiggleType for witx::NamedType {
impls_display(&self) -> bool100 fn impls_display(&self) -> bool {
101 self.tref.impls_display()
102 }
103 }
104
105 impl WiggleType for witx::Type {
impls_display(&self) -> bool106 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
119 impl WiggleType for witx::BuiltinType {
impls_display(&self) -> bool120 fn impls_display(&self) -> bool {
121 true
122 }
123 }
124
125 impl WiggleType for witx::InterfaceFuncParam {
impls_display(&self) -> bool126 fn impls_display(&self) -> bool {
127 self.tref.impls_display()
128 }
129 }
130