All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	qemu-rust@nongnu.org
Subject: Re: [PATCH v2 09/16] scripts/qapi: add QAPISchemaIfCond.rsgen()
Date: Wed, 25 Feb 2026 07:48:48 +0100	[thread overview]
Message-ID: <87pl5txqr3.fsf@pond.sub.org> (raw)
In-Reply-To: <20260108131043.490084-10-pbonzini@redhat.com> (Paolo Bonzini's message of "Thu, 8 Jan 2026 14:10:36 +0100")

Paolo Bonzini <pbonzini@redhat.com> writes:

> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>
> Generate Rust #[cfg(...)] guards from QAPI 'if' conditions; it
> turns out that they are very similar, with both of them using
> not/any/all, so just walk the tree.
>
> The next commit will put it to use.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> Link: https://lore.kernel.org/r/20210907121943.3498701-15-marcandre.lureau@redhat.com
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  scripts/qapi/common.py | 19 +++++++++++++++++++
>  scripts/qapi/schema.py |  4 ++++
>  2 files changed, 23 insertions(+)
>
> diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
> index d7c8aa3365c..14d5dd259c4 100644
> --- a/scripts/qapi/common.py
> +++ b/scripts/qapi/common.py
> @@ -199,6 +199,25 @@ def guardend(name: str) -> str:
>                   name=c_fname(name).upper())
>  
>  
> +def rsgen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]]) -> str:
> +
> +    def cfg(ifcond: Union[str, Dict[str, Any]]) -> str:
> +        if isinstance(ifcond, str):
> +            return ifcond
> +        assert isinstance(ifcond, dict) and len(ifcond) == 1
> +        if 'not' in ifcond:
> +            oper = 'not'
> +            arg = cfg(ifcond['not'])
> +        else:
> +            oper, operands = next(iter(ifcond.items()))
> +            arg = ', '.join([cfg(c) for c in operands])
> +        return f'{oper}({arg})'
> +
> +    if not ifcond:
> +        return ''
> +    return '#[cfg(%s)]' % cfg(ifcond)
> +
> +

Why not reuse the existing tree walker?  See appended diff.

>  def gen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]],
>                 cond_fmt: str, not_fmt: str,
>                 all_operator: str, any_operator: str) -> str:
> diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py
> index 8d88b40de2e..848a7401251 100644
> --- a/scripts/qapi/schema.py
> +++ b/scripts/qapi/schema.py
> @@ -37,6 +37,7 @@
>      docgen_ifcond,
>      gen_endif,
>      gen_if,
> +    rsgen_ifcond,
>  )
>  from .error import QAPIError, QAPISemError, QAPISourceError
>  from .expr import check_exprs
> @@ -63,6 +64,9 @@ def gen_endif(self) -> str:
>      def docgen(self) -> str:
>          return docgen_ifcond(self.ifcond)
>  
> +    def rsgen(self) -> str:
> +        return rsgen_ifcond(self.ifcond)
> +
>      def is_present(self) -> bool:
>          return bool(self.ifcond)


diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
index d7c8aa3365..d8accae835 100644
--- a/scripts/qapi/common.py
+++ b/scripts/qapi/common.py
@@ -201,7 +201,8 @@ def guardend(name: str) -> str:
 
 def gen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]],
                cond_fmt: str, not_fmt: str,
-               all_operator: str, any_operator: str) -> str:
+               all_fmt: str, all_sep: str,
+               any_fmt: str, any_sep: str) -> str:
 
     def do_gen(ifcond: Union[str, Dict[str, Any]],
                need_parens: bool) -> str:
@@ -211,15 +212,15 @@ def do_gen(ifcond: Union[str, Dict[str, Any]],
         if 'not' in ifcond:
             return not_fmt % do_gen(ifcond['not'], True)
         if 'all' in ifcond:
-            gen = gen_infix(all_operator, ifcond['all'])
+            gen = gen_infix(all_fmt, all_sep, ifcond['all'])
         else:
-            gen = gen_infix(any_operator, ifcond['any'])
+            gen = gen_infix(any_fmt, any_sep, ifcond['any'])
         if need_parens:
             gen = '(' + gen + ')'
         return gen
 
-    def gen_infix(operator: str, operands: Sequence[Any]) -> str:
-        return operator.join([do_gen(o, True) for o in operands])
+    def gen_infix(fmt: str, sep: str, operands: Sequence[Any]) -> str:
+        return fmt % sep.join([do_gen(o, True) for o in operands])
 
     if not ifcond:
         return ''
@@ -227,12 +228,18 @@ def gen_infix(operator: str, operands: Sequence[Any]) -> str:
 
 
 def cgen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]]) -> str:
-    return gen_ifcond(ifcond, 'defined(%s)', '!%s', ' && ', ' || ')
+    return gen_ifcond(ifcond, 'defined(%s)', '!%s',
+                      '%s', ' && ', '%s', ' || ')
+
+
+def rsgen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]]) -> str:
+    return '#[cfg(%s)]' % gen_ifcond(ifcond, '%s', 'not(%s)',
+                                     'all(%s)', ', ', 'any(%s)', ', ')
 
 
 def docgen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]]) -> str:
     # TODO Doc generated for conditions needs polish
-    return gen_ifcond(ifcond, '%s', 'not %s', ' and ', ' or ')
+    return gen_ifcond(ifcond, '%s', 'not %s', '%s', ' and ', '%s', ' or ')
 
 
 def gen_if(cond: str) -> str:



  parent reply	other threads:[~2026-02-25  6:49 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-08 13:10 [PATCH v2 00/16] rust: QObject and QAPI bindings Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 01/16] rust/qobject: add basic bindings Paolo Bonzini
2026-02-24 10:03   ` Markus Armbruster
2026-02-24 10:35     ` Paolo Bonzini
2026-02-24 13:33       ` Markus Armbruster
2026-02-25  8:05         ` Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 02/16] subprojects: add serde Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 03/16] rust/qobject: add Serialize implementation Paolo Bonzini
2026-02-24 10:29   ` Markus Armbruster
2026-02-24 10:48     ` Paolo Bonzini
2026-02-24 13:41       ` Markus Armbruster
2026-01-08 13:10 ` [PATCH v2 04/16] rust/qobject: add Serializer (to_qobject) implementation Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 05/16] rust/qobject: add Deserialize implementation Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 06/16] rust/qobject: add Deserializer (from_qobject) implementation Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 07/16] rust/qobject: add from/to JSON bindings for QObject Paolo Bonzini
2026-01-15 13:17   ` Zhao Liu
2026-01-08 13:10 ` [PATCH v2 08/16] rust/qobject: add Display/Debug Paolo Bonzini
2026-01-15 13:19   ` Zhao Liu
2026-01-08 13:10 ` [PATCH v2 09/16] scripts/qapi: add QAPISchemaIfCond.rsgen() Paolo Bonzini
2026-01-19  6:58   ` Zhao Liu
2026-02-25  6:48   ` Markus Armbruster [this message]
2026-02-25  7:53     ` Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 10/16] scripts/qapi: add QAPISchemaType.is_predefined Paolo Bonzini
2026-02-25  7:33   ` Markus Armbruster
2026-02-25  8:01     ` Paolo Bonzini
2026-02-25  8:44       ` Markus Armbruster
2026-02-26 14:12         ` Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 11/16] scripts/qapi: pull c_name from camel_to_upper to caller Paolo Bonzini
2026-01-19  7:05   ` Zhao Liu
2026-02-25  8:32   ` Markus Armbruster
2026-03-31  7:33     ` Paolo Bonzini
2026-03-31  7:37       ` Markus Armbruster
2026-01-08 13:10 ` [PATCH v2 12/16] scripts/qapi: generate high-level Rust bindings Paolo Bonzini
2026-02-23 12:36   ` Markus Armbruster
2026-02-23 16:11     ` Paolo Bonzini
2026-02-24 13:46       ` Markus Armbruster
2026-02-25 14:39   ` Markus Armbruster
2026-03-03 10:00     ` Paolo Bonzini
2026-03-03 12:31       ` Markus Armbruster
2026-03-03 15:55         ` Paolo Bonzini
2026-03-04  8:09           ` Markus Armbruster
2026-03-31  7:53             ` Paolo Bonzini
2026-05-06 10:59     ` Markus Armbruster
2026-05-06 12:35       ` Marc-André Lureau
2026-03-03  9:19   ` Markus Armbruster
2026-03-03 13:17     ` Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 13/16] scripts/rustc_args: add --no-strict-cfg Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 14/16] rust/util: build QAPI types Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 15/16] scripts/qapi: add serde attributes Paolo Bonzini
2026-01-08 13:10 ` [PATCH v2 16/16] rust/tests: QAPI integration tests Paolo Bonzini
2026-06-03  8:36   ` Marc-André Lureau
2026-02-17  8:10 ` [PATCH v2 00/16] rust: QObject and QAPI bindings Paolo Bonzini
2026-02-19 13:39 ` Markus Armbruster
2026-02-19 16:28   ` Paolo Bonzini
2026-02-23  9:53 ` Daniel P. Berrangé
2026-02-23 15:54   ` Paolo Bonzini
2026-02-23 16:24     ` Daniel P. Berrangé
2026-02-23 19:03       ` Paolo Bonzini
2026-02-24 14:06 ` Markus Armbruster
2026-02-24 17:28   ` Paolo Bonzini
2026-02-26 12:42     ` Markus Armbruster

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=87pl5txqr3.fsf@pond.sub.org \
    --to=armbru@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-rust@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.