qapi-event.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #
  2. # QAPI event generator
  3. #
  4. # Copyright (c) 2014 Wenchao Xia
  5. # Copyright (c) 2015-2016 Red Hat Inc.
  6. #
  7. # Authors:
  8. # Wenchao Xia <wenchaoqemu@gmail.com>
  9. # Markus Armbruster <armbru@redhat.com>
  10. #
  11. # This work is licensed under the terms of the GNU GPL, version 2.
  12. # See the COPYING file in the top-level directory.
  13. from qapi import *
  14. def gen_event_send_proto(name, arg_type):
  15. return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
  16. 'c_name': c_name(name.lower()),
  17. 'param': gen_params(arg_type, 'Error **errp')}
  18. def gen_event_send_decl(name, arg_type):
  19. return mcgen('''
  20. %(proto)s;
  21. ''',
  22. proto=gen_event_send_proto(name, arg_type))
  23. # Declare and initialize an object 'qapi' using parameters from gen_params()
  24. def gen_param_var(typ):
  25. assert not typ.variants
  26. ret = mcgen('''
  27. %(c_name)s param = {
  28. ''',
  29. c_name=typ.c_name())
  30. sep = ' '
  31. for memb in typ.members:
  32. ret += sep
  33. sep = ', '
  34. if memb.optional:
  35. ret += 'has_' + c_name(memb.name) + sep
  36. if memb.type.name == 'str':
  37. # Cast away const added in gen_params()
  38. ret += '(char *)'
  39. ret += c_name(memb.name)
  40. ret += mcgen('''
  41. };
  42. ''')
  43. return ret
  44. def gen_event_send(name, arg_type):
  45. # FIXME: Our declaration of local variables (and of 'errp' in the
  46. # parameter list) can collide with exploded members of the event's
  47. # data type passed in as parameters. If this collision ever hits in
  48. # practice, we can rename our local variables with a leading _ prefix,
  49. # or split the code into a wrapper function that creates a boxed
  50. # 'param' object then calls another to do the real work.
  51. ret = mcgen('''
  52. %(proto)s
  53. {
  54. QDict *qmp;
  55. Error *err = NULL;
  56. QMPEventFuncEmit emit;
  57. ''',
  58. proto=gen_event_send_proto(name, arg_type))
  59. if arg_type and arg_type.members:
  60. ret += mcgen('''
  61. QmpOutputVisitor *qov;
  62. Visitor *v;
  63. ''')
  64. ret += gen_param_var(arg_type)
  65. ret += mcgen('''
  66. emit = qmp_event_get_func_emit();
  67. if (!emit) {
  68. return;
  69. }
  70. qmp = qmp_event_build_dict("%(name)s");
  71. ''',
  72. name=name)
  73. if arg_type and arg_type.members:
  74. ret += mcgen('''
  75. qov = qmp_output_visitor_new();
  76. v = qmp_output_get_visitor(qov);
  77. visit_start_struct(v, "%(name)s", NULL, 0, &err);
  78. if (err) {
  79. goto out;
  80. }
  81. visit_type_%(c_name)s_members(v, &param, &err);
  82. if (!err) {
  83. visit_check_struct(v, &err);
  84. }
  85. visit_end_struct(v, NULL);
  86. if (err) {
  87. goto out;
  88. }
  89. qdict_put_obj(qmp, "data", qmp_output_get_qobject(qov));
  90. ''',
  91. name=name, c_name=arg_type.c_name())
  92. ret += mcgen('''
  93. emit(%(c_enum)s, qmp, &err);
  94. ''',
  95. c_enum=c_enum_const(event_enum_name, name))
  96. if arg_type and arg_type.members:
  97. ret += mcgen('''
  98. out:
  99. visit_free(v);
  100. ''')
  101. ret += mcgen('''
  102. error_propagate(errp, err);
  103. QDECREF(qmp);
  104. }
  105. ''')
  106. return ret
  107. class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
  108. def __init__(self):
  109. self.decl = None
  110. self.defn = None
  111. self._event_names = None
  112. def visit_begin(self, schema):
  113. self.decl = ''
  114. self.defn = ''
  115. self._event_names = []
  116. def visit_end(self):
  117. self.decl += gen_enum(event_enum_name, self._event_names)
  118. self.defn += gen_enum_lookup(event_enum_name, self._event_names)
  119. self._event_names = None
  120. def visit_event(self, name, info, arg_type):
  121. self.decl += gen_event_send_decl(name, arg_type)
  122. self.defn += gen_event_send(name, arg_type)
  123. self._event_names.append(name)
  124. (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
  125. c_comment = '''
  126. /*
  127. * schema-defined QAPI event functions
  128. *
  129. * Copyright (c) 2014 Wenchao Xia
  130. *
  131. * Authors:
  132. * Wenchao Xia <wenchaoqemu@gmail.com>
  133. *
  134. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  135. * See the COPYING.LIB file in the top-level directory.
  136. *
  137. */
  138. '''
  139. h_comment = '''
  140. /*
  141. * schema-defined QAPI event functions
  142. *
  143. * Copyright (c) 2014 Wenchao Xia
  144. *
  145. * Authors:
  146. * Wenchao Xia <wenchaoqemu@gmail.com>
  147. *
  148. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  149. * See the COPYING.LIB file in the top-level directory.
  150. *
  151. */
  152. '''
  153. (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
  154. 'qapi-event.c', 'qapi-event.h',
  155. c_comment, h_comment)
  156. fdef.write(mcgen('''
  157. #include "qemu/osdep.h"
  158. #include "qemu-common.h"
  159. #include "%(prefix)sqapi-event.h"
  160. #include "%(prefix)sqapi-visit.h"
  161. #include "qapi/qmp-output-visitor.h"
  162. #include "qapi/qmp-event.h"
  163. ''',
  164. prefix=prefix))
  165. fdecl.write(mcgen('''
  166. #include "qapi/error.h"
  167. #include "qapi/qmp/qdict.h"
  168. #include "%(prefix)sqapi-types.h"
  169. ''',
  170. prefix=prefix))
  171. event_enum_name = c_name(prefix + "QAPIEvent", protect=False)
  172. schema = QAPISchema(input_file)
  173. gen = QAPISchemaGenEventVisitor()
  174. schema.visit(gen)
  175. fdef.write(gen.defn)
  176. fdecl.write(gen.decl)
  177. close_output(fdef, fdecl)