master
  1# pretty printing for the zig language, zig standard library, and zig stage 2 compiler.
  2# put commands in ~/.lldbinit to run them automatically when starting lldb
  3# `command script import /path/to/zig/tools/lldb_pretty_printers.py` to import this file
  4# `type category enable zig.lang` to enable pretty printing for the zig language
  5# `type category enable zig.std` to enable pretty printing for the zig standard library
  6# `type category enable zig.stage2` to enable pretty printing for the zig stage 2 compiler
  7import lldb
  8import re
  9
 10# Helpers
 11
 12page_size = 1 << 12
 13
 14def log2_int(i): return i.bit_length() - 1
 15
 16def create_struct(parent, name, struct_type, inits):
 17    struct_bytes, struct_data = bytearray(struct_type.size), lldb.SBData()
 18    for field in struct_type.fields:
 19        field_size = field.type.size
 20        field_init = inits[field.name]
 21        if isinstance(field_init, int):
 22            match struct_data.byte_order:
 23                case lldb.eByteOrderLittle:
 24                    byte_order = 'little'
 25                case lldb.eByteOrderBig:
 26                    byte_order = 'big'
 27            field_bytes = field_init.to_bytes(field_size, byte_order, signed=field.type.GetTypeFlags() & lldb.eTypeIsSigned != 0)
 28        elif isinstance(field_init, lldb.SBValue):
 29            field_bytes = field_init.data.uint8
 30        else: return
 31        match struct_data.byte_order:
 32            case lldb.eByteOrderLittle:
 33                field_bytes = field_bytes[:field_size]
 34                field_start = field.byte_offset
 35                struct_bytes[field_start:field_start + len(field_bytes)] = field_bytes
 36            case lldb.eByteOrderBig:
 37                field_bytes = field_bytes[-field_size:]
 38                field_end = field.byte_offset + field_size
 39                struct_bytes[field_end - len(field_bytes):field_end] = field_bytes
 40    struct_data.SetData(lldb.SBError(), struct_bytes, struct_data.byte_order, struct_data.GetAddressByteSize())
 41    return parent.CreateValueFromData(name, struct_data, struct_type)
 42
 43# Define Zig Language
 44
 45zig_keywords = {
 46    'addrspace',
 47    'align',
 48    'allowzero',
 49    'and',
 50    'anyframe',
 51    'anytype',
 52    'asm',
 53    'break',
 54    'callconv',
 55    'catch',
 56    'comptime',
 57    'const',
 58    'continue',
 59    'defer',
 60    'else',
 61    'enum',
 62    'errdefer',
 63    'error',
 64    'export',
 65    'extern',
 66    'fn',
 67    'for',
 68    'if',
 69    'inline',
 70    'noalias',
 71    'noinline',
 72    'nosuspend',
 73    'opaque',
 74    'or',
 75    'orelse',
 76    'packed',
 77    'pub',
 78    'resume',
 79    'return',
 80    'linksection',
 81    'struct',
 82    'suspend',
 83    'switch',
 84    'test',
 85    'threadlocal',
 86    'try',
 87    'union',
 88    'unreachable',
 89    'var',
 90    'volatile',
 91    'while',
 92}
 93zig_primitives = {
 94    'anyerror',
 95    'anyframe',
 96    'anyopaque',
 97    'bool',
 98    'c_int',
 99    'c_long',
100    'c_longdouble',
101    'c_longlong',
102    'c_short',
103    'c_uint',
104    'c_ulong',
105    'c_ulonglong',
106    'c_ushort',
107    'comptime_float',
108    'comptime_int',
109    'f128',
110    'f16',
111    'f32',
112    'f64',
113    'f80',
114    'false',
115    'isize',
116    'noreturn',
117    'null',
118    'true',
119    'type',
120    'undefined',
121    'usize',
122    'void',
123}
124zig_integer_type = re.compile('[iu][1-9][0-9]+')
125zig_identifier_regex = re.compile('[A-Z_a-z][0-9A-Z_a-z]*')
126def zig_IsVariableName(string): return string != '_' and string not in zig_keywords and string not in zig_primitives and not zig_integer_type.fullmatch(string) and zig_identifier_regex.fullmatch(string)
127def zig_IsFieldName(string): return string not in zig_keywords and zig_identifier_regex.fullmatch(string)
128
129class zig_Slice_SynthProvider:
130    def __init__(self, value, _=None): self.value = value
131    def update(self):
132        try:
133            self.ptr = self.value.GetChildMemberWithName('ptr')
134            self.len = self.value.GetChildMemberWithName('len').unsigned if self.ptr.unsigned > page_size else 0
135            self.elem_type = self.ptr.type.GetPointeeType()
136            self.elem_size = self.elem_type.size
137        except: pass
138    def has_children(self): return True
139    def num_children(self): return self.len or 0
140    def get_child_index(self, name):
141        try: return int(name.removeprefix('[').removesuffix(']'))
142        except: return -1
143    def get_child_at_index(self, index):
144        if index not in range(self.len): return None
145        try: return self.ptr.CreateChildAtOffset('[%d]' % index, index * self.elem_size, self.elem_type)
146        except: return None
147
148def zig_String_decode(value, offset=0, length=None):
149    try:
150        value = value.GetNonSyntheticValue()
151        data = value.GetChildMemberWithName('ptr').GetPointeeData(offset, length if length is not None else value.GetChildMemberWithName('len').unsigned)
152        b = bytes(data.uint8)
153        b = b.replace(b'\\', b'\\\\')
154        b = b.replace(b'\n', b'\\n')
155        b = b.replace(b'\r', b'\\r')
156        b = b.replace(b'\t', b'\\t')
157        b = b.replace(b'"', b'\\"')
158        b = b.replace(b'\'', b'\\\'')
159        s = b.decode(encoding='ascii', errors='backslashreplace')
160        return s if s.isprintable() else ''.join((c if c.isprintable() else '\\x%02x' % ord(c) for c in s))
161    except: return None
162def zig_String_SummaryProvider(value, _=None): return '"%s"' % zig_String_decode(value)
163def zig_String_AsIdentifier(value, pred):
164    string = zig_String_decode(value)
165    return string if pred(string) else '@"%s"' % string
166
167class zig_Optional_SynthProvider:
168    def __init__(self, value, _=None): self.value = value
169    def update(self):
170        try:
171            self.child = self.value.GetChildMemberWithName('some').unsigned == 1 and self.value.GetChildMemberWithName('data').Clone('child')
172        except: pass
173    def has_children(self): return bool(self.child)
174    def num_children(self): return int(self.child)
175    def get_child_index(self, name): return 0 if self.child and (name == 'child' or name == '?') else -1
176    def get_child_at_index(self, index): return self.child if self.child and index == 0 else None
177def zig_Optional_SummaryProvider(value, _=None):
178    child = value.GetChildMemberWithName('child')
179    return child or 'null'
180
181class zig_ErrorUnion_SynthProvider:
182    def __init__(self, value, _=None): self.value = value
183    def update(self):
184        try:
185            self.error_set = self.value.GetChildMemberWithName('tag').Clone('error_set')
186            self.payload = self.value.GetChildMemberWithName('value').Clone('payload') if self.error_set.unsigned == 0 else None
187        except: pass
188    def has_children(self): return True
189    def num_children(self): return 1
190    def get_child_index(self, name): return 0 if name == ('payload' if self.payload else 'error_set') else -1
191    def get_child_at_index(self, index): return self.payload or self.error_set if index == 0 else None
192
193class zig_TaggedUnion_SynthProvider:
194    def __init__(self, value, _=None): self.value = value
195    def update(self):
196        try:
197            self.tag = self.value.GetChildMemberWithName('tag')
198            self.payload = self.value.GetChildMemberWithName('payload').GetChildMemberWithName(self.tag.value)
199        except: pass
200    def has_children(self): return True
201    def num_children(self): return 1 + (self.payload is not None)
202    def get_child_index(self, name):
203        try: return ('tag', 'payload').index(name)
204        except: return -1
205    def get_child_at_index(self, index): return (self.tag, self.payload)[index] if index in range(2) else None
206
207# Define Zig Standard Library
208
209class std_MultiArrayList_SynthProvider:
210    def __init__(self, value, _=None): self.value = value
211    def update(self):
212        try:
213            self.len = 0
214
215            value_type = self.value.type
216            for helper in self.value.target.FindFunctions('%s.dbHelper' % value_type.name, lldb.eFunctionNameTypeFull):
217                ptr_self_type, ptr_child_type, ptr_field_type, ptr_entry_type = helper.function.type.GetFunctionArgumentTypes()
218                if ptr_self_type.GetPointeeType() == value_type: break
219            else: return
220
221            self.entry_type = ptr_entry_type.GetPointeeType()
222            self.bytes = self.value.GetChildMemberWithName('bytes')
223            self.len = self.value.GetChildMemberWithName('len').unsigned
224            self.capacity = self.value.GetChildMemberWithName('capacity').unsigned
225        except: pass
226    def has_children(self): return True
227    def num_children(self): return self.len
228    def get_child_index(self, name):
229        try: return int(name.removeprefix('[').removesuffix(']'))
230        except: return -1
231    def get_child_at_index(self, index):
232        try:
233            if index not in range(self.len): return None
234            offset = 0
235            data = lldb.SBData()
236            for field in self.entry_type.fields:
237                field_type = field.type.GetPointeeType()
238                field_size = field_type.size
239                data.Append(self.bytes.CreateChildAtOffset(field.name, offset + index * field_size, field_type).address_of.data)
240                offset += self.capacity * field_size
241            return self.bytes.CreateValueFromData('[%d]' % index, data, self.entry_type)
242        except: return None
243class std_MultiArrayList_Slice_SynthProvider:
244    def __init__(self, value, _=None): self.value = value
245    def update(self):
246        try:
247            self.len = 0
248
249            value_type = self.value.type
250            for helper in self.value.target.FindFunctions('%s.dbHelper' % value_type.name, lldb.eFunctionNameTypeFull):
251                ptr_self_type, ptr_child_type, ptr_field_type, ptr_entry_type = helper.function.type.GetFunctionArgumentTypes()
252                if ptr_self_type.GetPointeeType() == value_type: break
253            else: return
254
255            self.fields = {member.name: index for index, member in enumerate(ptr_field_type.GetPointeeType().enum_members)}
256            self.entry_type = ptr_entry_type.GetPointeeType()
257            self.ptrs = self.value.GetChildMemberWithName('ptrs')
258            self.len = self.value.GetChildMemberWithName('len').unsigned
259            self.capacity = self.value.GetChildMemberWithName('capacity').unsigned
260        except: pass
261    def has_children(self): return True
262    def num_children(self): return self.len
263    def get_child_index(self, name):
264        try: return int(name.removeprefix('[').removesuffix(']'))
265        except: return -1
266    def get_child_at_index(self, index):
267        try:
268            if index not in range(self.len): return None
269            data = lldb.SBData()
270            for field in self.entry_type.fields:
271                field_type = field.type.GetPointeeType()
272                data.Append(self.ptrs.child[self.fields[field.name.removesuffix('_ptr')]].CreateChildAtOffset(field.name, index * field_type.size, field_type).address_of.data)
273            return self.ptrs.CreateValueFromData('[%d]' % index, data, self.entry_type)
274        except: return None
275
276def MultiArrayList_Entry(type): return '^multi_array_list\\.MultiArrayList\\(%s\\)\\.Entry__struct_[1-9][0-9]*$' % type
277
278class std_HashMapUnmanaged_SynthProvider:
279    def __init__(self, value, _=None): self.value = value
280    def update(self):
281        try:
282            self.capacity = 0
283            self.indices = tuple()
284
285            self.metadata = self.value.GetChildMemberWithName('metadata')
286            if not self.metadata.unsigned: return
287
288            value_type = self.value.type
289            for helper in self.value.target.FindFunctions('%s.dbHelper' % value_type.name, lldb.eFunctionNameTypeFull):
290                ptr_self_type, ptr_hdr_type, ptr_entry_type = helper.function.type.GetFunctionArgumentTypes()
291                if ptr_self_type.GetPointeeType() == value_type: break
292            else: return
293            self.entry_type = ptr_entry_type.GetPointeeType()
294
295            hdr_type = ptr_hdr_type.GetPointeeType()
296            hdr = self.metadata.CreateValueFromAddress('header', self.metadata.deref.load_addr - hdr_type.size, hdr_type)
297            self.values = hdr.GetChildMemberWithName('values')
298            self.keys = hdr.GetChildMemberWithName('keys')
299            self.capacity = hdr.GetChildMemberWithName('capacity').unsigned
300
301            self.indices = tuple(i for i, value in enumerate(self.metadata.GetPointeeData(0, self.capacity).sint8) if value < 0)
302        except: pass
303    def has_children(self): return True
304    def num_children(self): return len(self.indices)
305    def get_capacity(self): return self.capacity
306    def get_child_index(self, name):
307        try: return int(name.removeprefix('[').removesuffix(']'))
308        except: return -1
309    def get_child_at_index(self, index):
310        try:
311            fields = {name: base.CreateChildAtOffset(name, self.indices[index] * pointee_type.size, pointee_type).address_of.data for name, base, pointee_type in ((name, base, base.type.GetPointeeType()) for name, base in (('key_ptr', self.keys), ('value_ptr', self.values)))}
312            data = lldb.SBData()
313            for field in self.entry_type.fields: data.Append(fields[field.name])
314            return self.metadata.CreateValueFromData('[%d]' % index, data, self.entry_type)
315        except: return None
316def std_HashMapUnmanaged_SummaryProvider(value, _=None):
317    synth = std_HashMapUnmanaged_SynthProvider(value.GetNonSyntheticValue(), _)
318    synth.update()
319    return 'len=%d capacity=%d' % (synth.num_children(), synth.get_capacity())
320
321# formats a struct of fields of the form `name_ptr: *Type` by auto dereferencing its fields
322class std_Entry_SynthProvider:
323    def __init__(self, value, _=None): self.value = value
324    def update(self):
325        try:
326            self.children = tuple(child.Clone(child.name.removesuffix('_ptr')) for child in self.value.children if child.type.GetPointeeType().size != 0)
327            self.indices = {child.name: i for i, child in enumerate(self.children)}
328        except: pass
329    def has_children(self): return self.num_children() != 0
330    def num_children(self): return len(self.children)
331    def get_child_index(self, name): return self.indices.get(name)
332    def get_child_at_index(self, index): return self.children[index].deref if index in range(len(self.children)) else None
333
334# Define Zig Stage2 Compiler
335
336class TagAndPayload_SynthProvider:
337    def __init__(self, value, _=None): self.value = value
338    def update(self):
339        try:
340            self.tag = self.value.GetChildMemberWithName('tag') or self.value.GetChildMemberWithName('tag_ptr').deref.Clone('tag')
341            data = self.value.GetChildMemberWithName('data_ptr') or self.value.GetChildMemberWithName('data')
342            self.payload = data.GetChildMemberWithName('payload').GetChildMemberWithName(data.GetChildMemberWithName('tag').value)
343        except: pass
344    def has_children(self): return True
345    def num_children(self): return 2
346    def get_child_index(self, name):
347        try: return ('tag', 'payload').index(name)
348        except: return -1
349    def get_child_at_index(self, index): return (self.tag, self.payload)[index] if index in range(2) else None
350
351def InstRef_SummaryProvider(value, _=None):
352    return value if any(value.unsigned == member.unsigned for member in value.type.enum_members) else (
353        'InternPool.Index(%d)' % value.unsigned if value.unsigned < 0x80000000 else 'instructions[%d]' % (value.unsigned - 0x80000000))
354
355def InstIndex_SummaryProvider(value, _=None):
356    return 'instructions[%d]' % value.unsigned if value.unsigned < 0x80000000 else 'temps[%d]' % (value.unsigned - 0x80000000)
357
358class zig_DeclIndex_SynthProvider:
359    def __init__(self, value, _=None): self.value = value
360    def update(self):
361        try:
362            ip = InternPool_Find(self.value.thread)
363            if not ip: return
364            self.ptr = ip.GetChildMemberWithName('allocated_decls').GetChildAtIndex(self.value.unsigned).address_of.Clone('decl')
365        except: pass
366    def has_children(self): return True
367    def num_children(self): return 1
368    def get_child_index(self, name): return 0 if name == 'decl' else -1
369    def get_child_at_index(self, index): return self.ptr if index == 0 else None
370
371class Module_Namespace__Module_Namespace_Index_SynthProvider:
372    def __init__(self, value, _=None): self.value = value
373    def update(self):
374        try:
375            ip = InternPool_Find(self.value.thread)
376            if not ip: return
377            self.ptr = ip.GetChildMemberWithName('allocated_namespaces').GetChildAtIndex(self.value.unsigned).address_of.Clone('namespace')
378        except: pass
379    def has_children(self): return True
380    def num_children(self): return 1
381    def get_child_index(self, name): return 0 if name == 'namespace' else -1
382    def get_child_at_index(self, index): return self.ptr if index == 0 else None
383
384class TagOrPayloadPtr_SynthProvider:
385    def __init__(self, value, _=None): self.value = value
386    def update(self):
387        try:
388            value_type = self.value.type
389            for helper in self.value.target.FindFunctions('%s.dbHelper' % value_type.name, lldb.eFunctionNameTypeFull):
390                ptr_self_type, ptr_tag_to_payload_map_type = helper.function.type.GetFunctionArgumentTypes()
391                self_type = ptr_self_type.GetPointeeType()
392                if self_type == value_type: break
393            else: return
394            tag_to_payload_map = {field.name: field.type for field in ptr_tag_to_payload_map_type.GetPointeeType().fields}
395
396            tag = self.value.GetChildMemberWithName('tag_if_small_enough')
397            if tag.unsigned < page_size:
398                self.tag = tag.Clone('tag')
399                self.payload = None
400            else:
401                ptr_otherwise = self.value.GetChildMemberWithName('ptr_otherwise')
402                self.tag = ptr_otherwise.GetChildMemberWithName('tag')
403                self.payload = ptr_otherwise.Cast(tag_to_payload_map[self.tag.value]).GetChildMemberWithName('data').Clone('payload')
404        except: pass
405    def has_children(self): return True
406    def num_children(self): return 1 + (self.payload is not None)
407    def get_child_index(self, name):
408        try: return ('tag', 'payload').index(name)
409        except: return -1
410    def get_child_at_index(self, index): return (self.tag, self.payload)[index] if index in range(2) else None
411
412def Module_Decl_name(decl):
413    error = lldb.SBError()
414    return decl.process.ReadCStringFromMemory(decl.GetChildMemberWithName('name').deref.load_addr, 256, error)
415
416def Module_Namespace_RenderFullyQualifiedName(namespace):
417    parent = namespace.GetChildMemberWithName('parent')
418    if parent.unsigned < page_size: return zig_String_decode(namespace.GetChildMemberWithName('file_scope').GetChildMemberWithName('sub_file_path')).removesuffix('.zig').replace('/', '.')
419    return '.'.join((Module_Namespace_RenderFullyQualifiedName(parent), Module_Decl_name(namespace.GetChildMemberWithName('ty').GetChildMemberWithName('payload').GetChildMemberWithName('owner_decl').GetChildMemberWithName('decl'))))
420
421def Module_Decl_RenderFullyQualifiedName(decl): return '.'.join((Module_Namespace_RenderFullyQualifiedName(decl.GetChildMemberWithName('src_namespace')), Module_Decl_name(decl)))
422
423def OwnerDecl_RenderFullyQualifiedName(payload): return Module_Decl_RenderFullyQualifiedName(payload.GetChildMemberWithName('owner_decl').GetChildMemberWithName('decl'))
424
425def InternPool_Find(thread):
426    for frame in thread:
427        ip = frame.FindVariable('ip') or frame.FindVariable('intern_pool')
428        if ip: return ip
429        mod = frame.FindVariable('zcu') or frame.FindVariable('mod') or frame.FindVariable('module')
430        if mod:
431            ip = mod.GetChildMemberWithName('intern_pool')
432            if ip: return ip
433
434class InternPool_Index_SynthProvider:
435    def __init__(self, value, _=None): self.value = value
436    def update(self):
437        try:
438            index_type = self.value.type
439            for helper in self.value.target.FindFunctions('%s.dbHelper' % index_type.name, lldb.eFunctionNameTypeFull):
440                ptr_self_type, ptr_tag_to_encoding_map_type = helper.function.type.GetFunctionArgumentTypes()
441                if ptr_self_type.GetPointeeType() == index_type: break
442            else: return
443            tag_to_encoding_map = {field.name: field.type for field in ptr_tag_to_encoding_map_type.GetPointeeType().fields}
444
445            ip = InternPool_Find(self.value.thread)
446            if not ip: return
447            self.item = ip.GetChildMemberWithName('items').GetChildAtIndex(self.value.unsigned)
448            extra = ip.GetChildMemberWithName('extra').GetChildMemberWithName('items')
449            self.tag = self.item.GetChildMemberWithName('tag').Clone('tag')
450            self.data = None
451            self.trailing = None
452            data = self.item.GetChildMemberWithName('data')
453            encoding_type = tag_to_encoding_map[self.tag.value]
454            dynamic_values = {}
455            for encoding_field in encoding_type.fields:
456                if encoding_field.name == 'data':
457                    if encoding_field.type.IsPointerType():
458                        extra_index = data.unsigned
459                        self.data = extra.GetChildAtIndex(extra_index).address_of.Cast(encoding_field.type).deref.Clone('data')
460                        extra_index += encoding_field.type.GetPointeeType().num_fields
461                    else:
462                        self.data = data.Cast(encoding_field.type).Clone('data')
463                elif encoding_field.name == 'trailing':
464                    trailing_data = lldb.SBData()
465                    for trailing_field in encoding_field.type.fields:
466                        trailing_data.Append(extra.GetChildAtIndex(extra_index).address_of.data)
467                        trailing_len = dynamic_values['trailing.%s.len' % trailing_field.name].unsigned
468                        trailing_data.Append(lldb.SBData.CreateDataFromInt(trailing_len, trailing_data.GetAddressByteSize()))
469                        extra_index += trailing_len
470                    self.trailing = self.data.CreateValueFromData('trailing', trailing_data, encoding_field.type)
471                else:
472                    for path in encoding_field.type.GetPointeeType().name.removeprefix('%s::' % encoding_type.name).removeprefix('%s.' % encoding_type.name).partition('__')[0].split(' orelse '):
473                        if path.startswith('data.'):
474                            root = self.data
475                            path = path[len('data'):]
476                        else: return
477                        dynamic_value = root.GetValueForExpressionPath(path)
478                        if dynamic_value:
479                            dynamic_values[encoding_field.name] = dynamic_value
480                            break
481        except: pass
482    def has_children(self): return True
483    def num_children(self): return 2 + (self.trailing is not None)
484    def get_child_index(self, name):
485        try: return ('tag', 'data', 'trailing').index(name)
486        except: return -1
487    def get_child_at_index(self, index): return (self.tag, self.data, self.trailing)[index] if index in range(3) else None
488
489def InternPool_NullTerminatedString_SummaryProvider(value, _=None):
490    try:
491        ip = InternPool_Find(value.thread)
492        if not ip: return
493        items = ip.GetChildMemberWithName('string_bytes').GetChildMemberWithName('items')
494        b = bytearray()
495        i = 0
496        while True:
497            x = items.GetChildAtIndex(value.unsigned + i).GetValueAsUnsigned()
498            if x == 0: break
499            b.append(x)
500            i += 1
501        s = b.decode(encoding='utf8', errors='backslashreplace')
502        s1 = s if s.isprintable() else ''.join((c if c.isprintable() else '\\x%02x' % ord(c) for c in s))
503        return '"%s"' % s1
504    except:
505        pass
506
507def type_Type_pointer(payload):
508    pointee_type = payload.GetChildMemberWithName('pointee_type')
509    sentinel = payload.GetChildMemberWithName('sentinel').GetChildMemberWithName('child')
510    align = payload.GetChildMemberWithName('align').unsigned
511    addrspace = payload.GetChildMemberWithName('addrspace').value
512    bit_offset = payload.GetChildMemberWithName('bit_offset').unsigned
513    host_size = payload.GetChildMemberWithName('host_size').unsigned
514    vector_index = payload.GetChildMemberWithName('vector_index')
515    allowzero = payload.GetChildMemberWithName('allowzero').unsigned
516    const = not payload.GetChildMemberWithName('mutable').unsigned
517    volatile = payload.GetChildMemberWithName('volatile').unsigned
518    size = payload.GetChildMemberWithName('size').value
519
520    if size == 'One': summary = '*'
521    elif size == 'Many': summary = '[*'
522    elif size == 'Slice': summary = '['
523    elif size == 'C': summary = '[*c'
524    if sentinel: summary += ':%s' % value_Value_SummaryProvider(sentinel)
525    if size != 'One': summary += ']'
526    if allowzero: summary += 'allowzero '
527    if align != 0 or host_size != 0 or vector_index.value != 'none': summary += 'align(%d%s%s) ' % (align, ':%d:%d' % (bit_offset, host_size) if bit_offset != 0 or host_size != 0 else '', ':?' if vector_index.value == 'runtime' else ':%d' % vector_index.unsigned if vector_index.value != 'none' else '')
528    if addrspace != 'generic': summary += 'addrspace(.%s) ' % addrspace
529    if const: summary += 'const '
530    if volatile: summary += 'volatile '
531    summary += type_Type_SummaryProvider(pointee_type)
532    return summary
533
534def type_Type_function(payload):
535    param_types = payload.GetChildMemberWithName('param_types').children
536    comptime_params = payload.GetChildMemberWithName('comptime_params').GetPointeeData(0, len(param_types)).uint8
537    return_type = payload.GetChildMemberWithName('return_type')
538    alignment = payload.GetChildMemberWithName('alignment').unsigned
539    noalias_bits = payload.GetChildMemberWithName('noalias_bits').unsigned
540    cc = payload.GetChildMemberWithName('cc').value
541    is_var_args = payload.GetChildMemberWithName('is_var_args').unsigned
542
543    return 'fn(%s)%s%s %s' % (', '.join(tuple(''.join(('comptime ' if comptime_param else '', 'noalias ' if noalias_bits & 1 << i else '', type_Type_SummaryProvider(param_type))) for i, (comptime_param, param_type) in enumerate(zip(comptime_params, param_types))) + (('...',) if is_var_args else ())), ' align(%d)' % alignment if alignment != 0 else '', ' callconv(.%s)' % cc if cc != 'Unspecified' else '', type_Type_SummaryProvider(return_type))
544
545def type_Type_SummaryProvider(value, _=None):
546    tag = value.GetChildMemberWithName('tag').value
547    return type_tag_handlers.get(tag, lambda payload: tag)(value.GetChildMemberWithName('payload'))
548
549type_tag_handlers = {
550    'atomic_order': lambda payload: 'std.builtin.AtomicOrder',
551    'atomic_rmw_op': lambda payload: 'std.builtin.AtomicRmwOp',
552    'calling_convention': lambda payload: 'std.builtin.CallingConvention',
553    'address_space': lambda payload: 'std.builtin.AddressSpace',
554    'float_mode': lambda payload: 'std.builtin.FloatMode',
555    'reduce_op': lambda payload: 'std.builtin.ReduceOp',
556    'modifier': lambda payload: 'std.builtin.CallModifier',
557    'prefetch_options': lambda payload: 'std.builtin.PrefetchOptions',
558    'export_options': lambda payload: 'std.builtin.ExportOptions',
559    'extern_options': lambda payload: 'std.builtin.ExternOptions',
560    'type_info': lambda payload: 'std.builtin.Type',
561
562    'enum_literal': lambda payload: '@EnumLiteral()',
563    'null': lambda payload: '@TypeOf(null)',
564    'undefined': lambda payload: '@TypeOf(undefined)',
565    'empty_struct_literal': lambda payload: '@TypeOf(.{})',
566
567    'anyerror_void_error_union': lambda payload: 'anyerror!void',
568    'slice_const_u8': lambda payload: '[]const u8',
569    'slice_const_u8_sentinel_0': lambda payload: '[:0]const u8',
570    'fn_noreturn_no_args': lambda payload: 'fn() noreturn',
571    'fn_void_no_args': lambda payload: 'fn() void',
572    'fn_naked_noreturn_no_args': lambda payload: 'fn() callconv(.naked) noreturn',
573    'fn_ccc_void_no_args': lambda payload: 'fn() callconv(.c) void',
574    'ptr_usize': lambda payload: '*usize',
575    'ptr_const_comptime_int': lambda payload: '*const comptime_int',
576    'manyptr_u8': lambda payload: '[*]u8',
577    'manyptr_const_u8': lambda payload: '[*]const u8',
578    'manyptr_const_u8_sentinel_0': lambda payload: '[*:0]const u8',
579
580    'function': type_Type_function,
581    'error_union': lambda payload: '%s!%s' % (type_Type_SummaryProvider(payload.GetChildMemberWithName('error_set')), type_Type_SummaryProvider(payload.GetChildMemberWithName('payload'))),
582    'array_u8': lambda payload: '[%d]u8' % payload.unsigned,
583    'array_u8_sentinel_0': lambda payload: '[%d:0]u8' % payload.unsigned,
584    'vector': lambda payload: '@Vector(%d, %s)' % (payload.GetChildMemberWithName('len').unsigned, type_Type_SummaryProvider(payload.GetChildMemberWithName('elem_type'))),
585    'array': lambda payload: '[%d]%s' % (payload.GetChildMemberWithName('len').unsigned, type_Type_SummaryProvider(payload.GetChildMemberWithName('elem_type'))),
586    'array_sentinel': lambda payload: '[%d:%s]%s' % (payload.GetChildMemberWithName('len').unsigned, value_Value_SummaryProvider(payload.GetChildMemberWithName('sentinel')), type_Type_SummaryProvider(payload.GetChildMemberWithName('elem_type'))),
587    'tuple': lambda payload: 'tuple{%s}' % ', '.join(('comptime %%s = %s' % value_Value_SummaryProvider(value) if value.GetChildMemberWithName('tag').value != 'unreachable_value' else '%s') % type_Type_SummaryProvider(type) for type, value in zip(payload.GetChildMemberWithName('types').children, payload.GetChildMemberWithName('values').children)),
588    'anon_struct': lambda payload: 'struct{%s}' % ', '.join(('comptime %%s: %%s = %s' % value_Value_SummaryProvider(value) if value.GetChildMemberWithName('tag').value != 'unreachable_value' else '%s: %s') % (zig_String_AsIdentifier(name, zig_IsFieldName), type_Type_SummaryProvider(type)) for name, type, value in zip(payload.GetChildMemberWithName('names').children, payload.GetChildMemberWithName('types').children, payload.GetChildMemberWithName('values').children)),
589    'pointer': type_Type_pointer,
590    'single_const_pointer': lambda payload: '*const %s' % type_Type_SummaryProvider(payload),
591    'single_mut_pointer': lambda payload: '*%s' % type_Type_SummaryProvider(payload),
592    'many_const_pointer': lambda payload: '[*]const %s' % type_Type_SummaryProvider(payload),
593    'many_mut_pointer': lambda payload: '[*]%s' % type_Type_SummaryProvider(payload),
594    'c_const_pointer': lambda payload: '[*c]const %s' % type_Type_SummaryProvider(payload),
595    'c_mut_pointer': lambda payload: '[*c]%s' % type_Type_SummaryProvider(payload),
596    'slice_const': lambda payload: '[]const %s' % type_Type_SummaryProvider(payload),
597    'mut_slice': lambda payload: '[]%s' % type_Type_SummaryProvider(payload),
598    'int_signed': lambda payload: 'i%d' % payload.unsigned,
599    'int_unsigned': lambda payload: 'u%d' % payload.unsigned,
600    'optional': lambda payload: '?%s' % type_Type_SummaryProvider(payload),
601    'optional_single_mut_pointer': lambda payload: '?*%s' % type_Type_SummaryProvider(payload),
602    'optional_single_const_pointer': lambda payload: '?*const %s' % type_Type_SummaryProvider(payload),
603    'anyframe_T': lambda payload: 'anyframe->%s' % type_Type_SummaryProvider(payload),
604    'error_set': lambda payload: type_tag_handlers['error_set_merged'](payload.GetChildMemberWithName('names')),
605    'error_set_single': lambda payload: 'error{%s}' % zig_String_AsIdentifier(payload, zig_IsFieldName),
606    'error_set_merged': lambda payload: 'error{%s}' % ','.join(zig_String_AsIdentifier(child.GetChildMemberWithName('key'), zig_IsFieldName) for child in payload.GetChildMemberWithName('entries').children),
607    'error_set_inferred': lambda payload: '@typeInfo(@typeInfo(@TypeOf(%s)).@"fn".return_type.?).error_union.error_set' % OwnerDecl_RenderFullyQualifiedName(payload.GetChildMemberWithName('func')),
608
609    'enum_full': OwnerDecl_RenderFullyQualifiedName,
610    'enum_nonexhaustive': OwnerDecl_RenderFullyQualifiedName,
611    'enum_numbered': OwnerDecl_RenderFullyQualifiedName,
612    'enum_simple': OwnerDecl_RenderFullyQualifiedName,
613    'struct': OwnerDecl_RenderFullyQualifiedName,
614    'union': OwnerDecl_RenderFullyQualifiedName,
615    'union_safety_tagged': OwnerDecl_RenderFullyQualifiedName,
616    'union_tagged': OwnerDecl_RenderFullyQualifiedName,
617    'opaque': OwnerDecl_RenderFullyQualifiedName,
618}
619
620def value_Value_str_lit(payload):
621    for frame in payload.thread:
622        mod = frame.FindVariable('zcu') or frame.FindVariable('mod') or frame.FindVariable('module')
623        if mod: break
624    else: return
625    return '"%s"' % zig_String_decode(mod.GetChildMemberWithName('string_literal_bytes').GetChildMemberWithName('items'), payload.GetChildMemberWithName('index').unsigned, payload.GetChildMemberWithName('len').unsigned)
626
627def value_Value_SummaryProvider(value, _=None):
628    tag = value.GetChildMemberWithName('tag').value
629    return value_tag_handlers.get(tag, lambda payload: tag.removesuffix('_type'))(value.GetChildMemberWithName('payload'))
630
631value_tag_handlers = {
632    'undef': lambda payload: 'undefined',
633    'zero': lambda payload: '0',
634    'one': lambda payload: '1',
635    'void_value': lambda payload: '{}',
636    'unreachable_value': lambda payload: 'unreachable',
637    'null_value': lambda payload: 'null',
638    'bool_true': lambda payload: 'true',
639    'bool_false': lambda payload: 'false',
640
641    'empty_struct_value': lambda payload: '.{}',
642    'empty_array': lambda payload: '.{}',
643
644    'ty': type_Type_SummaryProvider,
645    'int_type': lambda payload: '%c%d' % (payload.GetChildMemberWithName('bits').unsigned, 's' if payload.GetChildMemberWithName('signed').unsigned == 1 else 'u'),
646    'int_u64': lambda payload: '%d' % payload.unsigned,
647    'int_i64': lambda payload: '%d' % payload.signed,
648    'int_big_positive': lambda payload: sum(child.unsigned << i * child.type.size * 8 for i, child in enumerate(payload.children)),
649    'int_big_negative': lambda payload: '-%s' % value_tag_handlers['int_big_positive'](payload),
650    'function': OwnerDecl_RenderFullyQualifiedName,
651    'extern_fn': OwnerDecl_RenderFullyQualifiedName,
652    'variable': lambda payload: value_Value_SummaryProvider(payload.GetChildMemberWithName('decl').GetChildMemberWithName('val')),
653    'runtime_value': value_Value_SummaryProvider,
654    'decl_ref': lambda payload: value_Value_SummaryProvider(payload.GetChildMemberWithName('decl').GetChildMemberWithName('val')),
655    'decl_ref_mut': lambda payload: value_Value_SummaryProvider(payload.GetChildMemberWithName('decl_index').GetChildMemberWithName('decl').GetChildMemberWithName('val')),
656    'comptime_field_ptr': lambda payload: '&%s' % value_Value_SummaryProvider(payload.GetChildMemberWithName('field_val')),
657    'elem_ptr': lambda payload: '(%s)[%d]' % (value_Value_SummaryProvider(payload.GetChildMemberWithName('array_ptr')), payload.GetChildMemberWithName('index').unsigned),
658    'field_ptr': lambda payload: '(%s).field[%d]' % (value_Value_SummaryProvider(payload.GetChildMemberWithName('container_ptr')), payload.GetChildMemberWithName('field_index').unsigned),
659    'bytes': lambda payload: '"%s"' % zig_String_decode(payload),
660    'str_lit': value_Value_str_lit,
661    'repeated': lambda payload: '.{%s} ** _' % value_Value_SummaryProvider(payload),
662    'empty_array_sentinel': lambda payload: '.{%s}' % value_Value_SummaryProvider(payload),
663    'slice': lambda payload: '(%s)[0..%s]' % tuple(value_Value_SummaryProvider(payload.GetChildMemberWithName(name)) for name in ('ptr', 'len')),
664    'float_16': lambda payload: payload.value,
665    'float_32': lambda payload: payload.value,
666    'float_64': lambda payload: payload.value,
667    'float_80': lambda payload: payload.value,
668    'float_128': lambda payload: payload.value,
669    'enum_literal': lambda payload: '.%s' % zig_String_AsIdentifier(payload, zig_IsFieldName),
670    'enum_field_index': lambda payload: 'field[%d]' % payload.unsigned,
671    'error': lambda payload: 'error.%s' % zig_String_AsIdentifier(payload.GetChildMemberWithName('name'), zig_IsFieldName),
672    'eu_payload': value_Value_SummaryProvider,
673    'eu_payload_ptr': lambda payload: '&((%s).* catch unreachable)' % value_Value_SummaryProvider(payload.GetChildMemberWithName('container_ptr')),
674    'opt_payload': value_Value_SummaryProvider,
675    'opt_payload_ptr': lambda payload: '&(%s).*.?' % value_Value_SummaryProvider(payload.GetChildMemberWithName('container_ptr')),
676    'aggregate': lambda payload: '.{%s}' % ', '.join(map(value_Value_SummaryProvider, payload.children)),
677    'union': lambda payload: '.{.%s = %s}' % tuple(value_Value_SummaryProvider(payload.GetChildMemberWithName(name)) for name in ('tag', 'val')),
678
679    'lazy_align': lambda payload: '@alignOf(%s)' % type_Type_SummaryProvider(payload),
680    'lazy_size': lambda payload: '@sizeOf(%s)' % type_Type_SummaryProvider(payload),
681}
682
683# Define Zig Stage2 Compiler (compiled with the self-hosted backend)
684
685class root_InternPool_Local_List_SynthProvider:
686    def __init__(self, value, _=None): self.value = value
687    def update(self):
688        capacity = self.value.EvaluateExpression('@as(*@This().Header, @alignCast(@ptrCast(@this().bytes - @This().bytes_offset))).capacity')
689        self.view = create_struct(self.value, '.view', self.value.type.FindDirectNestedType('View'), { 'bytes': self.value.GetChildMemberWithName('bytes'), 'len': capacity, 'capacity': capacity }).GetNonSyntheticValue()
690    def has_children(self): return True
691    def num_children(self): return 1
692    def get_child_index(self, name):
693        try: return ('view',).index(name)
694        except: pass
695    def get_child_at_index(self, index):
696        try: return (self.view,)[index]
697        except: pass
698
699expr_path_re = re.compile(r'\{([^}]+)%([^%#}]+)(?:#([^%#}]+))?\}')
700def root_InternPool_Index_SummaryProvider(value, _=None):
701    unwrapped = value.GetChildMemberWithName('unwrapped')
702    if not unwrapped: return '' # .none
703    tag = unwrapped.GetChildMemberWithName('tag')
704    tag_value = tag.value
705    summary = tag.CreateValueFromType(tag.type).GetChildMemberWithName('encodings').GetChildMemberWithName(tag_value.removeprefix('.').removeprefix('@"').removesuffix('"').replace(r'\"', '"')).GetChildMemberWithName('summary')
706    if not summary: return tag_value
707    return re.sub(
708        expr_path_re,
709        lambda matchobj: getattr(unwrapped.GetValueForExpressionPath(matchobj[1]), matchobj[2]).strip(matchobj[3] or ''),
710        summary.summary.removeprefix('.').removeprefix('@"').removesuffix('"').replace(r'\"', '"'),
711    )
712
713class root_InternPool_Index_SynthProvider:
714    def __init__(self, value, _=None): self.value = value
715    def update(self):
716        self.unwrapped = None
717        wrapped = self.value.unsigned
718        if wrapped == (1 << 32) - 1: return
719        unwrapped_type = self.value.type.FindDirectNestedType('Unwrapped')
720        ip = self.value.CreateValueFromType(unwrapped_type).GetChildMemberWithName('debug_state').GetChildMemberWithName('intern_pool').GetNonSyntheticValue().GetChildMemberWithName('?')
721        tid_shift_30 = ip.GetChildMemberWithName('tid_shift_30').unsigned
722        self.unwrapped = create_struct(self.value, '.unwrapped', unwrapped_type, { 'tid': wrapped >> tid_shift_30, 'index': wrapped & (1 << tid_shift_30) - 1 })
723    def has_children(self): return True
724    def num_children(self): return 0
725    def get_child_index(self, name):
726        try: return ('unwrapped',).index(name)
727        except: pass
728    def get_child_at_index(self, index):
729        try: return (self.unwrapped,)[index]
730        except: pass
731
732class root_InternPool_Index_Unwrapped_SynthProvider:
733    def __init__(self, value, _=None): self.value = value
734    def update(self):
735        self.tag, self.index, self.data, self.payload, self.trailing = None, None, None, None, None
736        index = self.value.GetChildMemberWithName('index')
737        ip = self.value.CreateValueFromType(self.value.type).GetChildMemberWithName('debug_state').GetChildMemberWithName('intern_pool').GetNonSyntheticValue().GetChildMemberWithName('?')
738        shared = ip.GetChildMemberWithName('locals').GetSyntheticValue().child[self.value.GetChildMemberWithName('tid').unsigned].GetChildMemberWithName('shared')
739        item = shared.GetChildMemberWithName('items').GetChildMemberWithName('view').child[index.unsigned]
740        self.tag, item_data = item.GetChildMemberWithName('tag'), item.GetChildMemberWithName('data')
741        encoding = self.tag.CreateValueFromType(self.tag.type).GetChildMemberWithName('encodings').GetChildMemberWithName(self.tag.value.removeprefix('.').removeprefix('@"').removesuffix('"').replace(r'\"', '"'))
742        encoding_index, encoding_data, encoding_payload, encoding_trailing, encoding_config = encoding.GetChildMemberWithName('index'), encoding.GetChildMemberWithName('data'), encoding.GetChildMemberWithName('payload'), encoding.GetChildMemberWithName('trailing'), encoding.GetChildMemberWithName('config')
743        if encoding_index:
744            index_type = encoding_index.GetValueAsType()
745            index_bytes, index_data = index.data.uint8, lldb.SBData()
746            match index_data.byte_order:
747                case lldb.eByteOrderLittle:
748                    index_bytes = bytes(index_bytes[:index_type.size])
749                case lldb.eByteOrderBig:
750                    index_bytes = bytes(index_bytes[-index_type.size:])
751            index_data.SetData(lldb.SBError(), index_bytes, index_data.byte_order, index_data.GetAddressByteSize())
752            self.index = self.value.CreateValueFromData('.index', index_data, index_type)
753        elif encoding_data:
754            data_type = encoding_data.GetValueAsType()
755            data_bytes, data_data = item_data.data.uint8, lldb.SBData()
756            match data_data.byte_order:
757                case lldb.eByteOrderLittle:
758                    data_bytes = bytes(data_bytes[:data_type.size])
759                case lldb.eByteOrderBig:
760                    data_bytes = bytes(data_bytes[-data_type.size:])
761            data_data.SetData(lldb.SBError(), data_bytes, data_data.byte_order, data_data.GetAddressByteSize())
762            self.data = self.value.CreateValueFromData('.data', data_data, data_type)
763        elif encoding_payload:
764            extra = shared.GetChildMemberWithName('extra').GetChildMemberWithName('view').GetChildMemberWithName('0')
765            extra_index = item_data.unsigned
766            payload_type = encoding_payload.GetValueAsType()
767            payload_fields = dict()
768            for payload_field in payload_type.fields:
769                payload_fields[payload_field.name] = extra.child[extra_index]
770                extra_index += 1
771            self.payload = create_struct(self.value, '.payload', payload_type, payload_fields)
772            if encoding_trailing and encoding_config:
773                trailing_type = encoding_trailing.GetValueAsType()
774                trailing_bytes, trailing_data = bytearray(trailing_type.size), lldb.SBData()
775                def eval_config(config_name):
776                    expr = encoding_config.GetChildMemberWithName(config_name).summary.removeprefix('.').removeprefix('@"').removesuffix('"').replace(r'\"', '"')
777                    if 'payload.' in expr:
778                        return self.payload.EvaluateExpression(expr.replace('payload.', '@this().'))
779                    elif expr.startswith('trailing.'):
780                        field_type, field_byte_offset = trailing_type, 0
781                        expr_parts = expr.split('.')
782                        for expr_part in expr_parts[1:]:
783                            field = next(filter(lambda field: field.name == expr_part, field_type.fields))
784                            field_type = field.type
785                            field_byte_offset += field.byte_offset
786                        field_data = lldb.SBData()
787                        field_bytes = trailing_bytes[field_byte_offset:field_byte_offset + field_type.size]
788                        field_data.SetData(lldb.SBError(), field_bytes, field_data.byte_order, field_data.GetAddressByteSize())
789                        return self.value.CreateValueFromData('.%s' % expr_parts[-1], field_data, field_type)
790                    else:
791                        return self.value.frame.EvaluateExpression(expr)
792                for trailing_field in trailing_type.fields:
793                    trailing_field_type = trailing_field.type
794                    trailing_field_name = 'trailing.%s' % trailing_field.name
795                    trailing_field_byte_offset = trailing_field.byte_offset
796                    while True:
797                        match [trailing_field_type_field.name for trailing_field_type_field in trailing_field_type.fields]:
798                            case ['has_value', '?']:
799                                has_value_field, child_field = trailing_field_type.fields
800                                trailing_field_name = '%s.%s' % (trailing_field_name, child_field.name)
801                                match eval_config(trailing_field_name).value:
802                                    case 'true':
803                                        if has_value_field.type.name == 'bool':
804                                            trailing_bytes[trailing_field_byte_offset + has_value_field.byte_offset] = True
805                                        trailing_field_type = child_field.type
806                                        trailing_field_byte_offset += child_field.byte_offset
807                                    case 'false':
808                                        break
809                            case ['ptr', 'len']:
810                                ptr_field, len_field = trailing_field_type.fields
811                                ptr_field_byte_offset, len_field_byte_offset = trailing_field_byte_offset + ptr_field.byte_offset, trailing_field_byte_offset + len_field.byte_offset
812                                trailing_bytes[ptr_field_byte_offset:ptr_field_byte_offset + ptr_field.type.size] = extra.child[extra_index].address_of.data.uint8
813                                len_field_value = eval_config('%s.len' % trailing_field_name)
814                                len_field_size = len_field.type.size
815                                match trailing_data.byte_order:
816                                    case lldb.eByteOrderLittle:
817                                        len_field_bytes = len_field_value.data.uint8[:len_field_size]
818                                        trailing_bytes[len_field_byte_offset:len_field_byte_offset + len(len_field_bytes)] = len_field_bytes
819                                    case lldb.eByteOrderBig:
820                                        len_field_bytes = len_field_value.data.uint8[-len_field_size:]
821                                        len_field_end = len_field_byte_offset + len_field_size
822                                        trailing_bytes[len_field_end - len(len_field_bytes):len_field_end] = len_field_bytes
823                                extra_index += (ptr_field.type.GetPointeeType().size * len_field_value.unsigned + 3) // 4
824                                break
825                            case _:
826                                for offset in range(0, trailing_field_type.size, 4):
827                                    trailing_bytes[trailing_field_byte_offset + offset:trailing_field_byte_offset + offset + 4] = extra.child[extra_index].data.uint8
828                                    extra_index += 1
829                                break
830                trailing_data.SetData(lldb.SBError(), trailing_bytes, trailing_data.byte_order, trailing_data.GetAddressByteSize())
831                self.trailing = self.value.CreateValueFromData('.trailing', trailing_data, trailing_type)
832    def has_children(self): return True
833    def num_children(self): return 1 + ((self.index or self.data or self.payload) is not None) + (self.trailing is not None)
834    def get_child_index(self, name):
835        try: return ('tag', 'index' if self.index is not None else 'data' if self.data is not None else 'payload', 'trailing').index(name)
836        except: pass
837    def get_child_at_index(self, index):
838        try: return (self.tag, self.index or self.data or self.payload, self.trailing)[index]
839        except: pass
840
841def root_InternPool_String_SummaryProvider(value, _=None):
842    wrapped = value.unsigned
843    if wrapped == (1 << 32) - 1: return ''
844    ip = value.CreateValueFromType(value.type).GetChildMemberWithName('debug_state').GetChildMemberWithName('intern_pool').GetNonSyntheticValue().GetChildMemberWithName('?')
845    tid_shift_32 = ip.GetChildMemberWithName('tid_shift_32').unsigned
846    locals_value = ip.GetChildMemberWithName('locals').GetSyntheticValue()
847    local_value = locals_value.child[wrapped >> tid_shift_32]
848    if local_value is None:
849        wrapped = 0
850        local_value = locals_value.child[0]
851    string = local_value.GetChildMemberWithName('shared').GetChildMemberWithName('strings').GetChildMemberWithName('view').GetChildMemberWithName('0').child[wrapped & (1 << tid_shift_32) - 1].address_of
852    string.format = lldb.eFormatCString
853    return string.value
854
855class root_InternPool_TrackedInst_Index_SynthProvider:
856    def __init__(self, value, _=None): self.value = value
857    def update(self):
858        self.tracked_inst = None
859        wrapped = self.value.unsigned
860        if wrapped == (1 << 32) - 1: return
861        ip = self.value.CreateValueFromType(self.value.type).GetChildMemberWithName('debug_state').GetChildMemberWithName('intern_pool').GetNonSyntheticValue().GetChildMemberWithName('?')
862        tid_shift_32 = ip.GetChildMemberWithName('tid_shift_32').unsigned
863        locals_value = ip.GetChildMemberWithName('locals').GetSyntheticValue()
864        local_value = locals_value.child[wrapped >> tid_shift_32]
865        if local_value is None:
866            wrapped = 0
867            local_value = locals_value.child[0]
868        self.tracked_inst = local_value.GetChildMemberWithName('shared').GetChildMemberWithName('tracked_insts').GetChildMemberWithName('view').GetChildMemberWithName('0').child[wrapped & (1 << tid_shift_32) - 1]
869    def has_children(self): return False if self.tracked_inst is None else self.tracked_inst.GetNumChildren(1) > 0
870    def num_children(self): return 0 if self.tracked_inst is None else self.tracked_inst.GetNumChildren()
871    def get_child_index(self, name): return -1 if self.tracked_inst is None else self.tracked_inst.GetIndexOfChildWithName(name)
872    def get_child_at_index(self, index): return None if self.tracked_inst is None else self.tracked_inst.GetChildAtIndex(index)
873
874class root_InternPool_Nav_Index_SynthProvider:
875    def __init__(self, value, _=None): self.value = value
876    def update(self):
877        self.nav = None
878        wrapped = self.value.unsigned
879        if wrapped == (1 << 32) - 1: return
880        ip = self.value.CreateValueFromType(self.value.type).GetChildMemberWithName('debug_state').GetChildMemberWithName('intern_pool').GetNonSyntheticValue().GetChildMemberWithName('?')
881        tid_shift_32 = ip.GetChildMemberWithName('tid_shift_32').unsigned
882        locals_value = ip.GetChildMemberWithName('locals').GetSyntheticValue()
883        local_value = locals_value.child[wrapped >> tid_shift_32]
884        if local_value is None:
885            wrapped = 0
886            local_value = locals_value.child[0]
887        self.nav = local_value.GetChildMemberWithName('shared').GetChildMemberWithName('navs').GetChildMemberWithName('view').child[wrapped & (1 << tid_shift_32) - 1]
888    def has_children(self): return False if self.nav is None else self.nav.GetNumChildren(1) > 0
889    def num_children(self): return 0 if self.nav is None else self.nav.GetNumChildren()
890    def get_child_index(self, name): return -1 if self.nav is None else self.nav.GetIndexOfChildWithName(name)
891    def get_child_at_index(self, index): return None if self.nav is None else self.nav.GetChildAtIndex(index)
892
893# Initialize
894
895def add(debugger, *, category, regex=False, type, identifier=None, synth=False, inline_children=False, expand=False, summary=False):
896    prefix = '.'.join((__name__, (identifier or type).replace('.', '_').replace(':', '_')))
897    if summary: debugger.HandleCommand('type summary add --category %s%s%s "%s"' % (category, ' --inline-children' if inline_children else ''.join((' --expand' if expand else '', ' --python-function %s_SummaryProvider' % prefix if summary == True else ' --summary-string "%s"' % summary)), ' --regex' if regex else '', type))
898    if synth: debugger.HandleCommand('type synthetic add --category %s%s --python-class %s_SynthProvider "%s"' % (category, ' --regex' if regex else '', prefix, type))
899
900def __lldb_init_module(debugger, _=None):
901    # Initialize Zig Categories
902    debugger.HandleCommand('type category define --language c99 zig.lang zig.std')
903
904    # Initialize Zig Language
905    add(debugger, category='zig.lang', regex=True, type='^\\[\\]', identifier='zig_Slice', synth=True, expand=True, summary='len=${svar%#}')
906    add(debugger, category='zig.lang', type='[]u8', identifier='zig_String', summary=True)
907    add(debugger, category='zig.lang', regex=True, type='^\\?', identifier='zig_Optional', synth=True, summary=True)
908    add(debugger, category='zig.lang', regex=True, type='^(error{.*}|anyerror)!', identifier='zig_ErrorUnion', synth=True, inline_children=True, summary=True)
909
910    # Initialize Zig Standard Library
911    add(debugger, category='zig.std', type='mem.Allocator', summary='${var.ptr}')
912    add(debugger, category='zig.std', regex=True, type='^multi_array_list\\.MultiArrayList\\(.*\\)$', identifier='std_MultiArrayList', synth=True, expand=True, summary='len=${var.len} capacity=${var.capacity}')
913    add(debugger, category='zig.std', regex=True, type='^multi_array_list\\.MultiArrayList\\(.*\\)\\.Slice$', identifier='std_MultiArrayList_Slice', synth=True, expand=True, summary='len=${var.len} capacity=${var.capacity}')
914    add(debugger, category='zig.std', regex=True, type=MultiArrayList_Entry('.*'), identifier='std_Entry', synth=True, inline_children=True, summary=True)
915    add(debugger, category='zig.std', regex=True, type='^hash_map\\.HashMapUnmanaged\\(.*\\)$', identifier='std_HashMapUnmanaged', synth=True, expand=True, summary=True)
916    add(debugger, category='zig.std', regex=True, type='^hash_map\\.HashMapUnmanaged\\(.*\\)\\.Entry$', identifier = 'std_Entry', synth=True, inline_children=True, summary=True)
917
918    # Initialize Zig Stage2 Compiler
919    add(debugger, category='zig.stage2', type='Zir.Inst', identifier='TagAndPayload', synth=True, inline_children=True, summary=True)
920    add(debugger, category='zig.stage2', regex=True, type=MultiArrayList_Entry('Zir\\.Inst'), identifier='TagAndPayload', synth=True, inline_children=True, summary=True)
921    add(debugger, category='zig.stage2', regex=True, type='^Zir\\.Inst\\.Data\\.Data__struct_[1-9][0-9]*$', inline_children=True, summary=True)
922    add(debugger, category='zig.stage2', type='Zir.Inst::Zir.Inst.Ref', identifier='InstRef', summary=True)
923    add(debugger, category='zig.stage2', type='Zir.Inst::Zir.Inst.Index', identifier='InstIndex', summary=True)
924    add(debugger, category='zig.stage2', type='Air.Inst', identifier='TagAndPayload', synth=True, inline_children=True, summary=True)
925    add(debugger, category='zig.stage2', type='Air.Inst::Air.Inst.Ref', identifier='InstRef', summary=True)
926    add(debugger, category='zig.stage2', type='Air.Inst::Air.Inst.Index', identifier='InstIndex', summary=True)
927    add(debugger, category='zig.stage2', regex=True, type=MultiArrayList_Entry('Air\\.Inst'), identifier='TagAndPayload', synth=True, inline_children=True, summary=True)
928    add(debugger, category='zig.stage2', regex=True, type='^Air\\.Inst\\.Data\\.Data__struct_[1-9][0-9]*$', inline_children=True, summary=True)
929    add(debugger, category='zig.stage2', type='zig.DeclIndex', synth=True)
930    add(debugger, category='zig.stage2', type='Module.Namespace::Module.Namespace.Index', synth=True)
931    add(debugger, category='zig.stage2', type='Module.LazySrcLoc', identifier='zig_TaggedUnion', synth=True)
932    add(debugger, category='zig.stage2', type='InternPool.Index', synth=True)
933    add(debugger, category='zig.stage2', type='InternPool.NullTerminatedString', summary=True)
934    add(debugger, category='zig.stage2', type='InternPool.Key', identifier='zig_TaggedUnion', synth=True)
935    add(debugger, category='zig.stage2', type='InternPool.Key.Int.Storage', identifier='zig_TaggedUnion', synth=True)
936    add(debugger, category='zig.stage2', type='InternPool.Key.ErrorUnion.Value', identifier='zig_TaggedUnion', synth=True)
937    add(debugger, category='zig.stage2', type='InternPool.Key.Float.Storage', identifier='zig_TaggedUnion', synth=True)
938    add(debugger, category='zig.stage2', type='InternPool.Key.Ptr.Addr', identifier='zig_TaggedUnion', synth=True)
939    add(debugger, category='zig.stage2', type='InternPool.Key.Aggregate.Storage', identifier='zig_TaggedUnion', synth=True)
940    add(debugger, category='zig.stage2', type='arch.x86_64.CodeGen.MCValue', identifier='zig_TaggedUnion', synth=True, inline_children=True, summary=True)
941
942    # Initialize Zig Stage2 Compiler (compiled with the self-hosted backend)
943    add(debugger, category='zig', regex=True, type=r'^root\.InternPool\.Local\.List\(.*\)$', identifier='root_InternPool_Local_List', synth=True, expand=True, summary='capacity=${var%#}')
944    add(debugger, category='zig', type='root.InternPool.Index', synth=True, summary=True)
945    add(debugger, category='zig', type='root.InternPool.Index.Unwrapped', synth=True)
946    add(debugger, category='zig', regex=True, type=r'^root\.InternPool\.(Optional)?(NullTerminated)?String$', identifier='root_InternPool_String', summary=True)
947    add(debugger, category='zig', regex=True, type=r'^root\.InternPool\.TrackedInst\.Index(\.Optional)?$', identifier='root_InternPool_TrackedInst_Index', synth=True)
948    add(debugger, category='zig', regex=True, type=r'^root\.InternPool\.Nav\.Index(\.Optional)?$', identifier='root_InternPool_Nav_Index', synth=True)