qapi-types.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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=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)
  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_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. ret += gen_struct_field(variants.tag_member)
  117. # FIXME: What purpose does data serve, besides preventing a union that
  118. # has a branch named 'data'? We use it in qapi-visit.py to decide
  119. # whether to bypass the switch statement if visiting the discriminator
  120. # failed; but since we 0-initialize structs, and cannot tell what
  121. # branch of the union is in use if the discriminator is invalid, there
  122. # should not be any data leaks even without a data pointer. Or, if
  123. # 'data' is merely added to guarantee we don't have an empty union,
  124. # shouldn't we enforce that at .json parse time?
  125. ret += mcgen('''
  126. union { /* union tag is @%(c_name)s */
  127. void *data;
  128. ''',
  129. c_name=c_name(variants.tag_member.name))
  130. for var in variants.variants:
  131. # Ugly special case for simple union TODO get rid of it
  132. typ = var.simple_union_type() or var.type
  133. ret += mcgen('''
  134. %(c_type)s %(c_name)s;
  135. ''',
  136. c_type=typ.c_type(),
  137. c_name=c_name(var.name))
  138. ret += mcgen('''
  139. } u;
  140. };
  141. ''')
  142. return ret
  143. def gen_type_cleanup_decl(name):
  144. ret = mcgen('''
  145. void qapi_free_%(c_name)s(%(c_name)s *obj);
  146. ''',
  147. c_name=c_name(name))
  148. return ret
  149. def gen_type_cleanup(name):
  150. ret = mcgen('''
  151. void qapi_free_%(c_name)s(%(c_name)s *obj)
  152. {
  153. QapiDeallocVisitor *qdv;
  154. Visitor *v;
  155. if (!obj) {
  156. return;
  157. }
  158. qdv = qapi_dealloc_visitor_new();
  159. v = qapi_dealloc_get_visitor(qdv);
  160. visit_type_%(c_name)s(v, &obj, NULL, NULL);
  161. qapi_dealloc_visitor_cleanup(qdv);
  162. }
  163. ''',
  164. c_name=c_name(name))
  165. return ret
  166. class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
  167. def __init__(self):
  168. self.decl = None
  169. self.defn = None
  170. self._fwdecl = None
  171. self._fwdefn = None
  172. self._btin = None
  173. def visit_begin(self, schema):
  174. self.decl = ''
  175. self.defn = ''
  176. self._fwdecl = ''
  177. self._fwdefn = ''
  178. self._btin = guardstart('QAPI_TYPES_BUILTIN')
  179. def visit_end(self):
  180. self.decl = self._fwdecl + self.decl
  181. self._fwdecl = None
  182. self.defn = self._fwdefn + self.defn
  183. self._fwdefn = None
  184. # To avoid header dependency hell, we always generate
  185. # declarations for built-in types in our header files and
  186. # simply guard them. See also do_builtins (command line
  187. # option -b).
  188. self._btin += guardend('QAPI_TYPES_BUILTIN')
  189. self.decl = self._btin + self.decl
  190. self._btin = None
  191. def visit_needed(self, entity):
  192. # Visit everything except implicit objects
  193. return not (entity.is_implicit() and
  194. isinstance(entity, QAPISchemaObjectType))
  195. def _gen_type_cleanup(self, name):
  196. self.decl += gen_type_cleanup_decl(name)
  197. self.defn += gen_type_cleanup(name)
  198. def visit_enum_type(self, name, info, values, prefix):
  199. self._fwdecl += gen_enum(name, values, prefix)
  200. self._fwdefn += gen_enum_lookup(name, values, prefix)
  201. def visit_array_type(self, name, info, element_type):
  202. if isinstance(element_type, QAPISchemaBuiltinType):
  203. self._btin += gen_fwd_object_or_array(name)
  204. self._btin += gen_array(name, element_type)
  205. self._btin += gen_type_cleanup_decl(name)
  206. if do_builtins:
  207. self.defn += gen_type_cleanup(name)
  208. else:
  209. self._fwdecl += gen_fwd_object_or_array(name)
  210. self.decl += gen_array(name, element_type)
  211. self._gen_type_cleanup(name)
  212. def visit_object_type(self, name, info, base, members, variants):
  213. self._fwdecl += gen_fwd_object_or_array(name)
  214. if variants:
  215. assert not members # not implemented
  216. self.decl += gen_union(name, base, variants)
  217. else:
  218. self.decl += gen_struct(name, base, members)
  219. if base:
  220. self.decl += gen_upcast(name, base)
  221. self._gen_type_cleanup(name)
  222. def visit_alternate_type(self, name, info, variants):
  223. self._fwdecl += gen_fwd_object_or_array(name)
  224. self._fwdefn += gen_alternate_qtypes(name, variants)
  225. self.decl += gen_union(name, None, variants)
  226. self.decl += gen_alternate_qtypes_decl(name)
  227. self._gen_type_cleanup(name)
  228. # If you link code generated from multiple schemata, you want only one
  229. # instance of the code for built-in types. Generate it only when
  230. # do_builtins, enabled by command line option -b. See also
  231. # QAPISchemaGenTypeVisitor.visit_end().
  232. do_builtins = False
  233. (input_file, output_dir, do_c, do_h, prefix, opts) = \
  234. parse_command_line("b", ["builtins"])
  235. for o, a in opts:
  236. if o in ("-b", "--builtins"):
  237. do_builtins = True
  238. c_comment = '''
  239. /*
  240. * deallocation functions for schema-defined QAPI types
  241. *
  242. * Copyright IBM, Corp. 2011
  243. *
  244. * Authors:
  245. * Anthony Liguori <aliguori@us.ibm.com>
  246. * Michael Roth <mdroth@linux.vnet.ibm.com>
  247. *
  248. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  249. * See the COPYING.LIB file in the top-level directory.
  250. *
  251. */
  252. '''
  253. h_comment = '''
  254. /*
  255. * schema-defined QAPI types
  256. *
  257. * Copyright IBM, Corp. 2011
  258. *
  259. * Authors:
  260. * Anthony Liguori <aliguori@us.ibm.com>
  261. *
  262. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  263. * See the COPYING.LIB file in the top-level directory.
  264. *
  265. */
  266. '''
  267. (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
  268. 'qapi-types.c', 'qapi-types.h',
  269. c_comment, h_comment)
  270. fdef.write(mcgen('''
  271. #include "qapi/dealloc-visitor.h"
  272. #include "%(prefix)sqapi-types.h"
  273. #include "%(prefix)sqapi-visit.h"
  274. ''',
  275. prefix=prefix))
  276. fdecl.write(mcgen('''
  277. #include <stdbool.h>
  278. #include <stdint.h>
  279. #include "qapi/qmp/qobject.h"
  280. '''))
  281. schema = QAPISchema(input_file)
  282. gen = QAPISchemaGenTypeVisitor()
  283. schema.visit(gen)
  284. fdef.write(gen.defn)
  285. fdecl.write(gen.decl)
  286. close_output(fdef, fdecl)