Add support for forward declarations

This commit is contained in:
Abdelrahman Said 2025-03-02 12:52:14 +00:00
parent a9791962b0
commit b8e2f8a4d7

View File

@ -190,6 +190,7 @@ class CInclude:
class CFile: class CFile:
name: str name: str
extension: str extension: str
decl_types: list[CStruct] = field(default_factory=list)
def save(self, output_dir: Path): def save(self, output_dir: Path):
output_file = output_dir / f"{self.name}.{self.extension}" output_file = output_dir / f"{self.name}.{self.extension}"
@ -212,6 +213,12 @@ class CHeader(CFile):
includes = _get_includes_string(self.includes) 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 = "" types = ""
for _type in self.types: for _type in self.types:
types += str(_type) + "\n" types += str(_type) + "\n"
@ -220,7 +227,7 @@ class CHeader(CFile):
for func in self.funcs: for func in self.funcs:
funcs += func.declare() funcs += func.declare()
return pragma + includes + types + funcs return pragma + includes + forward_declarations + types + funcs
@dataclass @dataclass
@ -234,6 +241,12 @@ class CSource(CFile):
def __str__(self) -> str: def __str__(self) -> str:
includes = _get_includes_string(self.includes) 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 = "" types = ""
for _type in self.types: for _type in self.types:
types += str(_type) + "\n" types += str(_type) + "\n"
@ -251,7 +264,7 @@ class CSource(CFile):
for func in self.funcs: for func in self.funcs:
funcs += func.define() 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: def get_datatype_string(_type: CDataType) -> str: