qapi-event.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. visit_end_struct(v, err ? NULL : &err);
  83. if (err) {
  84. goto out;
  85. }
  86. qdict_put_obj(qmp, "data", qmp_output_get_qobject(qov));
  87. ''',
  88. name=name, c_name=arg_type.c_name())
  89. ret += mcgen('''
  90. emit(%(c_enum)s, qmp, &err);
  91. ''',
  92. c_enum=c_enum_const(event_enum_name, name))
  93. if arg_type and arg_type.members:
  94. ret += mcgen('''
  95. out:
  96. qmp_output_visitor_cleanup(qov);
  97. ''')
  98. ret += mcgen('''
  99. error_propagate(errp, err);
  100. QDECREF(qmp);
  101. }
  102. ''')
  103. return ret
  104. class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
  105. def __init__(self):
  106. self.decl = None
  107. self.defn = None
  108. self._event_names = None
  109. def visit_begin(self, schema):
  110. self.decl = ''
  111. self.defn = ''
  112. self._event_names = []
  113. def visit_end(self):
  114. self.decl += gen_enum(event_enum_name, self._event_names)
  115. self.defn += gen_enum_lookup(event_enum_name, self._event_names)
  116. self._event_names = None
  117. def visit_event(self, name, info, arg_type):
  118. self.decl += gen_event_send_decl(name, arg_type)
  119. self.defn += gen_event_send(name, arg_type)
  120. self._event_names.append(name)
  121. (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
  122. c_comment = '''
  123. /*
  124. * schema-defined QAPI event functions
  125. *
  126. * Copyright (c) 2014 Wenchao Xia
  127. *
  128. * Authors:
  129. * Wenchao Xia <wenchaoqemu@gmail.com>
  130. *
  131. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  132. * See the COPYING.LIB file in the top-level directory.
  133. *
  134. */
  135. '''
  136. h_comment = '''
  137. /*
  138. * schema-defined QAPI event functions
  139. *
  140. * Copyright (c) 2014 Wenchao Xia
  141. *
  142. * Authors:
  143. * Wenchao Xia <wenchaoqemu@gmail.com>
  144. *
  145. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  146. * See the COPYING.LIB file in the top-level directory.
  147. *
  148. */
  149. '''
  150. (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
  151. 'qapi-event.c', 'qapi-event.h',
  152. c_comment, h_comment)
  153. fdef.write(mcgen('''
  154. #include "qemu/osdep.h"
  155. #include "qemu-common.h"
  156. #include "%(prefix)sqapi-event.h"
  157. #include "%(prefix)sqapi-visit.h"
  158. #include "qapi/qmp-output-visitor.h"
  159. #include "qapi/qmp-event.h"
  160. ''',
  161. prefix=prefix))
  162. fdecl.write(mcgen('''
  163. #include "qapi/error.h"
  164. #include "qapi/qmp/qdict.h"
  165. #include "%(prefix)sqapi-types.h"
  166. ''',
  167. prefix=prefix))
  168. event_enum_name = c_name(prefix + "QAPIEvent", protect=False)
  169. schema = QAPISchema(input_file)
  170. gen = QAPISchemaGenEventVisitor()
  171. schema.visit(gen)
  172. fdef.write(gen.defn)
  173. fdecl.write(gen.decl)
  174. close_output(fdef, fdecl)