qapi-types.py 9.8 KB

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