From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:32841) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZXVX6-0004O7-9f for qemu-devel@nongnu.org; Thu, 03 Sep 2015 10:30:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZXVX1-0005bL-Mn for qemu-devel@nongnu.org; Thu, 03 Sep 2015 10:30:32 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45357) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZXVX1-0005ao-Fp for qemu-devel@nongnu.org; Thu, 03 Sep 2015 10:30:27 -0400 From: Markus Armbruster Date: Thu, 3 Sep 2015 16:29:54 +0200 Message-Id: <1441290623-13631-4-git-send-email-armbru@redhat.com> In-Reply-To: <1441290623-13631-1-git-send-email-armbru@redhat.com> References: <1441290623-13631-1-git-send-email-armbru@redhat.com> Subject: [Qemu-devel] [PATCH RFC v4 03/32] qapi: QAPISchema code generation helper methods List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: mdroth@linux.vnet.ibm.com New methods c_name(), c_type(), c_null(), json_type(), alternate_qtype(). Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- scripts/qapi.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 7 deletions(-) diff --git a/scripts/qapi.py b/scripts/qapi.py index c42bea1..cf1436d 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -760,15 +760,47 @@ class QAPISchemaEntity(object): assert isinstance(name, str) 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): def __init__(self, name, info, values): @@ -778,6 +810,12 @@ class QAPISchemaEnumType(QAPISchemaType): self.values = values 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): @@ -788,6 +826,8 @@ class QAPISchemaArrayType(QAPISchemaType): def check(self, schema): 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): @@ -823,6 +863,14 @@ class QAPISchemaObjectType(QAPISchemaType): if self.variants: 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): @@ -886,6 +934,8 @@ class QAPISchemaAlternateType(QAPISchemaType): self.variants = variants 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): @@ -947,15 +997,27 @@ 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' -- 2.4.3