qdev: split out UI portions into a new function
[qemu.git] / scripts / qapi-types.py
1 #
2 # QAPI types generator
3 #
4 # Copyright IBM, Corp. 2011
5 #
6 # Authors:
7 # Anthony Liguori <aliguori@us.ibm.com>
8 #
9 # This work is licensed under the terms of the GNU GPLv2.
10 # See the COPYING.LIB file in the top-level directory.
11
12 from ordereddict import OrderedDict
13 from qapi import *
14 import sys
15 import os
16 import getopt
17 import errno
18
19 def generate_fwd_struct(name, members):
20 return mcgen('''
21 typedef struct %(name)s %(name)s;
22
23 typedef struct %(name)sList
24 {
25 %(name)s *value;
26 struct %(name)sList *next;
27 } %(name)sList;
28 ''',
29 name=name)
30
31 def generate_struct(structname, fieldname, members):
32 ret = mcgen('''
33 struct %(name)s
34 {
35 ''',
36 name=structname)
37
38 for argname, argentry, optional, structured in parse_args(members):
39 if optional:
40 ret += mcgen('''
41 bool has_%(c_name)s;
42 ''',
43 c_name=c_var(argname))
44 if structured:
45 push_indent()
46 ret += generate_struct("", argname, argentry)
47 pop_indent()
48 else:
49 ret += mcgen('''
50 %(c_type)s %(c_name)s;
51 ''',
52 c_type=c_type(argentry), c_name=c_var(argname))
53
54 if len(fieldname):
55 fieldname = " " + fieldname
56 ret += mcgen('''
57 }%(field)s;
58 ''',
59 field=fieldname)
60
61 return ret
62
63 def generate_enum_lookup(name, values):
64 ret = mcgen('''
65 const char *%(name)s_lookup[] = {
66 ''',
67 name=name)
68 i = 0
69 for value in values:
70 ret += mcgen('''
71 "%(value)s",
72 ''',
73 value=value.lower())
74
75 ret += mcgen('''
76 NULL,
77 };
78
79 ''')
80 return ret
81
82 def generate_enum(name, values):
83 lookup_decl = mcgen('''
84 extern const char *%(name)s_lookup[];
85 ''',
86 name=name)
87
88 enum_decl = mcgen('''
89 typedef enum %(name)s
90 {
91 ''',
92 name=name)
93
94 # append automatically generated _MAX value
95 enum_values = values + [ 'MAX' ]
96
97 i = 0
98 for value in enum_values:
99 enum_decl += mcgen('''
100 %(abbrev)s_%(value)s = %(i)d,
101 ''',
102 abbrev=de_camel_case(name).upper(),
103 value=c_var(value).upper(),
104 i=i)
105 i += 1
106
107 enum_decl += mcgen('''
108 } %(name)s;
109 ''',
110 name=name)
111
112 return lookup_decl + enum_decl
113
114 def generate_union(name, typeinfo):
115 ret = mcgen('''
116 struct %(name)s
117 {
118 %(name)sKind kind;
119 union {
120 ''',
121 name=name)
122
123 for key in typeinfo:
124 ret += mcgen('''
125 %(c_type)s %(c_name)s;
126 ''',
127 c_type=c_type(typeinfo[key]),
128 c_name=c_var(key))
129
130 ret += mcgen('''
131 };
132 };
133 ''')
134
135 return ret
136
137 def generate_type_cleanup_decl(name):
138 ret = mcgen('''
139 void qapi_free_%(type)s(%(c_type)s obj);
140 ''',
141 c_type=c_type(name),type=name)
142 return ret
143
144 def generate_type_cleanup(name):
145 ret = mcgen('''
146 void qapi_free_%(type)s(%(c_type)s obj)
147 {
148 QapiDeallocVisitor *md;
149 Visitor *v;
150
151 if (!obj) {
152 return;
153 }
154
155 md = qapi_dealloc_visitor_new();
156 v = qapi_dealloc_get_visitor(md);
157 visit_type_%(type)s(v, &obj, NULL, NULL);
158 qapi_dealloc_visitor_cleanup(md);
159 }
160 ''',
161 c_type=c_type(name),type=name)
162 return ret
163
164
165 try:
166 opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:",
167 ["source", "header", "prefix=", "output-dir="])
168 except getopt.GetoptError, err:
169 print str(err)
170 sys.exit(1)
171
172 output_dir = ""
173 prefix = ""
174 c_file = 'qapi-types.c'
175 h_file = 'qapi-types.h'
176
177 do_c = False
178 do_h = False
179
180 for o, a in opts:
181 if o in ("-p", "--prefix"):
182 prefix = a
183 elif o in ("-o", "--output-dir"):
184 output_dir = a + "/"
185 elif o in ("-c", "--source"):
186 do_c = True
187 elif o in ("-h", "--header"):
188 do_h = True
189
190 if not do_c and not do_h:
191 do_c = True
192 do_h = True
193
194 c_file = output_dir + prefix + c_file
195 h_file = output_dir + prefix + h_file
196
197 try:
198 os.makedirs(output_dir)
199 except os.error, e:
200 if e.errno != errno.EEXIST:
201 raise
202
203 def maybe_open(really, name, opt):
204 if really:
205 return open(name, opt)
206 else:
207 import StringIO
208 return StringIO.StringIO()
209
210 fdef = maybe_open(do_c, c_file, 'w')
211 fdecl = maybe_open(do_h, h_file, 'w')
212
213 fdef.write(mcgen('''
214 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
215
216 /*
217 * deallocation functions for schema-defined QAPI types
218 *
219 * Copyright IBM, Corp. 2011
220 *
221 * Authors:
222 * Anthony Liguori <aliguori@us.ibm.com>
223 * Michael Roth <mdroth@linux.vnet.ibm.com>
224 *
225 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
226 * See the COPYING.LIB file in the top-level directory.
227 *
228 */
229
230 #include "qapi/qapi-dealloc-visitor.h"
231 #include "%(prefix)sqapi-types.h"
232 #include "%(prefix)sqapi-visit.h"
233
234 ''', prefix=prefix))
235
236 fdecl.write(mcgen('''
237 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
238
239 /*
240 * schema-defined QAPI types
241 *
242 * Copyright IBM, Corp. 2011
243 *
244 * Authors:
245 * Anthony Liguori <aliguori@us.ibm.com>
246 *
247 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
248 * See the COPYING.LIB file in the top-level directory.
249 *
250 */
251
252 #ifndef %(guard)s
253 #define %(guard)s
254
255 #include "qapi/qapi-types-core.h"
256 ''',
257 guard=guardname(h_file)))
258
259 exprs = parse_schema(sys.stdin)
260 exprs = filter(lambda expr: not expr.has_key('gen'), exprs)
261
262 for expr in exprs:
263 ret = "\n"
264 if expr.has_key('type'):
265 ret += generate_fwd_struct(expr['type'], expr['data'])
266 elif expr.has_key('enum'):
267 ret += generate_enum(expr['enum'], expr['data'])
268 fdef.write(generate_enum_lookup(expr['enum'], expr['data']))
269 elif expr.has_key('union'):
270 ret += generate_fwd_struct(expr['union'], expr['data']) + "\n"
271 ret += generate_enum('%sKind' % expr['union'], expr['data'].keys())
272 else:
273 continue
274 fdecl.write(ret)
275
276 for expr in exprs:
277 ret = "\n"
278 if expr.has_key('type'):
279 ret += generate_struct(expr['type'], "", expr['data']) + "\n"
280 ret += generate_type_cleanup_decl(expr['type'] + "List")
281 fdef.write(generate_type_cleanup(expr['type'] + "List") + "\n")
282 ret += generate_type_cleanup_decl(expr['type'])
283 fdef.write(generate_type_cleanup(expr['type']) + "\n")
284 elif expr.has_key('union'):
285 ret += generate_union(expr['union'], expr['data'])
286 else:
287 continue
288 fdecl.write(ret)
289
290 fdecl.write('''
291 #endif
292 ''')
293
294 fdecl.flush()
295 fdecl.close()
296
297 fdef.flush()
298 fdef.close()