From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7E96C433F5 for ; Wed, 8 Sep 2021 14:09:54 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 37E8960C3E for ; Wed, 8 Sep 2021 14:09:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 37E8960C3E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=nongnu.org Received: from localhost ([::1]:57730 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mNyGv-0005Je-7H for qemu-devel@archiver.kernel.org; Wed, 08 Sep 2021 10:09:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41218) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mNyER-0003Uv-K0 for qemu-devel@nongnu.org; Wed, 08 Sep 2021 10:07:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:23529) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mNyEM-0004jd-GA for qemu-devel@nongnu.org; Wed, 08 Sep 2021 10:07:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1631110032; 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: in-reply-to:in-reply-to:references:references; bh=6mgtNnUM2/eMgGifbaRAXPUyQGb7uD/wx7orKImzjfA=; b=incAmaUfBoQSuAPDyAPbuLVnJd1qtOqSlid/uBzZa5IZbNIW+dQHVsiZDDIkECo0id7zCl sQRSx0GddovLvz+Aj0RdlLP5xda08VotBzkCR4x/e2bGP5f+WlJ87eZH8u8NoAWjYtMpfm 47OGCliB+rGfh12PHEM36mWevCZ43xc= Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-374-KTBqUrC8M9SX_XuQh2U6EA-1; Wed, 08 Sep 2021 10:07:11 -0400 X-MC-Unique: KTBqUrC8M9SX_XuQh2U6EA-1 Received: by mail-pg1-f198.google.com with SMTP id u7-20020a632347000000b0026722cd9defso1743160pgm.7 for ; Wed, 08 Sep 2021 07:07:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=6mgtNnUM2/eMgGifbaRAXPUyQGb7uD/wx7orKImzjfA=; b=64DQy+sfA+zTpl3NT+5gdPmS6F91TpLTpeY7+zYFfYh7ioQTunQRdDgcH5itqFJrF3 /oQ4uHjXMXnzAdD/cqdzfb/VOvtzJLB+fzEGKztqixC/LTxVmZzV8nukuhXULzE95ZAn mYaNEMZLzlxXuQhsux4Ru/aGO1/I+MTIFQqDh+yzYCgiik4BGsiWkM6IpKXh0HwHGW/k YWkg1c0GV+2zg3u/hf4mjStWfIHTnfax5ZB5GNF9B5Rfs6t5zmfPrJDgmtDffBm5iNhs bi69yyzP80VIIjooIeSuIQ7Sl7Y6z2WY85VdM6kDjT+nut5GipDQPvjZBS4dMPFpLvud HMfw== X-Gm-Message-State: AOAM5316Df+zoCZmEaYMkGrrjQrTwdtEb5Y8aCZp0AcV4AAL2T12PiCj e66PORLE0PI3KcxGe2HPxHqcpnwxr3odBo0Dh17wn7eUp0tN9W4firSbKRvpTqsmtgQxqe13KwF GcYxGsyKWZGeICvj9+WWHDFkdI+fLtOM= X-Received: by 2002:a63:30d:: with SMTP id 13mr3908128pgd.289.1631110030084; Wed, 08 Sep 2021 07:07:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJws+ptsDa+zk0j92zkj0aPjvwdwam9kkoVQUmAT7i24e1mOa9IxNF3tcMu+sI2nPVHNejGdgsD5eglENL2Hers= X-Received: by 2002:a63:30d:: with SMTP id 13mr3908103pgd.289.1631110029797; Wed, 08 Sep 2021 07:07:09 -0700 (PDT) MIME-Version: 1.0 References: <20210907121943.3498701-1-marcandre.lureau@redhat.com> <20210907121943.3498701-15-marcandre.lureau@redhat.com> <87h7evw8kk.fsf@dusky.pond.sub.org> In-Reply-To: <87h7evw8kk.fsf@dusky.pond.sub.org> From: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= Date: Wed, 8 Sep 2021 18:06:59 +0400 Message-ID: Subject: Re: [RFC v3 14/32] scripts/qapi: add QAPISchemaIfCond.rsgen() To: Markus Armbruster Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlureau@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/alternative; boundary="000000000000ea807705cb7c6544" Received-SPF: pass client-ip=170.10.133.124; envelope-from=mlureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.393, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Bonzini, Paolo" , "P. Berrange, Daniel" , qemu-devel , Stefan Hajnoczi Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" --000000000000ea807705cb7c6544 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi On Wed, Sep 8, 2021 at 4:33 PM Markus Armbruster wrote: > marcandre.lureau@redhat.com writes: > > > From: Marc-Andr=C3=A9 Lureau > > > > Generate Rust #[cfg(...)] guards from QAPI 'if' conditions. > > > > Signed-off-by: Marc-Andr=C3=A9 Lureau > > --- > > scripts/qapi/common.py | 16 ++++++++++++++++ > > scripts/qapi/schema.py | 4 ++++ > > 2 files changed, 20 insertions(+) > > > > diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py > > index 5f8f76e5b2..6d22c66391 100644 > > --- a/scripts/qapi/common.py > > +++ b/scripts/qapi/common.py > > @@ -201,6 +201,22 @@ def guardend(name: str) -> str: > > name=3Dc_fname(name).upper()) > > > > > > +def rsgen_ifcond(ifcond: Union[str, Dict[str, Any]]) -> str: > > + > > + def cfg(ifcond: Union[str, Dict[str, Any]]): > > + if isinstance(ifcond, str): > > + return ifcond > > + if isinstance(ifcond, list): > > + return ', '.join([cfg(c) for c in ifcond]) > > + oper, operands =3D next(iter(ifcond.items())) > > + operands =3D cfg(operands) > > + return f'{oper}({operands})' > > + > > + if not ifcond: > > + return '' > > + return '#[cfg(%s)]' % cfg(ifcond) > > + > > + > > def gen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]], > > cond_fmt: str, not_fmt: str, > > all_operator: str, any_operator: str) -> str: > > Can we generalize gen_ifcond() to work for rsgen_ifcond(), too? > > Not elegantly, I am afraid. The logic of gen_ifcond() is based around the distinct prefix vs infix handling. In contrast, Rust cfg are all infix. As you can see from the code above, it is quite straightforward. Reusing gen_ifcond() would make it quite convoluted. > > diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py > > index 6455a8f425..c61f35e13f 100644 > > --- a/scripts/qapi/schema.py > > +++ b/scripts/qapi/schema.py > > @@ -26,6 +26,7 @@ > > docgen_ifcond, > > gen_endif, > > gen_if, > > + rsgen_ifcond, > > ) > > from .error import QAPIError, QAPISemError, QAPISourceError > > from .expr import check_exprs > > @@ -48,6 +49,9 @@ def gen_endif(self): > > def docgen(self): > > return docgen_ifcond(self.ifcond) > > > > + def rsgen(self): > > + return rsgen_ifcond(self.ifcond) > > + > > def is_present(self): > > return bool(self.ifcond) > > --000000000000ea807705cb7c6544 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi

On Wed, Sep 8, 2021 at 4:33 PM Mark= us Armbruster <ar= mbru@redhat.com> wrote:
marcandre.lureau@redhat.com writes:

> From: Marc-Andr=C3=A9 Lureau <marcandre.lureau@redhat.com>
>
> Generate Rust #[cfg(...)] guards from QAPI 'if' conditions. >
> Signed-off-by: Marc-Andr=C3=A9 Lureau <marcandre.lureau@redhat.com> > ---
>=C2=A0 scripts/qapi/common.py | 16 ++++++++++++++++
>=C2=A0 scripts/qapi/schema.py |=C2=A0 4 ++++
>=C2=A0 2 files changed, 20 insertions(+)
>
> diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py
> index 5f8f76e5b2..6d22c66391 100644
> --- a/scripts/qapi/common.py
> +++ b/scripts/qapi/common.py
> @@ -201,6 +201,22 @@ def guardend(name: str) -> str:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0na= me=3Dc_fname(name).upper())
>=C2=A0
>=C2=A0
> +def rsgen_ifcond(ifcond: Union[str, Dict[str, Any]]) -> str:
> +
> +=C2=A0 =C2=A0 def cfg(ifcond: Union[str, Dict[str, Any]]):
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 if isinstance(ifcond, str):
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return ifcond
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 if isinstance(ifcond, list):
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return ', '.join([c= fg(c) for c in ifcond])
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 oper, operands =3D next(iter(ifcond.items= ()))
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 operands =3D cfg(operands)
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return f'{oper}({operands})'
> +
> +=C2=A0 =C2=A0 if not ifcond:
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return ''
> +=C2=A0 =C2=A0 return '#[cfg(%s)]' % cfg(ifcond)
> +
> +
>=C2=A0 def gen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]],
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0cond_fmt:= str, not_fmt: str,
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0all_opera= tor: str, any_operator: str) -> str:

Can we generalize gen_ifcond() to work for rsgen_ifcond(), too?


Not elegantly, I am afraid. The logic = of gen_ifcond() is based around the distinct prefix vs infix handling. In c= ontrast, Rust cfg are all infix. As you can see from the code above, it is = quite straightforward. Reusing gen_ifcond() would make it quite convoluted.=
=C2=A0