private void InitializeTypeDef(Cts.MetadataType entity, TypeDefinition record) { Debug.Assert(entity.IsTypeDefinition); Cts.MetadataType containingType = (Cts.MetadataType)entity.ContainingType; if (containingType != null) { var enclosingType = (TypeDefinition)HandleType(containingType); record.EnclosingType = enclosingType; enclosingType.NestedTypes.Add(record); var namespaceDefinition = HandleNamespaceDefinition(containingType.Module, entity.ContainingType.Namespace); record.NamespaceDefinition = namespaceDefinition; } else { var namespaceDefinition = HandleNamespaceDefinition(entity.Module, entity.Namespace); record.NamespaceDefinition = namespaceDefinition; if (entity.IsModuleType) { // These don't get added to the global namespace. // Instead, they have a dedicated field on the scope record. } else { namespaceDefinition.TypeDefinitions.Add(record); } } record.Name = HandleString(entity.Name); Cts.ClassLayoutMetadata layoutMetadata = entity.GetClassLayout(); record.Size = checked ((uint)layoutMetadata.Size); record.PackingSize = checked ((ushort)layoutMetadata.PackingSize); record.Flags = GetTypeAttributes(entity); try { if (entity.HasBaseType) { record.BaseType = HandleType(entity.BaseType); } } catch (Cts.TypeSystemException) when(HasNestedTypes(entity)) { // We might have been forced to generate metadata for a type // that wasn't looked at during code generation because it's an owning // type of a type we did look at. Allow those to generate incomplete // metadata. The ultimate fix is to rewrite metadata generation to be // System.Reflection.Metadata-based as opposed to type system based. // If there's no nested types, this is a bug and should tear down // the compiler at this point. } try { record.Interfaces.Capacity = entity.ExplicitlyImplementedInterfaces.Length; foreach (var interfaceType in entity.ExplicitlyImplementedInterfaces) { if (IsBlocked(interfaceType)) { continue; } record.Interfaces.Add(HandleType(interfaceType)); } } catch (Cts.TypeSystemException) when(HasNestedTypes(entity)) { // We might have been forced to generate metadata for a type // that wasn't looked at during code generation because it's an owning // type of a type we did look at. Allow those to generate incomplete // metadata. The ultimate fix is to rewrite metadata generation to be // System.Reflection.Metadata-based as opposed to type system based. // If there's no nested types, this is a bug and should tear down // the compiler at this point. } if (entity.HasInstantiation) { record.GenericParameters.Capacity = entity.Instantiation.Length; foreach (var p in entity.Instantiation) { record.GenericParameters.Add(HandleGenericParameter((Cts.GenericParameterDesc)p)); } } foreach (var field in entity.GetFields()) { if (_policy.GeneratesMetadata(field)) { record.Fields.Add(HandleFieldDefinition(field)); } } foreach (var method in entity.GetMethods()) { if (_policy.GeneratesMetadata(method)) { record.Methods.Add(HandleMethodDefinition(method)); } } var ecmaEntity = entity as Cts.Ecma.EcmaType; if (ecmaEntity != null) { Ecma.TypeDefinition ecmaRecord = ecmaEntity.MetadataReader.GetTypeDefinition(ecmaEntity.Handle); foreach (var e in ecmaRecord.GetEvents()) { Event evt = HandleEvent(ecmaEntity.EcmaModule, e); if (evt != null) { record.Events.Add(evt); } } foreach (var property in ecmaRecord.GetProperties()) { Property prop = HandleProperty(ecmaEntity.EcmaModule, property); if (prop != null) { record.Properties.Add(prop); } } Ecma.CustomAttributeHandleCollection customAttributes = ecmaRecord.GetCustomAttributes(); if (customAttributes.Count > 0) { record.CustomAttributes = HandleCustomAttributes(ecmaEntity.EcmaModule, customAttributes); } /* COMPLETENESS * foreach (var miHandle in ecmaRecord.GetMethodImplementations()) * { * Ecma.MetadataReader reader = ecmaEntity.EcmaModule.MetadataReader; * * Ecma.MethodImplementation miDef = reader.GetMethodImplementation(miHandle); * * Cts.MethodDesc methodBody = (Cts.MethodDesc)ecmaEntity.EcmaModule.GetObject(miDef.MethodBody); * if (_policy.IsBlocked(methodBody)) * continue; * * Cts.MethodDesc methodDecl = (Cts.MethodDesc)ecmaEntity.EcmaModule.GetObject(miDef.MethodDeclaration); * if (_policy.IsBlocked(methodDecl.GetTypicalMethodDefinition())) * continue; * * MethodImpl methodImplRecord = new MethodImpl * { * MethodBody = HandleQualifiedMethod(methodBody), * MethodDeclaration = HandleQualifiedMethod(methodDecl) * }; * * record.MethodImpls.Add(methodImplRecord); * }*/ }
private void InitializeTypeDef(Cts.MetadataType entity, TypeDefinition record) { Debug.Assert(entity.IsTypeDefinition); Cts.MetadataType containingType = (Cts.MetadataType)entity.ContainingType; if (containingType != null) { var enclosingType = (TypeDefinition)HandleType(containingType); record.EnclosingType = enclosingType; enclosingType.NestedTypes.Add(record); var namespaceDefinition = HandleNamespaceDefinition(containingType.Module, entity.ContainingType.Namespace); record.NamespaceDefinition = namespaceDefinition; } else { var namespaceDefinition = HandleNamespaceDefinition(entity.Module, entity.Namespace); record.NamespaceDefinition = namespaceDefinition; namespaceDefinition.TypeDefinitions.Add(record); } record.Name = HandleString(entity.Name); Cts.ClassLayoutMetadata layoutMetadata = entity.GetClassLayout(); record.Size = checked ((uint)layoutMetadata.Size); record.PackingSize = checked ((ushort)layoutMetadata.PackingSize); record.Flags = GetTypeAttributes(entity); if (entity.HasBaseType) { record.BaseType = HandleType(entity.BaseType); } record.Interfaces.Capacity = entity.ExplicitlyImplementedInterfaces.Length; foreach (var interfaceType in entity.ExplicitlyImplementedInterfaces) { if (IsBlocked(interfaceType)) { continue; } record.Interfaces.Add(HandleType(interfaceType)); } if (entity.HasInstantiation) { record.GenericParameters.Capacity = entity.Instantiation.Length; foreach (var p in entity.Instantiation) { record.GenericParameters.Add(HandleGenericParameter((Cts.GenericParameterDesc)p)); } } foreach (var field in entity.GetFields()) { if (_policy.GeneratesMetadata(field)) { record.Fields.Add(HandleFieldDefinition(field)); } } foreach (var method in entity.GetMethods()) { if (_policy.GeneratesMetadata(method)) { record.Methods.Add(HandleMethodDefinition(method)); } } var ecmaEntity = entity as Cts.Ecma.EcmaType; if (ecmaEntity != null) { Ecma.TypeDefinition ecmaRecord = ecmaEntity.MetadataReader.GetTypeDefinition(ecmaEntity.Handle); foreach (var e in ecmaRecord.GetEvents()) { Event evt = HandleEvent(ecmaEntity.EcmaModule, e); if (evt != null) { record.Events.Add(evt); } } foreach (var property in ecmaRecord.GetProperties()) { Property prop = HandleProperty(ecmaEntity.EcmaModule, property); if (prop != null) { record.Properties.Add(prop); } } Ecma.CustomAttributeHandleCollection customAttributes = ecmaRecord.GetCustomAttributes(); if (customAttributes.Count > 0) { record.CustomAttributes = HandleCustomAttributes(ecmaEntity.EcmaModule, customAttributes); } foreach (var miHandle in ecmaRecord.GetMethodImplementations()) { Ecma.MetadataReader reader = ecmaEntity.EcmaModule.MetadataReader; Ecma.MethodImplementation miDef = reader.GetMethodImplementation(miHandle); Cts.MethodDesc methodBody = (Cts.MethodDesc)ecmaEntity.EcmaModule.GetObject(miDef.MethodBody); if (_policy.IsBlocked(methodBody)) { continue; } Cts.MethodDesc methodDecl = (Cts.MethodDesc)ecmaEntity.EcmaModule.GetObject(miDef.MethodDeclaration); if (_policy.IsBlocked(methodDecl.GetTypicalMethodDefinition())) { continue; } MethodImpl methodImplRecord = new MethodImpl { MethodBody = HandleQualifiedMethod(methodBody), MethodDeclaration = HandleQualifiedMethod(methodDecl) }; record.MethodImpls.Add(methodImplRecord); } } }