| | | 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 a metadata entity (type reference/definition/specification, method definition, custom attribute, etc. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Use <see cref="EntityHandle"/> to store multiple kinds of entity handles. |
| | | 15 | | /// It has smaller memory footprint than <see cref="Handle"/>. |
| | | 16 | | /// </remarks> |
| | | 17 | | public readonly struct EntityHandle : IEquatable<EntityHandle> |
| | | 18 | | { |
| | | 19 | | // bits: |
| | | 20 | | // 31: IsVirtual |
| | | 21 | | // 24..30: type |
| | | 22 | | // 0..23: row id |
| | | 23 | | private readonly uint _vToken; |
| | | 24 | | |
| | | 25 | | internal EntityHandle(uint vToken) |
| | 0 | 26 | | { |
| | 0 | 27 | | _vToken = vToken; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public static implicit operator Handle(EntityHandle handle) |
| | 0 | 31 | | { |
| | 0 | 32 | | return Handle.FromVToken(handle._vToken); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public static explicit operator EntityHandle(Handle handle) |
| | 0 | 36 | | { |
| | 0 | 37 | | if (handle.IsHeapHandle) |
| | 0 | 38 | | { |
| | 0 | 39 | | Throw.InvalidCast(); |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | return new EntityHandle(handle.EntityHandleValue); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | internal uint Type |
| | | 46 | | { |
| | 0 | 47 | | get { return _vToken & TokenTypeIds.TypeMask; } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | internal uint VType |
| | | 51 | | { |
| | 0 | 52 | | get { return _vToken & (TokenTypeIds.VirtualBit | TokenTypeIds.TypeMask); } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | internal bool IsVirtual |
| | | 56 | | { |
| | 0 | 57 | | get { return (_vToken & TokenTypeIds.VirtualBit) != 0; } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public bool IsNil |
| | | 61 | | { |
| | | 62 | | // virtual handle is never nil |
| | 0 | 63 | | get { return (_vToken & (TokenTypeIds.VirtualBit | TokenTypeIds.RIDMask)) == 0; } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | internal int RowId |
| | | 67 | | { |
| | 0 | 68 | | get { return (int)(_vToken & TokenTypeIds.RIDMask); } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Value stored in a specific entity handle (see <see cref="TypeDefinitionHandle"/>, <see cref="MethodDefinitio |
| | | 73 | | /// </summary> |
| | | 74 | | internal uint SpecificHandleValue |
| | | 75 | | { |
| | 0 | 76 | | get { return _vToken & (TokenTypeIds.VirtualBit | TokenTypeIds.RIDMask); } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public HandleKind Kind |
| | | 80 | | { |
| | | 81 | | get |
| | 0 | 82 | | { |
| | | 83 | | // EntityHandles cannot be StringHandles and therefore we do not need |
| | | 84 | | // to handle stripping the extra non-virtual string type bits here. |
| | 0 | 85 | | return (HandleKind)(Type >> TokenTypeIds.RowIdBitCount); |
| | 0 | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | internal int Token |
| | | 90 | | { |
| | | 91 | | get |
| | 0 | 92 | | { |
| | 0 | 93 | | Debug.Assert(!IsVirtual); |
| | 0 | 94 | | return (int)_vToken; |
| | 0 | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | public override bool Equals([NotNullWhen(true)] object? obj) |
| | 0 | 99 | | { |
| | 0 | 100 | | return obj is EntityHandle entityHandle && Equals(entityHandle); |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | public bool Equals(EntityHandle other) |
| | 0 | 104 | | { |
| | 0 | 105 | | return _vToken == other._vToken; |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | public override int GetHashCode() |
| | 0 | 109 | | { |
| | 0 | 110 | | return unchecked((int)_vToken); |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | public static bool operator ==(EntityHandle left, EntityHandle right) |
| | 0 | 114 | | { |
| | 0 | 115 | | return left.Equals(right); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | public static bool operator !=(EntityHandle left, EntityHandle right) |
| | 0 | 119 | | { |
| | 0 | 120 | | return !left.Equals(right); |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | internal static int Compare(EntityHandle left, EntityHandle right) |
| | 0 | 124 | | { |
| | | 125 | | // All virtual tokens will be sorted after non-virtual tokens. |
| | | 126 | | // The order of handles that differ in kind is undefined, |
| | | 127 | | // but we include it so that we ensure consistency with == and != operators. |
| | 0 | 128 | | return left._vToken.CompareTo(right._vToken); |
| | 0 | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | public static readonly ModuleDefinitionHandle ModuleDefinition = new ModuleDefinitionHandle(1); |
| | 0 | 132 | | public static readonly AssemblyDefinitionHandle AssemblyDefinition = new AssemblyDefinitionHandle(1); |
| | | 133 | | } |
| | | 134 | | } |
| | | 135 | | |