[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 03/10] rust: macros: check that the first field of a #[derive(Obj
From: |
Paolo Bonzini |
Subject: |
[PATCH 03/10] rust: macros: check that the first field of a #[derive(Object)] struct is a ParentField |
Date: |
Thu, 19 Dec 2024 13:12:09 +0100 |
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/qemu-api-macros/src/lib.rs | 61 +++++++++++++++++++--------------
1 file changed, 36 insertions(+), 25 deletions(-)
diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib.rs
index 74a8bc7503e..778b698be45 100644
--- a/rust/qemu-api-macros/src/lib.rs
+++ b/rust/qemu-api-macros/src/lib.rs
@@ -19,6 +19,27 @@ fn from(err: CompileError) -> Self {
}
}
+fn get_fields<'a>(
+ input: &'a DeriveInput,
+ msg: &str,
+) -> Result<&'a Punctuated<Field, Comma>, CompileError> {
+ if let Data::Struct(s) = &input.data {
+ if let Fields::Named(fs) = &s.fields {
+ Ok(&fs.named)
+ } else {
+ Err(CompileError(
+ format!("Named fields required for {}", msg),
+ input.ident.span(),
+ ))
+ }
+ } else {
+ Err(CompileError(
+ format!("Struct required for {}", msg),
+ input.ident.span(),
+ ))
+ }
+}
+
fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), CompileError> {
let expected = parse_quote! { #[repr(C)] };
@@ -32,38 +53,28 @@ fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(),
CompileError> {
}
}
-#[proc_macro_derive(Object)]
-pub fn derive_object(input: TokenStream) -> TokenStream {
- let input = parse_macro_input!(input as DeriveInput);
- let name = input.ident;
+fn derive_object_or_error(input: DeriveInput) ->
Result<proc_macro2::TokenStream, CompileError> {
+ let name = &input.ident;
+ let parent = &get_fields(&input, "#[derive(Object)]")?[0].ident;
+
+ Ok(quote! {
+ ::qemu_api::assert_field_type!(#name, #parent,
+ ::qemu_api::qom::ParentField<<#name as
::qemu_api::qom::ObjectImpl>::ParentType>);
- let expanded = quote! {
::qemu_api::module_init! {
MODULE_INIT_QOM => unsafe {
::qemu_api::bindings::type_register_static(&<#name as
::qemu_api::qom::ObjectImpl>::TYPE_INFO);
}
}
- };
-
- TokenStream::from(expanded)
+ })
}
-fn get_fields(input: &DeriveInput) -> Result<&Punctuated<Field, Comma>,
CompileError> {
- if let Data::Struct(s) = &input.data {
- if let Fields::Named(fs) = &s.fields {
- Ok(&fs.named)
- } else {
- Err(CompileError(
- "Cannot generate offsets for unnamed fields.".to_string(),
- input.ident.span(),
- ))
- }
- } else {
- Err(CompileError(
- "Cannot generate offsets for union or enum.".to_string(),
- input.ident.span(),
- ))
- }
+#[proc_macro_derive(Object)]
+pub fn derive_object(input: TokenStream) -> TokenStream {
+ let input = parse_macro_input!(input as DeriveInput);
+ let expanded = derive_object_or_error(input).unwrap_or_else(Into::into);
+
+ TokenStream::from(expanded)
}
#[rustfmt::skip::macros(quote)]
@@ -71,7 +82,7 @@ fn derive_offsets_or_error(input: DeriveInput) ->
Result<proc_macro2::TokenStrea
is_c_repr(&input, "#[derive(offsets)]")?;
let name = &input.ident;
- let fields = get_fields(&input)?;
+ let fields = get_fields(&input, "#[derive(offsets)]")?;
let field_names: Vec<&Ident> = fields.iter().map(|f|
f.ident.as_ref().unwrap()).collect();
let field_types: Vec<&Type> = fields.iter().map(|f| &f.ty).collect();
let field_vis: Vec<&Visibility> = fields.iter().map(|f| &f.vis).collect();
--
2.47.1
- [PATCH 00/10] Next round of qemu_api patches, Paolo Bonzini, 2024/12/19
- [PATCH 07/10] rust: pl011: only leave embedded object initialization in instance_init, Paolo Bonzini, 2024/12/19
- [PATCH 01/10] rust: qom: add ParentField, Paolo Bonzini, 2024/12/19
- [PATCH 03/10] rust: macros: check that the first field of a #[derive(Object)] struct is a ParentField,
Paolo Bonzini <=
- [PATCH 06/10] rust: qom: move device_id to PL011 class side, Paolo Bonzini, 2024/12/19
- [PATCH 04/10] rust: macros: check that #[derive(Object)] requires #[repr(C)], Paolo Bonzini, 2024/12/19
- [PATCH 09/10] rust: qemu-api-macros: extend error reporting facility to parse errors, Paolo Bonzini, 2024/12/19
- [PATCH 05/10] rust: qom: automatically use Drop trait to implement instance_finalize, Paolo Bonzini, 2024/12/19
- [PATCH 02/10] rust: add a utility module for compile-time type checks, Paolo Bonzini, 2024/12/19
- [PATCH 10/10] rust: qemu-api-macros: add automatic TryFrom/TryInto derivation, Paolo Bonzini, 2024/12/19
- [PATCH 08/10] rust: qom: make INSTANCE_POST_INIT take a shared reference, Paolo Bonzini, 2024/12/19