ortem | 47c26e6 | 2019-05-14 12:50:58 | [diff] [blame] | 1 | import lldb |
| 2 | |
| 3 | from lldb_providers import * |
| 4 | from rust_types import RustType, classify_struct, classify_union |
| 5 | |
| 6 | |
| 7 | # BACKCOMPAT: rust 1.35 |
| 8 | def is_hashbrown_hashmap(hash_map): |
| 9 | return len(hash_map.type.fields) == 1 |
| 10 | |
| 11 | |
| 12 | def 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 | |
| 22 | def 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 | return "" |
| 59 | |
| 60 | |
| 61 | def synthetic_lookup(valobj, dict): |
| 62 | # type: (SBValue, dict) -> object |
| 63 | """Returns the synthetic provider for the given value""" |
| 64 | rust_type = classify_rust_type(valobj.GetType()) |
| 65 | |
| 66 | if rust_type == RustType.STRUCT: |
| 67 | return StructSyntheticProvider(valobj, dict) |
| 68 | if rust_type == RustType.STRUCT_VARIANT: |
| 69 | return StructSyntheticProvider(valobj, dict, is_variant=True) |
| 70 | if rust_type == RustType.TUPLE: |
| 71 | return TupleSyntheticProvider(valobj, dict) |
| 72 | if rust_type == RustType.TUPLE_VARIANT: |
| 73 | return TupleSyntheticProvider(valobj, dict, is_variant=True) |
| 74 | if rust_type == RustType.EMPTY: |
| 75 | return EmptySyntheticProvider(valobj, dict) |
| 76 | if rust_type == RustType.REGULAR_ENUM: |
| 77 | discriminant = valobj.GetChildAtIndex(0).GetChildAtIndex(0).GetValueAsUnsigned() |
| 78 | return synthetic_lookup(valobj.GetChildAtIndex(discriminant), dict) |
| 79 | if rust_type == RustType.SINGLETON_ENUM: |
| 80 | return synthetic_lookup(valobj.GetChildAtIndex(0), dict) |
| 81 | |
| 82 | if rust_type == RustType.STD_VEC: |
| 83 | return StdVecSyntheticProvider(valobj, dict) |
| 84 | if rust_type == RustType.STD_VEC_DEQUE: |
| 85 | return StdVecDequeSyntheticProvider(valobj, dict) |
| 86 | if rust_type == RustType.STD_SLICE: |
| 87 | return StdSliceSyntheticProvider(valobj, dict) |
| 88 | |
| 89 | if rust_type == RustType.STD_HASH_MAP: |
| 90 | if is_hashbrown_hashmap(valobj): |
| 91 | return StdHashMapSyntheticProvider(valobj, dict) |
| 92 | else: |
| 93 | return StdOldHashMapSyntheticProvider(valobj, dict) |
| 94 | if rust_type == RustType.STD_HASH_SET: |
| 95 | hash_map = valobj.GetChildAtIndex(0) |
| 96 | if is_hashbrown_hashmap(hash_map): |
Matt Brubeck | ebd15e7 | 2020-08-10 23:19:54 | [diff] [blame] | 97 | return StdHashMapSyntheticProvider(valobj, dict, show_values=False) |
ortem | 47c26e6 | 2019-05-14 12:50:58 | [diff] [blame] | 98 | else: |
| 99 | return StdOldHashMapSyntheticProvider(hash_map, dict, show_values=False) |
| 100 | |
| 101 | if rust_type == RustType.STD_RC: |
| 102 | return StdRcSyntheticProvider(valobj, dict) |
| 103 | if rust_type == RustType.STD_ARC: |
| 104 | return StdRcSyntheticProvider(valobj, dict, is_atomic=True) |
| 105 | |
| 106 | if rust_type == RustType.STD_CELL: |
| 107 | return StdCellSyntheticProvider(valobj, dict) |
| 108 | if rust_type == RustType.STD_REF: |
| 109 | return StdRefSyntheticProvider(valobj, dict) |
| 110 | if rust_type == RustType.STD_REF_MUT: |
| 111 | return StdRefSyntheticProvider(valobj, dict) |
| 112 | if rust_type == RustType.STD_REF_CELL: |
| 113 | return StdRefSyntheticProvider(valobj, dict, is_cell=True) |
| 114 | |
| 115 | return DefaultSynthteticProvider(valobj, dict) |