diff mbox

[v8,11/18] qapi: Simplify gen_struct_field()

Message ID 1444710158-8723-12-git-send-email-eblake@redhat.com
State New
Headers show

Commit Message

Eric Blake Oct. 13, 2015, 4:22 a.m. UTC
Rather than having all callers pass a name, type, and optional
flag, have them instead pass a QAPISchemaObjectTypeMember which
already has all that information.

This will allow a future patch to simplify alternates to use
a special tag 'qtype_code type'.  In the meantime, it requires
a hack to create a temporary member 'base' for struct base
classes; this temporary member will go away in a later patch
that flattens structs in the same way that flat union base
classes were flattened in commit 1e6c1616.

No change to generated code.

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v8: new patch
---
 scripts/qapi-types.py | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

Comments

Markus Armbruster Oct. 13, 2015, 12:12 p.m. UTC | #1
Eric Blake <eblake@redhat.com> writes:

> Rather than having all callers pass a name, type, and optional
> flag, have them instead pass a QAPISchemaObjectTypeMember which
> already has all that information.
>
> This will allow a future patch to simplify alternates to use
> a special tag 'qtype_code type'.  In the meantime, it requires
> a hack to create a temporary member 'base' for struct base
> classes; this temporary member will go away in a later patch
> that flattens structs in the same way that flat union base
> classes were flattened in commit 1e6c1616.
>
> No change to generated code.
>
> Signed-off-by: Eric Blake <eblake@redhat.com>
>
> ---
> v8: new patch
> ---
>  scripts/qapi-types.py | 16 ++++++++++------
>  1 file changed, 10 insertions(+), 6 deletions(-)
>
> diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
> index e37d529..5ffabf5 100644
> --- a/scripts/qapi-types.py
> +++ b/scripts/qapi-types.py
> @@ -36,18 +36,18 @@ struct %(c_name)s {
>                   c_name=c_name(name), c_type=element_type.c_type())
>
>
> -def gen_struct_field(name, typ, optional):
> +def gen_struct_field(member):
>      ret = ''
>
> -    if optional:
> +    if member.optional:
>          ret += mcgen('''
>      bool has_%(c_name)s;
>  ''',
> -                     c_name=c_name(name))
> +                     c_name=member.c_name())
>      ret += mcgen('''
>      %(c_type)s %(c_name)s;
>  ''',
> -                 c_type=typ.c_type(), c_name=c_name(name))
> +                 c_type=member.type.c_type(), c_name=member.c_name())
>      return ret
>
>
> @@ -55,7 +55,7 @@ def gen_struct_fields(members):
>      ret = ''
>
>      for memb in members:
> -        ret += gen_struct_field(memb.name, memb.type, memb.optional)
> +        ret += gen_struct_field(memb)
>      return ret
>
>
> @@ -67,7 +67,11 @@ struct %(c_name)s {
>                  c_name=c_name(name))
>
>      if base:
> -        ret += gen_struct_field('base', base, False)
> +        # TODO Flatten this struct, similar to flat union bases. Until
> +        # then, hack around it with a temporary member.
> +        member = QAPISchemaObjectTypeMember('base', base.name, False)
> +        member.type = base
> +        ret += gen_struct_field(member)
>
>      ret += gen_struct_fields(members)

Uff!  Are you really, really sure this is the right spot in the series
for this transformation?
Eric Blake Oct. 13, 2015, 1:11 p.m. UTC | #2
On 10/13/2015 06:12 AM, Markus Armbruster wrote:
> Eric Blake <eblake@redhat.com> writes:
> 
>> Rather than having all callers pass a name, type, and optional
>> flag, have them instead pass a QAPISchemaObjectTypeMember which
>> already has all that information.
>>
>> This will allow a future patch to simplify alternates to use
>> a special tag 'qtype_code type'.  In the meantime, it requires
>> a hack to create a temporary member 'base' for struct base
>> classes; this temporary member will go away in a later patch
>> that flattens structs in the same way that flat union base
>> classes were flattened in commit 1e6c1616.
>>
>> No change to generated code.
>>
>> Signed-off-by: Eric Blake <eblake@redhat.com>
>>

>>
>>      if base:
>> -        ret += gen_struct_field('base', base, False)
>> +        # TODO Flatten this struct, similar to flat union bases. Until
>> +        # then, hack around it with a temporary member.
>> +        member = QAPISchemaObjectTypeMember('base', base.name, False)
>> +        member.type = base
>> +        ret += gen_struct_field(member)
>>
>>      ret += gen_struct_fields(members)
> 
> Uff!  Are you really, really sure this is the right spot in the series
> for this transformation?

Serves me right for sending the series late at night. I can probably
delay this transformation to a later subset, after I have unboxed struct
base members, so I won't need this hack.  And I just confirmed that
nothing else in subset B depends on this patch, so let's defer it to
subset C or later (dropping from this series is not sufficient reason
for a v9 respin, but if something else turns up that needs a v9, this
patch has been moved out).
diff mbox

Patch

diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
index e37d529..5ffabf5 100644
--- a/scripts/qapi-types.py
+++ b/scripts/qapi-types.py
@@ -36,18 +36,18 @@  struct %(c_name)s {
                  c_name=c_name(name), c_type=element_type.c_type())


-def gen_struct_field(name, typ, optional):
+def gen_struct_field(member):
     ret = ''

-    if optional:
+    if member.optional:
         ret += mcgen('''
     bool has_%(c_name)s;
 ''',
-                     c_name=c_name(name))
+                     c_name=member.c_name())
     ret += mcgen('''
     %(c_type)s %(c_name)s;
 ''',
-                 c_type=typ.c_type(), c_name=c_name(name))
+                 c_type=member.type.c_type(), c_name=member.c_name())
     return ret


@@ -55,7 +55,7 @@  def gen_struct_fields(members):
     ret = ''

     for memb in members:
-        ret += gen_struct_field(memb.name, memb.type, memb.optional)
+        ret += gen_struct_field(memb)
     return ret


@@ -67,7 +67,11 @@  struct %(c_name)s {
                 c_name=c_name(name))

     if base:
-        ret += gen_struct_field('base', base, False)
+        # TODO Flatten this struct, similar to flat union bases. Until
+        # then, hack around it with a temporary member.
+        member = QAPISchemaObjectTypeMember('base', base.name, False)
+        member.type = base
+        ret += gen_struct_field(member)

     ret += gen_struct_fields(members)