| | | 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.Reflection.Metadata.Ecma335; |
| | | 6 | | |
| | | 7 | | namespace System.Reflection.Metadata |
| | | 8 | | { |
| | | 9 | | public readonly struct TypeReference |
| | | 10 | | { |
| | | 11 | | private readonly MetadataReader _reader; |
| | | 12 | | |
| | | 13 | | // Workaround: JIT doesn't generate good code for nested structures, so use RowId. |
| | | 14 | | private readonly uint _treatmentAndRowId; |
| | | 15 | | |
| | | 16 | | internal TypeReference(MetadataReader reader, uint treatmentAndRowId) |
| | 0 | 17 | | { |
| | 0 | 18 | | _reader = reader; |
| | 0 | 19 | | _treatmentAndRowId = treatmentAndRowId; |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | private int RowId |
| | | 23 | | { |
| | 0 | 24 | | get { return (int)(_treatmentAndRowId & TokenTypeIds.RIDMask); } |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | private TypeRefTreatment Treatment |
| | | 28 | | { |
| | 0 | 29 | | get { return (TypeRefTreatment)(_treatmentAndRowId >> TokenTypeIds.RowIdBitCount); } |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | private TypeReferenceHandle Handle |
| | | 33 | | { |
| | 0 | 34 | | get { return TypeReferenceHandle.FromRowId(RowId); } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Resolution scope in which the target type is defined and is uniquely identified by the specified <see cref=" |
| | | 39 | | /// </summary> |
| | | 40 | | /// <remarks> |
| | | 41 | | /// Resolution scope can be one of the following handles: |
| | | 42 | | /// <list type="bullet"> |
| | | 43 | | /// <item><description><see cref="TypeReferenceHandle"/> of the enclosing type, if the target type is a nested t |
| | | 44 | | /// <item><description><see cref="ModuleReferenceHandle"/>, if the target type is defined in another module with |
| | | 45 | | /// <item><description><see cref="EntityHandle.ModuleDefinition"/>, if the target type is defined in the current |
| | | 46 | | /// <item><description><see cref="AssemblyReferenceHandle"/>, if the target type is defined in a different assem |
| | | 47 | | /// <item><description>Nil handle if the target type must be resolved by searching the <see cref="MetadataReader |
| | | 48 | | /// </list> |
| | | 49 | | /// </remarks> |
| | | 50 | | public EntityHandle ResolutionScope |
| | | 51 | | { |
| | | 52 | | get |
| | 0 | 53 | | { |
| | 0 | 54 | | if (Treatment == 0) |
| | 0 | 55 | | { |
| | 0 | 56 | | return _reader.TypeRefTable.GetResolutionScope(Handle); |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | return GetProjectedResolutionScope(); |
| | 0 | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Name of the target type. |
| | | 65 | | /// </summary> |
| | | 66 | | public StringHandle Name |
| | | 67 | | { |
| | | 68 | | get |
| | 0 | 69 | | { |
| | 0 | 70 | | if (Treatment == 0) |
| | 0 | 71 | | { |
| | 0 | 72 | | return _reader.TypeRefTable.GetName(Handle); |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | return GetProjectedName(); |
| | 0 | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Full name of the namespace where the target type is defined, or nil if the type is nested or defined in a ro |
| | | 81 | | /// </summary> |
| | | 82 | | public StringHandle Namespace |
| | | 83 | | { |
| | | 84 | | get |
| | 0 | 85 | | { |
| | 0 | 86 | | if (Treatment == 0) |
| | 0 | 87 | | { |
| | 0 | 88 | | return _reader.TypeRefTable.GetNamespace(Handle); |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | return GetProjectedNamespace(); |
| | 0 | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | #region Projections |
| | | 96 | | |
| | | 97 | | private EntityHandle GetProjectedResolutionScope() |
| | 0 | 98 | | { |
| | 0 | 99 | | switch (Treatment) |
| | | 100 | | { |
| | | 101 | | case TypeRefTreatment.SystemAttribute: |
| | | 102 | | case TypeRefTreatment.SystemDelegate: |
| | 0 | 103 | | return AssemblyReferenceHandle.FromVirtualIndex(AssemblyReferenceHandle.VirtualIndex.System_Runtime) |
| | | 104 | | |
| | | 105 | | case TypeRefTreatment.UseProjectionInfo: |
| | 0 | 106 | | return MetadataReader.GetProjectedAssemblyRef(RowId); |
| | | 107 | | } |
| | | 108 | | |
| | 0 | 109 | | Debug.Fail("Unknown TypeRef treatment"); |
| | | 110 | | return default(AssemblyReferenceHandle); |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | private StringHandle GetProjectedName() |
| | 0 | 114 | | { |
| | 0 | 115 | | if (Treatment == TypeRefTreatment.UseProjectionInfo) |
| | 0 | 116 | | { |
| | 0 | 117 | | return MetadataReader.GetProjectedName(RowId); |
| | | 118 | | } |
| | | 119 | | else |
| | 0 | 120 | | { |
| | 0 | 121 | | return _reader.TypeRefTable.GetName(Handle); |
| | | 122 | | } |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | private StringHandle GetProjectedNamespace() |
| | 0 | 126 | | { |
| | 0 | 127 | | switch (Treatment) |
| | | 128 | | { |
| | | 129 | | case TypeRefTreatment.SystemAttribute: |
| | | 130 | | case TypeRefTreatment.SystemDelegate: |
| | 0 | 131 | | return StringHandle.FromVirtualIndex(StringHandle.VirtualIndex.System); |
| | | 132 | | |
| | | 133 | | case TypeRefTreatment.UseProjectionInfo: |
| | 0 | 134 | | return MetadataReader.GetProjectedNamespace(RowId); |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | Debug.Fail("Unknown TypeRef treatment"); |
| | | 138 | | return default(StringHandle); |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | internal TypeRefSignatureTreatment SignatureTreatment |
| | | 142 | | { |
| | | 143 | | get |
| | 0 | 144 | | { |
| | 0 | 145 | | if (Treatment == 0) |
| | 0 | 146 | | { |
| | 0 | 147 | | return TypeRefSignatureTreatment.None; |
| | | 148 | | } |
| | | 149 | | |
| | 0 | 150 | | return GetProjectedSignatureTreatment(); |
| | 0 | 151 | | } |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | private TypeRefSignatureTreatment GetProjectedSignatureTreatment() |
| | 0 | 155 | | { |
| | 0 | 156 | | if (Treatment == TypeRefTreatment.UseProjectionInfo) |
| | 0 | 157 | | { |
| | 0 | 158 | | return MetadataReader.GetProjectedSignatureTreatment(RowId); |
| | | 159 | | } |
| | | 160 | | else |
| | 0 | 161 | | { |
| | 0 | 162 | | return TypeRefSignatureTreatment.None; |
| | | 163 | | } |
| | 0 | 164 | | } |
| | | 165 | | #endregion |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | |