events.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.variants
  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.optional:
  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. if 'deprecated' in [f.name for f in features]:
  93. ret += mcgen('''
  94. if (compat_policy.deprecated_output == COMPAT_POLICY_OUTPUT_HIDE) {
  95. return;
  96. }
  97. ''')
  98. ret += mcgen('''
  99. qmp = qmp_event_build_dict("%(name)s");
  100. ''',
  101. name=name)
  102. if have_args:
  103. assert arg_type is not None
  104. ret += mcgen('''
  105. v = qobject_output_visitor_new_qmp(&obj);
  106. ''')
  107. if not arg_type.is_implicit():
  108. ret += mcgen('''
  109. visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort);
  110. ''',
  111. name=name, c_name=arg_type.c_name())
  112. else:
  113. ret += mcgen('''
  114. visit_start_struct(v, "%(name)s", NULL, 0, &error_abort);
  115. visit_type_%(c_name)s_members(v, &param, &error_abort);
  116. visit_check_struct(v, &error_abort);
  117. visit_end_struct(v, NULL);
  118. ''',
  119. name=name, c_name=arg_type.c_name())
  120. ret += mcgen('''
  121. visit_complete(v, &obj);
  122. if (qdict_size(qobject_to(QDict, obj))) {
  123. qdict_put_obj(qmp, "data", obj);
  124. } else {
  125. qobject_unref(obj);
  126. }
  127. ''')
  128. ret += mcgen('''
  129. %(event_emit)s(%(c_enum)s, qmp);
  130. ''',
  131. event_emit=event_emit,
  132. c_enum=c_enum_const(event_enum_name, name))
  133. if have_args:
  134. ret += mcgen('''
  135. visit_free(v);
  136. ''')
  137. ret += mcgen('''
  138. qobject_unref(qmp);
  139. }
  140. ''')
  141. return ret
  142. class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor):
  143. def __init__(self, prefix: str):
  144. super().__init__(
  145. prefix, 'qapi-events',
  146. ' * Schema-defined QAPI/QMP events', None, __doc__)
  147. self._event_enum_name = c_name(prefix + 'QAPIEvent', protect=False)
  148. self._event_enum_members: List[QAPISchemaEnumMember] = []
  149. self._event_emit_name = c_name(prefix + 'qapi_event_emit')
  150. def _begin_user_module(self, name: str) -> None:
  151. events = self._module_basename('qapi-events', name)
  152. types = self._module_basename('qapi-types', name)
  153. visit = self._module_basename('qapi-visit', name)
  154. self._genc.add(mcgen('''
  155. #include "qemu/osdep.h"
  156. #include "%(prefix)sqapi-emit-events.h"
  157. #include "%(events)s.h"
  158. #include "%(visit)s.h"
  159. #include "qapi/compat-policy.h"
  160. #include "qapi/error.h"
  161. #include "qapi/qmp/qdict.h"
  162. #include "qapi/qmp-event.h"
  163. ''',
  164. events=events, visit=visit,
  165. prefix=self._prefix))
  166. self._genh.add(mcgen('''
  167. #include "qapi/util.h"
  168. #include "%(types)s.h"
  169. ''',
  170. types=types))
  171. def visit_end(self) -> None:
  172. self._add_module('./emit', ' * QAPI Events emission')
  173. self._genc.preamble_add(mcgen('''
  174. #include "qemu/osdep.h"
  175. #include "%(prefix)sqapi-emit-events.h"
  176. ''',
  177. prefix=self._prefix))
  178. self._genh.preamble_add(mcgen('''
  179. #include "qapi/util.h"
  180. '''))
  181. self._genh.add(gen_enum(self._event_enum_name,
  182. self._event_enum_members))
  183. self._genc.add(gen_enum_lookup(self._event_enum_name,
  184. self._event_enum_members))
  185. self._genh.add(mcgen('''
  186. void %(event_emit)s(%(event_enum)s event, QDict *qdict);
  187. ''',
  188. event_emit=self._event_emit_name,
  189. event_enum=self._event_enum_name))
  190. def visit_event(self,
  191. name: str,
  192. info: Optional[QAPISourceInfo],
  193. ifcond: QAPISchemaIfCond,
  194. features: List[QAPISchemaFeature],
  195. arg_type: Optional[QAPISchemaObjectType],
  196. boxed: bool) -> None:
  197. with ifcontext(ifcond, self._genh, self._genc):
  198. self._genh.add(gen_event_send_decl(name, arg_type, boxed))
  199. self._genc.add(gen_event_send(name, arg_type, features, boxed,
  200. self._event_enum_name,
  201. self._event_emit_name))
  202. # Note: we generate the enum member regardless of @ifcond, to
  203. # keep the enumeration usable in target-independent code.
  204. self._event_enum_members.append(QAPISchemaEnumMember(name, None))
  205. def gen_events(schema: QAPISchema,
  206. output_dir: str,
  207. prefix: str) -> None:
  208. vis = QAPISchemaGenEventVisitor(prefix)
  209. schema.visit(vis)
  210. vis.write(output_dir)