Esempio n. 1
0
 public TypeResolver(GenericInstanceType typeDefinitionContext, GenericInstanceMethod methodDefinitionContext)
 {
     _typeDefinitionContext   = typeDefinitionContext;
     _methodDefinitionContext = methodDefinitionContext;
 }
Esempio n. 2
0
 public TypeResolver(GenericInstanceType typeDefinitionContext)
 {
     _typeDefinitionContext = typeDefinitionContext;
 }
        private static TypeDefinitionTreatment GenerateRedirectionInformation(TypeDefinition type, out Collection <MethodDefinition> redirectedMethods, out Collection <KeyValuePair <InterfaceImplementation, InterfaceImplementation> > redirectedInterfaces)
        {
            bool implementsProjectedInterface = false;

            redirectedMethods    = null;
            redirectedInterfaces = null;

            foreach (var implementedInterface in type.Interfaces)
            {
                if (IsRedirectedType(implementedInterface.InterfaceType))
                {
                    implementsProjectedInterface = true;
                    break;
                }
            }

            if (!implementsProjectedInterface)
            {
                return(TypeDefinitionTreatment.NormalType);
            }

            var allImplementedInterfaces = new HashSet <TypeReference> (new TypeReferenceEqualityComparer());

            redirectedMethods    = new Collection <MethodDefinition> ();
            redirectedInterfaces = new Collection <KeyValuePair <InterfaceImplementation, InterfaceImplementation> > ();

            foreach (var @interface in type.Interfaces)
            {
                var interfaceType = @interface.InterfaceType;

                if (IsRedirectedType(interfaceType))
                {
                    allImplementedInterfaces.Add(interfaceType);
                    CollectImplementedInterfaces(interfaceType, allImplementedInterfaces);
                }
            }

            foreach (var implementedInterface in type.Interfaces)
            {
                var interfaceType = implementedInterface.InterfaceType;
                if (IsRedirectedType(implementedInterface.InterfaceType))
                {
                    var etype           = interfaceType.GetElementType();
                    var unprojectedType = new TypeReference(etype.Namespace, etype.Name, etype.Module, etype.Scope)
                    {
                        DeclaringType = etype.DeclaringType,
                        projection    = etype.projection
                    };

                    RemoveProjection(unprojectedType);

                    var genericInstanceType = interfaceType as GenericInstanceType;
                    if (genericInstanceType != null)
                    {
                        var genericUnprojectedType = new GenericInstanceType(unprojectedType);
                        foreach (var genericArgument in genericInstanceType.GenericArguments)
                        {
                            genericUnprojectedType.GenericArguments.Add(genericArgument);
                        }

                        unprojectedType = genericUnprojectedType;
                    }

                    var unprojectedInterface = new InterfaceImplementation(unprojectedType);
                    redirectedInterfaces.Add(new KeyValuePair <InterfaceImplementation, InterfaceImplementation> (implementedInterface, unprojectedInterface));
                }
            }

            // Interfaces don't inherit methods of the interfaces they implement
            if (!type.IsInterface)
            {
                foreach (var implementedInterface in allImplementedInterfaces)
                {
                    RedirectInterfaceMethods(implementedInterface, redirectedMethods);
                }
            }

            return(TypeDefinitionTreatment.RedirectImplementedMethods);
        }
Esempio n. 4
0
        public TypeReference Resolve(TypeReference typeReference, bool includeTypeDefinitions)
        {
            if (IsDummy())
            {
                return(typeReference);
            }

            if (_typeDefinitionContext != null && _typeDefinitionContext.GenericArguments.Contains(typeReference))
            {
                return(typeReference);
            }
            if (_methodDefinitionContext != null && _methodDefinitionContext.GenericArguments.Contains(typeReference))
            {
                return(typeReference);
            }

            var genericParameter = typeReference as GenericParameter;

            if (genericParameter != null)
            {
                if (_typeDefinitionContext != null && _typeDefinitionContext.GenericArguments.Contains(genericParameter))
                {
                    return(genericParameter);
                }
                if (_methodDefinitionContext != null && _methodDefinitionContext.GenericArguments.Contains(genericParameter))
                {
                    return(genericParameter);
                }
                return(ResolveGenericParameter(genericParameter));
            }

            var arrayType = typeReference as ArrayType;

            if (arrayType != null)
            {
                return(new ArrayType(Resolve(arrayType.ElementType), arrayType.Rank));
            }

            var pointerType = typeReference as PointerType;

            if (pointerType != null)
            {
                return(new PointerType(Resolve(pointerType.ElementType)));
            }

            var byReferenceType = typeReference as ByReferenceType;

            if (byReferenceType != null)
            {
                return(new ByReferenceType(Resolve(byReferenceType.ElementType)));
            }

            var pinnedType = typeReference as PinnedType;

            if (pinnedType != null)
            {
                return(new PinnedType(Resolve(pinnedType.ElementType)));
            }

            var genericInstanceType = typeReference as GenericInstanceType;

            if (genericInstanceType != null)
            {
                var newGenericInstanceType = new GenericInstanceType(genericInstanceType.ElementType);
                foreach (var genericArgument in genericInstanceType.GenericArguments)
                {
                    newGenericInstanceType.GenericArguments.Add(Resolve(genericArgument));
                }
                return(newGenericInstanceType);
            }

            var requiredModType = typeReference as RequiredModifierType;

            if (requiredModType != null)
            {
                return(Resolve(requiredModType.ElementType, includeTypeDefinitions));
            }


            if (includeTypeDefinitions)
            {
                var typeDefinition = typeReference as TypeDefinition;
                if (typeDefinition != null && typeDefinition.HasGenericParameters)
                {
                    var newGenericInstanceType = new GenericInstanceType(typeDefinition);
                    foreach (var gp in typeDefinition.GenericParameters)
                    {
                        newGenericInstanceType.GenericArguments.Add(Resolve(gp));
                    }
                    return(newGenericInstanceType);
                }
            }

            if (typeReference is TypeSpecification)
            {
                throw new NotSupportedException(string.Format("The type {0} cannot be resolved correctly.", typeReference.FullName));
            }

            return(typeReference);
        }
Esempio n. 5
0
File: Import.cs Progetto: zwinter/ET
        TypeReference ImportTypeSpecification(TypeReference type, ImportGenericContext context)
        {
            switch (type.etype)
            {
            case ElementType.SzArray:
                var vector = (ArrayType)type;
                return(new ArrayType(ImportType(vector.ElementType, context)));

            case ElementType.Ptr:
                var pointer = (PointerType)type;
                return(new PointerType(ImportType(pointer.ElementType, context)));

            case ElementType.ByRef:
                var byref = (ByReferenceType)type;
                return(new ByReferenceType(ImportType(byref.ElementType, context)));

            case ElementType.Pinned:
                var pinned = (PinnedType)type;
                return(new PinnedType(ImportType(pinned.ElementType, context)));

            case ElementType.Sentinel:
                var sentinel = (SentinelType)type;
                return(new SentinelType(ImportType(sentinel.ElementType, context)));

            case ElementType.FnPtr:
                var fnptr          = (FunctionPointerType)type;
                var imported_fnptr = new FunctionPointerType()
                {
                    HasThis           = fnptr.HasThis,
                    ExplicitThis      = fnptr.ExplicitThis,
                    CallingConvention = fnptr.CallingConvention,
                    ReturnType        = ImportType(fnptr.ReturnType, context),
                };

                if (!fnptr.HasParameters)
                {
                    return(imported_fnptr);
                }

                for (int i = 0; i < fnptr.Parameters.Count; i++)
                {
                    imported_fnptr.Parameters.Add(new ParameterDefinition(
                                                      ImportType(fnptr.Parameters [i].ParameterType, context)));
                }

                return(imported_fnptr);

            case ElementType.CModOpt:
                var modopt = (OptionalModifierType)type;
                return(new OptionalModifierType(
                           ImportType(modopt.ModifierType, context),
                           ImportType(modopt.ElementType, context)));

            case ElementType.CModReqD:
                var modreq = (RequiredModifierType)type;
                return(new RequiredModifierType(
                           ImportType(modreq.ModifierType, context),
                           ImportType(modreq.ElementType, context)));

            case ElementType.Array:
                var array          = (ArrayType)type;
                var imported_array = new ArrayType(ImportType(array.ElementType, context));
                if (array.IsVector)
                {
                    return(imported_array);
                }

                var dimensions          = array.Dimensions;
                var imported_dimensions = imported_array.Dimensions;

                imported_dimensions.Clear();

                for (int i = 0; i < dimensions.Count; i++)
                {
                    var dimension = dimensions [i];

                    imported_dimensions.Add(new ArrayDimension(dimension.LowerBound, dimension.UpperBound));
                }

                return(imported_array);

            case ElementType.GenericInst:
                var instance           = (GenericInstanceType)type;
                var element_type       = ImportType(instance.ElementType, context);
                var arguments          = instance.GenericArguments;
                var imported_instance  = new GenericInstanceType(element_type, arguments.Count);
                var imported_arguments = imported_instance.GenericArguments;

                for (int i = 0; i < arguments.Count; i++)
                {
                    imported_arguments.Add(ImportType(arguments [i], context));
                }

                return(imported_instance);

            case ElementType.Var:
                var var_parameter = (GenericParameter)type;
                if (var_parameter.DeclaringType == null)
                {
                    throw new InvalidOperationException();
                }
                return(context.TypeParameter(var_parameter.DeclaringType.FullName, var_parameter.Position));

            case ElementType.MVar:
                var mvar_parameter = (GenericParameter)type;
                if (mvar_parameter.DeclaringMethod == null)
                {
                    throw new InvalidOperationException();
                }
                return(context.MethodParameter(context.NormalizeMethodName(mvar_parameter.DeclaringMethod), mvar_parameter.Position));
            }

            throw new NotSupportedException(type.etype.ToString());
        }