public void Read(ClrModuleReader reader) { this.Type = (ElementType)reader.Binary.ReadByte(); byte padding = reader.Binary.ReadByte(); this.Parent = reader.ReadCodedIndex<HasConstant>(); this.Value = reader.ReadBlob(); }
public void Read(ClrModuleReader reader) { this.MappingFlags = (PInvokeAttributes)reader.Binary.ReadUInt16(); this.MemberForwarded = reader.ReadCodedIndex<MemberForwarded>(); this.ImportName = reader.ReadString(); this.ImportScope = reader.ReadTableIndex(TableKind.ModuleRef); }
public static uint CompressMetadataToken(CodedIndex cidx, MetadataToken token) { uint ret = 0; if (token.RID == 0) return ret; switch (cidx) { <% $coded_indexes.each { |ci| %> case CodedIndex.<%=ci.name%> :
public void Read(ClrModuleReader reader) { this.Offset = reader.Binary.ReadUInt32(); this.Flags = (ManifestResourceAttributes)reader.Binary.ReadUInt32(); this.Name = reader.ReadString(); this.Implementation = reader.ReadCodedIndex<Implementation>(); }
public void Read(ClrModuleReader reader) { this.Number = reader.Binary.ReadUInt16(); this.Flags = (GenericParamAttributes)reader.Binary.ReadUInt16(); this.Owner = reader.ReadCodedIndex<TypeOrMethodDef>(); this.Name = reader.ReadString(); }
public void Read(ClrModuleReader reader) { this.Flags = (TypeAttributes)reader.Binary.ReadUInt32(); this.TypeDefId = reader.Binary.ReadUInt32(); this.TypeName = reader.ReadString(); this.TypeNamespace = reader.ReadString(); this.Implementation = reader.ReadCodedIndex<Implementation>(); }
public void Read(ClrModuleReader reader) { this.TypeReference = new TypeReference.External(); this.ResolutionScope = reader.ReadCodedIndex<ResolutionScope>(); this.TypeReference.Name = reader.ReadString(); this.TypeReference.Namespace = reader.ReadString(); }
public int GetCodedIndexSize (CodedIndex coded_index) { var index = (int) coded_index; var size = coded_index_sizes [index]; if (size != 0) return size; return coded_index_sizes [index] = coded_index.GetSize (counter); }
public void Read(ClrModuleReader reader) { this.Class = reader.ReadTableIndex(TableKind.TypeDef); this.Interface = reader.ReadCodedIndex <TypeDefOrRef>(); }
public void WriteCodedRID(uint rid, CodedIndex coded_index) { WriteBySize(rid, GetCodedIndexSize(coded_index)); }
private int GetCodedIndexSize(CodedIndex index) => this.image.GetCodedIndexSize(index);
public void Read(ClrModuleReader reader) { this.Action = (SecurityAction)reader.Binary.ReadUInt16(); this.Parent = reader.ReadCodedIndex<HasDeclSecurity>(); this.PermissionSet = reader.ReadBlob(); }
public void Read(ClrModuleReader reader) { this.Semantics = (MethodSemanticsAttributes)reader.Binary.ReadUInt16(); this.Method = reader.ReadTableIndex(TableKind.MethodDef); this.Association = reader.ReadCodedIndex<HasSemantics>(); }
void WriteMetadataToken(MetadataToken token, CodedIndex ci) { WriteByIndexSize(Utilities.CompressMetadataToken(ci, token), Utilities.GetCodedIndexSize(ci, m_counter, m_ciCache)); }
public void Read(ClrModuleReader reader) { this.Class = reader.ReadTableIndex(TableKind.TypeDef); this.Interface = reader.ReadCodedIndex<TypeDefOrRef>(); }
public void Read(ClrModuleReader reader) { this.Owner = reader.ReadTableIndex(TableKind.GenericParam); this.Constraint = reader.ReadCodedIndex <TypeDefOrRef>(); }
public void Read(ClrModuleReader reader) { this.Parent = reader.ReadCodedIndex<HasCustomAttribute>(); this.Type = reader.ReadCodedIndex<CustomAttributeType>(); this.Value = new CustomAttributeData(reader.ReadBlob()); }
public static MetadataToken GetMetadataToken(CodedIndex self, uint data) { uint rid; TokenType token_type; switch (self) { case CodedIndex.TypeDefOrRef: rid = data >> 2; switch (data & 3) { case 0: token_type = TokenType.TypeDef; goto ret; case 1: token_type = TokenType.TypeRef; goto ret; case 2: token_type = TokenType.TypeSpec; goto ret; default: goto exit; } case CodedIndex.HasConstant: rid = data >> 2; switch (data & 3) { case 0: token_type = TokenType.Field; goto ret; case 1: token_type = TokenType.Param; goto ret; case 2: token_type = TokenType.Property; goto ret; default: goto exit; } case CodedIndex.HasCustomAttribute: rid = data >> 5; switch (data & 31) { case 0: token_type = TokenType.Method; goto ret; case 1: token_type = TokenType.Field; goto ret; case 2: token_type = TokenType.TypeRef; goto ret; case 3: token_type = TokenType.TypeDef; goto ret; case 4: token_type = TokenType.Param; goto ret; case 5: token_type = TokenType.InterfaceImpl; goto ret; case 6: token_type = TokenType.MemberRef; goto ret; case 7: token_type = TokenType.Module; goto ret; case 8: token_type = TokenType.Permission; goto ret; case 9: token_type = TokenType.Property; goto ret; case 10: token_type = TokenType.Event; goto ret; case 11: token_type = TokenType.Signature; goto ret; case 12: token_type = TokenType.ModuleRef; goto ret; case 13: token_type = TokenType.TypeSpec; goto ret; case 14: token_type = TokenType.Assembly; goto ret; case 15: token_type = TokenType.AssemblyRef; goto ret; case 16: token_type = TokenType.File; goto ret; case 17: token_type = TokenType.ExportedType; goto ret; case 18: token_type = TokenType.ManifestResource; goto ret; case 19: token_type = TokenType.GenericParam; goto ret; default: goto exit; } case CodedIndex.HasFieldMarshal: rid = data >> 1; switch (data & 1) { case 0: token_type = TokenType.Field; goto ret; case 1: token_type = TokenType.Param; goto ret; default: goto exit; } case CodedIndex.HasDeclSecurity: rid = data >> 2; switch (data & 3) { case 0: token_type = TokenType.TypeDef; goto ret; case 1: token_type = TokenType.Method; goto ret; case 2: token_type = TokenType.Assembly; goto ret; default: goto exit; } case CodedIndex.MemberRefParent: rid = data >> 3; switch (data & 7) { case 0: token_type = TokenType.TypeDef; goto ret; case 1: token_type = TokenType.TypeRef; goto ret; case 2: token_type = TokenType.ModuleRef; goto ret; case 3: token_type = TokenType.Method; goto ret; case 4: token_type = TokenType.TypeSpec; goto ret; default: goto exit; } case CodedIndex.HasSemantics: rid = data >> 1; switch (data & 1) { case 0: token_type = TokenType.Event; goto ret; case 1: token_type = TokenType.Property; goto ret; default: goto exit; } case CodedIndex.MethodDefOrRef: rid = data >> 1; switch (data & 1) { case 0: token_type = TokenType.Method; goto ret; case 1: token_type = TokenType.MemberRef; goto ret; default: goto exit; } case CodedIndex.MemberForwarded: rid = data >> 1; switch (data & 1) { case 0: token_type = TokenType.Field; goto ret; case 1: token_type = TokenType.Method; goto ret; default: goto exit; } case CodedIndex.Implementation: rid = data >> 2; switch (data & 3) { case 0: token_type = TokenType.File; goto ret; case 1: token_type = TokenType.AssemblyRef; goto ret; case 2: token_type = TokenType.ExportedType; goto ret; default: goto exit; } case CodedIndex.CustomAttributeType: rid = data >> 3; switch (data & 7) { case 2: token_type = TokenType.Method; goto ret; case 3: token_type = TokenType.MemberRef; goto ret; default: goto exit; } case CodedIndex.ResolutionScope: rid = data >> 2; switch (data & 3) { case 0: token_type = TokenType.Module; goto ret; case 1: token_type = TokenType.ModuleRef; goto ret; case 2: token_type = TokenType.AssemblyRef; goto ret; case 3: token_type = TokenType.TypeRef; goto ret; default: goto exit; } case CodedIndex.TypeOrMethodDef: rid = data >> 1; switch (data & 1) { case 0: token_type = TokenType.TypeDef; goto ret; case 1: token_type = TokenType.Method; goto ret; default: goto exit; } default: goto exit; } ret: return new MetadataToken(token_type, rid); exit: return MetadataToken.Zero; }
public static int GetSize(CodedIndex self, Func<Table, int> counter) { int bits; Table[] tables; switch (self) { case CodedIndex.TypeDefOrRef: bits = 2; tables = new[] { Table.TypeDef, Table.TypeRef, Table.TypeSpec }; break; case CodedIndex.HasConstant: bits = 2; tables = new[] { Table.Field, Table.Param, Table.Property }; break; case CodedIndex.HasCustomAttribute: bits = 5; tables = new[] { Table.Method, Table.Field, Table.TypeRef, Table.TypeDef, Table.Param, Table.InterfaceImpl, Table.MemberRef, Table.Module, Table.DeclSecurity, Table.Property, Table.Event, Table.StandAloneSig, Table.ModuleRef, Table.TypeSpec, Table.Assembly, Table.AssemblyRef, Table.File, Table.ExportedType, Table.ManifestResource, Table.GenericParam }; break; case CodedIndex.HasFieldMarshal: bits = 1; tables = new[] { Table.Field, Table.Param }; break; case CodedIndex.HasDeclSecurity: bits = 2; tables = new[] { Table.TypeDef, Table.Method, Table.Assembly }; break; case CodedIndex.MemberRefParent: bits = 3; tables = new[] { Table.TypeDef, Table.TypeRef, Table.ModuleRef, Table.Method, Table.TypeSpec }; break; case CodedIndex.HasSemantics: bits = 1; tables = new[] { Table.Event, Table.Property }; break; case CodedIndex.MethodDefOrRef: bits = 1; tables = new[] { Table.Method, Table.MemberRef }; break; case CodedIndex.MemberForwarded: bits = 1; tables = new[] { Table.Field, Table.Method }; break; case CodedIndex.Implementation: bits = 2; tables = new[] { Table.File, Table.AssemblyRef, Table.ExportedType }; break; case CodedIndex.CustomAttributeType: bits = 3; tables = new[] { Table.Method, Table.MemberRef }; break; case CodedIndex.ResolutionScope: bits = 2; tables = new[] { Table.Module, Table.ModuleRef, Table.AssemblyRef, Table.TypeRef }; break; case CodedIndex.TypeOrMethodDef: bits = 1; tables = new[] { Table.TypeDef, Table.Method }; break; default: throw new ArgumentException(); } int max = 0; for (int i = 0; i < tables.Length; i++) { max = System.Math.Max(counter(tables[i]), max); } return max < (1 << (16 - bits)) ? 2 : 4; }
public void Read(ClrModuleReader reader) { this.Owner = reader.ReadTableIndex(TableKind.GenericParam); this.Constraint = reader.ReadCodedIndex<TypeDefOrRef>(); }
public static uint CompressMetadataToken(this CodedIndex self, MetadataToken token) { uint ret = 0; if (token.RID == 0) { return(ret); } switch (self) { case CodedIndex.TypeDefOrRef: ret = token.RID << 2; switch (token.TokenType) { case TokenType.TypeDef: return(ret | 0); case TokenType.TypeRef: return(ret | 1); case TokenType.TypeSpec: return(ret | 2); default: goto exit; } case CodedIndex.HasConstant: ret = token.RID << 2; switch (token.TokenType) { case TokenType.Field: return(ret | 0); case TokenType.Param: return(ret | 1); case TokenType.Property: return(ret | 2); default: goto exit; } case CodedIndex.HasCustomAttribute: ret = token.RID << 5; switch (token.TokenType) { case TokenType.Method: return(ret | 0); case TokenType.Field: return(ret | 1); case TokenType.TypeRef: return(ret | 2); case TokenType.TypeDef: return(ret | 3); case TokenType.Param: return(ret | 4); case TokenType.InterfaceImpl: return(ret | 5); case TokenType.MemberRef: return(ret | 6); case TokenType.Module: return(ret | 7); case TokenType.Permission: return(ret | 8); case TokenType.Property: return(ret | 9); case TokenType.Event: return(ret | 10); case TokenType.Signature: return(ret | 11); case TokenType.ModuleRef: return(ret | 12); case TokenType.TypeSpec: return(ret | 13); case TokenType.Assembly: return(ret | 14); case TokenType.AssemblyRef: return(ret | 15); case TokenType.File: return(ret | 16); case TokenType.ExportedType: return(ret | 17); case TokenType.ManifestResource: return(ret | 18); case TokenType.GenericParam: return(ret | 19); default: goto exit; } case CodedIndex.HasFieldMarshal: ret = token.RID << 1; switch (token.TokenType) { case TokenType.Field: return(ret | 0); case TokenType.Param: return(ret | 1); default: goto exit; } case CodedIndex.HasDeclSecurity: ret = token.RID << 2; switch (token.TokenType) { case TokenType.TypeDef: return(ret | 0); case TokenType.Method: return(ret | 1); case TokenType.Assembly: return(ret | 2); default: goto exit; } case CodedIndex.MemberRefParent: ret = token.RID << 3; switch (token.TokenType) { case TokenType.TypeDef: return(ret | 0); case TokenType.TypeRef: return(ret | 1); case TokenType.ModuleRef: return(ret | 2); case TokenType.Method: return(ret | 3); case TokenType.TypeSpec: return(ret | 4); default: goto exit; } case CodedIndex.HasSemantics: ret = token.RID << 1; switch (token.TokenType) { case TokenType.Event: return(ret | 0); case TokenType.Property: return(ret | 1); default: goto exit; } case CodedIndex.MethodDefOrRef: ret = token.RID << 1; switch (token.TokenType) { case TokenType.Method: return(ret | 0); case TokenType.MemberRef: return(ret | 1); default: goto exit; } case CodedIndex.MemberForwarded: ret = token.RID << 1; switch (token.TokenType) { case TokenType.Field: return(ret | 0); case TokenType.Method: return(ret | 1); default: goto exit; } case CodedIndex.Implementation: ret = token.RID << 2; switch (token.TokenType) { case TokenType.File: return(ret | 0); case TokenType.AssemblyRef: return(ret | 1); case TokenType.ExportedType: return(ret | 2); default: goto exit; } case CodedIndex.CustomAttributeType: ret = token.RID << 3; switch (token.TokenType) { case TokenType.Method: return(ret | 2); case TokenType.MemberRef: return(ret | 3); default: goto exit; } case CodedIndex.ResolutionScope: ret = token.RID << 2; switch (token.TokenType) { /*Telerik Authorship*/ case TokenType.Assembly: case TokenType.Module: return(ret | 0); case TokenType.ModuleRef: return(ret | 1); case TokenType.AssemblyRef: return(ret | 2); case TokenType.TypeRef: return(ret | 3); default: goto exit; } case CodedIndex.TypeOrMethodDef: ret = token.RID << 1; switch (token.TokenType) { case TokenType.TypeDef: return(ret | 0); case TokenType.Method: return(ret | 1); default: goto exit; } default: goto exit; } exit: throw new ArgumentException(); }
uint ReadIndex(CodedIndex codedIndex) { var size = image.GetCodedIndexSize(codedIndex); return(size == 2 ? ReadUInt16() : ReadUInt32()); }
public static int GetSize(this CodedIndex self, Func <Table, int> counter) { int bits; Table [] tables; switch (self) { case CodedIndex.TypeDefOrRef: bits = 2; tables = new [] { Table.TypeDef, Table.TypeRef, Table.TypeSpec }; break; case CodedIndex.HasConstant: bits = 2; tables = new [] { Table.Field, Table.Param, Table.Property }; break; case CodedIndex.HasCustomAttribute: bits = 5; tables = new [] { Table.Method, Table.Field, Table.TypeRef, Table.TypeDef, Table.Param, Table.InterfaceImpl, Table.MemberRef, Table.Module, Table.DeclSecurity, Table.Property, Table.Event, Table.StandAloneSig, Table.ModuleRef, Table.TypeSpec, Table.Assembly, Table.AssemblyRef, Table.File, Table.ExportedType, Table.ManifestResource, Table.GenericParam }; break; case CodedIndex.HasFieldMarshal: bits = 1; tables = new [] { Table.Field, Table.Param }; break; case CodedIndex.HasDeclSecurity: bits = 2; tables = new [] { Table.TypeDef, Table.Method, Table.Assembly }; break; case CodedIndex.MemberRefParent: bits = 3; tables = new [] { Table.TypeDef, Table.TypeRef, Table.ModuleRef, Table.Method, Table.TypeSpec }; break; case CodedIndex.HasSemantics: bits = 1; tables = new [] { Table.Event, Table.Property }; break; case CodedIndex.MethodDefOrRef: bits = 1; tables = new [] { Table.Method, Table.MemberRef }; break; case CodedIndex.MemberForwarded: bits = 1; tables = new [] { Table.Field, Table.Method }; break; case CodedIndex.Implementation: bits = 2; tables = new [] { Table.File, Table.AssemblyRef, Table.ExportedType }; break; case CodedIndex.CustomAttributeType: bits = 3; tables = new [] { Table.Method, Table.MemberRef }; break; case CodedIndex.ResolutionScope: bits = 2; tables = new [] { Table.Module, Table.ModuleRef, Table.AssemblyRef, Table.TypeRef }; break; case CodedIndex.TypeOrMethodDef: bits = 1; tables = new [] { Table.TypeDef, Table.Method }; break; default: throw new ArgumentException(); } int max = 0; for (int i = 0; i < tables.Length; i++) { max = System.Math.Max(counter(tables [i]), max); } return(max < (1 << (16 - bits)) ? 2 : 4); }
public void Read(ClrModuleReader reader) { this.Parent = reader.ReadCodedIndex<HasFieldMarshal>(); this.NativeType = new MarshalSpec(reader.ReadBlob()); }
public void Read(ClrModuleReader reader) { this.EventFlags = (EventAttributes)reader.Binary.ReadUInt16(); this.Name = reader.ReadString(); this.EventType = reader.ReadCodedIndex<TypeDefOrRef>(); }
private int GetCodedIndexSize(CodedIndex index) { return(this.image.GetCodedIndexSize(index)); }
void WriteMetadataToken(MetadataToken token, CodedIndex ci) { WriteByIndexSize (Utilities.CompressMetadataToken (ci, token), Utilities.GetCodedIndexSize ( ci, new Utilities.TableRowCounter (GetNumberOfRows), m_ciCache)); }
public void Read(ClrModuleReader reader) { this.Parent = reader.ReadCodedIndex <HasCustomAttribute>(); this.Type = reader.ReadCodedIndex <CustomAttributeType>(); this.Value = new CustomAttributeData(reader.ReadBlob()); }
internal static int GetCodedIndexSize (CodedIndex ci, TableRowCounter rowCounter, int [] codedIndexCache) { int bits = 0, max = 0, index = (int) ci; if (codedIndexCache [index] != 0) return codedIndexCache [index]; int res = 0; int [] rids; switch (ci) { case CodedIndex.TypeDefOrRef : bits = 2; rids = new int [3]; rids [0] = TypeDefTable.RId; rids [1] = TypeRefTable.RId; rids [2] = TypeSpecTable.RId; break; case CodedIndex.HasConstant : bits = 2; rids = new int [3]; rids [0] = FieldTable.RId; rids [1] = ParamTable.RId; rids [2] = PropertyTable.RId; break; case CodedIndex.HasCustomAttribute : bits = 5; rids = new int [20]; rids [0] = MethodTable.RId; rids [1] = FieldTable.RId; rids [2] = TypeRefTable.RId; rids [3] = TypeDefTable.RId; rids [4] = ParamTable.RId; rids [5] = InterfaceImplTable.RId; rids [6] = MemberRefTable.RId; rids [7] = ModuleTable.RId; rids [8] = DeclSecurityTable.RId; rids [9] = PropertyTable.RId; rids [10] = EventTable.RId; rids [11] = StandAloneSigTable.RId; rids [12] = ModuleRefTable.RId; rids [13] = TypeSpecTable.RId; rids [14] = AssemblyTable.RId; rids [15] = AssemblyRefTable.RId; rids [16] = FileTable.RId; rids [17] = ExportedTypeTable.RId; rids [18] = ManifestResourceTable.RId; rids [19] = GenericParamTable.RId; break; case CodedIndex.HasFieldMarshal : bits = 1; rids = new int [2]; rids [0] = FieldTable.RId; rids [1] = ParamTable.RId; break; case CodedIndex.HasDeclSecurity : bits = 2; rids = new int [3]; rids [0] = TypeDefTable.RId; rids [1] = MethodTable.RId; rids [2] = AssemblyTable.RId; break; case CodedIndex.MemberRefParent : bits = 3; rids = new int [5]; rids [0] = TypeDefTable.RId; rids [1] = TypeRefTable.RId; rids [2] = ModuleRefTable.RId; rids [3] = MethodTable.RId; rids [4] = TypeSpecTable.RId; break; case CodedIndex.HasSemantics : bits = 1; rids = new int [2]; rids [0] = EventTable.RId; rids [1] = PropertyTable.RId; break; case CodedIndex.MethodDefOrRef : bits = 1; rids = new int [2]; rids [0] = MethodTable.RId; rids [1] = MemberRefTable.RId; break; case CodedIndex.MemberForwarded : bits = 1; rids = new int [2]; rids [0] = FieldTable.RId; rids [1] = MethodTable.RId; break; case CodedIndex.Implementation : bits = 2; rids = new int [3]; rids [0] = FileTable.RId; rids [1] = AssemblyRefTable.RId; rids [2] = ExportedTypeTable.RId; break; case CodedIndex.CustomAttributeType : bits = 3; rids = new int [2]; rids [0] = MethodTable.RId; rids [1] = MemberRefTable.RId; break; case CodedIndex.ResolutionScope : bits = 2; rids = new int [4]; rids [0] = ModuleTable.RId; rids [1] = ModuleRefTable.RId; rids [2] = AssemblyRefTable.RId; rids [3] = TypeRefTable.RId; break; case CodedIndex.TypeOrMethodDef : bits = 1; rids = new int [2]; rids [0] = TypeDefTable.RId; rids [1] = MethodTable.RId; break; default : throw new MetadataFormatException ("Non valid CodedIndex"); } for (int i = 0; i < rids.Length; i++) { int rows = rowCounter (rids [i]); if (rows > max) max = rows; } res = max < (1 << (16 - bits)) ? 2 : 4; codedIndexCache [index] = res; return res; }
public void Read(ClrModuleReader reader) { this.Class = reader.ReadCodedIndex<MemberRefParent>(); this.Name = reader.ReadString(); this.SignatureBlob = reader.ReadBlob(); }
private void CreateMethod(MethodInfo method, List <DelayedWrite> genericParams) { if (!_metadata.TryGetMethodDefinition(method, out var metadata)) { ThrowMetadataIsNotReserved("Method", method); } EnsureMetadataWasNotEmitted(metadata, method); var body = method.GetMethodBody(); var localVariablesSignature = default(StandaloneSignatureHandle); if (body != null && body.LocalVariables.Count > 0) { localVariablesSignature = _metadata.Builder.AddStandaloneSignature(_metadata.GetOrAddBlob( MetadataHelper.BuildSignature(x => { x.LocalVariableSignature(body.LocalVariables.Count).AddRange(body.LocalVariables, _metadata); }))); } var offset = -1; // If body exists, we write it in IL body stream if (body != null && !method.IsAbstract) { var methodBodyWriter = new MethodBodyStreamWriter(_metadata); // offset can be aligned during serialization. So, override the correct offset. offset = methodBodyWriter.AddMethodBody(method, localVariablesSignature); } var parameters = CreateParameters(method.GetParameters()); var handle = _metadata.Builder.AddMethodDefinition( method.Attributes, method.MethodImplementationFlags, _metadata.GetOrAddString(method.Name), _metadata.GetMethodOrConstructorSignature(method), offset, parameters); // Explicit interface implementations need to be marked with method implementation // (This is the equivalent of .Override in msil) if (method.IsPrivate) { // Go through all the implemented interfaces and all their methods // looking for methods that this method implements and mark accordingly. // NB: This is not super efficient. Should probably create a map somewhere // for faster lookup, but this will do for now. var type = method.DeclaringType; foreach (var itf in type.GetInterfaces()) { var itfMap = type.GetInterfaceMap(itf); for (int i = 0; i < itfMap.TargetMethods.Length; i++) { var m = itfMap.TargetMethods[i]; if (m == method) { var itfImpl = itfMap.InterfaceMethods[i]; _metadata.Builder.AddMethodImplementation((TypeDefinitionHandle)_metadata.GetTypeHandle(method.DeclaringType), handle, _metadata.GetMethodHandle(itfImpl)); } } } } // Add generic parameters if (method.IsGenericMethodDefinition) { int index = 0; foreach (var arg in method.GetGenericArguments()) { genericParams.Add(new DelayedWrite(CodedIndex.TypeOrMethodDef(handle), () => { // Add the argument var gaHandle = _metadata.Builder.AddGenericParameter(handle, arg.GenericParameterAttributes, _metadata.GetOrAddString(arg.Name), index++); // Add it's constraints foreach (var constraint in arg.GetGenericParameterConstraints()) { _metadata.Builder.AddGenericParameterConstraint(gaHandle, _metadata.GetTypeHandle(constraint)); } })); } } VerifyEmittedHandle(metadata, handle); metadata.MarkAsEmitted(); CreateCustomAttributes(handle, method.GetCustomAttributesData()); }
public static ReadyToRunStandaloneMethodMetadata Compute(EcmaMethod wrappedMethod) { var metadataReader = wrappedMethod.MetadataReader; var _module = wrappedMethod.Module; var rva = wrappedMethod.MetadataReader.GetMethodDefinition(wrappedMethod.Handle).RelativeVirtualAddress; var _methodBody = _module.PEReader.GetMethodBody(rva); byte[] _alternateILStream = _methodBody.GetILBytes(); var exceptionRegions = _methodBody.ExceptionRegions; ILExceptionRegion[] _exceptionRegions; int length = exceptionRegions.Length; if (length == 0) { _exceptionRegions = Array.Empty <ILExceptionRegion>(); } else { _exceptionRegions = new ILExceptionRegion[length]; for (int i = 0; i < length; i++) { var exceptionRegion = exceptionRegions[i]; _exceptionRegions[i] = new ILExceptionRegion( (ILExceptionRegionKind)exceptionRegion.Kind, // assumes that ILExceptionRegionKind and ExceptionRegionKind enums are in sync exceptionRegion.TryOffset, exceptionRegion.TryLength, exceptionRegion.HandlerOffset, exceptionRegion.HandlerLength, MetadataTokens.GetToken(exceptionRegion.CatchType), exceptionRegion.FilterOffset); } } BlobReader localsBlob = default(BlobReader); if (!_methodBody.LocalSignature.IsNil) { localsBlob = wrappedMethod.MetadataReader.GetBlobReader(wrappedMethod.MetadataReader.GetStandaloneSignature(_methodBody.LocalSignature).Signature); } AlternativeTypeRefProvider alternateTypes = new AlternativeTypeRefProvider(); EcmaSignatureEncoder <AlternativeTypeRefProvider> alternateEncoder = new EcmaSignatureEncoder <AlternativeTypeRefProvider>(alternateTypes); Dictionary <int, int> _alternateNonTokenStrings = new Dictionary <int, int>(); BlobBuilder _alternateNonTypeRefStream = new BlobBuilder(); BlobBuilder _nonCodeAlternateBlob = new BlobBuilder(); { // Generate alternate stream for exceptions. _nonCodeAlternateBlob.WriteCompressedInteger(_exceptionRegions.Length); for (int i = 0; i < _exceptionRegions.Length; i++) { var region = _exceptionRegions[i]; _nonCodeAlternateBlob.WriteCompressedInteger((int)region.Kind); _nonCodeAlternateBlob.WriteCompressedInteger((int)region.TryOffset); _nonCodeAlternateBlob.WriteCompressedInteger((int)region.TryLength); _nonCodeAlternateBlob.WriteCompressedInteger((int)region.HandlerOffset); _nonCodeAlternateBlob.WriteCompressedInteger((int)region.HandlerLength); if (region.Kind == ILExceptionRegionKind.Catch) { int alternateToken = GetAlternateStreamToken(region.ClassToken); int encodedToken = CodedIndex.TypeDefOrRefOrSpec(MetadataTokens.EntityHandle(alternateToken)); _nonCodeAlternateBlob.WriteCompressedInteger(encodedToken); } else if (region.Kind == ILExceptionRegionKind.Filter) { _nonCodeAlternateBlob.WriteCompressedInteger(region.FilterOffset); } } if (localsBlob.Length == 0) { // No locals. Encode a 2 to indicate this _nonCodeAlternateBlob.WriteByte(2); } else { _nonCodeAlternateBlob.WriteByte(_methodBody.LocalVariablesInitialized ? (byte)1 : (byte)0); EcmaSignatureTranslator sigTranslator = new EcmaSignatureTranslator(localsBlob, _nonCodeAlternateBlob, GetAlternateStreamToken); sigTranslator.ParseLocalsSignature(); } } ILTokenReplacer.Replace(_alternateILStream, GetAlternateStreamToken); // Add all the streams together into the _nonCodeAlternateBlob int expectedFinalSize = _nonCodeAlternateBlob.Count + _alternateILStream.Length + _alternateNonTypeRefStream.Count; _nonCodeAlternateBlob.WriteBytes(_alternateILStream); _nonCodeAlternateBlob.LinkSuffix(_alternateNonTypeRefStream); Debug.Assert(expectedFinalSize == _nonCodeAlternateBlob.Count); ReadyToRunStandaloneMethodMetadata methodBlock = new ReadyToRunStandaloneMethodMetadata(); methodBlock.ConstantData = _nonCodeAlternateBlob.ToArray(); methodBlock.TypeRefs = alternateTypes._alternateTypeRefStream.ToArray(); return(methodBlock); ///// HELPER FUNCTIONS unsafe int GetAlternateStreamToken(int token) { if (token == 0 || !_alternateNonTokenStrings.TryGetValue(token, out int alternateToken)) { var handle = MetadataTokens.Handle(token); if (handle.Kind == HandleKind.TypeDefinition || handle.Kind == HandleKind.TypeReference) { EcmaType ecmaType = (EcmaType)wrappedMethod.Module.GetObject(MetadataTokens.EntityHandle(token)); alternateToken = MetadataTokens.GetToken(alternateTypes.GetTypeDefOrRefHandleForTypeDesc(ecmaType)); } else { BlobBuilder blob = new BlobBuilder(); int flag = 0; if (handle.Kind == HandleKind.UserString) { string strAlternate = metadataReader.GetUserString((UserStringHandle)handle); flag = 0x70000000; blob.WriteCompressedInteger(strAlternate.Length); fixed(char *charData = strAlternate) { blob.WriteBytes((byte *)charData, strAlternate.Length * 2); } // TODO: consider encoding via wtf-8, or possibly utf-8 } else if (handle.Kind == HandleKind.TypeSpecification) { flag = 0x1B000000; var sigBlob = metadataReader.GetBlobReader(metadataReader.GetTypeSpecification((TypeSpecificationHandle)handle).Signature); EcmaSignatureTranslator sigTranslator = new EcmaSignatureTranslator(sigBlob, blob, GetAlternateStreamToken); sigTranslator.ParseType(); } else if (handle.Kind == HandleKind.MemberReference) { flag = 0x0a000000; var memberReference = metadataReader.GetMemberReference((MemberReferenceHandle)handle); var sigBlob = metadataReader.GetBlobReader(memberReference.Signature); EcmaSignatureTranslator sigTranslator = new EcmaSignatureTranslator(sigBlob, blob, GetAlternateStreamToken); sigTranslator.ParseMemberRefSignature(); blob.WriteSerializedString(metadataReader.GetString(memberReference.Name)); blob.WriteCompressedInteger(CodedIndex.MemberRefParent(MetadataTokens.EntityHandle(GetAlternateStreamToken(MetadataTokens.GetToken(memberReference.Parent))))); } else if (handle.Kind == HandleKind.MethodDefinition) { flag = 0x0a000000; var methodDefinition = metadataReader.GetMethodDefinition((MethodDefinitionHandle)handle); var sigBlob = metadataReader.GetBlobReader(methodDefinition.Signature); EcmaSignatureTranslator sigTranslator = new EcmaSignatureTranslator(sigBlob, blob, GetAlternateStreamToken); sigTranslator.ParseMethodSignature(); blob.WriteSerializedString(metadataReader.GetString(methodDefinition.Name)); blob.WriteCompressedInteger(CodedIndex.MemberRefParent(MetadataTokens.EntityHandle(GetAlternateStreamToken(MetadataTokens.GetToken(methodDefinition.GetDeclaringType()))))); } else if (handle.Kind == HandleKind.FieldDefinition) { flag = 0x0a000000; var fieldDefinition = metadataReader.GetFieldDefinition((FieldDefinitionHandle)handle); var sigBlob = metadataReader.GetBlobReader(fieldDefinition.Signature); EcmaSignatureTranslator sigTranslator = new EcmaSignatureTranslator(sigBlob, blob, GetAlternateStreamToken); sigTranslator.ParseFieldSignature(); blob.WriteSerializedString(metadataReader.GetString(fieldDefinition.Name)); blob.WriteCompressedInteger(CodedIndex.MemberRefParent(MetadataTokens.EntityHandle(GetAlternateStreamToken(MetadataTokens.GetToken(fieldDefinition.GetDeclaringType()))))); } else if (handle.Kind == HandleKind.MethodSpecification) { flag = 0x2B000000; var methodSpecification = metadataReader.GetMethodSpecification((MethodSpecificationHandle)handle); var sigBlob = metadataReader.GetBlobReader(methodSpecification.Signature); blob.WriteCompressedInteger(MetadataTokens.GetRowNumber(MetadataTokens.EntityHandle(GetAlternateStreamToken(MetadataTokens.GetToken(methodSpecification.Method))))); EcmaSignatureTranslator sigTranslator = new EcmaSignatureTranslator(sigBlob, blob, GetAlternateStreamToken); sigTranslator.ParseMethodSpecSignature(); } else if (handle.Kind == HandleKind.StandaloneSignature) { flag = 0x11000000; var reader = wrappedMethod.Module.MetadataReader; var sigBlob = reader.GetBlobReader(reader.GetStandaloneSignature((StandaloneSignatureHandle)handle).Signature); EcmaSignatureTranslator sigTranslator = new EcmaSignatureTranslator(sigBlob, blob, GetAlternateStreamToken); sigTranslator.ParseMethodSignature(); } _alternateNonTypeRefStream.WriteBytes(blob.ToArray()); alternateToken = (_alternateNonTokenStrings.Count + 1) | flag; } _alternateNonTokenStrings.Add(token, alternateToken); } return(alternateToken); } }
private void CreateType(Type type) { // Check reserved and not already emitted if (!_metadata.TryGetTypeDefinition(type, out var metadata)) { ThrowMetadataIsNotReserved("Type", type); } EnsureMetadataWasNotEmitted(metadata, type); // Add the type definition var baseTypeHandle = type.BaseType != null?_metadata.GetTypeHandle(type.BaseType) : default; var typeHandle = _metadata.Builder.AddTypeDefinition( type.Attributes, type.DeclaringType == null ? _metadata.GetOrAddString(ApplyNameChange(type.Namespace)) : default(StringHandle), _metadata.GetOrAddString(type.Name), baseTypeHandle, MetadataTokens.FieldDefinitionHandle(_metadata.Builder.GetRowCount(TableIndex.Field) + 1), MetadataTokens.MethodDefinitionHandle(_metadata.Builder.GetRowCount(TableIndex.MethodDef) + 1)); // Verify and mark emitted VerifyEmittedHandle(metadata, typeHandle); metadata.MarkAsEmitted(); // Setup pack and size attributes (if explicit layout) if (type.IsExplicitLayout) { _metadata.Builder.AddTypeLayout( typeHandle, (ushort)type.StructLayoutAttribute.Pack, (uint)type.StructLayoutAttribute.Size ); } // Add implemented interfaces (not for enums though - eg: IComparable etc...) if (!type.IsEnum) { foreach (var itf in type.GetInterfaces().OrderBy(t => CodedIndex.TypeDefOrRefOrSpec(_metadata.GetTypeHandle(t)))) { _metadata.Builder.AddInterfaceImplementation(typeHandle, _metadata.GetTypeHandle(itf)); } } // Setup enclosing type if (type.DeclaringType != null) { _metadata.Builder.AddNestedType(typeHandle, (TypeDefinitionHandle)_metadata.GetTypeHandle(type.DeclaringType)); } // Create attributes CreateCustomAttributes(typeHandle, type.GetCustomAttributesData()); // Handle generics type if (type.IsGenericType) { if (type.IsGenericTypeDefinition) { var genericType = type.GetGenericTypeDefinition(); var typeInfo = genericType.GetTypeInfo(); for (var i = 0; i < typeInfo.GenericTypeParameters.Length; ++i) { var parm = typeInfo.GenericTypeParameters[i]; var attr = parm.GenericParameterAttributes; var genericParameterHandle = _metadata.Builder.AddGenericParameter(typeHandle, attr, _metadata.GetOrAddString(parm.Name), i); foreach (var constraint in parm.GetGenericParameterConstraints()) { _metadata.Builder.AddGenericParameterConstraint(genericParameterHandle, _metadata.GetTypeHandle(constraint)); } } } } // Create members... CreateFields(type.GetFields(AllFields)); CreatePropertiesForType(type.GetProperties(AllProperties)); CreateEventsForType(type.GetEvents(AllEvents)); CreateConstructors(type.GetConstructors(AllMethods)); CreateMethods(type.GetMethods(AllMethods)); }
int GetCodedIndexSize(CodedIndex ci) { return(Utilities.GetCodedIndexSize(ci, new Utilities.TableRowCounter(m_mtrv.GetNumberOfRows), m_ciCache)); }
public static MetadataToken GetMetadataToken (CodedIndex cidx, uint data) { uint rid = 0; switch (cidx) { <% $coded_indexes.each { |ci| %> case CodedIndex.<%=ci.name%> :
public ReservedBlob <GuidHandle> Import() { if (_reader.IsAssembly) { var srcAssembly = _reader.GetAssemblyDefinition(); _builder.AddAssembly(ImportValue(srcAssembly.Name), srcAssembly.Version, ImportValue(srcAssembly.Culture), ImportValue(srcAssembly.PublicKey), srcAssembly.Flags, srcAssembly.HashAlgorithm); Debug?.Invoke($"Imported assembly {_reader.ToString(srcAssembly)}"); } var srcModule = _reader.GetModuleDefinition(); var mvidBlob = _builder.ReserveGuid(); _builder.AddModule(srcModule.Generation, ImportValue(srcModule.Name), mvidBlob.Handle, ImportValue(srcModule.GenerationId), ImportValue(srcModule.BaseGenerationId)); Debug?.Invoke($"Imported module {_reader.ToString(srcModule)}"); Debug?.Invoke($"Importing assembly files"); foreach (var srcHandle in _reader.AssemblyFiles) { Import(srcHandle); } var index = 1; Debug?.Invoke($"Preparing type list for import"); var checker = new CachedAttributeChecker(); foreach (var srcHandle in _reader.TypeDefinitions) { bool shouldImport; var src = _reader.GetTypeDefinition(srcHandle); if (checker.HasAttribute(_reader, src, AttributeNames.Embedded) && checker.HasAttribute(_reader, src, AttributeNames.CompilerGenerated)) { Trace?.Invoke($"Embedded type found {_reader.ToString(srcHandle)}"); shouldImport = true; } else { shouldImport = Filter?.AllowImport(_reader.GetTypeDefinition(srcHandle), _reader) != false; } if (shouldImport) { _typeDefinitionCache[srcHandle] = MetadataTokens.TypeDefinitionHandle(index++); } else { Trace?.Invoke($"Type filtered and will not be imported {_reader.ToString(srcHandle)}"); } } Debug?.Invoke($"Importing type definitions"); foreach (var srcHandle in _reader.TypeDefinitions.Where(_typeDefinitionCache.ContainsKey)) { var dstHandle = ImportTypeDefinitionSkeleton(srcHandle); if (dstHandle != _typeDefinitionCache[srcHandle]) { throw new Exception("WTF: type handle mismatch"); } } Debug?.Invoke($"Importing type definition accessories"); foreach (var(srcHandle, dstHandle) in _typeDefinitionCache) { ImportTypeDefinitionAccessories(srcHandle, dstHandle); } Debug?.Invoke($"Importing method definition accessories"); foreach (var(srcHandle, dstHandle) in _methodDefinitionCache) { ImportMethodDefinitionAccessories(srcHandle, dstHandle); } Debug?.Invoke($"Importing field definition accessories"); foreach (var(srcHandle, dstHandle) in _fieldDefinitionCache) { ImportFieldDefinitionAccessories(srcHandle, dstHandle); } Debug?.Invoke($"Importing nested classes"); var nestedTypes = _typeDefinitionCache .Select(kv => Tuple.Create(kv.Value, _reader.GetTypeDefinition(kv.Key).GetNestedTypes())) .SelectMany(x => x.Item2.Select(y => Tuple.Create(x.Item1, y, Import(y)))) .Where(x => !x.Item3.IsNil) .OrderBy(x => RowId(x.Item3)) .ToList(); foreach (var(dstHandle, srcNested, dstNested) in nestedTypes) { _builder.AddNestedType(dstNested, dstHandle); Trace?.Invoke($"Imported nested type {_reader.ToString(srcNested)} -> {RowId(dstNested):X}"); } var generic = _typeDefinitionCache .Select(kv => Tuple.Create((EntityHandle)kv.Value, _reader.GetTypeDefinition(kv.Key).GetGenericParameters())) .Concat(_methodDefinitionCache .Select(kv => Tuple.Create((EntityHandle)kv.Value, _reader.GetMethodDefinition(kv.Key).GetGenericParameters()))) .OrderBy(x => CodedIndex.TypeOrMethodDef(x.Item1)) .ToList(); Debug?.Invoke($"Importing generic constraints"); foreach (var(dstHandle, genericParams) in generic) { ImportGenericConstraints(dstHandle, genericParams); } Debug?.Invoke($"Importing custom attributes"); foreach (var src in _reader.CustomAttributes) { Import(src); } Debug?.Invoke($"Importing declarative security attributes"); foreach (var src in _reader.DeclarativeSecurityAttributes) { Import(src); } Debug?.Invoke($"Importing exported types"); foreach (var src in _reader.ExportedTypes) { Import(src); } Debug?.Invoke($"Importing done"); return(mvidBlob); }
int GetCodedIndexSize(CodedIndex ci) { return(Utilities.GetCodedIndexSize(ci, m_counter, m_ciCache)); }
public static MetadataToken GetMetadataToken(CodedIndex cidx, uint data) { uint rid = 0; switch (cidx) { case CodedIndex.TypeDefOrRef: rid = data >> 2; switch (data & 3) { case 0: return(new MetadataToken(TokenType.TypeDef, rid)); case 1: return(new MetadataToken(TokenType.TypeRef, rid)); case 2: return(new MetadataToken(TokenType.TypeSpec, rid)); default: return(MetadataToken.Zero); } case CodedIndex.HasConstant: rid = data >> 2; switch (data & 3) { case 0: return(new MetadataToken(TokenType.Field, rid)); case 1: return(new MetadataToken(TokenType.Param, rid)); case 2: return(new MetadataToken(TokenType.Property, rid)); default: return(MetadataToken.Zero); } case CodedIndex.HasCustomAttribute: rid = data >> 5; switch (data & 31) { case 0: return(new MetadataToken(TokenType.Method, rid)); case 1: return(new MetadataToken(TokenType.Field, rid)); case 2: return(new MetadataToken(TokenType.TypeRef, rid)); case 3: return(new MetadataToken(TokenType.TypeDef, rid)); case 4: return(new MetadataToken(TokenType.Param, rid)); case 5: return(new MetadataToken(TokenType.InterfaceImpl, rid)); case 6: return(new MetadataToken(TokenType.MemberRef, rid)); case 7: return(new MetadataToken(TokenType.Module, rid)); case 8: return(new MetadataToken(TokenType.Permission, rid)); case 9: return(new MetadataToken(TokenType.Property, rid)); case 10: return(new MetadataToken(TokenType.Event, rid)); case 11: return(new MetadataToken(TokenType.Signature, rid)); case 12: return(new MetadataToken(TokenType.ModuleRef, rid)); case 13: return(new MetadataToken(TokenType.TypeSpec, rid)); case 14: return(new MetadataToken(TokenType.Assembly, rid)); case 15: return(new MetadataToken(TokenType.AssemblyRef, rid)); case 16: return(new MetadataToken(TokenType.File, rid)); case 17: return(new MetadataToken(TokenType.ExportedType, rid)); case 18: return(new MetadataToken(TokenType.ManifestResource, rid)); case 19: return(new MetadataToken(TokenType.GenericParam, rid)); default: return(MetadataToken.Zero); } case CodedIndex.HasFieldMarshal: rid = data >> 1; switch (data & 1) { case 0: return(new MetadataToken(TokenType.Field, rid)); case 1: return(new MetadataToken(TokenType.Param, rid)); default: return(MetadataToken.Zero); } case CodedIndex.HasDeclSecurity: rid = data >> 2; switch (data & 3) { case 0: return(new MetadataToken(TokenType.TypeDef, rid)); case 1: return(new MetadataToken(TokenType.Method, rid)); case 2: return(new MetadataToken(TokenType.Assembly, rid)); default: return(MetadataToken.Zero); } case CodedIndex.MemberRefParent: rid = data >> 3; switch (data & 7) { case 0: return(new MetadataToken(TokenType.TypeDef, rid)); case 1: return(new MetadataToken(TokenType.TypeRef, rid)); case 2: return(new MetadataToken(TokenType.ModuleRef, rid)); case 3: return(new MetadataToken(TokenType.Method, rid)); case 4: return(new MetadataToken(TokenType.TypeSpec, rid)); default: return(MetadataToken.Zero); } case CodedIndex.HasSemantics: rid = data >> 1; switch (data & 1) { case 0: return(new MetadataToken(TokenType.Event, rid)); case 1: return(new MetadataToken(TokenType.Property, rid)); default: return(MetadataToken.Zero); } case CodedIndex.MethodDefOrRef: rid = data >> 1; switch (data & 1) { case 0: return(new MetadataToken(TokenType.Method, rid)); case 1: return(new MetadataToken(TokenType.MemberRef, rid)); default: return(MetadataToken.Zero); } case CodedIndex.MemberForwarded: rid = data >> 1; switch (data & 1) { case 0: return(new MetadataToken(TokenType.Field, rid)); case 1: return(new MetadataToken(TokenType.Method, rid)); default: return(MetadataToken.Zero); } case CodedIndex.Implementation: rid = data >> 2; switch (data & 3) { case 0: return(new MetadataToken(TokenType.File, rid)); case 1: return(new MetadataToken(TokenType.AssemblyRef, rid)); case 2: return(new MetadataToken(TokenType.ExportedType, rid)); default: return(MetadataToken.Zero); } case CodedIndex.CustomAttributeType: rid = data >> 3; switch (data & 7) { case 2: return(new MetadataToken(TokenType.Method, rid)); case 3: return(new MetadataToken(TokenType.MemberRef, rid)); default: return(MetadataToken.Zero); } case CodedIndex.ResolutionScope: rid = data >> 2; switch (data & 3) { case 0: return(new MetadataToken(TokenType.Module, rid)); case 1: return(new MetadataToken(TokenType.ModuleRef, rid)); case 2: return(new MetadataToken(TokenType.AssemblyRef, rid)); case 3: return(new MetadataToken(TokenType.TypeRef, rid)); default: return(MetadataToken.Zero); } case CodedIndex.TypeOrMethodDef: rid = data >> 1; switch (data & 1) { case 0: return(new MetadataToken(TokenType.TypeDef, rid)); case 1: return(new MetadataToken(TokenType.Method, rid)); default: return(MetadataToken.Zero); } default: return(MetadataToken.Zero); } }
public void Read(ClrModuleReader reader) { this.EventFlags = (EventAttributes)reader.Binary.ReadUInt16(); this.Name = reader.ReadString(); this.EventType = reader.ReadCodedIndex <TypeDefOrRef>(); }
public static uint CompressMetadataToken(CodedIndex cidx, MetadataToken token) { uint ret = 0; if (token.RID == 0) { return(ret); } switch (cidx) { case CodedIndex.TypeDefOrRef: ret = token.RID << 2; switch (token.TokenType) { case TokenType.TypeDef: return(ret | 0); case TokenType.TypeRef: return(ret | 1); case TokenType.TypeSpec: return(ret | 2); default: throw new MetadataFormatException("Non valid Token for TypeDefOrRef"); } case CodedIndex.HasConstant: ret = token.RID << 2; switch (token.TokenType) { case TokenType.Field: return(ret | 0); case TokenType.Param: return(ret | 1); case TokenType.Property: return(ret | 2); default: throw new MetadataFormatException("Non valid Token for HasConstant"); } case CodedIndex.HasCustomAttribute: ret = token.RID << 5; switch (token.TokenType) { case TokenType.Method: return(ret | 0); case TokenType.Field: return(ret | 1); case TokenType.TypeRef: return(ret | 2); case TokenType.TypeDef: return(ret | 3); case TokenType.Param: return(ret | 4); case TokenType.InterfaceImpl: return(ret | 5); case TokenType.MemberRef: return(ret | 6); case TokenType.Module: return(ret | 7); case TokenType.Permission: return(ret | 8); case TokenType.Property: return(ret | 9); case TokenType.Event: return(ret | 10); case TokenType.Signature: return(ret | 11); case TokenType.ModuleRef: return(ret | 12); case TokenType.TypeSpec: return(ret | 13); case TokenType.Assembly: return(ret | 14); case TokenType.AssemblyRef: return(ret | 15); case TokenType.File: return(ret | 16); case TokenType.ExportedType: return(ret | 17); case TokenType.ManifestResource: return(ret | 18); case TokenType.GenericParam: return(ret | 19); default: throw new MetadataFormatException("Non valid Token for HasCustomAttribute"); } case CodedIndex.HasFieldMarshal: ret = token.RID << 1; switch (token.TokenType) { case TokenType.Field: return(ret | 0); case TokenType.Param: return(ret | 1); default: throw new MetadataFormatException("Non valid Token for HasFieldMarshal"); } case CodedIndex.HasDeclSecurity: ret = token.RID << 2; switch (token.TokenType) { case TokenType.TypeDef: return(ret | 0); case TokenType.Method: return(ret | 1); case TokenType.Assembly: return(ret | 2); default: throw new MetadataFormatException("Non valid Token for HasDeclSecurity"); } case CodedIndex.MemberRefParent: ret = token.RID << 3; switch (token.TokenType) { case TokenType.TypeDef: return(ret | 0); case TokenType.TypeRef: return(ret | 1); case TokenType.ModuleRef: return(ret | 2); case TokenType.Method: return(ret | 3); case TokenType.TypeSpec: return(ret | 4); default: throw new MetadataFormatException("Non valid Token for MemberRefParent"); } case CodedIndex.HasSemantics: ret = token.RID << 1; switch (token.TokenType) { case TokenType.Event: return(ret | 0); case TokenType.Property: return(ret | 1); default: throw new MetadataFormatException("Non valid Token for HasSemantics"); } case CodedIndex.MethodDefOrRef: ret = token.RID << 1; switch (token.TokenType) { case TokenType.Method: return(ret | 0); case TokenType.MemberRef: return(ret | 1); default: throw new MetadataFormatException("Non valid Token for MethodDefOrRef"); } case CodedIndex.MemberForwarded: ret = token.RID << 1; switch (token.TokenType) { case TokenType.Field: return(ret | 0); case TokenType.Method: return(ret | 1); default: throw new MetadataFormatException("Non valid Token for MemberForwarded"); } case CodedIndex.Implementation: ret = token.RID << 2; switch (token.TokenType) { case TokenType.File: return(ret | 0); case TokenType.AssemblyRef: return(ret | 1); case TokenType.ExportedType: return(ret | 2); default: throw new MetadataFormatException("Non valid Token for Implementation"); } case CodedIndex.CustomAttributeType: ret = token.RID << 3; switch (token.TokenType) { case TokenType.Method: return(ret | 2); case TokenType.MemberRef: return(ret | 3); default: throw new MetadataFormatException("Non valid Token for CustomAttributeType"); } case CodedIndex.ResolutionScope: ret = token.RID << 2; switch (token.TokenType) { case TokenType.Module: return(ret | 0); case TokenType.ModuleRef: return(ret | 1); case TokenType.AssemblyRef: return(ret | 2); case TokenType.TypeRef: return(ret | 3); default: throw new MetadataFormatException("Non valid Token for ResolutionScope"); } case CodedIndex.TypeOrMethodDef: ret = token.RID << 1; switch (token.TokenType) { case TokenType.TypeDef: return(ret | 0); case TokenType.Method: return(ret | 1); default: throw new MetadataFormatException("Non valid Token for TypeOrMethodDef"); } default: throw new MetadataFormatException("Non valid CodedIndex"); } }
int GetCodedIndexSize(CodedIndex index) { return(image.GetCodedIndexSize(index)); }
internal static int GetCodedIndexSize(CodedIndex ci, TableRowCounter rowCounter, int [] codedIndexCache) { int bits = 0, max = 0, index = (int)ci; if (codedIndexCache [index] != 0) { return(codedIndexCache [index]); } int res = 0; int [] rids; switch (ci) { case CodedIndex.TypeDefOrRef: bits = 2; rids = new int [3]; rids [0] = TypeDefTable.RId; rids [1] = TypeRefTable.RId; rids [2] = TypeSpecTable.RId; break; case CodedIndex.HasConstant: bits = 2; rids = new int [3]; rids [0] = FieldTable.RId; rids [1] = ParamTable.RId; rids [2] = PropertyTable.RId; break; case CodedIndex.HasCustomAttribute: bits = 5; rids = new int [20]; rids [0] = MethodTable.RId; rids [1] = FieldTable.RId; rids [2] = TypeRefTable.RId; rids [3] = TypeDefTable.RId; rids [4] = ParamTable.RId; rids [5] = InterfaceImplTable.RId; rids [6] = MemberRefTable.RId; rids [7] = ModuleTable.RId; rids [8] = DeclSecurityTable.RId; rids [9] = PropertyTable.RId; rids [10] = EventTable.RId; rids [11] = StandAloneSigTable.RId; rids [12] = ModuleRefTable.RId; rids [13] = TypeSpecTable.RId; rids [14] = AssemblyTable.RId; rids [15] = AssemblyRefTable.RId; rids [16] = FileTable.RId; rids [17] = ExportedTypeTable.RId; rids [18] = ManifestResourceTable.RId; rids [19] = GenericParamTable.RId; break; case CodedIndex.HasFieldMarshal: bits = 1; rids = new int [2]; rids [0] = FieldTable.RId; rids [1] = ParamTable.RId; break; case CodedIndex.HasDeclSecurity: bits = 2; rids = new int [3]; rids [0] = TypeDefTable.RId; rids [1] = MethodTable.RId; rids [2] = AssemblyTable.RId; break; case CodedIndex.MemberRefParent: bits = 3; rids = new int [5]; rids [0] = TypeDefTable.RId; rids [1] = TypeRefTable.RId; rids [2] = ModuleRefTable.RId; rids [3] = MethodTable.RId; rids [4] = TypeSpecTable.RId; break; case CodedIndex.HasSemantics: bits = 1; rids = new int [2]; rids [0] = EventTable.RId; rids [1] = PropertyTable.RId; break; case CodedIndex.MethodDefOrRef: bits = 1; rids = new int [2]; rids [0] = MethodTable.RId; rids [1] = MemberRefTable.RId; break; case CodedIndex.MemberForwarded: bits = 1; rids = new int [2]; rids [0] = FieldTable.RId; rids [1] = MethodTable.RId; break; case CodedIndex.Implementation: bits = 2; rids = new int [3]; rids [0] = FileTable.RId; rids [1] = AssemblyRefTable.RId; rids [2] = ExportedTypeTable.RId; break; case CodedIndex.CustomAttributeType: bits = 3; rids = new int [2]; rids [0] = MethodTable.RId; rids [1] = MemberRefTable.RId; break; case CodedIndex.ResolutionScope: bits = 2; rids = new int [4]; rids [0] = ModuleTable.RId; rids [1] = ModuleRefTable.RId; rids [2] = AssemblyRefTable.RId; rids [3] = TypeRefTable.RId; break; case CodedIndex.TypeOrMethodDef: bits = 1; rids = new int [2]; rids [0] = TypeDefTable.RId; rids [1] = MethodTable.RId; break; default: throw new MetadataFormatException("Non valid CodedIndex"); } for (int i = 0; i < rids.Length; i++) { int rows = rowCounter(rids [i]); if (rows > max) { max = rows; } } res = max < (1 << (16 - bits)) ? 2 : 4; codedIndexCache [index] = res; return(res); }
public static MetadataToken GetMetadataToken(this CodedIndex self, uint data) { uint rid; TokenType token_type; switch (self) { case CodedIndex.TypeDefOrRef: rid = data >> 2; switch (data & 3) { case 0: token_type = TokenType.TypeDef; goto ret; case 1: token_type = TokenType.TypeRef; goto ret; case 2: token_type = TokenType.TypeSpec; goto ret; default: goto exit; } case CodedIndex.HasConstant: rid = data >> 2; switch (data & 3) { case 0: token_type = TokenType.Field; goto ret; case 1: token_type = TokenType.Param; goto ret; case 2: token_type = TokenType.Property; goto ret; default: goto exit; } case CodedIndex.HasCustomAttribute: rid = data >> 5; switch (data & 31) { case 0: token_type = TokenType.Method; goto ret; case 1: token_type = TokenType.Field; goto ret; case 2: token_type = TokenType.TypeRef; goto ret; case 3: token_type = TokenType.TypeDef; goto ret; case 4: token_type = TokenType.Param; goto ret; case 5: token_type = TokenType.InterfaceImpl; goto ret; case 6: token_type = TokenType.MemberRef; goto ret; case 7: token_type = TokenType.Module; goto ret; case 8: token_type = TokenType.Permission; goto ret; case 9: token_type = TokenType.Property; goto ret; case 10: token_type = TokenType.Event; goto ret; case 11: token_type = TokenType.Signature; goto ret; case 12: token_type = TokenType.ModuleRef; goto ret; case 13: token_type = TokenType.TypeSpec; goto ret; case 14: token_type = TokenType.Assembly; goto ret; case 15: token_type = TokenType.AssemblyRef; goto ret; case 16: token_type = TokenType.File; goto ret; case 17: token_type = TokenType.ExportedType; goto ret; case 18: token_type = TokenType.ManifestResource; goto ret; case 19: token_type = TokenType.GenericParam; goto ret; default: goto exit; } case CodedIndex.HasFieldMarshal: rid = data >> 1; switch (data & 1) { case 0: token_type = TokenType.Field; goto ret; case 1: token_type = TokenType.Param; goto ret; default: goto exit; } case CodedIndex.HasDeclSecurity: rid = data >> 2; switch (data & 3) { case 0: token_type = TokenType.TypeDef; goto ret; case 1: token_type = TokenType.Method; goto ret; case 2: token_type = TokenType.Assembly; goto ret; default: goto exit; } case CodedIndex.MemberRefParent: rid = data >> 3; switch (data & 7) { case 0: token_type = TokenType.TypeDef; goto ret; case 1: token_type = TokenType.TypeRef; goto ret; case 2: token_type = TokenType.ModuleRef; goto ret; case 3: token_type = TokenType.Method; goto ret; case 4: token_type = TokenType.TypeSpec; goto ret; default: goto exit; } case CodedIndex.HasSemantics: rid = data >> 1; switch (data & 1) { case 0: token_type = TokenType.Event; goto ret; case 1: token_type = TokenType.Property; goto ret; default: goto exit; } case CodedIndex.MethodDefOrRef: rid = data >> 1; switch (data & 1) { case 0: token_type = TokenType.Method; goto ret; case 1: token_type = TokenType.MemberRef; goto ret; default: goto exit; } case CodedIndex.MemberForwarded: rid = data >> 1; switch (data & 1) { case 0: token_type = TokenType.Field; goto ret; case 1: token_type = TokenType.Method; goto ret; default: goto exit; } case CodedIndex.Implementation: rid = data >> 2; switch (data & 3) { case 0: token_type = TokenType.File; goto ret; case 1: token_type = TokenType.AssemblyRef; goto ret; case 2: token_type = TokenType.ExportedType; goto ret; default: goto exit; } case CodedIndex.CustomAttributeType: rid = data >> 3; switch (data & 7) { case 2: token_type = TokenType.Method; goto ret; case 3: token_type = TokenType.MemberRef; goto ret; default: goto exit; } case CodedIndex.ResolutionScope: rid = data >> 2; switch (data & 3) { case 0: token_type = TokenType.Module; goto ret; case 1: token_type = TokenType.ModuleRef; goto ret; case 2: token_type = TokenType.AssemblyRef; goto ret; case 3: token_type = TokenType.TypeRef; goto ret; default: goto exit; } case CodedIndex.TypeOrMethodDef: rid = data >> 1; switch (data & 1) { case 0: token_type = TokenType.TypeDef; goto ret; case 1: token_type = TokenType.Method; goto ret; default: goto exit; } default: goto exit; } ret: return(new MetadataToken(token_type, rid)); exit: return(MetadataToken.Zero); }
private BlobHandle SerializeLocalConstantSignature(ILocalDefinition localConstant) { var builder = new BlobBuilder(); // TODO: BlobEncoder.LocalConstantSignature // CustomMod* var encoder = new CustomModifiersEncoder(builder); SerializeCustomModifiers(encoder, localConstant.CustomModifiers); var type = localConstant.Type; var typeCode = type.TypeCode; object value = localConstant.CompileTimeValue.Value; // PrimitiveConstant or EnumConstant if (value is decimal) { builder.WriteByte((byte)SignatureTypeKind.ValueType); builder.WriteCompressedInteger(CodedIndex.TypeDefOrRefOrSpec(GetTypeHandle(type))); builder.WriteDecimal((decimal)value); } else if (value is DateTime) { builder.WriteByte((byte)SignatureTypeKind.ValueType); builder.WriteCompressedInteger(CodedIndex.TypeDefOrRefOrSpec(GetTypeHandle(type))); builder.WriteDateTime((DateTime)value); } else if (typeCode == PrimitiveTypeCode.String) { builder.WriteByte((byte)ConstantTypeCode.String); if (value == null) { builder.WriteByte(0xff); } else { builder.WriteUTF16((string)value); } } else if (value != null) { // TypeCode builder.WriteByte((byte)GetConstantTypeCode(value)); // Value builder.WriteConstant(value); // EnumType if (type.IsEnum) { builder.WriteCompressedInteger(CodedIndex.TypeDefOrRefOrSpec(GetTypeHandle(type))); } } else if (this.module.IsPlatformType(type, PlatformType.SystemObject)) { builder.WriteByte((byte)SignatureTypeCode.Object); } else { builder.WriteByte((byte)(type.IsValueType ? SignatureTypeKind.ValueType : SignatureTypeKind.Class)); builder.WriteCompressedInteger(CodedIndex.TypeDefOrRefOrSpec(GetTypeHandle(type))); } return(_debugMetadataOpt.GetOrAddBlob(builder)); }
/// <summary> /// Initiliases a new instance of the TypeRef class using the provided details. /// </summary> /// <param name="definingAssembly">The assembly which defines the type reference</param> /// <param name="name">The name of the type reference</param> /// <param name="namespaceName">The namespace it is defined in</param> /// <param name="resolutionScope">A CodedIndex determining the resolve the external reference</param> public TypeRef(AssemblyDef definingAssembly, string name, string namespaceName, CodedIndex resolutionScope) { UniqueId = definingAssembly.CreateUniqueId(); Assembly = definingAssembly; Name = name; Namespace = namespaceName; _isExternalReference = true; _resolutionScope = resolutionScope; _isGeneric = name.IndexOf('`') != -1; _extensionMethods = new List <MethodDef>(); }
/// <summary> /// Obtains all of the <see cref="TypeRef"/>s that extend this TypeDef. /// </summary> /// <returns>A collection of derived types.</returns> public List <TypeRef> GetExtendingTypes() { CodedIndex ciForThisType = new CodedIndex(_table, (uint)_index); return(Assembly.GetExtendindTypes(this, ciForThisType)); }
private void SerializeImport(BlobBuilder writer, UsedNamespaceOrType import) { if (import.TargetXmlNamespaceOpt != null) { Debug.Assert(import.TargetNamespaceOpt == null); Debug.Assert(import.TargetAssemblyOpt == null); Debug.Assert(import.TargetTypeOpt == null); // <import> ::= ImportXmlNamespace <alias> <target-namespace> writer.WriteByte((byte)ImportDefinitionKind.ImportXmlNamespace); writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt))); writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.TargetXmlNamespaceOpt))); } else if (import.TargetTypeOpt != null) { Debug.Assert(import.TargetNamespaceOpt == null); Debug.Assert(import.TargetAssemblyOpt == null); if (import.AliasOpt != null) { // <import> ::= AliasType <alias> <target-type> writer.WriteByte((byte)ImportDefinitionKind.AliasType); writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt))); } else { // <import> ::= ImportType <target-type> writer.WriteByte((byte)ImportDefinitionKind.ImportType); } writer.WriteCompressedInteger(CodedIndex.TypeDefOrRefOrSpec(GetTypeHandle(import.TargetTypeOpt))); // TODO: index in release build } else if (import.TargetNamespaceOpt != null) { if (import.TargetAssemblyOpt != null) { if (import.AliasOpt != null) { // <import> ::= AliasAssemblyNamespace <alias> <target-assembly> <target-namespace> writer.WriteByte((byte)ImportDefinitionKind.AliasAssemblyNamespace); writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt))); } else { // <import> ::= ImportAssemblyNamespace <target-assembly> <target-namespace> writer.WriteByte((byte)ImportDefinitionKind.ImportAssemblyNamespace); } writer.WriteCompressedInteger(MetadataTokens.GetRowNumber(GetAssemblyReferenceHandle(import.TargetAssemblyOpt))); } else { if (import.AliasOpt != null) { // <import> ::= AliasNamespace <alias> <target-namespace> writer.WriteByte((byte)ImportDefinitionKind.AliasNamespace); writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt))); } else { // <import> ::= ImportNamespace <target-namespace> writer.WriteByte((byte)ImportDefinitionKind.ImportNamespace); } } // TODO: cache? string namespaceName = TypeNameSerializer.BuildQualifiedNamespaceName(import.TargetNamespaceOpt); writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(namespaceName))); } else { // <import> ::= ImportReferenceAlias <alias> Debug.Assert(import.AliasOpt != null); Debug.Assert(import.TargetAssemblyOpt == null); writer.WriteByte((byte)ImportDefinitionKind.ImportAssemblyReferenceAlias); writer.WriteCompressedInteger(MetadataTokens.GetHeapOffset(_debugMetadataOpt.GetOrAddBlobUTF8(import.AliasOpt))); } }
private void CreateType(Type type, List <DelayedWrite> genericParams) { // Check reserved and not already emitted if (!_metadata.TryGetTypeDefinition(type, out var metadata)) { ThrowMetadataIsNotReserved("Type", type); } EnsureMetadataWasNotEmitted(metadata, type); // Add the type definition var baseTypeHandle = type.BaseType != null?_metadata.GetTypeHandle(type.BaseType) : default; var handle = _metadata.Builder.AddTypeDefinition( type.Attributes, type.DeclaringType == null ? _metadata.GetOrAddString(ApplyNameChange(type.Namespace)) : default(StringHandle), _metadata.GetOrAddString(type.Name), baseTypeHandle, MetadataTokens.FieldDefinitionHandle(_metadata.Builder.GetRowCount(TableIndex.Field) + 1), MetadataTokens.MethodDefinitionHandle(_metadata.Builder.GetRowCount(TableIndex.MethodDef) + 1)); // Verify and mark emitted VerifyEmittedHandle(metadata, handle); metadata.MarkAsEmitted(); // Setup pack and size attributes (if explicit layout) if (type.IsExplicitLayout) { _metadata.Builder.AddTypeLayout( handle, (ushort)type.StructLayoutAttribute.Pack, (uint)type.StructLayoutAttribute.Size ); } // Add implemented interfaces (not for enums though - eg: IComparable etc...) if (!type.IsEnum) { DeclareInterfacesAndCreateInterfaceMap(type, handle); } // Setup enclosing type if (type.DeclaringType != null) { _metadata.Builder.AddNestedType(handle, (TypeDefinitionHandle)_metadata.GetTypeHandle(type.DeclaringType)); } // Create attributes CreateCustomAttributes(handle, type.GetCustomAttributesData()); // Handle generics type if (type.IsGenericType) { if (type.IsGenericTypeDefinition) { var genericType = type.GetGenericTypeDefinition(); var typeInfo = genericType.GetTypeInfo(); int index = 0; foreach (var arg in typeInfo.GenericTypeParameters) { var attr = arg.GenericParameterAttributes; genericParams.Add(new DelayedWrite(CodedIndex.TypeOrMethodDef(handle), () => { var gpHandle = _metadata.Builder.AddGenericParameter(handle, attr, _metadata.GetOrAddString(arg.Name), index++); foreach (var constraint in arg.GetGenericParameterConstraints()) { _metadata.Builder.AddGenericParameterConstraint(gpHandle, _metadata.GetTypeHandle(constraint)); } })); } } } // Create members... CreateFields(type.GetFields(AllFields)); CreatePropertiesForType(type.GetProperties(AllProperties)); CreateEventsForType(type.GetEvents(AllEvents)); CreateConstructors(type.GetConstructors(AllMethods)); CreateMethods(type.GetMethods(AllMethods), genericParams); }
public int GetCodedIndexTable(CodedIndex index, ref uint val) { int bits = 0; switch (index) { case CodedIndex.HasConstant: case CodedIndex.TypeDefOrRef: case CodedIndex.HasDeclSecurity: case CodedIndex.Implementation: case CodedIndex.ResolutionScope: bits = 2; break; case CodedIndex.HasCustomAttribute: bits = 5; break; case CodedIndex.HasFieldMarshall: case CodedIndex.HasSemantics: case CodedIndex.MethodDefOrRef: case CodedIndex.MemberForwarded: bits = 1; break; case CodedIndex.MemberRefParent: case CodedIndex.CustomAttributeType: bits = 3; break; } uint origval = val; val = origval >> bits; return (int)(origval & ((int)Math.Pow(2, bits) - 1)); }
public void Read(ClrModuleReader reader) { this.Class = reader.ReadTableIndex(TableKind.TypeDef); this.MethodBody = reader.ReadCodedIndex <MethodDefOrRef>(); this.MethodDeclaration = reader.ReadCodedIndex <MethodDefOrRef>(); }
protected uint ReadCodedIndex(CodedIndex codedIndex) { uint number = 0; int bits = 0; switch (codedIndex) { case CodedIndex.TypeDefOrRef: number = metadataTable.GetMaxRowCount(TypeDef.TABLE_ID, TypeRef.TABLE_ID, TypeSpec.TABLE_ID); bits = 2; break; case CodedIndex.HasConstant: number = metadataTable.GetMaxRowCount(Field.TABLE_ID, Param.TABLE_ID, Property.TABLE_ID); bits = 2; break; case CodedIndex.HasCustomAttribute: number = metadataTable.GetMaxRowCount(Method.TABLE_ID, Field.TABLE_ID, TypeRef.TABLE_ID, TypeDef.TABLE_ID, Param.TABLE_ID, InterfaceImpl.TABLE_ID, MemberRef.TABLE_ID, Module.TABLE_ID, DeclSecurity.TABLE_ID, Property.TABLE_ID, Event.TABLE_ID, StandAloneSig.TABLE_ID, ModuleRef.TABLE_ID, TypeSpec.TABLE_ID, Assembly.TABLE_ID, AssemblyRef.TABLE_ID, File.TABLE_ID, ExportedType.TABLE_ID, ManifestResource.TABLE_ID); bits = 5; break; case CodedIndex.HasFieldMarshall: number = metadataTable.GetMaxRowCount(Field.TABLE_ID, Param.TABLE_ID); bits = 1; break; case CodedIndex.HasDeclSecurity: number = metadataTable.GetMaxRowCount(TypeDef.TABLE_ID, Method.TABLE_ID, Assembly.TABLE_ID); bits = 2; break; case CodedIndex.MemberRefParent: number = metadataTable.GetMaxRowCount(TypeDef.TABLE_ID, TypeRef.TABLE_ID, ModuleRef.TABLE_ID, Method.TABLE_ID, TypeSpec.TABLE_ID); bits = 3; break; case CodedIndex.HasSemantics: number = metadataTable.GetMaxRowCount(Event.TABLE_ID, Property.TABLE_ID); bits = 1; break; case CodedIndex.MethodDefOrRef: number = metadataTable.GetMaxRowCount(Method.TABLE_ID, MemberRef.TABLE_ID); bits = 1; break; case CodedIndex.MemberForwarded: number = metadataTable.GetMaxRowCount(Field.TABLE_ID, Method.TABLE_ID); bits = 1; break; case CodedIndex.Implementation: number = metadataTable.GetMaxRowCount(File.TABLE_ID, AssemblyRef.TABLE_ID, ExportedType.TABLE_ID); bits = 2; break; case CodedIndex.CustomAttributeType: //number = metadataTable.GetMaxRowCount(TypeRef.TABLE_ID, TypeDef.TABLE_ID, Method.TABLE_ID, MemberRef.TABLE_ID/* TODO : , String ? */); number = metadataTable.GetMaxRowCount(Method.TABLE_ID, MemberRef.TABLE_ID); bits = 3; break; case CodedIndex.ResolutionScope: number = metadataTable.GetMaxRowCount(Module.TABLE_ID, ModuleRef.TABLE_ID, AssemblyRef.TABLE_ID, TypeRef.TABLE_ID); bits = 2; break; } if (number > 1 << (16 - bits)) { return binaryReader.ReadUInt32(); } return binaryReader.ReadUInt16(); }
private void EncodeType(BlobBuilder blobBuilder, TypeDesc type, EmbeddedSignatureDataEmitter signatureDataEmitter) { signatureDataEmitter.Push(); signatureDataEmitter.Push(); signatureDataEmitter.EmitAtCurrentIndexStack(blobBuilder); signatureDataEmitter.Pop(); signatureDataEmitter.Push(); if (type.IsPrimitive) { SignatureTypeCode primitiveCode; switch (type.Category) { case TypeFlags.Void: primitiveCode = SignatureTypeCode.Void; break; case TypeFlags.Boolean: primitiveCode = SignatureTypeCode.Boolean; break; case TypeFlags.Char: primitiveCode = SignatureTypeCode.Char; break; case TypeFlags.SByte: primitiveCode = SignatureTypeCode.SByte; break; case TypeFlags.Byte: primitiveCode = SignatureTypeCode.Byte; break; case TypeFlags.Int16: primitiveCode = SignatureTypeCode.Int16; break; case TypeFlags.UInt16: primitiveCode = SignatureTypeCode.UInt16; break; case TypeFlags.Int32: primitiveCode = SignatureTypeCode.Int32; break; case TypeFlags.UInt32: primitiveCode = SignatureTypeCode.UInt32; break; case TypeFlags.Int64: primitiveCode = SignatureTypeCode.Int64; break; case TypeFlags.UInt64: primitiveCode = SignatureTypeCode.UInt64; break; case TypeFlags.IntPtr: primitiveCode = SignatureTypeCode.IntPtr; break; case TypeFlags.UIntPtr: primitiveCode = SignatureTypeCode.UIntPtr; break; case TypeFlags.Single: primitiveCode = SignatureTypeCode.Single; break; case TypeFlags.Double: primitiveCode = SignatureTypeCode.Double; break; default: throw new Exception("Unknown primitive type"); } blobBuilder.WriteByte((byte)primitiveCode); } else if (type.IsSzArray) { blobBuilder.WriteByte((byte)SignatureTypeCode.SZArray); EncodeType(blobBuilder, type.GetParameterType(), signatureDataEmitter); } else if (type.IsArray) { var arrayType = (ArrayType)type; blobBuilder.WriteByte((byte)SignatureTypeCode.Array); EncodeType(blobBuilder, type.GetParameterType(), signatureDataEmitter); var shapeEncoder = new ArrayShapeEncoder(blobBuilder); // TODO Add support for non-standard array shapes shapeEncoder.Shape(arrayType.Rank, default(ImmutableArray <int>), default(ImmutableArray <int>)); } else if (type.IsPointer) { blobBuilder.WriteByte((byte)SignatureTypeCode.Pointer); EncodeType(blobBuilder, type.GetParameterType(), signatureDataEmitter); } else if (type.IsFunctionPointer) { FunctionPointerType fnptrType = (FunctionPointerType)type; EncodeMethodSignature(blobBuilder, fnptrType.Signature, signatureDataEmitter); } else if (type.IsByRef) { blobBuilder.WriteByte((byte)SignatureTypeCode.ByReference); EncodeType(blobBuilder, type.GetParameterType(), signatureDataEmitter); } else if (type.IsObject) { blobBuilder.WriteByte((byte)SignatureTypeCode.Object); } else if (type.IsString) { blobBuilder.WriteByte((byte)SignatureTypeCode.String); } else if (type.IsWellKnownType(WellKnownType.TypedReference)) { blobBuilder.WriteByte((byte)SignatureTypeCode.TypedReference); } else if (type.IsWellKnownType(WellKnownType.Void)) { blobBuilder.WriteByte((byte)SignatureTypeCode.Void); } else if (type is SignatureVariable) { SignatureVariable sigVar = (SignatureVariable)type; SignatureTypeCode code = sigVar.IsMethodSignatureVariable ? SignatureTypeCode.GenericMethodParameter : SignatureTypeCode.GenericTypeParameter; blobBuilder.WriteByte((byte)code); blobBuilder.WriteCompressedInteger(sigVar.Index); } else if (type is InstantiatedType) { blobBuilder.WriteByte((byte)SignatureTypeCode.GenericTypeInstance); EncodeType(blobBuilder, type.GetTypeDefinition(), signatureDataEmitter); blobBuilder.WriteCompressedInteger(type.Instantiation.Length); foreach (var instantiationArg in type.Instantiation) { EncodeType(blobBuilder, instantiationArg, signatureDataEmitter); } } else if (type is MetadataType) { var metadataType = (MetadataType)type; // Must be class or valuetype blobBuilder.WriteByte(type.IsValueType ? (byte)SignatureTypeKind.ValueType : (byte)SignatureTypeKind.Class); int codedIndex = CodedIndex.TypeDefOrRef(GetTypeRef(metadataType)); blobBuilder.WriteCompressedInteger(codedIndex); } else { throw new Exception("Unexpected type"); } signatureDataEmitter.Pop(); signatureDataEmitter.Pop(); }
public void Read(ClrModuleReader reader) { this.Class = reader.ReadTableIndex(TableKind.TypeDef); this.MethodBody = reader.ReadCodedIndex<MethodDefOrRef>(); this.MethodDeclaration = reader.ReadCodedIndex<MethodDefOrRef>(); }
int GetCodedIndexSize(CodedIndex ci) { return Utilities.GetCodedIndexSize (ci, new Utilities.TableRowCounter (m_mtrv.GetNumberOfRows), m_ciCache); }
int GetCodedIndexSize(CodedIndex index) { return image.GetCodedIndexSize (index); }
void WriteMetadataToken (MetadataToken token, CodedIndex ci) { WriteByIndexSize (Utilities.CompressMetadataToken (ci, token), Utilities.GetCodedIndexSize (ci, m_counter, m_ciCache)); }
public static uint CompressMetadataToken (CodedIndex cidx, MetadataToken token) { uint ret = 0; if (token.RID == 0) return ret; switch (cidx) { case CodedIndex.TypeDefOrRef : ret = token.RID << 2; switch (token.TokenType) { case TokenType.TypeDef : return ret | 0; case TokenType.TypeRef : return ret | 1; case TokenType.TypeSpec : return ret | 2; default : throw new MetadataFormatException("Non valid Token for TypeDefOrRef"); } case CodedIndex.HasConstant : ret = token.RID << 2; switch (token.TokenType) { case TokenType.Field : return ret | 0; case TokenType.Param : return ret | 1; case TokenType.Property : return ret | 2; default : throw new MetadataFormatException("Non valid Token for HasConstant"); } case CodedIndex.HasCustomAttribute : ret = token.RID << 5; switch (token.TokenType) { case TokenType.Method : return ret | 0; case TokenType.Field : return ret | 1; case TokenType.TypeRef : return ret | 2; case TokenType.TypeDef : return ret | 3; case TokenType.Param : return ret | 4; case TokenType.InterfaceImpl : return ret | 5; case TokenType.MemberRef : return ret | 6; case TokenType.Module : return ret | 7; case TokenType.Permission : return ret | 8; case TokenType.Property : return ret | 9; case TokenType.Event : return ret | 10; case TokenType.Signature : return ret | 11; case TokenType.ModuleRef : return ret | 12; case TokenType.TypeSpec : return ret | 13; case TokenType.Assembly : return ret | 14; case TokenType.AssemblyRef : return ret | 15; case TokenType.File : return ret | 16; case TokenType.ExportedType : return ret | 17; case TokenType.ManifestResource : return ret | 18; case TokenType.GenericParam : return ret | 19; default : throw new MetadataFormatException("Non valid Token for HasCustomAttribute"); } case CodedIndex.HasFieldMarshal : ret = token.RID << 1; switch (token.TokenType) { case TokenType.Field : return ret | 0; case TokenType.Param : return ret | 1; default : throw new MetadataFormatException("Non valid Token for HasFieldMarshal"); } case CodedIndex.HasDeclSecurity : ret = token.RID << 2; switch (token.TokenType) { case TokenType.TypeDef : return ret | 0; case TokenType.Method : return ret | 1; case TokenType.Assembly : return ret | 2; default : throw new MetadataFormatException("Non valid Token for HasDeclSecurity"); } case CodedIndex.MemberRefParent : ret = token.RID << 3; switch (token.TokenType) { case TokenType.TypeDef : return ret | 0; case TokenType.TypeRef : return ret | 1; case TokenType.ModuleRef : return ret | 2; case TokenType.Method : return ret | 3; case TokenType.TypeSpec : return ret | 4; default : throw new MetadataFormatException("Non valid Token for MemberRefParent"); } case CodedIndex.HasSemantics : ret = token.RID << 1; switch (token.TokenType) { case TokenType.Event : return ret | 0; case TokenType.Property : return ret | 1; default : throw new MetadataFormatException("Non valid Token for HasSemantics"); } case CodedIndex.MethodDefOrRef : ret = token.RID << 1; switch (token.TokenType) { case TokenType.Method : return ret | 0; case TokenType.MemberRef : return ret | 1; default : throw new MetadataFormatException("Non valid Token for MethodDefOrRef"); } case CodedIndex.MemberForwarded : ret = token.RID << 1; switch (token.TokenType) { case TokenType.Field : return ret | 0; case TokenType.Method : return ret | 1; default : throw new MetadataFormatException("Non valid Token for MemberForwarded"); } case CodedIndex.Implementation : ret = token.RID << 2; switch (token.TokenType) { case TokenType.File : return ret | 0; case TokenType.AssemblyRef : return ret | 1; case TokenType.ExportedType : return ret | 2; default : throw new MetadataFormatException("Non valid Token for Implementation"); } case CodedIndex.CustomAttributeType : ret = token.RID << 3; switch (token.TokenType) { case TokenType.Method : return ret | 2; case TokenType.MemberRef : return ret | 3; default : throw new MetadataFormatException("Non valid Token for CustomAttributeType"); } case CodedIndex.ResolutionScope : ret = token.RID << 2; switch (token.TokenType) { case TokenType.Module : return ret | 0; case TokenType.ModuleRef : return ret | 1; case TokenType.AssemblyRef : return ret | 2; case TokenType.TypeRef : return ret | 3; default : throw new MetadataFormatException("Non valid Token for ResolutionScope"); } case CodedIndex.TypeOrMethodDef : ret = token.RID << 1; switch (token.TokenType) { case TokenType.TypeDef : return ret | 0; case TokenType.Method : return ret | 1; default : throw new MetadataFormatException("Non valid Token for TypeOrMethodDef"); } default : throw new MetadataFormatException ("Non valid CodedIndex"); } }
/// <summary> /// Gets the index encoder for a specific type of coded index. /// </summary> /// <param name="index">The index type to get the encoder for.</param> /// <returns></returns> public IndexEncoder GetIndexEncoder(CodedIndex index) { return(_encoders[(int)index]); }
public static MetadataToken GetMetadataToken (CodedIndex cidx, uint data) { uint rid = 0; switch (cidx) { case CodedIndex.TypeDefOrRef : rid = data >> 2; switch (data & 3) { case 0 : return new MetadataToken (TokenType.TypeDef, rid); case 1 : return new MetadataToken (TokenType.TypeRef, rid); case 2 : return new MetadataToken (TokenType.TypeSpec, rid); default : return MetadataToken.Zero; } case CodedIndex.HasConstant : rid = data >> 2; switch (data & 3) { case 0 : return new MetadataToken (TokenType.Field, rid); case 1 : return new MetadataToken (TokenType.Param, rid); case 2 : return new MetadataToken (TokenType.Property, rid); default : return MetadataToken.Zero; } case CodedIndex.HasCustomAttribute : rid = data >> 5; switch (data & 31) { case 0 : return new MetadataToken (TokenType.Method, rid); case 1 : return new MetadataToken (TokenType.Field, rid); case 2 : return new MetadataToken (TokenType.TypeRef, rid); case 3 : return new MetadataToken (TokenType.TypeDef, rid); case 4 : return new MetadataToken (TokenType.Param, rid); case 5 : return new MetadataToken (TokenType.InterfaceImpl, rid); case 6 : return new MetadataToken (TokenType.MemberRef, rid); case 7 : return new MetadataToken (TokenType.Module, rid); case 8 : return new MetadataToken (TokenType.Permission, rid); case 9 : return new MetadataToken (TokenType.Property, rid); case 10 : return new MetadataToken (TokenType.Event, rid); case 11 : return new MetadataToken (TokenType.Signature, rid); case 12 : return new MetadataToken (TokenType.ModuleRef, rid); case 13 : return new MetadataToken (TokenType.TypeSpec, rid); case 14 : return new MetadataToken (TokenType.Assembly, rid); case 15 : return new MetadataToken (TokenType.AssemblyRef, rid); case 16 : return new MetadataToken (TokenType.File, rid); case 17 : return new MetadataToken (TokenType.ExportedType, rid); case 18 : return new MetadataToken (TokenType.ManifestResource, rid); case 19 : return new MetadataToken (TokenType.GenericParam, rid); default : return MetadataToken.Zero; } case CodedIndex.HasFieldMarshal : rid = data >> 1; switch (data & 1) { case 0 : return new MetadataToken (TokenType.Field, rid); case 1 : return new MetadataToken (TokenType.Param, rid); default : return MetadataToken.Zero; } case CodedIndex.HasDeclSecurity : rid = data >> 2; switch (data & 3) { case 0 : return new MetadataToken (TokenType.TypeDef, rid); case 1 : return new MetadataToken (TokenType.Method, rid); case 2 : return new MetadataToken (TokenType.Assembly, rid); default : return MetadataToken.Zero; } case CodedIndex.MemberRefParent : rid = data >> 3; switch (data & 7) { case 0 : return new MetadataToken (TokenType.TypeDef, rid); case 1 : return new MetadataToken (TokenType.TypeRef, rid); case 2 : return new MetadataToken (TokenType.ModuleRef, rid); case 3 : return new MetadataToken (TokenType.Method, rid); case 4 : return new MetadataToken (TokenType.TypeSpec, rid); default : return MetadataToken.Zero; } case CodedIndex.HasSemantics : rid = data >> 1; switch (data & 1) { case 0 : return new MetadataToken (TokenType.Event, rid); case 1 : return new MetadataToken (TokenType.Property, rid); default : return MetadataToken.Zero; } case CodedIndex.MethodDefOrRef : rid = data >> 1; switch (data & 1) { case 0 : return new MetadataToken (TokenType.Method, rid); case 1 : return new MetadataToken (TokenType.MemberRef, rid); default : return MetadataToken.Zero; } case CodedIndex.MemberForwarded : rid = data >> 1; switch (data & 1) { case 0 : return new MetadataToken (TokenType.Field, rid); case 1 : return new MetadataToken (TokenType.Method, rid); default : return MetadataToken.Zero; } case CodedIndex.Implementation : rid = data >> 2; switch (data & 3) { case 0 : return new MetadataToken (TokenType.File, rid); case 1 : return new MetadataToken (TokenType.AssemblyRef, rid); case 2 : return new MetadataToken (TokenType.ExportedType, rid); default : return MetadataToken.Zero; } case CodedIndex.CustomAttributeType : rid = data >> 3; switch (data & 7) { case 2 : return new MetadataToken (TokenType.Method, rid); case 3 : return new MetadataToken (TokenType.MemberRef, rid); default : return MetadataToken.Zero; } case CodedIndex.ResolutionScope : rid = data >> 2; switch (data & 3) { case 0 : return new MetadataToken (TokenType.Module, rid); case 1 : return new MetadataToken (TokenType.ModuleRef, rid); case 2 : return new MetadataToken (TokenType.AssemblyRef, rid); case 3 : return new MetadataToken (TokenType.TypeRef, rid); default : return MetadataToken.Zero; } case CodedIndex.TypeOrMethodDef : rid = data >> 1; switch (data & 1) { case 0 : return new MetadataToken (TokenType.TypeDef, rid); case 1 : return new MetadataToken (TokenType.Method, rid); default : return MetadataToken.Zero; } default : return MetadataToken.Zero; } }
int GetCodedIndexSize (CodedIndex ci) { return Utilities.GetCodedIndexSize (ci, m_counter, m_ciCache); }