| | | 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.Collections.Immutable; |
| | | 5 | | using System.Reflection.Metadata.Ecma335; |
| | | 6 | | |
| | | 7 | | namespace System.Reflection.Metadata |
| | | 8 | | { |
| | | 9 | | public readonly struct TypeDefinition |
| | | 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 TypeDefinition(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 TypeDefTreatment Treatment |
| | | 28 | | { |
| | 0 | 29 | | get { return (TypeDefTreatment)(_treatmentAndRowId >> TokenTypeIds.RowIdBitCount); } |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | private TypeDefinitionHandle Handle |
| | | 33 | | { |
| | 0 | 34 | | get { return TypeDefinitionHandle.FromRowId(RowId); } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public TypeAttributes Attributes |
| | | 38 | | { |
| | | 39 | | get |
| | 0 | 40 | | { |
| | 0 | 41 | | if (Treatment == 0) |
| | 0 | 42 | | { |
| | 0 | 43 | | return _reader.TypeDefTable.GetFlags(Handle); |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | return GetProjectedFlags(); |
| | 0 | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Indicates whether this is a nested type. |
| | | 52 | | /// </summary> |
| | 0 | 53 | | public bool IsNested => Attributes.IsNested(); |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Name of the type. |
| | | 57 | | /// </summary> |
| | | 58 | | public StringHandle Name |
| | | 59 | | { |
| | | 60 | | get |
| | 0 | 61 | | { |
| | 0 | 62 | | if (Treatment == 0) |
| | 0 | 63 | | { |
| | 0 | 64 | | return _reader.TypeDefTable.GetName(Handle); |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | return GetProjectedName(); |
| | 0 | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Full name of the namespace where the type is defined, or nil if the type is nested or defined in a root name |
| | | 73 | | /// </summary> |
| | | 74 | | public StringHandle Namespace |
| | | 75 | | { |
| | | 76 | | get |
| | 0 | 77 | | { |
| | 0 | 78 | | if (Treatment == 0) |
| | 0 | 79 | | { |
| | 0 | 80 | | return _reader.TypeDefTable.GetNamespace(Handle); |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | return GetProjectedNamespaceString(); |
| | 0 | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// The definition handle of the namespace where the type is defined, or nil if the type is nested or defined in |
| | | 89 | | /// </summary> |
| | | 90 | | public NamespaceDefinitionHandle NamespaceDefinition |
| | | 91 | | { |
| | | 92 | | get |
| | 0 | 93 | | { |
| | 0 | 94 | | if (Treatment == 0) |
| | 0 | 95 | | { |
| | 0 | 96 | | return _reader.TypeDefTable.GetNamespaceDefinition(Handle); |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | return GetProjectedNamespace(); |
| | 0 | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// The base type of the type definition: either |
| | | 105 | | /// <see cref="TypeSpecificationHandle"/>, <see cref="TypeReferenceHandle"/> or <see cref="TypeDefinitionHandle" |
| | | 106 | | /// </summary> |
| | | 107 | | public EntityHandle BaseType |
| | | 108 | | { |
| | | 109 | | get |
| | 0 | 110 | | { |
| | 0 | 111 | | if (Treatment == 0) |
| | 0 | 112 | | { |
| | 0 | 113 | | return _reader.TypeDefTable.GetExtends(Handle); |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | return GetProjectedBaseType(); |
| | 0 | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | public TypeLayout GetLayout() |
| | 0 | 121 | | { |
| | 0 | 122 | | int classLayoutRowId = _reader.ClassLayoutTable.FindRow(Handle); |
| | 0 | 123 | | if (classLayoutRowId == 0) |
| | 0 | 124 | | { |
| | | 125 | | // NOTE: We don't need a bool/TryGetLayout because zero also means use default: |
| | | 126 | | // |
| | | 127 | | // Spec: |
| | | 128 | | // ClassSize of zero does not mean the class has zero size. It means that no .size directive was specif |
| | | 129 | | // at definition time, in which case, the actual size is calculated from the field types, taking accoun |
| | | 130 | | // packing size (default or specified) and natural alignment on the target, runtime platform. |
| | | 131 | | // |
| | | 132 | | // PackingSize shall be one of {0, 1, 2, 4, 8, 16, 32, 64, 128}. (0 means use |
| | | 133 | | // the default pack size for the platform on which the application is |
| | | 134 | | // running.) |
| | | 135 | | |
| | 0 | 136 | | return default(TypeLayout); |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | uint size = _reader.ClassLayoutTable.GetClassSize(classLayoutRowId); |
| | | 140 | | |
| | | 141 | | // The spec doesn't limit the size to 31bit. It only limits the size to 1MB if Parent is a value type. |
| | | 142 | | // It however doesn't make much sense to define classes with >2GB size. So in order to keep the API |
| | | 143 | | // clean of unsigned ints we impose the limit. |
| | 0 | 144 | | if (unchecked((int)size) != size) |
| | 0 | 145 | | { |
| | 0 | 146 | | throw new BadImageFormatException(SR.InvalidTypeSize); |
| | | 147 | | } |
| | | 148 | | |
| | 0 | 149 | | int packingSize = _reader.ClassLayoutTable.GetPackingSize(classLayoutRowId); |
| | 0 | 150 | | return new TypeLayout((int)size, packingSize); |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Returns the enclosing type of a specified nested type or nil handle if the type is not nested. |
| | | 155 | | /// </summary> |
| | | 156 | | public TypeDefinitionHandle GetDeclaringType() |
| | 0 | 157 | | { |
| | 0 | 158 | | return _reader.NestedClassTable.FindEnclosingType(Handle); |
| | 0 | 159 | | } |
| | | 160 | | |
| | | 161 | | public GenericParameterHandleCollection GetGenericParameters() |
| | 0 | 162 | | { |
| | 0 | 163 | | return _reader.GenericParamTable.FindGenericParametersForType(Handle); |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | public MethodDefinitionHandleCollection GetMethods() |
| | 0 | 167 | | { |
| | 0 | 168 | | return new MethodDefinitionHandleCollection(_reader, Handle); |
| | 0 | 169 | | } |
| | | 170 | | |
| | | 171 | | public FieldDefinitionHandleCollection GetFields() |
| | 0 | 172 | | { |
| | 0 | 173 | | return new FieldDefinitionHandleCollection(_reader, Handle); |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | public PropertyDefinitionHandleCollection GetProperties() |
| | 0 | 177 | | { |
| | 0 | 178 | | return new PropertyDefinitionHandleCollection(_reader, Handle); |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | public EventDefinitionHandleCollection GetEvents() |
| | 0 | 182 | | { |
| | 0 | 183 | | return new EventDefinitionHandleCollection(_reader, Handle); |
| | 0 | 184 | | } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Returns an array of types nested in the specified type. |
| | | 188 | | /// </summary> |
| | | 189 | | public ImmutableArray<TypeDefinitionHandle> GetNestedTypes() |
| | 0 | 190 | | { |
| | 0 | 191 | | return _reader.GetNestedTypes(Handle); |
| | 0 | 192 | | } |
| | | 193 | | |
| | | 194 | | public MethodImplementationHandleCollection GetMethodImplementations() |
| | 0 | 195 | | { |
| | 0 | 196 | | return new MethodImplementationHandleCollection(_reader, Handle); |
| | 0 | 197 | | } |
| | | 198 | | |
| | | 199 | | public InterfaceImplementationHandleCollection GetInterfaceImplementations() |
| | 0 | 200 | | { |
| | 0 | 201 | | return new InterfaceImplementationHandleCollection(_reader, Handle); |
| | 0 | 202 | | } |
| | | 203 | | |
| | | 204 | | public CustomAttributeHandleCollection GetCustomAttributes() |
| | 0 | 205 | | { |
| | 0 | 206 | | return new CustomAttributeHandleCollection(_reader, Handle); |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | public DeclarativeSecurityAttributeHandleCollection GetDeclarativeSecurityAttributes() |
| | 0 | 210 | | { |
| | 0 | 211 | | return new DeclarativeSecurityAttributeHandleCollection(_reader, Handle); |
| | 0 | 212 | | } |
| | | 213 | | |
| | | 214 | | #region Projections |
| | | 215 | | |
| | | 216 | | private TypeAttributes GetProjectedFlags() |
| | 0 | 217 | | { |
| | 0 | 218 | | var flags = _reader.TypeDefTable.GetFlags(Handle); |
| | 0 | 219 | | var treatment = Treatment; |
| | | 220 | | |
| | 0 | 221 | | switch (treatment & TypeDefTreatment.KindMask) |
| | | 222 | | { |
| | | 223 | | case TypeDefTreatment.NormalNonAttribute: |
| | 0 | 224 | | flags |= TypeAttributes.WindowsRuntime | TypeAttributes.Import; |
| | 0 | 225 | | break; |
| | | 226 | | |
| | | 227 | | case TypeDefTreatment.NormalAttribute: |
| | 0 | 228 | | flags |= TypeAttributes.WindowsRuntime | TypeAttributes.Sealed; |
| | 0 | 229 | | break; |
| | | 230 | | |
| | | 231 | | case TypeDefTreatment.UnmangleWinRTName: |
| | 0 | 232 | | flags = flags & ~TypeAttributes.SpecialName | TypeAttributes.Public; |
| | 0 | 233 | | break; |
| | | 234 | | |
| | | 235 | | case TypeDefTreatment.PrefixWinRTName: |
| | 0 | 236 | | flags = flags & ~TypeAttributes.Public | TypeAttributes.Import; |
| | 0 | 237 | | break; |
| | | 238 | | |
| | | 239 | | case TypeDefTreatment.RedirectedToClrType: |
| | 0 | 240 | | flags = flags & ~TypeAttributes.Public | TypeAttributes.Import; |
| | 0 | 241 | | break; |
| | | 242 | | |
| | | 243 | | case TypeDefTreatment.RedirectedToClrAttribute: |
| | 0 | 244 | | flags &= ~TypeAttributes.Public; |
| | 0 | 245 | | break; |
| | | 246 | | } |
| | | 247 | | |
| | 0 | 248 | | if ((treatment & TypeDefTreatment.MarkAbstractFlag) != 0) |
| | 0 | 249 | | { |
| | 0 | 250 | | flags |= TypeAttributes.Abstract; |
| | 0 | 251 | | } |
| | | 252 | | |
| | 0 | 253 | | if ((treatment & TypeDefTreatment.MarkInternalFlag) != 0) |
| | 0 | 254 | | { |
| | 0 | 255 | | flags &= ~TypeAttributes.Public; |
| | 0 | 256 | | } |
| | | 257 | | |
| | 0 | 258 | | return flags; |
| | 0 | 259 | | } |
| | | 260 | | |
| | | 261 | | private StringHandle GetProjectedName() |
| | 0 | 262 | | { |
| | 0 | 263 | | var name = _reader.TypeDefTable.GetName(Handle); |
| | | 264 | | |
| | 0 | 265 | | return (Treatment & TypeDefTreatment.KindMask) switch |
| | 0 | 266 | | { |
| | 0 | 267 | | TypeDefTreatment.UnmangleWinRTName => name.SuffixRaw(MetadataReader.ClrPrefix.Length), |
| | 0 | 268 | | TypeDefTreatment.PrefixWinRTName => name.WithWinRTPrefix(), |
| | 0 | 269 | | _ => name, |
| | 0 | 270 | | }; |
| | 0 | 271 | | } |
| | | 272 | | |
| | | 273 | | private NamespaceDefinitionHandle GetProjectedNamespace() |
| | 0 | 274 | | { |
| | | 275 | | // NOTE: NamespaceDefinitionHandle currently relies on never having virtual values. If this ever gets projec |
| | | 276 | | // to a virtual namespace name, then that assumption will need to be removed. |
| | | 277 | | |
| | | 278 | | // no change: |
| | 0 | 279 | | return _reader.TypeDefTable.GetNamespaceDefinition(Handle); |
| | 0 | 280 | | } |
| | | 281 | | |
| | | 282 | | private StringHandle GetProjectedNamespaceString() |
| | 0 | 283 | | { |
| | | 284 | | // no change: |
| | 0 | 285 | | return _reader.TypeDefTable.GetNamespace(Handle); |
| | 0 | 286 | | } |
| | | 287 | | |
| | | 288 | | private EntityHandle GetProjectedBaseType() |
| | 0 | 289 | | { |
| | | 290 | | // no change: |
| | 0 | 291 | | return _reader.TypeDefTable.GetExtends(Handle); |
| | 0 | 292 | | } |
| | | 293 | | |
| | | 294 | | #endregion |
| | | 295 | | } |
| | | 296 | | } |
| | | 297 | | |