From patchwork Mon Sep 7 10:16:14 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 515118 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id D3F9E1401CD for ; Mon, 7 Sep 2015 22:30:38 +1000 (AEST) Received: from localhost ([::1]:55451 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZYtVr-0002OH-JF for incoming@patchwork.ozlabs.org; Mon, 07 Sep 2015 06:18:59 -0400 Received: from eggs.gnu.org ([208.118.235.92]:34130) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZYtTm-0001T9-Jd for qemu-devel@nongnu.org; Mon, 07 Sep 2015 06:16:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZYtTj-0000GG-Ca for qemu-devel@nongnu.org; Mon, 07 Sep 2015 06:16:49 -0400 Received: from mx1.redhat.com ([209.132.183.28]:32863) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZYtTj-0000FH-5Y for qemu-devel@nongnu.org; Mon, 07 Sep 2015 06:16:47 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id B8BCB80B27; Mon, 7 Sep 2015 10:16:46 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-17.ams2.redhat.com [10.36.116.17]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t87AGiR0002143 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 7 Sep 2015 06:16:45 -0400 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 16AD430010EC; Mon, 7 Sep 2015 12:16:44 +0200 (CEST) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Mon, 7 Sep 2015 12:16:14 +0200 Message-Id: <1441621003-2434-4-git-send-email-armbru@redhat.com> In-Reply-To: <1441621003-2434-1-git-send-email-armbru@redhat.com> References: <1441621003-2434-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: mdroth@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH RFC v5 03/32] qapi: QAPISchema code generation helper methods X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org New methods c_name(), c_type(), c_null(), json_type(), alternate_qtype(). Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Daniel P. Berrange --- scripts/qapi.py | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 86 insertions(+), 7 deletions(-) diff --git a/scripts/qapi.py b/scripts/qapi.py index 6c7d37b..0f6d475 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -762,17 +762,57 @@ class QAPISchemaEntity(object): self.name = name self.info = info + def c_name(self): + return c_name(self.name) + def check(self, schema): pass class QAPISchemaType(QAPISchemaEntity): - pass + def c_type(self, is_param=False): + return c_name(self.name) + pointer_suffix + + def c_null(self): + return 'NULL' + + def json_type(self): + pass + + def alternate_qtype(self): + json2qtype = { + 'string': 'QTYPE_QSTRING', + 'number': 'QTYPE_QFLOAT', + 'int': 'QTYPE_QINT', + 'boolean': 'QTYPE_QBOOL', + 'object': 'QTYPE_QDICT' + } + return json2qtype.get(self.json_type()) class QAPISchemaBuiltinType(QAPISchemaType): - def __init__(self, name): + def __init__(self, name, json_type, c_type, c_null): QAPISchemaType.__init__(self, name, None) + assert not c_type or isinstance(c_type, str) + assert json_type in ('string', 'number', 'int', 'boolean', 'null', + 'value') + self._json_type_name = json_type + self._c_type_name = c_type + self._c_null_val = c_null + + def c_name(self): + return self.name + + def c_type(self, is_param=False): + if is_param and self.name == 'str': + return 'const ' + self._c_type_name + return self._c_type_name + + def c_null(self): + return self._c_null_val + + def json_type(self): + return self._json_type_name class QAPISchemaEnumType(QAPISchemaType): @@ -785,6 +825,15 @@ class QAPISchemaEnumType(QAPISchemaType): def check(self, schema): assert len(set(self.values)) == len(self.values) + def c_type(self, is_param=False): + return c_name(self.name) + + def c_null(self): + return c_enum_const(self.name, (self.values + ['MAX'])[0]) + + def json_type(self): + return 'string' + class QAPISchemaArrayType(QAPISchemaType): def __init__(self, name, info, element_type): @@ -797,6 +846,9 @@ class QAPISchemaArrayType(QAPISchemaType): self.element_type = schema.lookup_type(self._element_type_name) assert self.element_type + def json_type(self): + return 'array' + class QAPISchemaObjectType(QAPISchemaType): def __init__(self, name, info, base, local_members, variants): @@ -834,6 +886,17 @@ class QAPISchemaObjectType(QAPISchemaType): self.variants.check(schema, members, seen) self.members = members + def c_name(self): + assert self.info + return QAPISchemaType.c_name(self) + + def c_type(self, is_param=False): + assert self.info + return QAPISchemaType.c_type(self) + + def json_type(self): + return 'object' + class QAPISchemaObjectTypeMember(object): def __init__(self, name, typ, optional): @@ -898,6 +961,9 @@ class QAPISchemaAlternateType(QAPISchemaType): def check(self, schema): self.variants.check(schema, [], {}) + def json_type(self): + return 'value' + class QAPISchemaCommand(QAPISchemaEntity): def __init__(self, name, info, arg_type, ret_type, gen, success_response): @@ -963,15 +1029,28 @@ class QAPISchema(object): def lookup_type(self, name): return self.lookup_entity(name, QAPISchemaType) - def _def_builtin_type(self, name): - self._def_entity(QAPISchemaBuiltinType(name)) + def _def_builtin_type(self, name, json_type, c_type, c_null): + self._def_entity(QAPISchemaBuiltinType(name, json_type, + c_type, c_null)) if name != '**': self._make_array_type(name) # TODO really needed? def _def_predefineds(self): - for t in ['str', 'number', 'int', 'int8', 'int16', 'int32', 'int64', - 'uint8', 'uint16', 'uint32', 'uint64', 'size', 'bool', '**']: - self._def_builtin_type(t) + for t in [('str', 'string', 'char' + pointer_suffix, 'NULL'), + ('number', 'number', 'double', '0'), + ('int', 'int', 'int64_t', '0'), + ('int8', 'int', 'int8_t', '0'), + ('int16', 'int', 'int16_t', '0'), + ('int32', 'int', 'int32_t', '0'), + ('int64', 'int', 'int64_t', '0'), + ('uint8', 'int', 'uint8_t', '0'), + ('uint16', 'int', 'uint16_t', '0'), + ('uint32', 'int', 'uint32_t', '0'), + ('uint64', 'int', 'uint64_t', '0'), + ('size', 'int', 'uint64_t', '0'), + ('bool', 'boolean', 'bool', 'false'), + ('**', 'value', None, None)]: + self._def_builtin_type(*t) def _make_implicit_enum_type(self, name, values): name = name + 'Kind'