events.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 typing import List
  12. from .common import c_enum_const, c_name, mcgen
  13. from .gen import QAPISchemaModularCVisitor, build_params, ifcontext
  14. from .schema import (
  15. QAPISchema,
  16. QAPISchemaEnumMember,
  17. QAPISchemaFeature,
  18. QAPISchemaObjectType,
  19. )
  20. from .source import QAPISourceInfo
  21. from .types import gen_enum, gen_enum_lookup
  22. def build_event_send_proto(name: str,
  23. arg_type: QAPISchemaObjectType,
  24. boxed: bool) -> str:
  25. return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
  26. 'c_name': c_name(name.lower()),
  27. 'param': build_params(arg_type, boxed)}
  28. def gen_event_send_decl(name: str,
  29. arg_type: QAPISchemaObjectType,
  30. boxed: bool) -> str:
  31. return mcgen('''
  32. %(proto)s;
  33. ''',
  34. proto=build_event_send_proto(name, arg_type, boxed))
  35. def gen_param_var(typ: QAPISchemaObjectType) -> str:
  36. """
  37. Generate a struct variable holding the event parameters.
  38. Initialize it with the function arguments defined in `gen_event_send`.
  39. """
  40. assert not typ.variants
  41. ret = mcgen('''
  42. %(c_name)s param = {
  43. ''',
  44. c_name=typ.c_name())
  45. sep = ' '
  46. for memb in typ.members:
  47. ret += sep
  48. sep = ', '
  49. if memb.optional:
  50. ret += 'has_' + c_name(memb.name) + sep
  51. if memb.type.name == 'str':
  52. # Cast away const added in build_params()
  53. ret += '(char *)'
  54. ret += c_name(memb.name)
  55. ret += mcgen('''
  56. };
  57. ''')
  58. if not typ.is_implicit():
  59. ret += mcgen('''
  60. %(c_name)s *arg = &param;
  61. ''',
  62. c_name=typ.c_name())
  63. return ret
  64. def gen_event_send(name: str,
  65. arg_type: QAPISchemaObjectType,
  66. boxed: bool,
  67. event_enum_name: str,
  68. event_emit: str) -> str:
  69. # FIXME: Our declaration of local variables (and of 'errp' in the
  70. # parameter list) can collide with exploded members of the event's
  71. # data type passed in as parameters. If this collision ever hits in
  72. # practice, we can rename our local variables with a leading _ prefix,
  73. # or split the code into a wrapper function that creates a boxed
  74. # 'param' object then calls another to do the real work.
  75. have_args = boxed or (arg_type and not arg_type.is_empty())
  76. ret = mcgen('''
  77. %(proto)s
  78. {
  79. QDict *qmp;
  80. ''',
  81. proto=build_event_send_proto(name, arg_type, boxed))
  82. if have_args:
  83. ret += mcgen('''
  84. QObject *obj;
  85. Visitor *v;
  86. ''')
  87. if not boxed:
  88. ret += gen_param_var(arg_type)
  89. ret += mcgen('''
  90. qmp = qmp_event_build_dict("%(name)s");
  91. ''',
  92. name=name)
  93. if have_args:
  94. ret += mcgen('''
  95. v = qobject_output_visitor_new(&obj);
  96. ''')
  97. if not arg_type.is_implicit():
  98. ret += mcgen('''
  99. visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
  100. ''',
  101. name=name, c_name=arg_type.c_name())
  102. else:
  103. ret += mcgen('''
  104. visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
  105. visit_type_%(c_name)s_members(v, &param, &error_abort);
  106. visit_check_struct(v, &error_abort);
  107. visit_end_struct(v, NULL);
  108. ''',
  109. name=name, c_name=arg_type.c_name())
  110. ret += mcgen('''
  111. visit_complete(v, &obj);
  112. qdict_put_obj(qmp, "data", obj);
  113. ''')
  114. ret += mcgen('''
  115. %(event_emit)s(%(c_enum)s, qmp);
  116. ''',
  117. event_emit=event_emit,
  118. c_enum=c_enum_const(event_enum_name, name))
  119. if have_args:
  120. ret += mcgen('''
  121. visit_free(v);
  122. ''')
  123. ret += mcgen('''
  124. qobject_unref(qmp);
  125. }
  126. ''')
  127. return ret
  128. class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor):
  129. def __init__(self, prefix: str):
  130. super().__init__(
  131. prefix, 'qapi-events',
  132. ' * Schema-defined QAPI/QMP events', None, __doc__)
  133. self._event_enum_name = c_name(prefix + 'QAPIEvent', protect=False)
  134. self._event_enum_members: List[QAPISchemaEnumMember] = []
  135. self._event_emit_name = c_name(prefix + 'qapi_event_emit')
  136. def _begin_user_module(self, name: str) -> None:
  137. events = self._module_basename('qapi-events', name)
  138. types = self._module_basename('qapi-types', name)
  139. visit = self._module_basename('qapi-visit', name)
  140. self._genc.add(mcgen('''
  141. #include "qemu/osdep.h"
  142. #include "%(prefix)sqapi-emit-events.h"
  143. #include "%(events)s.h"
  144. #include "%(visit)s.h"
  145. #include "qapi/error.h"
  146. #include "qapi/qmp/qdict.h"
  147. #include "qapi/qobject-output-visitor.h"
  148. #include "qapi/qmp-event.h"
  149. ''',
  150. events=events, visit=visit,
  151. prefix=self._prefix))
  152. self._genh.add(mcgen('''
  153. #include "qapi/util.h"
  154. #include "%(types)s.h"
  155. ''',
  156. types=types))
  157. def visit_end(self) -> None:
  158. self._add_system_module('emit', ' * QAPI Events emission')
  159. self._genc.preamble_add(mcgen('''
  160. #include "qemu/osdep.h"
  161. #include "%(prefix)sqapi-emit-events.h"
  162. ''',
  163. prefix=self._prefix))
  164. self._genh.preamble_add(mcgen('''
  165. #include "qapi/util.h"
  166. '''))
  167. self._genh.add(gen_enum(self._event_enum_name,
  168. self._event_enum_members))
  169. self._genc.add(gen_enum_lookup(self._event_enum_name,
  170. self._event_enum_members))
  171. self._genh.add(mcgen('''
  172. void %(event_emit)s(%(event_enum)s event, QDict *qdict);
  173. ''',
  174. event_emit=self._event_emit_name,
  175. event_enum=self._event_enum_name))
  176. def visit_event(self,
  177. name: str,
  178. info: QAPISourceInfo,
  179. ifcond: List[str],
  180. features: List[QAPISchemaFeature],
  181. arg_type: QAPISchemaObjectType,
  182. boxed: bool) -> None:
  183. with ifcontext(ifcond, self._genh, self._genc):
  184. self._genh.add(gen_event_send_decl(name, arg_type, boxed))
  185. self._genc.add(gen_event_send(name, arg_type, boxed,
  186. self._event_enum_name,
  187. self._event_emit_name))
  188. # Note: we generate the enum member regardless of @ifcond, to
  189. # keep the enumeration usable in target-independent code.
  190. self._event_enum_members.append(QAPISchemaEnumMember(name, None))
  191. def gen_events(schema: QAPISchema,
  192. output_dir: str,
  193. prefix: str) -> None:
  194. vis = QAPISchemaGenEventVisitor(prefix)
  195. schema.visit(vis)
  196. vis.write(output_dir)