qapi-introspect.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #
  2. # QAPI introspection generator
  3. #
  4. # Copyright (C) 2015 Red Hat, Inc.
  5. #
  6. # Authors:
  7. # Markus Armbruster <armbru@redhat.com>
  8. #
  9. # This work is licensed under the terms of the GNU GPL, version 2.
  10. # See the COPYING file in the top-level directory.
  11. from qapi import *
  12. # Caveman's json.dumps() replacement (we're stuck at Python 2.4)
  13. # TODO try to use json.dumps() once we get unstuck
  14. def to_json(obj, level=0):
  15. if obj is None:
  16. ret = 'null'
  17. elif isinstance(obj, str):
  18. ret = '"' + obj.replace('"', r'\"') + '"'
  19. elif isinstance(obj, list):
  20. elts = [to_json(elt, level + 1)
  21. for elt in obj]
  22. ret = '[' + ', '.join(elts) + ']'
  23. elif isinstance(obj, dict):
  24. elts = ['"%s": %s' % (key.replace('"', r'\"'),
  25. to_json(obj[key], level + 1))
  26. for key in sorted(obj.keys())]
  27. ret = '{' + ', '.join(elts) + '}'
  28. else:
  29. assert False # not implemented
  30. if level == 1:
  31. ret = '\n' + ret
  32. return ret
  33. def to_c_string(string):
  34. return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
  35. class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):
  36. def __init__(self):
  37. self.defn = None
  38. self.decl = None
  39. self._schema = None
  40. self._jsons = None
  41. self._used_types = None
  42. def visit_begin(self, schema):
  43. self._schema = schema
  44. self._jsons = []
  45. self._used_types = []
  46. return QAPISchemaType # don't visit types for now
  47. def visit_end(self):
  48. # visit the types that are actually used
  49. for typ in self._used_types:
  50. typ.visit(self)
  51. self._jsons.sort()
  52. # generate C
  53. # TODO can generate awfully long lines
  54. name = prefix + 'qmp_schema_json'
  55. self.decl = mcgen('''
  56. extern const char %(c_name)s[];
  57. ''',
  58. c_name=c_name(name))
  59. lines = to_json(self._jsons).split('\n')
  60. c_string = '\n '.join([to_c_string(line) for line in lines])
  61. self.defn = mcgen('''
  62. const char %(c_name)s[] = %(c_string)s;
  63. ''',
  64. c_name=c_name(name),
  65. c_string=c_string)
  66. self._schema = None
  67. self._jsons = None
  68. self._used_types = None
  69. def _use_type(self, typ):
  70. # Map the various integer types to plain int
  71. if typ.json_type() == 'int':
  72. typ = self._schema.lookup_type('int')
  73. elif (isinstance(typ, QAPISchemaArrayType) and
  74. typ.element_type.json_type() == 'int'):
  75. typ = self._schema.lookup_type('intList')
  76. # Add type to work queue if new
  77. if typ not in self._used_types:
  78. self._used_types.append(typ)
  79. return typ.name
  80. def _gen_json(self, name, mtype, obj):
  81. obj['name'] = name
  82. obj['meta-type'] = mtype
  83. self._jsons.append(obj)
  84. def _gen_member(self, member):
  85. ret = {'name': member.name, 'type': self._use_type(member.type)}
  86. if member.optional:
  87. ret['default'] = None
  88. return ret
  89. def _gen_variants(self, tag_name, variants):
  90. return {'tag': tag_name,
  91. 'variants': [self._gen_variant(v) for v in variants]}
  92. def _gen_variant(self, variant):
  93. return {'case': variant.name, 'type': self._use_type(variant.type)}
  94. def visit_builtin_type(self, name, info, json_type):
  95. self._gen_json(name, 'builtin', {'json-type': json_type})
  96. def visit_enum_type(self, name, info, values, prefix):
  97. self._gen_json(name, 'enum', {'values': values})
  98. def visit_array_type(self, name, info, element_type):
  99. self._gen_json(name, 'array',
  100. {'element-type': self._use_type(element_type)})
  101. def visit_object_type_flat(self, name, info, members, variants):
  102. obj = {'members': [self._gen_member(m) for m in members]}
  103. if variants:
  104. obj.update(self._gen_variants(variants.tag_member.name,
  105. variants.variants))
  106. self._gen_json(name, 'object', obj)
  107. def visit_alternate_type(self, name, info, variants):
  108. self._gen_json(name, 'alternate',
  109. {'members': [{'type': self._use_type(m.type)}
  110. for m in variants.variants]})
  111. def visit_command(self, name, info, arg_type, ret_type,
  112. gen, success_response):
  113. arg_type = arg_type or self._schema.the_empty_object_type
  114. ret_type = ret_type or self._schema.the_empty_object_type
  115. self._gen_json(name, 'command',
  116. {'arg-type': self._use_type(arg_type),
  117. 'ret-type': self._use_type(ret_type)})
  118. def visit_event(self, name, info, arg_type):
  119. arg_type = arg_type or self._schema.the_empty_object_type
  120. self._gen_json(name, 'event', {'arg-type': self._use_type(arg_type)})
  121. (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
  122. c_comment = '''
  123. /*
  124. * QAPI/QMP schema introspection
  125. *
  126. * Copyright (C) 2015 Red Hat, Inc.
  127. *
  128. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  129. * See the COPYING.LIB file in the top-level directory.
  130. *
  131. */
  132. '''
  133. h_comment = '''
  134. /*
  135. * QAPI/QMP schema introspection
  136. *
  137. * Copyright (C) 2015 Red Hat, Inc.
  138. *
  139. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  140. * See the COPYING.LIB file in the top-level directory.
  141. *
  142. */
  143. '''
  144. (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
  145. 'qmp-introspect.c', 'qmp-introspect.h',
  146. c_comment, h_comment)
  147. fdef.write(mcgen('''
  148. #include "%(prefix)sqmp-introspect.h"
  149. ''',
  150. prefix=prefix))
  151. schema = QAPISchema(input_file)
  152. gen = QAPISchemaGenIntrospectVisitor()
  153. schema.visit(gen)
  154. fdef.write(gen.defn)
  155. fdecl.write(gen.decl)
  156. close_output(fdef, fdecl)