Skip to content

Commit 0f8749d

Browse files
committed
[api] The member_offset_bytes now optionally resolves also member type info.
1 parent 484ba01 commit 0f8749d

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

lib/bl/api/std/type_utils/type_utils.bl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,20 @@ has_member :: fn (TStruct: type #comptime, member_name: string_view) bool {
134134
return false;
135135
}
136136

137-
/// Resolve struct member offset in bytes at `path`. Path examples:
137+
/// Resolve struct member offset in bytes at `path`. Panics in case path does not exist and `optional`
138+
/// is `false` otherwise returns -1 offset.
139+
///
140+
/// Optional `member_type_info` is initialized to `member.base_type` in case path is valid.
141+
///
142+
/// Path examples:
138143
///
139144
/// - `member_name` top-level struct member name.
140145
/// - `member_name/nested_member_name` member nested in another struct.
141146
/// - `/member_name` with optional root.
142147
/// - `/member_name/nested_member_name` again with optional root.
143148
/// - `/` just root, returns offset 0.
144149
/// ```
145-
member_offset_bytes :: fn {
150+
member_offset_bytes :: fn { // @Incomplete 2026-05-27: Maybe rename to something like get_member_info/find_member_at_path and use multi-return?
146151
member_offset_bytes_impl;
147152
member_offset_bytes_with_typeinfo_impl;
148153
}
@@ -184,13 +189,13 @@ get_struct_c_names :: fn (TStruct: type #comptime) [struct_member_count(TStruct)
184189
return names;
185190
}
186191

187-
member_offset_bytes_impl :: fn (TStruct: type #comptime, path: string_view, is_optional := false) s64 {
192+
member_offset_bytes_impl :: fn (TStruct: type #comptime, path: string_view, is_optional := false, member_type_info: **TypeInfo = null) s64 {
188193
static_assert(typeinfo(TStruct).kind == TypeKind.STRUCT);
189194
assert(path.len > 0);
190-
return member_offset_bytes_with_typeinfo_impl(auto typeinfo(TStruct), path, is_optional);
195+
return member_offset_bytes_with_typeinfo_impl(auto typeinfo(TStruct), path, is_optional, member_type_info);
191196
};
192197

193-
member_offset_bytes_with_typeinfo_impl :: fn (type_info: *TypeInfoStruct, path: string_view, is_optional := false) s64 {
198+
member_offset_bytes_with_typeinfo_impl :: fn (type_info: *TypeInfoStruct, path: string_view, is_optional := false, member_type_info: **TypeInfo = null) s64 {
194199
assert(path.len > 0);
195200

196201
offset: s64;
@@ -214,6 +219,7 @@ member_offset_bytes_with_typeinfo_impl :: fn (type_info: *TypeInfoStruct, path:
214219
offset += member.offset_bytes;
215220
info = auto member.base_type;
216221
found = true;
222+
217223
break;
218224
}
219225
}
@@ -225,6 +231,8 @@ member_offset_bytes_with_typeinfo_impl :: fn (type_info: *TypeInfoStruct, path:
225231
panic("Member '%' in the structure '%' is supposed to be struct type.", lhs, type_info.name);
226232
}
227233
}
234+
235+
if member_type_info then @member_type_info = auto info;
228236
return offset;
229237
}
230238

0 commit comments

Comments
 (0)