i8254: Factor out pit_get_channel_info
[qemu/opensuse.git] / scripts / qapi-types.py
blobb56225bdaa9e0e708c5a54b66342a9a1df939636
2 # QAPI types generator
4 # Copyright IBM, Corp. 2011
6 # Authors:
7 # Anthony Liguori <aliguori@us.ibm.com>
9 # This work is licensed under the terms of the GNU GPLv2.
10 # See the COPYING.LIB file in the top-level directory.
12 from ordereddict import OrderedDict
13 from qapi import *
14 import sys
15 import os
16 import getopt
17 import errno
19 def generate_fwd_struct(name, members):
20 return mcgen('''
21 typedef struct %(name)s %(name)s;
23 typedef struct %(name)sList
25 %(name)s *value;
26 struct %(name)sList *next;
27 } %(name)sList;
28 ''',
29 name=name)
31 def generate_struct(structname, fieldname, members):
32 ret = mcgen('''
33 struct %(name)s
35 ''',
36 name=structname)
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))
54 if len(fieldname):
55 fieldname = " " + fieldname
56 ret += mcgen('''
57 }%(field)s;
58 ''',
59 field=fieldname)
61 return ret
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())
75 ret += mcgen('''
76 NULL,
79 ''')
80 return ret
82 def generate_enum(name, values):
83 lookup_decl = mcgen('''
84 extern const char *%(name)s_lookup[];
85 ''',
86 name=name)
88 enum_decl = mcgen('''
89 typedef enum %(name)s
91 ''',
92 name=name)
94 # append automatically generated _MAX value
95 enum_values = values + [ 'MAX' ]
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
107 enum_decl += mcgen('''
108 } %(name)s;
109 ''',
110 name=name)
112 return lookup_decl + enum_decl
114 def generate_union(name, typeinfo):
115 ret = mcgen('''
116 struct %(name)s
118 %(name)sKind kind;
119 union {
120 ''',
121 name=name)
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))
130 ret += mcgen('''
133 ''')
135 return ret
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
144 def generate_type_cleanup(name):
145 ret = mcgen('''
146 void qapi_free_%(type)s(%(c_type)s obj)
148 QapiDeallocVisitor *md;
149 Visitor *v;
151 if (!obj) {
152 return;
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);
160 ''',
161 c_type=c_type(name),type=name)
162 return ret
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)
172 output_dir = ""
173 prefix = ""
174 c_file = 'qapi-types.c'
175 h_file = 'qapi-types.h'
177 do_c = False
178 do_h = False
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
190 if not do_c and not do_h:
191 do_c = True
192 do_h = True
194 c_file = output_dir + prefix + c_file
195 h_file = output_dir + prefix + h_file
197 try:
198 os.makedirs(output_dir)
199 except os.error, e:
200 if e.errno != errno.EEXIST:
201 raise
203 def maybe_open(really, name, opt):
204 if really:
205 return open(name, opt)
206 else:
207 import StringIO
208 return StringIO.StringIO()
210 fdef = maybe_open(do_c, c_file, 'w')
211 fdecl = maybe_open(do_h, h_file, 'w')
213 fdef.write(mcgen('''
214 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
217 * deallocation functions for schema-defined QAPI types
219 * Copyright IBM, Corp. 2011
221 * Authors:
222 * Anthony Liguori <aliguori@us.ibm.com>
223 * Michael Roth <mdroth@linux.vnet.ibm.com>
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.
230 #include "qapi/qapi-dealloc-visitor.h"
231 #include "%(prefix)sqapi-types.h"
232 #include "%(prefix)sqapi-visit.h"
234 ''', prefix=prefix))
236 fdecl.write(mcgen('''
237 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
240 * schema-defined QAPI types
242 * Copyright IBM, Corp. 2011
244 * Authors:
245 * Anthony Liguori <aliguori@us.ibm.com>
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.
252 #ifndef %(guard)s
253 #define %(guard)s
255 #include "qapi/qapi-types-core.h"
256 ''',
257 guard=guardname(h_file)))
259 exprs = parse_schema(sys.stdin)
260 exprs = filter(lambda expr: not expr.has_key('gen'), exprs)
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)
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)
290 fdecl.write('''
291 #endif
292 ''')
294 fdecl.flush()
295 fdecl.close()
297 fdef.flush()
298 fdef.close()