From b8e2f8a4d78c5e544fd62ed4d8327e1f0639c769 Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Sun, 2 Mar 2025 12:52:14 +0000 Subject: [PATCH] Add support for forward declarations --- codegen/datatypes.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/codegen/datatypes.py b/codegen/datatypes.py index 8aef6b8..9dbbcfd 100644 --- a/codegen/datatypes.py +++ b/codegen/datatypes.py @@ -190,6 +190,7 @@ class CInclude: class CFile: name: str extension: str + decl_types: list[CStruct] = field(default_factory=list) def save(self, output_dir: Path): output_file = output_dir / f"{self.name}.{self.extension}" @@ -212,6 +213,12 @@ class CHeader(CFile): includes = _get_includes_string(self.includes) + forward_declarations = "" + for _type in self.decl_types: + forward_declarations += _type.declare() + if len(forward_declarations) > 0: + forward_declarations += "\n" + types = "" for _type in self.types: types += str(_type) + "\n" @@ -220,7 +227,7 @@ class CHeader(CFile): for func in self.funcs: funcs += func.declare() - return pragma + includes + types + funcs + return pragma + includes + forward_declarations + types + funcs @dataclass @@ -234,6 +241,12 @@ class CSource(CFile): def __str__(self) -> str: includes = _get_includes_string(self.includes) + forward_declarations = "" + for _type in self.decl_types: + forward_declarations += _type.declare() + if len(forward_declarations) > 0: + forward_declarations += "\n" + types = "" for _type in self.types: types += str(_type) + "\n" @@ -251,7 +264,7 @@ class CSource(CFile): for func in self.funcs: funcs += func.define() - return includes + types + internal_funcs_decl + funcs + internal_funcs_def + return includes + forward_declarations + types + internal_funcs_decl + funcs + internal_funcs_def def get_datatype_string(_type: CDataType) -> str: