Skip to content

Commit 34df056

Browse files
committed
refactor(visitor): add relative scope support to type resolution
1 parent a275e23 commit 34df056

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/dwarf2cpp/visitor.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040

4141
def scoped_tags(tag: str) -> bool:
42+
"""Return True if the DIE is a C++ scope component"""
4243
return tag in {
4344
"DW_TAG_structure_type",
4445
"DW_TAG_class_type",
@@ -50,23 +51,33 @@ def scoped_tags(tag: str) -> bool:
5051

5152

5253
@functools.cache
53-
def get_qualified_type(die: DWARFDie, split=False) -> str | tuple[str, str]:
54+
def get_qualified_type(die: DWARFDie, relative: DWARFDie | None = None, split=False) -> str | tuple[str, str]:
5455
printer = DWARFTypePrinter()
5556
if scoped_tags(die.tag):
5657
printer.append_scopes(die.parent)
5758

58-
if not split:
59-
printer.append_unqualified_name(die)
60-
return str(printer).strip()
59+
def relative_scope(ty: str, relative: DWARFDie | None = None) -> str:
60+
if not relative or relative.tag in {"DW_TAG_compile_unit", "DW_TAG_type_unit", "DW_TAG_skeleton_unit"}:
61+
return ty
6162

62-
inner = printer.append_unqualified_name_before(die)
63-
before = str(printer).strip()
63+
output = ty.replace(get_qualified_type(relative) + "::", "")
64+
if output != ty:
65+
return output
6466

65-
printer = DWARFTypePrinter()
66-
printer.append_unqualified_name_after(die, inner)
67-
after = str(printer).strip()
67+
return relative_scope(ty, relative.parent)
6868

69-
return before, after
69+
if not split:
70+
printer.append_unqualified_name(die)
71+
ty = str(printer).strip()
72+
return relative_scope(ty, relative)
73+
else:
74+
inner = printer.append_unqualified_name_before(die)
75+
before = str(printer).strip()
76+
77+
printer = DWARFTypePrinter()
78+
printer.append_unqualified_name_after(die, inner)
79+
after = str(printer).strip()
80+
return relative_scope(before, relative), after
7081

7182

7283
def float_to_str(value: float) -> str:

0 commit comments

Comments
 (0)