Ejemplo n.º 1
0
        static internal void BuildIndex()
        {
            if (Global != null && Usings != null && TypeCache != null)
            {
                return;
            }

            var global    = new NamespaceSymbol();
            var usings    = new List <ContainerSymbol>();
            var rtFuncs   = new List <ContainerSymbol>();
            var typeCache = new Dictionary <Type, TypeSymbol>();

            var usedSymbols = new HashSet <ContainerSymbol>();

            foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (a.IsDynamic)
                {
                    continue;
                }
                var most_visible = a == System.Reflection.Assembly.GetEntryAssembly();
                try
                {
                    var types = most_visible ? a.GetTypes() : a.GetExportedTypes();

                    // Build type lookup dictionary
                    foreach (var t in types.Where(t => !t.IsNested && !string.IsNullOrEmpty(t?.Name)))
                    {
                        var n   = global;
                        var nss = t.Namespace?.Split('.');
                        if (nss != null)
                        {
                            foreach (var ns in nss)
                            {
                                if (!string.IsNullOrEmpty(ns))
                                {
                                    Symbol nn;
                                    if (!n.Members.TryGetValue(ns, out nn) || (!(nn is NamespaceSymbol) && most_visible))
                                    {
                                        nn            = new NamespaceSymbol(ns, n);
                                        n.Members[ns] = nn;
                                    }
                                    if (!(nn is NamespaceSymbol))
                                    {
                                        continue;
                                    }
                                    n = nn as NamespaceSymbol;
                                }
                            }
                        }

                        Func <ContainerSymbol, Type, TypeSymbol> add_type = null;

                        add_type = (ct, ty) =>
                        {
                            if (ty.IsNested)
                            {
                                ct = add_type(ct, ty.DeclaringType);
                                if (ct == null)
                                {
                                    return(null);
                                }
                            }
                            Symbol tv;
                            if (ct.Members.TryGetValue(ty.Name, out tv))
                            {
                                if ((tv as TypeSymbol)?.Type == ty)
                                {
                                    return((TypeSymbol)tv);
                                }
                                if (!most_visible && tv is TypeSymbol)
                                {
                                    return(null);
                                }
                            }

                            var r = new TypeSymbol(ty);
                            ct.Members[ty.Name] = r;
                            typeCache[ty]       = r;
                            return(r);
                        };

                        var ts = add_type(n, t);

                        /*if (!t.IsNested && t.Name.Equals(XSharpSpecialNames.FunctionsClass, StringComparison.OrdinalIgnoreCase))
                         * {
                         *  if (ts != null && !usedSymbols.Contains(ts))
                         *  {
                         *      usedSymbols.Add(ts);
                         *      usings.Add(ts);
                         *  }
                         * }*/
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine("Error loading types from " + a.CodeBase + "\r" + e.Message);
                }
            }

            global    = System.Threading.Interlocked.CompareExchange(ref Global, global, null);
            typeCache = System.Threading.Interlocked.CompareExchange(ref TypeCache, typeCache, null);

            Compilation.InitializeNativeTypes();
            Compilation.InitializeWellKnownTypes();
            Compilation.InitializeWellKnownMembers();

            foreach (var ns in new string[] { OurNameSpaces.System, OurNameSpaces.XSharp, OurNameSpaces.Vulcan })
            {
                var s = LookupFullName(ns) as NamespaceSymbol;
                if (s != null && !usedSymbols.Contains(s))
                {
                    usedSymbols.Add(s);
                    usings.Add(s);
                }
            }

            var cla = Compilation.Get(WellKnownTypes.XSharp_Internal_ClassLibraryAttribute);
            var ina = Compilation.Get(WellKnownTypes.ImplicitNamespaceAttribute);

            if (cla != null && ina != null)
            {
                foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
                {
                    if (a.IsDynamic)
                    {
                        continue;
                    }
                    bool isXsRuntime  = a.IsInXSharpRuntime();
                    var  most_visible = a == System.Reflection.Assembly.GetEntryAssembly();
                    if (a.CustomAttributes != null)
                    {
                        foreach (var attr in a.CustomAttributes)
                        {
                            if (attr.AttributeType == ina.Type)
                            {
                                var args = attr.ConstructorArguments;
                                if (args != null && args.Count == 1)
                                {
                                    // first element is the default namespace
                                    var ns = args[0].Value.ToString();
                                    if (!string.IsNullOrEmpty(ns))
                                    {
                                        var s = LookupFullName(ns) as NamespaceSymbol;
                                        if (s != null && !usedSymbols.Contains(s))
                                        {
                                            usedSymbols.Add(s);
                                            usings.Add(s);
                                        }
                                    }
                                }
                            }
                            else if (attr.AttributeType == cla.Type)
                            {
                                var args = attr.ConstructorArguments;
                                if (args != null && args.Count == 2)
                                {
                                    // first element is the Functions class
                                    var cls = args[0].Value.ToString();
                                    if (!string.IsNullOrEmpty(cls))
                                    {
                                        var s = LookupFullName(cls) as TypeSymbol;
                                        if (s != null)
                                        {
                                            if (isXsRuntime)
                                            {
                                                rtFuncs.Add(s);
                                            }
                                            else if (!usedSymbols.Contains(s))
                                            {
                                                usedSymbols.Add(s);
                                                usings.Add(s);
                                            }
                                        }
                                    }
                                    // second element is the default namespace
                                    var ns = args[1].Value.ToString();
                                    if (!string.IsNullOrEmpty(ns))
                                    {
                                        var s = LookupFullName(ns) as NamespaceSymbol;
                                        if (s != null && !usedSymbols.Contains(s))
                                        {
                                            usedSymbols.Add(s);
                                            usings.Add(s);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            usings  = System.Threading.Interlocked.CompareExchange(ref Usings, usings, null);
            rtFuncs = System.Threading.Interlocked.CompareExchange(ref RuntimeFunctions, rtFuncs, null);
        }
Ejemplo n.º 2
0
        static internal void BuildIndex()
        {
            if (Global != null && Usings != null && TypeCache != null)
            {
                return;
            }

            var global    = new NamespaceSymbol();
            var usings    = new List <ContainerSymbol>();
            var rtFuncs   = new List <ContainerSymbol>();
            var typeCache = new Dictionary <Type, TypeSymbol>();

            var usedSymbols = new HashSet <ContainerSymbol>();

            var assemblies       = AppDomain.CurrentDomain.GetAssemblies();
            var loadedAssemblies = new HashSet <Assembly>(assemblies);

            foreach (var a in assemblies)
            {
                if (a.IsDynamic)
                {
                    continue;
                }
                UpdateTypeCache(global, typeCache, a);
            }

            System.Threading.Interlocked.CompareExchange(ref Global, global, null);
            System.Threading.Interlocked.CompareExchange(ref TypeCache, typeCache, null);
            System.Threading.Interlocked.CompareExchange(ref LoadedAssemblies, loadedAssemblies, null);

            Compilation.InitializeNativeTypes();
            Compilation.InitializeWellKnownTypes();
            Compilation.InitializeWellKnownMembers();

            foreach (var ns in new string[] { OurNameSpaces.System, OurNameSpaces.XSharp, OurNameSpaces.Vulcan })
            {
                var s = LookupFullName(ns) as NamespaceSymbol;
                if (s != null && !usedSymbols.Contains(s))
                {
                    usedSymbols.Add(s);
                    usings.Add(s);
                }
            }

            var cla = Compilation.Get(WellKnownTypes.XSharp_Internal_ClassLibraryAttribute);
            var ina = Compilation.Get(WellKnownTypes.ImplicitNamespaceAttribute);

            if (cla != null && ina != null)
            {
                foreach (var a in assemblies)
                {
                    if (a.IsDynamic)
                    {
                        continue;
                    }
                    UpdateUsings(usings, rtFuncs, a, usedSymbols);
                }
            }

            System.Threading.Interlocked.CompareExchange(ref Usings, usings, null);
            System.Threading.Interlocked.CompareExchange(ref RuntimeFunctions, rtFuncs, null);
        }