qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Michael Roth <mdroth@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: aliguori@linux.vnet.ibm.com, Jes.Sorensen@redhat.com,
	agl@linux.vnet.ibm.com, mdroth@linux.vnet.ibm.com,
	lcapitulino@redhat.com
Subject: [Qemu-devel] [PATCH v3 13/21] qapi: add qapi-visit.py code generator
Date: Mon, 13 Jun 2011 21:31:18 -0500	[thread overview]
Message-ID: <1308018686-8235-14-git-send-email-mdroth@linux.vnet.ibm.com> (raw)
In-Reply-To: <1308018686-8235-1-git-send-email-mdroth@linux.vnet.ibm.com>

This is the code generator for qapi visiter functions used to
marshal/unmarshal/dealloc qapi types. It generates the following 2
files:

  $(prefix)qapi-visit.c: visiter function for a particular c type, used
                         to automagically convert qobjects into the
                         corresponding C type and vice-versa, and well
                         as for deallocation memory for an existing C
                         type

  $(prefix)qapi-visit.h: declarations for previously mentioned visiter
                         functions

$(prefix) is used as decribed for qapi-types.py

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
 scripts/qapi-visit.py |  235 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 235 insertions(+), 0 deletions(-)
 create mode 100644 scripts/qapi-visit.py

diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
new file mode 100644
index 0000000..0eb45c9
--- /dev/null
+++ b/scripts/qapi-visit.py
@@ -0,0 +1,235 @@
+#
+# QAPI visitor generator
+#
+# Copyright IBM, Corp. 2011
+#
+# Authors:
+#  Anthony Liguori <aliguori@us.ibm.com>
+#  Michael Roth    <mdroth@linux.vnet.ibm.com>
+#
+# This work is licensed under the terms of the GNU GPLv2.
+# See the COPYING.LIB file in the top-level directory.
+
+from ordereddict import OrderedDict
+from qapi import *
+import sys
+import os
+import getopt
+
+def generate_visit_struct_body(field_prefix, members):
+    ret = ""
+    if len(field_prefix):
+        field_prefix = field_prefix + "."
+    for argname, argentry, optional, structured in parse_args(members):
+        if optional:
+            ret += mcgen('''
+visit_start_optional(m, (obj && *obj) ? &(*obj)->%(c_prefix)shas_%(c_name)s : NULL, "%(name)s", errp);
+if ((*obj)->%(prefix)shas_%(c_name)s) {
+''',
+                         c_prefix=c_var(field_prefix), prefix=field_prefix,
+                         c_name=c_var(argname), name=argname)
+            push_indent()
+
+        if structured:
+            ret += mcgen('''
+visit_start_struct(m, NULL, "", "%(name)s", 0, errp);
+''',
+                         name=argname)
+            ret += generate_visit_struct_body(field_prefix + argname, argentry)
+            ret += mcgen('''
+visit_end_struct(m, errp);
+''')
+        else:
+            ret += mcgen('''
+visit_type_%(type)s(m, (obj && *obj) ? &(*obj)->%(c_prefix)s%(c_name)s : NULL, "%(name)s", errp);
+''',
+                         c_prefix=c_var(field_prefix), prefix=field_prefix,
+                         type=type_name(argentry), c_name=c_var(argname),
+                         name=argname)
+
+        if optional:
+            pop_indent()
+            ret += mcgen('''
+}
+visit_end_optional(m, errp);
+''')
+    return ret
+
+def generate_visit_struct(name, members):
+    ret = mcgen('''
+
+void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
+{
+    visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), errp);
+''',
+                name=name)
+    push_indent()
+    ret += generate_visit_struct_body("", members)
+    pop_indent()
+
+    ret += mcgen('''
+    visit_end_struct(m, errp);
+}
+''')
+    return ret
+
+def generate_visit_list(name, members):
+    return mcgen('''
+
+void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp)
+{
+    GenericList *i;
+
+    visit_start_list(m, name, errp);
+
+    for (i = visit_next_list(m, (GenericList **)obj, errp); i; i = visit_next_list(m, &i, errp)) {
+        %(name)sList *native_i = (%(name)sList *)i;
+        visit_type_%(name)s(m, &native_i->value, NULL, errp);
+    }
+
+    visit_end_list(m, errp);
+}
+''',
+                name=name)
+
+def generate_visit_handle(name, typeinfo):
+    return mcgen('''
+
+void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
+{
+    visit_start_handle(m, (void **)obj, "%(name)s", name, errp);
+    visit_type_%(type_name)s(m, &(*obj)->handle, "handle", errp);
+    visit_end_handle(m, errp);
+}
+''',
+                name=name, type_name=type_name(typeinfo))
+
+def generate_visit_enum(name, members):
+    return mcgen('''
+
+void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp)
+{
+    visit_type_enum(m, (int *)obj, "%(name)s", name, errp);
+}
+''',
+                 name=name)
+
+def generate_visit_union(name, members):
+    ret = generate_visit_enum('%sKind' % name, members.keys())
+
+    ret += mcgen('''
+
+void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
+{
+}
+''',
+                 name=name)
+
+    return ret
+
+def generate_declaration(name, members, genlist=True):
+    ret = mcgen('''
+
+void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp);
+''',
+                name=name)
+
+    if genlist:
+        ret += mcgen('''
+void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
+''',
+                 name=name)
+
+    return ret
+
+def generate_decl_enum(name, members, genlist=True):
+    return mcgen('''
+
+void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
+''',
+                name=name)
+
+try:
+    opts, args = getopt.gnu_getopt(sys.argv[1:], "p:o:", ["prefix=", "output-dir="])
+except getopt.GetoptError, err:
+    print str(err)
+    sys.exit(1)
+
+output_dir = ""
+prefix = ""
+c_file = 'qapi-visit.c'
+h_file = 'qapi-visit.h'
+
+for o, a in opts:
+    if o in ("-p", "--prefix"):
+        prefix = a
+    elif o in ("-o", "--output-dir"):
+        output_dir = a + "/"
+
+c_file = output_dir + prefix + c_file
+h_file = output_dir + prefix + h_file
+
+if os.path.isdir(output_dir) == False:
+    os.makedirs(output_dir)
+
+fdef = open(c_file, 'w')
+fdecl = open(h_file, 'w')
+
+fdef.write(mcgen('''
+/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
+
+#include "%(header)s"
+''',
+                 header=basename(h_file)))
+
+fdecl.write(mcgen('''
+/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
+
+#ifndef %(guard)s
+#define %(guard)s
+
+#include "qapi/qapi-visit-core.h"
+#include "%(prefix)sqapi-types.h"
+''',
+                  prefix=prefix, guard=guardname(h_file)))
+
+exprs = parse_schema(sys.stdin)
+
+for expr in exprs:
+    if expr.has_key('type'):
+        ret = generate_visit_struct(expr['type'], expr['data'])
+        ret += generate_visit_list(expr['type'], expr['data'])
+        fdef.write(ret)
+
+        ret = generate_declaration(expr['type'], expr['data'])
+        fdecl.write(ret)
+    elif expr.has_key('union'):
+        ret = generate_visit_union(expr['union'], expr['data'])
+        fdef.write(ret)
+
+        ret = generate_decl_enum('%sKind' % expr['union'], expr['data'].keys())
+        ret += generate_declaration(expr['union'], expr['data'])
+        fdecl.write(ret)
+    elif expr.has_key('enum'):
+        ret = generate_visit_enum(expr['enum'], expr['data'])
+        fdef.write(ret)
+
+        ret = generate_decl_enum(expr['enum'], expr['data'])
+        fdecl.write(ret)
+    elif expr.has_key('handle'):
+        ret = generate_visit_handle(expr['handle'], expr['data'])
+        fdef.write(ret)
+
+        ret = generate_declaration(expr['handle'], expr['data'], False)
+        fdecl.write(ret)
+
+fdecl.write('''
+#endif
+''')
+
+fdecl.flush()
+fdecl.close()
+
+fdef.flush()
+fdef.close()
+
-- 
1.7.0.4

  parent reply	other threads:[~2011-06-14  2:33 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-14  2:31 [Qemu-devel] [QAPI+QGA 2/3] QAPI code generation infrastructure v3 Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 01/21] Add hard build dependency on glib Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 02/21] qlist: add qlist_first()/qlist_next() Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 03/21] qapi: add module init types for qapi Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 04/21] qapi: add QAPI visitor core Michael Roth
2011-06-15 14:33   ` Luiz Capitulino
2011-06-15 14:54     ` Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 05/21] qapi: add QMP input visitor Michael Roth
2011-06-15 16:11   ` Luiz Capitulino
2011-06-15 16:56     ` Michael Roth
2011-06-15 17:02       ` Luiz Capitulino
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 06/21] qapi: add QMP output visitor Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 07/21] qapi: add QAPI dealloc visitor Michael Roth
2011-06-15 18:25   ` Luiz Capitulino
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 08/21] qapi: add QMP command registration/lookup functions Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 09/21] qapi: add QMP dispatch functions Michael Roth
2011-06-15 19:33   ` Luiz Capitulino
2011-06-15 19:45     ` Anthony Liguori
2011-06-15 20:12       ` Luiz Capitulino
2011-06-15 20:45         ` Michael Roth
2011-06-15 20:48           ` Luiz Capitulino
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 10/21] qapi: add ordereddict.py helper library Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 11/21] qapi: add qapi.py helper libraries Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 12/21] qapi: add qapi-types.py code generator Michael Roth
2011-06-15 19:11   ` Luiz Capitulino
2011-06-14  2:31 ` Michael Roth [this message]
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 14/21] qapi: add qapi-commands.py " Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 15/21] qapi: test schema used for unit tests Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 16/21] qapi: add test-visitor, tests for gen. visitor code Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 17/21] qapi: configure, Fix build issue when using seperate build dir Michael Roth
2011-06-15 19:34   ` Luiz Capitulino
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 18/21] qapi: Makefile changes to build test-visitor Michael Roth
2011-06-15 19:35   ` Luiz Capitulino
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 19/21] qapi: add test-qmp-commands, tests for gen. marshalling/dispatch code Michael Roth
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 20/21] qapi: Makefile changes to build test-qmp-commands Michael Roth
2011-06-15 19:35   ` Luiz Capitulino
2011-06-14  2:31 ` [Qemu-devel] [PATCH v3 21/21] qapi: add QAPI code generation documentation Michael Roth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1308018686-8235-14-git-send-email-mdroth@linux.vnet.ibm.com \
    --to=mdroth@linux.vnet.ibm.com \
    --cc=Jes.Sorensen@redhat.com \
    --cc=agl@linux.vnet.ibm.com \
    --cc=aliguori@linux.vnet.ibm.com \
    --cc=lcapitulino@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).