From: Markus Armbruster <armbru@redhat.com>
To: "Marc-André Lureau" <marcandre.lureau@redhat.com>
Cc: qemu-devel@nongnu.org, "Markus Armbruster" <armbru@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Michael Roth" <michael.roth@amd.com>,
"Pierrick Bouvier" <pierrick.bouvier@oss.qualcomm.com>,
"Philippe Mathieu-Daudé" <philmd@mailo.com>
Subject: Re: [PATCH v2 03/53] qapi: add type-infos generator
Date: Thu, 06 Aug 2026 16:05:39 +0200 [thread overview]
Message-ID: <87zeyztkcs.fsf@pond.sub.org> (raw)
In-Reply-To: <20260616-qom-qapi-v2-3-cc9396b9c18c@redhat.com> ("Marc-André Lureau"'s message of "Tue, 16 Jun 2026 00:37:29 +0400")
Marc-André Lureau <marcandre.lureau@redhat.com> writes:
> New QAPISchemaGenTypeInfoVisitor produces per-module
> qapi-type-infos-*.h/c files. Each file declares QAPITypeInfo constants
> pairing the QAPI type name with its masked introspection name.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
> docs/devel/qapi-code-gen.rst | 90 +++++++++++++++++++++
> meson.build | 1 +
> scripts/qapi/type_infos.py | 182 +++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 273 insertions(+)
>
> diff --git a/docs/devel/qapi-code-gen.rst b/docs/devel/qapi-code-gen.rst
> index 3a632b4a648..bb2e8d9c675 100644
> --- a/docs/devel/qapi-code-gen.rst
> +++ b/docs/devel/qapi-code-gen.rst
> @@ -2103,3 +2103,93 @@ Example::
> }));
>
> [Uninteresting stuff omitted...]
> +
> +
> +Code generated for type information
> +-----------------------------------
> +
> +The following files are created:
> +
> + ``$(prefix)qapi-type-infos.c``
> + A ``QAPITypeInfo`` instance for each schema-defined type, providing
> + a mapping between the C type name and the schema name used by
> + introspection, along with optional enum lookup table and list-element
> + pointers.
> +
> + ``$(prefix)qapi-type-infos.h``
> + Declarations for the above type info instances
> +
> +Each ``QAPITypeInfo`` struct has the following fields:
> +
> +``name``
> + The C identifier of the type (e.g. ``"UserDefOne"``).
Isn't this the name used in the QAPI schema source?
> +
> +``schema_name``
> + The masked name used in ``query-qmp-schema`` output, or ``NULL``
> + for built-in types whose schema name equals their C name. This
> + allows management tools to cross-reference a QOM property's
> + ``qapi-type`` against the introspection schema.
I believe it can also be null when query-qmp-schema omits the type.
More on that below.
> +
> +``lookup``
> + For enum types, a pointer to the corresponding ``QEnumLookup``
> + table. ``NULL`` for non-enum types.
> +
> +``list``
> + For types that have an array variant, a pointer to the list type's
> + ``QAPITypeInfo``. ``NULL`` when no list type exists.
Confusing mix of "array" and "list" in the same sentence. Suggest "the
array type's".
QAPI can't decide whether to call the thing an array (like JSON) or a
list (like the C implementation). Unfortunate.
docs/devel/qapi-code-gen.rst generally sticks to array, but there are a
few lapses.
> +
> +These type info instances are used by QOM property registration
> +functions (``object_property_add_qapi()``,
> +``object_class_property_add_qapi_enum()``, etc.) to associate each
> +property with its QAPI schema type.
You're using "schema type" like you do above, i.e. for the masked type
name. Correct?
> The ``qom-list`` and
> +``device-list-properties`` QMP commands then expose the ``qapi-type``
> +field, giving management tools a formal type reference into the
> +introspection schema.
As far as I understand, we connect a property of QAPI type to the type's
QAPITypeInfo, the commands you mentioned then use it to emit the masked
type name, so that management applications can use that to find a
property's type in the output of query-qmp-schema.
Problem (already discussed elsewhere): only works when the type is used
in QMP. If it isn't, we still have QAPITypeInfo, but its .schema_name
is null. This needs to be documented as a known shortcoming.
Other uses of QAPITypeInfo may emerge in the future. We might also
replace it by something that supports additional uses; see my ramblings
in review of PATCH 01. In this series, however, connecting QOM
properties to their masked QAPI type name is their only use. I
understand we emit QAPITypeInfo for any QAPI type that could be used as
QOM property type. Dead data unless it is actually used as QOM property
type. This is acceptable; we already generate similarly dead visitors.
But it's worth mentioning in the commit message, I think.
> +
> +Implicit types (names starting with ``q_``) are skipped.
This isn't quite correct. Array types are also implicitly defined, and
their names don't start with 'q_'.
Possible fixes:
* Drop the sentence (only if we believe it's not needed)
* Drop the parenthesis, add "other than arrays" or something like that.
* Positive instead of negative: only user-defined and built-in types.
This could perhaps be worked into the text under "The following files
are created". May want to add why: only these may be used as property
types.
> +
> +Example::
> +
> + $ cat qapi-generated/example-qapi-type-infos.h
> + [Uninteresting stuff omitted...]
> +
> + #ifndef EXAMPLE_QAPI_TYPE_INFOS_H
> + #define EXAMPLE_QAPI_TYPE_INFOS_H
> +
> + #include "qapi/qapi-builtin-type-infos.h"
> +
> + extern const QAPITypeInfo UserDefOne_type_info;
> +
> + extern const QAPITypeInfo UserDefOneList_type_info;
> +
> + #endif /* EXAMPLE_QAPI_TYPE_INFOS_H */
> + $ cat qapi-generated/example-qapi-type-infos.c
> + [Uninteresting stuff omitted...]
> +
> + const QAPITypeInfo UserDefOne_type_info = {
> + .name = "UserDefOne",
> + .schema_name = "1",
> + .list = &UserDefOneList_type_info,
> + };
> +
> + const QAPITypeInfo UserDefOneList_type_info = {
> + .name = "UserDefOneList",
> + .schema_name = "[1]",
> + };
> +
> + [Uninteresting stuff omitted...]
> +
> +For a modular QAPI schema (see section `Include directives`_), code for
> +each sub-module SUBDIR/SUBMODULE.json is actually generated into ::
> +
> + SUBDIR/$(prefix)qapi-type-infos-SUBMODULE.h
> + SUBDIR/$(prefix)qapi-type-infos-SUBMODULE.c
> +
> +If qapi-gen.py is run with option --builtins, additional files are
> +created:
> +
> + ``qapi-builtin-type-infos.h``
> + Type info instances for built-in types
> +
> + ``qapi-builtin-type-infos.c``
> + Definitions for the above type info instances
> diff --git a/meson.build b/meson.build
> index 19e123423b5..f135eddad12 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -3487,6 +3487,7 @@ qapi_gen_depends = [ meson.current_source_dir() / 'scripts/qapi/__init__.py',
> meson.current_source_dir() / 'scripts/qapi/parser.py',
> meson.current_source_dir() / 'scripts/qapi/schema.py',
> meson.current_source_dir() / 'scripts/qapi/source.py',
> + meson.current_source_dir() / 'scripts/qapi/type_infos.py',
> meson.current_source_dir() / 'scripts/qapi/types.py',
> meson.current_source_dir() / 'scripts/qapi/visit.py',
> meson.current_source_dir() / 'scripts/qapi-gen.py'
> diff --git a/scripts/qapi/type_infos.py b/scripts/qapi/type_infos.py
> new file mode 100644
> index 00000000000..dc7e2bace9e
> --- /dev/null
> +++ b/scripts/qapi/type_infos.py
Please use '-' instead of '_' in filenames for local consistency.
> @@ -0,0 +1,182 @@
> +"""
> +QAPI type info generator
> +
> +SPDX-License-Identifier: GPL-2.0-or-later
> +"""
> +
> +from typing import (
> + Dict,
> + List,
> + Optional,
> + Set,
> +)
> +
> +from .common import c_name, mcgen
> +from .gen import QAPISchemaModularCVisitor, ifcontext
> +from .schema import (
> + QAPISchema,
> + QAPISchemaAlternatives,
> + QAPISchemaBranches,
> + QAPISchemaEnumMember,
> + QAPISchemaFeature,
> + QAPISchemaIfCond,
> + QAPISchemaObjectType,
> + QAPISchemaObjectTypeMember,
> + QAPISchemaType,
> + QAPISchemaVisitor,
> +)
> +from .source import QAPISourceInfo
> +
> +
> +class _ArrayTypeCollector(QAPISchemaVisitor):
> + def __init__(self) -> None:
> + self.list_types: Set[str] = set()
> +
> + def visit_array_type(self,
> + name: str,
> + info: Optional[QAPISourceInfo],
> + ifcond: QAPISchemaIfCond,
> + element_type: QAPISchemaType) -> None:
> + self.list_types.add(name)
> +
> +
> +class QAPISchemaGenTypeInfoVisitor(QAPISchemaModularCVisitor):
> +
> + def __init__(self, prefix: str, name_map: Dict[str, str],
> + list_types: Set[str]):
> + super().__init__(
> + prefix, 'qapi-type-infos',
> + ' * Schema-defined QAPI type info',
> + ' * Built-in QAPI type info', __doc__)
> + self._name_map = name_map
> + self._list_types = list_types
> +
> + def _begin_builtin_module(self) -> None:
> + self._genc.preamble_add(mcgen('''
> +#include "qemu/osdep.h"
> +#include "qapi/qapi-builtin-types.h"
> +#include "qapi/qapi-builtin-type-infos.h"
> +'''))
> + self._genh.preamble_add(mcgen('''
> +#include "qapi/qapi-type-info.h"
> +'''))
> +
> + def _begin_user_module(self, name: str) -> None:
> + type_infos = self._module_basename('qapi-type-infos', name)
> + types = self._module_basename('qapi-types', name)
> + self._genc.preamble_add(mcgen('''
> +#include "qemu/osdep.h"
> +#include "%(types)s.h"
> +#include "%(type_infos)s.h"
> +''',
> + types=types,
> + type_infos=type_infos))
> + self._genh.preamble_add(mcgen('''
> +#include "qapi/qapi-builtin-type-infos.h"
> +'''))
> +
> + def _gen_type_info(self, name: str,
> + ifcond: Optional[QAPISchemaIfCond] = None,
> + with_lookup: bool = False,
> + with_list: bool = False,
> + schema_name: Optional[str] = None) -> None:
> + c_id = c_name(name + '_type_info')
> + lookup = ''
> + if with_lookup:
> + lookup = mcgen('''
> + .lookup = &%(c_name)s_lookup,
> +''',
> + c_name=c_name(name))
> + list_ref = ''
> + if with_list:
> + list_ref = mcgen('''
> + .list = &%(list_id)s,
> +''',
> + list_id=c_name(name + 'List_type_info'))
> + if schema_name is None:
> + masked = self._name_map.get(name)
> + schema_name = '"%s"' % masked if masked is not None else 'NULL'
> + else:
> + schema_name = '"%s"' % schema_name
> + with ifcontext(ifcond or QAPISchemaIfCond(),
> + self._genh, self._genc):
> + self._genh.add(mcgen('''
> +
> +extern const QAPITypeInfo %(c_id)s;
> +''',
> + c_id=c_id))
> + self._genc.add(mcgen('''
> +
> +const QAPITypeInfo %(c_id)s = {
> + .name = "%(name)s",
> + .schema_name = %(schema_name)s,
> +%(lookup)s%(list_ref)s};
This substitutes values of mcgen() into mcgen(). That's not how it
wants to be used. Please ...
> +''',
> + c_id=c_id, name=name,
> + schema_name=schema_name,
> + lookup=lookup,
> + list_ref=list_ref))
... try something like
self._genh.add(mcgen('''
extern const QAPITypeInfo %(c_id)s;
''',
c_id=c_id))
self._genc.add(mcgen('''
const QAPITypeInfo %(c_id)s = {
.name = "%(name)s",
.schema_name = %(schema_name)s,
''',
c_id=c_id, name=name,
schema_name=schema_name))
if with_lookup:
self._genc.add(mcgen('''
.lookup = &%(c_name)s_lookup,
''',
c_name=c_name(name))
and so forth.
> +
> + def _has_list(self, name: str) -> bool:
> + return name + 'List' in self._list_types
Simpler, I think:
return self._schema.lookup_type(name + 'List')
where self._schema is set in .visit_begin().
> +
> + def visit_builtin_type(self,
> + name: str,
> + info: Optional[QAPISourceInfo],
> + json_type: str) -> None:
> + self._gen_type_info(name, with_list=self._has_list(name))
> +
> + def visit_enum_type(self,
> + name: str,
> + info: Optional[QAPISourceInfo],
> + ifcond: QAPISchemaIfCond,
> + features: List[QAPISchemaFeature],
> + members: List[QAPISchemaEnumMember],
> + prefix: Optional[str]) -> None:
> + self._gen_type_info(name, ifcond, with_lookup=True,
> + with_list=self._has_list(name))
> +
> + def visit_object_type(self,
> + name: str,
> + info: Optional[QAPISourceInfo],
> + ifcond: QAPISchemaIfCond,
> + features: List[QAPISchemaFeature],
> + base: Optional[QAPISchemaObjectType],
> + members: List[QAPISchemaObjectTypeMember],
> + branches: Optional[QAPISchemaBranches]) -> None:
> + if name.startswith('q_'):
Duplicates QAPISchemaObjectType.is_implicit(). Similar code in types.py
and visit.py. Not this patch's fault; moving on.
> + return
> + self._gen_type_info(name, ifcond,
> + with_list=self._has_list(name))
> +
> + def visit_array_type(self,
> + name: str,
> + info: Optional[QAPISourceInfo],
> + ifcond: QAPISchemaIfCond,
> + element_type: QAPISchemaType) -> None:
> + elem_schema = self._name_map.get(element_type.name,
> + element_type.name)
> + self._gen_type_info(name, ifcond,
> + schema_name='[' + elem_schema + ']')
> +
> + def visit_alternate_type(self,
> + name: str,
> + info: Optional[QAPISourceInfo],
> + ifcond: QAPISchemaIfCond,
> + features: List[QAPISchemaFeature],
> + alternatives: QAPISchemaAlternatives) -> None:
> + self._gen_type_info(name, ifcond,
> + with_list=self._has_list(name))
> +
> +
> +def gen_type_infos(schema: QAPISchema,
> + output_dir: str,
> + prefix: str,
> + opt_builtins: bool,
> + name_map: Dict[str, str]) -> None:
> + collector = _ArrayTypeCollector()
> + schema.visit(collector)
> + vis = QAPISchemaGenTypeInfoVisitor(prefix, name_map,
> + collector.list_types)
> + schema.visit(vis)
> + vis.write(output_dir, opt_builtins)
next prev parent reply other threads:[~2026-08-06 14:06 UTC|newest]
Thread overview: 72+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-15 20:37 [PATCH v2 00/53] qom/qdev: associate properties with QAPI schema types Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 01/53] qapi: add QAPITypeInfo struct definition Marc-André Lureau
2026-07-31 11:43 ` Markus Armbruster
2026-06-15 20:37 ` [PATCH v2 02/53] qapi/introspect: expose the type name map Marc-André Lureau
2026-08-06 9:48 ` Markus Armbruster
2026-08-06 12:12 ` Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 03/53] qapi: add type-infos generator Marc-André Lureau
2026-08-06 14:05 ` Markus Armbruster [this message]
2026-08-07 8:46 ` Markus Armbruster
2026-06-15 20:37 ` [PATCH v2 04/53] qapi/backend: wire up type-infos generation Marc-André Lureau
2026-08-07 5:36 ` Markus Armbruster
2026-06-15 20:37 ` [PATCH v2 05/53] qapi/gen: fix _module_basename for multi-dash 'what' parameters Marc-André Lureau
2026-08-07 6:46 ` Markus Armbruster
2026-06-15 20:37 ` [PATCH v2 06/53] meson: add qapi-type-infos-*.c/h to build Marc-André Lureau
2026-08-07 7:12 ` Markus Armbruster
2026-06-15 20:37 ` [PATCH v2 07/53] qom: add qapi_type field to ObjectProperty Marc-André Lureau
2026-08-07 9:16 ` Markus Armbruster
2026-06-15 20:37 ` [PATCH v2 08/53] qapi/qom: add qapi-type field to ObjectPropertyInfo Marc-André Lureau
2026-08-07 9:27 ` Markus Armbruster
2026-06-15 20:37 ` [PATCH v2 09/53] qom/qmp: populate qapi-type in QMP handlers Marc-André Lureau
2026-08-07 10:27 ` Markus Armbruster
2026-06-15 20:37 ` [PATCH v2 10/53] qom: add QAPI-aware property registration Marc-André Lureau
2026-08-07 11:45 ` Markus Armbruster
2026-06-15 20:37 ` [PATCH v2 11/53] tests: update check-qom-proplist for " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 12/53] qom: convert enum properties to QAPI-aware registration Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 13/53] qom: remove old enum property registration API Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 14/53] qom: convert struct properties to QAPI-aware registration Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 15/53] " Marc-André Lureau
2026-08-07 11:48 ` Markus Armbruster
2026-06-15 20:37 ` [PATCH v2 16/53] x86: convert OnOffAuto " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 17/53] microvm: " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 18/53] pc: convert OnOffAuto vmport property " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 19/53] arm/virt: convert OnOffAuto acpi " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 20/53] riscv/virt: convert OnOffAuto properties " Marc-André Lureau
2026-06-16 0:53 ` Alistair Francis
2026-06-15 20:37 ` [PATCH v2 21/53] loongarch/virt: " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 22/53] hostmem-file: convert OnOffAuto rom property " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 23/53] sev: convert OnOffAuto legacy-vm-type " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 24/53] whpx: convert OnOffAuto hyperv " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 25/53] whpx: convert OnOffAuto arch properties " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 26/53] accel/kvm: convert OnOffSplit property " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 27/53] whpx: " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 28/53] ppc/spapr-caps: convert to QAPI-aware property registration Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 29/53] system/memory: fix "priority" property typename Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 30/53] hw/mem/nvdimm: fix "size" " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 31/53] qom: convert scalar properties to QAPI-aware registration Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 32/53] i386/cpu: convert strList property " Marc-André Lureau
2026-06-15 20:37 ` [PATCH v2 33/53] accel/hvf: convert OnOffSplit " Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 34/53] i386/x86: convert SgxEPCList " Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 35/53] virtio-balloon: convert guest-stats property to QAPI type Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 36/53] qom: replace object_property_add_tm with StructTm " Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 37/53] hw/nvdimm: convert UUID property to QAPI-aware registration Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 38/53] hw/s390-virtio-ccw: convert loadparm " Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 39/53] hw/ppc/spapr_drc: convert fdt " Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 40/53] spdm-socket: convert SpdmTransportType to QAPI enum Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 41/53] hw/pci: change the busnr type to uint8 Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 42/53] qdev: add qapi_type field to PropertyInfo with fallback registration Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 43/53] qdev: convert core PropertyInfo definitions to use qapi_type Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 44/53] qdev: convert system " Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 45/53] hw: convert device-local " Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 46/53] target/riscv: fix incorrect QAPI types and u8 casting Marc-André Lureau
2026-06-16 0:54 ` Alistair Francis
2026-06-15 20:38 ` [PATCH v2 47/53] target/riscv: convert PropertyInfo definitions to use qapi_type Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 48/53] qdev: " Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 49/53] qdev: introduce typed array PropertyInfos Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 50/53] qdev: simplify DEFINE_PROP_ARRAY and remove generic array PropertyInfo Marc-André Lureau
2026-07-28 7:35 ` Markus Armbruster
2026-07-28 8:24 ` Markus Armbruster
2026-07-28 8:32 ` Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 51/53] qdev: remove deprecated PropertyInfo.type and .enum_table fields Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 52/53] memory: use object_property_add_link for container property Marc-André Lureau
2026-06-15 20:38 ` [PATCH v2 53/53] qom: use QAPITypeInfo in object_property_get_enum Marc-André Lureau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87zeyztkcs.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=michael.roth@amd.com \
--cc=pbonzini@redhat.com \
--cc=philmd@mailo.com \
--cc=pierrick.bouvier@oss.qualcomm.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.