mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2025-01-22 19:40:58 +00:00
qapi: New QAPISchemaVisitor
The visitor will help keeping the code generation code simple and reasonably separated from QAPISchema details. Backports commit 3f7dc21bee1e930d5cccf607b8f83831c3bbdb09 from qemu
This commit is contained in:
parent
8ce395e073
commit
72f6d68002
|
@ -769,6 +769,39 @@ class QAPISchemaEntity(object):
|
|||
def check(self, schema):
|
||||
pass
|
||||
|
||||
def visit(self, visitor):
|
||||
pass
|
||||
|
||||
|
||||
class QAPISchemaVisitor(object):
|
||||
def visit_begin(self, schema):
|
||||
pass
|
||||
|
||||
def visit_end(self):
|
||||
pass
|
||||
|
||||
def visit_builtin_type(self, name, info, json_type):
|
||||
pass
|
||||
|
||||
def visit_enum_type(self, name, info, values, prefix):
|
||||
pass
|
||||
|
||||
def visit_array_type(self, name, info, element_type):
|
||||
pass
|
||||
|
||||
def visit_object_type(self, name, info, base, members, variants):
|
||||
pass
|
||||
|
||||
def visit_alternate_type(self, name, info, variants):
|
||||
pass
|
||||
|
||||
def visit_command(self, name, info, arg_type, ret_type,
|
||||
gen, success_response):
|
||||
pass
|
||||
|
||||
def visit_event(self, name, info, arg_type):
|
||||
pass
|
||||
|
||||
|
||||
class QAPISchemaType(QAPISchemaEntity):
|
||||
def c_type(self, is_param=False):
|
||||
|
@ -815,6 +848,9 @@ class QAPISchemaBuiltinType(QAPISchemaType):
|
|||
def json_type(self):
|
||||
return self._json_type_name
|
||||
|
||||
def visit(self, visitor):
|
||||
visitor.visit_builtin_type(self.name, self.info, self.json_type())
|
||||
|
||||
|
||||
class QAPISchemaEnumType(QAPISchemaType):
|
||||
def __init__(self, name, info, values, prefix):
|
||||
|
@ -838,6 +874,10 @@ class QAPISchemaEnumType(QAPISchemaType):
|
|||
def json_type(self):
|
||||
return 'string'
|
||||
|
||||
def visit(self, visitor):
|
||||
visitor.visit_enum_type(self.name, self.info,
|
||||
self.values, self.prefix)
|
||||
|
||||
|
||||
class QAPISchemaArrayType(QAPISchemaType):
|
||||
def __init__(self, name, info, element_type):
|
||||
|
@ -853,6 +893,9 @@ class QAPISchemaArrayType(QAPISchemaType):
|
|||
def json_type(self):
|
||||
return 'array'
|
||||
|
||||
def visit(self, visitor):
|
||||
visitor.visit_array_type(self.name, self.info, self.element_type)
|
||||
|
||||
|
||||
class QAPISchemaObjectType(QAPISchemaType):
|
||||
def __init__(self, name, info, base, local_members, variants):
|
||||
|
@ -901,6 +944,10 @@ class QAPISchemaObjectType(QAPISchemaType):
|
|||
def json_type(self):
|
||||
return 'object'
|
||||
|
||||
def visit(self, visitor):
|
||||
visitor.visit_object_type(self.name, self.info,
|
||||
self.base, self.local_members, self.variants)
|
||||
|
||||
|
||||
class QAPISchemaObjectTypeMember(object):
|
||||
def __init__(self, name, typ, optional):
|
||||
|
@ -968,6 +1015,9 @@ class QAPISchemaAlternateType(QAPISchemaType):
|
|||
def json_type(self):
|
||||
return 'value'
|
||||
|
||||
def visit(self, visitor):
|
||||
visitor.visit_alternate_type(self.name, self.info, self.variants)
|
||||
|
||||
|
||||
class QAPISchemaCommand(QAPISchemaEntity):
|
||||
def __init__(self, name, info, arg_type, ret_type, gen, success_response):
|
||||
|
@ -990,6 +1040,11 @@ class QAPISchemaCommand(QAPISchemaEntity):
|
|||
self.ret_type = schema.lookup_type(self._ret_type_name)
|
||||
assert isinstance(self.ret_type, QAPISchemaType)
|
||||
|
||||
def visit(self, visitor):
|
||||
visitor.visit_command(self.name, self.info,
|
||||
self.arg_type, self.ret_type,
|
||||
self.gen, self.success_response)
|
||||
|
||||
|
||||
class QAPISchemaEvent(QAPISchemaEntity):
|
||||
def __init__(self, name, info, arg_type):
|
||||
|
@ -1004,6 +1059,9 @@ class QAPISchemaEvent(QAPISchemaEntity):
|
|||
assert isinstance(self.arg_type, QAPISchemaObjectType)
|
||||
assert not self.arg_type.variants # not implemented
|
||||
|
||||
def visit(self, visitor):
|
||||
visitor.visit_event(self.name, self.info, self.arg_type)
|
||||
|
||||
|
||||
class QAPISchema(object):
|
||||
def __init__(self, fname):
|
||||
|
@ -1201,6 +1259,12 @@ class QAPISchema(object):
|
|||
for ent in self._entity_dict.values():
|
||||
ent.check(self)
|
||||
|
||||
def visit(self, visitor):
|
||||
visitor.visit_begin(self)
|
||||
for name in sorted(self._entity_dict.keys()):
|
||||
self._entity_dict[name].visit(visitor)
|
||||
visitor.visit_end()
|
||||
|
||||
#
|
||||
# Code generation helpers
|
||||
#
|
||||
|
|
Loading…
Reference in a new issue