| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Reflection.Metadata.Ecma335; |
| | | 7 | | |
| | | 8 | | namespace System.Reflection.Metadata |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents any metadata entity (type reference/definition/specification, method definition, custom attribute, et |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Use <see cref="Handle"/> to store multiple kinds of handles. |
| | | 15 | | /// </remarks> |
| | | 16 | | public readonly struct Handle : IEquatable<Handle> |
| | | 17 | | { |
| | | 18 | | private readonly int _value; |
| | | 19 | | |
| | | 20 | | // bits: |
| | | 21 | | // 7: IsVirtual |
| | | 22 | | // 0..6: token type |
| | | 23 | | private readonly byte _vType; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Creates <see cref="Handle"/> from a token or a token combined with a virtual flag. |
| | | 27 | | /// </summary> |
| | | 28 | | internal static Handle FromVToken(uint vToken) |
| | 0 | 29 | | { |
| | 0 | 30 | | return new Handle((byte)(vToken >> TokenTypeIds.RowIdBitCount), (int)(vToken & TokenTypeIds.RIDMask)); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | internal Handle(byte vType, int value) |
| | 0 | 34 | | { |
| | 0 | 35 | | _vType = vType; |
| | 0 | 36 | | _value = value; |
| | | 37 | | |
| | 0 | 38 | | Debug.Assert(value >= 0); |
| | | 39 | | |
| | | 40 | | // No table can have more than 2^24 rows. |
| | | 41 | | // User String heap is also limited by 2^24 since user strings have tokens in IL. |
| | | 42 | | // We limit the size of #Blob, #String and #GUID heaps to 2^29 (max compressed integer) in order |
| | | 43 | | // to keep the sizes of corresponding handles to 32 bit. As a result we don't support reading metadata |
| | | 44 | | // files with heaps larger than 0.5GB. |
| | 0 | 45 | | Debug.Assert(IsHeapHandle && value <= HeapHandleType.OffsetMask || |
| | 0 | 46 | | !IsHeapHandle && value <= TokenTypeIds.RIDMask); |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | // for entity handles: |
| | | 50 | | internal int RowId |
| | | 51 | | { |
| | | 52 | | get |
| | 0 | 53 | | { |
| | 0 | 54 | | Debug.Assert(!IsHeapHandle); |
| | 0 | 55 | | return _value; |
| | 0 | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | // for heap handles: |
| | | 60 | | internal int Offset |
| | | 61 | | { |
| | | 62 | | get |
| | 0 | 63 | | { |
| | 0 | 64 | | Debug.Assert(IsHeapHandle); |
| | 0 | 65 | | return _value; |
| | 0 | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Token type (0x##000000), does not include virtual flag. |
| | | 71 | | /// </summary> |
| | | 72 | | internal uint EntityHandleType |
| | | 73 | | { |
| | 0 | 74 | | get { return Type << TokenTypeIds.RowIdBitCount; } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Small token type (0x##), does not include virtual flag. |
| | | 79 | | /// </summary> |
| | | 80 | | internal uint Type |
| | | 81 | | { |
| | 0 | 82 | | get { return _vType & HandleType.TypeMask; } |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Value stored in an <see cref="EntityHandle"/>. |
| | | 87 | | /// </summary> |
| | | 88 | | internal uint EntityHandleValue |
| | | 89 | | { |
| | | 90 | | get |
| | 0 | 91 | | { |
| | 0 | 92 | | Debug.Assert((_value & TokenTypeIds.RIDMask) == _value); |
| | 0 | 93 | | return (uint)_vType << TokenTypeIds.RowIdBitCount | (uint)_value; |
| | 0 | 94 | | } |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Value stored in a concrete entity handle (see <see cref="TypeDefinitionHandle"/>, <see cref="MethodDefinitio |
| | | 99 | | /// </summary> |
| | | 100 | | internal uint SpecificEntityHandleValue |
| | | 101 | | { |
| | | 102 | | get |
| | 0 | 103 | | { |
| | 0 | 104 | | Debug.Assert((_value & TokenTypeIds.RIDMask) == _value); |
| | 0 | 105 | | return (_vType & HandleType.VirtualBit) << TokenTypeIds.RowIdBitCount | (uint)_value; |
| | 0 | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | internal byte VType |
| | | 110 | | { |
| | 0 | 111 | | get { return _vType; } |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | internal bool IsVirtual |
| | | 115 | | { |
| | 0 | 116 | | get { return (_vType & HandleType.VirtualBit) != 0; } |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | internal bool IsHeapHandle |
| | | 120 | | { |
| | 0 | 121 | | get { return (_vType & HandleType.HeapMask) == HandleType.HeapMask; } |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public HandleKind Kind |
| | | 125 | | { |
| | | 126 | | get |
| | 0 | 127 | | { |
| | 0 | 128 | | uint type = Type; |
| | | 129 | | |
| | | 130 | | // Do not surface extra non-virtual string type bits in public handle kind |
| | 0 | 131 | | if ((type & ~HandleType.NonVirtualStringTypeMask) == HandleType.String) |
| | 0 | 132 | | { |
| | 0 | 133 | | return HandleKind.String; |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | return (HandleKind)type; |
| | 0 | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | public bool IsNil |
| | | 141 | | { |
| | | 142 | | // virtual handles are never nil |
| | 0 | 143 | | get { return ((uint)_value | (_vType & HandleType.VirtualBit)) == 0; } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | internal bool IsEntityOrUserStringHandle |
| | | 147 | | { |
| | 0 | 148 | | get { return Type <= HandleType.UserString; } |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | internal int Token |
| | | 152 | | { |
| | | 153 | | get |
| | 0 | 154 | | { |
| | 0 | 155 | | Debug.Assert(IsEntityOrUserStringHandle); |
| | 0 | 156 | | Debug.Assert(!IsVirtual); |
| | 0 | 157 | | Debug.Assert((_value & TokenTypeIds.RIDMask) == _value); |
| | | 158 | | |
| | 0 | 159 | | return _vType << TokenTypeIds.RowIdBitCount | _value; |
| | 0 | 160 | | } |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | public override bool Equals([NotNullWhen(true)] object? obj) |
| | 0 | 164 | | { |
| | 0 | 165 | | return obj is Handle handle && Equals(handle); |
| | 0 | 166 | | } |
| | | 167 | | |
| | | 168 | | public bool Equals(Handle other) |
| | 0 | 169 | | { |
| | 0 | 170 | | return _value == other._value && _vType == other._vType; |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | public override int GetHashCode() |
| | 0 | 174 | | { |
| | 0 | 175 | | return _value ^ (_vType << 24); |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | public static bool operator ==(Handle left, Handle right) |
| | 0 | 179 | | { |
| | 0 | 180 | | return left.Equals(right); |
| | 0 | 181 | | } |
| | | 182 | | |
| | | 183 | | public static bool operator !=(Handle left, Handle right) |
| | 0 | 184 | | { |
| | 0 | 185 | | return !left.Equals(right); |
| | 0 | 186 | | } |
| | | 187 | | |
| | | 188 | | internal static int Compare(Handle left, Handle right) |
| | 0 | 189 | | { |
| | | 190 | | // All virtual tokens will be sorted after non-virtual tokens. |
| | | 191 | | // The order of handles that differ in kind is undefined, |
| | | 192 | | // but we include it so that we ensure consistency with == and != operators. |
| | 0 | 193 | | return ((long)(uint)left._value | (long)left._vType << 32).CompareTo((long)(uint)right._value | (long)right. |
| | 0 | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | public static readonly ModuleDefinitionHandle ModuleDefinition = new ModuleDefinitionHandle(1); |
| | 0 | 197 | | public static readonly AssemblyDefinitionHandle AssemblyDefinition = new AssemblyDefinitionHandle(1); |
| | | 198 | | } |
| | | 199 | | } |
| | | 200 | | |