1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
mod r#enum;
mod flags;
mod handle;
mod int;
mod r#struct;
mod union;

use crate::lifetimes::LifetimeExt;
use crate::names::Names;

use proc_macro2::TokenStream;
use quote::quote;

pub fn define_datatype(names: &Names, namedtype: &witx::NamedType) -> TokenStream {
    match &namedtype.tref {
        witx::TypeRef::Name(alias_to) => define_alias(names, &namedtype.name, &alias_to),
        witx::TypeRef::Value(v) => match &**v {
            witx::Type::Enum(e) => r#enum::define_enum(names, &namedtype.name, &e),
            witx::Type::Int(i) => int::define_int(names, &namedtype.name, &i),
            witx::Type::Flags(f) => flags::define_flags(names, &namedtype.name, &f),
            witx::Type::Struct(s) => r#struct::define_struct(names, &namedtype.name, &s),
            witx::Type::Union(u) => union::define_union(names, &namedtype.name, &u),
            witx::Type::Handle(h) => handle::define_handle(names, &namedtype.name, &h),
            witx::Type::Builtin(b) => define_builtin(names, &namedtype.name, *b),
            witx::Type::Pointer(p) => {
                let rt = names.runtime_mod();
                define_witx_pointer(names, &namedtype.name, quote!(#rt::GuestPtr), p)
            }
            witx::Type::ConstPointer(p) => {
                let rt = names.runtime_mod();
                define_witx_pointer(names, &namedtype.name, quote!(#rt::GuestPtr), p)
            }
            witx::Type::Array(arr) => define_witx_array(names, &namedtype.name, &arr),
        },
    }
}

fn define_alias(names: &Names, name: &witx::Id, to: &witx::NamedType) -> TokenStream {
    let ident = names.type_(name);
    let rhs = names.type_(&to.name);
    if to.tref.needs_lifetime() {
        quote!(pub type #ident<'a> = #rhs<'a>;)
    } else {
        quote!(pub type #ident = #rhs;)
    }
}

fn define_builtin(names: &Names, name: &witx::Id, builtin: witx::BuiltinType) -> TokenStream {
    let ident = names.type_(name);
    let built = names.builtin_type(builtin, quote!('a));
    if builtin.needs_lifetime() {
        quote!(pub type #ident<'a> = #built;)
    } else {
        quote!(pub type #ident = #built;)
    }
}

fn define_witx_pointer(
    names: &Names,
    name: &witx::Id,
    pointer_type: TokenStream,
    pointee: &witx::TypeRef,
) -> TokenStream {
    let ident = names.type_(name);
    let pointee_type = names.type_ref(pointee, quote!('a));

    quote!(pub type #ident<'a> = #pointer_type<'a, #pointee_type>;)
}

fn define_witx_array(names: &Names, name: &witx::Id, arr_raw: &witx::TypeRef) -> TokenStream {
    let ident = names.type_(name);
    let rt = names.runtime_mod();
    let pointee_type = names.type_ref(arr_raw, quote!('a));
    quote!(pub type #ident<'a> = #rt::GuestPtr<'a, [#pointee_type]>;)
}

fn int_repr_tokens(int_repr: witx::IntRepr) -> TokenStream {
    match int_repr {
        witx::IntRepr::U8 => quote!(u8),
        witx::IntRepr::U16 => quote!(u16),
        witx::IntRepr::U32 => quote!(u32),
        witx::IntRepr::U64 => quote!(u64),
    }
}

fn atom_token(atom: witx::AtomType) -> TokenStream {
    match atom {
        witx::AtomType::I32 => quote!(i32),
        witx::AtomType::I64 => quote!(i64),
        witx::AtomType::F32 => quote!(f32),
        witx::AtomType::F64 => quote!(f64),
    }
}

pub trait WiggleType {
    fn impls_display(&self) -> bool;
}

impl WiggleType for witx::TypeRef {
    fn impls_display(&self) -> bool {
        match self {
            witx::TypeRef::Name(alias_to) => (&*alias_to).impls_display(),
            witx::TypeRef::Value(v) => (&*v).impls_display(),
        }
    }
}

impl WiggleType for witx::NamedType {
    fn impls_display(&self) -> bool {
        self.tref.impls_display()
    }
}

impl WiggleType for witx::Type {
    fn impls_display(&self) -> bool {
        match self {
            witx::Type::Enum(x) => x.impls_display(),
            witx::Type::Int(x) => x.impls_display(),
            witx::Type::Flags(x) => x.impls_display(),
            witx::Type::Struct(x) => x.impls_display(),
            witx::Type::Union(x) => x.impls_display(),
            witx::Type::Handle(x) => x.impls_display(),
            witx::Type::Builtin(x) => x.impls_display(),
            witx::Type::Pointer { .. }
            | witx::Type::ConstPointer { .. }
            | witx::Type::Array { .. } => false,
        }
    }
}

impl WiggleType for witx::BuiltinType {
    fn impls_display(&self) -> bool {
        true
    }
}

impl WiggleType for witx::InterfaceFuncParam {
    fn impls_display(&self) -> bool {
        match &*self.tref.type_() {
            witx::Type::Struct { .. }
            | witx::Type::Union { .. }
            | witx::Type::Builtin(witx::BuiltinType::String { .. }) => false,
            _ => self.tref.impls_display(),
        }
    }
}