| | | 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.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 7 | | { |
| | | 8 | | internal static class HasCustomAttributeTag |
| | | 9 | | { |
| | | 10 | | internal const int NumberOfBits = 5; |
| | | 11 | | internal const int LargeRowSize = 0x00000001 << (16 - NumberOfBits); |
| | | 12 | | internal const uint MethodDef = 0x00000000; |
| | | 13 | | internal const uint Field = 0x00000001; |
| | | 14 | | internal const uint TypeRef = 0x00000002; |
| | | 15 | | internal const uint TypeDef = 0x00000003; |
| | | 16 | | internal const uint Param = 0x00000004; |
| | | 17 | | internal const uint InterfaceImpl = 0x00000005; |
| | | 18 | | internal const uint MemberRef = 0x00000006; |
| | | 19 | | internal const uint Module = 0x00000007; |
| | | 20 | | internal const uint DeclSecurity = 0x00000008; |
| | | 21 | | internal const uint Property = 0x00000009; |
| | | 22 | | internal const uint Event = 0x0000000A; |
| | | 23 | | internal const uint StandAloneSig = 0x0000000B; |
| | | 24 | | internal const uint ModuleRef = 0x0000000C; |
| | | 25 | | internal const uint TypeSpec = 0x0000000D; |
| | | 26 | | internal const uint Assembly = 0x0000000E; |
| | | 27 | | internal const uint AssemblyRef = 0x0000000F; |
| | | 28 | | internal const uint File = 0x00000010; |
| | | 29 | | internal const uint ExportedType = 0x00000011; |
| | | 30 | | internal const uint ManifestResource = 0x00000012; |
| | | 31 | | internal const uint GenericParam = 0x00000013; |
| | | 32 | | internal const uint GenericParamConstraint = 0x00000014; |
| | | 33 | | internal const uint MethodSpec = 0x00000015; |
| | | 34 | | internal const uint TagMask = 0x0000001F; |
| | | 35 | | |
| | | 36 | | // Arbitrary value not equal to any of the token types in the array. This includes 0 which is TokenTypeIds.Modul |
| | | 37 | | internal const uint InvalidTokenType = uint.MaxValue; |
| | | 38 | | |
| | | 39 | | internal static ReadOnlySpan<uint> TagToTokenTypeArray => |
| | 0 | 40 | | [ |
| | 0 | 41 | | TokenTypeIds.MethodDef, |
| | 0 | 42 | | TokenTypeIds.FieldDef, |
| | 0 | 43 | | TokenTypeIds.TypeRef, |
| | 0 | 44 | | TokenTypeIds.TypeDef, |
| | 0 | 45 | | TokenTypeIds.ParamDef, |
| | 0 | 46 | | TokenTypeIds.InterfaceImpl, |
| | 0 | 47 | | TokenTypeIds.MemberRef, |
| | 0 | 48 | | TokenTypeIds.Module, |
| | 0 | 49 | | TokenTypeIds.DeclSecurity, |
| | 0 | 50 | | TokenTypeIds.Property, |
| | 0 | 51 | | TokenTypeIds.Event, |
| | 0 | 52 | | TokenTypeIds.Signature, |
| | 0 | 53 | | TokenTypeIds.ModuleRef, |
| | 0 | 54 | | TokenTypeIds.TypeSpec, |
| | 0 | 55 | | TokenTypeIds.Assembly, |
| | 0 | 56 | | TokenTypeIds.AssemblyRef, |
| | 0 | 57 | | TokenTypeIds.File, |
| | 0 | 58 | | TokenTypeIds.ExportedType, |
| | 0 | 59 | | TokenTypeIds.ManifestResource, |
| | 0 | 60 | | TokenTypeIds.GenericParam, |
| | 0 | 61 | | TokenTypeIds.GenericParamConstraint, |
| | 0 | 62 | | TokenTypeIds.MethodSpec, |
| | 0 | 63 | | |
| | 0 | 64 | | InvalidTokenType, |
| | 0 | 65 | | InvalidTokenType, |
| | 0 | 66 | | InvalidTokenType, |
| | 0 | 67 | | InvalidTokenType, |
| | 0 | 68 | | InvalidTokenType, |
| | 0 | 69 | | InvalidTokenType, |
| | 0 | 70 | | InvalidTokenType, |
| | 0 | 71 | | InvalidTokenType, |
| | 0 | 72 | | InvalidTokenType, |
| | 0 | 73 | | InvalidTokenType |
| | 0 | 74 | | ]; |
| | | 75 | | |
| | | 76 | | internal const TableMask TablesReferenced = |
| | | 77 | | TableMask.MethodDef |
| | | 78 | | | TableMask.Field |
| | | 79 | | | TableMask.TypeRef |
| | | 80 | | | TableMask.TypeDef |
| | | 81 | | | TableMask.Param |
| | | 82 | | | TableMask.InterfaceImpl |
| | | 83 | | | TableMask.MemberRef |
| | | 84 | | | TableMask.Module |
| | | 85 | | | TableMask.DeclSecurity |
| | | 86 | | | TableMask.Property |
| | | 87 | | | TableMask.Event |
| | | 88 | | | TableMask.StandAloneSig |
| | | 89 | | | TableMask.ModuleRef |
| | | 90 | | | TableMask.TypeSpec |
| | | 91 | | | TableMask.Assembly |
| | | 92 | | | TableMask.AssemblyRef |
| | | 93 | | | TableMask.File |
| | | 94 | | | TableMask.ExportedType |
| | | 95 | | | TableMask.ManifestResource |
| | | 96 | | | TableMask.GenericParam |
| | | 97 | | | TableMask.GenericParamConstraint |
| | | 98 | | | TableMask.MethodSpec; |
| | | 99 | | |
| | | 100 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 101 | | internal static EntityHandle ConvertToHandle(uint hasCustomAttribute) |
| | 0 | 102 | | { |
| | 0 | 103 | | uint tokenType = TagToTokenTypeArray[(int)(hasCustomAttribute & TagMask)]; |
| | 0 | 104 | | uint rowId = (hasCustomAttribute >> NumberOfBits); |
| | | 105 | | |
| | 0 | 106 | | if (tokenType == InvalidTokenType || ((rowId & ~TokenTypeIds.RIDMask) != 0)) |
| | 0 | 107 | | { |
| | 0 | 108 | | Throw.InvalidCodedIndex(); |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | return new EntityHandle(tokenType | rowId); |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | internal static uint ConvertToTag(EntityHandle handle) |
| | 0 | 115 | | { |
| | 0 | 116 | | uint tokenType = handle.Type; |
| | 0 | 117 | | uint rowId = (uint)handle.RowId; |
| | 0 | 118 | | return (tokenType >> TokenTypeIds.RowIdBitCount) switch |
| | 0 | 119 | | { |
| | 0 | 120 | | TokenTypeIds.MethodDef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | MethodDef, |
| | 0 | 121 | | TokenTypeIds.FieldDef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | Field, |
| | 0 | 122 | | TokenTypeIds.TypeRef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | TypeRef, |
| | 0 | 123 | | TokenTypeIds.TypeDef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | TypeDef, |
| | 0 | 124 | | TokenTypeIds.ParamDef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | Param, |
| | 0 | 125 | | TokenTypeIds.InterfaceImpl >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | InterfaceImpl, |
| | 0 | 126 | | TokenTypeIds.MemberRef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | MemberRef, |
| | 0 | 127 | | TokenTypeIds.Module >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | Module, |
| | 0 | 128 | | TokenTypeIds.DeclSecurity >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | DeclSecurity, |
| | 0 | 129 | | TokenTypeIds.Property >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | Property, |
| | 0 | 130 | | TokenTypeIds.Event >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | Event, |
| | 0 | 131 | | TokenTypeIds.Signature >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | StandAloneSig, |
| | 0 | 132 | | TokenTypeIds.ModuleRef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | ModuleRef, |
| | 0 | 133 | | TokenTypeIds.TypeSpec >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | TypeSpec, |
| | 0 | 134 | | TokenTypeIds.Assembly >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | Assembly, |
| | 0 | 135 | | TokenTypeIds.AssemblyRef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | AssemblyRef, |
| | 0 | 136 | | TokenTypeIds.File >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | File, |
| | 0 | 137 | | TokenTypeIds.ExportedType >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | ExportedType, |
| | 0 | 138 | | TokenTypeIds.ManifestResource >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | ManifestResource, |
| | 0 | 139 | | TokenTypeIds.GenericParam >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | GenericParam, |
| | 0 | 140 | | TokenTypeIds.GenericParamConstraint >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | GenericPara |
| | 0 | 141 | | TokenTypeIds.MethodSpec >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | MethodSpec, |
| | 0 | 142 | | |
| | 0 | 143 | | _ => 0, |
| | 0 | 144 | | }; |
| | 0 | 145 | | } |
| | | 146 | | } |
| | | 147 | | } |
| | | 148 | | |