From nobody Sat Jan 31 20:31:43 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1767877975; cv=none; d=zohomail.com; s=zohoarc; b=GnvyLFX6qwEX9MgNMsIe0VdNRIc/C49SOdfOn14nTSlv5TdJI+oRISnppC8Vbfaju9bV5/d/KY0UBM/x62DbAU19orI7FeV+6Cg2sedr4lVZeh94HRk/j2s9f3AmtE0Y9tnVEIzEpaKFeuQ4G2/m8LQwnxr5gVU9vJAn0lRR3ys= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1767877975; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=RLf8C04HThWu7hxDLSyEOaJNCSCBlM6N9D2NQTVosRM=; b=K4dAchvV9z9skGZ+xC3gBPLOpyLSasDrjkOyISh54WQLDePYXyUZOYXKZ8ZJoDClWyfAOW+oRz5BsaF3Ykxwy4fSMIdkbsgVhx1MTaW4FNmtp/B6wKiiw5KxtepO6HTrx/R2gQsi6C1QEkefgKItxFdA772K3vuYxtDvJz1rPZw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1767877975413233.5913698394404; Thu, 8 Jan 2026 05:12:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vdpnW-0007xl-8c; Thu, 08 Jan 2026 08:11:30 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vdpnT-0007qX-8X for qemu-devel@nongnu.org; Thu, 08 Jan 2026 08:11:29 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1vdpnQ-00083D-3y for qemu-devel@nongnu.org; Thu, 08 Jan 2026 08:11:27 -0500 Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-556-W-SwEtCLOIq8zOGfPCYsfw-1; Thu, 08 Jan 2026 08:11:22 -0500 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-43102ac1da8so2261449f8f.2 for ; Thu, 08 Jan 2026 05:11:21 -0800 (PST) Received: from [192.168.10.48] ([151.61.26.160]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-432bd5ee870sm16251504f8f.36.2026.01.08.05.11.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jan 2026 05:11:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767877883; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RLf8C04HThWu7hxDLSyEOaJNCSCBlM6N9D2NQTVosRM=; b=CQiZBmxzEhwnYMyVhVyY47HoD9Y6lQD45G+uMjTjCyz9Z06XIQw2tJYoJ+hLh/jfi8YqBp fdKFi0mdhHL5mCUAoAf0i6Ezx8PIpE7C46dtjqc20Bha+a0aTQ5nJqMyv5rMIlZxsNcX48 Wj7agLnWVMtHgwVpjoN+YH0PFueomKY= X-MC-Unique: W-SwEtCLOIq8zOGfPCYsfw-1 X-Mimecast-MFC-AGG-ID: W-SwEtCLOIq8zOGfPCYsfw_1767877881 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1767877880; x=1768482680; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RLf8C04HThWu7hxDLSyEOaJNCSCBlM6N9D2NQTVosRM=; b=MBhgK77p1YL7u68zoCxSkuAuCPFVdBlNWck80MQzA0wXIxEPjjIGFo6JAAOKpFedm9 w80owy87G3X7MzpwK2bicXqKWQ903nKhK7r2WW+/vAKFuQ9YWoXaDS6TW2rtfcIABvZ7 XoutCTh8DkkMyhuHZxw4UpcR4+4CfBsHsEevG2Jl3fEffZRZgR3itX9MMX73g1Kd0+K4 uH/iWALlwR2BHtOat9dP8a3PrLJ9RR6bqUmtVcwPckxQgxgmKOiHumDqEqG9045rdQwM +RMmT1ZgPhUYwtOLnEaBWGQNporhPcgVgrBEORgufnM9ita7n7qTE9AfJBC9A4AQQbvs PAUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767877880; x=1768482680; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=RLf8C04HThWu7hxDLSyEOaJNCSCBlM6N9D2NQTVosRM=; b=GUzkDiNgEUUWj/ogn9GiOcBqYELHFBdLP5D98l1/bbFf7mc342lQTnR4GbLTPsJ8Fc Fy38TWZ2plvOrElsBwtabN7wsyFyDXJ5YHw37HFi7ZUDDWSxLWhtQF8hhY2tMmKxsFhT 8N7Cv0ZMzSBzjYZ7nxgtJrGIgZU9oQpmjyd+NeH/E9FU40MknxcFaxYzZ4gwXO9Df98i eZ0ax0f+Fhwbue9tzwJNYtZPMsNpR26MkY+rnRWwMkVnkZZ1wedMLz+v3np03bxM4BpA TNKaC0YokAsinSpziBVpH0SE2JMTxqg0ZfH2cZywIHULCWZbzkTlU+fPigS4MBzFL1jN SwkA== X-Gm-Message-State: AOJu0Yy7KGyWNS+DjgaGIWsdO92lGePgDD39gqfDG4R+A8m0Pc3nypRH w4mV+NvNL+y8TzydmTiJXQ1/ZzqwkCwb/QEnWN/kEiI6pjrteVqo7E6chZ+gZJB6JAiA3rwOnYc cgah2tykmS8PLEw+T0EHTzTHaFNgOb6JyrgF0vUKzRXoC7NqmZMad1YsrEq0PSD7N7gUk36WPow v6F/JWzUI9s7jgErZaOU1wTX8dAXMdp+eUILUTa3qu X-Gm-Gg: AY/fxX5vY/1ePPvUzMOaqQWMelIWGeAO3d9LE/4lH/QREzsG9WVbjynL2xSMJ+KqzIv BQBemb8H4kHrlUpm/6rvqdCGiIbc3QFqAhyVgZ1/qS6GfZmyHO9lPV+o0FdhSIW14DP7l21yYLl x0qsmijSSsDAAfTLS22VAI8qlEUD1XTyFuQCj9WL9/1yaO/zH0fC++EaJlWk0TXDHKdrBlt6Z/f gCErObXZ2pll2MCXOfmhDhS/ybskCkH9nMvxOtKqJfXgW+i1JCOIOG0b0i6ZKxYfsiQ1OFl205S bKQTqc2lTeXSgIpsD/MCuyqtoWwtXYOvp+l1cdSLhvWjCYhrwDVYrkEvzmTGGkwYDhEo5D4UohD bgp7a7nARrrF4sX/MO7J4xXl09MkyweE+6FhrE2nkuaek73xzGqDpXJZHTdkTO5fw8/6fOM0YHQ DAIPTCx9VWcMgXqA== X-Received: by 2002:a05:6000:2908:b0:431:a38:c2f7 with SMTP id ffacd0b85a97d-432c37a7d86mr7165471f8f.59.1767877879852; Thu, 08 Jan 2026 05:11:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IHIBPHXJGQY+rvLsf+AbhuQcxem2L+UpS2wdfb1SS+pCtTPKnI+J60VkjZFNDBpeX7UKeSWpA== X-Received: by 2002:a05:6000:2908:b0:431:a38:c2f7 with SMTP id ffacd0b85a97d-432c37a7d86mr7165423f8f.59.1767877879040; Thu, 08 Jan 2026 05:11:19 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: armbru@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , qemu-rust@nongnu.org Subject: [PATCH v2 12/16] scripts/qapi: generate high-level Rust bindings Date: Thu, 8 Jan 2026 14:10:39 +0100 Message-ID: <20260108131043.490084-13-pbonzini@redhat.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260108131043.490084-1-pbonzini@redhat.com> References: <20260108131043.490084-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1767877976498158500 From: Marc-Andr=C3=A9 Lureau Generate high-level native Rust declarations for the QAPI types. - char* is mapped to String, scalars to there corresponding Rust types - enums use #[repr(u32)] and can be transmuted to their C counterparts - has_foo/foo members are mapped to Option - lists are represented as Vec - structures map fields 1:1 to Rust - alternate are represented as Rust enum, each variant being a 1-element tuple - unions are represented in a similar way as in C: a struct S with a "u" member (since S may have extra 'base' fields). The discriminant isn't a member of S, since Rust enum already include it, but it can be recovered with "mystruct.u.into()" Anything that includes a recursive struct puts it in a Box. Lists are not considered recursive, because Vec breaks the recursion (it's possible to construct an object containing an empty Vec of its own type). Signed-off-by: Marc-Andr=C3=A9 Lureau Link: https://lore.kernel.org/r/20210907121943.3498701-21-marcandre.lureau@= redhat.com [Paolo: rewrite conversion of schema types to Rust types] Signed-off-by: Paolo Bonzini --- meson.build | 4 +- scripts/qapi/backend.py | 25 +++ scripts/qapi/common.py | 43 +++++ scripts/qapi/rs.py | 61 +++++++ scripts/qapi/rs_types.py | 373 +++++++++++++++++++++++++++++++++++++++ scripts/qapi/schema.py | 59 +++++-- 6 files changed, 546 insertions(+), 19 deletions(-) create mode 100644 scripts/qapi/rs.py create mode 100644 scripts/qapi/rs_types.py diff --git a/meson.build b/meson.build index db87358d62d..4228792f0f6 100644 --- a/meson.build +++ b/meson.build @@ -3540,11 +3540,13 @@ qapi_gen_depends =3D [ meson.current_source_dir() /= 'scripts/qapi/__init__.py', meson.current_source_dir() / 'scripts/qapi/introspect= .py', meson.current_source_dir() / 'scripts/qapi/main.py', meson.current_source_dir() / 'scripts/qapi/parser.py', + meson.current_source_dir() / 'scripts/qapi/rs_types.p= y', meson.current_source_dir() / 'scripts/qapi/schema.py', meson.current_source_dir() / 'scripts/qapi/source.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' + meson.current_source_dir() / 'scripts/qapi-gen.py', + meson.current_source_dir() / 'scripts/qapi/rs.py', ] =20 tracetool =3D [ diff --git a/scripts/qapi/backend.py b/scripts/qapi/backend.py index 49ae6ecdd33..8023acce0d6 100644 --- a/scripts/qapi/backend.py +++ b/scripts/qapi/backend.py @@ -7,6 +7,7 @@ from .events import gen_events from .features import gen_features from .introspect import gen_introspect +from .rs_types import gen_rs_types from .schema import QAPISchema from .types import gen_types from .visit import gen_visit @@ -63,3 +64,27 @@ def generate(self, gen_commands(schema, output_dir, prefix, gen_tracing) gen_events(schema, output_dir, prefix) gen_introspect(schema, output_dir, prefix, unmask) + + +class QAPIRsBackend(QAPIBackend): + # pylint: disable=3Dtoo-few-public-methods + + def generate(self, + schema: QAPISchema, + output_dir: str, + prefix: str, + unmask: bool, + builtins: bool, + gen_tracing: bool) -> None: + """ + Generate Rust code for the given schema into the target directory. + + :param schema_file: The primary QAPI schema file. + :param output_dir: The output directory to store generated code. + :param prefix: Optional C-code prefix for symbol names. + :param unmask: Expose non-ABI names through introspection? + :param builtins: Generate code for built-in types? + + :raise QAPIError: On failures. + """ + gen_rs_types(schema, output_dir, prefix) diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index c75396a01b5..e9261a3411e 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -64,6 +64,13 @@ def camel_to_upper(value: str) -> str: return ret.upper() =20 =20 +def camel_to_lower(value: str) -> str: + """ + Converts CamelCase to camel_case. + """ + return camel_to_upper(value).lower() + + def c_enum_const(type_name: str, const_name: str, prefix: Optional[str] =3D None) -> str: @@ -129,6 +136,42 @@ def c_name(name: str, protect: bool =3D True) -> str: return name =20 =20 +def rs_name(name: str) -> str: + """ + Map @name to a valid, possibly raw Rust identifier. + """ + name =3D re.sub(r'[^A-Za-z0-9_]', '_', name) + if name[0].isnumeric(): + name =3D '_' + name + # based from the list: + # https://doc.rust-lang.org/reference/keywords.html + if name in ('Self', 'abstract', 'as', 'async', + 'await', 'become', 'box', 'break', + 'const', 'continue', 'crate', 'do', + 'dyn', 'else', 'enum', 'extern', + 'false', 'final', 'fn', 'for', + 'if', 'impl', 'in', 'let', + 'loop', 'macro', 'match', 'mod', + 'move', 'mut', 'override', 'priv', + 'pub', 'ref', 'return', 'self', + 'static', 'struct', 'super', 'trait', + 'true', 'try', 'type', 'typeof', + 'union', 'unsafe', 'unsized', 'use', + 'virtual', 'where', 'while', 'yield'): + name =3D 'r#' + name + # avoid some clashes with the standard library + if name in ('String',): + name =3D 'Qapi' + name + + return name + + +def to_camel_case(value: str) -> str: + return ''.join('_' + word if word[0].isdigit() + else word[:1].upper() + word[1:] + for word in filter(None, re.split("[-_]+", value))) + + class Indentation: """ Indentation level management. diff --git a/scripts/qapi/rs.py b/scripts/qapi/rs.py new file mode 100644 index 00000000000..2cf0c0e07f1 --- /dev/null +++ b/scripts/qapi/rs.py @@ -0,0 +1,61 @@ +# This work is licensed under the terms of the GNU GPL, version 2. +# See the COPYING file in the top-level directory. +""" +QAPI Rust generator +""" + +import os +import re +import subprocess +import sys + +from .common import mcgen as mcgen_common +from .gen import QAPIGen +from .schema import QAPISchemaVisitor + + +def mcgen(s: str, **kwds: object) -> str: + s =3D mcgen_common(s, **kwds) + return re.sub(r'(?: *\n)+', '\n', s) + + +class QAPIGenRs(QAPIGen): + def __init__(self, fname: str, blurb: str, pydoc: str): + super().__init__(fname) + self._blurb =3D blurb + self._copyright =3D '\n//! '.join(re.findall(r'^Copyright .*', pyd= oc, + re.MULTILINE)) + + def _top(self) -> str: + return mcgen(''' +// @generated by qapi-gen, DO NOT EDIT + +//! +//! Schema-defined QAPI types +//! +//! %(copyright)s +//! +//! This work is licensed under the terms of the GNU LGPL, version 2.1 or +//! later. See the COPYING.LIB file in the top-level directory. + +''', + tool=3Dos.path.basename(sys.argv[0]), + blurb=3Dself._blurb, copyright=3Dself._copyright) + + +class QAPISchemaRsVisitor(QAPISchemaVisitor): + + def __init__(self, prefix: str, what: str, + blurb: str, pydoc: str): + super().__init__() + self._prefix =3D prefix + self._what =3D what + self._gen =3D QAPIGenRs(self._prefix + self._what + '.rs', blurb, = pydoc) + + def write(self, output_dir: str) -> None: + self._gen.write(output_dir) + + try: + subprocess.check_call(['rustfmt', self._gen.fname], cwd=3Doutp= ut_dir) + except FileNotFoundError: + pass diff --git a/scripts/qapi/rs_types.py b/scripts/qapi/rs_types.py new file mode 100644 index 00000000000..64702eb54ae --- /dev/null +++ b/scripts/qapi/rs_types.py @@ -0,0 +1,373 @@ +# This work is licensed under the terms of the GNU GPL, version 2. +# See the COPYING file in the top-level directory. +""" +QAPI Rust types generator + +Copyright (c) 2025 Red Hat, Inc. + +This work is licensed under the terms of the GNU GPL, version 2. +See the COPYING file in the top-level directory. +""" + +from typing import List, Optional, Set + +from .common import ( + camel_to_lower, + camel_to_upper, + rs_name, + to_camel_case, +) +from .rs import QAPISchemaRsVisitor, mcgen +from .schema import ( + QAPISchema, + QAPISchemaAlternateType, + QAPISchemaEnumMember, + QAPISchemaFeature, + QAPISchemaIfCond, + QAPISchemaObjectType, + QAPISchemaObjectTypeMember, + QAPISchemaType, + QAPISchemaVariants, +) +from .source import QAPISourceInfo + + +objects_seen =3D set() + + +def gen_rs_variants_to_tag(name: str, + ifcond: QAPISchemaIfCond, + variants: QAPISchemaVariants) -> str: + ret =3D mcgen(''' + +%(cfg)s +impl From<&%(rs_name)sVariant> for %(tag)s { + fn from(e: &%(rs_name)sVariant) -> Self { + match e { +''', + cfg=3Difcond.rsgen(), + rs_name=3Drs_name(name), + tag=3Dvariants.tag_member.type.rs_type()) + + for var in variants.variants: + type_name =3D var.type.name + tag_name =3D var.name + patt =3D '(_)' + if type_name =3D=3D 'q_empty': + patt =3D '' + ret +=3D mcgen(''' + %(cfg)s + %(rs_name)sVariant::%(var_name)s%(patt)s =3D> Self::%(tag_name= )s, +''', + cfg=3Dvar.ifcond.rsgen(), + rs_name=3Drs_name(name), + tag_name=3Drs_name(camel_to_upper(tag_name)), + var_name=3Drs_name(to_camel_case(tag_name)), + patt=3Dpatt) + + ret +=3D mcgen(''' + } + } +} +''') + return ret + + +def gen_rs_variants(name: str, + ifcond: QAPISchemaIfCond, + variants: QAPISchemaVariants) -> str: + ret =3D mcgen(''' + +%(cfg)s +#[derive(Clone, Debug, PartialEq)] +pub enum %(rs_name)sVariant { +''', + cfg=3Difcond.rsgen(), + rs_name=3Drs_name(name)) + + for var in variants.variants: + type_name =3D var.type.name + var_name =3D rs_name(to_camel_case(var.name)) + if type_name =3D=3D 'q_empty': + ret +=3D mcgen(''' + %(cfg)s + %(var_name)s, +''', + cfg=3Dvar.ifcond.rsgen(), + var_name=3Dvar_name) + else: + ret +=3D mcgen(''' + %(cfg)s + %(var_name)s(%(rs_type)s), +''', + cfg=3Dvar.ifcond.rsgen(), + var_name=3Dvar_name, + rs_type=3Dvar.type.rs_type()) + + ret +=3D mcgen(''' +} +''') + + ret +=3D gen_rs_variants_to_tag(name, ifcond, variants) + + return ret + + +def gen_rs_members(members: List[QAPISchemaObjectTypeMember], + exclude: Optional[List[str]] =3D None) -> List[str]: + exclude =3D exclude or [] + return [f"{m.ifcond.rsgen()} {rs_name(camel_to_lower(m.name))}" + for m in members if m.name not in exclude] + + +def has_recursive_type(memb: QAPISchemaType, + name: str, + visited: Set[str]) -> bool: + # pylint: disable=3Dtoo-many-return-statements + if name =3D=3D memb.name: + return True + if memb.name in visited: + return False + visited.add(memb.name) + if isinstance(memb, QAPISchemaObjectType): + if memb.base and has_recursive_type(memb.base, name, visited): + return True + if memb.branches and \ + any(has_recursive_type(m.type, name, visited) + for m in memb.branches.variants): + return True + if any(has_recursive_type(m.type, name, visited) + for m in memb.members): + return True + return any(has_recursive_type(m.type, name, visited) + for m in memb.local_members) + if isinstance(memb, QAPISchemaAlternateType): + return any(has_recursive_type(m.type, name, visited) + for m in memb.alternatives.variants) + return False + + +def gen_struct_members(members: List[QAPISchemaObjectTypeMember], + name: str) -> str: + ret =3D '' + for memb in members: + typ =3D memb.type.rs_type() + if has_recursive_type(memb.type, name, set()): + typ =3D 'Box<%s>' % typ + if memb.optional: + typ =3D 'Option<%s>' % typ + ret +=3D mcgen(''' + %(cfg)s + pub %(rs_name)s: %(rs_type)s, +''', + cfg=3Dmemb.ifcond.rsgen(), + rs_type=3Dtyp, + rs_name=3Drs_name(camel_to_lower(memb.name))) + return ret + + +def gen_rs_object(name: str, + ifcond: QAPISchemaIfCond, + base: Optional[QAPISchemaObjectType], + members: List[QAPISchemaObjectTypeMember], + variants: Optional[QAPISchemaVariants]) -> str: + if name in objects_seen: + return '' + + if variants: + members =3D [m for m in members + if m.name !=3D variants.tag_member.name] + + ret =3D '' + objects_seen.add(name) + + if variants: + ret +=3D gen_rs_variants(name, ifcond, variants) + + ret +=3D mcgen(''' + +%(cfg)s +#[derive(Clone, Debug, PartialEq)] +pub struct %(rs_name)s { +''', + cfg=3Difcond.rsgen(), + rs_name=3Drs_name(name)) + + if base: + if not base.is_implicit(): + ret +=3D mcgen(''' + // Members inherited: +''', + c_name=3Dbase.c_name()) + base_members =3D base.members + if variants: + base_members =3D [m for m in base.members + if m.name !=3D variants.tag_member.name] + ret +=3D gen_struct_members(base_members, name) + if not base.is_implicit(): + ret +=3D mcgen(''' + // Own members: +''') + + ret +=3D gen_struct_members(members, name) + + if variants: + ret +=3D mcgen(''' + pub u: %(rs_type)sVariant, +''', rs_type=3Drs_name(name)) + ret +=3D mcgen(''' +} +''') + return ret + + +def gen_rs_enum(name: str, + ifcond: QAPISchemaIfCond, + members: List[QAPISchemaEnumMember]) -> str: + ret =3D mcgen(''' + +%(cfg)s +#[derive(Copy, Clone, Debug, PartialEq)] +''', + cfg=3Difcond.rsgen()) + + if members: + ret +=3D '''#[repr(u32)] +#[derive(common::TryInto)] +''' + ret +=3D mcgen(''' +pub enum %(rs_name)s { +''', + rs_name=3Drs_name(name)) + + for member in members: + ret +=3D mcgen(''' + %(cfg)s + %(c_enum)s, +''', + cfg=3Dmember.ifcond.rsgen(), + c_enum=3Drs_name(camel_to_upper(member.name))) + ret +=3D '''} + +''' + + # pick the first, since that's what malloc0 does + if members: + default =3D rs_name(camel_to_upper(members[0].name)) + ret +=3D mcgen(''' +%(cfg)s +impl Default for %(rs_name)s { + #[inline] + fn default() -> %(rs_name)s { + Self::%(default)s + } +} +''', + cfg=3Difcond.rsgen(), + rs_name=3Drs_name(name), + default=3Ddefault) + return ret + + +def gen_rs_alternate(name: str, + ifcond: QAPISchemaIfCond, + variants: QAPISchemaVariants) -> str: + if name in objects_seen: + return '' + + ret =3D '' + objects_seen.add(name) + + ret +=3D mcgen(''' +%(cfg)s +#[derive(Clone, Debug, PartialEq)] +pub enum %(rs_name)s { +''', + cfg=3Difcond.rsgen(), + rs_name=3Drs_name(name)) + + for var in variants.variants: + if var.type.name =3D=3D 'q_empty': + continue + typ =3D var.type.rs_type() + if has_recursive_type(var.type, name, set()): + typ =3D 'Box<%s>' % typ + ret +=3D mcgen(''' + %(cfg)s + %(mem_name)s(%(rs_type)s), +''', + cfg=3Dvar.ifcond.rsgen(), + rs_type=3Dtyp, + mem_name=3Drs_name(to_camel_case(var.name))) + + ret +=3D mcgen(''' +} +''') + return ret + + +class QAPISchemaGenRsTypeVisitor(QAPISchemaRsVisitor): + _schema: Optional[QAPISchema] + + def __init__(self, prefix: str) -> None: + super().__init__(prefix, 'qapi-types', + 'Schema-defined QAPI types', __doc__) + + def visit_begin(self, schema: QAPISchema) -> None: + self._schema =3D schema + objects_seen.add(schema.the_empty_object_type.name) + + self._gen.preamble_add( + mcgen(''' +#![allow(unexpected_cfgs)] +#![allow(non_camel_case_types)] +#![allow(clippy::empty_structs_with_brackets)] +#![allow(clippy::large_enum_variant)] +#![allow(clippy::pub_underscore_fields)] + +// Because QAPI structs can contain float, for simplicity we never +// derive Eq. Clippy however would complain for those structs +// that *could* be Eq too. +#![allow(clippy::derive_partial_eq_without_eq)] + +use util::qobject::QObject; +''')) + + def visit_object_type(self, + name: str, + info: Optional[QAPISourceInfo], + ifcond: QAPISchemaIfCond, + features: List[QAPISchemaFeature], + base: Optional[QAPISchemaObjectType], + members: List[QAPISchemaObjectTypeMember], + branches: Optional[QAPISchemaVariants]) -> None: + assert self._schema is not None + if self._schema.is_predefined(name) or name.startswith('q_'): + return + self._gen.add(gen_rs_object(name, ifcond, base, members, branches)) + + def visit_enum_type(self, + name: str, + info: Optional[QAPISourceInfo], + ifcond: QAPISchemaIfCond, + features: List[QAPISchemaFeature], + members: List[QAPISchemaEnumMember], + prefix: Optional[str]) -> None: + assert self._schema is not None + if self._schema.is_predefined(name): + return + self._gen.add(gen_rs_enum(name, ifcond, members)) + + def visit_alternate_type(self, + name: str, + info: Optional[QAPISourceInfo], + ifcond: QAPISchemaIfCond, + features: List[QAPISchemaFeature], + alternatives: QAPISchemaVariants) -> None: + self._gen.add(gen_rs_alternate(name, ifcond, alternatives)) + + +def gen_rs_types(schema: QAPISchema, output_dir: str, prefix: str) -> None: + vis =3D QAPISchemaGenRsTypeVisitor(prefix) + schema.visit(vis) + vis.write(output_dir) diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py index 15f5d97418f..a65b25141fa 100644 --- a/scripts/qapi/schema.py +++ b/scripts/qapi/schema.py @@ -37,6 +37,7 @@ docgen_ifcond, gen_endif, gen_if, + rs_name, rsgen_ifcond, ) from .error import QAPIError, QAPISemError, QAPISourceError @@ -341,6 +342,11 @@ def c_param_type(self) -> str: def c_unboxed_type(self) -> str: return self.c_type() =20 + # Return the Rust type for common use + @abstractmethod + def rs_type(self) -> str: + pass + @abstractmethod def json_type(self) -> str: pass @@ -382,11 +388,12 @@ def describe(self) -> str: class QAPISchemaBuiltinType(QAPISchemaType): meta =3D 'built-in' =20 - def __init__(self, name: str, json_type: str, c_type: str): + def __init__(self, name: str, json_type: str, rs_type: str, c_type: st= r): super().__init__(name, None, None) assert json_type in ('string', 'number', 'int', 'boolean', 'null', 'value') self._json_type_name =3D json_type + self._rs_type_name =3D rs_type self._c_type_name =3D c_type =20 def c_name(self) -> str: @@ -406,6 +413,9 @@ def json_type(self) -> str: def doc_type(self) -> str: return self.json_type() =20 + def rs_type(self) -> str: + return self._rs_type_name + def visit(self, visitor: QAPISchemaVisitor) -> None: super().visit(visitor) visitor.visit_builtin_type(self.name, self.info, self.json_type()) @@ -449,6 +459,9 @@ def is_implicit(self) -> bool: def c_type(self) -> str: return c_name(self.name) =20 + def rs_type(self) -> str: + return rs_name(self.name) + def member_names(self) -> List[str]: return [m.name for m in self.members] =20 @@ -498,6 +511,9 @@ def is_implicit(self) -> bool: def c_type(self) -> str: return c_name(self.name) + POINTER_SUFFIX =20 + def rs_type(self) -> str: + return 'Vec<%s>' % self.element_type.rs_type() + def json_type(self) -> str: return 'array' =20 @@ -630,6 +646,9 @@ def c_type(self) -> str: def c_unboxed_type(self) -> str: return c_name(self.name) =20 + def rs_type(self) -> str: + return rs_name(self.name) + def json_type(self) -> str: return 'object' =20 @@ -711,6 +730,9 @@ def c_type(self) -> str: def json_type(self) -> str: return 'value' =20 + def rs_type(self) -> str: + return rs_name(self.name) + def visit(self, visitor: QAPISchemaVisitor) -> None: super().visit(visitor) visitor.visit_alternate_type( @@ -1234,9 +1256,10 @@ def _def_include(self, expr: QAPIExpression) -> None: QAPISchemaInclude(self._make_module(include), expr.info)) =20 def _def_builtin_type( - self, name: str, json_type: str, c_type: str + self, name: str, json_type: str, rs_type: str, c_type: str ) -> None: - self._def_definition(QAPISchemaBuiltinType(name, json_type, c_type= )) + builtin =3D QAPISchemaBuiltinType(name, json_type, rs_type, c_type) + self._def_definition(builtin) # Instantiating only the arrays that are actually used would # be nice, but we can't as long as their generated code # (qapi-builtin-types.[ch]) may be shared by some other @@ -1255,21 +1278,21 @@ def is_predefined(self, name: str) -> bool: return False =20 def _def_predefineds(self) -> None: - for t in [('str', 'string', 'char' + POINTER_SUFFIX), - ('number', 'number', 'double'), - ('int', 'int', 'int64_t'), - ('int8', 'int', 'int8_t'), - ('int16', 'int', 'int16_t'), - ('int32', 'int', 'int32_t'), - ('int64', 'int', 'int64_t'), - ('uint8', 'int', 'uint8_t'), - ('uint16', 'int', 'uint16_t'), - ('uint32', 'int', 'uint32_t'), - ('uint64', 'int', 'uint64_t'), - ('size', 'int', 'uint64_t'), - ('bool', 'boolean', 'bool'), - ('any', 'value', 'QObject' + POINTER_SUFFIX), - ('null', 'null', 'QNull' + POINTER_SUFFIX)]: + for t in [('str', 'string', 'String', 'char' + POINTER_SUFFIX= ), + ('number', 'number', 'f64', 'double'), + ('int', 'int', 'i64', 'int64_t'), + ('int8', 'int', 'i8', 'int8_t'), + ('int16', 'int', 'i16', 'int16_t'), + ('int32', 'int', 'i32', 'int32_t'), + ('int64', 'int', 'i64', 'int64_t'), + ('uint8', 'int', 'u8', 'uint8_t'), + ('uint16', 'int', 'u16', 'uint16_t'), + ('uint32', 'int', 'u32', 'uint32_t'), + ('uint64', 'int', 'u64', 'uint64_t'), + ('size', 'int', 'u64', 'uint64_t'), + ('bool', 'boolean', 'bool', 'bool'), + ('any', 'value', 'QObject', 'QObject' + POINTER_SUF= FIX), + ('null', 'null', '()', 'QNull' + POINTER_SUFFI= X)]: self._def_builtin_type(*t) self.the_empty_object_type =3D QAPISchemaObjectType( 'q_empty', None, None, None, None, None, [], None) --=20 2.52.0