qapi: QAPISchema code generation helper methods

New methods c_name(), c_type(), c_null(), json_type(),
alternate_qtype().

Backports commit f51d8c3db11b0f3052b3bb4b8b0c7f0bc76f7136 from qemu
This commit is contained in:
Markus Armbruster 2018-02-19 16:47:32 -05:00 committed by Lioncash
parent a3660e451d
commit 8ce395e073
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -763,17 +763,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):
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):
@ -788,6 +828,16 @@ 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],
self.prefix)
def json_type(self):
return 'string'
class QAPISchemaArrayType(QAPISchemaType):
def __init__(self, name, info, element_type):
@ -800,6 +850,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):
@ -837,6 +890,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):
@ -901,6 +965,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):
@ -966,15 +1033,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'