qapi-commands.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. QapiDeallocVisitor *qdv;
  53. Visitor *v;
  54. v = qmp_output_get_visitor(qov);
  55. visit_type_%(c_name)s(v, "unused", &ret_in, &err);
  56. if (err) {
  57. goto out;
  58. }
  59. *ret_out = qmp_output_get_qobject(qov);
  60. out:
  61. error_propagate(errp, err);
  62. qmp_output_visitor_cleanup(qov);
  63. qdv = qapi_dealloc_visitor_new();
  64. v = qapi_dealloc_get_visitor(qdv);
  65. visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
  66. qapi_dealloc_visitor_cleanup(qdv);
  67. }
  68. ''',
  69. c_type=ret_type.c_type(), c_name=ret_type.c_name())
  70. def gen_marshal_proto(name):
  71. ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
  72. if not middle_mode:
  73. ret = 'static ' + ret
  74. return ret
  75. def gen_marshal_decl(name):
  76. return mcgen('''
  77. %(proto)s;
  78. ''',
  79. proto=gen_marshal_proto(name))
  80. def gen_marshal(name, arg_type, ret_type):
  81. ret = mcgen('''
  82. %(proto)s
  83. {
  84. Error *err = NULL;
  85. ''',
  86. proto=gen_marshal_proto(name))
  87. if ret_type:
  88. ret += mcgen('''
  89. %(c_type)s retval;
  90. ''',
  91. c_type=ret_type.c_type())
  92. if arg_type and arg_type.members:
  93. ret += mcgen('''
  94. QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true);
  95. QapiDeallocVisitor *qdv;
  96. Visitor *v;
  97. %(c_name)s arg = {0};
  98. v = qmp_input_get_visitor(qiv);
  99. visit_start_struct(v, NULL, NULL, 0, &err);
  100. if (err) {
  101. goto out;
  102. }
  103. visit_type_%(c_name)s_members(v, &arg, &err);
  104. if (!err) {
  105. visit_check_struct(v, &err);
  106. }
  107. visit_end_struct(v);
  108. if (err) {
  109. goto out;
  110. }
  111. ''',
  112. c_name=arg_type.c_name())
  113. else:
  114. ret += mcgen('''
  115. (void)args;
  116. ''')
  117. ret += gen_call(name, arg_type, ret_type)
  118. # 'goto out' produced above for arg_type, and by gen_call() for ret_type
  119. if (arg_type and arg_type.members) or ret_type:
  120. ret += mcgen('''
  121. out:
  122. ''')
  123. ret += mcgen('''
  124. error_propagate(errp, err);
  125. ''')
  126. if arg_type and arg_type.members:
  127. ret += mcgen('''
  128. qmp_input_visitor_cleanup(qiv);
  129. qdv = qapi_dealloc_visitor_new();
  130. v = qapi_dealloc_get_visitor(qdv);
  131. visit_start_struct(v, NULL, NULL, 0, NULL);
  132. visit_type_%(c_name)s_members(v, &arg, NULL);
  133. visit_end_struct(v);
  134. qapi_dealloc_visitor_cleanup(qdv);
  135. ''',
  136. c_name=arg_type.c_name())
  137. ret += mcgen('''
  138. }
  139. ''')
  140. return ret
  141. def gen_register_command(name, success_response):
  142. options = 'QCO_NO_OPTIONS'
  143. if not success_response:
  144. options = 'QCO_NO_SUCCESS_RESP'
  145. ret = mcgen('''
  146. qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
  147. ''',
  148. name=name, c_name=c_name(name),
  149. opts=options)
  150. return ret
  151. def gen_registry(registry):
  152. ret = mcgen('''
  153. static void qmp_init_marshal(void)
  154. {
  155. ''')
  156. ret += registry
  157. ret += mcgen('''
  158. }
  159. qapi_init(qmp_init_marshal);
  160. ''')
  161. return ret
  162. class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
  163. def __init__(self):
  164. self.decl = None
  165. self.defn = None
  166. self._regy = None
  167. self._visited_ret_types = None
  168. def visit_begin(self, schema):
  169. self.decl = ''
  170. self.defn = ''
  171. self._regy = ''
  172. self._visited_ret_types = set()
  173. def visit_end(self):
  174. if not middle_mode:
  175. self.defn += gen_registry(self._regy)
  176. self._regy = None
  177. self._visited_ret_types = None
  178. def visit_command(self, name, info, arg_type, ret_type,
  179. gen, success_response):
  180. if not gen:
  181. return
  182. self.decl += gen_command_decl(name, arg_type, ret_type)
  183. if ret_type and ret_type not in self._visited_ret_types:
  184. self._visited_ret_types.add(ret_type)
  185. self.defn += gen_marshal_output(ret_type)
  186. if middle_mode:
  187. self.decl += gen_marshal_decl(name)
  188. self.defn += gen_marshal(name, arg_type, ret_type)
  189. if not middle_mode:
  190. self._regy += gen_register_command(name, success_response)
  191. middle_mode = False
  192. (input_file, output_dir, do_c, do_h, prefix, opts) = \
  193. parse_command_line("m", ["middle"])
  194. for o, a in opts:
  195. if o in ("-m", "--middle"):
  196. middle_mode = True
  197. c_comment = '''
  198. /*
  199. * schema-defined QMP->QAPI command dispatch
  200. *
  201. * Copyright IBM, Corp. 2011
  202. *
  203. * Authors:
  204. * Anthony Liguori <aliguori@us.ibm.com>
  205. *
  206. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  207. * See the COPYING.LIB file in the top-level directory.
  208. *
  209. */
  210. '''
  211. h_comment = '''
  212. /*
  213. * schema-defined QAPI function prototypes
  214. *
  215. * Copyright IBM, Corp. 2011
  216. *
  217. * Authors:
  218. * Anthony Liguori <aliguori@us.ibm.com>
  219. *
  220. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  221. * See the COPYING.LIB file in the top-level directory.
  222. *
  223. */
  224. '''
  225. (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
  226. 'qmp-marshal.c', 'qmp-commands.h',
  227. c_comment, h_comment)
  228. fdef.write(mcgen('''
  229. #include "qemu/osdep.h"
  230. #include "qemu-common.h"
  231. #include "qemu/module.h"
  232. #include "qapi/qmp/types.h"
  233. #include "qapi/qmp/dispatch.h"
  234. #include "qapi/visitor.h"
  235. #include "qapi/qmp-output-visitor.h"
  236. #include "qapi/qmp-input-visitor.h"
  237. #include "qapi/dealloc-visitor.h"
  238. #include "%(prefix)sqapi-types.h"
  239. #include "%(prefix)sqapi-visit.h"
  240. #include "%(prefix)sqmp-commands.h"
  241. ''',
  242. prefix=prefix))
  243. fdecl.write(mcgen('''
  244. #include "%(prefix)sqapi-types.h"
  245. #include "qapi/qmp/qdict.h"
  246. #include "qapi/error.h"
  247. ''',
  248. prefix=prefix))
  249. schema = QAPISchema(input_file)
  250. gen = QAPISchemaGenCommandVisitor()
  251. schema.visit(gen)
  252. fdef.write(gen.defn)
  253. fdecl.write(gen.decl)
  254. close_output(fdef, fdecl)