Exemplo n.º 1
0
Arquivo: DC.cs Projeto: airbrush/CSD
        void AddType(TypeDefinition type, Action<ModelType, string> add)
        {
            MetadataReader reader = _reader.Value.Reader;

            ModelType modelType = new ModelType { Name = reader.GetString(type.Name) };
            add(modelType, reader.GetString(type.Namespace));

            foreach (var hmember in type.GetFields())
            {
                FieldDefinition member = reader.GetFieldDefinition(hmember);
                modelType.Members.Add(new ModelTypeMember
                {
                    Name = "[f] " + reader.GetString(member.Name)
                });
            }

            foreach (var hmember in type.GetMethods())
            {
                MethodDefinition member = reader.GetMethodDefinition(hmember);
                modelType.Members.Add(new ModelMethod
                {
                    Name = reader.GetString(member.Name) + "()",
                    Handle = hmember,
                    Type = type,
                });
            }

            foreach (var hchild in type.GetNestedTypes())
            {
                TypeDefinition child = reader.GetTypeDefinition(hchild);
                AddType(child, (m, typeNs) => modelType.Members.Add(m));
            }
        }
        internal void HandleStaticFieldInitializers(IEnumerable <AstNode> members)
        {
            // Translate static constructor into field initializers if the class is BeforeFieldInit
            var staticCtor = members.OfType <ConstructorDeclaration>().FirstOrDefault(c => (c.Modifiers & Modifiers.Static) == Modifiers.Static);

            if (staticCtor != null)
            {
                IMethod ctorMethod = staticCtor.GetSymbol() as IMethod;
                if (!ctorMethod.MetadataToken.IsNil)
                {
                    var metadata = context.TypeSystem.MainModule.PEFile.Metadata;
                    SRM.MethodDefinition ctorMethodDef = metadata.GetMethodDefinition((SRM.MethodDefinitionHandle)ctorMethod.MetadataToken);
                    SRM.TypeDefinition   declaringType = metadata.GetTypeDefinition(ctorMethodDef.GetDeclaringType());
                    if (declaringType.HasFlag(System.Reflection.TypeAttributes.BeforeFieldInit))
                    {
                        while (true)
                        {
                            ExpressionStatement es = staticCtor.Body.Statements.FirstOrDefault() as ExpressionStatement;
                            if (es == null)
                            {
                                break;
                            }
                            AssignmentExpression assignment = es.Expression as AssignmentExpression;
                            if (assignment == null || assignment.Operator != AssignmentOperatorType.Assign)
                            {
                                break;
                            }
                            IMember fieldOrProperty = (assignment.Left.GetSymbol() as IMember)?.MemberDefinition;
                            if (!(fieldOrProperty is IField || fieldOrProperty is IProperty) || !fieldOrProperty.IsStatic)
                            {
                                break;
                            }
                            AstNode fieldOrPropertyDecl = members.FirstOrDefault(f => f.GetSymbol() == fieldOrProperty);
                            if (fieldOrPropertyDecl == null)
                            {
                                break;
                            }
                            if (fieldOrPropertyDecl is FieldDeclaration fd)
                            {
                                fd.Variables.Single().Initializer = assignment.Right.Detach();
                            }
                            else if (fieldOrPropertyDecl is PropertyDeclaration pd)
                            {
                                pd.Initializer = assignment.Right.Detach();
                            }
                            else
                            {
                                break;
                            }
                            es.Remove();
                        }
                        if (staticCtor.Body.Statements.Count == 0)
                        {
                            staticCtor.Remove();
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static string GetFullTypeName(this MetadataReader metadataReader, TypeDefinition typeDefinition)
        {
            var name = metadataReader.GetString(typeDefinition.Namespace) ?? "";
            if (name != "")
            {
                name = name + ".";
            }

            name = name + metadataReader.GetString(typeDefinition.Name);
            return name;
        }
Exemplo n.º 4
0
        internal EcmaType(EcmaModule module, TypeDefinitionHandle handle)
        {
            _module = module;
            _handle = handle;

            _typeDefinition = module.MetadataReader.GetTypeDefinition(handle);

            _baseType = this; // Not yet initialized flag

#if DEBUG
            // Initialize name eagerly in debug builds for convenience
            this.ToString();
#endif
        }
Exemplo n.º 5
0
        private TypeAttributes GetTypeAttributes(Cts.MetadataType type)
        {
            TypeAttributes result;

            var ecmaType = type as Cts.Ecma.EcmaType;

            if (ecmaType != null)
            {
                Ecma.TypeDefinition ecmaRecord = ecmaType.MetadataReader.GetTypeDefinition(ecmaType.Handle);
                result = ecmaRecord.Attributes;
            }
            else
            {
                result = 0;

                if (type.IsExplicitLayout)
                {
                    result |= TypeAttributes.ExplicitLayout;
                }
                if (type.IsSequentialLayout)
                {
                    result |= TypeAttributes.SequentialLayout;
                }
                if (type.IsInterface)
                {
                    result |= TypeAttributes.Interface;
                }
                if (type.IsSealed)
                {
                    result |= TypeAttributes.Sealed;
                }
                if (type.IsBeforeFieldInit)
                {
                    result |= TypeAttributes.BeforeFieldInit;
                }

                // Not set: Abstract, Ansi/Unicode/Auto, HasSecurity, Import, visibility, Serializable,
                //          WindowsRuntime, HasSecurity, SpecialName, RTSpecialName
            }

            return(result);
        }
Exemplo n.º 6
0
        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);
                 * }*/
            }
            public static MetadataDefinition Create(
                MetadataReader reader, TypeDefinition definition)
            {
                var typeName = reader.GetString(definition.Name);
                var index = typeName.IndexOf('`');
                typeName = index > 0 ? typeName.Substring(0, index) : typeName;

                return new MetadataDefinition(
                    MetadataDefinitionKind.Type,
                    typeName)
                {
                    Type = definition
                };
            }
 public string GetFullyQualifiedName(TypeDefinition type)
 {
     return GetFullyQualifiedNameCore(type.Namespace, type.Name);
 }
Exemplo n.º 9
0
 private bool HasVisibleConstructors(MetadataReader metadataReader, TypeDefinition typeDefinition)
 {
     // for now, assume that the type has publicly-visible constructors.
     return true;
 }
        private static void LookupMetadataDefinitions(
            MetadataReader reader, TypeDefinition typeDefinition,
            OrderPreservingMultiDictionary<string, MetadataDefinition> definitionMap)
        {
            // Only bother looking for extension methods in static types.
            if ((typeDefinition.Attributes & TypeAttributes.Abstract) != 0 &&
                (typeDefinition.Attributes & TypeAttributes.Sealed) != 0)
            {
                foreach (var child in typeDefinition.GetMethods())
                {
                    var method = reader.GetMethodDefinition(child);
                    if ((method.Attributes & MethodAttributes.SpecialName) != 0 ||
                        (method.Attributes & MethodAttributes.RTSpecialName) != 0)
                    {
                        continue;
                    }

                    // SymbolTreeInfo is only searched for types and extension methods.
                    // So we don't want to pull in all methods here.  As a simple approximation
                    // we just pull in methods that have attributes on them.
                    if ((method.Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public &&
                        (method.Attributes & MethodAttributes.Static) != 0 &&
                        method.GetCustomAttributes().Count > 0)
                    {
                        var definition = new MetadataDefinition(
                            MetadataDefinitionKind.Member, reader.GetString(method.Name));

                        definitionMap.Add(definition.Name, definition);
                    }
                }
            }

            foreach (var child in typeDefinition.GetNestedTypes())
            {
                var type = reader.GetTypeDefinition(child);

                // We don't include internals from metadata assemblies.  It's less likely that
                // a project would have IVT to it and so it helps us save on memory.  It also
                // means we can avoid loading lots and lots of obfuscated code in the case the
                // dll was obfuscated.
                if (IsPublic(type.Attributes))
                {
                    var definition = MetadataDefinition.Create(reader, type);
                    definitionMap.Add(definition.Name, definition);
                }
            }
        }
Exemplo n.º 11
0
 private static bool TryGetAnonymousTypeKey(
     MetadataReader reader,
     TypeDefinition def,
     ArrayBuilder<string> builder)
 {
     foreach (var typeParameterHandle in def.GetGenericParameters())
     {
         var typeParameter = reader.GetGenericParameter(typeParameterHandle);
         string fieldName;
         if (!GeneratedNames.TryParseAnonymousTypeParameterName(reader.GetString(typeParameter.Name), out fieldName))
         {
             return false;
         }
         builder.Add(fieldName);
     }
     return true;
 }
Exemplo n.º 12
0
        private void InitializeTypeDef(Cts.MetadataType entity, TypeDefinition record)
        {
            if (entity.ContainingType != null)
            {
                var enclosingType = (TypeDefinition)HandleType(entity.ContainingType);
                record.EnclosingType = enclosingType;
                enclosingType.NestedTypes.Add(record);

                var namespaceDefinition =
                    HandleNamespaceDefinition(entity.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 ((uint)layoutMetadata.PackingSize);
            record.Flags       = GetTypeAttributes(entity);

            if (entity.HasBaseType)
            {
                record.BaseType = HandleType(entity.BaseType);
            }

            if (entity.ExplicitlyImplementedInterfaces.Length > 0)
            {
                record.Interfaces = entity.ExplicitlyImplementedInterfaces
                                    .Where(i => !IsBlocked(i))
                                    .Select(i => HandleType(i)).ToList();
            }

            if (entity.HasInstantiation)
            {
                var genericParams = new List <GenericParameter>(entity.Instantiation.Length);
                foreach (var p in entity.Instantiation)
                {
                    genericParams.Add(HandleGenericParameter((Cts.GenericParameterDesc)p));
                }
                record.GenericParameters = genericParams;
            }

            var fields = new List <Field>();

            foreach (var field in entity.GetFields())
            {
                if (_policy.GeneratesMetadata(field))
                {
                    fields.Add(HandleFieldDefinition(field));
                }
            }
            record.Fields = fields;

            var methods = new List <Method>();

            foreach (var method in entity.GetMethods())
            {
                if (_policy.GeneratesMetadata(method))
                {
                    methods.Add(HandleMethodDefinition(method));
                }
            }
            record.Methods = methods;

            var ecmaEntity = entity as Cts.Ecma.EcmaType;

            if (ecmaEntity != null)
            {
                Ecma.TypeDefinition ecmaRecord = ecmaEntity.MetadataReader.GetTypeDefinition(ecmaEntity.Handle);
                foreach (var property in ecmaRecord.GetProperties())
                {
                    Property prop = HandleProperty(ecmaEntity.EcmaModule, property);
                    if (prop != null)
                    {
                        record.Properties.Add(prop);
                    }
                }

                // TODO: Events

                // TODO: CustomAttributes
            }
        }
Exemplo n.º 13
0
            public static MetadataDefinition Create(
                MetadataReader reader, TypeDefinition definition)
            {
                var typeName = GetMetadataNameWithoutBackticks(reader, definition.Name);

                return new MetadataDefinition(MetadataDefinitionKind.Type,typeName)
                {
                    Type = definition
                };
            }
Exemplo n.º 14
0
        /// <summary>
        /// Determine if this type should be one of the `class` values passed to xunit.  This
        /// code doesn't actually resolve base types or trace through inherrited Fact attributes
        /// hence we have to error on the side of including types with no tests vs. excluding them.
        /// </summary>
        private static bool ShouldIncludeType(MetadataReader reader, TypeDefinition type, int testMethodCount)
        {
            // xunit only handles public, non-abstract classes
            var isPublic =
                TypeAttributes.Public == (type.Attributes & TypeAttributes.Public) ||
                TypeAttributes.NestedPublic == (type.Attributes & TypeAttributes.NestedPublic);
            if (!isPublic ||
                TypeAttributes.Abstract == (type.Attributes & TypeAttributes.Abstract)  ||
                TypeAttributes.Class != (type.Attributes & TypeAttributes.Class))
            {
                return false;
            }

            // Compiler generated types / methods have the shape of the heuristic that we are looking
            // at here.  Filter them out as well.
            if (!IsValidIdentifier(reader, type.Name))
            {
                return false;
            }

            if (testMethodCount > 0)
            {
                return true;
            }

            // The case we still have to consider at this point is a class with 0 defined methods, 
            // inheritting from a class with > 0 defined test methods.  That is a completely valid
            // xunit scenario.  For now we're just going to exclude types that inherit from object
            // because they clearly don't fit that category.
            return !(InheritsFromObject(reader, type) ?? false);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Get the full name of a Type.
        /// </summary>
        private static string GetTypeFullName(MetadataReader metadataReader, TypeDefinition typeDefinition)
        {
            string fullName;
            string typeName = metadataReader.GetString(typeDefinition.Name);
            string nsName = metadataReader.GetString(typeDefinition.Namespace);

            // Get the enclosing type if the type is nested
            TypeDefinitionHandle declaringTypeHandle = typeDefinition.GetDeclaringType();
            if (declaringTypeHandle.IsNil)
            {
                fullName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", nsName, typeName);
            }
            else
            {
                fullName = typeName;
                while (!declaringTypeHandle.IsNil)
                {
                    TypeDefinition declaringTypeDef = metadataReader.GetTypeDefinition(declaringTypeHandle);
                    declaringTypeHandle = declaringTypeDef.GetDeclaringType();
                    if (declaringTypeHandle.IsNil)
                    {
                        fullName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}+{2}",
                                                 metadataReader.GetString(declaringTypeDef.Namespace),
                                                 metadataReader.GetString(declaringTypeDef.Name),
                                                 fullName);
                    }
                    else
                    {
                        fullName = string.Format(CultureInfo.InvariantCulture, "{0}+{1}",
                                                 metadataReader.GetString(declaringTypeDef.Name),
                                                 fullName);
                    }
                }
            }

            return fullName;
        }
Exemplo n.º 16
0
 private static bool HasAttributes(System.Reflection.Metadata.TypeDefinition type, TypeAttributes attributes)
 {
     return((type.Attributes & attributes) == attributes);
 }
Exemplo n.º 17
0
 private string GetMetadataName(MetadataReader metadataReader, EventDefinition eventDefinition, TypeDefinition declaringTypeDefinition)
 {
     string typeName = GetMetadataName(metadataReader, declaringTypeDefinition);
     string eventName = metadataReader.GetString(eventDefinition.Name);
     return string.Format("{0}.{1}", typeName, eventName);
 }
Exemplo n.º 18
0
        private bool IsPubliclyVisible(MetadataReader metadataReader, TypeDefinition typeDefinition)
        {
            switch (typeDefinition.Attributes & TypeAttributes.VisibilityMask)
            {
            case TypeAttributes.Public:
                return true;

            case TypeAttributes.NestedPublic:
            case TypeAttributes.NestedFamORAssem:
            case TypeAttributes.NestedFamily:
                TypeDefinition declaringType = metadataReader.GetTypeDefinition(typeDefinition.GetDeclaringType());
                return IsPubliclyVisible(metadataReader, declaringType);

            case TypeAttributes.NestedFamANDAssem:
            case TypeAttributes.NestedPrivate:
            case TypeAttributes.NotPublic:
            default:
                return false;
            }
        }
Exemplo n.º 19
0
 private bool IsMarkedPreliminary(MetadataReader metadataReader, TypeDefinition typeDefinition)
 {
     return false;
 }
Exemplo n.º 20
0
 private string GetMetadataName(MetadataReader metadataReader, TypeDefinition typeDefinition)
 {
     if (typeDefinition.GetDeclaringType().IsNil)
     {
         string namespaceName = metadataReader.GetString(typeDefinition.Namespace);
         string typeName = metadataReader.GetString(typeDefinition.Name);
         return string.Format("{0}.{1}", namespaceName, typeName);
     }
     else
     {
         TypeDefinition declaringTypeDefinition = metadataReader.GetTypeDefinition(typeDefinition.GetDeclaringType());
         string declaringTypeName = GetMetadataName(metadataReader, declaringTypeDefinition);
         string name = metadataReader.GetString(typeDefinition.Name);
         return string.Format("{0}+{1}", declaringTypeName, name);
     }
 }
Exemplo n.º 21
0
        private static bool? InheritsFromObject(MetadataReader reader, TypeDefinition type)
        {
            if (type.BaseType.Kind != HandleKind.TypeReference)
            {
                return null;
            }

            var typeRef = reader.GetTypeReference((TypeReferenceHandle)type.BaseType);
            return 
                reader.GetString(typeRef.Namespace) == "System" && 
                reader.GetString(typeRef.Name) == "Object";
        }
Exemplo n.º 22
0
        void ParseEnum(TypeDefinition enumTypeDef)
        {
            // TODO: verify that int32 is the enums storage type for constant read below

            InspectorEnum ienum = new InspectorEnum ();

            ienum.Name = metaReader.GetString (enumTypeDef.Name);

            InspectorEnums [ienum.Name] = ienum;

            var fields = enumTypeDef.GetFields ();

            foreach (var fieldHandle in fields) {

                var inspectorField = new InspectorField ();

                var fieldDef = metaReader.GetFieldDefinition (fieldHandle);

                if ((fieldDef.Attributes & FieldAttributes.HasDefault) != 0) {

                    var constantHandle = fieldDef.GetDefaultValue ();
                    var constant = metaReader.GetConstant (constantHandle);

                    BlobReader constantReader = metaReader.GetBlobReader (constant.Value);

                    ienum.Values [metaReader.GetString (fieldDef.Name)] = constantReader.ReadInt32 ();

                }
            }

            return;
        }
Exemplo n.º 23
0
 /// <exception cref="BadImageFormatException">An exception from metadata reader.</exception>
 internal static bool IsPublicNonInterfaceType(this MetadataReader reader, TypeDefinition typeDef, string namespaceName, string typeName)
 {
     return (typeDef.Attributes & (TypeAttributes.Public | TypeAttributes.Interface)) == TypeAttributes.Public &&
         reader.StringComparer.Equals(typeDef.Name, typeName) &&
         reader.StringComparer.Equals(typeDef.Namespace, namespaceName);
 }
Exemplo n.º 24
0
 /// <exception cref="BadImageFormatException">An exception from metadata reader.</exception>
 private static bool IsSystemObjectOrThrow(this MetadataReader reader, TypeDefinition typeDef)
 {
     return reader.StringComparer.Equals(typeDef.Name, "Object") &&
         reader.StringComparer.Equals(typeDef.Namespace, "System");
 }
Exemplo n.º 25
0
        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);
                }
            }
        }
Exemplo n.º 26
0
        private void Walk(TypeDefinition typeDefinition)
        {
            var baseTypeDefinitionHandle = typeDefinition.BaseType;
            if (baseTypeDefinitionHandle.IsNil)
            {
                return;
            }

            var Kind = baseTypeDefinitionHandle.Kind;
            if (Kind == HandleKind.TypeDefinition)
            {
                typeDefinition = metadataReader.GetTypeDefinition((TypeDefinitionHandle)baseTypeDefinitionHandle);
                var typeFullName = metadataReader.GetFullTypeName(typeDefinition);
                if (knownExportAttributeOrDerivedType.Contains(typeFullName))
                {
                    typeFullName = metadataReader.GetFullTypeName(typeDefinition);
                    knownExportAttributeOrDerivedType.Add(typeFullName);
                    //isExportAttributeCache[baseTypeDefinitionHandle] = ExportOrInheritedExport.Export;
                    return;
                }
                else if (knownImportAttributeOrDerivedType.Contains(typeFullName))
                {
                    typeFullName = metadataReader.GetFullTypeName(typeDefinition);
                    knownImportAttributeOrDerivedType.Add(typeFullName);
                    //isImportAttributeCache[baseTypeDefinitionHandle] = true;
                    return;
                }

                Walk(typeDefinition);
            }
            else if (Kind == HandleKind.TypeDefinition)
            {
                TypeReference typeReference = metadataReader.GetTypeReference((TypeReferenceHandle)baseTypeDefinitionHandle);
                var typeFullName = metadataReader.GetFullTypeName(typeReference);
                if (knownExportAttributeOrDerivedType.Contains(typeFullName))
                {
                    typeFullName = metadataReader.GetFullTypeName(typeDefinition);
                    knownExportAttributeOrDerivedType.Add(typeFullName);
                    //isExportAttributeCache[baseTypeDefinitionHandle] = ExportOrInheritedExport.Export;
                    return;
                }
                else if (knownImportAttributeOrDerivedType.Contains(typeFullName))
                {
                    typeFullName = metadataReader.GetFullTypeName(typeDefinition);
                    knownImportAttributeOrDerivedType.Add(typeFullName);
                    //isImportAttributeCache[baseTypeDefinitionHandle] = true;
                    return;
                }
            }
            else if (Kind == HandleKind.TypeDefinition)
            {
                // TODO: not implemented
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Exemplo n.º 27
0
 private string GetMetadataName(MetadataReader metadataReader, PropertyDefinition propertyDefinition, TypeDefinition declaringTypeDefinition)
 {
     string typeName = GetMetadataName(metadataReader, declaringTypeDefinition);
     string propertyName = metadataReader.GetString(propertyDefinition.Name);
     return string.Format("{0}.{1}", typeName, propertyName);
 }
Exemplo n.º 28
0
        private static int GetMethodCount(MetadataReader reader, TypeDefinition type)
        {
            var count = 0;
            foreach (var handle in type.GetMethods())
            {
                var methodDefinition = reader.GetMethodDefinition(handle);
                if (methodDefinition.GetCustomAttributes().Count == 0 ||
                    !IsValidIdentifier(reader, methodDefinition.Name))
                {
                    continue;
                }

                if (MethodAttributes.Public != (methodDefinition.Attributes & MethodAttributes.Public))
                {
                    continue;
                }

                count++;
            }

            return count;
        }
 internal ImportedType(ImportedModule module, TypeDefinition typeDef)
     : base(module, typeDef.Name, null)
 {
     _typeDef = typeDef;
 }
Exemplo n.º 30
0
        private static string GetFullName(MetadataReader reader, TypeDefinition type)
        {
            var typeName = reader.GetString(type.Name);

            if (TypeAttributes.NestedPublic == (type.Attributes & TypeAttributes.NestedPublic))
            {
                // Need to take into account the containing type.
                var declaringType = reader.GetTypeDefinition(type.GetDeclaringType());
                var declaringTypeFullName = GetFullName(reader, declaringType);
                return $"{declaringTypeFullName}+{typeName}";
            }
            
            var namespaceName = reader.GetString(type.Namespace);
            if (string.IsNullOrEmpty(namespaceName))
            {
                return typeName;
            }

            return $"{namespaceName}.{typeName}";
        }
Exemplo n.º 31
0
 private static bool IsTheObjectClass(this MetadataReader reader, TypeDefinition typeDef)
 {
     return typeDef.BaseType.IsNil &&
         reader.IsPublicNonInterfaceType(typeDef, "System", "Object");
 }