qapi-event.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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, boxed):
  15. return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
  16. 'c_name': c_name(name.lower()),
  17. 'param': gen_params(arg_type, boxed, 'Error **errp')}
  18. def gen_event_send_decl(name, arg_type, boxed):
  19. return mcgen('''
  20. %(proto)s;
  21. ''',
  22. proto=gen_event_send_proto(name, arg_type, boxed))
  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. if not typ.is_implicit():
  44. ret += mcgen('''
  45. %(c_name)s *arg = &param;
  46. ''',
  47. c_name=typ.c_name())
  48. return ret
  49. def gen_event_send(name, arg_type, boxed):
  50. # FIXME: Our declaration of local variables (and of 'errp' in the
  51. # parameter list) can collide with exploded members of the event's
  52. # data type passed in as parameters. If this collision ever hits in
  53. # practice, we can rename our local variables with a leading _ prefix,
  54. # or split the code into a wrapper function that creates a boxed
  55. # 'param' object then calls another to do the real work.
  56. ret = mcgen('''
  57. %(proto)s
  58. {
  59. QDict *qmp;
  60. Error *err = NULL;
  61. QMPEventFuncEmit emit;
  62. ''',
  63. proto=gen_event_send_proto(name, arg_type, boxed))
  64. if arg_type and not arg_type.is_empty():
  65. ret += mcgen('''
  66. QObject *obj;
  67. Visitor *v;
  68. ''')
  69. ret += gen_param_var(arg_type)
  70. ret += mcgen('''
  71. emit = qmp_event_get_func_emit();
  72. if (!emit) {
  73. return;
  74. }
  75. qmp = qmp_event_build_dict("%(name)s");
  76. ''',
  77. name=name)
  78. if arg_type and not arg_type.is_empty():
  79. ret += mcgen('''
  80. v = qmp_output_visitor_new(&obj);
  81. ''')
  82. if not arg_type.is_implicit():
  83. ret += mcgen('''
  84. visit_type_%(c_name)s(v, "%(name)s", &arg, &err);
  85. ''',
  86. name=name, c_name=arg_type.c_name())
  87. else:
  88. ret += mcgen('''
  89. visit_start_struct(v, "%(name)s", NULL, 0, &err);
  90. if (err) {
  91. goto out;
  92. }
  93. visit_type_%(c_name)s_members(v, &param, &err);
  94. if (!err) {
  95. visit_check_struct(v, &err);
  96. }
  97. visit_end_struct(v, NULL);
  98. ''',
  99. name=name, c_name=arg_type.c_name())
  100. ret += mcgen('''
  101. if (err) {
  102. goto out;
  103. }
  104. visit_complete(v, &obj);
  105. qdict_put_obj(qmp, "data", obj);
  106. ''')
  107. ret += mcgen('''
  108. emit(%(c_enum)s, qmp, &err);
  109. ''',
  110. c_enum=c_enum_const(event_enum_name, name))
  111. if arg_type and not arg_type.is_empty():
  112. ret += mcgen('''
  113. out:
  114. visit_free(v);
  115. ''')
  116. ret += mcgen('''
  117. error_propagate(errp, err);
  118. QDECREF(qmp);
  119. }
  120. ''')
  121. return ret
  122. class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
  123. def __init__(self):
  124. self.decl = None
  125. self.defn = None
  126. self._event_names = None
  127. def visit_begin(self, schema):
  128. self.decl = ''
  129. self.defn = ''
  130. self._event_names = []
  131. def visit_end(self):
  132. self.decl += gen_enum(event_enum_name, self._event_names)
  133. self.defn += gen_enum_lookup(event_enum_name, self._event_names)
  134. self._event_names = None
  135. def visit_event(self, name, info, arg_type, boxed):
  136. self.decl += gen_event_send_decl(name, arg_type, boxed)
  137. self.defn += gen_event_send(name, arg_type, boxed)
  138. self._event_names.append(name)
  139. (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
  140. c_comment = '''
  141. /*
  142. * schema-defined QAPI event functions
  143. *
  144. * Copyright (c) 2014 Wenchao Xia
  145. *
  146. * Authors:
  147. * Wenchao Xia <wenchaoqemu@gmail.com>
  148. *
  149. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  150. * See the COPYING.LIB file in the top-level directory.
  151. *
  152. */
  153. '''
  154. h_comment = '''
  155. /*
  156. * schema-defined QAPI event functions
  157. *
  158. * Copyright (c) 2014 Wenchao Xia
  159. *
  160. * Authors:
  161. * Wenchao Xia <wenchaoqemu@gmail.com>
  162. *
  163. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  164. * See the COPYING.LIB file in the top-level directory.
  165. *
  166. */
  167. '''
  168. (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
  169. 'qapi-event.c', 'qapi-event.h',
  170. c_comment, h_comment)
  171. fdef.write(mcgen('''
  172. #include "qemu/osdep.h"
  173. #include "qemu-common.h"
  174. #include "%(prefix)sqapi-event.h"
  175. #include "%(prefix)sqapi-visit.h"
  176. #include "qapi/qmp-output-visitor.h"
  177. #include "qapi/qmp-event.h"
  178. ''',
  179. prefix=prefix))
  180. fdecl.write(mcgen('''
  181. #include "qapi/error.h"
  182. #include "qapi/qmp/qdict.h"
  183. #include "%(prefix)sqapi-types.h"
  184. ''',
  185. prefix=prefix))
  186. event_enum_name = c_name(prefix + "QAPIEvent", protect=False)
  187. schema = QAPISchema(input_file)
  188. gen = QAPISchemaGenEventVisitor()
  189. schema.visit(gen)
  190. fdef.write(gen.defn)
  191. fdecl.write(gen.decl)
  192. close_output(fdef, fdecl)