tests: acpi: add applesmc testcase
[qemu.git] / scripts / qapi / types.py
blob477d02700137c2abdb1cc9dab411a0a43b7bab06
1 """
2 QAPI types generator
4 Copyright IBM, Corp. 2011
5 Copyright (c) 2013-2018 Red Hat Inc.
7 Authors:
8 Anthony Liguori <aliguori@us.ibm.com>
9 Michael Roth <mdroth@linux.vnet.ibm.com>
10 Markus Armbruster <armbru@redhat.com>
12 This work is licensed under the terms of the GNU GPL, version 2.
13 # See the COPYING file in the top-level directory.
14 """
16 from typing import List, Optional
18 from .common import c_enum_const, c_name, mcgen
19 from .gen import (
20 QAPISchemaModularCVisitor,
21 gen_special_features,
22 ifcontext,
24 from .schema import (
25 QAPISchema,
26 QAPISchemaEnumMember,
27 QAPISchemaFeature,
28 QAPISchemaIfCond,
29 QAPISchemaObjectType,
30 QAPISchemaObjectTypeMember,
31 QAPISchemaType,
32 QAPISchemaVariants,
34 from .source import QAPISourceInfo
37 # variants must be emitted before their container; track what has already
38 # been output
39 objects_seen = set()
42 def gen_enum_lookup(name: str,
43 members: List[QAPISchemaEnumMember],
44 prefix: Optional[str] = None) -> str:
45 max_index = c_enum_const(name, '_MAX', prefix)
46 feats = ''
47 ret = mcgen('''
49 const QEnumLookup %(c_name)s_lookup = {
50 .array = (const char *const[]) {
51 ''',
52 c_name=c_name(name))
53 for memb in members:
54 ret += memb.ifcond.gen_if()
55 index = c_enum_const(name, memb.name, prefix)
56 ret += mcgen('''
57 [%(index)s] = "%(name)s",
58 ''',
59 index=index, name=memb.name)
60 ret += memb.ifcond.gen_endif()
62 special_features = gen_special_features(memb.features)
63 if special_features != '0':
64 feats += mcgen('''
65 [%(index)s] = %(special_features)s,
66 ''',
67 index=index, special_features=special_features)
69 if feats:
70 ret += mcgen('''
72 .special_features = (const unsigned char[%(max_index)s]) {
73 ''',
74 max_index=max_index)
75 ret += feats
77 ret += mcgen('''
79 .size = %(max_index)s
81 ''',
82 max_index=max_index)
83 return ret
86 def gen_enum(name: str,
87 members: List[QAPISchemaEnumMember],
88 prefix: Optional[str] = None) -> str:
89 # append automatically generated _MAX value
90 enum_members = members + [QAPISchemaEnumMember('_MAX', None)]
92 ret = mcgen('''
94 typedef enum %(c_name)s {
95 ''',
96 c_name=c_name(name))
98 for memb in enum_members:
99 ret += memb.ifcond.gen_if()
100 ret += mcgen('''
101 %(c_enum)s,
102 ''',
103 c_enum=c_enum_const(name, memb.name, prefix))
104 ret += memb.ifcond.gen_endif()
106 ret += mcgen('''
107 } %(c_name)s;
108 ''',
109 c_name=c_name(name))
111 ret += mcgen('''
113 #define %(c_name)s_str(val) \\
114 qapi_enum_lookup(&%(c_name)s_lookup, (val))
116 extern const QEnumLookup %(c_name)s_lookup;
117 ''',
118 c_name=c_name(name))
119 return ret
122 def gen_fwd_object_or_array(name: str) -> str:
123 return mcgen('''
125 typedef struct %(c_name)s %(c_name)s;
126 ''',
127 c_name=c_name(name))
130 def gen_array(name: str, element_type: QAPISchemaType) -> str:
131 return mcgen('''
133 struct %(c_name)s {
134 %(c_name)s *next;
135 %(c_type)s value;
137 ''',
138 c_name=c_name(name), c_type=element_type.c_type())
141 def gen_struct_members(members: List[QAPISchemaObjectTypeMember]) -> str:
142 ret = ''
143 for memb in members:
144 ret += memb.ifcond.gen_if()
145 if memb.optional:
146 ret += mcgen('''
147 bool has_%(c_name)s;
148 ''',
149 c_name=c_name(memb.name))
150 ret += mcgen('''
151 %(c_type)s %(c_name)s;
152 ''',
153 c_type=memb.type.c_type(), c_name=c_name(memb.name))
154 ret += memb.ifcond.gen_endif()
155 return ret
158 def gen_object(name: str, ifcond: QAPISchemaIfCond,
159 base: Optional[QAPISchemaObjectType],
160 members: List[QAPISchemaObjectTypeMember],
161 variants: Optional[QAPISchemaVariants]) -> str:
162 if name in objects_seen:
163 return ''
164 objects_seen.add(name)
166 ret = ''
167 for var in variants.variants if variants else ():
168 obj = var.type
169 if not isinstance(obj, QAPISchemaObjectType):
170 continue
171 ret += gen_object(obj.name, obj.ifcond, obj.base,
172 obj.local_members, obj.variants)
174 ret += mcgen('''
176 ''')
177 ret += ifcond.gen_if()
178 ret += mcgen('''
179 struct %(c_name)s {
180 ''',
181 c_name=c_name(name))
183 if base:
184 if not base.is_implicit():
185 ret += mcgen('''
186 /* Members inherited from %(c_name)s: */
187 ''',
188 c_name=base.c_name())
189 ret += gen_struct_members(base.members)
190 if not base.is_implicit():
191 ret += mcgen('''
192 /* Own members: */
193 ''')
194 ret += gen_struct_members(members)
196 if variants:
197 ret += gen_variants(variants)
199 # Make sure that all structs have at least one member; this avoids
200 # potential issues with attempting to malloc space for zero-length
201 # structs in C, and also incompatibility with C++ (where an empty
202 # struct is size 1).
203 if (not base or base.is_empty()) and not members and not variants:
204 ret += mcgen('''
205 char qapi_dummy_for_empty_struct;
206 ''')
208 ret += mcgen('''
210 ''')
211 ret += ifcond.gen_endif()
213 return ret
216 def gen_upcast(name: str, base: QAPISchemaObjectType) -> str:
217 # C makes const-correctness ugly. We have to cast away const to let
218 # this function work for both const and non-const obj.
219 return mcgen('''
221 static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
223 return (%(base)s *)obj;
225 ''',
226 c_name=c_name(name), base=base.c_name())
229 def gen_variants(variants: QAPISchemaVariants) -> str:
230 ret = mcgen('''
231 union { /* union tag is @%(c_name)s */
232 ''',
233 c_name=c_name(variants.tag_member.name))
235 for var in variants.variants:
236 if var.type.name == 'q_empty':
237 continue
238 ret += var.ifcond.gen_if()
239 ret += mcgen('''
240 %(c_type)s %(c_name)s;
241 ''',
242 c_type=var.type.c_unboxed_type(),
243 c_name=c_name(var.name))
244 ret += var.ifcond.gen_endif()
246 ret += mcgen('''
247 } u;
248 ''')
250 return ret
253 def gen_type_cleanup_decl(name: str) -> str:
254 ret = mcgen('''
256 void qapi_free_%(c_name)s(%(c_name)s *obj);
257 G_DEFINE_AUTOPTR_CLEANUP_FUNC(%(c_name)s, qapi_free_%(c_name)s)
258 ''',
259 c_name=c_name(name))
260 return ret
263 def gen_type_cleanup(name: str) -> str:
264 ret = mcgen('''
266 void qapi_free_%(c_name)s(%(c_name)s *obj)
268 Visitor *v;
270 if (!obj) {
271 return;
274 v = qapi_dealloc_visitor_new();
275 visit_type_%(c_name)s(v, NULL, &obj, NULL);
276 visit_free(v);
278 ''',
279 c_name=c_name(name))
280 return ret
283 class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor):
285 def __init__(self, prefix: str):
286 super().__init__(
287 prefix, 'qapi-types', ' * Schema-defined QAPI types',
288 ' * Built-in QAPI types', __doc__)
290 def _begin_builtin_module(self) -> None:
291 self._genc.preamble_add(mcgen('''
292 #include "qemu/osdep.h"
293 #include "qapi/dealloc-visitor.h"
294 #include "qapi/qapi-builtin-types.h"
295 #include "qapi/qapi-builtin-visit.h"
296 '''))
297 self._genh.preamble_add(mcgen('''
298 #include "qapi/util.h"
299 '''))
301 def _begin_user_module(self, name: str) -> None:
302 types = self._module_basename('qapi-types', name)
303 visit = self._module_basename('qapi-visit', name)
304 self._genc.preamble_add(mcgen('''
305 #include "qemu/osdep.h"
306 #include "qapi/dealloc-visitor.h"
307 #include "%(types)s.h"
308 #include "%(visit)s.h"
309 ''',
310 types=types, visit=visit))
311 self._genh.preamble_add(mcgen('''
312 #include "qapi/qapi-builtin-types.h"
313 '''))
315 def visit_begin(self, schema: QAPISchema) -> None:
316 # gen_object() is recursive, ensure it doesn't visit the empty type
317 objects_seen.add(schema.the_empty_object_type.name)
319 def _gen_type_cleanup(self, name: str) -> None:
320 self._genh.add(gen_type_cleanup_decl(name))
321 self._genc.add(gen_type_cleanup(name))
323 def visit_enum_type(self,
324 name: str,
325 info: Optional[QAPISourceInfo],
326 ifcond: QAPISchemaIfCond,
327 features: List[QAPISchemaFeature],
328 members: List[QAPISchemaEnumMember],
329 prefix: Optional[str]) -> None:
330 with ifcontext(ifcond, self._genh, self._genc):
331 self._genh.preamble_add(gen_enum(name, members, prefix))
332 self._genc.add(gen_enum_lookup(name, members, prefix))
334 def visit_array_type(self,
335 name: str,
336 info: Optional[QAPISourceInfo],
337 ifcond: QAPISchemaIfCond,
338 element_type: QAPISchemaType) -> None:
339 with ifcontext(ifcond, self._genh, self._genc):
340 self._genh.preamble_add(gen_fwd_object_or_array(name))
341 self._genh.add(gen_array(name, element_type))
342 self._gen_type_cleanup(name)
344 def visit_object_type(self,
345 name: str,
346 info: Optional[QAPISourceInfo],
347 ifcond: QAPISchemaIfCond,
348 features: List[QAPISchemaFeature],
349 base: Optional[QAPISchemaObjectType],
350 members: List[QAPISchemaObjectTypeMember],
351 variants: Optional[QAPISchemaVariants]) -> None:
352 # Nothing to do for the special empty builtin
353 if name == 'q_empty':
354 return
355 with ifcontext(ifcond, self._genh):
356 self._genh.preamble_add(gen_fwd_object_or_array(name))
357 self._genh.add(gen_object(name, ifcond, base, members, variants))
358 with ifcontext(ifcond, self._genh, self._genc):
359 if base and not base.is_implicit():
360 self._genh.add(gen_upcast(name, base))
361 # TODO Worth changing the visitor signature, so we could
362 # directly use rather than repeat type.is_implicit()?
363 if not name.startswith('q_'):
364 # implicit types won't be directly allocated/freed
365 self._gen_type_cleanup(name)
367 def visit_alternate_type(self,
368 name: str,
369 info: Optional[QAPISourceInfo],
370 ifcond: QAPISchemaIfCond,
371 features: List[QAPISchemaFeature],
372 variants: QAPISchemaVariants) -> None:
373 with ifcontext(ifcond, self._genh):
374 self._genh.preamble_add(gen_fwd_object_or_array(name))
375 self._genh.add(gen_object(name, ifcond, None,
376 [variants.tag_member], variants))
377 with ifcontext(ifcond, self._genh, self._genc):
378 self._gen_type_cleanup(name)
381 def gen_types(schema: QAPISchema,
382 output_dir: str,
383 prefix: str,
384 opt_builtins: bool) -> None:
385 vis = QAPISchemaGenTypeVisitor(prefix)
386 schema.visit(vis)
387 vis.write(output_dir, opt_builtins)