qapi-event.py 5.2 KB

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