[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 06/38] rust: macros: check that the first field of a #[derive(Obje
From: |
Paolo Bonzini |
Subject: |
[PULL 06/38] rust: macros: check that the first field of a #[derive(Object)] struct is a ParentField |
Date: |
Fri, 10 Jan 2025 19:45:47 +0100 |
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/qemu-api-macros/src/lib.rs | 46 +++++++++++++++++++--------------
1 file changed, 27 insertions(+), 19 deletions(-)
diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib.rs
index 160b283d7fd..0f04cca3841 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)] };
@@ -36,7 +57,12 @@ fn derive_object_or_error(input: DeriveInput) ->
Result<proc_macro2::TokenStream
is_c_repr(&input, "#[derive(Object)]")?;
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>);
+
::qemu_api::module_init! {
MODULE_INIT_QOM => unsafe {
::qemu_api::bindings::type_register_static(&<#name as
::qemu_api::qom::ObjectImpl>::TYPE_INFO);
@@ -53,30 +79,12 @@ pub fn derive_object(input: TokenStream) -> TokenStream {
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(),
- ))
- }
-}
-
#[rustfmt::skip::macros(quote)]
fn derive_offsets_or_error(input: DeriveInput) ->
Result<proc_macro2::TokenStream, CompileError> {
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
- [PULL 14/38] rust: hide warnings for subprojects, (continued)
- [PULL 14/38] rust: hide warnings for subprojects, Paolo Bonzini, 2025/01/10
- [PULL 02/38] rust: add --check-cfg test to rustc arguments, Paolo Bonzini, 2025/01/10
- [PULL 04/38] rust: add a utility module for compile-time type checks, Paolo Bonzini, 2025/01/10
- [PULL 16/38] make-release: only leave tarball of wrap-file subprojects, Paolo Bonzini, 2025/01/10
- [PULL 15/38] qom: remove unused field, Paolo Bonzini, 2025/01/10
- [PULL 03/38] rust: qom: add ParentField, Paolo Bonzini, 2025/01/10
- [PULL 10/38] rust: qom: make INSTANCE_POST_INIT take a shared reference, Paolo Bonzini, 2025/01/10
- [PULL 17/38] target/i386: improve code generation for BT, Paolo Bonzini, 2025/01/10
- [PULL 06/38] rust: macros: check that the first field of a #[derive(Object)] struct is a ParentField,
Paolo Bonzini <=
- [PULL 05/38] rust: macros: check that #[derive(Object)] requires #[repr(C)], Paolo Bonzini, 2025/01/10
- [PULL 11/38] rust: qemu-api-macros: extend error reporting facility to parse errors, Paolo Bonzini, 2025/01/10
- [PULL 13/38] rust: qdev: expose inherited methods to subclasses of SysBusDevice, Paolo Bonzini, 2025/01/10
- [PULL 21/38] target/i386/kvm: Remove local MSR_KVM_WALL_CLOCK and MSR_KVM_SYSTEM_TIME definitions, Paolo Bonzini, 2025/01/10
- [PULL 24/38] target/i386/confidential-guest: Fix comment of x86_confidential_guest_kvm_type(), Paolo Bonzini, 2025/01/10
- [PULL 33/38] i386/topology: Introduce helpers for various topology info of different level, Paolo Bonzini, 2025/01/10
- [PULL 30/38] i386/cpu: Drop the variable smp_cores and smp_threads in x86_cpu_pre_plug(), Paolo Bonzini, 2025/01/10
- [PULL 34/38] i386/cpu: Track a X86CPUTopoInfo directly in CPUX86State, Paolo Bonzini, 2025/01/10
- [PULL 28/38] target/i386/kvm: Replace ARRAY_SIZE(msr_handlers) with KVM_MSR_FILTER_MAX_RANGES, Paolo Bonzini, 2025/01/10
- [PULL 29/38] i386/cpu: Extract a common fucntion to setup value of MSR_CORE_THREAD_COUNT, Paolo Bonzini, 2025/01/10