From nobody Sat Sep 21 07:31:39 2024 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=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1705098739; cv=none; d=zohomail.com; s=zohoarc; b=ZtZ69pkzPBdSDEyKG4L9bkQegK5BvCimysv9vSypE2mBGG12BfHqKFvTQbeLD8uVYGqqQjHSLvMu+YKIHHSdeGyJcTXqhrjBRg+BIIt2Mn9oBdaqbgki0JnOMzQ72JAlBgaTocYtx5U69j7owspEsygGStbp0bESXKxbxwzjIM8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1705098739; h=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=Fgi5fW6fhsdEJueh5NdVJ1BXW3ext3pLY93BajstAWA=; b=XYbBWcI00UjC3xH0aWL9bRlVxdlGsVo5P9IUiqga2hl03V5rrujUglo8aPl2DPoCApGx20TjbMRMKPu/f8foGTH6YQ1jWqFiVNlt9ajchn2zUsmTr0rS+uBGq5jqdh2ENTOGgWqqzENYStyX3KMerRN0rLLuf5b3Y+MqdScO4Eo= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1705098739454126.5555712174613; Fri, 12 Jan 2024 14:32:19 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rOQ2h-0006CK-OT; Fri, 12 Jan 2024 17:30:23 -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 1rOQ2P-0005pp-Gu for qemu-devel@nongnu.org; Fri, 12 Jan 2024 17:30:07 -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 1rOQ2J-00063F-I0 for qemu-devel@nongnu.org; Fri, 12 Jan 2024 17:30:04 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-606-knS6JtjpO5-eqaXqB4h0Iw-1; Fri, 12 Jan 2024 17:29:47 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 80B241025874; Fri, 12 Jan 2024 22:29:47 +0000 (UTC) Received: from scv.localdomain (unknown [10.22.17.171]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3B6A0C25AC8; Fri, 12 Jan 2024 22:29:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705098598; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Fgi5fW6fhsdEJueh5NdVJ1BXW3ext3pLY93BajstAWA=; b=K7GopWbJV2EEi1uVhRD4QDLzy3HTnDJqeTeRqhitzZc7SBHW2cFunB87ndrKVbtC2kYny/ YJDTcf42mJk83yLOfdzmOUMzRYFtgoAfnbvfTYSpk1J0lqadVE3OmX7tch2fTJv3UOr7yU FHh64j1e8Yq0+fsKHZAWRSealoErSF4= X-MC-Unique: knS6JtjpO5-eqaXqB4h0Iw-1 From: John Snow To: qemu-devel@nongnu.org Cc: Michael Roth , Peter Maydell , Markus Armbruster , John Snow Subject: [PATCH v2 03/19] qapi: create QAPISchemaDefinition Date: Fri, 12 Jan 2024 17:29:29 -0500 Message-ID: <20240112222945.3033854-4-jsnow@redhat.com> In-Reply-To: <20240112222945.3033854-1-jsnow@redhat.com> References: <20240112222945.3033854-1-jsnow@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.09, 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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: 1705098741373100006 Content-Type: text/plain; charset="utf-8" Include entities don't have names, but we generally expect "entities" to have names. Reclassify all entities with names as *definitions*, leaving the nameless include entities as QAPISchemaEntity instances. This is primarily to help simplify typing around expectations of what callers expect for properties of an "entity". Suggested-by: Markus Armbruster Signed-off-by: John Snow --- scripts/qapi/schema.py | 117 ++++++++++++++++++++++++----------------- 1 file changed, 70 insertions(+), 47 deletions(-) diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py index b7830672e57..e39ed972a80 100644 --- a/scripts/qapi/schema.py +++ b/scripts/qapi/schema.py @@ -55,14 +55,14 @@ def is_present(self): =20 =20 class QAPISchemaEntity: - meta: Optional[str] =3D None + """ + QAPISchemaEntity represents all schema entities. =20 - def __init__(self, name: str, info, doc, ifcond=3DNone, features=3DNon= e): - assert name is None or isinstance(name, str) - for f in features or []: - assert isinstance(f, QAPISchemaFeature) - f.set_defined_in(name) - self.name =3D name + Notably, this includes both named and un-named entities, such as + include directives. Entities with names are represented by the + more specific sub-class `QAPISchemaDefinition` instead. + """ + def __init__(self, info): self._module =3D None # For explicitly defined entities, info points to the (explicit) # definition. For builtins (and their arrays), info is None. @@ -70,14 +70,50 @@ def __init__(self, name: str, info, doc, ifcond=3DNone,= features=3DNone): # triggered the implicit definition (there may be more than one # such place). self.info =3D info + self._checked =3D False + + def __repr__(self): + return "<%s at 0x%x>" % (type(self).__name__, id(self)) + + def check(self, schema): + # pylint: disable=3Dunused-argument + self._checked =3D True + + def connect_doc(self, doc=3DNone): + pass + + def check_doc(self): + pass + + def _set_module(self, schema, info): + assert self._checked + fname =3D info.fname if info else QAPISchemaModule.BUILTIN_MODULE_= NAME + self._module =3D schema.module_by_fname(fname) + self._module.add_entity(self) + + def set_module(self, schema): + self._set_module(schema, self.info) + + def visit(self, visitor): + # pylint: disable=3Dunused-argument + assert self._checked + + +class QAPISchemaDefinition(QAPISchemaEntity): + meta: Optional[str] =3D None + + def __init__(self, name: str, info, doc, ifcond=3DNone, features=3DNon= e): + assert isinstance(name, str) + super().__init__(info) + for f in features or []: + assert isinstance(f, QAPISchemaFeature) + f.set_defined_in(name) + self.name =3D name self.doc =3D doc self._ifcond =3D ifcond or QAPISchemaIfCond() self.features =3D features or [] - self._checked =3D False =20 def __repr__(self): - if self.name is None: - return "<%s at 0x%x>" % (type(self).__name__, id(self)) return "<%s:%s at 0x%x>" % (type(self).__name__, self.name, id(self)) =20 @@ -102,15 +138,6 @@ def check_doc(self): if self.doc: self.doc.check() =20 - def _set_module(self, schema, info): - assert self._checked - fname =3D info.fname if info else QAPISchemaModule.BUILTIN_MODULE_= NAME - self._module =3D schema.module_by_fname(fname) - self._module.add_entity(self) - - def set_module(self, schema): - self._set_module(schema, self.info) - @property def ifcond(self): assert self._checked @@ -119,10 +146,6 @@ def ifcond(self): def is_implicit(self): return not self.info =20 - def visit(self, visitor): - # pylint: disable=3Dunused-argument - assert self._checked - def describe(self): assert self.meta return "%s '%s'" % (self.meta, self.name) @@ -222,7 +245,7 @@ def visit(self, visitor): =20 class QAPISchemaInclude(QAPISchemaEntity): def __init__(self, sub_module, info): - super().__init__(None, info, None) + super().__init__(info) self._sub_module =3D sub_module =20 def visit(self, visitor): @@ -230,7 +253,7 @@ def visit(self, visitor): visitor.visit_include(self._sub_module.name, self.info) =20 =20 -class QAPISchemaType(QAPISchemaEntity): +class QAPISchemaType(QAPISchemaDefinition): # Return the C type for common use. # For the types we commonly box, this is a pointer type. def c_type(self): @@ -801,7 +824,7 @@ def __init__(self, name, info, typ, ifcond=3DNone): super().__init__(name, info, typ, False, ifcond) =20 =20 -class QAPISchemaCommand(QAPISchemaEntity): +class QAPISchemaCommand(QAPISchemaDefinition): meta =3D 'command' =20 def __init__(self, name, info, doc, ifcond, features, @@ -872,7 +895,7 @@ def visit(self, visitor): self.coroutine) =20 =20 -class QAPISchemaEvent(QAPISchemaEntity): +class QAPISchemaEvent(QAPISchemaDefinition): meta =3D 'event' =20 def __init__(self, name, info, doc, ifcond, features, arg_type, boxed): @@ -943,11 +966,12 @@ def __init__(self, fname): self.check() =20 def _def_entity(self, ent): + self._entity_list.append(ent) + + def _def_definition(self, ent): # Only the predefined types are allowed to not have info assert ent.info or self._predefining - self._entity_list.append(ent) - if ent.name is None: - return + self._def_entity(ent) # TODO reject names that differ only in '_' vs. '.' vs. '-', # because they're liable to clash in generated C. other_ent =3D self._entity_dict.get(ent.name) @@ -1001,7 +1025,7 @@ def _def_include(self, expr: QAPIExpression): QAPISchemaInclude(self._make_module(include), expr.info)) =20 def _def_builtin_type(self, name, json_type, c_type): - self._def_entity(QAPISchemaBuiltinType(name, json_type, c_type)) + self._def_definition(QAPISchemaBuiltinType(name, json_type, c_type= )) # 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 @@ -1027,15 +1051,15 @@ def _def_predefineds(self): self._def_builtin_type(*t) self.the_empty_object_type =3D QAPISchemaObjectType( 'q_empty', None, None, None, None, None, [], None) - self._def_entity(self.the_empty_object_type) + self._def_definition(self.the_empty_object_type) =20 qtypes =3D ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] qtype_values =3D self._make_enum_members( [{'name': n} for n in qtypes], None) =20 - self._def_entity(QAPISchemaEnumType('QType', None, None, None, Non= e, - qtype_values, 'QTYPE')) + self._def_definition(QAPISchemaEnumType('QType', None, None, None, + None, qtype_values, 'QTYPE= ')) =20 def _make_features(self, features, info): if features is None: @@ -1057,7 +1081,7 @@ def _make_enum_members(self, values, info): def _make_array_type(self, element_type, info): name =3D element_type + 'List' # reserved by check_defn_name_st= r() if not self.lookup_type(name): - self._def_entity(QAPISchemaArrayType(name, info, element_type)) + self._def_definition(QAPISchemaArrayType(name, info, element_t= ype)) return name =20 def _make_implicit_object_type(self, name, info, ifcond, role, members= ): @@ -1072,7 +1096,7 @@ def _make_implicit_object_type(self, name, info, ifco= nd, role, members): # later. pass else: - self._def_entity(QAPISchemaObjectType( + self._def_definition(QAPISchemaObjectType( name, info, None, ifcond, None, None, members, None)) return name =20 @@ -1083,7 +1107,7 @@ def _def_enum_type(self, expr: QAPIExpression): ifcond =3D QAPISchemaIfCond(expr.get('if')) info =3D expr.info features =3D self._make_features(expr.get('features'), info) - self._def_entity(QAPISchemaEnumType( + self._def_definition(QAPISchemaEnumType( name, info, expr.doc, ifcond, features, self._make_enum_members(data, info), prefix)) =20 @@ -1111,7 +1135,7 @@ def _def_struct_type(self, expr: QAPIExpression): info =3D expr.info ifcond =3D QAPISchemaIfCond(expr.get('if')) features =3D self._make_features(expr.get('features'), info) - self._def_entity(QAPISchemaObjectType( + self._def_definition(QAPISchemaObjectType( name, info, expr.doc, ifcond, features, base, self._make_members(data, info), None)) @@ -1141,7 +1165,7 @@ def _def_union_type(self, expr: QAPIExpression): info) for (key, value) in data.items()] members: List[QAPISchemaObjectTypeMember] =3D [] - self._def_entity( + self._def_definition( QAPISchemaObjectType(name, info, expr.doc, ifcond, features, base, members, QAPISchemaVariants( @@ -1160,7 +1184,7 @@ def _def_alternate_type(self, expr: QAPIExpression): info) for (key, value) in data.items()] tag_member =3D QAPISchemaObjectTypeMember('type', info, 'QType', F= alse) - self._def_entity( + self._def_definition( QAPISchemaAlternateType( name, info, expr.doc, ifcond, features, QAPISchemaVariants(None, info, tag_member, variants))) @@ -1185,11 +1209,10 @@ def _def_command(self, expr: QAPIExpression): if isinstance(rets, list): assert len(rets) =3D=3D 1 rets =3D self._make_array_type(rets[0], info) - self._def_entity(QAPISchemaCommand(name, info, expr.doc, ifcond, - features, data, rets, - gen, success_response, - boxed, allow_oob, allow_preconf= ig, - coroutine)) + self._def_definition( + QAPISchemaCommand(name, info, expr.doc, ifcond, features, data, + rets, gen, success_response, boxed, allow_oo= b, + allow_preconfig, coroutine)) =20 def _def_event(self, expr: QAPIExpression): name =3D expr['event'] @@ -1202,8 +1225,8 @@ def _def_event(self, expr: QAPIExpression): data =3D self._make_implicit_object_type( name, info, ifcond, 'arg', self._make_members(data, info)) - self._def_entity(QAPISchemaEvent(name, info, expr.doc, ifcond, - features, data, boxed)) + self._def_definition(QAPISchemaEvent(name, info, expr.doc, ifcond, + features, data, boxed)) =20 def _def_exprs(self, exprs): for expr in exprs: --=20 2.43.0