]>
Commit | Line | Data |
---|---|---|
fb3182ce MR |
1 | # |
2 | # QAPI types generator | |
3 | # | |
4 | # Copyright IBM, Corp. 2011 | |
5 | # | |
6 | # Authors: | |
7 | # Anthony Liguori <[email protected]> | |
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=c_var(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 | i = 0 | |
95 | for value in values: | |
96 | enum_decl += mcgen(''' | |
97 | %(abbrev)s_%(value)s = %(i)d, | |
98 | ''', | |
99 | abbrev=de_camel_case(name).upper(), | |
100 | value=c_var(value).upper(), | |
101 | i=i) | |
102 | i += 1 | |
103 | ||
104 | enum_decl += mcgen(''' | |
105 | } %(name)s; | |
106 | ''', | |
107 | name=name) | |
108 | ||
109 | return lookup_decl + enum_decl | |
110 | ||
111 | def generate_union(name, typeinfo): | |
112 | ret = mcgen(''' | |
113 | struct %(name)s | |
114 | { | |
115 | %(name)sKind kind; | |
116 | union { | |
117 | ''', | |
118 | name=name) | |
119 | ||
120 | for key in typeinfo: | |
121 | ret += mcgen(''' | |
122 | %(c_type)s %(c_name)s; | |
123 | ''', | |
124 | c_type=c_type(typeinfo[key]), | |
125 | c_name=c_var(key)) | |
126 | ||
127 | ret += mcgen(''' | |
128 | }; | |
129 | }; | |
130 | ''') | |
131 | ||
132 | return ret | |
133 | ||
134 | def generate_type_cleanup_decl(name): | |
135 | ret = mcgen(''' | |
136 | void qapi_free_%(type)s(%(c_type)s obj); | |
137 | ''', | |
138 | c_type=c_type(name),type=name) | |
139 | return ret | |
140 | ||
141 | def generate_type_cleanup(name): | |
142 | ret = mcgen(''' | |
143 | void qapi_free_%(type)s(%(c_type)s obj) | |
144 | { | |
145 | QapiDeallocVisitor *md; | |
146 | Visitor *v; | |
147 | ||
148 | if (!obj) { | |
149 | return; | |
150 | } | |
151 | ||
152 | md = qapi_dealloc_visitor_new(); | |
153 | v = qapi_dealloc_get_visitor(md); | |
154 | visit_type_%(type)s(v, &obj, NULL, NULL); | |
155 | qapi_dealloc_visitor_cleanup(md); | |
156 | } | |
157 | ''', | |
158 | c_type=c_type(name),type=name) | |
159 | return ret | |
160 | ||
161 | ||
162 | try: | |
163 | opts, args = getopt.gnu_getopt(sys.argv[1:], "p:o:", ["prefix=", "output-dir="]) | |
164 | except getopt.GetoptError, err: | |
165 | print str(err) | |
166 | sys.exit(1) | |
167 | ||
168 | output_dir = "" | |
169 | prefix = "" | |
170 | c_file = 'qapi-types.c' | |
171 | h_file = 'qapi-types.h' | |
172 | ||
173 | for o, a in opts: | |
174 | if o in ("-p", "--prefix"): | |
175 | prefix = a | |
176 | elif o in ("-o", "--output-dir"): | |
177 | output_dir = a + "/" | |
178 | ||
179 | c_file = output_dir + prefix + c_file | |
180 | h_file = output_dir + prefix + h_file | |
181 | ||
182 | try: | |
183 | os.makedirs(output_dir) | |
184 | except os.error, e: | |
185 | if e.errno != errno.EEXIST: | |
186 | raise | |
187 | ||
188 | fdef = open(c_file, 'w') | |
189 | fdecl = open(h_file, 'w') | |
190 | ||
191 | fdef.write(mcgen(''' | |
192 | /* AUTOMATICALLY GENERATED, DO NOT MODIFY */ | |
193 | ||
194 | /* | |
195 | * deallocation functions for schema-defined QAPI types | |
196 | * | |
197 | * Copyright IBM, Corp. 2011 | |
198 | * | |
199 | * Authors: | |
200 | * Anthony Liguori <[email protected]> | |
201 | * Michael Roth <[email protected]> | |
202 | * | |
203 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
204 | * See the COPYING.LIB file in the top-level directory. | |
205 | * | |
206 | */ | |
207 | ||
208 | #include "qapi/qapi-dealloc-visitor.h" | |
209 | #include "%(prefix)sqapi-types.h" | |
210 | #include "%(prefix)sqapi-visit.h" | |
211 | ||
212 | ''', prefix=prefix)) | |
213 | ||
214 | fdecl.write(mcgen(''' | |
215 | /* AUTOMATICALLY GENERATED, DO NOT MODIFY */ | |
216 | ||
217 | /* | |
218 | * schema-defined QAPI types | |
219 | * | |
220 | * Copyright IBM, Corp. 2011 | |
221 | * | |
222 | * Authors: | |
223 | * Anthony Liguori <[email protected]> | |
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 | #ifndef %(guard)s | |
231 | #define %(guard)s | |
232 | ||
233 | #include "qapi/qapi-types-core.h" | |
234 | ''', | |
235 | guard=guardname(h_file))) | |
236 | ||
237 | exprs = parse_schema(sys.stdin) | |
238 | ||
239 | for expr in exprs: | |
240 | ret = "\n" | |
241 | if expr.has_key('type'): | |
242 | ret += generate_fwd_struct(expr['type'], expr['data']) | |
243 | elif expr.has_key('enum'): | |
244 | ret += generate_enum(expr['enum'], expr['data']) | |
245 | fdef.write(generate_enum_lookup(expr['enum'], expr['data'])) | |
246 | elif expr.has_key('union'): | |
247 | ret += generate_fwd_struct(expr['union'], expr['data']) + "\n" | |
248 | ret += generate_enum('%sKind' % expr['union'], expr['data'].keys()) | |
249 | else: | |
250 | continue | |
251 | fdecl.write(ret) | |
252 | ||
253 | for expr in exprs: | |
254 | ret = "\n" | |
255 | if expr.has_key('type'): | |
256 | ret += generate_struct(expr['type'], "", expr['data']) + "\n" | |
257 | ret += generate_type_cleanup_decl(expr['type']) | |
258 | fdef.write(generate_type_cleanup(expr['type']) + "\n") | |
259 | elif expr.has_key('union'): | |
260 | ret += generate_union(expr['union'], expr['data']) | |
261 | else: | |
262 | continue | |
263 | fdecl.write(ret) | |
264 | ||
265 | fdecl.write(''' | |
266 | #endif | |
267 | ''') | |
268 | ||
269 | fdecl.flush() | |
270 | fdecl.close() | |
776574d6 AL |
271 | |
272 | fdef.flush() | |
273 | fdef.close() |