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