Skip to content

Commit de41522

Browse files
committed
refactor(visitor): simplify _handle_struct logic and unify type handling mechanism
1 parent 481a028 commit de41522

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

src/dwarf2cpp/visitor.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import posixpath
44
import struct
5+
import typing
56
from collections import defaultdict
67
from typing import Any, Callable, Generator
78

@@ -292,14 +293,7 @@ def visit_typedef(self, die: DWARFDie) -> None:
292293
self._set(die, typedef)
293294

294295
def visit_class_type(self, die: DWARFDie) -> None:
295-
if die.find("DW_AT_signature"):
296-
sig = die.resolve_type_unit_reference()
297-
self.visit(sig)
298-
cls = copy.copy(self._get(sig))
299-
else:
300-
cls = Class(name=die.short_name)
301-
302-
self._handle_struct(die, cls)
296+
self._handle_struct(die, Class)
303297

304298
def visit_enumeration_type(self, die: DWARFDie) -> None:
305299
if die.find("DW_AT_signature"):
@@ -343,24 +337,10 @@ def visit_enumeration_type(self, die: DWARFDie) -> None:
343337
self._set(die, enum)
344338

345339
def visit_union_type(self, die: DWARFDie) -> None:
346-
if die.find("DW_AT_signature"):
347-
sig = die.resolve_type_unit_reference()
348-
self.visit(sig)
349-
union = copy.copy(self._get(sig))
350-
else:
351-
union = Union(name=die.short_name)
352-
353-
self._handle_struct(die, union)
340+
self._handle_struct(die, Union)
354341

355342
def visit_structure_type(self, die: DWARFDie) -> None:
356-
if die.find("DW_AT_signature"):
357-
sig = die.resolve_type_unit_reference()
358-
self.visit(sig)
359-
struct = copy.copy(self._get(sig))
360-
else:
361-
struct = Struct(name=die.short_name)
362-
363-
self._handle_struct(die, struct)
343+
self._handle_struct(die, Struct)
364344

365345
def visit_variable(self, die: DWARFDie) -> None:
366346
self._handle_attribute(die)
@@ -828,7 +808,17 @@ def _handle_attribute(self, die: DWARFDie) -> None:
828808

829809
self._set(die, variable)
830810

831-
def _handle_struct(self, die: DWARFDie, struct: Struct) -> None:
811+
def _handle_struct(self, die: DWARFDie, ty: typing.Type[Class | Struct | Union]) -> None:
812+
signature = die.find("DW_AT_signature")
813+
if signature is not None:
814+
signature = die.resolve_type_unit_reference()
815+
self.visit(signature)
816+
declaration = self._get(signature)
817+
assert declaration is not None, "Expected valid declaration"
818+
struct = copy.deepcopy(declaration)
819+
else:
820+
struct: Class | Struct | Union = ty(name=die.short_name)
821+
832822
class_name = struct.name.split("<", maxsplit=1)[0] if struct.name else None
833823
access = AccessAttribute.PRIVATE if isinstance(struct, Class) else AccessAttribute.PUBLIC
834824

0 commit comments

Comments
 (0)