< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 74
Coverable lines: 74
Total lines: 148
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 46
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
ConvertToHandle(...)0%440%
ConvertToTag(...)0%42420%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/Internal/HasCustomAttributeTag.cs

#LineLine coverage
 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
 4using System.Runtime.CompilerServices;
 5
 6namespace 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 =>
 040        [
 041            TokenTypeIds.MethodDef,
 042            TokenTypeIds.FieldDef,
 043            TokenTypeIds.TypeRef,
 044            TokenTypeIds.TypeDef,
 045            TokenTypeIds.ParamDef,
 046            TokenTypeIds.InterfaceImpl,
 047            TokenTypeIds.MemberRef,
 048            TokenTypeIds.Module,
 049            TokenTypeIds.DeclSecurity,
 050            TokenTypeIds.Property,
 051            TokenTypeIds.Event,
 052            TokenTypeIds.Signature,
 053            TokenTypeIds.ModuleRef,
 054            TokenTypeIds.TypeSpec,
 055            TokenTypeIds.Assembly,
 056            TokenTypeIds.AssemblyRef,
 057            TokenTypeIds.File,
 058            TokenTypeIds.ExportedType,
 059            TokenTypeIds.ManifestResource,
 060            TokenTypeIds.GenericParam,
 061            TokenTypeIds.GenericParamConstraint,
 062            TokenTypeIds.MethodSpec,
 063
 064            InvalidTokenType,
 065            InvalidTokenType,
 066            InvalidTokenType,
 067            InvalidTokenType,
 068            InvalidTokenType,
 069            InvalidTokenType,
 070            InvalidTokenType,
 071            InvalidTokenType,
 072            InvalidTokenType,
 073            InvalidTokenType
 074        ];
 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)
 0102        {
 0103            uint tokenType = TagToTokenTypeArray[(int)(hasCustomAttribute & TagMask)];
 0104            uint rowId = (hasCustomAttribute >> NumberOfBits);
 105
 0106            if (tokenType == InvalidTokenType || ((rowId & ~TokenTypeIds.RIDMask) != 0))
 0107            {
 0108                Throw.InvalidCodedIndex();
 109            }
 110
 0111            return new EntityHandle(tokenType | rowId);
 0112        }
 113
 114        internal static uint ConvertToTag(EntityHandle handle)
 0115        {
 0116            uint tokenType = handle.Type;
 0117            uint rowId = (uint)handle.RowId;
 0118            return (tokenType >> TokenTypeIds.RowIdBitCount) switch
 0119            {
 0120                TokenTypeIds.MethodDef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | MethodDef,
 0121                TokenTypeIds.FieldDef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | Field,
 0122                TokenTypeIds.TypeRef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | TypeRef,
 0123                TokenTypeIds.TypeDef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | TypeDef,
 0124                TokenTypeIds.ParamDef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | Param,
 0125                TokenTypeIds.InterfaceImpl >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | InterfaceImpl,
 0126                TokenTypeIds.MemberRef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | MemberRef,
 0127                TokenTypeIds.Module >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | Module,
 0128                TokenTypeIds.DeclSecurity >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | DeclSecurity,
 0129                TokenTypeIds.Property >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | Property,
 0130                TokenTypeIds.Event >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | Event,
 0131                TokenTypeIds.Signature >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | StandAloneSig,
 0132                TokenTypeIds.ModuleRef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | ModuleRef,
 0133                TokenTypeIds.TypeSpec >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | TypeSpec,
 0134                TokenTypeIds.Assembly >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | Assembly,
 0135                TokenTypeIds.AssemblyRef >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | AssemblyRef,
 0136                TokenTypeIds.File >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | File,
 0137                TokenTypeIds.ExportedType >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | ExportedType,
 0138                TokenTypeIds.ManifestResource >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | ManifestResource,
 0139                TokenTypeIds.GenericParam >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | GenericParam,
 0140                TokenTypeIds.GenericParamConstraint >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | GenericPara
 0141                TokenTypeIds.MethodSpec >> TokenTypeIds.RowIdBitCount => rowId << NumberOfBits | MethodSpec,
 0142
 0143                _ => 0,
 0144            };
 0145        }
 146    }
 147}
 148