12b74dd91SManos Pitsidianakis // Copyright 2024, Linaro Limited
22b74dd91SManos Pitsidianakis // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
32b74dd91SManos Pitsidianakis // SPDX-License-Identifier: GPL-2.0-or-later
42b74dd91SManos Pitsidianakis
52b74dd91SManos Pitsidianakis use proc_macro::TokenStream;
6*f3518400SJunjie Mao use proc_macro2::Span;
7*f3518400SJunjie Mao use quote::{quote, quote_spanned};
8*f3518400SJunjie Mao use syn::{
9*f3518400SJunjie Mao parse_macro_input, parse_quote, punctuated::Punctuated, token::Comma, Data, DeriveInput, Field,
10*f3518400SJunjie Mao Fields, Ident, Type, Visibility,
11*f3518400SJunjie Mao };
12*f3518400SJunjie Mao
13*f3518400SJunjie Mao struct CompileError(String, Span);
14*f3518400SJunjie Mao
15*f3518400SJunjie Mao impl From<CompileError> for proc_macro2::TokenStream {
from(err: CompileError) -> Self16*f3518400SJunjie Mao fn from(err: CompileError) -> Self {
17*f3518400SJunjie Mao let CompileError(msg, span) = err;
18*f3518400SJunjie Mao quote_spanned! { span => compile_error!(#msg); }
19*f3518400SJunjie Mao }
20*f3518400SJunjie Mao }
21*f3518400SJunjie Mao
is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), CompileError>22*f3518400SJunjie Mao fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), CompileError> {
23*f3518400SJunjie Mao let expected = parse_quote! { #[repr(C)] };
24*f3518400SJunjie Mao
25*f3518400SJunjie Mao if input.attrs.iter().any(|attr| attr == &expected) {
26*f3518400SJunjie Mao Ok(())
27*f3518400SJunjie Mao } else {
28*f3518400SJunjie Mao Err(CompileError(
29*f3518400SJunjie Mao format!("#[repr(C)] required for {}", msg),
30*f3518400SJunjie Mao input.ident.span(),
31*f3518400SJunjie Mao ))
32*f3518400SJunjie Mao }
33*f3518400SJunjie Mao }
342b74dd91SManos Pitsidianakis
352b74dd91SManos Pitsidianakis #[proc_macro_derive(Object)]
derive_object(input: TokenStream) -> TokenStream362b74dd91SManos Pitsidianakis pub fn derive_object(input: TokenStream) -> TokenStream {
372b74dd91SManos Pitsidianakis let input = parse_macro_input!(input as DeriveInput);
382b74dd91SManos Pitsidianakis let name = input.ident;
392b74dd91SManos Pitsidianakis
402b74dd91SManos Pitsidianakis let expanded = quote! {
41e90d4707SPaolo Bonzini ::qemu_api::module_init! {
42e90d4707SPaolo Bonzini MODULE_INIT_QOM => unsafe {
432b74dd91SManos Pitsidianakis ::qemu_api::bindings::type_register_static(&<#name as ::qemu_api::definitions::ObjectImpl>::TYPE_INFO);
442b74dd91SManos Pitsidianakis }
452b74dd91SManos Pitsidianakis }
462b74dd91SManos Pitsidianakis };
472b74dd91SManos Pitsidianakis
482b74dd91SManos Pitsidianakis TokenStream::from(expanded)
492b74dd91SManos Pitsidianakis }
50*f3518400SJunjie Mao
get_fields(input: &DeriveInput) -> Result<&Punctuated<Field, Comma>, CompileError>51*f3518400SJunjie Mao fn get_fields(input: &DeriveInput) -> Result<&Punctuated<Field, Comma>, CompileError> {
52*f3518400SJunjie Mao if let Data::Struct(s) = &input.data {
53*f3518400SJunjie Mao if let Fields::Named(fs) = &s.fields {
54*f3518400SJunjie Mao Ok(&fs.named)
55*f3518400SJunjie Mao } else {
56*f3518400SJunjie Mao Err(CompileError(
57*f3518400SJunjie Mao "Cannot generate offsets for unnamed fields.".to_string(),
58*f3518400SJunjie Mao input.ident.span(),
59*f3518400SJunjie Mao ))
60*f3518400SJunjie Mao }
61*f3518400SJunjie Mao } else {
62*f3518400SJunjie Mao Err(CompileError(
63*f3518400SJunjie Mao "Cannot generate offsets for union or enum.".to_string(),
64*f3518400SJunjie Mao input.ident.span(),
65*f3518400SJunjie Mao ))
66*f3518400SJunjie Mao }
67*f3518400SJunjie Mao }
68*f3518400SJunjie Mao
69*f3518400SJunjie Mao #[rustfmt::skip::macros(quote)]
derive_offsets_or_error(input: DeriveInput) -> Result<proc_macro2::TokenStream, CompileError>70*f3518400SJunjie Mao fn derive_offsets_or_error(input: DeriveInput) -> Result<proc_macro2::TokenStream, CompileError> {
71*f3518400SJunjie Mao is_c_repr(&input, "#[derive(offsets)]")?;
72*f3518400SJunjie Mao
73*f3518400SJunjie Mao let name = &input.ident;
74*f3518400SJunjie Mao let fields = get_fields(&input)?;
75*f3518400SJunjie Mao let field_names: Vec<&Ident> = fields.iter().map(|f| f.ident.as_ref().unwrap()).collect();
76*f3518400SJunjie Mao let field_types: Vec<&Type> = fields.iter().map(|f| &f.ty).collect();
77*f3518400SJunjie Mao let field_vis: Vec<&Visibility> = fields.iter().map(|f| &f.vis).collect();
78*f3518400SJunjie Mao
79*f3518400SJunjie Mao Ok(quote! {
80*f3518400SJunjie Mao ::qemu_api::with_offsets! {
81*f3518400SJunjie Mao struct #name {
82*f3518400SJunjie Mao #(#field_vis #field_names: #field_types,)*
83*f3518400SJunjie Mao }
84*f3518400SJunjie Mao }
85*f3518400SJunjie Mao })
86*f3518400SJunjie Mao }
87*f3518400SJunjie Mao
88*f3518400SJunjie Mao #[proc_macro_derive(offsets)]
derive_offsets(input: TokenStream) -> TokenStream89*f3518400SJunjie Mao pub fn derive_offsets(input: TokenStream) -> TokenStream {
90*f3518400SJunjie Mao let input = parse_macro_input!(input as DeriveInput);
91*f3518400SJunjie Mao let expanded = derive_offsets_or_error(input).unwrap_or_else(Into::into);
92*f3518400SJunjie Mao
93*f3518400SJunjie Mao TokenStream::from(expanded)
94*f3518400SJunjie Mao }
95