diff options
author | Peter Maydell <peter.maydell@linaro.org> | 2015-05-15 17:51:20 +0100 |
---|---|---|
committer | Peter Maydell <peter.maydell@linaro.org> | 2015-05-15 17:51:20 +0100 |
commit | 385057cbec9b4a0eb6150330c572e875ed714965 (patch) | |
tree | 7adc24566bfe8afc56aee25128b2fc3bd5a6a9a5 /scripts/qapi-types.py | |
parent | 99e7627a70d1a23e30a514e5a4798005cf4eb3aa (diff) | |
parent | 4180978c9205c50acd2d6c385def9b3e81911696 (diff) |
Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2015-05-15' into staging
qapi: Fix qapi mangling of downstream names, and more
# gpg: Signature made Fri May 15 17:41:31 2015 BST using RSA key ID EB918653
# gpg: Good signature from "Markus Armbruster <armbru@redhat.com>"
# gpg: aka "Markus Armbruster <armbru@pond.sub.org>"
* remotes/armbru/tags/pull-qapi-2015-05-15: (26 commits)
qapi: Inline gen_command_decl_prologue(), gen_command_def_prologue()
qapi: Drop pointless flush() before close()
qapi: Factor open_output(), close_output() out of generators
qapi: Turn generators' mandatory option -i into an argument
qapi: Fix generators to report command line errors decently
qapi: Factor parse_command_line() out of the generators
qapi: qapi-commands.py option --type is unused, drop it
qapi: qapi-event.py option -b does nothing, drop it
tests: Add missing dependencies on $(qapi-py)
qapi: Support downstream events and commands
qapi: Support downstream alternates
qapi: Support downstream flat unions
qapi: Support downstream simple unions
qapi: Support downstream structs
qapi: Support downstream enums
qapi: Make c_type() consistently convert qapi names
qapi: Tidy c_type() logic
qapi: Move camel_to_upper(), c_enum_const() to closely related code
qapi: Use c_enum_const() in generate_alternate_qtypes()
qapi: Simplify c_enum_const()
...
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'scripts/qapi-types.py')
-rw-r--r-- | scripts/qapi-types.py | 147 |
1 files changed, 44 insertions, 103 deletions
diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py index 2bf8145076..6bd0b13759 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) |