Ejemplo n.º 1
0
        static void CreateInstantiatedSubtypes(InstantiatedType result, IType curType, IList <IReturnType> genericArguments, GenericTypeInstanceResolver resolver)
        {
            foreach (IType innerType in curType.InnerTypes)
            {
                List <IReturnType> newArguments             = new List <IReturnType> ();
                List <int>         removeInheritedArguments = new List <int> ();
                for (int i = 0; i < innerType.TypeParameters.Count; i++)
                {
                    ITypeParameter curParameter = innerType.TypeParameters [i];
                    bool           found        = false;
                    for (int j = curType.TypeParameters.Count - 1; j >= 0; j--)
                    {
                        if (curType.TypeParameters [j].Name == curParameter.Name)
                        {
                            removeInheritedArguments.Add(newArguments.Count);
                            newArguments.Add(genericArguments [j]);
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        newArguments.Add(new DomReturnType(curParameter.Name));
                    }
                }

                InstantiatedType innerInstantiatedType = (InstantiatedType)CreateInstantiatedGenericTypeInternal(innerType, newArguments, resolver);
                for (int i = 0, j = 0; i < innerInstantiatedType.TypeParameters.Count && j < innerInstantiatedType.TypeParameters.Count; i++, j++)
                {
                    if (curType.TypeParameters [i].Name == innerInstantiatedType.TypeParameters [j].Name)
                    {
                        innerInstantiatedType.typeParameters.RemoveAt(j);
                        j--;
                    }
                }

                result.Add(innerInstantiatedType);
                CreateInstantiatedSubtypes(innerInstantiatedType, innerType, newArguments, resolver);
                foreach (int i in removeInheritedArguments)
                {
                    if (i >= 0 && i < newArguments.Count)
                    {
                        newArguments.RemoveAt(i);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static IType CreateInstantiatedGenericTypeInternal(IType type, IList <IReturnType> genericArguments)
        {
            // This method is now internal. The public one has been moved to ProjectDom, which take cares of caching
            // instantiated generic types.
            if (type is InstantiatedType)
            {
                return(type);
            }
            string name = GetInstantiatedTypeName(type.Name, genericArguments);
            GenericTypeInstanceResolver resolver = new GenericTypeInstanceResolver();

            if (genericArguments != null)
            {
                int   j       = genericArguments.Count - 1;
                IType curType = type;
                while (curType != null)
                {
                    string fullTypeName = curType.DecoratedFullName;
                    for (int i = curType.TypeParameters.Count - 1; i >= 0 && j >= 0; i--, j--)
                    {
                        resolver.Add(fullTypeName + "." + curType.TypeParameters[i].Name, genericArguments[j]);
                    }
                    curType = curType.DeclaringType;
                }
            }
            InstantiatedType result = (InstantiatedType)type.AcceptVisitor(resolver, type);

            if (result.typeParameters != null)
            {
                result.typeParameters.Clear();
            }
            result.Name               = name;
            result.SourceProjectDom   = type.SourceProjectDom;
            result.Resolved           = (type is DomType) ? ((DomType)type).Resolved : false;
            result.GenericParameters  = genericArguments;
            result.UninstantiatedType = type;
            result.DeclaringType      = type.DeclaringType;
            CreateInstantiatedSubtypes(result, type, genericArguments);
            return(result);
        }
Ejemplo n.º 3
0
        public IMethod Extends(ProjectDom dom, IType type)
        {
            if (dom == null || type == null || Parameters.Count == 0 || !IsExtension)
            {
                return(null);
            }
            string extensionTableKey = this.HelpUrl + "/" + type.FullName;

            if (Name.StartsWith("ForEachWithIndex"))
            {
                Console.WriteLine("table key:" + extensionTableKey);
            }
            lock (extensionTable) {
                if (extensionTable.ContainsKey(extensionTableKey))
                {
                    return(extensionTable[extensionTableKey]);
                }
                if (type.BaseType != null && type.BaseType.FullName == "System.Array" && Parameters[0].ReturnType.ArrayDimensions > 0)
                {
                    IReturnType elementType = null;
                    foreach (IReturnType returnType in type.BaseTypes)
                    {
                        if (returnType.FullName == "System.Collections.Generic.IList" && returnType.GenericArguments.Count > 0)
                        {
                            elementType = returnType.GenericArguments[0];
                            break;
                        }
                    }
                    if (elementType != null)
                    {
                        IMethod instMethod = DomMethod.CreateInstantiatedGenericMethod(this, new IReturnType[] {}, new IReturnType[] { elementType });
                        instMethod = new ExtensionMethod(type, instMethod, null, null);
                        extensionTable.Add(extensionTableKey, instMethod);
                        return(instMethod);
                    }
                }
                foreach (IType baseType in dom.GetInheritanceTree(type))
                {
                    IMethod instMethod       = DomMethod.CreateInstantiatedGenericMethod(this, new IReturnType[] {}, new IReturnType[] { new DomReturnType(baseType) });
                    string  baseTypeFullName = baseType is InstantiatedType ? ((InstantiatedType)baseType).UninstantiatedType.FullName : baseType.FullName;

                    // compare the generic arguments.
                    if (instMethod.Parameters[0].ReturnType.FullName == baseTypeFullName && Parameters[0].ReturnType.ArrayDimensions == 0 && Parameters[0].ReturnType.PointerNestingLevel == 0)
                    {
                        if (instMethod.Parameters[0].ReturnType.GenericArguments.Count > 0)
                        {
                            InstantiatedType instType = baseType as InstantiatedType;
                            if (instType == null || instType.GenericParameters.Count != instMethod.Parameters[0].ReturnType.GenericArguments.Count)
                            {
                                continue;
                            }
                            bool genericArgumentsAreEqual = true;
                            for (int i = 0; i < instMethod.Parameters[0].ReturnType.GenericArguments.Count; i++)
                            {
                                if (Name.StartsWith("ForEachWithIndex"))
                                {
                                    Console.WriteLine(instMethod.Parameters[0].ReturnType.GenericArguments[i].DecoratedFullName + " --- " + instType.GenericParameters[i].DecoratedFullName);
                                }
                                if (instMethod.Parameters[0].ReturnType.GenericArguments[i].DecoratedFullName != instType.GenericParameters[i].DecoratedFullName)
                                {
                                    genericArgumentsAreEqual = false;
                                    break;
                                }
                            }
                            if (!genericArgumentsAreEqual)
                            {
                                continue;
                            }
                        }

                        //ExtensionMethod result = new ExtensionMethod (baseType, this, null, null);
                        instMethod = new ExtensionMethod(type, instMethod, null, null);
                        extensionTable.Add(extensionTableKey, instMethod);
                        //Console.WriteLine ("ext. method:" + instMethod);
                        return(instMethod);
                    }
                }
//				Console.WriteLine ("null");
                extensionTable.Add(extensionTableKey, null);
                return(null);
            }
        }
Ejemplo n.º 4
0
        public ExtensionMethod(IType extensionType, IMethod originalMethod, IList <IReturnType> genericArguments, IEnumerable <IReturnType> methodArguments)
        {
            if (extensionType == null)
            {
                throw new ArgumentNullException("extensionType");
            }
            if (originalMethod == null)
            {
                throw new ArgumentNullException("originalMethod");
            }
            this.DeclaringType = extensionType;
            List <IReturnType> args = new List <IReturnType> ();

            if (extensionType.FullName.EndsWith("[]"))
            {
                foreach (IReturnType returnType in extensionType.BaseTypes)
                {
                    if (returnType.FullName == "System.Collections.Generic.IList" && returnType.GenericArguments.Count > 0)
                    {
                        args.Add(returnType.GenericArguments[0]);
                        break;
                    }
                }
                if (args.Count == 0)
                {
                    args.Add(new DomReturnType(extensionType));
                }
            }
            else if (extensionType is InstantiatedType)
            {
                InstantiatedType instType = (InstantiatedType)extensionType;
                DomReturnType    uninstantiatedReturnType = new DomReturnType(instType.UninstantiatedType.FullName);
                foreach (IReturnType genArg in instType.GenericParameters)
                {
                    uninstantiatedReturnType.AddTypeParameter(genArg);
                }
                args.Add(uninstantiatedReturnType);
            }
            else
            {
                args.Add(new DomReturnType(extensionType));
            }
            if (methodArguments != null)
            {
                args.AddRange(methodArguments);
            }
//			Console.WriteLine ("Create Extension method from:");
//			Console.WriteLine ("ext type:" + args[0]);
//			Console.WriteLine (originalMethod);
            this.method = DomMethod.CreateInstantiatedGenericMethod(originalMethod, genericArguments, args);

            // skip first parameter.
            for (int i = 1; i < method.Parameters.Count; i++)
            {
                Add(method.Parameters[i]);
            }
            foreach (ITypeParameter par in method.TypeParameters)
            {
                AddTypeParameter(par);
            }

            this.ExtensionType  = extensionType;
            this.OriginalMethod = originalMethod;
            //Console.WriteLine (this);
            //Console.WriteLine ("oOoOoOoOoOoOoOoOoOoOoOoOoOoO");
        }