summaryrefslogtreecommitdiffstats
path: root/scripts/qapi-types.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/qapi-types.py')
-rw-r--r--scripts/qapi-types.py147
1 files changed, 44 insertions, 103 deletions
diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
index 2bf8145..6bd0b13 100644
--- a/scripts/qapi-types.py
+++ b/scripts/qapi-types.py
@@ -11,10 +11,6 @@
from ordereddict import OrderedDict
from qapi import *
-import sys
-import os
-import getopt
-import errno
def generate_fwd_struct(name, members, builtin_type=False):
if builtin_type:
@@ -45,7 +41,7 @@ typedef struct %(name)sList
struct %(name)sList *next;
} %(name)sList;
''',
- name=name)
+ name=c_name(name))
def generate_fwd_enum_struct(name, members):
return mcgen('''
@@ -58,7 +54,7 @@ typedef struct %(name)sList
struct %(name)sList *next;
} %(name)sList;
''',
- name=name)
+ name=c_name(name))
def generate_struct_fields(members):
ret = ''
@@ -68,11 +64,11 @@ def generate_struct_fields(members):
ret += mcgen('''
bool has_%(c_name)s;
''',
- c_name=c_var(argname))
+ c_name=c_name(argname))
ret += mcgen('''
%(c_type)s %(c_name)s;
''',
- c_type=c_type(argentry), c_name=c_var(argname))
+ c_type=c_type(argentry), c_name=c_name(argname))
return ret
@@ -87,7 +83,7 @@ def generate_struct(expr):
struct %(name)s
{
''',
- name=structname)
+ name=c_name(structname))
if base:
ret += generate_struct_fields({'base': base})
@@ -115,16 +111,16 @@ def generate_enum_lookup(name, values):
ret = mcgen('''
const char *%(name)s_lookup[] = {
''',
- name=name)
+ name=c_name(name))
i = 0
for value in values:
- index = generate_enum_full_value(name, value)
+ index = c_enum_const(name, value)
ret += mcgen('''
[%(index)s] = "%(value)s",
''',
index = index, value = value)
- max_index = generate_enum_full_value(name, 'MAX')
+ max_index = c_enum_const(name, 'MAX')
ret += mcgen('''
[%(max_index)s] = NULL,
};
@@ -134,6 +130,7 @@ const char *%(name)s_lookup[] = {
return ret
def generate_enum(name, values):
+ name = c_name(name)
lookup_decl = mcgen('''
extern const char *%(name)s_lookup[];
''',
@@ -150,7 +147,7 @@ typedef enum %(name)s
i = 0
for value in enum_values:
- enum_full_value = generate_enum_full_value(name, value)
+ enum_full_value = c_enum_const(name, value)
enum_decl += mcgen('''
%(enum_full_value)s = %(i)d,
''',
@@ -173,18 +170,17 @@ def generate_alternate_qtypes(expr):
ret = mcgen('''
const int %(name)s_qtypes[QTYPE_MAX] = {
''',
- name=name)
+ name=c_name(name))
for key in members:
qtype = find_alternate_member_qtype(members[key])
assert qtype, "Invalid alternate member"
ret += mcgen('''
- [ %(qtype)s ] = %(abbrev)s_KIND_%(enum)s,
+ [%(qtype)s] = %(enum_const)s,
''',
- qtype = qtype,
- abbrev = de_camel_case(name).upper(),
- enum = c_fun(de_camel_case(key),False).upper())
+ qtype = qtype,
+ enum_const = c_enum_const(name + 'Kind', key))
ret += mcgen('''
};
@@ -194,7 +190,7 @@ const int %(name)s_qtypes[QTYPE_MAX] = {
def generate_union(expr, meta):
- name = expr[meta]
+ name = c_name(expr[meta])
typeinfo = expr['data']
base = expr.get('base')
@@ -214,14 +210,14 @@ struct %(name)s
void *data;
''',
name=name,
- discriminator_type_name=discriminator_type_name)
+ discriminator_type_name=c_name(discriminator_type_name))
for key in typeinfo:
ret += mcgen('''
%(c_type)s %(c_name)s;
''',
c_type=c_type(typeinfo[key]),
- c_name=c_fun(key))
+ c_name=c_name(key))
ret += mcgen('''
};
@@ -249,15 +245,15 @@ extern const int %(name)s_qtypes[];
def generate_type_cleanup_decl(name):
ret = mcgen('''
-void qapi_free_%(type)s(%(c_type)s obj);
+void qapi_free_%(name)s(%(c_type)s obj);
''',
- c_type=c_type(name),type=name)
+ c_type=c_type(name), name=c_name(name))
return ret
def generate_type_cleanup(name):
ret = mcgen('''
-void qapi_free_%(type)s(%(c_type)s obj)
+void qapi_free_%(name)s(%(c_type)s obj)
{
QapiDeallocVisitor *md;
Visitor *v;
@@ -268,72 +264,23 @@ void qapi_free_%(type)s(%(c_type)s obj)
md = qapi_dealloc_visitor_new();
v = qapi_dealloc_get_visitor(md);
- visit_type_%(type)s(v, &obj, NULL, NULL);
+ visit_type_%(name)s(v, &obj, NULL, NULL);
qapi_dealloc_visitor_cleanup(md);
}
''',
- c_type=c_type(name),type=name)
+ c_type=c_type(name), name=c_name(name))
return ret
-
-try:
- opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:i:o:",
- ["source", "header", "builtins",
- "prefix=", "input-file=", "output-dir="])
-except getopt.GetoptError, err:
- print str(err)
- sys.exit(1)
-
-output_dir = ""
-input_file = ""
-prefix = ""
-c_file = 'qapi-types.c'
-h_file = 'qapi-types.h'
-
-do_c = False
-do_h = False
do_builtins = False
+(input_file, output_dir, do_c, do_h, prefix, opts) = \
+ parse_command_line("b", ["builtins"])
+
for o, a in opts:
- if o in ("-p", "--prefix"):
- prefix = a
- elif o in ("-i", "--input-file"):
- input_file = a
- elif o in ("-o", "--output-dir"):
- output_dir = a + "/"
- elif o in ("-c", "--source"):
- do_c = True
- elif o in ("-h", "--header"):
- do_h = True
- elif o in ("-b", "--builtins"):
+ if o in ("-b", "--builtins"):
do_builtins = True
-if not do_c and not do_h:
- do_c = True
- do_h = True
-
-c_file = output_dir + prefix + c_file
-h_file = output_dir + prefix + h_file
-
-try:
- os.makedirs(output_dir)
-except os.error, e:
- if e.errno != errno.EEXIST:
- raise
-
-def maybe_open(really, name, opt):
- if really:
- return open(name, opt)
- else:
- import StringIO
- return StringIO.StringIO()
-
-fdef = maybe_open(do_c, c_file, 'w')
-fdecl = maybe_open(do_h, h_file, 'w')
-
-fdef.write(mcgen('''
-/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
+c_comment = '''
/*
* deallocation functions for schema-defined QAPI types
*
@@ -347,16 +294,8 @@ fdef.write(mcgen('''
* See the COPYING.LIB file in the top-level directory.
*
*/
-
-#include "qapi/dealloc-visitor.h"
-#include "%(prefix)sqapi-types.h"
-#include "%(prefix)sqapi-visit.h"
-
-''', prefix=prefix))
-
-fdecl.write(mcgen('''
-/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
+'''
+h_comment = '''
/*
* schema-defined QAPI types
*
@@ -369,15 +308,25 @@ fdecl.write(mcgen('''
* See the COPYING.LIB file in the top-level directory.
*
*/
+'''
-#ifndef %(guard)s
-#define %(guard)s
+(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
+ 'qapi-types.c', 'qapi-types.h',
+ c_comment, h_comment)
+fdef.write(mcgen('''
+#include "qapi/dealloc-visitor.h"
+#include "%(prefix)sqapi-types.h"
+#include "%(prefix)sqapi-visit.h"
+
+''',
+ prefix=prefix))
+
+fdecl.write(mcgen('''
#include <stdbool.h>
#include <stdint.h>
-''',
- guard=guardname(h_file)))
+'''))
exprs = parse_schema(input_file)
exprs = filter(lambda expr: not expr.has_key('gen'), exprs)
@@ -455,12 +404,4 @@ for expr in exprs:
continue
fdecl.write(ret)
-fdecl.write('''
-#endif
-''')
-
-fdecl.flush()
-fdecl.close()
-
-fdef.flush()
-fdef.close()
+close_output(fdef, fdecl)
OpenPOWER on IntegriCloud