diff options
author | Peter Maydell <peter.maydell@linaro.org> | 2021-08-26 13:42:34 +0100 |
---|---|---|
committer | Peter Maydell <peter.maydell@linaro.org> | 2021-08-26 13:42:34 +0100 |
commit | c83fcfaf8a54d0d034bd0edf7bbb3b0d16669be9 (patch) | |
tree | c25238aa2e85e53975e34765616fb0a3976bf2f0 /scripts/qapi/common.py | |
parent | 0a9be955459f21968516f5b220822f7350a3d3c1 (diff) | |
parent | 8a9f1e1d9cc55f5eb0946cbf8fd1ef9a0e7d3dac (diff) | |
download | qemu-c83fcfaf8a54d0d034bd0edf7bbb3b0d16669be9.zip qemu-c83fcfaf8a54d0d034bd0edf7bbb3b0d16669be9.tar.gz qemu-c83fcfaf8a54d0d034bd0edf7bbb3b0d16669be9.tar.bz2 |
Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2021-08-26' into staging
QAPI patches patches for 2021-08-26
# gpg: Signature made Thu 26 Aug 2021 13:18:34 BST
# gpg: using RSA key 354BC8B3D7EB2A6B68674E5F3870B400EB918653
# gpg: issuer "armbru@redhat.com"
# gpg: Good signature from "Markus Armbruster <armbru@redhat.com>" [full]
# gpg: aka "Markus Armbruster <armbru@pond.sub.org>" [full]
# Primary key fingerprint: 354B C8B3 D7EB 2A6B 6867 4E5F 3870 B400 EB91 8653
* remotes/armbru/tags/pull-qapi-2021-08-26:
qapi: make 'if' condition strings simple identifiers
qapi: add 'not' condition operation
qapi: Use 'if': { 'any': ... } where appropriate
qapi: add 'any' condition
qapi: replace if condition list with dict {'all': [...]}
qapidoc: introduce QAPISchemaIfCond.docgen()
qapi: introduce QAPISchemaIfCond.cgen()
qapi: add QAPISchemaIfCond.is_present()
qapi: wrap Sequence[str] in an object
docs: update the documentation upfront about schema configuration
qapi: Fix crash on redefinition with a different condition
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'scripts/qapi/common.py')
-rw-r--r-- | scripts/qapi/common.py | 59 |
1 files changed, 46 insertions, 13 deletions
diff --git a/scripts/qapi/common.py b/scripts/qapi/common.py index 6ad1eeb..1724ac3 100644 --- a/scripts/qapi/common.py +++ b/scripts/qapi/common.py @@ -12,7 +12,13 @@ # See the COPYING file in the top-level directory. import re -from typing import Match, Optional, Sequence +from typing import ( + Any, + Dict, + Match, + Optional, + Union, +) #: Magic string that gets removed along with all space to its right. @@ -194,22 +200,49 @@ def guardend(name: str) -> str: name=c_fname(name).upper()) -def gen_if(ifcond: Sequence[str]) -> str: - ret = '' - for ifc in ifcond: - ret += mcgen(''' +def cgen_ifcond(ifcond: Union[str, Dict[str, Any]]) -> str: + if not ifcond: + return '' + if isinstance(ifcond, str): + return 'defined(' + ifcond + ')' + + oper, operands = next(iter(ifcond.items())) + if oper == 'not': + return '!' + cgen_ifcond(operands) + oper = {'all': '&&', 'any': '||'}[oper] + operands = [cgen_ifcond(o) for o in operands] + return '(' + (') ' + oper + ' (').join(operands) + ')' + + +def docgen_ifcond(ifcond: Union[str, Dict[str, Any]]) -> str: + # TODO Doc generated for conditions needs polish + if not ifcond: + return '' + if isinstance(ifcond, str): + return ifcond + + oper, operands = next(iter(ifcond.items())) + if oper == 'not': + return '!' + docgen_ifcond(operands) + oper = {'all': ' and ', 'any': ' or '}[oper] + operands = [docgen_ifcond(o) for o in operands] + return '(' + oper.join(operands) + ')' + + +def gen_if(cond: str) -> str: + if not cond: + return '' + return mcgen(''' #if %(cond)s -''', cond=ifc) - return ret +''', cond=cond) -def gen_endif(ifcond: Sequence[str]) -> str: - ret = '' - for ifc in reversed(ifcond): - ret += mcgen(''' +def gen_endif(cond: str) -> str: + if not cond: + return '' + return mcgen(''' #endif /* %(cond)s */ -''', cond=ifc) - return ret +''', cond=cond) def must_match(pattern: str, string: str) -> Match[str]: |