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_type
= base_members
.get(discriminator
)
592 if not discriminator_type
:
595 return enum_types
.get(discriminator_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_type(info
, "Member '%s' of %s" % (key
, source
), arg
,
709 allow_metas
=['built-in', 'union', 'alternate', 'struct',
713 def check_command(expr
, info
):
714 name
= expr
['command']
715 boxed
= expr
.get('boxed', False)
717 args_meta
= ['struct']
719 args_meta
+= ['union', 'alternate']
720 check_type(info
, "'data' for command '%s'" % name
,
721 expr
.get('data'), allow_dict
=not boxed
, allow_optional
=True,
722 allow_metas
=args_meta
)
723 returns_meta
= ['union', 'struct']
724 if name
in returns_whitelist
:
725 returns_meta
+= ['built-in', 'alternate', 'enum']
726 check_type(info
, "'returns' for command '%s'" % name
,
727 expr
.get('returns'), allow_array
=True,
728 allow_optional
=True, allow_metas
=returns_meta
)
731 def check_event(expr
, info
):
733 boxed
= expr
.get('boxed', False)
737 meta
+= ['union', 'alternate']
738 check_type(info
, "'data' for event '%s'" % name
,
739 expr
.get('data'), allow_dict
=not boxed
, allow_optional
=True,
743 def check_union(expr
, info
):
745 base
= expr
.get('base')
746 discriminator
= expr
.get('discriminator')
747 members
= expr
['data']
749 # Two types of unions, determined by discriminator.
751 # With no discriminator it is a simple union.
752 if discriminator
is None:
754 allow_metas
= ['built-in', 'union', 'alternate', 'struct', 'enum']
756 raise QAPISemError(info
, "Simple union '%s' must not have a base" %
759 # Else, it's a flat union.
761 # The object must have a string or dictionary 'base'.
762 check_type(info
, "'base' for union '%s'" % name
,
763 base
, allow_dict
=True, allow_optional
=True,
764 allow_metas
=['struct'])
766 raise QAPISemError(info
, "Flat union '%s' must have a base"
768 base_members
= find_base_members(base
)
769 assert base_members
is not None
771 # The value of member 'discriminator' must name a non-optional
772 # member of the base struct.
773 check_name(info
, "Discriminator of flat union '%s'" % name
,
775 discriminator_type
= base_members
.get(discriminator
)
776 if not discriminator_type
:
777 raise QAPISemError(info
,
778 "Discriminator '%s' is not a member of base "
780 % (discriminator
, base
))
781 enum_define
= enum_types
.get(discriminator_type
)
782 allow_metas
= ['struct']
783 # Do not allow string discriminator
785 raise QAPISemError(info
,
786 "Discriminator '%s' must be of enumeration "
787 "type" % discriminator
)
789 # Check every branch; don't allow an empty union
790 if len(members
) == 0:
791 raise QAPISemError(info
, "Union '%s' cannot have empty 'data'" % name
)
792 for (key
, value
) in members
.items():
793 check_name(info
, "Member of union '%s'" % name
, key
)
795 # Each value must name a known type
796 check_type(info
, "Member '%s' of union '%s'" % (key
, name
),
797 value
, allow_array
=not base
, allow_metas
=allow_metas
)
799 # If the discriminator names an enum type, then all members
800 # of 'data' must also be members of the enum type.
802 if key
not in enum_define
['data']:
803 raise QAPISemError(info
,
804 "Discriminator value '%s' is not found in "
806 % (key
, enum_define
['enum']))
809 def check_alternate(expr
, info
):
810 name
= expr
['alternate']
811 members
= expr
['data']
814 # Check every branch; require at least two branches
816 raise QAPISemError(info
,
817 "Alternate '%s' should have at least two branches "
819 for (key
, value
) in members
.items():
820 check_name(info
, "Member of alternate '%s'" % name
, key
)
822 # Ensure alternates have no type conflicts.
823 check_type(info
, "Member '%s' of alternate '%s'" % (key
, name
),
825 allow_metas
=['built-in', 'union', 'struct', 'enum'])
826 qtype
= find_alternate_member_qtype(value
)
828 raise QAPISemError(info
, "Alternate '%s' member '%s' cannot use "
829 "type '%s'" % (name
, key
, value
))
830 conflicting
= set([qtype
])
831 if qtype
== 'QTYPE_QSTRING':
832 enum_expr
= enum_types
.get(value
)
834 for v
in enum_expr
['data']:
835 if v
in ['on', 'off']:
836 conflicting
.add('QTYPE_QBOOL')
837 if re
.match(r
'[-+0-9.]', v
): # lazy, could be tightened
838 conflicting
.add('QTYPE_QNUM')
840 conflicting
.add('QTYPE_QNUM')
841 conflicting
.add('QTYPE_QBOOL')
842 for qt
in conflicting
:
844 raise QAPISemError(info
, "Alternate '%s' member '%s' can't "
845 "be distinguished from member '%s'"
846 % (name
, key
, types_seen
[qt
]))
850 def check_enum(expr
, info
):
852 members
= expr
.get('data')
853 prefix
= expr
.get('prefix')
855 if not isinstance(members
, list):
856 raise QAPISemError(info
,
857 "Enum '%s' requires an array for 'data'" % name
)
858 if prefix
is not None and not isinstance(prefix
, str):
859 raise QAPISemError(info
,
860 "Enum '%s' requires a string for 'prefix'" % name
)
861 for member
in members
:
862 check_name(info
, "Member of enum '%s'" % name
, member
,
866 def check_struct(expr
, info
):
867 name
= expr
['struct']
868 members
= expr
['data']
870 check_type(info
, "'data' for struct '%s'" % name
, members
,
871 allow_dict
=True, allow_optional
=True)
872 check_type(info
, "'base' for struct '%s'" % name
, expr
.get('base'),
873 allow_metas
=['struct'])
876 def check_keys(expr_elem
, meta
, required
, optional
=[]):
877 expr
= expr_elem
['expr']
878 info
= expr_elem
['info']
880 if not isinstance(name
, str):
881 raise QAPISemError(info
, "'%s' key must have a string value" % meta
)
882 required
= required
+ [meta
]
883 for (key
, value
) in expr
.items():
884 if key
not in required
and key
not in optional
:
885 raise QAPISemError(info
, "Unknown key '%s' in %s '%s'"
887 if (key
== 'gen' or key
== 'success-response') and value
is not False:
888 raise QAPISemError(info
,
889 "'%s' of %s '%s' should only use false value"
891 if (key
== 'boxed' or key
== 'allow-oob' or
892 key
== 'allow-preconfig') and value
is not True:
893 raise QAPISemError(info
,
894 "'%s' of %s '%s' should only use true value"
900 raise QAPISemError(info
, "Key '%s' is missing from %s '%s'"
904 def check_exprs(exprs
):
907 # Populate name table with names of built-in types
908 for builtin
in builtin_types
.keys():
909 all_names
[builtin
] = 'built-in'
911 # Learn the types and check for valid expression keys
912 for expr_elem
in exprs
:
913 expr
= expr_elem
['expr']
914 info
= expr_elem
['info']
915 doc
= expr_elem
.get('doc')
917 if 'include' in expr
:
920 if not doc
and doc_required
:
921 raise QAPISemError(info
,
922 "Expression missing documentation comment")
926 check_keys(expr_elem
, 'enum', ['data'], ['if', 'prefix'])
927 enum_types
[expr
[meta
]] = expr
928 elif 'union' in expr
:
930 check_keys(expr_elem
, 'union', ['data'],
931 ['base', 'discriminator', 'if'])
932 union_types
[expr
[meta
]] = expr
933 elif 'alternate' in expr
:
935 check_keys(expr_elem
, 'alternate', ['data'], ['if'])
936 elif 'struct' in expr
:
938 check_keys(expr_elem
, 'struct', ['data'], ['base', 'if'])
939 struct_types
[expr
[meta
]] = expr
940 elif 'command' in expr
:
942 check_keys(expr_elem
, 'command', [],
943 ['data', 'returns', 'gen', 'success-response',
944 'boxed', 'allow-oob', 'allow-preconfig', 'if'])
945 elif 'event' in expr
:
947 check_keys(expr_elem
, 'event', [], ['data', 'boxed', 'if'])
949 raise QAPISemError(expr_elem
['info'],
950 "Expression is missing metatype")
952 add_name(name
, info
, meta
)
953 if doc
and doc
.symbol
!= name
:
954 raise QAPISemError(info
, "Definition of '%s' follows documentation"
955 " for '%s'" % (name
, doc
.symbol
))
957 # Try again for hidden UnionKind enum
958 for expr_elem
in exprs
:
959 expr
= expr_elem
['expr']
961 if 'include' in expr
:
963 if 'union' in expr
and not discriminator_find_enum_define(expr
):
964 name
= '%sKind' % expr
['union']
965 elif 'alternate' in expr
:
966 name
= '%sKind' % expr
['alternate']
969 enum_types
[name
] = {'enum': name
}
970 add_name(name
, info
, 'enum', implicit
=True)
972 # Validate that exprs make sense
973 for expr_elem
in exprs
:
974 expr
= expr_elem
['expr']
975 info
= expr_elem
['info']
976 doc
= expr_elem
.get('doc')
978 if 'include' in expr
:
981 check_enum(expr
, info
)
982 elif 'union' in expr
:
983 check_union(expr
, info
)
984 elif 'alternate' in expr
:
985 check_alternate(expr
, info
)
986 elif 'struct' in expr
:
987 check_struct(expr
, info
)
988 elif 'command' in expr
:
989 check_command(expr
, info
)
990 elif 'event' in expr
:
991 check_event(expr
, info
)
993 assert False, 'unexpected meta type'
1002 # Schema compiler frontend
1005 def listify_cond(ifcond
):
1008 if not isinstance(ifcond
, list):
1013 class QAPISchemaEntity(object):
1014 def __init__(self
, name
, info
, doc
, ifcond
=None):
1015 assert name
is None or isinstance(name
, str)
1018 # For explicitly defined entities, info points to the (explicit)
1019 # definition. For builtins (and their arrays), info is None.
1020 # For implicitly defined entities, info points to a place that
1021 # triggered the implicit definition (there may be more than one
1025 self
._ifcond
= ifcond
# self.ifcond is set only after .check()
1028 return c_name(self
.name
)
1030 def check(self
, schema
):
1031 if isinstance(self
._ifcond
, QAPISchemaType
):
1032 # inherit the condition from a type
1035 self
.ifcond
= typ
.ifcond
1037 self
.ifcond
= listify_cond(self
._ifcond
)
1039 def is_implicit(self
):
1040 return not self
.info
1042 def visit(self
, visitor
):
1046 class QAPISchemaVisitor(object):
1047 def visit_begin(self
, schema
):
1050 def visit_end(self
):
1053 def visit_module(self
, fname
):
1056 def visit_needed(self
, entity
):
1057 # Default to visiting everything
1060 def visit_include(self
, fname
, info
):
1063 def visit_builtin_type(self
, name
, info
, json_type
):
1066 def visit_enum_type(self
, name
, info
, ifcond
, values
, prefix
):
1069 def visit_array_type(self
, name
, info
, ifcond
, element_type
):
1072 def visit_object_type(self
, name
, info
, ifcond
, base
, members
, variants
):
1075 def visit_object_type_flat(self
, name
, info
, ifcond
, members
, variants
):
1078 def visit_alternate_type(self
, name
, info
, ifcond
, variants
):
1081 def visit_command(self
, name
, info
, ifcond
, arg_type
, ret_type
, gen
,
1082 success_response
, boxed
, allow_oob
, allow_preconfig
):
1085 def visit_event(self
, name
, info
, ifcond
, arg_type
, boxed
):
1089 class QAPISchemaInclude(QAPISchemaEntity
):
1091 def __init__(self
, fname
, info
):
1092 QAPISchemaEntity
.__init
__(self
, None, info
, None)
1095 def visit(self
, visitor
):
1096 visitor
.visit_include(self
.fname
, self
.info
)
1099 class QAPISchemaType(QAPISchemaEntity
):
1100 # Return the C type for common use.
1101 # For the types we commonly box, this is a pointer type.
1105 # Return the C type to be used in a parameter list.
1106 def c_param_type(self
):
1107 return self
.c_type()
1109 # Return the C type to be used where we suppress boxing.
1110 def c_unboxed_type(self
):
1111 return self
.c_type()
1113 def json_type(self
):
1116 def alternate_qtype(self
):
1118 'null': 'QTYPE_QNULL',
1119 'string': 'QTYPE_QSTRING',
1120 'number': 'QTYPE_QNUM',
1121 'int': 'QTYPE_QNUM',
1122 'boolean': 'QTYPE_QBOOL',
1123 'object': 'QTYPE_QDICT'
1125 return json2qtype
.get(self
.json_type())
1128 if self
.is_implicit():
1133 class QAPISchemaBuiltinType(QAPISchemaType
):
1134 def __init__(self
, name
, json_type
, c_type
):
1135 QAPISchemaType
.__init
__(self
, name
, None, None)
1136 assert not c_type
or isinstance(c_type
, str)
1137 assert json_type
in ('string', 'number', 'int', 'boolean', 'null',
1139 self
._json
_type
_name
= json_type
1140 self
._c
_type
_name
= c_type
1146 return self
._c
_type
_name
1148 def c_param_type(self
):
1149 if self
.name
== 'str':
1150 return 'const ' + self
._c
_type
_name
1151 return self
._c
_type
_name
1153 def json_type(self
):
1154 return self
._json
_type
_name
1157 return self
.json_type()
1159 def visit(self
, visitor
):
1160 visitor
.visit_builtin_type(self
.name
, self
.info
, self
.json_type())
1163 class QAPISchemaEnumType(QAPISchemaType
):
1164 def __init__(self
, name
, info
, doc
, ifcond
, values
, prefix
):
1165 QAPISchemaType
.__init
__(self
, name
, info
, doc
, ifcond
)
1167 assert isinstance(v
, QAPISchemaMember
)
1169 assert prefix
is None or isinstance(prefix
, str)
1170 self
.values
= values
1171 self
.prefix
= prefix
1173 def check(self
, schema
):
1174 QAPISchemaType
.check(self
, schema
)
1176 for v
in self
.values
:
1177 v
.check_clash(self
.info
, seen
)
1179 self
.doc
.connect_member(v
)
1181 def is_implicit(self
):
1182 # See QAPISchema._make_implicit_enum_type() and ._def_predefineds()
1183 return self
.name
.endswith('Kind') or self
.name
== 'QType'
1186 return c_name(self
.name
)
1188 def member_names(self
):
1189 return [v
.name
for v
in self
.values
]
1191 def json_type(self
):
1194 def visit(self
, visitor
):
1195 visitor
.visit_enum_type(self
.name
, self
.info
, self
.ifcond
,
1196 self
.member_names(), self
.prefix
)
1199 class QAPISchemaArrayType(QAPISchemaType
):
1200 def __init__(self
, name
, info
, element_type
):
1201 QAPISchemaType
.__init
__(self
, name
, info
, None, None)
1202 assert isinstance(element_type
, str)
1203 self
._element
_type
_name
= element_type
1204 self
.element_type
= None
1206 def check(self
, schema
):
1207 QAPISchemaType
.check(self
, schema
)
1208 self
.element_type
= schema
.lookup_type(self
._element
_type
_name
)
1209 assert self
.element_type
1210 self
.element_type
.check(schema
)
1211 self
.ifcond
= self
.element_type
.ifcond
1213 def is_implicit(self
):
1217 return c_name(self
.name
) + pointer_suffix
1219 def json_type(self
):
1223 elt_doc_type
= self
.element_type
.doc_type()
1224 if not elt_doc_type
:
1226 return 'array of ' + elt_doc_type
1228 def visit(self
, visitor
):
1229 visitor
.visit_array_type(self
.name
, self
.info
, self
.ifcond
,
1233 class QAPISchemaObjectType(QAPISchemaType
):
1234 def __init__(self
, name
, info
, doc
, ifcond
,
1235 base
, local_members
, variants
):
1236 # struct has local_members, optional base, and no variants
1237 # flat union has base, variants, and no local_members
1238 # simple union has local_members, variants, and no base
1239 QAPISchemaType
.__init
__(self
, name
, info
, doc
, ifcond
)
1240 assert base
is None or isinstance(base
, str)
1241 for m
in local_members
:
1242 assert isinstance(m
, QAPISchemaObjectTypeMember
)
1244 if variants
is not None:
1245 assert isinstance(variants
, QAPISchemaObjectTypeVariants
)
1246 variants
.set_owner(name
)
1247 self
._base
_name
= base
1249 self
.local_members
= local_members
1250 self
.variants
= variants
1253 def check(self
, schema
):
1254 QAPISchemaType
.check(self
, schema
)
1255 if self
.members
is False: # check for cycles
1256 raise QAPISemError(self
.info
,
1257 "Object %s contains itself" % self
.name
)
1260 self
.members
= False # mark as being checked
1261 seen
= OrderedDict()
1263 self
.base
= schema
.lookup_type(self
._base
_name
)
1264 assert isinstance(self
.base
, QAPISchemaObjectType
)
1265 self
.base
.check(schema
)
1266 self
.base
.check_clash(self
.info
, seen
)
1267 for m
in self
.local_members
:
1269 m
.check_clash(self
.info
, seen
)
1271 self
.doc
.connect_member(m
)
1272 self
.members
= seen
.values()
1274 self
.variants
.check(schema
, seen
)
1275 assert self
.variants
.tag_member
in self
.members
1276 self
.variants
.check_clash(self
.info
, seen
)
1280 # Check that the members of this type do not cause duplicate JSON members,
1281 # and update seen to track the members seen so far. Report any errors
1282 # on behalf of info, which is not necessarily self.info
1283 def check_clash(self
, info
, seen
):
1284 assert not self
.variants
# not implemented
1285 for m
in self
.members
:
1286 m
.check_clash(info
, seen
)
1288 def is_implicit(self
):
1289 # See QAPISchema._make_implicit_object_type(), as well as
1290 # _def_predefineds()
1291 return self
.name
.startswith('q_')
1294 assert self
.members
is not None
1295 return not self
.members
and not self
.variants
1298 assert self
.name
!= 'q_empty'
1299 return QAPISchemaType
.c_name(self
)
1302 assert not self
.is_implicit()
1303 return c_name(self
.name
) + pointer_suffix
1305 def c_unboxed_type(self
):
1306 return c_name(self
.name
)
1308 def json_type(self
):
1311 def visit(self
, visitor
):
1312 visitor
.visit_object_type(self
.name
, self
.info
, self
.ifcond
,
1313 self
.base
, self
.local_members
, self
.variants
)
1314 visitor
.visit_object_type_flat(self
.name
, self
.info
, self
.ifcond
,
1315 self
.members
, self
.variants
)
1318 class QAPISchemaMember(object):
1321 def __init__(self
, name
):
1322 assert isinstance(name
, str)
1326 def set_owner(self
, name
):
1327 assert not self
.owner
1330 def check_clash(self
, info
, seen
):
1331 cname
= c_name(self
.name
)
1332 if cname
.lower() != cname
and self
.owner
not in name_case_whitelist
:
1333 raise QAPISemError(info
,
1334 "%s should not use uppercase" % self
.describe())
1336 raise QAPISemError(info
, "%s collides with %s" %
1337 (self
.describe(), seen
[cname
].describe()))
1340 def _pretty_owner(self
):
1342 if owner
.startswith('q_obj_'):
1343 # See QAPISchema._make_implicit_object_type() - reverse the
1344 # mapping there to create a nice human-readable description
1346 if owner
.endswith('-arg'):
1347 return '(parameter of %s)' % owner
[:-4]
1348 elif owner
.endswith('-base'):
1349 return '(base of %s)' % owner
[:-5]
1351 assert owner
.endswith('-wrapper')
1352 # Unreachable and not implemented
1354 if owner
.endswith('Kind'):
1355 # See QAPISchema._make_implicit_enum_type()
1356 return '(branch of %s)' % owner
[:-4]
1357 return '(%s of %s)' % (self
.role
, owner
)
1360 return "'%s' %s" % (self
.name
, self
._pretty
_owner
())
1363 class QAPISchemaObjectTypeMember(QAPISchemaMember
):
1364 def __init__(self
, name
, typ
, optional
):
1365 QAPISchemaMember
.__init
__(self
, name
)
1366 assert isinstance(typ
, str)
1367 assert isinstance(optional
, bool)
1368 self
._type
_name
= typ
1370 self
.optional
= optional
1372 def check(self
, schema
):
1374 self
.type = schema
.lookup_type(self
._type
_name
)
1378 class QAPISchemaObjectTypeVariants(object):
1379 def __init__(self
, tag_name
, tag_member
, variants
):
1380 # Flat unions pass tag_name but not tag_member.
1381 # Simple unions and alternates pass tag_member but not tag_name.
1382 # After check(), tag_member is always set, and tag_name remains
1383 # a reliable witness of being used by a flat union.
1384 assert bool(tag_member
) != bool(tag_name
)
1385 assert (isinstance(tag_name
, str) or
1386 isinstance(tag_member
, QAPISchemaObjectTypeMember
))
1387 assert len(variants
) > 0
1389 assert isinstance(v
, QAPISchemaObjectTypeVariant
)
1390 self
._tag
_name
= tag_name
1391 self
.tag_member
= tag_member
1392 self
.variants
= variants
1394 def set_owner(self
, name
):
1395 for v
in self
.variants
:
1398 def check(self
, schema
, seen
):
1399 if not self
.tag_member
: # flat union
1400 self
.tag_member
= seen
[c_name(self
._tag
_name
)]
1401 assert self
._tag
_name
== self
.tag_member
.name
1402 assert isinstance(self
.tag_member
.type, QAPISchemaEnumType
)
1403 if self
._tag
_name
: # flat union
1404 # branches that are not explicitly covered get an empty type
1405 cases
= set([v
.name
for v
in self
.variants
])
1406 for val
in self
.tag_member
.type.values
:
1407 if val
.name
not in cases
:
1408 v
= QAPISchemaObjectTypeVariant(val
.name
, 'q_empty')
1409 v
.set_owner(self
.tag_member
.owner
)
1410 self
.variants
.append(v
)
1411 for v
in self
.variants
:
1413 # Union names must match enum values; alternate names are
1414 # checked separately. Use 'seen' to tell the two apart.
1416 assert v
.name
in self
.tag_member
.type.member_names()
1417 assert isinstance(v
.type, QAPISchemaObjectType
)
1418 v
.type.check(schema
)
1420 def check_clash(self
, info
, seen
):
1421 for v
in self
.variants
:
1422 # Reset seen map for each variant, since qapi names from one
1423 # branch do not affect another branch
1424 assert isinstance(v
.type, QAPISchemaObjectType
)
1425 v
.type.check_clash(info
, dict(seen
))
1428 class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember
):
1431 def __init__(self
, name
, typ
):
1432 QAPISchemaObjectTypeMember
.__init
__(self
, name
, typ
, False)
1435 class QAPISchemaAlternateType(QAPISchemaType
):
1436 def __init__(self
, name
, info
, doc
, ifcond
, variants
):
1437 QAPISchemaType
.__init
__(self
, name
, info
, doc
, ifcond
)
1438 assert isinstance(variants
, QAPISchemaObjectTypeVariants
)
1439 assert variants
.tag_member
1440 variants
.set_owner(name
)
1441 variants
.tag_member
.set_owner(self
.name
)
1442 self
.variants
= variants
1444 def check(self
, schema
):
1445 QAPISchemaType
.check(self
, schema
)
1446 self
.variants
.tag_member
.check(schema
)
1447 # Not calling self.variants.check_clash(), because there's nothing
1449 self
.variants
.check(schema
, {})
1450 # Alternate branch names have no relation to the tag enum values;
1451 # so we have to check for potential name collisions ourselves.
1453 for v
in self
.variants
.variants
:
1454 v
.check_clash(self
.info
, seen
)
1456 self
.doc
.connect_member(v
)
1461 return c_name(self
.name
) + pointer_suffix
1463 def json_type(self
):
1466 def visit(self
, visitor
):
1467 visitor
.visit_alternate_type(self
.name
, self
.info
, self
.ifcond
,
1474 class QAPISchemaCommand(QAPISchemaEntity
):
1475 def __init__(self
, name
, info
, doc
, ifcond
, arg_type
, ret_type
,
1476 gen
, success_response
, boxed
, allow_oob
, allow_preconfig
):
1477 QAPISchemaEntity
.__init
__(self
, name
, info
, doc
, ifcond
)
1478 assert not arg_type
or isinstance(arg_type
, str)
1479 assert not ret_type
or isinstance(ret_type
, str)
1480 self
._arg
_type
_name
= arg_type
1481 self
.arg_type
= None
1482 self
._ret
_type
_name
= ret_type
1483 self
.ret_type
= None
1485 self
.success_response
= success_response
1487 self
.allow_oob
= allow_oob
1488 self
.allow_preconfig
= allow_preconfig
1490 def check(self
, schema
):
1491 QAPISchemaEntity
.check(self
, schema
)
1492 if self
._arg
_type
_name
:
1493 self
.arg_type
= schema
.lookup_type(self
._arg
_type
_name
)
1494 assert (isinstance(self
.arg_type
, QAPISchemaObjectType
) or
1495 isinstance(self
.arg_type
, QAPISchemaAlternateType
))
1496 self
.arg_type
.check(schema
)
1498 if self
.arg_type
.is_empty():
1499 raise QAPISemError(self
.info
,
1500 "Cannot use 'boxed' with empty type")
1502 assert not isinstance(self
.arg_type
, QAPISchemaAlternateType
)
1503 assert not self
.arg_type
.variants
1505 raise QAPISemError(self
.info
, "Use of 'boxed' requires 'data'")
1506 if self
._ret
_type
_name
:
1507 self
.ret_type
= schema
.lookup_type(self
._ret
_type
_name
)
1508 assert isinstance(self
.ret_type
, QAPISchemaType
)
1510 def visit(self
, visitor
):
1511 visitor
.visit_command(self
.name
, self
.info
, self
.ifcond
,
1512 self
.arg_type
, self
.ret_type
,
1513 self
.gen
, self
.success_response
,
1514 self
.boxed
, self
.allow_oob
,
1515 self
.allow_preconfig
)
1518 class QAPISchemaEvent(QAPISchemaEntity
):
1519 def __init__(self
, name
, info
, doc
, ifcond
, arg_type
, boxed
):
1520 QAPISchemaEntity
.__init
__(self
, name
, info
, doc
, ifcond
)
1521 assert not arg_type
or isinstance(arg_type
, str)
1522 self
._arg
_type
_name
= arg_type
1523 self
.arg_type
= None
1526 def check(self
, schema
):
1527 QAPISchemaEntity
.check(self
, schema
)
1528 if self
._arg
_type
_name
:
1529 self
.arg_type
= schema
.lookup_type(self
._arg
_type
_name
)
1530 assert (isinstance(self
.arg_type
, QAPISchemaObjectType
) or
1531 isinstance(self
.arg_type
, QAPISchemaAlternateType
))
1532 self
.arg_type
.check(schema
)
1534 if self
.arg_type
.is_empty():
1535 raise QAPISemError(self
.info
,
1536 "Cannot use 'boxed' with empty type")
1538 assert not isinstance(self
.arg_type
, QAPISchemaAlternateType
)
1539 assert not self
.arg_type
.variants
1541 raise QAPISemError(self
.info
, "Use of 'boxed' requires 'data'")
1543 def visit(self
, visitor
):
1544 visitor
.visit_event(self
.name
, self
.info
, self
.ifcond
,
1545 self
.arg_type
, self
.boxed
)
1548 class QAPISchema(object):
1549 def __init__(self
, fname
):
1551 if sys
.version_info
[0] >= 3:
1552 f
= open(fname
, 'r', encoding
='utf-8')
1554 f
= open(fname
, 'r')
1555 parser
= QAPISchemaParser(f
)
1556 exprs
= check_exprs(parser
.exprs
)
1557 self
.docs
= parser
.docs
1558 self
._entity
_list
= []
1559 self
._entity
_dict
= {}
1560 self
._predefining
= True
1561 self
._def
_predefineds
()
1562 self
._predefining
= False
1563 self
._def
_exprs
(exprs
)
1566 def _def_entity(self
, ent
):
1567 # Only the predefined types are allowed to not have info
1568 assert ent
.info
or self
._predefining
1569 assert ent
.name
is None or ent
.name
not in self
._entity
_dict
1570 self
._entity
_list
.append(ent
)
1571 if ent
.name
is not None:
1572 self
._entity
_dict
[ent
.name
] = ent
1574 ent
.module
= os
.path
.relpath(ent
.info
['file'],
1575 os
.path
.dirname(self
._fname
))
1577 def lookup_entity(self
, name
, typ
=None):
1578 ent
= self
._entity
_dict
.get(name
)
1579 if typ
and not isinstance(ent
, typ
):
1583 def lookup_type(self
, name
):
1584 return self
.lookup_entity(name
, QAPISchemaType
)
1586 def _def_include(self
, expr
, info
, doc
):
1587 include
= expr
['include']
1590 while main_info
['parent']:
1591 main_info
= main_info
['parent']
1592 fname
= os
.path
.relpath(include
, os
.path
.dirname(main_info
['file']))
1593 self
._def
_entity
(QAPISchemaInclude(fname
, info
))
1595 def _def_builtin_type(self
, name
, json_type
, c_type
):
1596 self
._def
_entity
(QAPISchemaBuiltinType(name
, json_type
, c_type
))
1597 # Instantiating only the arrays that are actually used would
1598 # be nice, but we can't as long as their generated code
1599 # (qapi-builtin-types.[ch]) may be shared by some other
1601 self
._make
_array
_type
(name
, None)
1603 def _def_predefineds(self
):
1604 for t
in [('str', 'string', 'char' + pointer_suffix
),
1605 ('number', 'number', 'double'),
1606 ('int', 'int', 'int64_t'),
1607 ('int8', 'int', 'int8_t'),
1608 ('int16', 'int', 'int16_t'),
1609 ('int32', 'int', 'int32_t'),
1610 ('int64', 'int', 'int64_t'),
1611 ('uint8', 'int', 'uint8_t'),
1612 ('uint16', 'int', 'uint16_t'),
1613 ('uint32', 'int', 'uint32_t'),
1614 ('uint64', 'int', 'uint64_t'),
1615 ('size', 'int', 'uint64_t'),
1616 ('bool', 'boolean', 'bool'),
1617 ('any', 'value', 'QObject' + pointer_suffix
),
1618 ('null', 'null', 'QNull' + pointer_suffix
)]:
1619 self
._def
_builtin
_type
(*t
)
1620 self
.the_empty_object_type
= QAPISchemaObjectType(
1621 'q_empty', None, None, None, None, [], None)
1622 self
._def
_entity
(self
.the_empty_object_type
)
1623 qtype_values
= self
._make
_enum
_members
(['none', 'qnull', 'qnum',
1624 'qstring', 'qdict', 'qlist',
1626 self
._def
_entity
(QAPISchemaEnumType('QType', None, None, None,
1627 qtype_values
, 'QTYPE'))
1629 def _make_enum_members(self
, values
):
1630 return [QAPISchemaMember(v
) for v
in values
]
1632 def _make_implicit_enum_type(self
, name
, info
, ifcond
, values
):
1633 # See also QAPISchemaObjectTypeMember._pretty_owner()
1634 name
= name
+ 'Kind' # Use namespace reserved by add_name()
1635 self
._def
_entity
(QAPISchemaEnumType(
1636 name
, info
, None, ifcond
, self
._make
_enum
_members
(values
), None))
1639 def _make_array_type(self
, element_type
, info
):
1640 name
= element_type
+ 'List' # Use namespace reserved by add_name()
1641 if not self
.lookup_type(name
):
1642 self
._def
_entity
(QAPISchemaArrayType(name
, info
, element_type
))
1645 def _make_implicit_object_type(self
, name
, info
, doc
, ifcond
,
1649 # See also QAPISchemaObjectTypeMember._pretty_owner()
1650 name
= 'q_obj_%s-%s' % (name
, role
)
1651 typ
= self
.lookup_entity(name
, QAPISchemaObjectType
)
1653 # The implicit object type has multiple users. This can
1654 # happen only for simple unions' implicit wrapper types.
1655 # Its ifcond should be the disjunction of its user's
1656 # ifconds. Not implemented. Instead, we always pass the
1657 # wrapped type's ifcond, which is trivially the same for all
1658 # users. It's also necessary for the wrapper to compile.
1659 # But it's not tight: the disjunction need not imply it. We
1660 # may end up compiling useless wrapper types.
1661 # TODO kill simple unions or implement the disjunction
1662 assert ifcond
== typ
._ifcond
# pylint: disable=protected-access
1664 self
._def
_entity
(QAPISchemaObjectType(name
, info
, doc
, ifcond
,
1665 None, members
, None))
1668 def _def_enum_type(self
, expr
, info
, doc
):
1671 prefix
= expr
.get('prefix')
1672 ifcond
= expr
.get('if')
1673 self
._def
_entity
(QAPISchemaEnumType(
1674 name
, info
, doc
, ifcond
,
1675 self
._make
_enum
_members
(data
), prefix
))
1677 def _make_member(self
, name
, typ
, info
):
1679 if name
.startswith('*'):
1682 if isinstance(typ
, list):
1683 assert len(typ
) == 1
1684 typ
= self
._make
_array
_type
(typ
[0], info
)
1685 return QAPISchemaObjectTypeMember(name
, typ
, optional
)
1687 def _make_members(self
, data
, info
):
1688 return [self
._make
_member
(key
, value
, info
)
1689 for (key
, value
) in data
.items()]
1691 def _def_struct_type(self
, expr
, info
, doc
):
1692 name
= expr
['struct']
1693 base
= expr
.get('base')
1695 ifcond
= expr
.get('if')
1696 self
._def
_entity
(QAPISchemaObjectType(name
, info
, doc
, ifcond
, base
,
1697 self
._make
_members
(data
, info
),
1700 def _make_variant(self
, case
, typ
):
1701 return QAPISchemaObjectTypeVariant(case
, typ
)
1703 def _make_simple_variant(self
, case
, typ
, info
):
1704 if isinstance(typ
, list):
1705 assert len(typ
) == 1
1706 typ
= self
._make
_array
_type
(typ
[0], info
)
1707 typ
= self
._make
_implicit
_object
_type
(
1708 typ
, info
, None, self
.lookup_type(typ
),
1709 'wrapper', [self
._make
_member
('data', typ
, info
)])
1710 return QAPISchemaObjectTypeVariant(case
, typ
)
1712 def _def_union_type(self
, expr
, info
, doc
):
1713 name
= expr
['union']
1715 base
= expr
.get('base')
1716 ifcond
= expr
.get('if')
1717 tag_name
= expr
.get('discriminator')
1719 if isinstance(base
, dict):
1720 base
= self
._make
_implicit
_object
_type
(
1721 name
, info
, doc
, ifcond
,
1722 'base', self
._make
_members
(base
, info
))
1724 variants
= [self
._make
_variant
(key
, value
)
1725 for (key
, value
) in data
.items()]
1728 variants
= [self
._make
_simple
_variant
(key
, value
, info
)
1729 for (key
, value
) in data
.items()]
1730 typ
= self
._make
_implicit
_enum
_type
(name
, info
, ifcond
,
1731 [v
.name
for v
in variants
])
1732 tag_member
= QAPISchemaObjectTypeMember('type', typ
, False)
1733 members
= [tag_member
]
1735 QAPISchemaObjectType(name
, info
, doc
, ifcond
, base
, members
,
1736 QAPISchemaObjectTypeVariants(tag_name
,
1740 def _def_alternate_type(self
, expr
, info
, doc
):
1741 name
= expr
['alternate']
1743 ifcond
= expr
.get('if')
1744 variants
= [self
._make
_variant
(key
, value
)
1745 for (key
, value
) in data
.items()]
1746 tag_member
= QAPISchemaObjectTypeMember('type', 'QType', False)
1748 QAPISchemaAlternateType(name
, info
, doc
, ifcond
,
1749 QAPISchemaObjectTypeVariants(None,
1753 def _def_command(self
, expr
, info
, doc
):
1754 name
= expr
['command']
1755 data
= expr
.get('data')
1756 rets
= expr
.get('returns')
1757 gen
= expr
.get('gen', True)
1758 success_response
= expr
.get('success-response', True)
1759 boxed
= expr
.get('boxed', False)
1760 allow_oob
= expr
.get('allow-oob', False)
1761 allow_preconfig
= expr
.get('allow-preconfig', False)
1762 ifcond
= expr
.get('if')
1763 if isinstance(data
, OrderedDict
):
1764 data
= self
._make
_implicit
_object
_type
(
1765 name
, info
, doc
, ifcond
, 'arg', self
._make
_members
(data
, info
))
1766 if isinstance(rets
, list):
1767 assert len(rets
) == 1
1768 rets
= self
._make
_array
_type
(rets
[0], info
)
1769 self
._def
_entity
(QAPISchemaCommand(name
, info
, doc
, ifcond
, data
, rets
,
1770 gen
, success_response
,
1771 boxed
, allow_oob
, allow_preconfig
))
1773 def _def_event(self
, expr
, info
, doc
):
1774 name
= expr
['event']
1775 data
= expr
.get('data')
1776 boxed
= expr
.get('boxed', False)
1777 ifcond
= expr
.get('if')
1778 if isinstance(data
, OrderedDict
):
1779 data
= self
._make
_implicit
_object
_type
(
1780 name
, info
, doc
, ifcond
, 'arg', self
._make
_members
(data
, info
))
1781 self
._def
_entity
(QAPISchemaEvent(name
, info
, doc
, ifcond
, data
, boxed
))
1783 def _def_exprs(self
, exprs
):
1784 for expr_elem
in exprs
:
1785 expr
= expr_elem
['expr']
1786 info
= expr_elem
['info']
1787 doc
= expr_elem
.get('doc')
1789 self
._def
_enum
_type
(expr
, info
, doc
)
1790 elif 'struct' in expr
:
1791 self
._def
_struct
_type
(expr
, info
, doc
)
1792 elif 'union' in expr
:
1793 self
._def
_union
_type
(expr
, info
, doc
)
1794 elif 'alternate' in expr
:
1795 self
._def
_alternate
_type
(expr
, info
, doc
)
1796 elif 'command' in expr
:
1797 self
._def
_command
(expr
, info
, doc
)
1798 elif 'event' in expr
:
1799 self
._def
_event
(expr
, info
, doc
)
1800 elif 'include' in expr
:
1801 self
._def
_include
(expr
, info
, doc
)
1806 for ent
in self
._entity
_list
:
1809 def visit(self
, visitor
):
1810 visitor
.visit_begin(self
)
1812 for entity
in self
._entity
_list
:
1813 if visitor
.visit_needed(entity
):
1814 if entity
.module
!= module
:
1815 module
= entity
.module
1816 visitor
.visit_module(module
)
1817 entity
.visit(visitor
)
1822 # Code generation helpers
1825 def camel_case(name
):
1829 if ch
in ['_', '-']:
1832 new_name
+= ch
.upper()
1835 new_name
+= ch
.lower()
1839 # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
1840 # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
1841 # ENUM24_Name -> ENUM24_NAME
1842 def camel_to_upper(value
):
1843 c_fun_str
= c_name(value
, False)
1851 # When c is upper and no '_' appears before, do more checks
1852 if c
.isupper() and (i
> 0) and c_fun_str
[i
- 1] != '_':
1853 if i
< l
- 1 and c_fun_str
[i
+ 1].islower():
1855 elif c_fun_str
[i
- 1].isdigit():
1858 return new_name
.lstrip('_').upper()
1861 def c_enum_const(type_name
, const_name
, prefix
=None):
1862 if prefix
is not None:
1864 return camel_to_upper(type_name
) + '_' + c_name(const_name
, False).upper()
1866 if hasattr(str, 'maketrans'):
1867 c_name_trans
= str.maketrans('.-', '__')
1869 c_name_trans
= string
.maketrans('.-', '__')
1872 # Map @name to a valid C identifier.
1873 # If @protect, avoid returning certain ticklish identifiers (like
1874 # C keywords) by prepending 'q_'.
1876 # Used for converting 'name' from a 'name':'type' qapi definition
1877 # into a generated struct member, as well as converting type names
1878 # into substrings of a generated C function name.
1879 # '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
1880 # protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
1881 def c_name(name
, protect
=True):
1882 # ANSI X3J11/88-090, 3.1.1
1883 c89_words
= set(['auto', 'break', 'case', 'char', 'const', 'continue',
1884 'default', 'do', 'double', 'else', 'enum', 'extern',
1885 'float', 'for', 'goto', 'if', 'int', 'long', 'register',
1886 'return', 'short', 'signed', 'sizeof', 'static',
1887 'struct', 'switch', 'typedef', 'union', 'unsigned',
1888 'void', 'volatile', 'while'])
1889 # ISO/IEC 9899:1999, 6.4.1
1890 c99_words
= set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
1891 # ISO/IEC 9899:2011, 6.4.1
1892 c11_words
= set(['_Alignas', '_Alignof', '_Atomic', '_Generic',
1893 '_Noreturn', '_Static_assert', '_Thread_local'])
1894 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
1896 gcc_words
= set(['asm', 'typeof'])
1897 # C++ ISO/IEC 14882:2003 2.11
1898 cpp_words
= set(['bool', 'catch', 'class', 'const_cast', 'delete',
1899 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
1900 'namespace', 'new', 'operator', 'private', 'protected',
1901 'public', 'reinterpret_cast', 'static_cast', 'template',
1902 'this', 'throw', 'true', 'try', 'typeid', 'typename',
1903 'using', 'virtual', 'wchar_t',
1904 # alternative representations
1905 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
1906 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
1907 # namespace pollution:
1908 polluted_words
= set(['unix', 'errno', 'mips', 'sparc', 'i386'])
1909 name
= name
.translate(c_name_trans
)
1910 if protect
and (name
in c89_words | c99_words | c11_words | gcc_words
1911 | cpp_words | polluted_words
):
1915 eatspace
= '\033EATSPACE.'
1916 pointer_suffix
= ' *' + eatspace
1919 def genindent(count
):
1921 for _
in range(count
):
1928 def push_indent(indent_amount
=4):
1930 indent_level
+= indent_amount
1933 def pop_indent(indent_amount
=4):
1935 indent_level
-= indent_amount
1938 # Generate @code with @kwds interpolated.
1939 # Obey indent_level, and strip eatspace.
1940 def cgen(code
, **kwds
):
1943 indent
= genindent(indent_level
)
1944 # re.subn() lacks flags support before Python 2.7, use re.compile()
1945 raw
= re
.subn(re
.compile(r
'^(?!(#|$))', re
.MULTILINE
),
1948 return re
.sub(re
.escape(eatspace
) + r
' *', '', raw
)
1951 def mcgen(code
, **kwds
):
1954 return cgen(code
, **kwds
)
1957 def guardname(filename
):
1958 return re
.sub(r
'[^A-Za-z0-9_]', '_', filename
).upper()
1961 def guardstart(name
):
1967 name
=guardname(name
))
1973 #endif /* %(name)s */
1975 name
=guardname(name
))
1987 def gen_endif(ifcond
):
1989 for ifc
in reversed(ifcond
):
1991 #endif /* %(cond)s */
1996 def _wrap_ifcond(ifcond
, before
, after
):
1998 return after
# suppress empty #if ... #endif
2000 assert after
.startswith(before
)
2002 added
= after
[len(before
):]
2003 if added
[0] == '\n':
2006 out
+= gen_if(ifcond
)
2008 out
+= gen_endif(ifcond
)
2012 def gen_enum_lookup(name
, values
, prefix
=None):
2015 const QEnumLookup %(c_name)s_lookup = {
2016 .array = (const char *const[]) {
2018 c_name
=c_name(name
))
2019 for value
in values
:
2020 index
= c_enum_const(name
, value
, prefix
)
2022 [%(index)s] = "%(value)s",
2024 index
=index
, value
=value
)
2028 .size = %(max_index)s
2031 max_index
=c_enum_const(name
, '_MAX', prefix
))
2035 def gen_enum(name
, values
, prefix
=None):
2036 # append automatically generated _MAX value
2037 enum_values
= values
+ ['_MAX']
2041 typedef enum %(c_name)s {
2043 c_name
=c_name(name
))
2046 for value
in enum_values
:
2050 c_enum
=c_enum_const(name
, value
, prefix
),
2057 c_name
=c_name(name
))
2061 #define %(c_name)s_str(val) \\
2062 qapi_enum_lookup(&%(c_name)s_lookup, (val))
2064 extern const QEnumLookup %(c_name)s_lookup;
2066 c_name
=c_name(name
))
2070 def build_params(arg_type
, boxed
, extra
):
2077 ret
+= '%s arg' % arg_type
.c_param_type()
2080 assert not arg_type
.variants
2081 for memb
in arg_type
.members
:
2085 ret
+= 'bool has_%s, ' % c_name(memb
.name
)
2086 ret
+= '%s %s' % (memb
.type.c_param_type(),
2094 # Accumulate and write output
2097 class QAPIGen(object):
2103 def preamble_add(self
, text
):
2104 self
._preamble
+= text
2106 def add(self
, text
):
2109 def get_content(self
, fname
=None):
2110 return (self
._top
(fname
) + self
._preamble
+ self
._body
2111 + self
._bottom
(fname
))
2113 def _top(self
, fname
):
2116 def _bottom(self
, fname
):
2119 def write(self
, output_dir
, fname
):
2120 pathname
= os
.path
.join(output_dir
, fname
)
2121 dir = os
.path
.dirname(pathname
)
2125 except os
.error
as e
:
2126 if e
.errno
!= errno
.EEXIST
:
2128 fd
= os
.open(pathname
, os
.O_RDWR | os
.O_CREAT
, 0o666)
2129 if sys
.version_info
[0] >= 3:
2130 f
= open(fd
, 'r+', encoding
='utf-8')
2132 f
= os
.fdopen(fd
, 'r+')
2133 text
= self
.get_content(fname
)
2134 oldtext
= f
.read(len(text
) + 1)
2143 def ifcontext(ifcond
, *args
):
2144 """A 'with' statement context manager to wrap with start_if()/end_if()
2146 *args: any number of QAPIGenCCode
2150 with ifcontext(ifcond, self._genh, self._genc):
2151 modify self._genh and self._genc ...
2153 Is equivalent to calling::
2155 self._genh.start_if(ifcond)
2156 self._genc.start_if(ifcond)
2157 modify self._genh and self._genc ...
2162 arg
.start_if(ifcond
)
2168 class QAPIGenCCode(QAPIGen
):
2171 QAPIGen
.__init
__(self
)
2172 self
._start
_if
= None
2174 def start_if(self
, ifcond
):
2175 assert self
._start
_if
is None
2176 self
._start
_if
= (ifcond
, self
._body
, self
._preamble
)
2179 assert self
._start
_if
2181 self
._start
_if
= None
2183 def _wrap_ifcond(self
):
2184 self
._body
= _wrap_ifcond(self
._start
_if
[0],
2185 self
._start
_if
[1], self
._body
)
2186 self
._preamble
= _wrap_ifcond(self
._start
_if
[0],
2187 self
._start
_if
[2], self
._preamble
)
2189 def get_content(self
, fname
=None):
2190 assert self
._start
_if
is None
2191 return QAPIGen
.get_content(self
, fname
)
2194 class QAPIGenC(QAPIGenCCode
):
2196 def __init__(self
, blurb
, pydoc
):
2197 QAPIGenCCode
.__init
__(self
)
2199 self
._copyright
= '\n * '.join(re
.findall(r
'^Copyright .*', pydoc
,
2202 def _top(self
, fname
):
2204 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
2211 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
2212 * See the COPYING.LIB file in the top-level directory.
2216 blurb
=self
._blurb
, copyright
=self
._copyright
)
2218 def _bottom(self
, fname
):
2220 /* Dummy declaration to prevent empty .o file */
2221 char dummy_%(name)s;
2226 class QAPIGenH(QAPIGenC
):
2228 def _top(self
, fname
):
2229 return QAPIGenC
._top
(self
, fname
) + guardstart(fname
)
2231 def _bottom(self
, fname
):
2232 return guardend(fname
)
2235 class QAPIGenDoc(QAPIGen
):
2237 def _top(self
, fname
):
2238 return (QAPIGen
._top
(self
, fname
)
2239 + '@c AUTOMATICALLY GENERATED, DO NOT MODIFY\n\n')
2242 class QAPISchemaMonolithicCVisitor(QAPISchemaVisitor
):
2244 def __init__(self
, prefix
, what
, blurb
, pydoc
):
2245 self
._prefix
= prefix
2247 self
._genc
= QAPIGenC(blurb
, pydoc
)
2248 self
._genh
= QAPIGenH(blurb
, pydoc
)
2250 def write(self
, output_dir
):
2251 self
._genc
.write(output_dir
, self
._prefix
+ self
._what
+ '.c')
2252 self
._genh
.write(output_dir
, self
._prefix
+ self
._what
+ '.h')
2255 class QAPISchemaModularCVisitor(QAPISchemaVisitor
):
2257 def __init__(self
, prefix
, what
, blurb
, pydoc
):
2258 self
._prefix
= prefix
2263 self
._main
_module
= None
2265 def _module_basename(self
, what
, name
):
2267 return re
.sub(r
'-', '-builtin-', what
)
2268 basename
= os
.path
.join(os
.path
.dirname(name
),
2269 self
._prefix
+ what
)
2270 if name
== self
._main
_module
:
2272 return basename
+ '-' + os
.path
.splitext(os
.path
.basename(name
))[0]
2274 def _add_module(self
, name
, blurb
):
2275 if self
._main
_module
is None and name
is not None:
2276 self
._main
_module
= name
2277 genc
= QAPIGenC(blurb
, self
._pydoc
)
2278 genh
= QAPIGenH(blurb
, self
._pydoc
)
2279 self
._module
[name
] = (genc
, genh
)
2280 self
._set
_module
(name
)
2282 def _set_module(self
, name
):
2283 self
._genc
, self
._genh
= self
._module
[name
]
2285 def write(self
, output_dir
, opt_builtins
=False):
2286 for name
in self
._module
:
2287 if name
is None and not opt_builtins
:
2289 basename
= self
._module
_basename
(self
._what
, name
)
2290 (genc
, genh
) = self
._module
[name
]
2291 genc
.write(output_dir
, basename
+ '.c')
2292 genh
.write(output_dir
, basename
+ '.h')
2294 def _begin_module(self
, name
):
2297 def visit_module(self
, name
):
2298 if name
in self
._module
:
2299 self
._set
_module
(name
)
2301 self
._add
_module
(name
, self
._blurb
)
2302 self
._begin
_module
(name
)
2304 def visit_include(self
, name
, info
):
2305 basename
= self
._module
_basename
(self
._what
, name
)
2306 self
._genh
.preamble_add(mcgen('''
2307 #include "%(basename)s.h"