Minor refactors to make wiggle-generate reusable externally (#34)

* wiggle-generate: pass witx doc in explicitly

* wiggle-generate: Names takes &Config, minor refactor for reuse
This commit is contained in:
Pat Hickey
2020-03-06 16:55:05 -08:00
committed by GitHub
parent c78416912c
commit 6e3ec6a96d
5 changed files with 20 additions and 22 deletions

View File

@@ -11,22 +11,10 @@ pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream {
let ctx_type = names.ctx_type();
let coretype = func.core_type();
let params = coretype.args.iter().map(|arg| match arg.signifies {
witx::CoreParamSignifies::Value(atom) => {
let atom = names.atom_type(atom);
let name = names.func_param(&arg.param.name);
quote!(#name : #atom)
}
witx::CoreParamSignifies::PointerTo => {
let atom = names.atom_type(witx::AtomType::I32);
let name = names.func_ptr_binding(&arg.param.name);
quote!(#name: #atom)
}
witx::CoreParamSignifies::LengthOf => {
let atom = names.atom_type(witx::AtomType::I32);
let name = names.func_len_binding(&arg.param.name);
quote!(#name: #atom)
}
let params = coretype.args.iter().map(|arg| {
let name = names.func_core_arg(arg);
let atom = names.atom_type(arg.repr());
quote!(#name : #atom)
});
let abi_args = quote!(

View File

@@ -14,9 +14,7 @@ pub use module_trait::define_module_trait;
pub use names::Names;
pub use types::define_datatype;
pub fn generate(config: Config) -> TokenStream {
let doc = witx::load(&config.witx.paths).expect("loading witx");
pub fn generate(doc: &witx::Document, config: &Config) -> TokenStream {
let names = Names::new(config); // TODO parse the names from the invocation of the macro, or from a file?
let types = doc.typenames().map(|t| define_datatype(&names, &t));

View File

@@ -12,8 +12,10 @@ pub struct Names {
}
impl Names {
pub fn new(config: Config) -> Names {
Names { config }
pub fn new(config: &Config) -> Names {
Names {
config: config.clone(),
}
}
pub fn ctx_type(&self) -> Ident {
self.config.ctx.name.clone()
@@ -118,6 +120,14 @@ impl Names {
}
}
pub fn func_core_arg(&self, arg: &witx::CoreParamType) -> Ident {
match arg.signifies {
witx::CoreParamSignifies::Value { .. } => self.func_param(&arg.param.name),
witx::CoreParamSignifies::PointerTo => self.func_ptr_binding(&arg.param.name),
witx::CoreParamSignifies::LengthOf => self.func_len_binding(&arg.param.name),
}
}
/// For when you need a {name}_ptr binding for passing a value by reference:
pub fn func_ptr_binding(&self, id: &Id) -> Ident {
format_ident!("{}_ptr", id.as_str().to_snake_case())