Skip to content

Commit 27dbc3f

Browse files
committed
[Python] Add virtual bit_offset member to the StructureMember and clarify bit_position
This is a helper because it is likely one dealing with bitfields knows the bit offset, rather than the bit position, so this can be used instead.
1 parent 9db4108 commit 27dbc3f

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

python/types.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,12 +1327,35 @@ def children(self) -> List[TypeBuilder]:
13271327
class StructureMember:
13281328
type: 'Type'
13291329
name: str
1330-
offset: int
1330+
offset: int # Offset (in bytes) from the start of the structure. Use `bit_offset` for bitwise fields.
13311331
access: MemberAccess = MemberAccess.NoAccess
13321332
scope: MemberScope = MemberScope.NoScope
1333-
bit_position: int = 0
1333+
bit_position: int = 0 # Relative to the starting byte at `offset`, must be in range 0 to 7.
13341334
bit_width: int = 0
13351335

1336+
@property
1337+
def bit_offset(self) -> int:
1338+
"""
1339+
Total bit offset from the start of the structure.
1340+
1341+
Computed as: offset * 8 + bit_position.
1342+
"""
1343+
return (self.offset * 8) + self.bit_position
1344+
1345+
@bit_offset.setter
1346+
def bit_offset(self, value: int) -> None:
1347+
"""
1348+
Set the total bit offset from the start of the structure.
1349+
1350+
This will automatically set:
1351+
- offset to value // 8 (byte offset)
1352+
- bit_position to value % 8 (bit within the byte offset)
1353+
"""
1354+
if value < 0:
1355+
raise ValueError("bit_offset must be non-negative")
1356+
self.offset = value // 8
1357+
self.bit_position = value % 8
1358+
13361359
def __repr__(self):
13371360
if len(self.name) == 0:
13381361
base = f"<member: {self.type}, offset {self.offset:#x}>"

0 commit comments

Comments
 (0)