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
20 from collections
import OrderedDict
22 from ordereddict
import OrderedDict
25 'null': 'QTYPE_QNULL',
26 'str': 'QTYPE_QSTRING',
28 'number': 'QTYPE_QNUM',
29 'bool': 'QTYPE_QBOOL',
31 'int16': 'QTYPE_QNUM',
32 'int32': 'QTYPE_QNUM',
33 'int64': 'QTYPE_QNUM',
34 'uint8': 'QTYPE_QNUM',
35 'uint16': 'QTYPE_QNUM',
36 'uint32': 'QTYPE_QNUM',
37 'uint64': 'QTYPE_QNUM',
39 'any': None, # any QType possible, actually
40 'QType': 'QTYPE_QSTRING',
43 # Are documentation comments required?
46 # Whitelist of commands allowed to return a non-dictionary
47 returns_whitelist
= []
49 # Whitelist of entities allowed to violate case conventions
50 name_case_whitelist
= []
58 # Parsing the schema into expressions
62 def error_path(parent
):
65 res
= ('In file included from %s:%d:\n' % (parent
['file'],
66 parent
['line'])) + res
67 parent
= parent
['parent']
71 class QAPIError(Exception):
72 def __init__(self
, fname
, line
, col
, incl_info
, msg
):
73 Exception.__init
__(self
)
81 loc
= '%s:%d' % (self
.fname
, self
.line
)
82 if self
.col
is not None:
83 loc
+= ':%s' % self
.col
84 return error_path(self
.info
) + '%s: %s' % (loc
, self
.msg
)
87 class QAPIParseError(QAPIError
):
88 def __init__(self
, parser
, msg
):
90 for ch
in parser
.src
[parser
.line_pos
:parser
.pos
]:
92 col
= (col
+ 7) % 8 + 1
95 QAPIError
.__init
__(self
, parser
.fname
, parser
.line
, col
,
96 parser
.incl_info
, msg
)
99 class QAPISemError(QAPIError
):
100 def __init__(self
, info
, msg
):
101 QAPIError
.__init
__(self
, info
['file'], info
['line'], None,
105 class QAPIDoc(object):
106 class Section(object):
107 def __init__(self
, name
=None):
108 # optional section name (argument/member or section name)
110 # the list of lines for this section
113 def append(self
, line
):
114 self
.text
+= line
.rstrip() + '\n'
116 class ArgSection(Section
):
117 def __init__(self
, name
):
118 QAPIDoc
.Section
.__init
__(self
, name
)
121 def connect(self
, member
):
124 def __init__(self
, parser
, info
):
125 # self._parser is used to report errors with QAPIParseError. The
126 # resulting error position depends on the state of the parser.
127 # It happens to be the beginning of the comment. More or less
128 # servicable, but action at a distance.
129 self
._parser
= parser
132 self
.body
= QAPIDoc
.Section()
133 # dict mapping parameter name to ArgSection
134 self
.args
= OrderedDict()
137 # the current section
138 self
._section
= self
.body
140 def has_section(self
, name
):
141 """Return True if we have a section with this name."""
142 for i
in self
.sections
:
147 def append(self
, line
):
148 """Parse a comment line and add it to the documentation."""
151 self
._append
_freeform
(line
)
155 raise QAPIParseError(self
._parser
, "Missing space after #")
158 # FIXME not nice: things like '# @foo:' and '# @foo: ' aren't
159 # recognized, and get silently treated as ordinary text
161 self
._append
_symbol
_line
(line
)
162 elif not self
.body
.text
and line
.startswith('@'):
163 if not line
.endswith(':'):
164 raise QAPIParseError(self
._parser
, "Line should end with :")
165 self
.symbol
= line
[1:-1]
166 # FIXME invalid names other than the empty string aren't flagged
168 raise QAPIParseError(self
._parser
, "Invalid name")
170 self
._append
_freeform
(line
)
172 def end_comment(self
):
175 def _append_symbol_line(self
, line
):
176 name
= line
.split(' ', 1)[0]
178 if name
.startswith('@') and name
.endswith(':'):
179 line
= line
[len(name
)+1:]
180 self
._start
_args
_section
(name
[1:-1])
181 elif name
in ('Returns:', 'Since:',
182 # those are often singular or plural
184 'Example:', 'Examples:',
186 line
= line
[len(name
)+1:]
187 self
._start
_section
(name
[:-1])
189 self
._append
_freeform
(line
)
191 def _start_args_section(self
, name
):
192 # FIXME invalid names other than the empty string aren't flagged
194 raise QAPIParseError(self
._parser
, "Invalid parameter name")
195 if name
in self
.args
:
196 raise QAPIParseError(self
._parser
,
197 "'%s' parameter name duplicated" % name
)
199 raise QAPIParseError(self
._parser
,
200 "'@%s:' can't follow '%s' section"
201 % (name
, self
.sections
[0].name
))
203 self
._section
= QAPIDoc
.ArgSection(name
)
204 self
.args
[name
] = self
._section
206 def _start_section(self
, name
=None):
207 if name
in ('Returns', 'Since') and self
.has_section(name
):
208 raise QAPIParseError(self
._parser
,
209 "Duplicated '%s' section" % name
)
211 self
._section
= QAPIDoc
.Section(name
)
212 self
.sections
.append(self
._section
)
214 def _end_section(self
):
216 text
= self
._section
.text
= self
._section
.text
.strip()
217 if self
._section
.name
and (not text
or text
.isspace()):
218 raise QAPIParseError(self
._parser
, "Empty doc section '%s'"
219 % self
._section
.name
)
222 def _append_freeform(self
, line
):
223 in_arg
= isinstance(self
._section
, QAPIDoc
.ArgSection
)
224 if (in_arg
and self
._section
.text
.endswith('\n\n')
225 and line
and not line
[0].isspace()):
226 self
._start
_section
()
227 if (in_arg
or not self
._section
.name
228 or not self
._section
.name
.startswith('Example')):
230 match
= re
.match(r
'(@\S+:)', line
)
232 raise QAPIParseError(self
._parser
,
233 "'%s' not allowed in free-form documentation"
235 self
._section
.append(line
)
237 def connect_member(self
, member
):
238 if member
.name
not in self
.args
:
239 # Undocumented TODO outlaw
240 self
.args
[member
.name
] = QAPIDoc
.ArgSection(member
.name
)
241 self
.args
[member
.name
].connect(member
)
243 def check_expr(self
, expr
):
244 if self
.has_section('Returns') and 'command' not in expr
:
245 raise QAPISemError(self
.info
,
246 "'Returns:' is only valid for commands")
249 bogus
= [name
for name
, section
in self
.args
.items()
250 if not section
.member
]
254 "The following documented members are not in "
255 "the declaration: %s" % ", ".join(bogus
))
258 class QAPISchemaParser(object):
260 def __init__(self
, fp
, previously_included
=[], incl_info
=None):
262 previously_included
.append(os
.path
.abspath(fp
.name
))
263 self
.incl_info
= incl_info
265 if self
.src
== '' or self
.src
[-1] != '\n':
275 while self
.tok
is not None:
276 info
= {'file': self
.fname
, 'line': self
.line
,
277 'parent': self
.incl_info
}
279 self
.reject_expr_doc(cur_doc
)
280 cur_doc
= self
.get_doc(info
)
281 self
.docs
.append(cur_doc
)
284 expr
= self
.get_expr(False)
285 if 'include' in expr
:
286 self
.reject_expr_doc(cur_doc
)
288 raise QAPISemError(info
, "Invalid 'include' directive")
289 include
= expr
['include']
290 if not isinstance(include
, str):
291 raise QAPISemError(info
,
292 "Value of 'include' must be a string")
293 incl_fname
= os
.path
.join(os
.path
.dirname(self
.fname
),
295 self
.exprs
.append({'expr': {'include': incl_fname
},
297 exprs_include
= self
._include
(include
, info
, incl_fname
,
300 self
.exprs
.extend(exprs_include
.exprs
)
301 self
.docs
.extend(exprs_include
.docs
)
302 elif "pragma" in expr
:
303 self
.reject_expr_doc(cur_doc
)
305 raise QAPISemError(info
, "Invalid 'pragma' directive")
306 pragma
= expr
['pragma']
307 if not isinstance(pragma
, dict):
309 info
, "Value of 'pragma' must be a dictionary")
310 for name
, value
in pragma
.items():
311 self
._pragma
(name
, value
, info
)
313 expr_elem
= {'expr': expr
,
316 if not cur_doc
.symbol
:
318 cur_doc
.info
, "Expression documentation required")
319 expr_elem
['doc'] = cur_doc
320 self
.exprs
.append(expr_elem
)
322 self
.reject_expr_doc(cur_doc
)
325 def reject_expr_doc(doc
):
326 if doc
and doc
.symbol
:
329 "Documentation for '%s' is not followed by the definition"
332 def _include(self
, include
, info
, incl_fname
, previously_included
):
333 incl_abs_fname
= os
.path
.abspath(incl_fname
)
334 # catch inclusion cycle
337 if incl_abs_fname
== os
.path
.abspath(inf
['file']):
338 raise QAPISemError(info
, "Inclusion loop for %s" % include
)
341 # skip multiple include of the same file
342 if incl_abs_fname
in previously_included
:
346 fobj
= open(incl_fname
, 'r')
348 raise QAPISemError(info
, '%s: %s' % (e
.strerror
, incl_fname
))
349 return QAPISchemaParser(fobj
, previously_included
, info
)
351 def _pragma(self
, name
, value
, info
):
352 global doc_required
, returns_whitelist
, name_case_whitelist
353 if name
== 'doc-required':
354 if not isinstance(value
, bool):
355 raise QAPISemError(info
,
356 "Pragma 'doc-required' must be boolean")
358 elif name
== 'returns-whitelist':
359 if (not isinstance(value
, list)
360 or any([not isinstance(elt
, str) for elt
in value
])):
361 raise QAPISemError(info
,
362 "Pragma returns-whitelist must be"
363 " a list of strings")
364 returns_whitelist
= value
365 elif name
== 'name-case-whitelist':
366 if (not isinstance(value
, list)
367 or any([not isinstance(elt
, str) for elt
in value
])):
368 raise QAPISemError(info
,
369 "Pragma name-case-whitelist must be"
370 " a list of strings")
371 name_case_whitelist
= value
373 raise QAPISemError(info
, "Unknown pragma '%s'" % name
)
375 def accept(self
, skip_comment
=True):
377 self
.tok
= self
.src
[self
.cursor
]
378 self
.pos
= self
.cursor
383 if self
.src
[self
.cursor
] == '#':
384 # Start of doc comment
386 self
.cursor
= self
.src
.find('\n', self
.cursor
)
388 self
.val
= self
.src
[self
.pos
:self
.cursor
]
390 elif self
.tok
in '{}:,[]':
392 elif self
.tok
== "'":
396 ch
= self
.src
[self
.cursor
]
399 raise QAPIParseError(self
, 'Missing terminating "\'"')
413 for _
in range(0, 4):
414 ch
= self
.src
[self
.cursor
]
416 if ch
not in '0123456789abcdefABCDEF':
417 raise QAPIParseError(self
,
418 '\\u escape needs 4 '
420 value
= (value
<< 4) + int(ch
, 16)
421 # If Python 2 and 3 didn't disagree so much on
422 # how to handle Unicode, then we could allow
423 # Unicode string defaults. But most of QAPI is
424 # ASCII-only, so we aren't losing much for now.
425 if not value
or value
> 0x7f:
426 raise QAPIParseError(self
,
427 'For now, \\u escape '
428 'only supports non-zero '
429 'values up to \\u007f')
434 raise QAPIParseError(self
,
435 "Unknown escape \\%s" % ch
)
444 elif self
.src
.startswith('true', self
.pos
):
448 elif self
.src
.startswith('false', self
.pos
):
452 elif self
.src
.startswith('null', self
.pos
):
456 elif self
.tok
== '\n':
457 if self
.cursor
== len(self
.src
):
461 self
.line_pos
= self
.cursor
462 elif not self
.tok
.isspace():
463 raise QAPIParseError(self
, 'Stray "%s"' % self
.tok
)
465 def get_members(self
):
471 raise QAPIParseError(self
, 'Expected string or "}"')
476 raise QAPIParseError(self
, 'Expected ":"')
479 raise QAPIParseError(self
, 'Duplicate key "%s"' % key
)
480 expr
[key
] = self
.get_expr(True)
485 raise QAPIParseError(self
, 'Expected "," or "}"')
488 raise QAPIParseError(self
, 'Expected string')
490 def get_values(self
):
495 if self
.tok
not in "{['tfn":
496 raise QAPIParseError(self
, 'Expected "{", "[", "]", string, '
499 expr
.append(self
.get_expr(True))
504 raise QAPIParseError(self
, 'Expected "," or "]"')
507 def get_expr(self
, nested
):
508 if self
.tok
!= '{' and not nested
:
509 raise QAPIParseError(self
, 'Expected "{"')
512 expr
= self
.get_members()
513 elif self
.tok
== '[':
515 expr
= self
.get_values()
516 elif self
.tok
in "'tfn":
520 raise QAPIParseError(self
, 'Expected "{", "[", string, '
524 def get_doc(self
, info
):
526 raise QAPIParseError(self
, "Junk after '##' at start of "
527 "documentation comment")
529 doc
= QAPIDoc(self
, info
)
531 while self
.tok
== '#':
532 if self
.val
.startswith('##'):
535 raise QAPIParseError(self
, "Junk after '##' at end of "
536 "documentation comment")
544 raise QAPIParseError(self
, "Documentation comment must end with '##'")
548 # Semantic analysis of schema expressions
549 # TODO fold into QAPISchema
550 # TODO catching name collisions in generated code would be nice
554 def find_base_members(base
):
555 if isinstance(base
, dict):
557 base_struct_define
= struct_types
.get(base
)
558 if not base_struct_define
:
560 return base_struct_define
['data']
563 # Return the qtype of an alternate branch, or None on error.
564 def find_alternate_member_qtype(qapi_type
):
565 if qapi_type
in builtin_types
:
566 return builtin_types
[qapi_type
]
567 elif qapi_type
in struct_types
:
569 elif qapi_type
in enum_types
:
570 return 'QTYPE_QSTRING'
571 elif qapi_type
in union_types
:
576 # Return the discriminator enum define if discriminator is specified as an
577 # enum type, otherwise return None.
578 def discriminator_find_enum_define(expr
):
579 base
= expr
.get('base')
580 discriminator
= expr
.get('discriminator')
582 if not (discriminator
and base
):
585 base_members
= find_base_members(base
)
589 discriminator_type
= base_members
.get(discriminator
)
590 if not discriminator_type
:
593 return enum_types
.get(discriminator_type
)
596 # Names must be letters, numbers, -, and _. They must start with letter,
597 # except for downstream extensions which must start with __RFQDN_.
598 # Dots are only valid in the downstream extension prefix.
599 valid_name
= re
.compile(r
'^(__[a-zA-Z0-9.-]+_)?'
600 '[a-zA-Z][a-zA-Z0-9_-]*$')
603 def check_name(info
, source
, name
, allow_optional
=False,
608 if not isinstance(name
, str):
609 raise QAPISemError(info
, "%s requires a string name" % source
)
610 if name
.startswith('*'):
611 membername
= name
[1:]
612 if not allow_optional
:
613 raise QAPISemError(info
, "%s does not allow optional name '%s'"
615 # Enum members can start with a digit, because the generated C
616 # code always prefixes it with the enum name
617 if enum_member
and membername
[0].isdigit():
618 membername
= 'D' + membername
619 # Reserve the entire 'q_' namespace for c_name(), and for 'q_empty'
620 # and 'q_obj_*' implicit type names.
621 if not valid_name
.match(membername
) or \
622 c_name(membername
, False).startswith('q_'):
623 raise QAPISemError(info
, "%s uses invalid name '%s'" % (source
, name
))
626 def add_name(name
, info
, meta
, implicit
=False):
628 check_name(info
, "'%s'" % meta
, name
)
629 # FIXME should reject names that differ only in '_' vs. '.'
630 # vs. '-', because they're liable to clash in generated C.
631 if name
in all_names
:
632 raise QAPISemError(info
, "%s '%s' is already defined"
633 % (all_names
[name
], name
))
634 if not implicit
and (name
.endswith('Kind') or name
.endswith('List')):
635 raise QAPISemError(info
, "%s '%s' should not end in '%s'"
636 % (meta
, name
, name
[-4:]))
637 all_names
[name
] = meta
640 def check_type(info
, source
, value
, allow_array
=False,
641 allow_dict
=False, allow_optional
=False,
648 # Check if array type for value is okay
649 if isinstance(value
, list):
651 raise QAPISemError(info
, "%s cannot be an array" % source
)
652 if len(value
) != 1 or not isinstance(value
[0], str):
653 raise QAPISemError(info
,
654 "%s: array type must contain single type name" %
658 # Check if type name for value is okay
659 if isinstance(value
, str):
660 if value
not in all_names
:
661 raise QAPISemError(info
, "%s uses unknown type '%s'"
663 if not all_names
[value
] in allow_metas
:
664 raise QAPISemError(info
, "%s cannot use %s type '%s'" %
665 (source
, all_names
[value
], value
))
669 raise QAPISemError(info
, "%s should be a type name" % source
)
671 if not isinstance(value
, OrderedDict
):
672 raise QAPISemError(info
,
673 "%s should be a dictionary or type name" % source
)
675 # value is a dictionary, check that each member is okay
676 for (key
, arg
) in value
.items():
677 check_name(info
, "Member of %s" % source
, key
,
678 allow_optional
=allow_optional
)
679 if c_name(key
, False) == 'u' or c_name(key
, False).startswith('has_'):
680 raise QAPISemError(info
, "Member of %s uses reserved name '%s'"
682 # Todo: allow dictionaries to represent default values of
683 # an optional argument.
684 check_type(info
, "Member '%s' of %s" % (key
, source
), arg
,
686 allow_metas
=['built-in', 'union', 'alternate', 'struct',
690 def check_command(expr
, info
):
691 name
= expr
['command']
692 boxed
= expr
.get('boxed', False)
694 args_meta
= ['struct']
696 args_meta
+= ['union', 'alternate']
697 check_type(info
, "'data' for command '%s'" % name
,
698 expr
.get('data'), allow_dict
=not boxed
, allow_optional
=True,
699 allow_metas
=args_meta
)
700 returns_meta
= ['union', 'struct']
701 if name
in returns_whitelist
:
702 returns_meta
+= ['built-in', 'alternate', 'enum']
703 check_type(info
, "'returns' for command '%s'" % name
,
704 expr
.get('returns'), allow_array
=True,
705 allow_optional
=True, allow_metas
=returns_meta
)
708 def check_event(expr
, info
):
710 boxed
= expr
.get('boxed', False)
714 meta
+= ['union', 'alternate']
715 check_type(info
, "'data' for event '%s'" % name
,
716 expr
.get('data'), allow_dict
=not boxed
, allow_optional
=True,
720 def check_union(expr
, info
):
722 base
= expr
.get('base')
723 discriminator
= expr
.get('discriminator')
724 members
= expr
['data']
726 # Two types of unions, determined by discriminator.
728 # With no discriminator it is a simple union.
729 if discriminator
is None:
731 allow_metas
= ['built-in', 'union', 'alternate', 'struct', 'enum']
733 raise QAPISemError(info
, "Simple union '%s' must not have a base" %
736 # Else, it's a flat union.
738 # The object must have a string or dictionary 'base'.
739 check_type(info
, "'base' for union '%s'" % name
,
740 base
, allow_dict
=True, allow_optional
=True,
741 allow_metas
=['struct'])
743 raise QAPISemError(info
, "Flat union '%s' must have a base"
745 base_members
= find_base_members(base
)
746 assert base_members
is not None
748 # The value of member 'discriminator' must name a non-optional
749 # member of the base struct.
750 check_name(info
, "Discriminator of flat union '%s'" % name
,
752 discriminator_type
= base_members
.get(discriminator
)
753 if not discriminator_type
:
754 raise QAPISemError(info
,
755 "Discriminator '%s' is not a member of base "
757 % (discriminator
, base
))
758 enum_define
= enum_types
.get(discriminator_type
)
759 allow_metas
= ['struct']
760 # Do not allow string discriminator
762 raise QAPISemError(info
,
763 "Discriminator '%s' must be of enumeration "
764 "type" % discriminator
)
766 # Check every branch; don't allow an empty union
767 if len(members
) == 0:
768 raise QAPISemError(info
, "Union '%s' cannot have empty 'data'" % name
)
769 for (key
, value
) in members
.items():
770 check_name(info
, "Member of union '%s'" % name
, key
)
772 # Each value must name a known type
773 check_type(info
, "Member '%s' of union '%s'" % (key
, name
),
774 value
, allow_array
=not base
, allow_metas
=allow_metas
)
776 # If the discriminator names an enum type, then all members
777 # of 'data' must also be members of the enum type.
779 if key
not in enum_define
['data']:
780 raise QAPISemError(info
,
781 "Discriminator value '%s' is not found in "
783 % (key
, enum_define
['enum']))
785 # If discriminator is user-defined, ensure all values are covered
787 for value
in enum_define
['data']:
788 if value
not in members
.keys():
789 raise QAPISemError(info
, "Union '%s' data missing '%s' branch"
793 def check_alternate(expr
, info
):
794 name
= expr
['alternate']
795 members
= expr
['data']
798 # Check every branch; require at least two branches
800 raise QAPISemError(info
,
801 "Alternate '%s' should have at least two branches "
803 for (key
, value
) in members
.items():
804 check_name(info
, "Member of alternate '%s'" % name
, key
)
806 # Ensure alternates have no type conflicts.
807 check_type(info
, "Member '%s' of alternate '%s'" % (key
, name
),
809 allow_metas
=['built-in', 'union', 'struct', 'enum'])
810 qtype
= find_alternate_member_qtype(value
)
812 raise QAPISemError(info
, "Alternate '%s' member '%s' cannot use "
813 "type '%s'" % (name
, key
, value
))
814 conflicting
= set([qtype
])
815 if qtype
== 'QTYPE_QSTRING':
816 enum_expr
= enum_types
.get(value
)
818 for v
in enum_expr
['data']:
819 if v
in ['on', 'off']:
820 conflicting
.add('QTYPE_QBOOL')
821 if re
.match(r
'[-+0-9.]', v
): # lazy, could be tightened
822 conflicting
.add('QTYPE_QNUM')
824 conflicting
.add('QTYPE_QNUM')
825 conflicting
.add('QTYPE_QBOOL')
826 for qt
in conflicting
:
828 raise QAPISemError(info
, "Alternate '%s' member '%s' can't "
829 "be distinguished from member '%s'"
830 % (name
, key
, types_seen
[qt
]))
834 def check_enum(expr
, info
):
836 members
= expr
.get('data')
837 prefix
= expr
.get('prefix')
839 if not isinstance(members
, list):
840 raise QAPISemError(info
,
841 "Enum '%s' requires an array for 'data'" % name
)
842 if prefix
is not None and not isinstance(prefix
, str):
843 raise QAPISemError(info
,
844 "Enum '%s' requires a string for 'prefix'" % name
)
845 for member
in members
:
846 check_name(info
, "Member of enum '%s'" % name
, member
,
850 def check_struct(expr
, info
):
851 name
= expr
['struct']
852 members
= expr
['data']
854 check_type(info
, "'data' for struct '%s'" % name
, members
,
855 allow_dict
=True, allow_optional
=True)
856 check_type(info
, "'base' for struct '%s'" % name
, expr
.get('base'),
857 allow_metas
=['struct'])
860 def check_keys(expr_elem
, meta
, required
, optional
=[]):
861 expr
= expr_elem
['expr']
862 info
= expr_elem
['info']
864 if not isinstance(name
, str):
865 raise QAPISemError(info
, "'%s' key must have a string value" % meta
)
866 required
= required
+ [meta
]
867 for (key
, value
) in expr
.items():
868 if key
not in required
and key
not in optional
:
869 raise QAPISemError(info
, "Unknown key '%s' in %s '%s'"
871 if (key
== 'gen' or key
== 'success-response') and value
is not False:
872 raise QAPISemError(info
,
873 "'%s' of %s '%s' should only use false value"
875 if key
== 'boxed' and value
is not True:
876 raise QAPISemError(info
,
877 "'%s' of %s '%s' should only use true value"
881 raise QAPISemError(info
, "Key '%s' is missing from %s '%s'"
885 def check_exprs(exprs
):
888 # Populate name table with names of built-in types
889 for builtin
in builtin_types
.keys():
890 all_names
[builtin
] = 'built-in'
892 # Learn the types and check for valid expression keys
893 for expr_elem
in exprs
:
894 expr
= expr_elem
['expr']
895 info
= expr_elem
['info']
896 doc
= expr_elem
.get('doc')
898 if 'include' in expr
:
901 if not doc
and doc_required
:
902 raise QAPISemError(info
,
903 "Expression missing documentation comment")
907 check_keys(expr_elem
, 'enum', ['data'], ['prefix'])
908 enum_types
[expr
[meta
]] = expr
909 elif 'union' in expr
:
911 check_keys(expr_elem
, 'union', ['data'],
912 ['base', 'discriminator'])
913 union_types
[expr
[meta
]] = expr
914 elif 'alternate' in expr
:
916 check_keys(expr_elem
, 'alternate', ['data'])
917 elif 'struct' in expr
:
919 check_keys(expr_elem
, 'struct', ['data'], ['base'])
920 struct_types
[expr
[meta
]] = expr
921 elif 'command' in expr
:
923 check_keys(expr_elem
, 'command', [],
924 ['data', 'returns', 'gen', 'success-response', 'boxed'])
925 elif 'event' in expr
:
927 check_keys(expr_elem
, 'event', [], ['data', 'boxed'])
929 raise QAPISemError(expr_elem
['info'],
930 "Expression is missing metatype")
932 add_name(name
, info
, meta
)
933 if doc
and doc
.symbol
!= name
:
934 raise QAPISemError(info
, "Definition of '%s' follows documentation"
935 " for '%s'" % (name
, doc
.symbol
))
937 # Try again for hidden UnionKind enum
938 for expr_elem
in exprs
:
939 expr
= expr_elem
['expr']
941 if 'include' in expr
:
943 if 'union' in expr
and not discriminator_find_enum_define(expr
):
944 name
= '%sKind' % expr
['union']
945 elif 'alternate' in expr
:
946 name
= '%sKind' % expr
['alternate']
949 enum_types
[name
] = {'enum': name
}
950 add_name(name
, info
, 'enum', implicit
=True)
952 # Validate that exprs make sense
953 for expr_elem
in exprs
:
954 expr
= expr_elem
['expr']
955 info
= expr_elem
['info']
956 doc
= expr_elem
.get('doc')
958 if 'include' in expr
:
961 check_enum(expr
, info
)
962 elif 'union' in expr
:
963 check_union(expr
, info
)
964 elif 'alternate' in expr
:
965 check_alternate(expr
, info
)
966 elif 'struct' in expr
:
967 check_struct(expr
, info
)
968 elif 'command' in expr
:
969 check_command(expr
, info
)
970 elif 'event' in expr
:
971 check_event(expr
, info
)
973 assert False, 'unexpected meta type'
982 # Schema compiler frontend
985 class QAPISchemaEntity(object):
986 def __init__(self
, name
, info
, doc
):
987 assert name
is None or isinstance(name
, str)
990 # For explicitly defined entities, info points to the (explicit)
991 # definition. For builtins (and their arrays), info is None.
992 # For implicitly defined entities, info points to a place that
993 # triggered the implicit definition (there may be more than one
999 return c_name(self
.name
)
1001 def check(self
, schema
):
1004 def is_implicit(self
):
1005 return not self
.info
1007 def visit(self
, visitor
):
1011 class QAPISchemaVisitor(object):
1012 def visit_begin(self
, schema
):
1015 def visit_end(self
):
1018 def visit_module(self
, fname
):
1021 def visit_needed(self
, entity
):
1022 # Default to visiting everything
1025 def visit_include(self
, fname
, info
):
1028 def visit_builtin_type(self
, name
, info
, json_type
):
1031 def visit_enum_type(self
, name
, info
, values
, prefix
):
1034 def visit_array_type(self
, name
, info
, element_type
):
1037 def visit_object_type(self
, name
, info
, base
, members
, variants
):
1040 def visit_object_type_flat(self
, name
, info
, members
, variants
):
1043 def visit_alternate_type(self
, name
, info
, variants
):
1046 def visit_command(self
, name
, info
, arg_type
, ret_type
,
1047 gen
, success_response
, boxed
):
1050 def visit_event(self
, name
, info
, arg_type
, boxed
):
1054 class QAPISchemaInclude(QAPISchemaEntity
):
1056 def __init__(self
, fname
, info
):
1057 QAPISchemaEntity
.__init
__(self
, None, info
, None)
1060 def visit(self
, visitor
):
1061 visitor
.visit_include(self
.fname
, self
.info
)
1064 class QAPISchemaType(QAPISchemaEntity
):
1065 # Return the C type for common use.
1066 # For the types we commonly box, this is a pointer type.
1070 # Return the C type to be used in a parameter list.
1071 def c_param_type(self
):
1072 return self
.c_type()
1074 # Return the C type to be used where we suppress boxing.
1075 def c_unboxed_type(self
):
1076 return self
.c_type()
1078 def json_type(self
):
1081 def alternate_qtype(self
):
1083 'null': 'QTYPE_QNULL',
1084 'string': 'QTYPE_QSTRING',
1085 'number': 'QTYPE_QNUM',
1086 'int': 'QTYPE_QNUM',
1087 'boolean': 'QTYPE_QBOOL',
1088 'object': 'QTYPE_QDICT'
1090 return json2qtype
.get(self
.json_type())
1093 if self
.is_implicit():
1098 class QAPISchemaBuiltinType(QAPISchemaType
):
1099 def __init__(self
, name
, json_type
, c_type
):
1100 QAPISchemaType
.__init
__(self
, name
, None, None)
1101 assert not c_type
or isinstance(c_type
, str)
1102 assert json_type
in ('string', 'number', 'int', 'boolean', 'null',
1104 self
._json
_type
_name
= json_type
1105 self
._c
_type
_name
= c_type
1111 return self
._c
_type
_name
1113 def c_param_type(self
):
1114 if self
.name
== 'str':
1115 return 'const ' + self
._c
_type
_name
1116 return self
._c
_type
_name
1118 def json_type(self
):
1119 return self
._json
_type
_name
1122 return self
.json_type()
1124 def visit(self
, visitor
):
1125 visitor
.visit_builtin_type(self
.name
, self
.info
, self
.json_type())
1128 class QAPISchemaEnumType(QAPISchemaType
):
1129 def __init__(self
, name
, info
, doc
, values
, prefix
):
1130 QAPISchemaType
.__init
__(self
, name
, info
, doc
)
1132 assert isinstance(v
, QAPISchemaMember
)
1134 assert prefix
is None or isinstance(prefix
, str)
1135 self
.values
= values
1136 self
.prefix
= prefix
1138 def check(self
, schema
):
1140 for v
in self
.values
:
1141 v
.check_clash(self
.info
, seen
)
1143 self
.doc
.connect_member(v
)
1145 def is_implicit(self
):
1146 # See QAPISchema._make_implicit_enum_type() and ._def_predefineds()
1147 return self
.name
.endswith('Kind') or self
.name
== 'QType'
1150 return c_name(self
.name
)
1152 def member_names(self
):
1153 return [v
.name
for v
in self
.values
]
1155 def json_type(self
):
1158 def visit(self
, visitor
):
1159 visitor
.visit_enum_type(self
.name
, self
.info
,
1160 self
.member_names(), self
.prefix
)
1163 class QAPISchemaArrayType(QAPISchemaType
):
1164 def __init__(self
, name
, info
, element_type
):
1165 QAPISchemaType
.__init
__(self
, name
, info
, None)
1166 assert isinstance(element_type
, str)
1167 self
._element
_type
_name
= element_type
1168 self
.element_type
= None
1170 def check(self
, schema
):
1171 self
.element_type
= schema
.lookup_type(self
._element
_type
_name
)
1172 assert self
.element_type
1174 def is_implicit(self
):
1178 return c_name(self
.name
) + pointer_suffix
1180 def json_type(self
):
1184 elt_doc_type
= self
.element_type
.doc_type()
1185 if not elt_doc_type
:
1187 return 'array of ' + elt_doc_type
1189 def visit(self
, visitor
):
1190 visitor
.visit_array_type(self
.name
, self
.info
, self
.element_type
)
1193 class QAPISchemaObjectType(QAPISchemaType
):
1194 def __init__(self
, name
, info
, doc
, base
, local_members
, variants
):
1195 # struct has local_members, optional base, and no variants
1196 # flat union has base, variants, and no local_members
1197 # simple union has local_members, variants, and no base
1198 QAPISchemaType
.__init
__(self
, name
, info
, doc
)
1199 assert base
is None or isinstance(base
, str)
1200 for m
in local_members
:
1201 assert isinstance(m
, QAPISchemaObjectTypeMember
)
1203 if variants
is not None:
1204 assert isinstance(variants
, QAPISchemaObjectTypeVariants
)
1205 variants
.set_owner(name
)
1206 self
._base
_name
= base
1208 self
.local_members
= local_members
1209 self
.variants
= variants
1212 def check(self
, schema
):
1213 if self
.members
is False: # check for cycles
1214 raise QAPISemError(self
.info
,
1215 "Object %s contains itself" % self
.name
)
1218 self
.members
= False # mark as being checked
1219 seen
= OrderedDict()
1221 self
.base
= schema
.lookup_type(self
._base
_name
)
1222 assert isinstance(self
.base
, QAPISchemaObjectType
)
1223 self
.base
.check(schema
)
1224 self
.base
.check_clash(self
.info
, seen
)
1225 for m
in self
.local_members
:
1227 m
.check_clash(self
.info
, seen
)
1229 self
.doc
.connect_member(m
)
1230 self
.members
= seen
.values()
1232 self
.variants
.check(schema
, seen
)
1233 assert self
.variants
.tag_member
in self
.members
1234 self
.variants
.check_clash(self
.info
, seen
)
1238 # Check that the members of this type do not cause duplicate JSON members,
1239 # and update seen to track the members seen so far. Report any errors
1240 # on behalf of info, which is not necessarily self.info
1241 def check_clash(self
, info
, seen
):
1242 assert not self
.variants
# not implemented
1243 for m
in self
.members
:
1244 m
.check_clash(info
, seen
)
1246 def is_implicit(self
):
1247 # See QAPISchema._make_implicit_object_type(), as well as
1248 # _def_predefineds()
1249 return self
.name
.startswith('q_')
1252 assert self
.members
is not None
1253 return not self
.members
and not self
.variants
1256 assert self
.name
!= 'q_empty'
1257 return QAPISchemaType
.c_name(self
)
1260 assert not self
.is_implicit()
1261 return c_name(self
.name
) + pointer_suffix
1263 def c_unboxed_type(self
):
1264 return c_name(self
.name
)
1266 def json_type(self
):
1269 def visit(self
, visitor
):
1270 visitor
.visit_object_type(self
.name
, self
.info
,
1271 self
.base
, self
.local_members
, self
.variants
)
1272 visitor
.visit_object_type_flat(self
.name
, self
.info
,
1273 self
.members
, self
.variants
)
1276 class QAPISchemaMember(object):
1279 def __init__(self
, name
):
1280 assert isinstance(name
, str)
1284 def set_owner(self
, name
):
1285 assert not self
.owner
1288 def check_clash(self
, info
, seen
):
1289 cname
= c_name(self
.name
)
1290 if cname
.lower() != cname
and self
.owner
not in name_case_whitelist
:
1291 raise QAPISemError(info
,
1292 "%s should not use uppercase" % self
.describe())
1294 raise QAPISemError(info
, "%s collides with %s" %
1295 (self
.describe(), seen
[cname
].describe()))
1298 def _pretty_owner(self
):
1300 if owner
.startswith('q_obj_'):
1301 # See QAPISchema._make_implicit_object_type() - reverse the
1302 # mapping there to create a nice human-readable description
1304 if owner
.endswith('-arg'):
1305 return '(parameter of %s)' % owner
[:-4]
1306 elif owner
.endswith('-base'):
1307 return '(base of %s)' % owner
[:-5]
1309 assert owner
.endswith('-wrapper')
1310 # Unreachable and not implemented
1312 if owner
.endswith('Kind'):
1313 # See QAPISchema._make_implicit_enum_type()
1314 return '(branch of %s)' % owner
[:-4]
1315 return '(%s of %s)' % (self
.role
, owner
)
1318 return "'%s' %s" % (self
.name
, self
._pretty
_owner
())
1321 class QAPISchemaObjectTypeMember(QAPISchemaMember
):
1322 def __init__(self
, name
, typ
, optional
):
1323 QAPISchemaMember
.__init
__(self
, name
)
1324 assert isinstance(typ
, str)
1325 assert isinstance(optional
, bool)
1326 self
._type
_name
= typ
1328 self
.optional
= optional
1330 def check(self
, schema
):
1332 self
.type = schema
.lookup_type(self
._type
_name
)
1336 class QAPISchemaObjectTypeVariants(object):
1337 def __init__(self
, tag_name
, tag_member
, variants
):
1338 # Flat unions pass tag_name but not tag_member.
1339 # Simple unions and alternates pass tag_member but not tag_name.
1340 # After check(), tag_member is always set, and tag_name remains
1341 # a reliable witness of being used by a flat union.
1342 assert bool(tag_member
) != bool(tag_name
)
1343 assert (isinstance(tag_name
, str) or
1344 isinstance(tag_member
, QAPISchemaObjectTypeMember
))
1345 assert len(variants
) > 0
1347 assert isinstance(v
, QAPISchemaObjectTypeVariant
)
1348 self
._tag
_name
= tag_name
1349 self
.tag_member
= tag_member
1350 self
.variants
= variants
1352 def set_owner(self
, name
):
1353 for v
in self
.variants
:
1356 def check(self
, schema
, seen
):
1357 if not self
.tag_member
: # flat union
1358 self
.tag_member
= seen
[c_name(self
._tag
_name
)]
1359 assert self
._tag
_name
== self
.tag_member
.name
1360 assert isinstance(self
.tag_member
.type, QAPISchemaEnumType
)
1361 for v
in self
.variants
:
1363 # Union names must match enum values; alternate names are
1364 # checked separately. Use 'seen' to tell the two apart.
1366 assert v
.name
in self
.tag_member
.type.member_names()
1367 assert isinstance(v
.type, QAPISchemaObjectType
)
1368 v
.type.check(schema
)
1370 def check_clash(self
, info
, seen
):
1371 for v
in self
.variants
:
1372 # Reset seen map for each variant, since qapi names from one
1373 # branch do not affect another branch
1374 assert isinstance(v
.type, QAPISchemaObjectType
)
1375 v
.type.check_clash(info
, dict(seen
))
1378 class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember
):
1381 def __init__(self
, name
, typ
):
1382 QAPISchemaObjectTypeMember
.__init
__(self
, name
, typ
, False)
1385 class QAPISchemaAlternateType(QAPISchemaType
):
1386 def __init__(self
, name
, info
, doc
, variants
):
1387 QAPISchemaType
.__init
__(self
, name
, info
, doc
)
1388 assert isinstance(variants
, QAPISchemaObjectTypeVariants
)
1389 assert variants
.tag_member
1390 variants
.set_owner(name
)
1391 variants
.tag_member
.set_owner(self
.name
)
1392 self
.variants
= variants
1394 def check(self
, schema
):
1395 self
.variants
.tag_member
.check(schema
)
1396 # Not calling self.variants.check_clash(), because there's nothing
1398 self
.variants
.check(schema
, {})
1399 # Alternate branch names have no relation to the tag enum values;
1400 # so we have to check for potential name collisions ourselves.
1402 for v
in self
.variants
.variants
:
1403 v
.check_clash(self
.info
, seen
)
1405 self
.doc
.connect_member(v
)
1410 return c_name(self
.name
) + pointer_suffix
1412 def json_type(self
):
1415 def visit(self
, visitor
):
1416 visitor
.visit_alternate_type(self
.name
, self
.info
, self
.variants
)
1422 class QAPISchemaCommand(QAPISchemaEntity
):
1423 def __init__(self
, name
, info
, doc
, arg_type
, ret_type
,
1424 gen
, success_response
, boxed
):
1425 QAPISchemaEntity
.__init
__(self
, name
, info
, doc
)
1426 assert not arg_type
or isinstance(arg_type
, str)
1427 assert not ret_type
or isinstance(ret_type
, str)
1428 self
._arg
_type
_name
= arg_type
1429 self
.arg_type
= None
1430 self
._ret
_type
_name
= ret_type
1431 self
.ret_type
= None
1433 self
.success_response
= success_response
1436 def check(self
, schema
):
1437 if self
._arg
_type
_name
:
1438 self
.arg_type
= schema
.lookup_type(self
._arg
_type
_name
)
1439 assert (isinstance(self
.arg_type
, QAPISchemaObjectType
) or
1440 isinstance(self
.arg_type
, QAPISchemaAlternateType
))
1441 self
.arg_type
.check(schema
)
1443 if self
.arg_type
.is_empty():
1444 raise QAPISemError(self
.info
,
1445 "Cannot use 'boxed' with empty type")
1447 assert not isinstance(self
.arg_type
, QAPISchemaAlternateType
)
1448 assert not self
.arg_type
.variants
1450 raise QAPISemError(self
.info
, "Use of 'boxed' requires 'data'")
1451 if self
._ret
_type
_name
:
1452 self
.ret_type
= schema
.lookup_type(self
._ret
_type
_name
)
1453 assert isinstance(self
.ret_type
, QAPISchemaType
)
1455 def visit(self
, visitor
):
1456 visitor
.visit_command(self
.name
, self
.info
,
1457 self
.arg_type
, self
.ret_type
,
1458 self
.gen
, self
.success_response
, self
.boxed
)
1461 class QAPISchemaEvent(QAPISchemaEntity
):
1462 def __init__(self
, name
, info
, doc
, arg_type
, boxed
):
1463 QAPISchemaEntity
.__init
__(self
, name
, info
, doc
)
1464 assert not arg_type
or isinstance(arg_type
, str)
1465 self
._arg
_type
_name
= arg_type
1466 self
.arg_type
= None
1469 def check(self
, schema
):
1470 if self
._arg
_type
_name
:
1471 self
.arg_type
= schema
.lookup_type(self
._arg
_type
_name
)
1472 assert (isinstance(self
.arg_type
, QAPISchemaObjectType
) or
1473 isinstance(self
.arg_type
, QAPISchemaAlternateType
))
1474 self
.arg_type
.check(schema
)
1476 if self
.arg_type
.is_empty():
1477 raise QAPISemError(self
.info
,
1478 "Cannot use 'boxed' with empty type")
1480 assert not isinstance(self
.arg_type
, QAPISchemaAlternateType
)
1481 assert not self
.arg_type
.variants
1483 raise QAPISemError(self
.info
, "Use of 'boxed' requires 'data'")
1485 def visit(self
, visitor
):
1486 visitor
.visit_event(self
.name
, self
.info
, self
.arg_type
, self
.boxed
)
1489 class QAPISchema(object):
1490 def __init__(self
, fname
):
1492 parser
= QAPISchemaParser(open(fname
, 'r'))
1493 exprs
= check_exprs(parser
.exprs
)
1494 self
.docs
= parser
.docs
1495 self
._entity
_list
= []
1496 self
._entity
_dict
= {}
1497 self
._predefining
= True
1498 self
._def
_predefineds
()
1499 self
._predefining
= False
1500 self
._def
_exprs
(exprs
)
1503 def _def_entity(self
, ent
):
1504 # Only the predefined types are allowed to not have info
1505 assert ent
.info
or self
._predefining
1506 assert ent
.name
is None or ent
.name
not in self
._entity
_dict
1507 self
._entity
_list
.append(ent
)
1508 if ent
.name
is not None:
1509 self
._entity
_dict
[ent
.name
] = ent
1511 ent
.module
= os
.path
.relpath(ent
.info
['file'],
1512 os
.path
.dirname(self
._fname
))
1514 def lookup_entity(self
, name
, typ
=None):
1515 ent
= self
._entity
_dict
.get(name
)
1516 if typ
and not isinstance(ent
, typ
):
1520 def lookup_type(self
, name
):
1521 return self
.lookup_entity(name
, QAPISchemaType
)
1523 def _def_include(self
, expr
, info
, doc
):
1524 include
= expr
['include']
1527 while main_info
['parent']:
1528 main_info
= main_info
['parent']
1529 fname
= os
.path
.relpath(include
, os
.path
.dirname(main_info
['file']))
1530 self
._def
_entity
(QAPISchemaInclude(fname
, info
))
1532 def _def_builtin_type(self
, name
, json_type
, c_type
):
1533 self
._def
_entity
(QAPISchemaBuiltinType(name
, json_type
, c_type
))
1534 # Instantiating only the arrays that are actually used would
1535 # be nice, but we can't as long as their generated code
1536 # (qapi-builtin-types.[ch]) may be shared by some other
1538 self
._make
_array
_type
(name
, None)
1540 def _def_predefineds(self
):
1541 for t
in [('str', 'string', 'char' + pointer_suffix
),
1542 ('number', 'number', 'double'),
1543 ('int', 'int', 'int64_t'),
1544 ('int8', 'int', 'int8_t'),
1545 ('int16', 'int', 'int16_t'),
1546 ('int32', 'int', 'int32_t'),
1547 ('int64', 'int', 'int64_t'),
1548 ('uint8', 'int', 'uint8_t'),
1549 ('uint16', 'int', 'uint16_t'),
1550 ('uint32', 'int', 'uint32_t'),
1551 ('uint64', 'int', 'uint64_t'),
1552 ('size', 'int', 'uint64_t'),
1553 ('bool', 'boolean', 'bool'),
1554 ('any', 'value', 'QObject' + pointer_suffix
),
1555 ('null', 'null', 'QNull' + pointer_suffix
)]:
1556 self
._def
_builtin
_type
(*t
)
1557 self
.the_empty_object_type
= QAPISchemaObjectType(
1558 'q_empty', None, None, None, [], None)
1559 self
._def
_entity
(self
.the_empty_object_type
)
1560 qtype_values
= self
._make
_enum
_members
(['none', 'qnull', 'qnum',
1561 'qstring', 'qdict', 'qlist',
1563 self
._def
_entity
(QAPISchemaEnumType('QType', None, None,
1564 qtype_values
, 'QTYPE'))
1566 def _make_enum_members(self
, values
):
1567 return [QAPISchemaMember(v
) for v
in values
]
1569 def _make_implicit_enum_type(self
, name
, info
, values
):
1570 # See also QAPISchemaObjectTypeMember._pretty_owner()
1571 name
= name
+ 'Kind' # Use namespace reserved by add_name()
1572 self
._def
_entity
(QAPISchemaEnumType(
1573 name
, info
, None, self
._make
_enum
_members
(values
), None))
1576 def _make_array_type(self
, element_type
, info
):
1577 name
= element_type
+ 'List' # Use namespace reserved by add_name()
1578 if not self
.lookup_type(name
):
1579 self
._def
_entity
(QAPISchemaArrayType(name
, info
, element_type
))
1582 def _make_implicit_object_type(self
, name
, info
, doc
, role
, members
):
1585 # See also QAPISchemaObjectTypeMember._pretty_owner()
1586 name
= 'q_obj_%s-%s' % (name
, role
)
1587 if not self
.lookup_entity(name
, QAPISchemaObjectType
):
1588 self
._def
_entity
(QAPISchemaObjectType(name
, info
, doc
, None,
1592 def _def_enum_type(self
, expr
, info
, doc
):
1595 prefix
= expr
.get('prefix')
1596 self
._def
_entity
(QAPISchemaEnumType(
1597 name
, info
, doc
, self
._make
_enum
_members
(data
), prefix
))
1599 def _make_member(self
, name
, typ
, info
):
1601 if name
.startswith('*'):
1604 if isinstance(typ
, list):
1605 assert len(typ
) == 1
1606 typ
= self
._make
_array
_type
(typ
[0], info
)
1607 return QAPISchemaObjectTypeMember(name
, typ
, optional
)
1609 def _make_members(self
, data
, info
):
1610 return [self
._make
_member
(key
, value
, info
)
1611 for (key
, value
) in data
.items()]
1613 def _def_struct_type(self
, expr
, info
, doc
):
1614 name
= expr
['struct']
1615 base
= expr
.get('base')
1617 self
._def
_entity
(QAPISchemaObjectType(name
, info
, doc
, base
,
1618 self
._make
_members
(data
, info
),
1621 def _make_variant(self
, case
, typ
):
1622 return QAPISchemaObjectTypeVariant(case
, typ
)
1624 def _make_simple_variant(self
, case
, typ
, info
):
1625 if isinstance(typ
, list):
1626 assert len(typ
) == 1
1627 typ
= self
._make
_array
_type
(typ
[0], info
)
1628 typ
= self
._make
_implicit
_object
_type
(
1629 typ
, info
, None, 'wrapper', [self
._make
_member
('data', typ
, info
)])
1630 return QAPISchemaObjectTypeVariant(case
, typ
)
1632 def _def_union_type(self
, expr
, info
, doc
):
1633 name
= expr
['union']
1635 base
= expr
.get('base')
1636 tag_name
= expr
.get('discriminator')
1638 if isinstance(base
, dict):
1639 base
= (self
._make
_implicit
_object
_type
(
1640 name
, info
, doc
, 'base', self
._make
_members
(base
, info
)))
1642 variants
= [self
._make
_variant
(key
, value
)
1643 for (key
, value
) in data
.items()]
1646 variants
= [self
._make
_simple
_variant
(key
, value
, info
)
1647 for (key
, value
) in data
.items()]
1648 typ
= self
._make
_implicit
_enum
_type
(name
, info
,
1649 [v
.name
for v
in variants
])
1650 tag_member
= QAPISchemaObjectTypeMember('type', typ
, False)
1651 members
= [tag_member
]
1653 QAPISchemaObjectType(name
, info
, doc
, base
, members
,
1654 QAPISchemaObjectTypeVariants(tag_name
,
1658 def _def_alternate_type(self
, expr
, info
, doc
):
1659 name
= expr
['alternate']
1661 variants
= [self
._make
_variant
(key
, value
)
1662 for (key
, value
) in data
.items()]
1663 tag_member
= QAPISchemaObjectTypeMember('type', 'QType', False)
1665 QAPISchemaAlternateType(name
, info
, doc
,
1666 QAPISchemaObjectTypeVariants(None,
1670 def _def_command(self
, expr
, info
, doc
):
1671 name
= expr
['command']
1672 data
= expr
.get('data')
1673 rets
= expr
.get('returns')
1674 gen
= expr
.get('gen', True)
1675 success_response
= expr
.get('success-response', True)
1676 boxed
= expr
.get('boxed', False)
1677 if isinstance(data
, OrderedDict
):
1678 data
= self
._make
_implicit
_object
_type
(
1679 name
, info
, doc
, 'arg', self
._make
_members
(data
, info
))
1680 if isinstance(rets
, list):
1681 assert len(rets
) == 1
1682 rets
= self
._make
_array
_type
(rets
[0], info
)
1683 self
._def
_entity
(QAPISchemaCommand(name
, info
, doc
, data
, rets
,
1684 gen
, success_response
, boxed
))
1686 def _def_event(self
, expr
, info
, doc
):
1687 name
= expr
['event']
1688 data
= expr
.get('data')
1689 boxed
= expr
.get('boxed', False)
1690 if isinstance(data
, OrderedDict
):
1691 data
= self
._make
_implicit
_object
_type
(
1692 name
, info
, doc
, 'arg', self
._make
_members
(data
, info
))
1693 self
._def
_entity
(QAPISchemaEvent(name
, info
, doc
, data
, boxed
))
1695 def _def_exprs(self
, exprs
):
1696 for expr_elem
in exprs
:
1697 expr
= expr_elem
['expr']
1698 info
= expr_elem
['info']
1699 doc
= expr_elem
.get('doc')
1701 self
._def
_enum
_type
(expr
, info
, doc
)
1702 elif 'struct' in expr
:
1703 self
._def
_struct
_type
(expr
, info
, doc
)
1704 elif 'union' in expr
:
1705 self
._def
_union
_type
(expr
, info
, doc
)
1706 elif 'alternate' in expr
:
1707 self
._def
_alternate
_type
(expr
, info
, doc
)
1708 elif 'command' in expr
:
1709 self
._def
_command
(expr
, info
, doc
)
1710 elif 'event' in expr
:
1711 self
._def
_event
(expr
, info
, doc
)
1712 elif 'include' in expr
:
1713 self
._def
_include
(expr
, info
, doc
)
1718 for ent
in self
._entity
_list
:
1721 def visit(self
, visitor
):
1722 visitor
.visit_begin(self
)
1724 for entity
in self
._entity
_list
:
1725 if visitor
.visit_needed(entity
):
1726 if entity
.module
!= module
:
1727 module
= entity
.module
1728 visitor
.visit_module(module
)
1729 entity
.visit(visitor
)
1734 # Code generation helpers
1737 def camel_case(name
):
1741 if ch
in ['_', '-']:
1744 new_name
+= ch
.upper()
1747 new_name
+= ch
.lower()
1751 # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
1752 # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
1753 # ENUM24_Name -> ENUM24_NAME
1754 def camel_to_upper(value
):
1755 c_fun_str
= c_name(value
, False)
1763 # When c is upper and no '_' appears before, do more checks
1764 if c
.isupper() and (i
> 0) and c_fun_str
[i
- 1] != '_':
1765 if i
< l
- 1 and c_fun_str
[i
+ 1].islower():
1767 elif c_fun_str
[i
- 1].isdigit():
1770 return new_name
.lstrip('_').upper()
1773 def c_enum_const(type_name
, const_name
, prefix
=None):
1774 if prefix
is not None:
1776 return camel_to_upper(type_name
) + '_' + c_name(const_name
, False).upper()
1778 # Temporary HACK for '/':
1779 if hasattr(str, 'maketrans'):
1780 c_name_trans
= str.maketrans('.-/', '___')
1782 c_name_trans
= string
.maketrans('.-/', '___')
1785 # Map @name to a valid C identifier.
1786 # If @protect, avoid returning certain ticklish identifiers (like
1787 # C keywords) by prepending 'q_'.
1789 # Used for converting 'name' from a 'name':'type' qapi definition
1790 # into a generated struct member, as well as converting type names
1791 # into substrings of a generated C function name.
1792 # '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
1793 # protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
1794 def c_name(name
, protect
=True):
1795 # ANSI X3J11/88-090, 3.1.1
1796 c89_words
= set(['auto', 'break', 'case', 'char', 'const', 'continue',
1797 'default', 'do', 'double', 'else', 'enum', 'extern',
1798 'float', 'for', 'goto', 'if', 'int', 'long', 'register',
1799 'return', 'short', 'signed', 'sizeof', 'static',
1800 'struct', 'switch', 'typedef', 'union', 'unsigned',
1801 'void', 'volatile', 'while'])
1802 # ISO/IEC 9899:1999, 6.4.1
1803 c99_words
= set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
1804 # ISO/IEC 9899:2011, 6.4.1
1805 c11_words
= set(['_Alignas', '_Alignof', '_Atomic', '_Generic',
1806 '_Noreturn', '_Static_assert', '_Thread_local'])
1807 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
1809 gcc_words
= set(['asm', 'typeof'])
1810 # C++ ISO/IEC 14882:2003 2.11
1811 cpp_words
= set(['bool', 'catch', 'class', 'const_cast', 'delete',
1812 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
1813 'namespace', 'new', 'operator', 'private', 'protected',
1814 'public', 'reinterpret_cast', 'static_cast', 'template',
1815 'this', 'throw', 'true', 'try', 'typeid', 'typename',
1816 'using', 'virtual', 'wchar_t',
1817 # alternative representations
1818 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
1819 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
1820 # namespace pollution:
1821 polluted_words
= set(['unix', 'errno', 'mips', 'sparc'])
1822 name
= name
.translate(c_name_trans
)
1823 if protect
and (name
in c89_words | c99_words | c11_words | gcc_words
1824 | cpp_words | polluted_words
):
1828 eatspace
= '\033EATSPACE.'
1829 pointer_suffix
= ' *' + eatspace
1832 def genindent(count
):
1834 for _
in range(count
):
1841 def push_indent(indent_amount
=4):
1843 indent_level
+= indent_amount
1846 def pop_indent(indent_amount
=4):
1848 indent_level
-= indent_amount
1851 # Generate @code with @kwds interpolated.
1852 # Obey indent_level, and strip eatspace.
1853 def cgen(code
, **kwds
):
1856 indent
= genindent(indent_level
)
1857 # re.subn() lacks flags support before Python 2.7, use re.compile()
1858 raw
= re
.subn(re
.compile(r
'^.', re
.MULTILINE
),
1859 indent
+ r
'\g<0>', raw
)
1861 return re
.sub(re
.escape(eatspace
) + r
' *', '', raw
)
1864 def mcgen(code
, **kwds
):
1867 return cgen(code
, **kwds
)
1870 def guardname(filename
):
1871 return re
.sub(r
'[^A-Za-z0-9_]', '_', filename
).upper()
1874 def guardstart(name
):
1880 name
=guardname(name
))
1886 #endif /* %(name)s */
1888 name
=guardname(name
))
1891 def gen_enum_lookup(name
, values
, prefix
=None):
1894 const QEnumLookup %(c_name)s_lookup = {
1895 .array = (const char *const[]) {
1897 c_name
=c_name(name
))
1898 for value
in values
:
1899 index
= c_enum_const(name
, value
, prefix
)
1901 [%(index)s] = "%(value)s",
1903 index
=index
, value
=value
)
1907 .size = %(max_index)s
1910 max_index
=c_enum_const(name
, '_MAX', prefix
))
1914 def gen_enum(name
, values
, prefix
=None):
1915 # append automatically generated _MAX value
1916 enum_values
= values
+ ['_MAX']
1920 typedef enum %(c_name)s {
1922 c_name
=c_name(name
))
1925 for value
in enum_values
:
1929 c_enum
=c_enum_const(name
, value
, prefix
),
1936 c_name
=c_name(name
))
1940 #define %(c_name)s_str(val) \\
1941 qapi_enum_lookup(&%(c_name)s_lookup, (val))
1943 extern const QEnumLookup %(c_name)s_lookup;
1945 c_name
=c_name(name
))
1949 def build_params(arg_type
, boxed
, extra
):
1956 ret
+= '%s arg' % arg_type
.c_param_type()
1959 assert not arg_type
.variants
1960 for memb
in arg_type
.members
:
1964 ret
+= 'bool has_%s, ' % c_name(memb
.name
)
1965 ret
+= '%s %s' % (memb
.type.c_param_type(),
1973 # Accumulate and write output
1976 class QAPIGen(object):
1982 def preamble_add(self
, text
):
1983 self
._preamble
+= text
1985 def add(self
, text
):
1988 def _top(self
, fname
):
1991 def _bottom(self
, fname
):
1994 def write(self
, output_dir
, fname
):
1995 pathname
= os
.path
.join(output_dir
, fname
)
1996 dir = os
.path
.dirname(pathname
)
2000 except os
.error
as e
:
2001 if e
.errno
!= errno
.EEXIST
:
2003 fd
= os
.open(pathname
, os
.O_RDWR | os
.O_CREAT
, 0o666)
2004 f
= os
.fdopen(fd
, 'r+')
2005 text
= (self
._top
(fname
) + self
._preamble
+ self
._body
2006 + self
._bottom
(fname
))
2007 oldtext
= f
.read(len(text
) + 1)
2015 class QAPIGenC(QAPIGen
):
2017 def __init__(self
, blurb
, pydoc
):
2018 QAPIGen
.__init
__(self
)
2020 self
._copyright
= '\n * '.join(re
.findall(r
'^Copyright .*', pydoc
,
2023 def _top(self
, fname
):
2025 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
2032 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
2033 * See the COPYING.LIB file in the top-level directory.
2037 blurb
=self
._blurb
, copyright
=self
._copyright
)
2039 def _bottom(self
, fname
):
2041 /* Dummy declaration to prevent empty .o file */
2042 char dummy_%(name)s;
2047 class QAPIGenH(QAPIGenC
):
2049 def _top(self
, fname
):
2050 return QAPIGenC
._top
(self
, fname
) + guardstart(fname
)
2052 def _bottom(self
, fname
):
2053 return guardend(fname
)
2056 class QAPIGenDoc(QAPIGen
):
2058 def _top(self
, fname
):
2059 return (QAPIGen
._top
(self
, fname
)
2060 + '@c AUTOMATICALLY GENERATED, DO NOT MODIFY\n\n')
2063 class QAPISchemaMonolithicCVisitor(QAPISchemaVisitor
):
2065 def __init__(self
, prefix
, what
, blurb
, pydoc
):
2066 self
._prefix
= prefix
2068 self
._genc
= QAPIGenC(blurb
, pydoc
)
2069 self
._genh
= QAPIGenH(blurb
, pydoc
)
2071 def write(self
, output_dir
):
2072 self
._genc
.write(output_dir
, self
._prefix
+ self
._what
+ '.c')
2073 self
._genh
.write(output_dir
, self
._prefix
+ self
._what
+ '.h')
2076 class QAPISchemaModularCVisitor(QAPISchemaVisitor
):
2078 def __init__(self
, prefix
, what
, blurb
, pydoc
):
2079 self
._prefix
= prefix
2084 self
._main
_module
= None
2086 def _module_basename(self
, what
, name
):
2088 return re
.sub(r
'-', '-builtin-', what
)
2089 basename
= os
.path
.join(os
.path
.dirname(name
),
2090 self
._prefix
+ what
)
2091 if name
== self
._main
_module
:
2093 return basename
+ '-' + os
.path
.splitext(os
.path
.basename(name
))[0]
2095 def _add_module(self
, name
, blurb
):
2096 if self
._main
_module
is None and name
is not None:
2097 self
._main
_module
= name
2098 genc
= QAPIGenC(blurb
, self
._pydoc
)
2099 genh
= QAPIGenH(blurb
, self
._pydoc
)
2100 self
._module
[name
] = (genc
, genh
)
2101 self
._set
_module
(name
)
2103 def _set_module(self
, name
):
2104 self
._genc
, self
._genh
= self
._module
[name
]
2106 def write(self
, output_dir
, opt_builtins
=False):
2107 for name
in self
._module
:
2108 if name
is None and not opt_builtins
:
2110 basename
= self
._module
_basename
(self
._what
, name
)
2111 (genc
, genh
) = self
._module
[name
]
2112 genc
.write(output_dir
, basename
+ '.c')
2113 genh
.write(output_dir
, basename
+ '.h')
2115 def _begin_module(self
, name
):
2118 def visit_module(self
, name
):
2119 if name
in self
._module
:
2120 self
._set
_module
(name
)
2122 self
._add
_module
(name
, self
._blurb
)
2123 self
._begin
_module
(name
)
2125 def visit_include(self
, name
, info
):
2126 basename
= self
._module
_basename
(self
._what
, name
)
2127 self
._genh
.preamble_add(mcgen('''
2128 #include "%(basename)s.h"