abi-vtable-macro 0.1.0

Proc-macro generating #[repr(C)] C vtables (struct + thunks + static + getter) from Rust trait definitions, for the dyn-loader abi mode
Documentation
//! # abi-vtable-macro — shadow-mode vtable generation
//!
//! **Shadow mode**: annotate a trait and every companion entity of the
//! abi-mode (cdyn) protocol is generated automatically — no manual steps.
//! Usage is as frictionless as the native (dyn) mode.
//!
//! ## When to use which mode
//! - **Rust-only** → native (dyn) mode: fastest and lightest, direct fat
//!   pointers, Arc ref-counting, no vtable struct at all.
//! - **Cross-language (C/C++/Zig/…)** → this (abi/cdyn) mode: the generated
//!   `#[repr(C)]` vtable is the language-neutral contract.

use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{parse_macro_input, ItemTrait, TraitItem};

/// Map a Rust type to its C-representable counterpart.
/// Only types with a stable C representation are supported.
fn map_type(ty: &syn::Type) -> Option<&'static str> {
    let ty_str = quote!(#ty).to_string().replace(' ', "");
    Some(match ty_str.as_str() {
        "i8" => "i8",
        "i16" => "i16",
        "i32" => "i32",
        "i64" => "i64",
        "u8" => "u8",
        "u16" => "u16",
        "u32" => "u32",
        "u64" => "u64",
        "f32" => "f32",
        "f64" => "f64",
        "bool" => "bool",
        "usize" => "usize",
        "isize" => "isize",
        _ => return None,
    })
}

struct AttrOpts {
    /// Export prefix, e.g. "calc" → `calc_get_vtable`, `CALC_VTABLE`.
    name: String,
}

fn parse_attr_tokens(ts: proc_macro2::TokenStream) -> AttrOpts {
    let mut name = String::new();
    let mut iter = ts.into_iter().peekable();
    while let Some(t) = iter.next() {
        if let proc_macro2::TokenTree::Ident(id) = t {
            if id == "name" {
                for t2 in iter.by_ref() {
                    if let proc_macro2::TokenTree::Literal(lit) = t2 {
                        name = lit.to_string().trim_matches('"').to_string();
                        break;
                    }
                }
            }
        }
    }
    if name.is_empty() {
        panic!("#[abi_vtable] requires `name = \"...\"` (getter symbol prefix)");
    }
    AttrOpts { name }
}

#[proc_macro_attribute]
pub fn abi_vtable(attr: TokenStream, item: TokenStream) -> TokenStream {
    let opts = parse_attr_tokens(attr.into());
    let trait_def = parse_macro_input!(item as ItemTrait);
    let trait_name = &trait_def.ident;
    let trait_name_str = trait_name.to_string();
    let vis = &trait_def.vis;

    // ---- collect &self methods with C-representable signatures ----
    struct Method {
        name: syn::Ident,
        params: Vec<proc_macro2::TokenStream>, // "id: CTy"
        ret: Option<syn::Type>,
        call_args: Vec<syn::Ident>,
    }

    let mut methods: Vec<Method> = Vec::new();
    for item in &trait_def.items {
        if let TraitItem::Fn(m) = item {
            let mut params = Vec::new();
            let mut call_args = Vec::new();
            for input in &m.sig.inputs {
                if let syn::FnArg::Typed(pt) = input {
                    let ident = match pt.pat.as_ref() {
                        syn::Pat::Ident(pi) => pi.ident.clone(),
                        _ => continue,
                    };
                    let ty_str = quote!(#pt.ty).to_string().replace(' ', "");
                    let c_ty = map_type(&pt.ty).unwrap_or_else(|| {
                        panic!(
                            "abi_vtable: unsupported param type `{}` in `{}::{}` — only C-representable scalars cross the boundary",
                            ty_str, trait_name_str, m.sig.ident
                        )
                    });
                    let c_ty: syn::Type = syn::parse_str(c_ty).unwrap();
                    params.push(quote!(#ident: #c_ty));
                    call_args.push(ident);
                }
            }
            let ret = match &m.sig.output {
                syn::ReturnType::Type(_, ty) => {
                    let ty_str = quote!(#ty).to_string().replace(' ', "");
                    let c_ty = map_type(ty).unwrap_or_else(|| {
                        panic!(
                            "abi_vtable: unsupported return type `{}` in `{}::{}`",
                            ty_str, trait_name_str, m.sig.ident
                        )
                    });
                    Some(syn::parse_str::<syn::Type>(c_ty).unwrap())
                }
                syn::ReturnType::Default => None,
            };
            methods.push(Method {
                name: m.sig.ident.clone(),
                params,
                ret,
                call_args,
            });
        }
    }

    // ---- shadow entities ----
    let vtable_struct_name = format_ident!("{}Vtable", trait_name);

    let mut struct_fields = Vec::new();
    // Fully-materialized thunk fns (one token blob per method), consumed by
    // the generated macro_rules! via a flat single-level repetition.
    let mut thunks = Vec::new();
    let mut thunk_assignments = Vec::new();

    for m in &methods {
        let fname = &m.name;
        let thunk_name = format_ident!("{}_thunk", m.name);
        let arg_tys = &m.params;
        let ret = match &m.ret {
            Some(t) => quote!(-> #t),
            None => quote!(),
        };

        struct_fields.push(quote! {
            pub #fname: unsafe extern "C" fn(ctx: *mut ::std::ffi::c_void, #(#arg_tys),*) #ret
        });

        let call_args = &m.call_args;
        // NOTE: no generics here — `$impl_ty` is substituted by the generated
        // macro_rules! at expansion time, so thunks are monomorphic by design.
        thunks.push(quote! {
            unsafe extern "C" fn #thunk_name(
                ctx: *mut ::std::ffi::c_void,
                #(#arg_tys),*
            ) #ret {
                unsafe { (&*(ctx as *const $impl_ty)).#fname(#(#call_args),*) }
            }
        });
        thunk_assignments.push(quote! { #fname: #thunk_name });
    }

    let static_name = format_ident!("{}_VTABLE", opts.name.to_uppercase());
    let getter_name = format_ident!("{}_get_vtable", opts.name);
    let instance_name = format_ident!("{}_INSTANCE", opts.name.to_uppercase());
    let impl_macro = format_ident!("abi_vtable_impl_{}", trait_name_str.to_lowercase());

    let expanded = quote! {
        #trait_def

        /// Shadow vtable generated from [`#trait_name`] — the C contract.
        /// Field order IS the protocol; never reorder after release.
        #[repr(C)]
        #[derive(Clone, Copy)]
        #vis struct #vtable_struct_name {
            #(#struct_fields,)*
        }

        /// One-line binding: impl type → static instance + vtable + getter.
        ///
        /// `$impl_ty` must implement [`#trait_name`]; `$instance` is a
        /// const-constructible expression (e.g. `MyCalc`).
        #[macro_export]
        macro_rules! #impl_macro {
            // (ImplType, instance_expr)
            ($impl_ty:ty, $instance:expr) => {
                static #instance_name: $impl_ty = $instance;

                // Concrete thunks — one per method, bound to $impl_ty.
                #(
                    #thunks
                )*

                static #static_name: #vtable_struct_name = #vtable_struct_name {
                    #(#thunk_assignments,)*
                };

                #[no_mangle]
                pub extern "C" fn #getter_name() -> *const #vtable_struct_name {
                    &#static_name
                }
            };
        }
    };

    TokenStream::from(expanded)
}