qapi-types.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #
  2. # QAPI types generator
  3. #
  4. # Copyright IBM, Corp. 2011
  5. # Copyright (c) 2013-2015 Red Hat Inc.
  6. #
  7. # Authors:
  8. # Anthony Liguori <aliguori@us.ibm.com>
  9. # Markus Armbruster <armbru@redhat.com>
  10. #
  11. # This work is licensed under the terms of the GNU GPL, version 2.
  12. # See the COPYING file in the top-level directory.
  13. from qapi import *
  14. def gen_fwd_object_or_array(name):
  15. return mcgen('''
  16. typedef struct %(c_name)s %(c_name)s;
  17. ''',
  18. c_name=c_name(name))
  19. def gen_array(name, element_type):
  20. return mcgen('''
  21. struct %(c_name)s {
  22. union {
  23. %(c_type)s value;
  24. uint64_t padding;
  25. };
  26. %(c_name)s *next;
  27. };
  28. ''',
  29. c_name=c_name(name), c_type=element_type.c_type())
  30. def gen_struct_field(name, typ, optional):
  31. ret = ''
  32. if optional:
  33. ret += mcgen('''
  34. bool has_%(c_name)s;
  35. ''',
  36. c_name=c_name(name))
  37. ret += mcgen('''
  38. %(c_type)s %(c_name)s;
  39. ''',
  40. c_type=typ.c_type(), c_name=c_name(name))
  41. return ret
  42. def gen_struct_fields(members):
  43. ret = ''
  44. for memb in members:
  45. ret += gen_struct_field(memb.name, memb.type, memb.optional)
  46. return ret
  47. def gen_struct(name, base, members):
  48. ret = mcgen('''
  49. struct %(c_name)s {
  50. ''',
  51. c_name=c_name(name))
  52. if base:
  53. ret += gen_struct_field('base', base, False)
  54. ret += gen_struct_fields(members)
  55. # Make sure that all structs have at least one field; this avoids
  56. # potential issues with attempting to malloc space for zero-length
  57. # structs in C, and also incompatibility with C++ (where an empty
  58. # struct is size 1).
  59. if not base and not members:
  60. ret += mcgen('''
  61. char qapi_dummy_field_for_empty_struct;
  62. ''')
  63. ret += mcgen('''
  64. };
  65. ''')
  66. return ret
  67. def gen_alternate_qtypes_decl(name):
  68. return mcgen('''
  69. extern const int %(c_name)s_qtypes[];
  70. ''',
  71. c_name=c_name(name))
  72. def gen_alternate_qtypes(name, variants):
  73. ret = mcgen('''
  74. const int %(c_name)s_qtypes[QTYPE_MAX] = {
  75. ''',
  76. c_name=c_name(name))
  77. for var in variants.variants:
  78. qtype = var.type.alternate_qtype()
  79. assert qtype
  80. ret += mcgen('''
  81. [%(qtype)s] = %(enum_const)s,
  82. ''',
  83. qtype=qtype,
  84. enum_const=c_enum_const(variants.tag_member.type.name,
  85. var.name))
  86. ret += mcgen('''
  87. };
  88. ''')
  89. return ret
  90. def gen_union(name, base, variants):
  91. ret = mcgen('''
  92. struct %(c_name)s {
  93. ''',
  94. c_name=c_name(name))
  95. if base:
  96. ret += mcgen('''
  97. /* Members inherited from %(c_name)s: */
  98. ''',
  99. c_name=c_name(base.name))
  100. ret += gen_struct_fields(base.members)
  101. ret += mcgen('''
  102. /* Own members: */
  103. ''')
  104. else:
  105. ret += mcgen('''
  106. %(c_type)s kind;
  107. ''',
  108. c_type=c_name(variants.tag_member.type.name))
  109. # FIXME: What purpose does data serve, besides preventing a union that
  110. # has a branch named 'data'? We use it in qapi-visit.py to decide
  111. # whether to bypass the switch statement if visiting the discriminator
  112. # failed; but since we 0-initialize structs, and cannot tell what
  113. # branch of the union is in use if the discriminator is invalid, there
  114. # should not be any data leaks even without a data pointer. Or, if
  115. # 'data' is merely added to guarantee we don't have an empty union,
  116. # shouldn't we enforce that at .json parse time?
  117. ret += mcgen('''
  118. union { /* union tag is @%(c_name)s */
  119. void *data;
  120. ''',
  121. # TODO ugly special case for simple union
  122. # Use same tag name in C as on the wire to get rid of
  123. # it, then: c_name=c_name(variants.tag_member.name)
  124. c_name=c_name(variants.tag_name or 'kind'))
  125. for var in variants.variants:
  126. # Ugly special case for simple union TODO get rid of it
  127. typ = var.simple_union_type() or var.type
  128. ret += mcgen('''
  129. %(c_type)s %(c_name)s;
  130. ''',
  131. c_type=typ.c_type(),
  132. c_name=c_name(var.name))
  133. ret += mcgen('''
  134. };
  135. };
  136. ''')
  137. return ret
  138. def gen_type_cleanup_decl(name):
  139. ret = mcgen('''
  140. void qapi_free_%(c_name)s(%(c_name)s *obj);
  141. ''',
  142. c_name=c_name(name))
  143. return ret
  144. def gen_type_cleanup(name):
  145. ret = mcgen('''
  146. void qapi_free_%(c_name)s(%(c_name)s *obj)
  147. {
  148. QapiDeallocVisitor *qdv;
  149. Visitor *v;
  150. if (!obj) {
  151. return;
  152. }
  153. qdv = qapi_dealloc_visitor_new();
  154. v = qapi_dealloc_get_visitor(qdv);
  155. visit_type_%(c_name)s(v, &obj, NULL, NULL);
  156. qapi_dealloc_visitor_cleanup(qdv);
  157. }
  158. ''',
  159. c_name=c_name(name))
  160. return ret
  161. class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
  162. def __init__(self):
  163. self.decl = None
  164. self.defn = None
  165. self._fwdecl = None
  166. self._fwdefn = None
  167. self._btin = None
  168. def visit_begin(self, schema):
  169. self.decl = ''
  170. self.defn = ''
  171. self._fwdecl = ''
  172. self._fwdefn = ''
  173. self._btin = guardstart('QAPI_TYPES_BUILTIN')
  174. def visit_end(self):
  175. self.decl = self._fwdecl + self.decl
  176. self._fwdecl = None
  177. self.defn = self._fwdefn + self.defn
  178. self._fwdefn = None
  179. # To avoid header dependency hell, we always generate
  180. # declarations for built-in types in our header files and
  181. # simply guard them. See also do_builtins (command line
  182. # option -b).
  183. self._btin += guardend('QAPI_TYPES_BUILTIN')
  184. self.decl = self._btin + self.decl
  185. self._btin = None
  186. def visit_needed(self, entity):
  187. # Visit everything except implicit objects
  188. return not isinstance(entity, QAPISchemaObjectType) or entity.info
  189. def _gen_type_cleanup(self, name):
  190. self.decl += gen_type_cleanup_decl(name)
  191. self.defn += gen_type_cleanup(name)
  192. def visit_enum_type(self, name, info, values, prefix):
  193. self._fwdecl += gen_enum(name, values, prefix)
  194. self._fwdefn += gen_enum_lookup(name, values, prefix)
  195. def visit_array_type(self, name, info, element_type):
  196. if isinstance(element_type, QAPISchemaBuiltinType):
  197. self._btin += gen_fwd_object_or_array(name)
  198. self._btin += gen_array(name, element_type)
  199. self._btin += gen_type_cleanup_decl(name)
  200. if do_builtins:
  201. self.defn += gen_type_cleanup(name)
  202. else:
  203. self._fwdecl += gen_fwd_object_or_array(name)
  204. self.decl += gen_array(name, element_type)
  205. self._gen_type_cleanup(name)
  206. def visit_object_type(self, name, info, base, members, variants):
  207. self._fwdecl += gen_fwd_object_or_array(name)
  208. if variants:
  209. assert not members # not implemented
  210. self.decl += gen_union(name, base, variants)
  211. else:
  212. self.decl += gen_struct(name, base, members)
  213. self._gen_type_cleanup(name)
  214. def visit_alternate_type(self, name, info, variants):
  215. self._fwdecl += gen_fwd_object_or_array(name)
  216. self._fwdefn += gen_alternate_qtypes(name, variants)
  217. self.decl += gen_union(name, None, variants)
  218. self.decl += gen_alternate_qtypes_decl(name)
  219. self._gen_type_cleanup(name)
  220. # If you link code generated from multiple schemata, you want only one
  221. # instance of the code for built-in types. Generate it only when
  222. # do_builtins, enabled by command line option -b. See also
  223. # QAPISchemaGenTypeVisitor.visit_end().
  224. do_builtins = False
  225. (input_file, output_dir, do_c, do_h, prefix, opts) = \
  226. parse_command_line("b", ["builtins"])
  227. for o, a in opts:
  228. if o in ("-b", "--builtins"):
  229. do_builtins = True
  230. c_comment = '''
  231. /*
  232. * deallocation functions for schema-defined QAPI types
  233. *
  234. * Copyright IBM, Corp. 2011
  235. *
  236. * Authors:
  237. * Anthony Liguori <aliguori@us.ibm.com>
  238. * Michael Roth <mdroth@linux.vnet.ibm.com>
  239. *
  240. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  241. * See the COPYING.LIB file in the top-level directory.
  242. *
  243. */
  244. '''
  245. h_comment = '''
  246. /*
  247. * schema-defined QAPI types
  248. *
  249. * Copyright IBM, Corp. 2011
  250. *
  251. * Authors:
  252. * Anthony Liguori <aliguori@us.ibm.com>
  253. *
  254. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  255. * See the COPYING.LIB file in the top-level directory.
  256. *
  257. */
  258. '''
  259. (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
  260. 'qapi-types.c', 'qapi-types.h',
  261. c_comment, h_comment)
  262. fdef.write(mcgen('''
  263. #include "qapi/dealloc-visitor.h"
  264. #include "%(prefix)sqapi-types.h"
  265. #include "%(prefix)sqapi-visit.h"
  266. ''',
  267. prefix=prefix))
  268. fdecl.write(mcgen('''
  269. #include <stdbool.h>
  270. #include <stdint.h>
  271. #include "qapi/qmp/qobject.h"
  272. '''))
  273. schema = QAPISchema(input_file)
  274. gen = QAPISchemaGenTypeVisitor()
  275. schema.visit(gen)
  276. fdef.write(gen.defn)
  277. fdecl.write(gen.decl)
  278. close_output(fdef, fdecl)