| | | 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.Reflection.Metadata.Ecma335; |
| | | 5 | | |
| | | 6 | | namespace System.Reflection.Metadata |
| | | 7 | | { |
| | | 8 | | public readonly struct FieldDefinition |
| | | 9 | | { |
| | | 10 | | private readonly MetadataReader _reader; |
| | | 11 | | |
| | | 12 | | // Workaround: JIT doesn't generate good code for nested structures, so use RowId. |
| | | 13 | | private readonly uint _treatmentAndRowId; |
| | | 14 | | |
| | | 15 | | internal FieldDefinition(MetadataReader reader, uint treatmentAndRowId) |
| | 0 | 16 | | { |
| | 0 | 17 | | _reader = reader; |
| | 0 | 18 | | _treatmentAndRowId = treatmentAndRowId; |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | private int RowId |
| | | 22 | | { |
| | 0 | 23 | | get { return (int)(_treatmentAndRowId & TokenTypeIds.RIDMask); } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | private FieldDefTreatment Treatment |
| | | 27 | | { |
| | 0 | 28 | | get { return (FieldDefTreatment)(_treatmentAndRowId >> TokenTypeIds.RowIdBitCount); } |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | private FieldDefinitionHandle Handle |
| | | 32 | | { |
| | 0 | 33 | | get { return FieldDefinitionHandle.FromRowId(RowId); } |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public StringHandle Name |
| | | 37 | | { |
| | | 38 | | get |
| | 0 | 39 | | { |
| | 0 | 40 | | if (Treatment == 0) |
| | 0 | 41 | | { |
| | 0 | 42 | | return _reader.FieldTable.GetName(Handle); |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | return GetProjectedName(); |
| | 0 | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public FieldAttributes Attributes |
| | | 50 | | { |
| | | 51 | | get |
| | 0 | 52 | | { |
| | 0 | 53 | | if (Treatment == 0) |
| | 0 | 54 | | { |
| | 0 | 55 | | return _reader.FieldTable.GetFlags(Handle); |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | return GetProjectedFlags(); |
| | 0 | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public BlobHandle Signature |
| | | 63 | | { |
| | | 64 | | get |
| | 0 | 65 | | { |
| | 0 | 66 | | if (Treatment == 0) |
| | 0 | 67 | | { |
| | 0 | 68 | | return _reader.FieldTable.GetSignature(Handle); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | return GetProjectedSignature(); |
| | 0 | 72 | | } |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public TType DecodeSignature<TType, TGenericContext>(ISignatureTypeProvider<TType, TGenericContext> provider, TG |
| | 0 | 76 | | { |
| | 0 | 77 | | var decoder = new SignatureDecoder<TType, TGenericContext>(provider, _reader, genericContext); |
| | 0 | 78 | | var blob = _reader.GetBlobReader(Signature); |
| | 0 | 79 | | return decoder.DecodeFieldSignature(ref blob); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Returns a handle to the type that declares this field. |
| | | 84 | | /// </summary> |
| | | 85 | | public TypeDefinitionHandle GetDeclaringType() |
| | 0 | 86 | | { |
| | 0 | 87 | | return _reader.GetDeclaringType(Handle); |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | public ConstantHandle GetDefaultValue() |
| | 0 | 91 | | { |
| | 0 | 92 | | return _reader.ConstantTable.FindConstant(Handle); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | public int GetRelativeVirtualAddress() |
| | 0 | 96 | | { |
| | 0 | 97 | | int fieldRvaRowId = _reader.FieldRvaTable.FindFieldRvaRowId(Handle.RowId); |
| | 0 | 98 | | if (fieldRvaRowId == 0) |
| | 0 | 99 | | { |
| | 0 | 100 | | return 0; |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | return _reader.FieldRvaTable.GetRva(fieldRvaRowId); |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Returns field layout offset, or -1 if not available. |
| | | 108 | | /// </summary> |
| | | 109 | | public int GetOffset() |
| | 0 | 110 | | { |
| | 0 | 111 | | int layoutRowId = _reader.FieldLayoutTable.FindFieldLayoutRowId(Handle); |
| | 0 | 112 | | if (layoutRowId == 0) |
| | 0 | 113 | | { |
| | 0 | 114 | | return -1; |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | uint offset = _reader.FieldLayoutTable.GetOffset(layoutRowId); |
| | 0 | 118 | | if (offset > int.MaxValue) |
| | 0 | 119 | | { |
| | | 120 | | // CLI spec says: |
| | | 121 | | // "Offset (a 4-byte constant)" |
| | | 122 | | // "Offset shall be zero or more" |
| | | 123 | | // |
| | | 124 | | // Peverify fails with "Type load failed" error if offset is greater than Int32.MaxValue. |
| | 0 | 125 | | return -1; |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | return (int)offset; |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | public BlobHandle GetMarshallingDescriptor() |
| | 0 | 132 | | { |
| | 0 | 133 | | int marshalRowId = _reader.FieldMarshalTable.FindFieldMarshalRowId(Handle); |
| | 0 | 134 | | if (marshalRowId == 0) |
| | 0 | 135 | | { |
| | 0 | 136 | | return default(BlobHandle); |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | return _reader.FieldMarshalTable.GetNativeType(marshalRowId); |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | public CustomAttributeHandleCollection GetCustomAttributes() |
| | 0 | 143 | | { |
| | 0 | 144 | | return new CustomAttributeHandleCollection(_reader, Handle); |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | #region Projections |
| | | 148 | | |
| | | 149 | | private StringHandle GetProjectedName() |
| | 0 | 150 | | { |
| | | 151 | | // no change: |
| | 0 | 152 | | return _reader.FieldTable.GetName(Handle); |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | private FieldAttributes GetProjectedFlags() |
| | 0 | 156 | | { |
| | 0 | 157 | | var flags = _reader.FieldTable.GetFlags(Handle); |
| | | 158 | | |
| | 0 | 159 | | if (Treatment == FieldDefTreatment.EnumValue) |
| | 0 | 160 | | { |
| | 0 | 161 | | return (flags & ~FieldAttributes.FieldAccessMask) | FieldAttributes.Public; |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | return flags; |
| | 0 | 165 | | } |
| | | 166 | | |
| | | 167 | | private BlobHandle GetProjectedSignature() |
| | 0 | 168 | | { |
| | 0 | 169 | | return _reader.FieldTable.GetSignature(Handle); |
| | 0 | 170 | | } |
| | | 171 | | #endregion |
| | | 172 | | } |
| | | 173 | | } |
| | | 174 | | |