features.py 1.4 KB

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