4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013-2018 Red Hat Inc.
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Markus Armbruster <armbru@redhat.com>
11 # This work is licensed under the terms of the GNU GPL, version 2.
12 # See the COPYING file in the top-level directory.
14 from __future__
import print_function
15 from contextlib
import contextmanager
21 from collections
import OrderedDict
24 'null': 'QTYPE_QNULL',
25 'str': 'QTYPE_QSTRING',
27 'number': 'QTYPE_QNUM',
28 'bool': 'QTYPE_QBOOL',
30 'int16': 'QTYPE_QNUM',
31 'int32': 'QTYPE_QNUM',
32 'int64': 'QTYPE_QNUM',
33 'uint8': 'QTYPE_QNUM',
34 'uint16': 'QTYPE_QNUM',
35 'uint32': 'QTYPE_QNUM',
36 'uint64': 'QTYPE_QNUM',
38 'any': None, # any QType possible, actually
39 'QType': 'QTYPE_QSTRING',
42 # Are documentation comments required?
45 # Whitelist of commands allowed to return a non-dictionary
46 returns_whitelist
= []
48 # Whitelist of entities allowed to violate case conventions
49 name_case_whitelist
= []
57 # Parsing the schema into expressions
61 def error_path(parent
):
64 res
= ('In file included from %s:%d:\n' % (parent
['file'],
65 parent
['line'])) + res
66 parent
= parent
['parent']
70 class QAPIError(Exception):
71 def __init__(self
, fname
, line
, col
, incl_info
, msg
):
72 Exception.__init
__(self
)
80 loc
= '%s:%d' % (self
.fname
, self
.line
)
81 if self
.col
is not None:
82 loc
+= ':%s' % self
.col
83 return error_path(self
.info
) + '%s: %s' % (loc
, self
.msg
)
86 class QAPIParseError(QAPIError
):
87 def __init__(self
, parser
, msg
):
89 for ch
in parser
.src
[parser
.line_pos
:parser
.pos
]:
91 col
= (col
+ 7) % 8 + 1
94 QAPIError
.__init
__(self
, parser
.fname
, parser
.line
, col
,
95 parser
.incl_info
, msg
)
98 class QAPISemError(QAPIError
):
99 def __init__(self
, info
, msg
):
100 QAPIError
.__init
__(self
, info
['file'], info
['line'], None,
104 class QAPIDoc(object):
105 class Section(object):
106 def __init__(self
, name
=None):
107 # optional section name (argument/member or section name)
109 # the list of lines for this section
112 def append(self
, line
):
113 self
.text
+= line
.rstrip() + '\n'
115 class ArgSection(Section
):
116 def __init__(self
, name
):
117 QAPIDoc
.Section
.__init
__(self
, name
)
120 def connect(self
, member
):
123 def __init__(self
, parser
, info
):
124 # self._parser is used to report errors with QAPIParseError. The
125 # resulting error position depends on the state of the parser.
126 # It happens to be the beginning of the comment. More or less
127 # servicable, but action at a distance.
128 self
._parser
= parser
131 self
.body
= QAPIDoc
.Section()
132 # dict mapping parameter name to ArgSection
133 self
.args
= OrderedDict()
136 # the current section
137 self
._section
= self
.body
139 def has_section(self
, name
):
140 """Return True if we have a section with this name."""
141 for i
in self
.sections
:
146 def append(self
, line
):
147 """Parse a comment line and add it to the documentation."""
150 self
._append
_freeform
(line
)
154 raise QAPIParseError(self
._parser
, "Missing space after #")
157 # FIXME not nice: things like '# @foo:' and '# @foo: ' aren't
158 # recognized, and get silently treated as ordinary text
160 self
._append
_symbol
_line
(line
)
161 elif not self
.body
.text
and line
.startswith('@'):
162 if not line
.endswith(':'):
163 raise QAPIParseError(self
._parser
, "Line should end with :")
164 self
.symbol
= line
[1:-1]
165 # FIXME invalid names other than the empty string aren't flagged
167 raise QAPIParseError(self
._parser
, "Invalid name")
169 self
._append
_freeform
(line
)
171 def end_comment(self
):
174 def _append_symbol_line(self
, line
):
175 name
= line
.split(' ', 1)[0]
177 if name
.startswith('@') and name
.endswith(':'):
178 line
= line
[len(name
)+1:]
179 self
._start
_args
_section
(name
[1:-1])
180 elif name
in ('Returns:', 'Since:',
181 # those are often singular or plural
183 'Example:', 'Examples:',
185 line
= line
[len(name
)+1:]
186 self
._start
_section
(name
[:-1])
188 self
._append
_freeform
(line
)
190 def _start_args_section(self
, name
):
191 # FIXME invalid names other than the empty string aren't flagged
193 raise QAPIParseError(self
._parser
, "Invalid parameter name")
194 if name
in self
.args
:
195 raise QAPIParseError(self
._parser
,
196 "'%s' parameter name duplicated" % name
)
198 raise QAPIParseError(self
._parser
,
199 "'@%s:' can't follow '%s' section"
200 % (name
, self
.sections
[0].name
))
202 self
._section
= QAPIDoc
.ArgSection(name
)
203 self
.args
[name
] = self
._section
205 def _start_section(self
, name
=None):
206 if name
in ('Returns', 'Since') and self
.has_section(name
):
207 raise QAPIParseError(self
._parser
,
208 "Duplicated '%s' section" % name
)
210 self
._section
= QAPIDoc
.Section(name
)
211 self
.sections
.append(self
._section
)
213 def _end_section(self
):
215 text
= self
._section
.text
= self
._section
.text
.strip()
216 if self
._section
.name
and (not text
or text
.isspace()):
217 raise QAPIParseError(self
._parser
, "Empty doc section '%s'"
218 % self
._section
.name
)
221 def _append_freeform(self
, line
):
222 in_arg
= isinstance(self
._section
, QAPIDoc
.ArgSection
)
223 if (in_arg
and self
._section
.text
.endswith('\n\n')
224 and line
and not line
[0].isspace()):
225 self
._start
_section
()
226 if (in_arg
or not self
._section
.name
227 or not self
._section
.name
.startswith('Example')):
229 match
= re
.match(r
'(@\S+:)', line
)
231 raise QAPIParseError(self
._parser
,
232 "'%s' not allowed in free-form documentation"
234 self
._section
.append(line
)
236 def connect_member(self
, member
):
237 if member
.name
not in self
.args
:
238 # Undocumented TODO outlaw
239 self
.args
[member
.name
] = QAPIDoc
.ArgSection(member
.name
)
240 self
.args
[member
.name
].connect(member
)
242 def check_expr(self
, expr
):
243 if self
.has_section('Returns') and 'command' not in expr
:
244 raise QAPISemError(self
.info
,
245 "'Returns:' is only valid for commands")
248 bogus
= [name
for name
, section
in self
.args
.items()
249 if not section
.member
]
253 "The following documented members are not in "
254 "the declaration: %s" % ", ".join(bogus
))
257 class QAPISchemaParser(object):
259 def __init__(self
, fp
, previously_included
=[], incl_info
=None):
261 previously_included
.append(os
.path
.abspath(fp
.name
))
262 self
.incl_info
= incl_info
264 if self
.src
== '' or self
.src
[-1] != '\n':
274 while self
.tok
is not None:
275 info
= {'file': self
.fname
, 'line': self
.line
,
276 'parent': self
.incl_info
}
278 self
.reject_expr_doc(cur_doc
)
279 cur_doc
= self
.get_doc(info
)
280 self
.docs
.append(cur_doc
)
283 expr
= self
.get_expr(False)
284 if 'include' in expr
:
285 self
.reject_expr_doc(cur_doc
)
287 raise QAPISemError(info
, "Invalid 'include' directive")
288 include
= expr
['include']
289 if not isinstance(include
, str):
290 raise QAPISemError(info
,
291 "Value of 'include' must be a string")
292 incl_fname
= os
.path
.join(os
.path
.dirname(self
.fname
),
294 self
.exprs
.append({'expr': {'include': incl_fname
},
296 exprs_include
= self
._include
(include
, info
, incl_fname
,
299 self
.exprs
.extend(exprs_include
.exprs
)
300 self
.docs
.extend(exprs_include
.docs
)
301 elif "pragma" in expr
:
302 self
.reject_expr_doc(cur_doc
)
304 raise QAPISemError(info
, "Invalid 'pragma' directive")
305 pragma
= expr
['pragma']
306 if not isinstance(pragma
, dict):
308 info
, "Value of 'pragma' must be a dictionary")
309 for name
, value
in pragma
.items():
310 self
._pragma
(name
, value
, info
)
312 expr_elem
= {'expr': expr
,
315 if not cur_doc
.symbol
:
317 cur_doc
.info
, "Expression documentation required")
318 expr_elem
['doc'] = cur_doc
319 self
.exprs
.append(expr_elem
)
321 self
.reject_expr_doc(cur_doc
)
324 def reject_expr_doc(doc
):
325 if doc
and doc
.symbol
:
328 "Documentation for '%s' is not followed by the definition"
331 def _include(self
, include
, info
, incl_fname
, previously_included
):
332 incl_abs_fname
= os
.path
.abspath(incl_fname
)
333 # catch inclusion cycle
336 if incl_abs_fname
== os
.path
.abspath(inf
['file']):
337 raise QAPISemError(info
, "Inclusion loop for %s" % include
)
340 # skip multiple include of the same file
341 if incl_abs_fname
in previously_included
:
345 if sys
.version_info
[0] >= 3:
346 fobj
= open(incl_fname
, 'r', encoding
='utf-8')
348 fobj
= open(incl_fname
, 'r')
350 raise QAPISemError(info
, '%s: %s' % (e
.strerror
, incl_fname
))
351 return QAPISchemaParser(fobj
, previously_included
, info
)
353 def _pragma(self
, name
, value
, info
):
354 global doc_required
, returns_whitelist
, name_case_whitelist
355 if name
== 'doc-required':
356 if not isinstance(value
, bool):
357 raise QAPISemError(info
,
358 "Pragma 'doc-required' must be boolean")
360 elif name
== 'returns-whitelist':
361 if (not isinstance(value
, list)
362 or any([not isinstance(elt
, str) for elt
in value
])):
363 raise QAPISemError(info
,
364 "Pragma returns-whitelist must be"
365 " a list of strings")
366 returns_whitelist
= value
367 elif name
== 'name-case-whitelist':
368 if (not isinstance(value
, list)
369 or any([not isinstance(elt
, str) for elt
in value
])):
370 raise QAPISemError(info
,
371 "Pragma name-case-whitelist must be"
372 " a list of strings")
373 name_case_whitelist
= value
375 raise QAPISemError(info
, "Unknown pragma '%s'" % name
)
377 def accept(self
, skip_comment
=True):
379 self
.tok
= self
.src
[self
.cursor
]
380 self
.pos
= self
.cursor
385 if self
.src
[self
.cursor
] == '#':
386 # Start of doc comment
388 self
.cursor
= self
.src
.find('\n', self
.cursor
)
390 self
.val
= self
.src
[self
.pos
:self
.cursor
]
392 elif self
.tok
in '{}:,[]':
394 elif self
.tok
== "'":
398 ch
= self
.src
[self
.cursor
]
401 raise QAPIParseError(self
, 'Missing terminating "\'"')
415 for _
in range(0, 4):
416 ch
= self
.src
[self
.cursor
]
418 if ch
not in '0123456789abcdefABCDEF':
419 raise QAPIParseError(self
,
420 '\\u escape needs 4 '
422 value
= (value
<< 4) + int(ch
, 16)
423 # If Python 2 and 3 didn't disagree so much on
424 # how to handle Unicode, then we could allow
425 # Unicode string defaults. But most of QAPI is
426 # ASCII-only, so we aren't losing much for now.
427 if not value
or value
> 0x7f:
428 raise QAPIParseError(self
,
429 'For now, \\u escape '
430 'only supports non-zero '
431 'values up to \\u007f')
436 raise QAPIParseError(self
,
437 "Unknown escape \\%s" % ch
)
446 elif self
.src
.startswith('true', self
.pos
):
450 elif self
.src
.startswith('false', self
.pos
):
454 elif self
.src
.startswith('null', self
.pos
):
458 elif self
.tok
== '\n':
459 if self
.cursor
== len(self
.src
):
463 self
.line_pos
= self
.cursor
464 elif not self
.tok
.isspace():
465 raise QAPIParseError(self
, 'Stray "%s"' % self
.tok
)
467 def get_members(self
):
473 raise QAPIParseError(self
, 'Expected string or "}"')
478 raise QAPIParseError(self
, 'Expected ":"')
481 raise QAPIParseError(self
, 'Duplicate key "%s"' % key
)
482 expr
[key
] = self
.get_expr(True)
487 raise QAPIParseError(self
, 'Expected "," or "}"')
490 raise QAPIParseError(self
, 'Expected string')
492 def get_values(self
):
497 if self
.tok
not in "{['tfn":
498 raise QAPIParseError(self
, 'Expected "{", "[", "]", string, '
501 expr
.append(self
.get_expr(True))
506 raise QAPIParseError(self
, 'Expected "," or "]"')
509 def get_expr(self
, nested
):
510 if self
.tok
!= '{' and not nested
:
511 raise QAPIParseError(self
, 'Expected "{"')
514 expr
= self
.get_members()
515 elif self
.tok
== '[':
517 expr
= self
.get_values()
518 elif self
.tok
in "'tfn":
522 raise QAPIParseError(self
, 'Expected "{", "[", string, '
526 def get_doc(self
, info
):
528 raise QAPIParseError(self
, "Junk after '##' at start of "
529 "documentation comment")
531 doc
= QAPIDoc(self
, info
)
533 while self
.tok
== '#':
534 if self
.val
.startswith('##'):
537 raise QAPIParseError(self
, "Junk after '##' at end of "
538 "documentation comment")
546 raise QAPIParseError(self
, "Documentation comment must end with '##'")
550 # Semantic analysis of schema expressions
551 # TODO fold into QAPISchema
552 # TODO catching name collisions in generated code would be nice
556 def find_base_members(base
):
557 if isinstance(base
, dict):
559 base_struct_define
= struct_types
.get(base
)
560 if not base_struct_define
:
562 return base_struct_define
['data']
565 # Return the qtype of an alternate branch, or None on error.
566 def find_alternate_member_qtype(qapi_type
):
567 if qapi_type
in builtin_types
:
568 return builtin_types
[qapi_type
]
569 elif qapi_type
in struct_types
:
571 elif qapi_type
in enum_types
:
572 return 'QTYPE_QSTRING'
573 elif qapi_type
in union_types
:
578 # Return the discriminator enum define if discriminator is specified as an
579 # enum type, otherwise return None.
580 def discriminator_find_enum_define(expr
):
581 base
= expr
.get('base')
582 discriminator
= expr
.get('discriminator')
584 if not (discriminator
and base
):
587 base_members
= find_base_members(base
)
591 discriminator_value
= base_members
.get(discriminator
)
592 if not discriminator_value
:
595 return enum_types
.get(discriminator_value
['type'])
598 # Names must be letters, numbers, -, and _. They must start with letter,
599 # except for downstream extensions which must start with __RFQDN_.
600 # Dots are only valid in the downstream extension prefix.
601 valid_name
= re
.compile(r
'^(__[a-zA-Z0-9.-]+_)?'
602 '[a-zA-Z][a-zA-Z0-9_-]*$')
605 def check_name(info
, source
, name
, allow_optional
=False,
610 if not isinstance(name
, str):
611 raise QAPISemError(info
, "%s requires a string name" % source
)
612 if name
.startswith('*'):
613 membername
= name
[1:]
614 if not allow_optional
:
615 raise QAPISemError(info
, "%s does not allow optional name '%s'"
617 # Enum members can start with a digit, because the generated C
618 # code always prefixes it with the enum name
619 if enum_member
and membername
[0].isdigit():
620 membername
= 'D' + membername
621 # Reserve the entire 'q_' namespace for c_name(), and for 'q_empty'
622 # and 'q_obj_*' implicit type names.
623 if not valid_name
.match(membername
) or \
624 c_name(membername
, False).startswith('q_'):
625 raise QAPISemError(info
, "%s uses invalid name '%s'" % (source
, name
))
628 def add_name(name
, info
, meta
, implicit
=False):
630 check_name(info
, "'%s'" % meta
, name
)
631 # FIXME should reject names that differ only in '_' vs. '.'
632 # vs. '-', because they're liable to clash in generated C.
633 if name
in all_names
:
634 raise QAPISemError(info
, "%s '%s' is already defined"
635 % (all_names
[name
], name
))
636 if not implicit
and (name
.endswith('Kind') or name
.endswith('List')):
637 raise QAPISemError(info
, "%s '%s' should not end in '%s'"
638 % (meta
, name
, name
[-4:]))
639 all_names
[name
] = meta
642 def check_if(expr
, info
):
644 def check_if_str(ifcond
, info
):
645 if not isinstance(ifcond
, str):
647 info
, "'if' condition must be a string or a list of strings")
649 raise QAPISemError(info
, "'if' condition '' makes no sense")
651 ifcond
= expr
.get('if')
654 if isinstance(ifcond
, list):
656 raise QAPISemError(info
, "'if' condition [] is useless")
658 check_if_str(elt
, info
)
660 check_if_str(ifcond
, info
)
663 def check_type(info
, source
, value
, allow_array
=False,
664 allow_dict
=False, allow_optional
=False,
671 # Check if array type for value is okay
672 if isinstance(value
, list):
674 raise QAPISemError(info
, "%s cannot be an array" % source
)
675 if len(value
) != 1 or not isinstance(value
[0], str):
676 raise QAPISemError(info
,
677 "%s: array type must contain single type name" %
681 # Check if type name for value is okay
682 if isinstance(value
, str):
683 if value
not in all_names
:
684 raise QAPISemError(info
, "%s uses unknown type '%s'"
686 if not all_names
[value
] in allow_metas
:
687 raise QAPISemError(info
, "%s cannot use %s type '%s'" %
688 (source
, all_names
[value
], value
))
692 raise QAPISemError(info
, "%s should be a type name" % source
)
694 if not isinstance(value
, OrderedDict
):
695 raise QAPISemError(info
,
696 "%s should be a dictionary or type name" % source
)
698 # value is a dictionary, check that each member is okay
699 for (key
, arg
) in value
.items():
700 check_name(info
, "Member of %s" % source
, key
,
701 allow_optional
=allow_optional
)
702 if c_name(key
, False) == 'u' or c_name(key
, False).startswith('has_'):
703 raise QAPISemError(info
, "Member of %s uses reserved name '%s'"
705 # Todo: allow dictionaries to represent default values of
706 # an optional argument.
707 check_known_keys(info
, "member '%s' of %s" % (key
, source
),
708 arg
, ['type'], ['if'])
709 check_type(info
, "Member '%s' of %s" % (key
, source
),
710 arg
['type'], allow_array
=True,
711 allow_metas
=['built-in', 'union', 'alternate', 'struct',
715 def check_command(expr
, info
):
716 name
= expr
['command']
717 boxed
= expr
.get('boxed', False)
719 args_meta
= ['struct']
721 args_meta
+= ['union', 'alternate']
722 check_type(info
, "'data' for command '%s'" % name
,
723 expr
.get('data'), allow_dict
=not boxed
, allow_optional
=True,
724 allow_metas
=args_meta
)
725 returns_meta
= ['union', 'struct']
726 if name
in returns_whitelist
:
727 returns_meta
+= ['built-in', 'alternate', 'enum']
728 check_type(info
, "'returns' for command '%s'" % name
,
729 expr
.get('returns'), allow_array
=True,
730 allow_optional
=True, allow_metas
=returns_meta
)
733 def check_event(expr
, info
):
735 boxed
= expr
.get('boxed', False)
739 meta
+= ['union', 'alternate']
740 check_type(info
, "'data' for event '%s'" % name
,
741 expr
.get('data'), allow_dict
=not boxed
, allow_optional
=True,
745 def enum_get_names(expr
):
746 return [e
['name'] for e
in expr
['data']]
749 def check_union(expr
, info
):
751 base
= expr
.get('base')
752 discriminator
= expr
.get('discriminator')
753 members
= expr
['data']
755 # Two types of unions, determined by discriminator.
757 # With no discriminator it is a simple union.
758 if discriminator
is None:
760 allow_metas
= ['built-in', 'union', 'alternate', 'struct', 'enum']
762 raise QAPISemError(info
, "Simple union '%s' must not have a base" %
765 # Else, it's a flat union.
767 # The object must have a string or dictionary 'base'.
768 check_type(info
, "'base' for union '%s'" % name
,
769 base
, allow_dict
=True, allow_optional
=True,
770 allow_metas
=['struct'])
772 raise QAPISemError(info
, "Flat union '%s' must have a base"
774 base_members
= find_base_members(base
)
775 assert base_members
is not None
777 # The value of member 'discriminator' must name a non-optional
778 # member of the base struct.
779 check_name(info
, "Discriminator of flat union '%s'" % name
,
781 discriminator_value
= base_members
.get(discriminator
)
782 if not discriminator_value
:
783 raise QAPISemError(info
,
784 "Discriminator '%s' is not a member of base "
786 % (discriminator
, base
))
787 if discriminator_value
.get('if'):
788 raise QAPISemError(info
, 'The discriminator %s.%s for union %s '
789 'must not be conditional' %
790 (base
, discriminator
, name
))
791 enum_define
= enum_types
.get(discriminator_value
['type'])
792 allow_metas
= ['struct']
793 # Do not allow string discriminator
795 raise QAPISemError(info
,
796 "Discriminator '%s' must be of enumeration "
797 "type" % discriminator
)
799 # Check every branch; don't allow an empty union
800 if len(members
) == 0:
801 raise QAPISemError(info
, "Union '%s' cannot have empty 'data'" % name
)
802 for (key
, value
) in members
.items():
803 check_name(info
, "Member of union '%s'" % name
, key
)
805 check_known_keys(info
, "member '%s' of union '%s'" % (key
, name
),
806 value
, ['type'], ['if'])
807 # Each value must name a known type
808 check_type(info
, "Member '%s' of union '%s'" % (key
, name
),
810 allow_array
=not base
, allow_metas
=allow_metas
)
812 # If the discriminator names an enum type, then all members
813 # of 'data' must also be members of the enum type.
815 if key
not in enum_get_names(enum_define
):
816 raise QAPISemError(info
,
817 "Discriminator value '%s' is not found in "
819 % (key
, enum_define
['enum']))
822 def check_alternate(expr
, info
):
823 name
= expr
['alternate']
824 members
= expr
['data']
827 # Check every branch; require at least two branches
829 raise QAPISemError(info
,
830 "Alternate '%s' should have at least two branches "
832 for (key
, value
) in members
.items():
833 check_name(info
, "Member of alternate '%s'" % name
, key
)
834 check_known_keys(info
,
835 "member '%s' of alternate '%s'" % (key
, name
),
836 value
, ['type'], ['if'])
839 # Ensure alternates have no type conflicts.
840 check_type(info
, "Member '%s' of alternate '%s'" % (key
, name
), typ
,
841 allow_metas
=['built-in', 'union', 'struct', 'enum'])
842 qtype
= find_alternate_member_qtype(typ
)
844 raise QAPISemError(info
, "Alternate '%s' member '%s' cannot use "
845 "type '%s'" % (name
, key
, typ
))
846 conflicting
= set([qtype
])
847 if qtype
== 'QTYPE_QSTRING':
848 enum_expr
= enum_types
.get(typ
)
850 for v
in enum_get_names(enum_expr
):
851 if v
in ['on', 'off']:
852 conflicting
.add('QTYPE_QBOOL')
853 if re
.match(r
'[-+0-9.]', v
): # lazy, could be tightened
854 conflicting
.add('QTYPE_QNUM')
856 conflicting
.add('QTYPE_QNUM')
857 conflicting
.add('QTYPE_QBOOL')
858 for qt
in conflicting
:
860 raise QAPISemError(info
, "Alternate '%s' member '%s' can't "
861 "be distinguished from member '%s'"
862 % (name
, key
, types_seen
[qt
]))
866 def check_enum(expr
, info
):
868 members
= expr
['data']
869 prefix
= expr
.get('prefix')
871 if not isinstance(members
, list):
872 raise QAPISemError(info
,
873 "Enum '%s' requires an array for 'data'" % name
)
874 if prefix
is not None and not isinstance(prefix
, str):
875 raise QAPISemError(info
,
876 "Enum '%s' requires a string for 'prefix'" % name
)
878 for member
in members
:
879 source
= "dictionary member of enum '%s'" % name
880 check_known_keys(info
, source
, member
, ['name'], ['if'])
881 check_if(member
, info
)
882 check_name(info
, "Member of enum '%s'" % name
, member
['name'],
886 def check_struct(expr
, info
):
887 name
= expr
['struct']
888 members
= expr
['data']
890 check_type(info
, "'data' for struct '%s'" % name
, members
,
891 allow_dict
=True, allow_optional
=True)
892 check_type(info
, "'base' for struct '%s'" % name
, expr
.get('base'),
893 allow_metas
=['struct'])
896 def check_known_keys(info
, source
, keys
, required
, optional
):
899 return ', '.join("'" + e
+ "'" for e
in sorted(elems
))
901 missing
= set(required
) - set(keys
)
903 raise QAPISemError(info
, "Key%s %s %s missing from %s"
904 % ('s' if len(missing
) > 1 else '', pprint(missing
),
905 'are' if len(missing
) > 1 else 'is', source
))
906 allowed
= set(required
+ optional
)
907 unknown
= set(keys
) - allowed
909 raise QAPISemError(info
, "Unknown key%s %s in %s\nValid keys are %s."
910 % ('s' if len(unknown
) > 1 else '', pprint(unknown
),
911 source
, pprint(allowed
)))
914 def check_keys(expr_elem
, meta
, required
, optional
=[]):
915 expr
= expr_elem
['expr']
916 info
= expr_elem
['info']
918 if not isinstance(name
, str):
919 raise QAPISemError(info
, "'%s' key must have a string value" % meta
)
920 required
= required
+ [meta
]
921 source
= "%s '%s'" % (meta
, name
)
922 check_known_keys(info
, source
, expr
.keys(), required
, optional
)
923 for (key
, value
) in expr
.items():
924 if key
in ['gen', 'success-response'] and value
is not False:
925 raise QAPISemError(info
,
926 "'%s' of %s '%s' should only use false value"
928 if (key
in ['boxed', 'allow-oob', 'allow-preconfig']
929 and value
is not True):
930 raise QAPISemError(info
,
931 "'%s' of %s '%s' should only use true value"
937 def normalize_enum(expr
):
938 if isinstance(expr
['data'], list):
939 expr
['data'] = [m
if isinstance(m
, dict) else {'name': m
}
940 for m
in expr
['data']]
943 def normalize_members(members
):
944 if isinstance(members
, OrderedDict
):
945 for key
, arg
in members
.items():
946 if isinstance(arg
, dict):
948 members
[key
] = {'type': arg
}
951 def check_exprs(exprs
):
954 # Populate name table with names of built-in types
955 for builtin
in builtin_types
.keys():
956 all_names
[builtin
] = 'built-in'
958 # Learn the types and check for valid expression keys
959 for expr_elem
in exprs
:
960 expr
= expr_elem
['expr']
961 info
= expr_elem
['info']
962 doc
= expr_elem
.get('doc')
964 if 'include' in expr
:
967 if not doc
and doc_required
:
968 raise QAPISemError(info
,
969 "Expression missing documentation comment")
973 check_keys(expr_elem
, 'enum', ['data'], ['if', 'prefix'])
975 enum_types
[expr
[meta
]] = expr
976 elif 'union' in expr
:
978 check_keys(expr_elem
, 'union', ['data'],
979 ['base', 'discriminator', 'if'])
980 normalize_members(expr
.get('base'))
981 normalize_members(expr
['data'])
982 union_types
[expr
[meta
]] = expr
983 elif 'alternate' in expr
:
985 check_keys(expr_elem
, 'alternate', ['data'], ['if'])
986 normalize_members(expr
['data'])
987 elif 'struct' in expr
:
989 check_keys(expr_elem
, 'struct', ['data'], ['base', 'if'])
990 normalize_members(expr
['data'])
991 struct_types
[expr
[meta
]] = expr
992 elif 'command' in expr
:
994 check_keys(expr_elem
, 'command', [],
995 ['data', 'returns', 'gen', 'success-response',
996 'boxed', 'allow-oob', 'allow-preconfig', 'if'])
997 normalize_members(expr
.get('data'))
998 elif 'event' in expr
:
1000 check_keys(expr_elem
, 'event', [], ['data', 'boxed', 'if'])
1001 normalize_members(expr
.get('data'))
1003 raise QAPISemError(expr_elem
['info'],
1004 "Expression is missing metatype")
1006 add_name(name
, info
, meta
)
1007 if doc
and doc
.symbol
!= name
:
1008 raise QAPISemError(info
, "Definition of '%s' follows documentation"
1009 " for '%s'" % (name
, doc
.symbol
))
1011 # Try again for hidden UnionKind enum
1012 for expr_elem
in exprs
:
1013 expr
= expr_elem
['expr']
1015 if 'include' in expr
:
1017 if 'union' in expr
and not discriminator_find_enum_define(expr
):
1018 name
= '%sKind' % expr
['union']
1019 elif 'alternate' in expr
:
1020 name
= '%sKind' % expr
['alternate']
1023 enum_types
[name
] = {'enum': name
}
1024 add_name(name
, info
, 'enum', implicit
=True)
1026 # Validate that exprs make sense
1027 for expr_elem
in exprs
:
1028 expr
= expr_elem
['expr']
1029 info
= expr_elem
['info']
1030 doc
= expr_elem
.get('doc')
1032 if 'include' in expr
:
1035 check_enum(expr
, info
)
1036 elif 'union' in expr
:
1037 check_union(expr
, info
)
1038 elif 'alternate' in expr
:
1039 check_alternate(expr
, info
)
1040 elif 'struct' in expr
:
1041 check_struct(expr
, info
)
1042 elif 'command' in expr
:
1043 check_command(expr
, info
)
1044 elif 'event' in expr
:
1045 check_event(expr
, info
)
1047 assert False, 'unexpected meta type'
1050 doc
.check_expr(expr
)
1056 # Schema compiler frontend
1059 def listify_cond(ifcond
):
1062 if not isinstance(ifcond
, list):
1067 class QAPISchemaEntity(object):
1068 def __init__(self
, name
, info
, doc
, ifcond
=None):
1069 assert name
is None or isinstance(name
, str)
1072 # For explicitly defined entities, info points to the (explicit)
1073 # definition. For builtins (and their arrays), info is None.
1074 # For implicitly defined entities, info points to a place that
1075 # triggered the implicit definition (there may be more than one
1079 self
._ifcond
= ifcond
# self.ifcond is set only after .check()
1082 return c_name(self
.name
)
1084 def check(self
, schema
):
1085 if isinstance(self
._ifcond
, QAPISchemaType
):
1086 # inherit the condition from a type
1089 self
.ifcond
= typ
.ifcond
1091 self
.ifcond
= listify_cond(self
._ifcond
)
1093 def is_implicit(self
):
1094 return not self
.info
1096 def visit(self
, visitor
):
1100 class QAPISchemaVisitor(object):
1101 def visit_begin(self
, schema
):
1104 def visit_end(self
):
1107 def visit_module(self
, fname
):
1110 def visit_needed(self
, entity
):
1111 # Default to visiting everything
1114 def visit_include(self
, fname
, info
):
1117 def visit_builtin_type(self
, name
, info
, json_type
):
1120 def visit_enum_type(self
, name
, info
, ifcond
, members
, prefix
):
1123 def visit_array_type(self
, name
, info
, ifcond
, element_type
):
1126 def visit_object_type(self
, name
, info
, ifcond
, base
, members
, variants
):
1129 def visit_object_type_flat(self
, name
, info
, ifcond
, members
, variants
):
1132 def visit_alternate_type(self
, name
, info
, ifcond
, variants
):
1135 def visit_command(self
, name
, info
, ifcond
, arg_type
, ret_type
, gen
,
1136 success_response
, boxed
, allow_oob
, allow_preconfig
):
1139 def visit_event(self
, name
, info
, ifcond
, arg_type
, boxed
):
1143 class QAPISchemaInclude(QAPISchemaEntity
):
1145 def __init__(self
, fname
, info
):
1146 QAPISchemaEntity
.__init
__(self
, None, info
, None)
1149 def visit(self
, visitor
):
1150 visitor
.visit_include(self
.fname
, self
.info
)
1153 class QAPISchemaType(QAPISchemaEntity
):
1154 # Return the C type for common use.
1155 # For the types we commonly box, this is a pointer type.
1159 # Return the C type to be used in a parameter list.
1160 def c_param_type(self
):
1161 return self
.c_type()
1163 # Return the C type to be used where we suppress boxing.
1164 def c_unboxed_type(self
):
1165 return self
.c_type()
1167 def json_type(self
):
1170 def alternate_qtype(self
):
1172 'null': 'QTYPE_QNULL',
1173 'string': 'QTYPE_QSTRING',
1174 'number': 'QTYPE_QNUM',
1175 'int': 'QTYPE_QNUM',
1176 'boolean': 'QTYPE_QBOOL',
1177 'object': 'QTYPE_QDICT'
1179 return json2qtype
.get(self
.json_type())
1182 if self
.is_implicit():
1187 class QAPISchemaBuiltinType(QAPISchemaType
):
1188 def __init__(self
, name
, json_type
, c_type
):
1189 QAPISchemaType
.__init
__(self
, name
, None, None)
1190 assert not c_type
or isinstance(c_type
, str)
1191 assert json_type
in ('string', 'number', 'int', 'boolean', 'null',
1193 self
._json
_type
_name
= json_type
1194 self
._c
_type
_name
= c_type
1200 return self
._c
_type
_name
1202 def c_param_type(self
):
1203 if self
.name
== 'str':
1204 return 'const ' + self
._c
_type
_name
1205 return self
._c
_type
_name
1207 def json_type(self
):
1208 return self
._json
_type
_name
1211 return self
.json_type()
1213 def visit(self
, visitor
):
1214 visitor
.visit_builtin_type(self
.name
, self
.info
, self
.json_type())
1217 class QAPISchemaEnumType(QAPISchemaType
):
1218 def __init__(self
, name
, info
, doc
, ifcond
, members
, prefix
):
1219 QAPISchemaType
.__init
__(self
, name
, info
, doc
, ifcond
)
1221 assert isinstance(m
, QAPISchemaMember
)
1223 assert prefix
is None or isinstance(prefix
, str)
1224 self
.members
= members
1225 self
.prefix
= prefix
1227 def check(self
, schema
):
1228 QAPISchemaType
.check(self
, schema
)
1230 for m
in self
.members
:
1231 m
.check_clash(self
.info
, seen
)
1233 self
.doc
.connect_member(m
)
1235 def is_implicit(self
):
1236 # See QAPISchema._make_implicit_enum_type() and ._def_predefineds()
1237 return self
.name
.endswith('Kind') or self
.name
== 'QType'
1240 return c_name(self
.name
)
1242 def member_names(self
):
1243 return [m
.name
for m
in self
.members
]
1245 def json_type(self
):
1248 def visit(self
, visitor
):
1249 visitor
.visit_enum_type(self
.name
, self
.info
, self
.ifcond
,
1250 self
.members
, self
.prefix
)
1253 class QAPISchemaArrayType(QAPISchemaType
):
1254 def __init__(self
, name
, info
, element_type
):
1255 QAPISchemaType
.__init
__(self
, name
, info
, None, None)
1256 assert isinstance(element_type
, str)
1257 self
._element
_type
_name
= element_type
1258 self
.element_type
= None
1260 def check(self
, schema
):
1261 QAPISchemaType
.check(self
, schema
)
1262 self
.element_type
= schema
.lookup_type(self
._element
_type
_name
)
1263 assert self
.element_type
1264 self
.element_type
.check(schema
)
1265 self
.ifcond
= self
.element_type
.ifcond
1267 def is_implicit(self
):
1271 return c_name(self
.name
) + pointer_suffix
1273 def json_type(self
):
1277 elt_doc_type
= self
.element_type
.doc_type()
1278 if not elt_doc_type
:
1280 return 'array of ' + elt_doc_type
1282 def visit(self
, visitor
):
1283 visitor
.visit_array_type(self
.name
, self
.info
, self
.ifcond
,
1287 class QAPISchemaObjectType(QAPISchemaType
):
1288 def __init__(self
, name
, info
, doc
, ifcond
,
1289 base
, local_members
, variants
):
1290 # struct has local_members, optional base, and no variants
1291 # flat union has base, variants, and no local_members
1292 # simple union has local_members, variants, and no base
1293 QAPISchemaType
.__init
__(self
, name
, info
, doc
, ifcond
)
1294 assert base
is None or isinstance(base
, str)
1295 for m
in local_members
:
1296 assert isinstance(m
, QAPISchemaObjectTypeMember
)
1298 if variants
is not None:
1299 assert isinstance(variants
, QAPISchemaObjectTypeVariants
)
1300 variants
.set_owner(name
)
1301 self
._base
_name
= base
1303 self
.local_members
= local_members
1304 self
.variants
= variants
1307 def check(self
, schema
):
1308 QAPISchemaType
.check(self
, schema
)
1309 if self
.members
is False: # check for cycles
1310 raise QAPISemError(self
.info
,
1311 "Object %s contains itself" % self
.name
)
1314 self
.members
= False # mark as being checked
1315 seen
= OrderedDict()
1317 self
.base
= schema
.lookup_type(self
._base
_name
)
1318 assert isinstance(self
.base
, QAPISchemaObjectType
)
1319 self
.base
.check(schema
)
1320 self
.base
.check_clash(self
.info
, seen
)
1321 for m
in self
.local_members
:
1323 m
.check_clash(self
.info
, seen
)
1325 self
.doc
.connect_member(m
)
1326 self
.members
= seen
.values()
1328 self
.variants
.check(schema
, seen
)
1329 assert self
.variants
.tag_member
in self
.members
1330 self
.variants
.check_clash(self
.info
, seen
)
1334 # Check that the members of this type do not cause duplicate JSON members,
1335 # and update seen to track the members seen so far. Report any errors
1336 # on behalf of info, which is not necessarily self.info
1337 def check_clash(self
, info
, seen
):
1338 assert not self
.variants
# not implemented
1339 for m
in self
.members
:
1340 m
.check_clash(info
, seen
)
1342 def is_implicit(self
):
1343 # See QAPISchema._make_implicit_object_type(), as well as
1344 # _def_predefineds()
1345 return self
.name
.startswith('q_')
1348 assert self
.members
is not None
1349 return not self
.members
and not self
.variants
1352 assert self
.name
!= 'q_empty'
1353 return QAPISchemaType
.c_name(self
)
1356 assert not self
.is_implicit()
1357 return c_name(self
.name
) + pointer_suffix
1359 def c_unboxed_type(self
):
1360 return c_name(self
.name
)
1362 def json_type(self
):
1365 def visit(self
, visitor
):
1366 visitor
.visit_object_type(self
.name
, self
.info
, self
.ifcond
,
1367 self
.base
, self
.local_members
, self
.variants
)
1368 visitor
.visit_object_type_flat(self
.name
, self
.info
, self
.ifcond
,
1369 self
.members
, self
.variants
)
1372 class QAPISchemaMember(object):
1375 def __init__(self
, name
, ifcond
=None):
1376 assert isinstance(name
, str)
1378 self
.ifcond
= listify_cond(ifcond
)
1381 def set_owner(self
, name
):
1382 assert not self
.owner
1385 def check_clash(self
, info
, seen
):
1386 cname
= c_name(self
.name
)
1387 if cname
.lower() != cname
and self
.owner
not in name_case_whitelist
:
1388 raise QAPISemError(info
,
1389 "%s should not use uppercase" % self
.describe())
1391 raise QAPISemError(info
, "%s collides with %s" %
1392 (self
.describe(), seen
[cname
].describe()))
1395 def _pretty_owner(self
):
1397 if owner
.startswith('q_obj_'):
1398 # See QAPISchema._make_implicit_object_type() - reverse the
1399 # mapping there to create a nice human-readable description
1401 if owner
.endswith('-arg'):
1402 return '(parameter of %s)' % owner
[:-4]
1403 elif owner
.endswith('-base'):
1404 return '(base of %s)' % owner
[:-5]
1406 assert owner
.endswith('-wrapper')
1407 # Unreachable and not implemented
1409 if owner
.endswith('Kind'):
1410 # See QAPISchema._make_implicit_enum_type()
1411 return '(branch of %s)' % owner
[:-4]
1412 return '(%s of %s)' % (self
.role
, owner
)
1415 return "'%s' %s" % (self
.name
, self
._pretty
_owner
())
1418 class QAPISchemaObjectTypeMember(QAPISchemaMember
):
1419 def __init__(self
, name
, typ
, optional
, ifcond
=None):
1420 QAPISchemaMember
.__init
__(self
, name
, ifcond
)
1421 assert isinstance(typ
, str)
1422 assert isinstance(optional
, bool)
1423 self
._type
_name
= typ
1425 self
.optional
= optional
1427 def check(self
, schema
):
1429 self
.type = schema
.lookup_type(self
._type
_name
)
1433 class QAPISchemaObjectTypeVariants(object):
1434 def __init__(self
, tag_name
, tag_member
, variants
):
1435 # Flat unions pass tag_name but not tag_member.
1436 # Simple unions and alternates pass tag_member but not tag_name.
1437 # After check(), tag_member is always set, and tag_name remains
1438 # a reliable witness of being used by a flat union.
1439 assert bool(tag_member
) != bool(tag_name
)
1440 assert (isinstance(tag_name
, str) or
1441 isinstance(tag_member
, QAPISchemaObjectTypeMember
))
1442 assert len(variants
) > 0
1444 assert isinstance(v
, QAPISchemaObjectTypeVariant
)
1445 self
._tag
_name
= tag_name
1446 self
.tag_member
= tag_member
1447 self
.variants
= variants
1449 def set_owner(self
, name
):
1450 for v
in self
.variants
:
1453 def check(self
, schema
, seen
):
1454 if not self
.tag_member
: # flat union
1455 self
.tag_member
= seen
[c_name(self
._tag
_name
)]
1456 assert self
._tag
_name
== self
.tag_member
.name
1457 assert isinstance(self
.tag_member
.type, QAPISchemaEnumType
)
1458 if self
._tag
_name
: # flat union
1459 # branches that are not explicitly covered get an empty type
1460 cases
= set([v
.name
for v
in self
.variants
])
1461 for m
in self
.tag_member
.type.members
:
1462 if m
.name
not in cases
:
1463 v
= QAPISchemaObjectTypeVariant(m
.name
, 'q_empty',
1465 v
.set_owner(self
.tag_member
.owner
)
1466 self
.variants
.append(v
)
1467 for v
in self
.variants
:
1469 # Union names must match enum values; alternate names are
1470 # checked separately. Use 'seen' to tell the two apart.
1472 assert v
.name
in self
.tag_member
.type.member_names()
1473 assert isinstance(v
.type, QAPISchemaObjectType
)
1474 v
.type.check(schema
)
1476 def check_clash(self
, info
, seen
):
1477 for v
in self
.variants
:
1478 # Reset seen map for each variant, since qapi names from one
1479 # branch do not affect another branch
1480 assert isinstance(v
.type, QAPISchemaObjectType
)
1481 v
.type.check_clash(info
, dict(seen
))
1484 class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember
):
1487 def __init__(self
, name
, typ
, ifcond
=None):
1488 QAPISchemaObjectTypeMember
.__init
__(self
, name
, typ
, False, ifcond
)
1491 class QAPISchemaAlternateType(QAPISchemaType
):
1492 def __init__(self
, name
, info
, doc
, ifcond
, variants
):
1493 QAPISchemaType
.__init
__(self
, name
, info
, doc
, ifcond
)
1494 assert isinstance(variants
, QAPISchemaObjectTypeVariants
)
1495 assert variants
.tag_member
1496 variants
.set_owner(name
)
1497 variants
.tag_member
.set_owner(self
.name
)
1498 self
.variants
= variants
1500 def check(self
, schema
):
1501 QAPISchemaType
.check(self
, schema
)
1502 self
.variants
.tag_member
.check(schema
)
1503 # Not calling self.variants.check_clash(), because there's nothing
1505 self
.variants
.check(schema
, {})
1506 # Alternate branch names have no relation to the tag enum values;
1507 # so we have to check for potential name collisions ourselves.
1509 for v
in self
.variants
.variants
:
1510 v
.check_clash(self
.info
, seen
)
1512 self
.doc
.connect_member(v
)
1517 return c_name(self
.name
) + pointer_suffix
1519 def json_type(self
):
1522 def visit(self
, visitor
):
1523 visitor
.visit_alternate_type(self
.name
, self
.info
, self
.ifcond
,
1530 class QAPISchemaCommand(QAPISchemaEntity
):
1531 def __init__(self
, name
, info
, doc
, ifcond
, arg_type
, ret_type
,
1532 gen
, success_response
, boxed
, allow_oob
, allow_preconfig
):
1533 QAPISchemaEntity
.__init
__(self
, name
, info
, doc
, ifcond
)
1534 assert not arg_type
or isinstance(arg_type
, str)
1535 assert not ret_type
or isinstance(ret_type
, str)
1536 self
._arg
_type
_name
= arg_type
1537 self
.arg_type
= None
1538 self
._ret
_type
_name
= ret_type
1539 self
.ret_type
= None
1541 self
.success_response
= success_response
1543 self
.allow_oob
= allow_oob
1544 self
.allow_preconfig
= allow_preconfig
1546 def check(self
, schema
):
1547 QAPISchemaEntity
.check(self
, schema
)
1548 if self
._arg
_type
_name
:
1549 self
.arg_type
= schema
.lookup_type(self
._arg
_type
_name
)
1550 assert (isinstance(self
.arg_type
, QAPISchemaObjectType
) or
1551 isinstance(self
.arg_type
, QAPISchemaAlternateType
))
1552 self
.arg_type
.check(schema
)
1554 if self
.arg_type
.is_empty():
1555 raise QAPISemError(self
.info
,
1556 "Cannot use 'boxed' with empty type")
1558 assert not isinstance(self
.arg_type
, QAPISchemaAlternateType
)
1559 assert not self
.arg_type
.variants
1561 raise QAPISemError(self
.info
, "Use of 'boxed' requires 'data'")
1562 if self
._ret
_type
_name
:
1563 self
.ret_type
= schema
.lookup_type(self
._ret
_type
_name
)
1564 assert isinstance(self
.ret_type
, QAPISchemaType
)
1566 def visit(self
, visitor
):
1567 visitor
.visit_command(self
.name
, self
.info
, self
.ifcond
,
1568 self
.arg_type
, self
.ret_type
,
1569 self
.gen
, self
.success_response
,
1570 self
.boxed
, self
.allow_oob
,
1571 self
.allow_preconfig
)
1574 class QAPISchemaEvent(QAPISchemaEntity
):
1575 def __init__(self
, name
, info
, doc
, ifcond
, arg_type
, boxed
):
1576 QAPISchemaEntity
.__init
__(self
, name
, info
, doc
, ifcond
)
1577 assert not arg_type
or isinstance(arg_type
, str)
1578 self
._arg
_type
_name
= arg_type
1579 self
.arg_type
= None
1582 def check(self
, schema
):
1583 QAPISchemaEntity
.check(self
, schema
)
1584 if self
._arg
_type
_name
:
1585 self
.arg_type
= schema
.lookup_type(self
._arg
_type
_name
)
1586 assert (isinstance(self
.arg_type
, QAPISchemaObjectType
) or
1587 isinstance(self
.arg_type
, QAPISchemaAlternateType
))
1588 self
.arg_type
.check(schema
)
1590 if self
.arg_type
.is_empty():
1591 raise QAPISemError(self
.info
,
1592 "Cannot use 'boxed' with empty type")
1594 assert not isinstance(self
.arg_type
, QAPISchemaAlternateType
)
1595 assert not self
.arg_type
.variants
1597 raise QAPISemError(self
.info
, "Use of 'boxed' requires 'data'")
1599 def visit(self
, visitor
):
1600 visitor
.visit_event(self
.name
, self
.info
, self
.ifcond
,
1601 self
.arg_type
, self
.boxed
)
1604 class QAPISchema(object):
1605 def __init__(self
, fname
):
1607 if sys
.version_info
[0] >= 3:
1608 f
= open(fname
, 'r', encoding
='utf-8')
1610 f
= open(fname
, 'r')
1611 parser
= QAPISchemaParser(f
)
1612 exprs
= check_exprs(parser
.exprs
)
1613 self
.docs
= parser
.docs
1614 self
._entity
_list
= []
1615 self
._entity
_dict
= {}
1616 self
._predefining
= True
1617 self
._def
_predefineds
()
1618 self
._predefining
= False
1619 self
._def
_exprs
(exprs
)
1622 def _def_entity(self
, ent
):
1623 # Only the predefined types are allowed to not have info
1624 assert ent
.info
or self
._predefining
1625 assert ent
.name
is None or ent
.name
not in self
._entity
_dict
1626 self
._entity
_list
.append(ent
)
1627 if ent
.name
is not None:
1628 self
._entity
_dict
[ent
.name
] = ent
1630 ent
.module
= os
.path
.relpath(ent
.info
['file'],
1631 os
.path
.dirname(self
._fname
))
1633 def lookup_entity(self
, name
, typ
=None):
1634 ent
= self
._entity
_dict
.get(name
)
1635 if typ
and not isinstance(ent
, typ
):
1639 def lookup_type(self
, name
):
1640 return self
.lookup_entity(name
, QAPISchemaType
)
1642 def _def_include(self
, expr
, info
, doc
):
1643 include
= expr
['include']
1646 while main_info
['parent']:
1647 main_info
= main_info
['parent']
1648 fname
= os
.path
.relpath(include
, os
.path
.dirname(main_info
['file']))
1649 self
._def
_entity
(QAPISchemaInclude(fname
, info
))
1651 def _def_builtin_type(self
, name
, json_type
, c_type
):
1652 self
._def
_entity
(QAPISchemaBuiltinType(name
, json_type
, c_type
))
1653 # Instantiating only the arrays that are actually used would
1654 # be nice, but we can't as long as their generated code
1655 # (qapi-builtin-types.[ch]) may be shared by some other
1657 self
._make
_array
_type
(name
, None)
1659 def _def_predefineds(self
):
1660 for t
in [('str', 'string', 'char' + pointer_suffix
),
1661 ('number', 'number', 'double'),
1662 ('int', 'int', 'int64_t'),
1663 ('int8', 'int', 'int8_t'),
1664 ('int16', 'int', 'int16_t'),
1665 ('int32', 'int', 'int32_t'),
1666 ('int64', 'int', 'int64_t'),
1667 ('uint8', 'int', 'uint8_t'),
1668 ('uint16', 'int', 'uint16_t'),
1669 ('uint32', 'int', 'uint32_t'),
1670 ('uint64', 'int', 'uint64_t'),
1671 ('size', 'int', 'uint64_t'),
1672 ('bool', 'boolean', 'bool'),
1673 ('any', 'value', 'QObject' + pointer_suffix
),
1674 ('null', 'null', 'QNull' + pointer_suffix
)]:
1675 self
._def
_builtin
_type
(*t
)
1676 self
.the_empty_object_type
= QAPISchemaObjectType(
1677 'q_empty', None, None, None, None, [], None)
1678 self
._def
_entity
(self
.the_empty_object_type
)
1680 qtypes
= ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist',
1682 qtype_values
= self
._make
_enum
_members
([{'name': n
} for n
in qtypes
])
1684 self
._def
_entity
(QAPISchemaEnumType('QType', None, None, None,
1685 qtype_values
, 'QTYPE'))
1687 def _make_enum_members(self
, values
):
1688 return [QAPISchemaMember(v
['name'], v
.get('if')) for v
in values
]
1690 def _make_implicit_enum_type(self
, name
, info
, ifcond
, values
):
1691 # See also QAPISchemaObjectTypeMember._pretty_owner()
1692 name
= name
+ 'Kind' # Use namespace reserved by add_name()
1693 self
._def
_entity
(QAPISchemaEnumType(
1694 name
, info
, None, ifcond
, self
._make
_enum
_members
(values
), None))
1697 def _make_array_type(self
, element_type
, info
):
1698 name
= element_type
+ 'List' # Use namespace reserved by add_name()
1699 if not self
.lookup_type(name
):
1700 self
._def
_entity
(QAPISchemaArrayType(name
, info
, element_type
))
1703 def _make_implicit_object_type(self
, name
, info
, doc
, ifcond
,
1707 # See also QAPISchemaObjectTypeMember._pretty_owner()
1708 name
= 'q_obj_%s-%s' % (name
, role
)
1709 typ
= self
.lookup_entity(name
, QAPISchemaObjectType
)
1711 # The implicit object type has multiple users. This can
1712 # happen only for simple unions' implicit wrapper types.
1713 # Its ifcond should be the disjunction of its user's
1714 # ifconds. Not implemented. Instead, we always pass the
1715 # wrapped type's ifcond, which is trivially the same for all
1716 # users. It's also necessary for the wrapper to compile.
1717 # But it's not tight: the disjunction need not imply it. We
1718 # may end up compiling useless wrapper types.
1719 # TODO kill simple unions or implement the disjunction
1720 assert ifcond
== typ
._ifcond
# pylint: disable=protected-access
1722 self
._def
_entity
(QAPISchemaObjectType(name
, info
, doc
, ifcond
,
1723 None, members
, None))
1726 def _def_enum_type(self
, expr
, info
, doc
):
1729 prefix
= expr
.get('prefix')
1730 ifcond
= expr
.get('if')
1731 self
._def
_entity
(QAPISchemaEnumType(
1732 name
, info
, doc
, ifcond
,
1733 self
._make
_enum
_members
(data
), prefix
))
1735 def _make_member(self
, name
, typ
, ifcond
, info
):
1737 if name
.startswith('*'):
1740 if isinstance(typ
, list):
1741 assert len(typ
) == 1
1742 typ
= self
._make
_array
_type
(typ
[0], info
)
1743 return QAPISchemaObjectTypeMember(name
, typ
, optional
, ifcond
)
1745 def _make_members(self
, data
, info
):
1746 return [self
._make
_member
(key
, value
['type'], value
.get('if'), info
)
1747 for (key
, value
) in data
.items()]
1749 def _def_struct_type(self
, expr
, info
, doc
):
1750 name
= expr
['struct']
1751 base
= expr
.get('base')
1753 ifcond
= expr
.get('if')
1754 self
._def
_entity
(QAPISchemaObjectType(name
, info
, doc
, ifcond
, base
,
1755 self
._make
_members
(data
, info
),
1758 def _make_variant(self
, case
, typ
, ifcond
):
1759 return QAPISchemaObjectTypeVariant(case
, typ
, ifcond
)
1761 def _make_simple_variant(self
, case
, typ
, ifcond
, info
):
1762 if isinstance(typ
, list):
1763 assert len(typ
) == 1
1764 typ
= self
._make
_array
_type
(typ
[0], info
)
1765 typ
= self
._make
_implicit
_object
_type
(
1766 typ
, info
, None, self
.lookup_type(typ
),
1767 'wrapper', [self
._make
_member
('data', typ
, None, info
)])
1768 return QAPISchemaObjectTypeVariant(case
, typ
, ifcond
)
1770 def _def_union_type(self
, expr
, info
, doc
):
1771 name
= expr
['union']
1773 base
= expr
.get('base')
1774 ifcond
= expr
.get('if')
1775 tag_name
= expr
.get('discriminator')
1777 if isinstance(base
, dict):
1778 base
= self
._make
_implicit
_object
_type
(
1779 name
, info
, doc
, ifcond
,
1780 'base', self
._make
_members
(base
, info
))
1782 variants
= [self
._make
_variant
(key
, value
['type'], value
.get('if'))
1783 for (key
, value
) in data
.items()]
1786 variants
= [self
._make
_simple
_variant
(key
, value
['type'],
1787 value
.get('if'), info
)
1788 for (key
, value
) in data
.items()]
1789 enum
= [{'name': v
.name
, 'if': v
.ifcond
} for v
in variants
]
1790 typ
= self
._make
_implicit
_enum
_type
(name
, info
, ifcond
, enum
)
1791 tag_member
= QAPISchemaObjectTypeMember('type', typ
, False)
1792 members
= [tag_member
]
1794 QAPISchemaObjectType(name
, info
, doc
, ifcond
, base
, members
,
1795 QAPISchemaObjectTypeVariants(tag_name
,
1799 def _def_alternate_type(self
, expr
, info
, doc
):
1800 name
= expr
['alternate']
1802 ifcond
= expr
.get('if')
1803 variants
= [self
._make
_variant
(key
, value
['type'], value
.get('if'))
1804 for (key
, value
) in data
.items()]
1805 tag_member
= QAPISchemaObjectTypeMember('type', 'QType', False)
1807 QAPISchemaAlternateType(name
, info
, doc
, ifcond
,
1808 QAPISchemaObjectTypeVariants(None,
1812 def _def_command(self
, expr
, info
, doc
):
1813 name
= expr
['command']
1814 data
= expr
.get('data')
1815 rets
= expr
.get('returns')
1816 gen
= expr
.get('gen', True)
1817 success_response
= expr
.get('success-response', True)
1818 boxed
= expr
.get('boxed', False)
1819 allow_oob
= expr
.get('allow-oob', False)
1820 allow_preconfig
= expr
.get('allow-preconfig', False)
1821 ifcond
= expr
.get('if')
1822 if isinstance(data
, OrderedDict
):
1823 data
= self
._make
_implicit
_object
_type
(
1824 name
, info
, doc
, ifcond
, 'arg', self
._make
_members
(data
, info
))
1825 if isinstance(rets
, list):
1826 assert len(rets
) == 1
1827 rets
= self
._make
_array
_type
(rets
[0], info
)
1828 self
._def
_entity
(QAPISchemaCommand(name
, info
, doc
, ifcond
, data
, rets
,
1829 gen
, success_response
,
1830 boxed
, allow_oob
, allow_preconfig
))
1832 def _def_event(self
, expr
, info
, doc
):
1833 name
= expr
['event']
1834 data
= expr
.get('data')
1835 boxed
= expr
.get('boxed', False)
1836 ifcond
= expr
.get('if')
1837 if isinstance(data
, OrderedDict
):
1838 data
= self
._make
_implicit
_object
_type
(
1839 name
, info
, doc
, ifcond
, 'arg', self
._make
_members
(data
, info
))
1840 self
._def
_entity
(QAPISchemaEvent(name
, info
, doc
, ifcond
, data
, boxed
))
1842 def _def_exprs(self
, exprs
):
1843 for expr_elem
in exprs
:
1844 expr
= expr_elem
['expr']
1845 info
= expr_elem
['info']
1846 doc
= expr_elem
.get('doc')
1848 self
._def
_enum
_type
(expr
, info
, doc
)
1849 elif 'struct' in expr
:
1850 self
._def
_struct
_type
(expr
, info
, doc
)
1851 elif 'union' in expr
:
1852 self
._def
_union
_type
(expr
, info
, doc
)
1853 elif 'alternate' in expr
:
1854 self
._def
_alternate
_type
(expr
, info
, doc
)
1855 elif 'command' in expr
:
1856 self
._def
_command
(expr
, info
, doc
)
1857 elif 'event' in expr
:
1858 self
._def
_event
(expr
, info
, doc
)
1859 elif 'include' in expr
:
1860 self
._def
_include
(expr
, info
, doc
)
1865 for ent
in self
._entity
_list
:
1868 def visit(self
, visitor
):
1869 visitor
.visit_begin(self
)
1871 for entity
in self
._entity
_list
:
1872 if visitor
.visit_needed(entity
):
1873 if entity
.module
!= module
:
1874 module
= entity
.module
1875 visitor
.visit_module(module
)
1876 entity
.visit(visitor
)
1881 # Code generation helpers
1884 def camel_case(name
):
1888 if ch
in ['_', '-']:
1891 new_name
+= ch
.upper()
1894 new_name
+= ch
.lower()
1898 # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
1899 # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
1900 # ENUM24_Name -> ENUM24_NAME
1901 def camel_to_upper(value
):
1902 c_fun_str
= c_name(value
, False)
1907 length
= len(c_fun_str
)
1908 for i
in range(length
):
1910 # When c is upper and no '_' appears before, do more checks
1911 if c
.isupper() and (i
> 0) and c_fun_str
[i
- 1] != '_':
1912 if i
< length
- 1 and c_fun_str
[i
+ 1].islower():
1914 elif c_fun_str
[i
- 1].isdigit():
1917 return new_name
.lstrip('_').upper()
1920 def c_enum_const(type_name
, const_name
, prefix
=None):
1921 if prefix
is not None:
1923 return camel_to_upper(type_name
) + '_' + c_name(const_name
, False).upper()
1926 if hasattr(str, 'maketrans'):
1927 c_name_trans
= str.maketrans('.-', '__')
1929 c_name_trans
= string
.maketrans('.-', '__')
1932 # Map @name to a valid C identifier.
1933 # If @protect, avoid returning certain ticklish identifiers (like
1934 # C keywords) by prepending 'q_'.
1936 # Used for converting 'name' from a 'name':'type' qapi definition
1937 # into a generated struct member, as well as converting type names
1938 # into substrings of a generated C function name.
1939 # '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
1940 # protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
1941 def c_name(name
, protect
=True):
1942 # ANSI X3J11/88-090, 3.1.1
1943 c89_words
= set(['auto', 'break', 'case', 'char', 'const', 'continue',
1944 'default', 'do', 'double', 'else', 'enum', 'extern',
1945 'float', 'for', 'goto', 'if', 'int', 'long', 'register',
1946 'return', 'short', 'signed', 'sizeof', 'static',
1947 'struct', 'switch', 'typedef', 'union', 'unsigned',
1948 'void', 'volatile', 'while'])
1949 # ISO/IEC 9899:1999, 6.4.1
1950 c99_words
= set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
1951 # ISO/IEC 9899:2011, 6.4.1
1952 c11_words
= set(['_Alignas', '_Alignof', '_Atomic', '_Generic',
1953 '_Noreturn', '_Static_assert', '_Thread_local'])
1954 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
1956 gcc_words
= set(['asm', 'typeof'])
1957 # C++ ISO/IEC 14882:2003 2.11
1958 cpp_words
= set(['bool', 'catch', 'class', 'const_cast', 'delete',
1959 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
1960 'namespace', 'new', 'operator', 'private', 'protected',
1961 'public', 'reinterpret_cast', 'static_cast', 'template',
1962 'this', 'throw', 'true', 'try', 'typeid', 'typename',
1963 'using', 'virtual', 'wchar_t',
1964 # alternative representations
1965 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
1966 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
1967 # namespace pollution:
1968 polluted_words
= set(['unix', 'errno', 'mips', 'sparc', 'i386'])
1969 name
= name
.translate(c_name_trans
)
1970 if protect
and (name
in c89_words | c99_words | c11_words | gcc_words
1971 | cpp_words | polluted_words
):
1976 eatspace
= '\033EATSPACE.'
1977 pointer_suffix
= ' *' + eatspace
1980 def genindent(count
):
1982 for _
in range(count
):
1990 def push_indent(indent_amount
=4):
1992 indent_level
+= indent_amount
1995 def pop_indent(indent_amount
=4):
1997 indent_level
-= indent_amount
2000 # Generate @code with @kwds interpolated.
2001 # Obey indent_level, and strip eatspace.
2002 def cgen(code
, **kwds
):
2005 indent
= genindent(indent_level
)
2006 # re.subn() lacks flags support before Python 2.7, use re.compile()
2007 raw
= re
.subn(re
.compile(r
'^(?!(#|$))', re
.MULTILINE
),
2010 return re
.sub(re
.escape(eatspace
) + r
' *', '', raw
)
2013 def mcgen(code
, **kwds
):
2016 return cgen(code
, **kwds
)
2019 def guardname(filename
):
2020 return re
.sub(r
'[^A-Za-z0-9_]', '_', filename
).upper()
2023 def guardstart(name
):
2029 name
=guardname(name
))
2035 #endif /* %(name)s */
2037 name
=guardname(name
))
2049 def gen_endif(ifcond
):
2051 for ifc
in reversed(ifcond
):
2053 #endif /* %(cond)s */
2058 def _wrap_ifcond(ifcond
, before
, after
):
2060 return after
# suppress empty #if ... #endif
2062 assert after
.startswith(before
)
2064 added
= after
[len(before
):]
2065 if added
[0] == '\n':
2068 out
+= gen_if(ifcond
)
2070 out
+= gen_endif(ifcond
)
2074 def gen_enum_lookup(name
, members
, prefix
=None):
2077 const QEnumLookup %(c_name)s_lookup = {
2078 .array = (const char *const[]) {
2080 c_name
=c_name(name
))
2082 ret
+= gen_if(m
.ifcond
)
2083 index
= c_enum_const(name
, m
.name
, prefix
)
2085 [%(index)s] = "%(name)s",
2087 index
=index
, name
=m
.name
)
2088 ret
+= gen_endif(m
.ifcond
)
2092 .size = %(max_index)s
2095 max_index
=c_enum_const(name
, '_MAX', prefix
))
2099 def gen_enum(name
, members
, prefix
=None):
2100 # append automatically generated _MAX value
2101 enum_members
= members
+ [QAPISchemaMember('_MAX')]
2105 typedef enum %(c_name)s {
2107 c_name
=c_name(name
))
2109 for m
in enum_members
:
2110 ret
+= gen_if(m
.ifcond
)
2114 c_enum
=c_enum_const(name
, m
.name
, prefix
))
2115 ret
+= gen_endif(m
.ifcond
)
2120 c_name
=c_name(name
))
2124 #define %(c_name)s_str(val) \\
2125 qapi_enum_lookup(&%(c_name)s_lookup, (val))
2127 extern const QEnumLookup %(c_name)s_lookup;
2129 c_name
=c_name(name
))
2133 def build_params(arg_type
, boxed
, extra
=None):
2138 ret
+= '%s arg' % arg_type
.c_param_type()
2141 assert not arg_type
.variants
2142 for memb
in arg_type
.members
:
2146 ret
+= 'bool has_%s, ' % c_name(memb
.name
)
2147 ret
+= '%s %s' % (memb
.type.c_param_type(),
2151 return ret
if ret
else 'void'
2155 # Accumulate and write output
2158 class QAPIGen(object):
2164 def preamble_add(self
, text
):
2165 self
._preamble
+= text
2167 def add(self
, text
):
2170 def get_content(self
, fname
=None):
2171 return (self
._top
(fname
) + self
._preamble
+ self
._body
2172 + self
._bottom
(fname
))
2174 def _top(self
, fname
):
2177 def _bottom(self
, fname
):
2180 def write(self
, output_dir
, fname
):
2181 pathname
= os
.path
.join(output_dir
, fname
)
2182 dir = os
.path
.dirname(pathname
)
2186 except os
.error
as e
:
2187 if e
.errno
!= errno
.EEXIST
:
2189 fd
= os
.open(pathname
, os
.O_RDWR | os
.O_CREAT
, 0o666)
2190 if sys
.version_info
[0] >= 3:
2191 f
= open(fd
, 'r+', encoding
='utf-8')
2193 f
= os
.fdopen(fd
, 'r+')
2194 text
= self
.get_content(fname
)
2195 oldtext
= f
.read(len(text
) + 1)
2204 def ifcontext(ifcond
, *args
):
2205 """A 'with' statement context manager to wrap with start_if()/end_if()
2207 *args: any number of QAPIGenCCode
2211 with ifcontext(ifcond, self._genh, self._genc):
2212 modify self._genh and self._genc ...
2214 Is equivalent to calling::
2216 self._genh.start_if(ifcond)
2217 self._genc.start_if(ifcond)
2218 modify self._genh and self._genc ...
2223 arg
.start_if(ifcond
)
2229 class QAPIGenCCode(QAPIGen
):
2232 QAPIGen
.__init
__(self
)
2233 self
._start
_if
= None
2235 def start_if(self
, ifcond
):
2236 assert self
._start
_if
is None
2237 self
._start
_if
= (ifcond
, self
._body
, self
._preamble
)
2240 assert self
._start
_if
2242 self
._start
_if
= None
2244 def _wrap_ifcond(self
):
2245 self
._body
= _wrap_ifcond(self
._start
_if
[0],
2246 self
._start
_if
[1], self
._body
)
2247 self
._preamble
= _wrap_ifcond(self
._start
_if
[0],
2248 self
._start
_if
[2], self
._preamble
)
2250 def get_content(self
, fname
=None):
2251 assert self
._start
_if
is None
2252 return QAPIGen
.get_content(self
, fname
)
2255 class QAPIGenC(QAPIGenCCode
):
2257 def __init__(self
, blurb
, pydoc
):
2258 QAPIGenCCode
.__init
__(self
)
2260 self
._copyright
= '\n * '.join(re
.findall(r
'^Copyright .*', pydoc
,
2263 def _top(self
, fname
):
2265 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
2272 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
2273 * See the COPYING.LIB file in the top-level directory.
2277 blurb
=self
._blurb
, copyright
=self
._copyright
)
2279 def _bottom(self
, fname
):
2282 /* Dummy declaration to prevent empty .o file */
2283 char dummy_%(name)s;
2288 class QAPIGenH(QAPIGenC
):
2290 def _top(self
, fname
):
2291 return QAPIGenC
._top
(self
, fname
) + guardstart(fname
)
2293 def _bottom(self
, fname
):
2294 return guardend(fname
)
2297 class QAPIGenDoc(QAPIGen
):
2299 def _top(self
, fname
):
2300 return (QAPIGen
._top
(self
, fname
)
2301 + '@c AUTOMATICALLY GENERATED, DO NOT MODIFY\n\n')
2304 class QAPISchemaMonolithicCVisitor(QAPISchemaVisitor
):
2306 def __init__(self
, prefix
, what
, blurb
, pydoc
):
2307 self
._prefix
= prefix
2309 self
._genc
= QAPIGenC(blurb
, pydoc
)
2310 self
._genh
= QAPIGenH(blurb
, pydoc
)
2312 def write(self
, output_dir
):
2313 self
._genc
.write(output_dir
, self
._prefix
+ self
._what
+ '.c')
2314 self
._genh
.write(output_dir
, self
._prefix
+ self
._what
+ '.h')
2317 class QAPISchemaModularCVisitor(QAPISchemaVisitor
):
2319 def __init__(self
, prefix
, what
, blurb
, pydoc
):
2320 self
._prefix
= prefix
2325 self
._main
_module
= None
2327 def _module_basename(self
, what
, name
):
2329 return re
.sub(r
'-', '-builtin-', what
)
2330 basename
= os
.path
.join(os
.path
.dirname(name
),
2331 self
._prefix
+ what
)
2332 if name
== self
._main
_module
:
2334 return basename
+ '-' + os
.path
.splitext(os
.path
.basename(name
))[0]
2336 def _add_module(self
, name
, blurb
):
2337 if self
._main
_module
is None and name
is not None:
2338 self
._main
_module
= name
2339 genc
= QAPIGenC(blurb
, self
._pydoc
)
2340 genh
= QAPIGenH(blurb
, self
._pydoc
)
2341 self
._module
[name
] = (genc
, genh
)
2342 self
._set
_module
(name
)
2344 def _set_module(self
, name
):
2345 self
._genc
, self
._genh
= self
._module
[name
]
2347 def write(self
, output_dir
, opt_builtins
=False):
2348 for name
in self
._module
:
2349 if name
is None and not opt_builtins
:
2351 basename
= self
._module
_basename
(self
._what
, name
)
2352 (genc
, genh
) = self
._module
[name
]
2353 genc
.write(output_dir
, basename
+ '.c')
2354 genh
.write(output_dir
, basename
+ '.h')
2356 def _begin_module(self
, name
):
2359 def visit_module(self
, name
):
2360 if name
in self
._module
:
2361 self
._set
_module
(name
)
2363 self
._add
_module
(name
, self
._blurb
)
2364 self
._begin
_module
(name
)
2366 def visit_include(self
, name
, info
):
2367 basename
= self
._module
_basename
(self
._what
, name
)
2368 self
._genh
.preamble_add(mcgen('''
2369 #include "%(basename)s.h"