qapi-commands.py 7.6 KB

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