| 1 | """ |
| 2 | QAPI features generator |
| 3 | |
| 4 | Copyright 2024 Red Hat |
| 5 | |
| 6 | This work is licensed under the terms of the GNU GPL, version 2. |
| 7 | # See the COPYING file in the top-level directory. |
| 8 | """ |
| 9 | |
| 10 | from typing import ValuesView |
| 11 | |
| 12 | from .common import c_enum_const, c_name |
| 13 | from .gen import QAPISchemaMonolithicCVisitor |
| 14 | from .schema import QAPISchema, QAPISchemaFeature |
| 15 | |
| 16 | |
| 17 | class 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 | |
| 43 | def 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) |