diff mbox

[RFC,v3,04/32] qapi: New QAPISchemaVisitor

Message ID 1438703896-12553-5-git-send-email-armbru@redhat.com
State New
Headers show

Commit Message

Markus Armbruster Aug. 4, 2015, 3:57 p.m. UTC
The visitor will help keeping the code generation code simple and
reasonably separated from QAPISchema details.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 scripts/qapi.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

Comments

Eric Blake Aug. 4, 2015, 10:26 p.m. UTC | #1
On 08/04/2015 09:57 AM, Markus Armbruster wrote:
> The visitor will help keeping the code generation code simple and
> reasonably separated from QAPISchema details.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> Reviewed-by: Eric Blake <eblake@redhat.com>
> ---
>  scripts/qapi.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 46 insertions(+)
> 
> diff --git a/scripts/qapi.py b/scripts/qapi.py
> index 3c596c3..019d22c 100644
> --- a/scripts/qapi.py
> +++ b/scripts/qapi.py
> @@ -771,6 +771,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):
> +        pass

Don't know if you wanted to hoist from later patches, and write this as:

def visit_begin(self, schema):

> @@ -1166,6 +1206,12 @@ class QAPISchema(object):
>          for ent in self.entity_dict.values():
>              ent.check(self)
>  
> +    def visit(self, visitor):
> +        visitor.visit_begin()

and this as

visitor.visit_begin(self)

up front, for less churn later on.  Not the end of the world to leave it
as is, so my R-b stands either way.
Markus Armbruster Aug. 5, 2015, 6:24 a.m. UTC | #2
Eric Blake <eblake@redhat.com> writes:

> On 08/04/2015 09:57 AM, Markus Armbruster wrote:
>> The visitor will help keeping the code generation code simple and
>> reasonably separated from QAPISchema details.
>> 
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> Reviewed-by: Eric Blake <eblake@redhat.com>
>> ---
>>  scripts/qapi.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++
>>  1 file changed, 46 insertions(+)
>> 
>> diff --git a/scripts/qapi.py b/scripts/qapi.py
>> index 3c596c3..019d22c 100644
>> --- a/scripts/qapi.py
>> +++ b/scripts/qapi.py
>> @@ -771,6 +771,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):
>> +        pass
>
> Don't know if you wanted to hoist from later patches, and write this as:
>
> def visit_begin(self, schema):
>
>> @@ -1166,6 +1206,12 @@ class QAPISchema(object):
>>          for ent in self.entity_dict.values():
>>              ent.check(self)
>>  
>> +    def visit(self, visitor):
>> +        visitor.visit_begin()
>
> and this as
>
> visitor.visit_begin(self)
>
> up front, for less churn later on.  Not the end of the world to leave it
> as is, so my R-b stands either way.

I ran out of time.  Perhaps I can still try in the next iteration.
diff mbox

Patch

diff --git a/scripts/qapi.py b/scripts/qapi.py
index 3c596c3..019d22c 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -771,6 +771,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):
+        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):
@@ -808,6 +831,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):
@@ -823,6 +848,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):
@@ -833,6 +860,8 @@  class QAPISchemaArrayType(QAPISchemaType):
     def check(self, schema):
         self.element_type = schema.lookup_type(self.element_type_name)
         assert self.element_type
+    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):
@@ -876,6 +905,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):
@@ -939,6 +971,8 @@  class QAPISchemaAlternateType(QAPISchemaType):
         self.variants = variants
     def check(self, schema):
         self.variants.check(schema, [], {})
+    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):
@@ -959,6 +993,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):
@@ -971,6 +1009,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):
@@ -1166,6 +1206,12 @@  class QAPISchema(object):
         for ent in self.entity_dict.values():
             ent.check(self)
 
+    def visit(self, visitor):
+        visitor.visit_begin()
+        for name in sorted(self.entity_dict.keys()):
+            self.entity_dict[name].visit(visitor)
+        visitor.visit_end()
+
 #
 # Code generation helpers
 #