2
0

qapi-event.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #
  2. # QAPI event generator
  3. #
  4. # Copyright (c) 2014 Wenchao Xia
  5. # Copyright (c) 2015 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. def gen_event_send(name, arg_type):
  24. ret = mcgen('''
  25. %(proto)s
  26. {
  27. QDict *qmp;
  28. Error *err = NULL;
  29. QMPEventFuncEmit emit;
  30. ''',
  31. proto=gen_event_send_proto(name, arg_type))
  32. if arg_type and arg_type.members:
  33. ret += mcgen('''
  34. QmpOutputVisitor *qov;
  35. Visitor *v;
  36. QObject *obj;
  37. ''')
  38. ret += mcgen('''
  39. emit = qmp_event_get_func_emit();
  40. if (!emit) {
  41. return;
  42. }
  43. qmp = qmp_event_build_dict("%(name)s");
  44. ''',
  45. name=name)
  46. if arg_type and arg_type.members:
  47. ret += mcgen('''
  48. qov = qmp_output_visitor_new();
  49. g_assert(qov);
  50. v = qmp_output_get_visitor(qov);
  51. g_assert(v);
  52. /* Fake visit, as if all members are under a structure */
  53. visit_start_struct(v, NULL, "", "%(name)s", 0, &err);
  54. ''',
  55. name=name)
  56. ret += gen_err_check()
  57. ret += gen_visit_fields(arg_type.members, need_cast=True)
  58. ret += mcgen('''
  59. visit_end_struct(v, &err);
  60. if (err) {
  61. goto out;
  62. }
  63. obj = qmp_output_get_qobject(qov);
  64. g_assert(obj != NULL);
  65. qdict_put_obj(qmp, "data", obj);
  66. ''')
  67. ret += mcgen('''
  68. emit(%(c_enum)s, qmp, &err);
  69. ''',
  70. c_enum=c_enum_const(event_enum_name, name))
  71. if arg_type and arg_type.members:
  72. ret += mcgen('''
  73. out:
  74. qmp_output_visitor_cleanup(qov);
  75. ''')
  76. ret += mcgen('''
  77. error_propagate(errp, err);
  78. QDECREF(qmp);
  79. }
  80. ''')
  81. return ret
  82. class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
  83. def __init__(self):
  84. self.decl = None
  85. self.defn = None
  86. self._event_names = None
  87. def visit_begin(self, schema):
  88. self.decl = ''
  89. self.defn = ''
  90. self._event_names = []
  91. def visit_end(self):
  92. self.decl += gen_enum(event_enum_name, self._event_names)
  93. self.defn += gen_enum_lookup(event_enum_name, self._event_names)
  94. self._event_names = None
  95. def visit_event(self, name, info, arg_type):
  96. self.decl += gen_event_send_decl(name, arg_type)
  97. self.defn += gen_event_send(name, arg_type)
  98. self._event_names.append(name)
  99. (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
  100. c_comment = '''
  101. /*
  102. * schema-defined QAPI event functions
  103. *
  104. * Copyright (c) 2014 Wenchao Xia
  105. *
  106. * Authors:
  107. * Wenchao Xia <wenchaoqemu@gmail.com>
  108. *
  109. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  110. * See the COPYING.LIB file in the top-level directory.
  111. *
  112. */
  113. '''
  114. h_comment = '''
  115. /*
  116. * schema-defined QAPI event functions
  117. *
  118. * Copyright (c) 2014 Wenchao Xia
  119. *
  120. * Authors:
  121. * Wenchao Xia <wenchaoqemu@gmail.com>
  122. *
  123. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  124. * See the COPYING.LIB file in the top-level directory.
  125. *
  126. */
  127. '''
  128. (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
  129. 'qapi-event.c', 'qapi-event.h',
  130. c_comment, h_comment)
  131. fdef.write(mcgen('''
  132. #include "qemu-common.h"
  133. #include "%(prefix)sqapi-event.h"
  134. #include "%(prefix)sqapi-visit.h"
  135. #include "qapi/qmp-output-visitor.h"
  136. #include "qapi/qmp-event.h"
  137. ''',
  138. prefix=prefix))
  139. fdecl.write(mcgen('''
  140. #include "qapi/error.h"
  141. #include "qapi/qmp/qdict.h"
  142. #include "%(prefix)sqapi-types.h"
  143. ''',
  144. prefix=prefix))
  145. event_enum_name = c_name(prefix + "QAPIEvent", protect=False)
  146. schema = QAPISchema(input_file)
  147. gen = QAPISchemaGenEventVisitor()
  148. schema.visit(gen)
  149. fdef.write(gen.defn)
  150. fdecl.write(gen.decl)
  151. close_output(fdef, fdecl)