Ejemplo n.º 1
0
 public void Add(TypeIndex item)
 {
     if (!ContainsKey(item.Name))
     {
         Add(item.Name, item as T);
     }
 }
Ejemplo n.º 2
0
 public Property(PropertyInfo prop) : base(prop.Name, prop.PropertyType)
 {
     if (CanRead = prop.CanRead)
     {
         Methodnames.Add(prop.GetMethod.Name);
     }
     if (CanWrite = prop.CanWrite)
     {
         Methodnames.Add(prop.SetMethod.Name);
     }
     if (prop.Name == "Item" && !TypeIndex.Equals(TypeIndex.Object) && prop.GetIndexParameters().Length > 0)
     {
         IsIndex = true;
     }
 }
Ejemplo n.º 3
0
        public override XElement ToXml()
        {
            var xe =
                new XElement("Define", TypeIndex.ToXml());

            if (IsStatic)
            {
                xe.Add(new XAttribute("IsStatic", IsStatic));
            }
            if (IsExtension)
            {
                xe.Add(new XAttribute("DeclaringType", DeclaringType));
            }
            foreach (var par in Params)
            {
                xe.Add(par.ToXml());
            }
            return(xe);
        }
Ejemplo n.º 4
0
        public Define(MethodInfo method, string name = null)
            : base(name ?? method.Name, method.ReturnType)
        {
            IsStatic = method.IsStatic;
            var pars = method.GetParameters();

            foreach (var p in pars)
            {
                Params.Add(new Param(p));
            }
            if (IsStatic && method.IsDefined(typeof(ExtensionAttribute)))
            {
                var    type    = TypeIndex.FromSystemType(pars[0].ParameterType);
                var    ns      = Namespace.FromString(type.Namespace);
                var    etype   = ns.GetExtype(type.Name, true);
                Method emethod = null;
                if (etype.Methods.ContainsKey(Name))
                {
                    emethod = etype.Methods[Name];
                }
                else
                {
                    emethod = new Method(Name);
                    etype.Methods.Add(emethod);
                }

                var define =
                    new Define
                {
                    Name          = Name,
                    TypeIndex     = TypeIndex.FromSystemType(method.ReturnType),
                    IsExtension   = true,
                    DeclaringType = method.DeclaringType.FullName,
                };

                for (int i = 1; i < pars.Length; i++)
                {
                    define.Params.Add(new Param(pars[i]));
                }
                emethod.AddDefine(define);
            }
        }
Ejemplo n.º 5
0
        public override void Desc(StreamWriter sw, int space)
        {
            var    pre  = GetPrefixSpace(space);
            string pars = string.Join(", ", Params.Select(p => p.ToString()));

            if (IsStatic)
            {
                sw.WriteLine($"{pre}@staticmethod");
            }
            else if (Params.Count > 0)
            {
                pars = "self, " + pars;
            }
            else
            {
                pars = "self";
            }

            if (Name == "__init__")
            {
                sw.Write($"{pre}def {Name}({pars}) -> {TypeIndex.Name}:");
            }
            else if (TypeIndex.Equals(TypeIndex.None))
            {
                sw.Write($"{pre}def {Name}({pars}):");
            }
            else
            {
                sw.Write($"{pre}def {Name}({pars}) -> {TypeIndex.Fullname}:");
            }

            if (IsExtension)
            {
                sw.WriteLine();
                sw.WriteLine($"{pre}    \"\"\"Extension from: {DeclaringType}\"\"\"");
            }
            else
            {
                sw.WriteLine("...");
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 获取类型索引中包含的所有类型
        /// </summary>
        /// <param name="ti">类型索引</param>
        private void Import(TypeIndex ti)
        {
            //把信息存入Imports字典中,以在索引的Fullname属性中解析

            switch (ti.Kind)
            {
            case TypeIndexKind.System:
            case TypeIndexKind.GenericParam:
                return;

            case TypeIndexKind.GenericType:
                foreach (var tp in ti.GenericParams)
                {
                    Import(tp);
                }
                break;
            }

            if (ti.Namespace == Fullname || ti.Namespace == "typing")
            {
                return;
            }

            if (!Imports.ContainsKey(ti.Namespace))
            {
                Imports.Add(ti.Namespace, new List <string>());
            }
            var lst = Imports[ti.Namespace];

            var name = ti.Name.Split(new char[] { '.' }, 2)[0];

            if (!lst.Contains(name))
            {
                lst.Add(name);
            }
        }
Ejemplo n.º 7
0
 public void Add(TypeIndex item)
 {
     Add(item as T);
 }
Ejemplo n.º 8
0
 public override void FromXml(XElement xe)
 {
     base.FromXml(xe);
     TypeIndex = Parse <TypeIndex>(xe.Element("TypeIndex"));
 }
Ejemplo n.º 9
0
 public Variable(string name, System.Type type)
 {
     TypeIndex = TypeIndex.FromSystemType(type);
     Name      = name;
 }
Ejemplo n.º 10
0
 public Variable(string name)
 {
     TypeIndex = new TypeIndex("None");
     Name      = name;
 }
Ejemplo n.º 11
0
        public Type(System.Type type)
        {
            if (type.FullName == "System.Array")
            {
                Namespace = "System";
                Name      = "Array";
                Kind      = TypeIndexKind.GenericDefinition;
                GenericParams.Add(new TypeIndex("T", TypeIndexKind.GenericParam));
            }
            else
            {
                var ti = FromSystemType(type);
                if (string.IsNullOrEmpty(ti.Namespace))
                {
                    return;
                }
                Namespace     = ti.Namespace;
                Name          = ti.Name;
                Kind          = ti.Kind;
                GenericParams = ti.GenericParams;
            }

            var ns = GetNamespace();

            if (type.IsNested)
            {
                var names = Name.Split('.');
                var ptype = ns.Types[names[0]];
                int i     = 1;
                for (; i < names.Length - 1; i++)
                {
                    ptype = ptype.Types[names[i]];
                }
                Owner = ptype;
                Name  = names[i];
                if (ptype.Types.ContainsKey(Name))
                {
                    if (ptype.Types[Name].Kind == TypeIndexKind.General)
                    {
                        ptype.Types.Remove(Name);
                    }
                    else
                    {
                        return;
                    }
                }
                ptype.Types.Add(this);
            }
            else
            {
                if (ns.Types.ContainsKey(Name))
                {
                    if (ns.Types[Name].Kind == TypeIndexKind.General)
                    {
                        ns.Types.Remove(Name);
                    }
                    else
                    {
                        return;
                    }
                }
                ns.Types.Add(this);
            }

            if (type.BaseType != null)
            {
                BaseTypes.Add(FromSystemType(type.BaseType));
            }

            var itypes = type.GetInterfaces().ToList();

            for (int i = itypes.Count - 1; i >= 0; i--)
            {
                for (int j = 0; j < i; j++)
                {
                    if (itypes[i].IsAssignableFrom(itypes[j]))
                    {
                        itypes.RemoveAt(i);
                        break;
                    }
                }
            }

            foreach (var it in itypes)
            {
                BaseTypes.Add(FromSystemType(it));
            }

            foreach (var field in type.GetFields())
            {
                Fields.Add(new Field(field, _type_int));
            }

            if (!type.IsEnum)
            {
                foreach (var field in type.GetFields())
                {
                    Fields.Add(new Field(field));
                }

                foreach (var property in type.GetProperties())
                {
                    Properties.Add(new Property(property));
                }

                foreach (var @event in type.GetEvents())
                {
                    Events.Add(new Event(@event));
                }

                var inits = type.GetConstructors().Where(m => !m.IsStatic);
                Methods.Add(new Method(inits));

                //去除基类已定义的函数
                var imethodnames =
                    type.GetInterfaces()
                    .SelectMany(t => t.GetMethods())
                    .Select(m => m.Name)
                    .Distinct();
                var methodnames =
                    Properties.SelectMany(p => p.Value.Methodnames)
                    .Union(Events.SelectMany(e => e.Value.Methodnames))
                    .Union(imethodnames);
                if (type.BaseType != null)
                {
                    var bmethodnames =
                        type.BaseType
                        .GetMethods()
                        .Select(m => m.Name)
                        .Distinct();
                    methodnames = methodnames.Union(bmethodnames);
                }
                var methods =
                    type.GetMethods()
                    .Where(m => !(m.IsStatic && m.Name.StartsWith("op_")) && !m.Name.Contains("."))
                    .GroupBy(m => m.Name)
                    .Where(g => !methodnames.Contains(g.Key));

                foreach (var g in methods)
                {
                    Methods.Add(new Method(g));
                }

                if (Kind == TypeIndexKind.GenericDefinition)
                {
                    var bti = new TypeIndex("typing", "Generic", TypeIndexKind.GenericDefinition);
                    foreach (var p in GenericParams)
                    {
                        bti.GenericParams.Add(p);
                    }
                    BaseTypes.Add(bti);
                }

                var prop = Properties.Select(p => p.Value).FirstOrDefault(p => p.IsIndex);
                if (prop != null)
                {
                    var bti = new TypeIndex("typing", "Iterable", TypeIndexKind.GenericDefinition);
                    bti.GenericParams.Add(prop.TypeIndex);
                    BaseTypes.Add(bti);
                }
                else if (type.FullName == "System.Array")
                {
                    var bti = new TypeIndex("typing", "Iterable", TypeIndexKind.GenericDefinition);
                    bti.GenericParams.Add(new TypeIndex("T", TypeIndexKind.GenericParam));
                    BaseTypes.Add(bti);
                }

                if (BaseTypes.Count > 1 && BaseTypes[0].Equals(Object))
                {
                    BaseTypes.RemoveAt(0);
                }

                //去除基类已定义的事件
                var eventnames =
                    type.GetInterfaces()
                    .SelectMany(i => i.GetEvents())
                    .Select(e => e.Name);
                if (type.BaseType != null)
                {
                    eventnames =
                        type.BaseType
                        .GetEvents()
                        .Select(e => e.Name)
                        .Union(eventnames);
                }
                foreach (var name in eventnames.Distinct())
                {
                    Events.Remove(name);
                }

                //去除基类已定义的属性
                var propnames =
                    type.GetInterfaces()
                    .SelectMany(i => i.GetProperties())
                    .Select(p => p.Name);
                if (type.BaseType != null)
                {
                    propnames =
                        type.BaseType
                        .GetProperties()
                        .Select(p => p.Name)
                        .Union(propnames);
                }
                foreach (var name in propnames.Distinct())
                {
                    Properties.Remove(name);
                }
            }
        }