• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import lldb
2
3from lldb_providers import *
4from rust_types import RustType, classify_struct, classify_union
5
6
7# BACKCOMPAT: rust 1.35
8def is_hashbrown_hashmap(hash_map):
9    return len(hash_map.type.fields) == 1
10
11
12def classify_rust_type(type):
13    type_class = type.GetTypeClass()
14    if type_class == lldb.eTypeClassStruct:
15        return classify_struct(type.name, type.fields)
16    if type_class == lldb.eTypeClassUnion:
17        return classify_union(type.fields)
18
19    return RustType.OTHER
20
21
22def summary_lookup(valobj, dict):
23    # type: (SBValue, dict) -> str
24    """Returns the summary provider for the given value"""
25    rust_type = classify_rust_type(valobj.GetType())
26
27    if rust_type == RustType.STD_STRING:
28        return StdStringSummaryProvider(valobj, dict)
29    if rust_type == RustType.STD_OS_STRING:
30        return StdOsStringSummaryProvider(valobj, dict)
31    if rust_type == RustType.STD_STR:
32        return StdStrSummaryProvider(valobj, dict)
33
34    if rust_type == RustType.STD_VEC:
35        return SizeSummaryProvider(valobj, dict)
36    if rust_type == RustType.STD_VEC_DEQUE:
37        return SizeSummaryProvider(valobj, dict)
38    if rust_type == RustType.STD_SLICE:
39        return SizeSummaryProvider(valobj, dict)
40
41    if rust_type == RustType.STD_HASH_MAP:
42        return SizeSummaryProvider(valobj, dict)
43    if rust_type == RustType.STD_HASH_SET:
44        return SizeSummaryProvider(valobj, dict)
45
46    if rust_type == RustType.STD_RC:
47        return StdRcSummaryProvider(valobj, dict)
48    if rust_type == RustType.STD_ARC:
49        return StdRcSummaryProvider(valobj, dict)
50
51    if rust_type == RustType.STD_REF:
52        return StdRefSummaryProvider(valobj, dict)
53    if rust_type == RustType.STD_REF_MUT:
54        return StdRefSummaryProvider(valobj, dict)
55    if rust_type == RustType.STD_REF_CELL:
56        return StdRefSummaryProvider(valobj, dict)
57
58    if rust_type == RustType.STD_NONZERO_NUMBER:
59        return StdNonZeroNumberSummaryProvider(valobj, dict)
60
61    return ""
62
63
64def synthetic_lookup(valobj, dict):
65    # type: (SBValue, dict) -> object
66    """Returns the synthetic provider for the given value"""
67    rust_type = classify_rust_type(valobj.GetType())
68
69    if rust_type == RustType.STRUCT:
70        return StructSyntheticProvider(valobj, dict)
71    if rust_type == RustType.STRUCT_VARIANT:
72        return StructSyntheticProvider(valobj, dict, is_variant=True)
73    if rust_type == RustType.TUPLE:
74        return TupleSyntheticProvider(valobj, dict)
75    if rust_type == RustType.TUPLE_VARIANT:
76        return TupleSyntheticProvider(valobj, dict, is_variant=True)
77    if rust_type == RustType.EMPTY:
78        return EmptySyntheticProvider(valobj, dict)
79    if rust_type == RustType.REGULAR_ENUM:
80        discriminant = valobj.GetChildAtIndex(0).GetChildAtIndex(0).GetValueAsUnsigned()
81        return synthetic_lookup(valobj.GetChildAtIndex(discriminant), dict)
82    if rust_type == RustType.SINGLETON_ENUM:
83        return synthetic_lookup(valobj.GetChildAtIndex(0), dict)
84
85    if rust_type == RustType.STD_VEC:
86        return StdVecSyntheticProvider(valobj, dict)
87    if rust_type == RustType.STD_VEC_DEQUE:
88        return StdVecDequeSyntheticProvider(valobj, dict)
89    if rust_type == RustType.STD_SLICE:
90        return StdSliceSyntheticProvider(valobj, dict)
91
92    if rust_type == RustType.STD_HASH_MAP:
93        if is_hashbrown_hashmap(valobj):
94            return StdHashMapSyntheticProvider(valobj, dict)
95        else:
96            return StdOldHashMapSyntheticProvider(valobj, dict)
97    if rust_type == RustType.STD_HASH_SET:
98        hash_map = valobj.GetChildAtIndex(0)
99        if is_hashbrown_hashmap(hash_map):
100            return StdHashMapSyntheticProvider(valobj, dict, show_values=False)
101        else:
102            return StdOldHashMapSyntheticProvider(hash_map, dict, show_values=False)
103
104    if rust_type == RustType.STD_RC:
105        return StdRcSyntheticProvider(valobj, dict)
106    if rust_type == RustType.STD_ARC:
107        return StdRcSyntheticProvider(valobj, dict, is_atomic=True)
108
109    if rust_type == RustType.STD_CELL:
110        return StdCellSyntheticProvider(valobj, dict)
111    if rust_type == RustType.STD_REF:
112        return StdRefSyntheticProvider(valobj, dict)
113    if rust_type == RustType.STD_REF_MUT:
114        return StdRefSyntheticProvider(valobj, dict)
115    if rust_type == RustType.STD_REF_CELL:
116        return StdRefSyntheticProvider(valobj, dict, is_cell=True)
117
118    return DefaultSyntheticProvider(valobj, dict)
119