summaryrefslogtreecommitdiff
path: root/python/types.py
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2025-11-03 20:12:21 -0500
committerMason Reed <mason@vector35.com>2025-11-04 13:34:46 -0500
commit427745da6d4bed73394995a3c768efcd10a78062 (patch)
tree02641df312b322a3580431099cf02039988ae844 /python/types.py
parentcc742d95264d1fb1ead1b5fbea0a1dc0b06b6d23 (diff)
[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.
Diffstat (limited to 'python/types.py')
-rw-r--r--python/types.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/python/types.py b/python/types.py
index c88360f2..5b3de7c9 100644
--- a/python/types.py
+++ b/python/types.py
@@ -1327,12 +1327,35 @@ class FunctionBuilder(TypeBuilder):
class StructureMember:
type: 'Type'
name: str
- offset: int
+ offset: int # Offset (in bytes) from the start of the structure. Use `bit_offset` for bitwise fields.
access: MemberAccess = MemberAccess.NoAccess
scope: MemberScope = MemberScope.NoScope
- bit_position: int = 0
+ bit_position: int = 0 # Relative to the starting byte at `offset`, must be in range 0 to 7.
bit_width: int = 0
+ @property
+ def bit_offset(self) -> int:
+ """
+ Total bit offset from the start of the structure.
+
+ Computed as: offset * 8 + bit_position.
+ """
+ return (self.offset * 8) + self.bit_position
+
+ @bit_offset.setter
+ def bit_offset(self, value: int) -> None:
+ """
+ Set the total bit offset from the start of the structure.
+
+ This will automatically set:
+ - offset to value // 8 (byte offset)
+ - bit_position to value % 8 (bit within the byte offset)
+ """
+ if value < 0:
+ raise ValueError("bit_offset must be non-negative")
+ self.offset = value // 8
+ self.bit_position = value % 8
+
def __repr__(self):
if len(self.name) == 0:
base = f"<member: {self.type}, offset {self.offset:#x}>"