2
0

qapi-commands.py 6.8 KB

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