qapi-types.py 8.8 KB

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