qapi-commands.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #
  2. # QAPI command marshaller generator
  3. #
  4. # Copyright IBM, Corp. 2011
  5. # Copyright (C) 2014-2016 Red Hat, Inc.
  6. #
  7. # Authors:
  8. # Anthony Liguori <aliguori@us.ibm.com>
  9. # Michael Roth <mdroth@linux.vnet.ibm.com>
  10. # Markus Armbruster <armbru@redhat.com>
  11. #
  12. # This work is licensed under the terms of the GNU GPL, version 2.
  13. # See the COPYING file in the top-level directory.
  14. from qapi import *
  15. import re
  16. def gen_command_decl(name, arg_type, ret_type):
  17. return mcgen('''
  18. %(c_type)s qmp_%(c_name)s(%(params)s);
  19. ''',
  20. c_type=(ret_type and ret_type.c_type()) or 'void',
  21. c_name=c_name(name),
  22. params=gen_params(arg_type, 'Error **errp'))
  23. def gen_call(name, arg_type, ret_type):
  24. ret = ''
  25. argstr = ''
  26. if arg_type:
  27. assert not arg_type.variants
  28. for memb in arg_type.members:
  29. if memb.optional:
  30. argstr += 'arg.has_%s, ' % c_name(memb.name)
  31. argstr += 'arg.%s, ' % c_name(memb.name)
  32. lhs = ''
  33. if ret_type:
  34. lhs = 'retval = '
  35. ret = mcgen('''
  36. %(lhs)sqmp_%(c_name)s(%(args)s&err);
  37. ''',
  38. c_name=c_name(name), args=argstr, lhs=lhs)
  39. if ret_type:
  40. ret += gen_err_check()
  41. ret += mcgen('''
  42. qmp_marshal_output_%(c_name)s(retval, ret, &err);
  43. ''',
  44. c_name=ret_type.c_name())
  45. return ret
  46. def gen_marshal_output(ret_type):
  47. return mcgen('''
  48. static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
  49. {
  50. Error *err = NULL;
  51. QmpOutputVisitor *qov = qmp_output_visitor_new();
  52. Visitor *v;
  53. v = qmp_output_get_visitor(qov);
  54. visit_type_%(c_name)s(v, "unused", &ret_in, &err);
  55. if (err) {
  56. goto out;
  57. }
  58. *ret_out = qmp_output_get_qobject(qov);
  59. out:
  60. error_propagate(errp, err);
  61. visit_free(v);
  62. v = qapi_dealloc_visitor_new();
  63. visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
  64. visit_free(v);
  65. }
  66. ''',
  67. c_type=ret_type.c_type(), c_name=ret_type.c_name())
  68. def gen_marshal_proto(name):
  69. ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
  70. if not middle_mode:
  71. ret = 'static ' + ret
  72. return ret
  73. def gen_marshal_decl(name):
  74. return mcgen('''
  75. %(proto)s;
  76. ''',
  77. proto=gen_marshal_proto(name))
  78. def gen_marshal(name, arg_type, ret_type):
  79. ret = mcgen('''
  80. %(proto)s
  81. {
  82. Error *err = NULL;
  83. ''',
  84. proto=gen_marshal_proto(name))
  85. if ret_type:
  86. ret += mcgen('''
  87. %(c_type)s retval;
  88. ''',
  89. c_type=ret_type.c_type())
  90. if arg_type and arg_type.members:
  91. ret += mcgen('''
  92. QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true);
  93. Visitor *v;
  94. %(c_name)s arg = {0};
  95. v = qmp_input_get_visitor(qiv);
  96. visit_start_struct(v, NULL, NULL, 0, &err);
  97. if (err) {
  98. goto out;
  99. }
  100. visit_type_%(c_name)s_members(v, &arg, &err);
  101. if (!err) {
  102. visit_check_struct(v, &err);
  103. }
  104. visit_end_struct(v, NULL);
  105. if (err) {
  106. goto out;
  107. }
  108. ''',
  109. c_name=arg_type.c_name())
  110. else:
  111. ret += mcgen('''
  112. (void)args;
  113. ''')
  114. ret += gen_call(name, arg_type, ret_type)
  115. # 'goto out' produced above for arg_type, and by gen_call() for ret_type
  116. if (arg_type and arg_type.members) or ret_type:
  117. ret += mcgen('''
  118. out:
  119. ''')
  120. ret += mcgen('''
  121. error_propagate(errp, err);
  122. ''')
  123. if arg_type and arg_type.members:
  124. ret += mcgen('''
  125. visit_free(v);
  126. v = qapi_dealloc_visitor_new();
  127. visit_start_struct(v, NULL, NULL, 0, NULL);
  128. visit_type_%(c_name)s_members(v, &arg, NULL);
  129. visit_end_struct(v, NULL);
  130. visit_free(v);
  131. ''',
  132. c_name=arg_type.c_name())
  133. ret += mcgen('''
  134. }
  135. ''')
  136. return ret
  137. def gen_register_command(name, success_response):
  138. options = 'QCO_NO_OPTIONS'
  139. if not success_response:
  140. options = 'QCO_NO_SUCCESS_RESP'
  141. ret = mcgen('''
  142. qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
  143. ''',
  144. name=name, c_name=c_name(name),
  145. opts=options)
  146. return ret
  147. def gen_registry(registry):
  148. ret = mcgen('''
  149. static void qmp_init_marshal(void)
  150. {
  151. ''')
  152. ret += registry
  153. ret += mcgen('''
  154. }
  155. qapi_init(qmp_init_marshal);
  156. ''')
  157. return ret
  158. class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
  159. def __init__(self):
  160. self.decl = None
  161. self.defn = None
  162. self._regy = None
  163. self._visited_ret_types = None
  164. def visit_begin(self, schema):
  165. self.decl = ''
  166. self.defn = ''
  167. self._regy = ''
  168. self._visited_ret_types = set()
  169. def visit_end(self):
  170. if not middle_mode:
  171. self.defn += gen_registry(self._regy)
  172. self._regy = None
  173. self._visited_ret_types = None
  174. def visit_command(self, name, info, arg_type, ret_type,
  175. gen, success_response):
  176. if not gen:
  177. return
  178. self.decl += gen_command_decl(name, arg_type, ret_type)
  179. if ret_type and ret_type not in self._visited_ret_types:
  180. self._visited_ret_types.add(ret_type)
  181. self.defn += gen_marshal_output(ret_type)
  182. if middle_mode:
  183. self.decl += gen_marshal_decl(name)
  184. self.defn += gen_marshal(name, arg_type, ret_type)
  185. if not middle_mode:
  186. self._regy += gen_register_command(name, success_response)
  187. middle_mode = False
  188. (input_file, output_dir, do_c, do_h, prefix, opts) = \
  189. parse_command_line("m", ["middle"])
  190. for o, a in opts:
  191. if o in ("-m", "--middle"):
  192. middle_mode = True
  193. c_comment = '''
  194. /*
  195. * schema-defined QMP->QAPI command dispatch
  196. *
  197. * Copyright IBM, Corp. 2011
  198. *
  199. * Authors:
  200. * Anthony Liguori <aliguori@us.ibm.com>
  201. *
  202. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  203. * See the COPYING.LIB file in the top-level directory.
  204. *
  205. */
  206. '''
  207. h_comment = '''
  208. /*
  209. * schema-defined QAPI function prototypes
  210. *
  211. * Copyright IBM, Corp. 2011
  212. *
  213. * Authors:
  214. * Anthony Liguori <aliguori@us.ibm.com>
  215. *
  216. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  217. * See the COPYING.LIB file in the top-level directory.
  218. *
  219. */
  220. '''
  221. (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
  222. 'qmp-marshal.c', 'qmp-commands.h',
  223. c_comment, h_comment)
  224. fdef.write(mcgen('''
  225. #include "qemu/osdep.h"
  226. #include "qemu-common.h"
  227. #include "qemu/module.h"
  228. #include "qapi/qmp/types.h"
  229. #include "qapi/qmp/dispatch.h"
  230. #include "qapi/visitor.h"
  231. #include "qapi/qmp-output-visitor.h"
  232. #include "qapi/qmp-input-visitor.h"
  233. #include "qapi/dealloc-visitor.h"
  234. #include "%(prefix)sqapi-types.h"
  235. #include "%(prefix)sqapi-visit.h"
  236. #include "%(prefix)sqmp-commands.h"
  237. ''',
  238. prefix=prefix))
  239. fdecl.write(mcgen('''
  240. #include "%(prefix)sqapi-types.h"
  241. #include "qapi/qmp/qdict.h"
  242. #include "qapi/error.h"
  243. ''',
  244. prefix=prefix))
  245. schema = QAPISchema(input_file)
  246. gen = QAPISchemaGenCommandVisitor()
  247. schema.visit(gen)
  248. fdef.write(gen.defn)
  249. fdecl.write(gen.decl)
  250. close_output(fdef, fdecl)