1 # -*- coding: utf-8 -*-
3 # QAPI schema internal representation
5 # Copyright (c) 2015-2019 Red Hat Inc.
8 # Markus Armbruster <armbru@redhat.com>
9 # Eric Blake <eblake@redhat.com>
10 # Marc-André Lureau <marcandre.lureau@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.
15 # TODO catching name collisions in generated code would be nice
19 from collections
import OrderedDict
21 from qapi
.common
import c_name
, pointer_suffix
22 from qapi
.error
import QAPIError
, QAPISemError
23 from qapi
.expr
import check_exprs
24 from qapi
.parser
import QAPISchemaParser
27 class QAPISchemaEntity
:
30 def __init__(self
, name
, info
, doc
, ifcond
=None, features
=None):
31 assert name
is None or isinstance(name
, str)
32 for f
in features
or []:
33 assert isinstance(f
, QAPISchemaFeature
)
34 f
.set_defined_in(name
)
37 # For explicitly defined entities, info points to the (explicit)
38 # definition. For builtins (and their arrays), info is None.
39 # For implicitly defined entities, info points to a place that
40 # triggered the implicit definition (there may be more than one
44 self
._ifcond
= ifcond
or []
45 self
.features
= features
or []
49 return c_name(self
.name
)
51 def check(self
, schema
):
52 assert not self
._checked
54 for f
in self
.features
:
55 f
.check_clash(self
.info
, seen
)
57 self
.doc
.connect_feature(f
)
61 def connect_doc(self
, doc
=None):
68 def _set_module(self
, schema
, info
):
70 self
._module
= schema
.module_by_fname(info
and info
.fname
)
71 self
._module
.add_entity(self
)
73 def set_module(self
, schema
):
74 self
._set
_module
(schema
, self
.info
)
81 def is_implicit(self
):
84 def visit(self
, visitor
):
89 return "%s '%s'" % (self
.meta
, self
.name
)
92 class QAPISchemaVisitor
:
93 def visit_begin(self
, schema
):
99 def visit_module(self
, name
):
102 def visit_needed(self
, entity
):
103 # Default to visiting everything
106 def visit_include(self
, name
, info
):
109 def visit_builtin_type(self
, name
, info
, json_type
):
112 def visit_enum_type(self
, name
, info
, ifcond
, members
, prefix
):
115 def visit_array_type(self
, name
, info
, ifcond
, element_type
):
118 def visit_object_type(self
, name
, info
, ifcond
, base
, members
, variants
,
122 def visit_object_type_flat(self
, name
, info
, ifcond
, members
, variants
,
126 def visit_alternate_type(self
, name
, info
, ifcond
, variants
):
129 def visit_command(self
, name
, info
, ifcond
, arg_type
, ret_type
, gen
,
130 success_response
, boxed
, allow_oob
, allow_preconfig
,
134 def visit_event(self
, name
, info
, ifcond
, arg_type
, boxed
):
138 class QAPISchemaModule
:
139 def __init__(self
, name
):
141 self
._entity
_list
= []
143 def add_entity(self
, ent
):
144 self
._entity
_list
.append(ent
)
146 def visit(self
, visitor
):
147 visitor
.visit_module(self
.name
)
148 for entity
in self
._entity
_list
:
149 if visitor
.visit_needed(entity
):
150 entity
.visit(visitor
)
153 class QAPISchemaInclude(QAPISchemaEntity
):
154 def __init__(self
, sub_module
, info
):
155 super().__init
__(None, info
, None)
156 self
._sub
_module
= sub_module
158 def visit(self
, visitor
):
159 super().visit(visitor
)
160 visitor
.visit_include(self
._sub
_module
.name
, self
.info
)
163 class QAPISchemaType(QAPISchemaEntity
):
164 # Return the C type for common use.
165 # For the types we commonly box, this is a pointer type.
169 # Return the C type to be used in a parameter list.
170 def c_param_type(self
):
173 # Return the C type to be used where we suppress boxing.
174 def c_unboxed_type(self
):
180 def alternate_qtype(self
):
182 'null': 'QTYPE_QNULL',
183 'string': 'QTYPE_QSTRING',
184 'number': 'QTYPE_QNUM',
186 'boolean': 'QTYPE_QBOOL',
187 'object': 'QTYPE_QDICT'
189 return json2qtype
.get(self
.json_type())
192 if self
.is_implicit():
198 return "%s type '%s'" % (self
.meta
, self
.name
)
201 class QAPISchemaBuiltinType(QAPISchemaType
):
204 def __init__(self
, name
, json_type
, c_type
):
205 super().__init
__(name
, None, None)
206 assert not c_type
or isinstance(c_type
, str)
207 assert json_type
in ('string', 'number', 'int', 'boolean', 'null',
209 self
._json
_type
_name
= json_type
210 self
._c
_type
_name
= c_type
216 return self
._c
_type
_name
218 def c_param_type(self
):
219 if self
.name
== 'str':
220 return 'const ' + self
._c
_type
_name
221 return self
._c
_type
_name
224 return self
._json
_type
_name
227 return self
.json_type()
229 def visit(self
, visitor
):
230 super().visit(visitor
)
231 visitor
.visit_builtin_type(self
.name
, self
.info
, self
.json_type())
234 class QAPISchemaEnumType(QAPISchemaType
):
237 def __init__(self
, name
, info
, doc
, ifcond
, members
, prefix
):
238 super().__init
__(name
, info
, doc
, ifcond
)
240 assert isinstance(m
, QAPISchemaEnumMember
)
241 m
.set_defined_in(name
)
242 assert prefix
is None or isinstance(prefix
, str)
243 self
.members
= members
246 def check(self
, schema
):
247 super().check(schema
)
249 for m
in self
.members
:
250 m
.check_clash(self
.info
, seen
)
252 def connect_doc(self
, doc
=None):
253 doc
= doc
or self
.doc
255 for m
in self
.members
:
256 doc
.connect_member(m
)
258 def is_implicit(self
):
259 # See QAPISchema._make_implicit_enum_type() and ._def_predefineds()
260 return self
.name
.endswith('Kind') or self
.name
== 'QType'
263 return c_name(self
.name
)
265 def member_names(self
):
266 return [m
.name
for m
in self
.members
]
271 def visit(self
, visitor
):
272 super().visit(visitor
)
273 visitor
.visit_enum_type(self
.name
, self
.info
, self
.ifcond
,
274 self
.members
, self
.prefix
)
277 class QAPISchemaArrayType(QAPISchemaType
):
280 def __init__(self
, name
, info
, element_type
):
281 super().__init
__(name
, info
, None, None)
282 assert isinstance(element_type
, str)
283 self
._element
_type
_name
= element_type
284 self
.element_type
= None
286 def check(self
, schema
):
287 super().check(schema
)
288 self
.element_type
= schema
.resolve_type(
289 self
._element
_type
_name
, self
.info
,
290 self
.info
and self
.info
.defn_meta
)
291 assert not isinstance(self
.element_type
, QAPISchemaArrayType
)
293 def set_module(self
, schema
):
294 self
._set
_module
(schema
, self
.element_type
.info
)
299 return self
.element_type
.ifcond
301 def is_implicit(self
):
305 return c_name(self
.name
) + pointer_suffix
311 elt_doc_type
= self
.element_type
.doc_type()
314 return 'array of ' + elt_doc_type
316 def visit(self
, visitor
):
317 super().visit(visitor
)
318 visitor
.visit_array_type(self
.name
, self
.info
, self
.ifcond
,
323 return "%s type ['%s']" % (self
.meta
, self
._element
_type
_name
)
326 class QAPISchemaObjectType(QAPISchemaType
):
327 def __init__(self
, name
, info
, doc
, ifcond
,
328 base
, local_members
, variants
, features
):
329 # struct has local_members, optional base, and no variants
330 # flat union has base, variants, and no local_members
331 # simple union has local_members, variants, and no base
332 super().__init
__(name
, info
, doc
, ifcond
, features
)
333 self
.meta
= 'union' if variants
else 'struct'
334 assert base
is None or isinstance(base
, str)
335 for m
in local_members
:
336 assert isinstance(m
, QAPISchemaObjectTypeMember
)
337 m
.set_defined_in(name
)
338 if variants
is not None:
339 assert isinstance(variants
, QAPISchemaObjectTypeVariants
)
340 variants
.set_defined_in(name
)
341 self
._base
_name
= base
343 self
.local_members
= local_members
344 self
.variants
= variants
347 def check(self
, schema
):
348 # This calls another type T's .check() exactly when the C
349 # struct emitted by gen_object() contains that T's C struct
350 # (pointers don't count).
351 if self
.members
is not None:
352 # A previous .check() completed: nothing to do
355 # Recursed: C struct contains itself
356 raise QAPISemError(self
.info
,
357 "object %s contains itself" % self
.name
)
359 super().check(schema
)
360 assert self
._checked
and self
.members
is None
364 self
.base
= schema
.resolve_type(self
._base
_name
, self
.info
,
366 if (not isinstance(self
.base
, QAPISchemaObjectType
)
367 or self
.base
.variants
):
370 "'base' requires a struct type, %s isn't"
371 % self
.base
.describe())
372 self
.base
.check(schema
)
373 self
.base
.check_clash(self
.info
, seen
)
374 for m
in self
.local_members
:
376 m
.check_clash(self
.info
, seen
)
377 members
= seen
.values()
380 self
.variants
.check(schema
, seen
)
381 self
.variants
.check_clash(self
.info
, seen
)
383 self
.members
= members
# mark completed
385 # Check that the members of this type do not cause duplicate JSON members,
386 # and update seen to track the members seen so far. Report any errors
387 # on behalf of info, which is not necessarily self.info
388 def check_clash(self
, info
, seen
):
390 assert not self
.variants
# not implemented
391 for m
in self
.members
:
392 m
.check_clash(info
, seen
)
394 def connect_doc(self
, doc
=None):
395 doc
= doc
or self
.doc
397 if self
.base
and self
.base
.is_implicit():
398 self
.base
.connect_doc(doc
)
399 for m
in self
.local_members
:
400 doc
.connect_member(m
)
405 if isinstance(self
._ifcond
, QAPISchemaType
):
406 # Simple union wrapper type inherits from wrapped type;
407 # see _make_implicit_object_type()
408 return self
._ifcond
.ifcond
411 def is_implicit(self
):
412 # See QAPISchema._make_implicit_object_type(), as well as
414 return self
.name
.startswith('q_')
417 assert self
.members
is not None
418 return not self
.members
and not self
.variants
421 assert self
.name
!= 'q_empty'
422 return super().c_name()
425 assert not self
.is_implicit()
426 return c_name(self
.name
) + pointer_suffix
428 def c_unboxed_type(self
):
429 return c_name(self
.name
)
434 def visit(self
, visitor
):
435 super().visit(visitor
)
436 visitor
.visit_object_type(self
.name
, self
.info
, self
.ifcond
,
437 self
.base
, self
.local_members
, self
.variants
,
439 visitor
.visit_object_type_flat(self
.name
, self
.info
, self
.ifcond
,
440 self
.members
, self
.variants
,
444 class QAPISchemaMember
:
445 """ Represents object members, enum members and features """
448 def __init__(self
, name
, info
, ifcond
=None):
449 assert isinstance(name
, str)
452 self
.ifcond
= ifcond
or []
453 self
.defined_in
= None
455 def set_defined_in(self
, name
):
456 assert not self
.defined_in
457 self
.defined_in
= name
459 def check_clash(self
, info
, seen
):
460 cname
= c_name(self
.name
)
464 "%s collides with %s"
465 % (self
.describe(info
), seen
[cname
].describe(info
)))
468 def describe(self
, info
):
470 defined_in
= self
.defined_in
473 if defined_in
.startswith('q_obj_'):
474 # See QAPISchema._make_implicit_object_type() - reverse the
475 # mapping there to create a nice human-readable description
476 defined_in
= defined_in
[6:]
477 if defined_in
.endswith('-arg'):
478 # Implicit type created for a command's dict 'data'
479 assert role
== 'member'
481 elif defined_in
.endswith('-base'):
482 # Implicit type created for a flat union's dict 'base'
483 role
= 'base ' + role
485 # Implicit type created for a simple union's branch
486 assert defined_in
.endswith('-wrapper')
487 # Unreachable and not implemented
489 elif defined_in
.endswith('Kind'):
490 # See QAPISchema._make_implicit_enum_type()
491 # Implicit enum created for simple union's branches
492 assert role
== 'value'
494 elif defined_in
!= info
.defn_name
:
495 return "%s '%s' of type '%s'" % (role
, self
.name
, defined_in
)
496 return "%s '%s'" % (role
, self
.name
)
499 class QAPISchemaEnumMember(QAPISchemaMember
):
503 class QAPISchemaFeature(QAPISchemaMember
):
507 class QAPISchemaObjectTypeMember(QAPISchemaMember
):
508 def __init__(self
, name
, info
, typ
, optional
, ifcond
=None):
509 super().__init
__(name
, info
, ifcond
)
510 assert isinstance(typ
, str)
511 assert isinstance(optional
, bool)
512 self
._type
_name
= typ
514 self
.optional
= optional
516 def check(self
, schema
):
517 assert self
.defined_in
518 self
.type = schema
.resolve_type(self
._type
_name
, self
.info
,
522 class QAPISchemaObjectTypeVariants
:
523 def __init__(self
, tag_name
, info
, tag_member
, variants
):
524 # Flat unions pass tag_name but not tag_member.
525 # Simple unions and alternates pass tag_member but not tag_name.
526 # After check(), tag_member is always set, and tag_name remains
527 # a reliable witness of being used by a flat union.
528 assert bool(tag_member
) != bool(tag_name
)
529 assert (isinstance(tag_name
, str) or
530 isinstance(tag_member
, QAPISchemaObjectTypeMember
))
532 assert isinstance(v
, QAPISchemaObjectTypeVariant
)
533 self
._tag
_name
= tag_name
535 self
.tag_member
= tag_member
536 self
.variants
= variants
538 def set_defined_in(self
, name
):
539 for v
in self
.variants
:
540 v
.set_defined_in(name
)
542 def check(self
, schema
, seen
):
543 if not self
.tag_member
: # flat union
544 self
.tag_member
= seen
.get(c_name(self
._tag
_name
))
546 # Pointing to the base type when not implicit would be
547 # nice, but we don't know it here
548 if not self
.tag_member
or self
._tag
_name
!= self
.tag_member
.name
:
551 "discriminator '%s' is not a member of %s"
552 % (self
._tag
_name
, base
))
554 base_type
= schema
.lookup_type(self
.tag_member
.defined_in
)
556 if not base_type
.is_implicit():
557 base
= "base type '%s'" % self
.tag_member
.defined_in
558 if not isinstance(self
.tag_member
.type, QAPISchemaEnumType
):
561 "discriminator member '%s' of %s must be of enum type"
562 % (self
._tag
_name
, base
))
563 if self
.tag_member
.optional
:
566 "discriminator member '%s' of %s must not be optional"
567 % (self
._tag
_name
, base
))
568 if self
.tag_member
.ifcond
:
571 "discriminator member '%s' of %s must not be conditional"
572 % (self
._tag
_name
, base
))
574 assert isinstance(self
.tag_member
.type, QAPISchemaEnumType
)
575 assert not self
.tag_member
.optional
576 assert self
.tag_member
.ifcond
== []
577 if self
._tag
_name
: # flat union
578 # branches that are not explicitly covered get an empty type
579 cases
= {v
.name
for v
in self
.variants
}
580 for m
in self
.tag_member
.type.members
:
581 if m
.name
not in cases
:
582 v
= QAPISchemaObjectTypeVariant(m
.name
, self
.info
,
584 v
.set_defined_in(self
.tag_member
.defined_in
)
585 self
.variants
.append(v
)
586 if not self
.variants
:
587 raise QAPISemError(self
.info
, "union has no branches")
588 for v
in self
.variants
:
590 # Union names must match enum values; alternate names are
591 # checked separately. Use 'seen' to tell the two apart.
593 if v
.name
not in self
.tag_member
.type.member_names():
596 "branch '%s' is not a value of %s"
597 % (v
.name
, self
.tag_member
.type.describe()))
598 if (not isinstance(v
.type, QAPISchemaObjectType
)
603 % (v
.describe(self
.info
), v
.type.describe()))
606 def check_clash(self
, info
, seen
):
607 for v
in self
.variants
:
608 # Reset seen map for each variant, since qapi names from one
609 # branch do not affect another branch
610 v
.type.check_clash(info
, dict(seen
))
613 class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember
):
616 def __init__(self
, name
, info
, typ
, ifcond
=None):
617 super().__init
__(name
, info
, typ
, False, ifcond
)
620 class QAPISchemaAlternateType(QAPISchemaType
):
623 def __init__(self
, name
, info
, doc
, ifcond
, variants
):
624 super().__init
__(name
, info
, doc
, ifcond
)
625 assert isinstance(variants
, QAPISchemaObjectTypeVariants
)
626 assert variants
.tag_member
627 variants
.set_defined_in(name
)
628 variants
.tag_member
.set_defined_in(self
.name
)
629 self
.variants
= variants
631 def check(self
, schema
):
632 super().check(schema
)
633 self
.variants
.tag_member
.check(schema
)
634 # Not calling self.variants.check_clash(), because there's nothing
636 self
.variants
.check(schema
, {})
637 # Alternate branch names have no relation to the tag enum values;
638 # so we have to check for potential name collisions ourselves.
641 for v
in self
.variants
.variants
:
642 v
.check_clash(self
.info
, seen
)
643 qtype
= v
.type.alternate_qtype()
648 % (v
.describe(self
.info
), v
.type.describe()))
649 conflicting
= set([qtype
])
650 if qtype
== 'QTYPE_QSTRING':
651 if isinstance(v
.type, QAPISchemaEnumType
):
652 for m
in v
.type.members
:
653 if m
.name
in ['on', 'off']:
654 conflicting
.add('QTYPE_QBOOL')
655 if re
.match(r
'[-+0-9.]', m
.name
):
656 # lazy, could be tightened
657 conflicting
.add('QTYPE_QNUM')
659 conflicting
.add('QTYPE_QNUM')
660 conflicting
.add('QTYPE_QBOOL')
661 for qt
in conflicting
:
665 "%s can't be distinguished from '%s'"
666 % (v
.describe(self
.info
), types_seen
[qt
]))
667 types_seen
[qt
] = v
.name
669 def connect_doc(self
, doc
=None):
670 doc
= doc
or self
.doc
672 for v
in self
.variants
.variants
:
673 doc
.connect_member(v
)
676 return c_name(self
.name
) + pointer_suffix
681 def visit(self
, visitor
):
682 super().visit(visitor
)
683 visitor
.visit_alternate_type(self
.name
, self
.info
, self
.ifcond
,
687 class QAPISchemaCommand(QAPISchemaEntity
):
690 def __init__(self
, name
, info
, doc
, ifcond
, arg_type
, ret_type
,
691 gen
, success_response
, boxed
, allow_oob
, allow_preconfig
,
693 super().__init
__(name
, info
, doc
, ifcond
, features
)
694 assert not arg_type
or isinstance(arg_type
, str)
695 assert not ret_type
or isinstance(ret_type
, str)
696 self
._arg
_type
_name
= arg_type
698 self
._ret
_type
_name
= ret_type
701 self
.success_response
= success_response
703 self
.allow_oob
= allow_oob
704 self
.allow_preconfig
= allow_preconfig
706 def check(self
, schema
):
707 super().check(schema
)
708 if self
._arg
_type
_name
:
709 self
.arg_type
= schema
.resolve_type(
710 self
._arg
_type
_name
, self
.info
, "command's 'data'")
711 if not isinstance(self
.arg_type
, QAPISchemaObjectType
):
714 "command's 'data' cannot take %s"
715 % self
.arg_type
.describe())
716 if self
.arg_type
.variants
and not self
.boxed
:
719 "command's 'data' can take %s only with 'boxed': true"
720 % self
.arg_type
.describe())
721 if self
._ret
_type
_name
:
722 self
.ret_type
= schema
.resolve_type(
723 self
._ret
_type
_name
, self
.info
, "command's 'returns'")
724 if self
.name
not in self
.info
.pragma
.returns_whitelist
:
726 if isinstance(typ
, QAPISchemaArrayType
):
727 typ
= self
.ret_type
.element_type
729 if not isinstance(typ
, QAPISchemaObjectType
):
732 "command's 'returns' cannot take %s"
733 % self
.ret_type
.describe())
735 def connect_doc(self
, doc
=None):
736 doc
= doc
or self
.doc
738 if self
.arg_type
and self
.arg_type
.is_implicit():
739 self
.arg_type
.connect_doc(doc
)
741 def visit(self
, visitor
):
742 super().visit(visitor
)
743 visitor
.visit_command(self
.name
, self
.info
, self
.ifcond
,
744 self
.arg_type
, self
.ret_type
,
745 self
.gen
, self
.success_response
,
746 self
.boxed
, self
.allow_oob
,
747 self
.allow_preconfig
,
751 class QAPISchemaEvent(QAPISchemaEntity
):
754 def __init__(self
, name
, info
, doc
, ifcond
, arg_type
, boxed
):
755 super().__init
__(name
, info
, doc
, ifcond
)
756 assert not arg_type
or isinstance(arg_type
, str)
757 self
._arg
_type
_name
= arg_type
761 def check(self
, schema
):
762 super().check(schema
)
763 if self
._arg
_type
_name
:
764 self
.arg_type
= schema
.resolve_type(
765 self
._arg
_type
_name
, self
.info
, "event's 'data'")
766 if not isinstance(self
.arg_type
, QAPISchemaObjectType
):
769 "event's 'data' cannot take %s"
770 % self
.arg_type
.describe())
771 if self
.arg_type
.variants
and not self
.boxed
:
774 "event's 'data' can take %s only with 'boxed': true"
775 % self
.arg_type
.describe())
777 def connect_doc(self
, doc
=None):
778 doc
= doc
or self
.doc
780 if self
.arg_type
and self
.arg_type
.is_implicit():
781 self
.arg_type
.connect_doc(doc
)
783 def visit(self
, visitor
):
784 super().visit(visitor
)
785 visitor
.visit_event(self
.name
, self
.info
, self
.ifcond
,
786 self
.arg_type
, self
.boxed
)
790 def __init__(self
, fname
):
792 parser
= QAPISchemaParser(fname
)
793 exprs
= check_exprs(parser
.exprs
)
794 self
.docs
= parser
.docs
795 self
._entity
_list
= []
796 self
._entity
_dict
= {}
797 self
._module
_dict
= OrderedDict()
798 self
._schema
_dir
= os
.path
.dirname(fname
)
799 self
._make
_module
(None) # built-ins
800 self
._make
_module
(fname
)
801 self
._predefining
= True
802 self
._def
_predefineds
()
803 self
._predefining
= False
804 self
._def
_exprs
(exprs
)
807 def _def_entity(self
, ent
):
808 # Only the predefined types are allowed to not have info
809 assert ent
.info
or self
._predefining
810 self
._entity
_list
.append(ent
)
813 # TODO reject names that differ only in '_' vs. '.' vs. '-',
814 # because they're liable to clash in generated C.
815 other_ent
= self
._entity
_dict
.get(ent
.name
)
818 where
= QAPIError(other_ent
.info
, None, "previous definition")
821 "'%s' is already defined\n%s" % (ent
.name
, where
))
823 ent
.info
, "%s is already defined" % other_ent
.describe())
824 self
._entity
_dict
[ent
.name
] = ent
826 def lookup_entity(self
, name
, typ
=None):
827 ent
= self
._entity
_dict
.get(name
)
828 if typ
and not isinstance(ent
, typ
):
832 def lookup_type(self
, name
):
833 return self
.lookup_entity(name
, QAPISchemaType
)
835 def resolve_type(self
, name
, info
, what
):
836 typ
= self
.lookup_type(name
)
841 info
, "%s uses unknown type '%s'" % (what
, name
))
844 def _module_name(self
, fname
):
847 return os
.path
.relpath(fname
, self
._schema
_dir
)
849 def _make_module(self
, fname
):
850 name
= self
._module
_name
(fname
)
851 if name
not in self
._module
_dict
:
852 self
._module
_dict
[name
] = QAPISchemaModule(name
)
853 return self
._module
_dict
[name
]
855 def module_by_fname(self
, fname
):
856 name
= self
._module
_name
(fname
)
857 assert name
in self
._module
_dict
858 return self
._module
_dict
[name
]
860 def _def_include(self
, expr
, info
, doc
):
861 include
= expr
['include']
863 self
._def
_entity
(QAPISchemaInclude(self
._make
_module
(include
), info
))
865 def _def_builtin_type(self
, name
, json_type
, c_type
):
866 self
._def
_entity
(QAPISchemaBuiltinType(name
, json_type
, c_type
))
867 # Instantiating only the arrays that are actually used would
868 # be nice, but we can't as long as their generated code
869 # (qapi-builtin-types.[ch]) may be shared by some other
871 self
._make
_array
_type
(name
, None)
873 def _def_predefineds(self
):
874 for t
in [('str', 'string', 'char' + pointer_suffix
),
875 ('number', 'number', 'double'),
876 ('int', 'int', 'int64_t'),
877 ('int8', 'int', 'int8_t'),
878 ('int16', 'int', 'int16_t'),
879 ('int32', 'int', 'int32_t'),
880 ('int64', 'int', 'int64_t'),
881 ('uint8', 'int', 'uint8_t'),
882 ('uint16', 'int', 'uint16_t'),
883 ('uint32', 'int', 'uint32_t'),
884 ('uint64', 'int', 'uint64_t'),
885 ('size', 'int', 'uint64_t'),
886 ('bool', 'boolean', 'bool'),
887 ('any', 'value', 'QObject' + pointer_suffix
),
888 ('null', 'null', 'QNull' + pointer_suffix
)]:
889 self
._def
_builtin
_type
(*t
)
890 self
.the_empty_object_type
= QAPISchemaObjectType(
891 'q_empty', None, None, None, None, [], None, [])
892 self
._def
_entity
(self
.the_empty_object_type
)
894 qtypes
= ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist',
896 qtype_values
= self
._make
_enum
_members
(
897 [{'name': n
} for n
in qtypes
], None)
899 self
._def
_entity
(QAPISchemaEnumType('QType', None, None, None,
900 qtype_values
, 'QTYPE'))
902 def _make_features(self
, features
, info
):
903 return [QAPISchemaFeature(f
['name'], info
, f
.get('if'))
906 def _make_enum_members(self
, values
, info
):
907 return [QAPISchemaEnumMember(v
['name'], info
, v
.get('if'))
910 def _make_implicit_enum_type(self
, name
, info
, ifcond
, values
):
911 # See also QAPISchemaObjectTypeMember.describe()
912 name
= name
+ 'Kind' # reserved by check_defn_name_str()
913 self
._def
_entity
(QAPISchemaEnumType(
914 name
, info
, None, ifcond
, self
._make
_enum
_members
(values
, info
),
918 def _make_array_type(self
, element_type
, info
):
919 name
= element_type
+ 'List' # reserved by check_defn_name_str()
920 if not self
.lookup_type(name
):
921 self
._def
_entity
(QAPISchemaArrayType(name
, info
, element_type
))
924 def _make_implicit_object_type(self
, name
, info
, ifcond
, role
, members
):
927 # See also QAPISchemaObjectTypeMember.describe()
928 name
= 'q_obj_%s-%s' % (name
, role
)
929 typ
= self
.lookup_entity(name
, QAPISchemaObjectType
)
931 # The implicit object type has multiple users. This can
932 # happen only for simple unions' implicit wrapper types.
933 # Its ifcond should be the disjunction of its user's
934 # ifconds. Not implemented. Instead, we always pass the
935 # wrapped type's ifcond, which is trivially the same for all
936 # users. It's also necessary for the wrapper to compile.
937 # But it's not tight: the disjunction need not imply it. We
938 # may end up compiling useless wrapper types.
939 # TODO kill simple unions or implement the disjunction
940 assert (ifcond
or []) == typ
._ifcond
# pylint: disable=protected-access
942 self
._def
_entity
(QAPISchemaObjectType(name
, info
, None, ifcond
,
943 None, members
, None, []))
946 def _def_enum_type(self
, expr
, info
, doc
):
949 prefix
= expr
.get('prefix')
950 ifcond
= expr
.get('if')
951 self
._def
_entity
(QAPISchemaEnumType(
952 name
, info
, doc
, ifcond
,
953 self
._make
_enum
_members
(data
, info
), prefix
))
955 def _make_member(self
, name
, typ
, ifcond
, info
):
957 if name
.startswith('*'):
960 if isinstance(typ
, list):
962 typ
= self
._make
_array
_type
(typ
[0], info
)
963 return QAPISchemaObjectTypeMember(name
, info
, typ
, optional
, ifcond
)
965 def _make_members(self
, data
, info
):
966 return [self
._make
_member
(key
, value
['type'], value
.get('if'), info
)
967 for (key
, value
) in data
.items()]
969 def _def_struct_type(self
, expr
, info
, doc
):
970 name
= expr
['struct']
971 base
= expr
.get('base')
973 ifcond
= expr
.get('if')
974 features
= expr
.get('features', [])
975 self
._def
_entity
(QAPISchemaObjectType(
976 name
, info
, doc
, ifcond
, base
,
977 self
._make
_members
(data
, info
),
979 self
._make
_features
(features
, info
)))
981 def _make_variant(self
, case
, typ
, ifcond
, info
):
982 return QAPISchemaObjectTypeVariant(case
, info
, typ
, ifcond
)
984 def _make_simple_variant(self
, case
, typ
, ifcond
, info
):
985 if isinstance(typ
, list):
987 typ
= self
._make
_array
_type
(typ
[0], info
)
988 typ
= self
._make
_implicit
_object
_type
(
989 typ
, info
, self
.lookup_type(typ
),
990 'wrapper', [self
._make
_member
('data', typ
, None, info
)])
991 return QAPISchemaObjectTypeVariant(case
, info
, typ
, ifcond
)
993 def _def_union_type(self
, expr
, info
, doc
):
996 base
= expr
.get('base')
997 ifcond
= expr
.get('if')
998 tag_name
= expr
.get('discriminator')
1000 if isinstance(base
, dict):
1001 base
= self
._make
_implicit
_object
_type
(
1003 'base', self
._make
_members
(base
, info
))
1005 variants
= [self
._make
_variant
(key
, value
['type'],
1006 value
.get('if'), info
)
1007 for (key
, value
) in data
.items()]
1010 variants
= [self
._make
_simple
_variant
(key
, value
['type'],
1011 value
.get('if'), info
)
1012 for (key
, value
) in data
.items()]
1013 enum
= [{'name': v
.name
, 'if': v
.ifcond
} for v
in variants
]
1014 typ
= self
._make
_implicit
_enum
_type
(name
, info
, ifcond
, enum
)
1015 tag_member
= QAPISchemaObjectTypeMember('type', info
, typ
, False)
1016 members
= [tag_member
]
1018 QAPISchemaObjectType(name
, info
, doc
, ifcond
, base
, members
,
1019 QAPISchemaObjectTypeVariants(
1020 tag_name
, info
, tag_member
, variants
),
1023 def _def_alternate_type(self
, expr
, info
, doc
):
1024 name
= expr
['alternate']
1026 ifcond
= expr
.get('if')
1027 variants
= [self
._make
_variant
(key
, value
['type'], value
.get('if'),
1029 for (key
, value
) in data
.items()]
1030 tag_member
= QAPISchemaObjectTypeMember('type', info
, 'QType', False)
1032 QAPISchemaAlternateType(name
, info
, doc
, ifcond
,
1033 QAPISchemaObjectTypeVariants(
1034 None, info
, tag_member
, variants
)))
1036 def _def_command(self
, expr
, info
, doc
):
1037 name
= expr
['command']
1038 data
= expr
.get('data')
1039 rets
= expr
.get('returns')
1040 gen
= expr
.get('gen', True)
1041 success_response
= expr
.get('success-response', True)
1042 boxed
= expr
.get('boxed', False)
1043 allow_oob
= expr
.get('allow-oob', False)
1044 allow_preconfig
= expr
.get('allow-preconfig', False)
1045 ifcond
= expr
.get('if')
1046 features
= expr
.get('features', [])
1047 if isinstance(data
, OrderedDict
):
1048 data
= self
._make
_implicit
_object
_type
(
1049 name
, info
, ifcond
, 'arg', self
._make
_members
(data
, info
))
1050 if isinstance(rets
, list):
1051 assert len(rets
) == 1
1052 rets
= self
._make
_array
_type
(rets
[0], info
)
1053 self
._def
_entity
(QAPISchemaCommand(name
, info
, doc
, ifcond
, data
, rets
,
1054 gen
, success_response
,
1055 boxed
, allow_oob
, allow_preconfig
,
1056 self
._make
_features
(features
, info
)))
1058 def _def_event(self
, expr
, info
, doc
):
1059 name
= expr
['event']
1060 data
= expr
.get('data')
1061 boxed
= expr
.get('boxed', False)
1062 ifcond
= expr
.get('if')
1063 if isinstance(data
, OrderedDict
):
1064 data
= self
._make
_implicit
_object
_type
(
1065 name
, info
, ifcond
, 'arg', self
._make
_members
(data
, info
))
1066 self
._def
_entity
(QAPISchemaEvent(name
, info
, doc
, ifcond
, data
, boxed
))
1068 def _def_exprs(self
, exprs
):
1069 for expr_elem
in exprs
:
1070 expr
= expr_elem
['expr']
1071 info
= expr_elem
['info']
1072 doc
= expr_elem
.get('doc')
1074 self
._def
_enum
_type
(expr
, info
, doc
)
1075 elif 'struct' in expr
:
1076 self
._def
_struct
_type
(expr
, info
, doc
)
1077 elif 'union' in expr
:
1078 self
._def
_union
_type
(expr
, info
, doc
)
1079 elif 'alternate' in expr
:
1080 self
._def
_alternate
_type
(expr
, info
, doc
)
1081 elif 'command' in expr
:
1082 self
._def
_command
(expr
, info
, doc
)
1083 elif 'event' in expr
:
1084 self
._def
_event
(expr
, info
, doc
)
1085 elif 'include' in expr
:
1086 self
._def
_include
(expr
, info
, doc
)
1091 for ent
in self
._entity
_list
:
1095 for ent
in self
._entity
_list
:
1096 ent
.set_module(self
)
1098 def visit(self
, visitor
):
1099 visitor
.visit_begin(self
)
1100 for mod
in self
._module
_dict
.values():