qapi-event.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if not boxed:
  70. ret += gen_param_var(arg_type)
  71. else:
  72. assert not boxed
  73. ret += mcgen('''
  74. emit = qmp_event_get_func_emit();
  75. if (!emit) {
  76. return;
  77. }
  78. qmp = qmp_event_build_dict("%(name)s");
  79. ''',
  80. name=name)
  81. if arg_type and not arg_type.is_empty():
  82. ret += mcgen('''
  83. v = qobject_output_visitor_new(&obj);
  84. ''')
  85. if not arg_type.is_implicit():
  86. ret += mcgen('''
  87. visit_type_%(c_name)s(v, "%(name)s", &arg, &err);
  88. ''',
  89. name=name, c_name=arg_type.c_name())
  90. else:
  91. ret += mcgen('''
  92. visit_start_struct(v, "%(name)s", NULL, 0, &err);
  93. if (err) {
  94. goto out;
  95. }
  96. visit_type_%(c_name)s_members(v, &param, &err);
  97. if (!err) {
  98. visit_check_struct(v, &err);
  99. }
  100. visit_end_struct(v, NULL);
  101. ''',
  102. name=name, c_name=arg_type.c_name())
  103. ret += mcgen('''
  104. if (err) {
  105. goto out;
  106. }
  107. visit_complete(v, &obj);
  108. qdict_put_obj(qmp, "data", obj);
  109. ''')
  110. ret += mcgen('''
  111. emit(%(c_enum)s, qmp, &err);
  112. ''',
  113. c_enum=c_enum_const(event_enum_name, name))
  114. if arg_type and not arg_type.is_empty():
  115. ret += mcgen('''
  116. out:
  117. visit_free(v);
  118. ''')
  119. ret += mcgen('''
  120. error_propagate(errp, err);
  121. QDECREF(qmp);
  122. }
  123. ''')
  124. return ret
  125. class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
  126. def __init__(self):
  127. self.decl = None
  128. self.defn = None
  129. self._event_names = None
  130. def visit_begin(self, schema):
  131. self.decl = ''
  132. self.defn = ''
  133. self._event_names = []
  134. def visit_end(self):
  135. self.decl += gen_enum(event_enum_name, self._event_names)
  136. self.defn += gen_enum_lookup(event_enum_name, self._event_names)
  137. self._event_names = None
  138. def visit_event(self, name, info, arg_type, boxed):
  139. self.decl += gen_event_send_decl(name, arg_type, boxed)
  140. self.defn += gen_event_send(name, arg_type, boxed)
  141. self._event_names.append(name)
  142. (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
  143. c_comment = '''
  144. /*
  145. * schema-defined QAPI event functions
  146. *
  147. * Copyright (c) 2014 Wenchao Xia
  148. *
  149. * Authors:
  150. * Wenchao Xia <wenchaoqemu@gmail.com>
  151. *
  152. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  153. * See the COPYING.LIB file in the top-level directory.
  154. *
  155. */
  156. '''
  157. h_comment = '''
  158. /*
  159. * schema-defined QAPI event functions
  160. *
  161. * Copyright (c) 2014 Wenchao Xia
  162. *
  163. * Authors:
  164. * Wenchao Xia <wenchaoqemu@gmail.com>
  165. *
  166. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  167. * See the COPYING.LIB file in the top-level directory.
  168. *
  169. */
  170. '''
  171. (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
  172. 'qapi-event.c', 'qapi-event.h',
  173. c_comment, h_comment)
  174. fdef.write(mcgen('''
  175. #include "qemu/osdep.h"
  176. #include "qemu-common.h"
  177. #include "%(prefix)sqapi-event.h"
  178. #include "%(prefix)sqapi-visit.h"
  179. #include "qapi/qobject-output-visitor.h"
  180. #include "qapi/qmp-event.h"
  181. ''',
  182. prefix=prefix))
  183. fdecl.write(mcgen('''
  184. #include "qapi/error.h"
  185. #include "qapi/qmp/qdict.h"
  186. #include "%(prefix)sqapi-types.h"
  187. ''',
  188. prefix=prefix))
  189. event_enum_name = c_name(prefix + "QAPIEvent", protect=False)
  190. schema = QAPISchema(input_file)
  191. gen = QAPISchemaGenEventVisitor()
  192. schema.visit(gen)
  193. fdef.write(gen.defn)
  194. fdecl.write(gen.decl)
  195. close_output(fdef, fdecl)