From nobody Mon Nov 17 15:46:43 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1604579713; cv=none; d=zohomail.com; s=zohoarc; b=IgoYlkp5D1hPsg43E+w6wRm5NvySI1WZP5lUShAquTglxWnrZrvHtLg0UT93iAiKGSSHB8sCB60Ih5K6cr3B9uUmq8SdAUnUnqCUfcHOGQoLys21AbZzD/Vu8sPWsoPfamI+aU5sq4+pbHP16gL2akHtgJt7PVayXBgyU9nccpU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1604579713; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=iR5GG7M5PTCht1mkfo5nVq2J6BmR8kXc7i/kXsqPQNE=; b=WNxeluGrfuibswTHOxho4frWm1iPVQYe1qhheCyH9+DFZaFS0oxxCHLfp/gHlkH2HDOLuIPbsSHd02RsD9KNl7juZ+wjvvFMUGskZLEcvk0h3vEbqbszu6jKpxzr+jMCfZuE2UOCsag4Mlt+6lYajWmhyLdVmUZvIYkrIQTIe74= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1604579713678577.7646065037185; Thu, 5 Nov 2020 04:35:13 -0800 (PST) Received: from localhost ([::1]:38684 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kaeTw-0000iO-Db for importer@patchew.org; Thu, 05 Nov 2020 07:35:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:44926) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kaeOe-0002sY-J9 for qemu-devel@nongnu.org; Thu, 05 Nov 2020 07:29:51 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:53473) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kaeOT-0004pN-88 for qemu-devel@nongnu.org; Thu, 05 Nov 2020 07:29:42 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-37-gpKNZU5AO2iW9ncYKb5H_w-1; Thu, 05 Nov 2020 07:29:30 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 010B41899421; Thu, 5 Nov 2020 12:29:29 +0000 (UTC) Received: from localhost (unknown [10.36.110.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82BE7614F5; Thu, 5 Nov 2020 12:29:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1604579372; 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=iR5GG7M5PTCht1mkfo5nVq2J6BmR8kXc7i/kXsqPQNE=; b=Dg9ZCry0P0dFTEco/TDMRpkzvcPyCMlPDjgqaeW3vUGTsEFxM9CfcqJMnD/BvR9X0t9uPM jw93/8Kv/3zKasz8X97SVAultv3cyN6kqK11kdgynCbgvy3cvQVNreYMl1HIJqn0kMO3El q3cbQBgnCeUuN/CH+eVSoX1Teo6UOoc= X-MC-Unique: gpKNZU5AO2iW9ncYKb5H_w-1 From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Subject: [PATCH v2 3/9] qapi: start building an 'if' predicate tree Date: Thu, 5 Nov 2020 16:28:02 +0400 Message-Id: <20201105122808.1182973-4-marcandre.lureau@redhat.com> In-Reply-To: <20201105122808.1182973-1-marcandre.lureau@redhat.com> References: <20201105122808.1182973-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=marcandre.lureau@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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 client-ip=216.205.24.124; envelope-from=marcandre.lureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/05 01:14:53 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] 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_H4=0.001, RCVD_IN_MSPIKE_WL=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: Peter Maydell , Michael Roth , Juan Quintela , Markus Armbruster , "Dr. David Alan Gilbert" , Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , jsnow@redhat.com, Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) From: Marc-Andr=C3=A9 Lureau The following patches are going to express conditions in a target language agnostic way. For that, let's start building a predicate tree of configuration options. This intermediary steps still uses C-preprocessor expressions as predicates: "if: [STR, ..]" is translated to a "IfCond -> IfAll -> IfOption[STR]" tree, which will generate "#if STR && .." C code. After the necessary condition tree nodes are introduced, the 'if' C expressions will be converted. (the tree will be less expressive than the full C macros expressions, and will be based only on identifiers. It will only support these operations over the configuration identifiers: 'all', 'any' and 'not') For now, the documentation will continue to use a C condition expression (with defined(FOO), &&, || etc). Eventually, we may want to generate a more human friendly syntax later. Signed-off-by: Marc-Andr=C3=A9 Lureau --- docs/sphinx/qapidoc.py | 6 +-- scripts/qapi/common.py | 42 ++++++++++++++++++- scripts/qapi/schema.py | 47 +++++++++++---------- tests/qapi-schema/doc-good.out | 12 +++--- tests/qapi-schema/qapi-schema-test.out | 58 +++++++++++++------------- 5 files changed, 104 insertions(+), 61 deletions(-) diff --git a/docs/sphinx/qapidoc.py b/docs/sphinx/qapidoc.py index db9520f37f..31b47e4745 100644 --- a/docs/sphinx/qapidoc.py +++ b/docs/sphinx/qapidoc.py @@ -112,12 +112,10 @@ class QAPISchemaGenRSTVisitor(QAPISchemaVisitor): def _nodes_for_ifcond(self, ifcond, with_if=3DTrue): """Return list of Text, literal nodes for the ifcond =20 - Return a list which gives text like ' (If: cond1, cond2, cond3)', = where - the conditions are in literal-text and the commas are not. + Return a list which gives text like ' (If: C-condition)'. If with_if is False, we don't return the "(If: " and ")". """ - condlist =3D intersperse([nodes.literal('', c) for c in ifcond.ifc= ond], - nodes.Text(', ')) + condlist =3D [nodes.literal('', ifcond.gen_if())] if not with_if: return condlist =20 diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index 6ee72956b5..01c4c0c878 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -12,7 +12,7 @@ # See the COPYING file in the top-level directory. =20 import re -from typing import Optional +from typing import Optional, Sequence =20 =20 #: Magic string that gets removed along with all space to its right. @@ -192,3 +192,43 @@ def guardend(name: str) -> str: #endif /* %(name)s */ ''', name=3Dc_fname(name).upper()) + + +class IfPredicate: + def cgen(self) -> str: + raise NotImplementedError() + + +class IfOption(IfPredicate): + def __init__(self, option: str): + self.option =3D option + + def cgen(self) -> str: + return self.option + + def __repr__(self) -> str: + return repr(self.option) + + def __eq__(self, other: object) -> bool: + if not isinstance(other, IfOption): + return False + return self.option =3D=3D other.option + + +class IfAll(IfPredicate): + def __init__(self, pred_list: Sequence[IfPredicate]): + self.pred_list =3D pred_list + + def cgen(self) -> str: + return " && ".join([p.cgen() for p in self.pred_list]) + + def __bool__(self) -> bool: + return bool(self.pred_list) + + def __repr__(self) -> str: + return f"IfAll({self.pred_list})" + + def __eq__(self, other: object) -> bool: + if not isinstance(other, IfAll): + return False + return self.pred_list =3D=3D other.pred_list diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py index 92961aedcf..4a4dbf2da7 100644 --- a/scripts/qapi/schema.py +++ b/scripts/qapi/schema.py @@ -70,7 +70,13 @@ from typing import ( overload, ) =20 -from .common import POINTER_SUFFIX, c_name, mcgen +from .common import ( + POINTER_SUFFIX, + IfAll, + IfOption, + c_name, + mcgen, +) from .error import QAPISemError, QAPISourceError from .expr import check_exprs from .parser import ParsedExpression, QAPIDoc, QAPISchemaParser @@ -87,34 +93,33 @@ class Visitable: =20 class QAPISchemaIf: def __init__(self, ifcond: Optional[Sequence[str]] =3D None): - self.ifcond =3D ifcond or [] - - def gen_if(self) -> str: - ret =3D '' - for ifc in self.ifcond: - ret +=3D mcgen(''' -#if %(cond)s -''', cond=3Difc) - return ret - - def gen_endif(self) -> str: - ret =3D '' - for ifc in reversed(self.ifcond): - ret +=3D mcgen(''' -#endif /* %(cond)s */ -''', cond=3Difc) - return ret + pred_list =3D [IfOption(opt) for opt in ifcond or []] + self.pred =3D IfAll(pred_list) =20 def __bool__(self) -> bool: - return bool(self.ifcond) + return bool(self.pred) =20 def __repr__(self) -> str: - return repr(self.ifcond) + return repr(self.pred) =20 def __eq__(self, other: object) -> bool: if not isinstance(other, QAPISchemaIf): return NotImplemented - return self.ifcond =3D=3D other.ifcond + return self.pred =3D=3D other.pred + + def gen_if(self) -> str: + if self.pred: + return mcgen(''' +#if %(cond)s +''', cond=3Dself.pred.cgen()) + return "" + + def gen_endif(self) -> str: + if self.pred: + return mcgen(''' +#endif // %(cond)s +''', cond=3Dself.pred.cgen()) + return "" =20 =20 class QAPISchemaEntity(Visitable): diff --git a/tests/qapi-schema/doc-good.out b/tests/qapi-schema/doc-good.out index 419284dae2..0d27323c47 100644 --- a/tests/qapi-schema/doc-good.out +++ b/tests/qapi-schema/doc-good.out @@ -12,15 +12,15 @@ enum QType module doc-good.json enum Enum member one - if ['defined(IFONE)'] + if IfAll(['defined(IFONE)']) member two - if ['defined(IFCOND)'] + if IfAll(['defined(IFCOND)']) feature enum-feat object Base member base1: Enum optional=3DFalse object Variant1 member var1: str optional=3DFalse - if ['defined(IFSTR)'] + if IfAll(['defined(IFSTR)']) feature member-feat feature variant1-feat object Variant2 @@ -29,7 +29,7 @@ object Object tag base1 case one: Variant1 case two: Variant2 - if ['IFTWO'] + if IfAll(['IFTWO']) feature union-feat1 object q_obj_Variant1-wrapper member data: Variant1 optional=3DFalse @@ -38,13 +38,13 @@ object q_obj_Variant2-wrapper enum SugaredUnionKind member one member two - if ['IFTWO'] + if IfAll(['IFTWO']) object SugaredUnion member type: SugaredUnionKind optional=3DFalse tag type case one: q_obj_Variant1-wrapper case two: q_obj_Variant2-wrapper - if ['IFTWO'] + if IfAll(['IFTWO']) feature union-feat2 alternate Alternate tag type diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qap= i-schema-test.out index 8868ca0dca..8ba658cd38 100644 --- a/tests/qapi-schema/qapi-schema-test.out +++ b/tests/qapi-schema/qapi-schema-test.out @@ -298,65 +298,65 @@ command __org.qemu_x-command q_obj___org.qemu_x-comma= nd-arg -> __org.qemu_x-Unio object TestIfStruct member foo: int optional=3DFalse member bar: int optional=3DFalse - if ['defined(TEST_IF_STRUCT_BAR)'] - if ['defined(TEST_IF_STRUCT)'] + if IfAll(['defined(TEST_IF_STRUCT_BAR)']) + if IfAll(['defined(TEST_IF_STRUCT)']) enum TestIfEnum member foo member bar - if ['defined(TEST_IF_ENUM_BAR)'] - if ['defined(TEST_IF_ENUM)'] + if IfAll(['defined(TEST_IF_ENUM_BAR)']) + if IfAll(['defined(TEST_IF_ENUM)']) object q_obj_TestStruct-wrapper member data: TestStruct optional=3DFalse enum TestIfUnionKind member foo member union_bar - if ['defined(TEST_IF_UNION_BAR)'] - if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)'] + if IfAll(['defined(TEST_IF_UNION_BAR)']) + if IfAll(['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)']) object TestIfUnion member type: TestIfUnionKind optional=3DFalse tag type case foo: q_obj_TestStruct-wrapper case union_bar: q_obj_str-wrapper - if ['defined(TEST_IF_UNION_BAR)'] - if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)'] + if IfAll(['defined(TEST_IF_UNION_BAR)']) + if IfAll(['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)']) object q_obj_TestIfUnionCmd-arg member union_cmd_arg: TestIfUnion optional=3DFalse - if ['defined(TEST_IF_UNION)'] + if IfAll(['defined(TEST_IF_UNION)']) command TestIfUnionCmd q_obj_TestIfUnionCmd-arg -> None gen=3DTrue success_response=3DTrue boxed=3DFalse oob=3DFalse preconfig= =3DFalse - if ['defined(TEST_IF_UNION)'] + if IfAll(['defined(TEST_IF_UNION)']) alternate TestIfAlternate tag type case foo: int case bar: TestStruct - if ['defined(TEST_IF_ALT_BAR)'] - if ['defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)'] + if IfAll(['defined(TEST_IF_ALT_BAR)']) + if IfAll(['defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)']) object q_obj_TestIfAlternateCmd-arg member alt_cmd_arg: TestIfAlternate optional=3DFalse - if ['defined(TEST_IF_ALT)'] + if IfAll(['defined(TEST_IF_ALT)']) command TestIfAlternateCmd q_obj_TestIfAlternateCmd-arg -> None gen=3DTrue success_response=3DTrue boxed=3DFalse oob=3DFalse preconfig= =3DFalse - if ['defined(TEST_IF_ALT)'] + if IfAll(['defined(TEST_IF_ALT)']) object q_obj_TestIfCmd-arg member foo: TestIfStruct optional=3DFalse member bar: TestIfEnum optional=3DFalse - if ['defined(TEST_IF_CMD_BAR)'] - if ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] + if IfAll(['defined(TEST_IF_CMD_BAR)']) + if IfAll(['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)']) command TestIfCmd q_obj_TestIfCmd-arg -> UserDefThree gen=3DTrue success_response=3DTrue boxed=3DFalse oob=3DFalse preconfig= =3DFalse - if ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] + if IfAll(['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)']) command TestCmdReturnDefThree None -> UserDefThree gen=3DTrue success_response=3DTrue boxed=3DFalse oob=3DFalse preconfig= =3DFalse array TestIfEnumList TestIfEnum - if ['defined(TEST_IF_ENUM)'] + if IfAll(['defined(TEST_IF_ENUM)']) object q_obj_TestIfEvent-arg member foo: TestIfStruct optional=3DFalse member bar: TestIfEnumList optional=3DFalse - if ['defined(TEST_IF_EVT_BAR)'] - if ['defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)'] + if IfAll(['defined(TEST_IF_EVT_BAR)']) + if IfAll(['defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)']) event TestIfEvent q_obj_TestIfEvent-arg boxed=3DFalse - if ['defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)'] + if IfAll(['defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)']) object FeatureStruct0 member foo: int optional=3DFalse object FeatureStruct1 @@ -379,17 +379,17 @@ object FeatureStruct4 object CondFeatureStruct1 member foo: int optional=3DFalse feature feature1 - if ['defined(TEST_IF_FEATURE_1)'] + if IfAll(['defined(TEST_IF_FEATURE_1)']) object CondFeatureStruct2 member foo: int optional=3DFalse feature feature1 - if ['defined(TEST_IF_FEATURE_1)'] + if IfAll(['defined(TEST_IF_FEATURE_1)']) feature feature2 - if ['defined(TEST_IF_FEATURE_2)'] + if IfAll(['defined(TEST_IF_FEATURE_2)']) object CondFeatureStruct3 member foo: int optional=3DFalse feature feature1 - if ['defined(TEST_IF_COND_1)', 'defined(TEST_IF_COND_2)'] + if IfAll(['defined(TEST_IF_COND_1)', 'defined(TEST_IF_COND_2)']) enum FeatureEnum1 member eins member zwei @@ -429,17 +429,17 @@ command test-command-features3 None -> None command test-command-cond-features1 None -> None gen=3DTrue success_response=3DTrue boxed=3DFalse oob=3DFalse preconfig= =3DFalse feature feature1 - if ['defined(TEST_IF_FEATURE_1)'] + if IfAll(['defined(TEST_IF_FEATURE_1)']) command test-command-cond-features2 None -> None gen=3DTrue success_response=3DTrue boxed=3DFalse oob=3DFalse preconfig= =3DFalse feature feature1 - if ['defined(TEST_IF_FEATURE_1)'] + if IfAll(['defined(TEST_IF_FEATURE_1)']) feature feature2 - if ['defined(TEST_IF_FEATURE_2)'] + if IfAll(['defined(TEST_IF_FEATURE_2)']) command test-command-cond-features3 None -> None gen=3DTrue success_response=3DTrue boxed=3DFalse oob=3DFalse preconfig= =3DFalse feature feature1 - if ['defined(TEST_IF_COND_1)', 'defined(TEST_IF_COND_2)'] + if IfAll(['defined(TEST_IF_COND_1)', 'defined(TEST_IF_COND_2)']) event TEST-EVENT-FEATURES1 None boxed=3DFalse feature deprecated --=20 2.29.0