xref: /qemu/scripts/qapi/features.py (revision 3228d311ab1882f75b04d080d33a71fc7a0bcac5)
1"""
2QAPI features generator
3
4Copyright 2024 Red Hat
5
6This work is licensed under the terms of the GNU GPL, version 2.
7# See the COPYING file in the top-level directory.
8"""
9
10from typing import ValuesView
11
12from .common import c_enum_const, c_name
13from .gen import QAPISchemaMonolithicCVisitor
14from .schema import QAPISchema, QAPISchemaFeature
15
16
17class QAPISchemaGenFeatureVisitor(QAPISchemaMonolithicCVisitor):
18
19    def __init__(self, prefix: str):
20        super().__init__(
21            prefix, 'qapi-features',
22            ' * Schema-defined QAPI features',
23            __doc__)
24
25        self.features: ValuesView[QAPISchemaFeature]
26
27    def visit_begin(self, schema: QAPISchema) -> None:
28        self.features = schema.features()
29        self._genh.add("#include \"qapi/util.h\"\n\n")
30
31    def visit_end(self) -> None:
32        self._genh.add("typedef enum {\n")
33        for f in self.features:
34            self._genh.add(f"    {c_enum_const('qapi_feature', f.name)}")
35            if f.name in QAPISchemaFeature.SPECIAL_NAMES:
36                self._genh.add(f" = {c_enum_const('qapi', f.name)},\n")
37            else:
38                self._genh.add(",\n")
39
40        self._genh.add("} " + c_name('QapiFeature') + ";\n")
41
42
43def gen_features(schema: QAPISchema,
44                 output_dir: str,
45                 prefix: str) -> None:
46    vis = QAPISchemaGenFeatureVisitor(prefix)
47    schema.visit(vis)
48    vis.write(output_dir)
49