The visitor will help keeping the code generation code simple and reasonably separated from QAPISchema details.
Signed-off-by: Markus Armbruster <arm...@redhat.com> Reviewed-by: Eric Blake <ebl...@redhat.com> --- scripts/qapi.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/scripts/qapi.py b/scripts/qapi.py index cf1436d..3e7c154 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -764,6 +764,29 @@ class QAPISchemaEntity(object): return c_name(self.name) 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): + 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): @@ -801,6 +824,8 @@ class QAPISchemaBuiltinType(QAPISchemaType): return self.c_null_val 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): @@ -816,6 +841,8 @@ class QAPISchemaEnumType(QAPISchemaType): return c_enum_const(self.name, (self.values + ['MAX'])[0]) def json_type(self): return 'string' + def visit(self, visitor): + visitor.visit_enum_type(self.name, self.info, self.values) class QAPISchemaArrayType(QAPISchemaType): def __init__(self, name, info, element_type): @@ -828,6 +855,8 @@ class QAPISchemaArrayType(QAPISchemaType): assert self.element_type 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): @@ -871,6 +900,9 @@ class QAPISchemaObjectType(QAPISchemaType): return QAPISchemaType.c_type(self) 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): @@ -936,6 +968,8 @@ class QAPISchemaAlternateType(QAPISchemaType): self.variants.check(schema, [], {}) 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): @@ -956,6 +990,10 @@ class QAPISchemaCommand(QAPISchemaEntity): if self.ret_type_name: 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): @@ -968,6 +1006,8 @@ class QAPISchemaEvent(QAPISchemaEntity): self.arg_type = schema.lookup_type(self.arg_type_name) 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): @@ -1161,6 +1201,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 # -- 2.4.3