qapi-types.py 8.1 KB

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