Skip to content

Commit 4d13f94

Browse files
committed
feat: add merge functionality
1 parent 4b95995 commit 4d13f94

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

src/dwarf2cpp/models.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ class Object:
1313
is_implicit: bool = False
1414
access: AccessAttribute | None = None
1515

16+
def merge(self, other: "Object") -> bool:
17+
if self != other:
18+
return False
19+
20+
return True
21+
1622

1723
@dataclass
1824
class Namespace:
@@ -50,9 +56,29 @@ class Attribute(Object):
5056
default_value: int | float | None = None
5157
alignment: int | None = None
5258
bit_size: int | None = None
53-
is_template: bool = False
5459
is_static: bool = False
5560

61+
def merge(self, other: "Object") -> bool:
62+
if not isinstance(other, Attribute):
63+
return False
64+
65+
if self.name != other.name or self.type != other.type:
66+
return False
67+
68+
if self.default_value is None:
69+
self.default_value = other.default_value
70+
71+
if self.alignment is None:
72+
self.alignment = other.alignment
73+
74+
if self.bit_size is None:
75+
self.bit_size = other.bit_size
76+
77+
if self.is_static is None:
78+
self.is_static = other.is_static
79+
80+
return True
81+
5682

5783
class ParameterKind(enum.StrEnum):
5884
POSITIONAL = "positional"

src/dwarf2cpp/visitor.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,19 @@ def files(self) -> Generator[tuple[str, dict[int, list[Object]]]]:
136136
continue
137137

138138
for line, objects in file.items():
139-
# ensure the uniqueness of elements
140-
if len(objects) > 1:
141-
seen = []
142-
for obj in objects:
143-
if obj not in seen:
144-
seen.append(obj)
145-
objects = seen
139+
result = []
146140

147-
file[line] = objects
141+
for item in objects:
142+
if not result:
143+
# First item, just add it
144+
result.append(item)
145+
elif not item in result:
146+
last = result[-1]
147+
if not last.merge(item):
148+
# merge() returned False, so append new item
149+
result.append(item)
150+
151+
file[line] = result
148152

149153
yield rel_path, file
150154

@@ -662,8 +666,7 @@ def _handle_attribute(self, die: DWARFDie) -> None:
662666
"DW_TAG_template_value_parameter",
663667
"DW_TAG_GNU_template_parameter_pack",
664668
}:
665-
variable.is_template = True
666-
variable.default_value = None
669+
pass
667670
else:
668671
raise ValueError(f"Unhandled child tag {child.tag}")
669672

0 commit comments

Comments
 (0)