events.py 7.4 KB

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