Example #1
0
        private CPythonType LookupType(object type)
        {
            if (type != null)
            {
                object[] typeInfo = (object[])type;
                if (typeInfo.Length == 2)
                {
                    string modName  = typeInfo[0] as string;
                    string typeName = typeInfo[1] as string;

                    if (modName != null)
                    {
                        if (typeName != null)
                        {
                            var module = GetModule(modName);
                            if (module != null)
                            {
                                return(module.GetAnyMember(typeName) as CPythonType);
                            }
                        }
                    }
                }
            }
            else
            {
                return(BuiltinModule.GetAnyMember("object") as CPythonType);
            }
            return(null);
        }
Example #2
0
        private async Task LoadKnownTypesAsync(CancellationToken token)
        {
            _itemCache.Clear();

            var fallback = new FallbackBuiltinModule(_langVersion);

            var moduleRef = await Modules.TryImportAsync(_builtinName, token).ConfigureAwait(false);

            if (moduleRef != null)
            {
                _builtinModule = (BuiltinModule)moduleRef.Module;
            }
            else
            {
                _builtinModule        = new BuiltinModule(fallback, this);
                Modules[_builtinName] = new ModuleReference(_builtinModule, _builtinName);
            }
            _builtinModule.InterpreterModule.Imported(_defaultContext);

            Modules.AddBuiltinModuleWrapper("sys", SysModuleInfo.Wrap);
            Modules.AddBuiltinModuleWrapper("typing", TypingModuleInfo.Wrap);

            _knownTypes = KnownTypes.Create(this, fallback);

            _noneInst = (ConstantInfo)GetCached(
                _nullKey,
                () => new ConstantInfo(ClassInfos[BuiltinTypeId.NoneType], null, PythonMemberType.Constant)
                );

            AddBuiltInSpecializations();
        }
Example #3
0
        private async Task LoadKnownTypesAsync()
        {
            if (_loadKnownTypesException != null)
            {
                _loadKnownTypesException.Throw();
            }

            _itemCache.Clear();

            Debug.Assert(_builtinModule != null, "LoadInitialKnownTypes was not called");
            if (_builtinModule == null)
            {
                LoadInitialKnownTypes();
            }

            var moduleRef = await Modules.TryImportAsync(_builtinName).ConfigureAwait(false);

            if (moduleRef != null)
            {
                _builtinModule = (BuiltinModule)moduleRef.Module;
            }
            else
            {
                Modules[_builtinName] = new ModuleReference(_builtinModule, _builtinName);
            }

            Modules.AddBuiltinModuleWrapper("sys", SysModuleInfo.Wrap);
            Modules.AddBuiltinModuleWrapper("typing", TypingModuleInfo.Wrap);

            FinishLoadKnownTypes(null);
        }
Example #4
0
        private async Task LoadKnownTypesAsync()
        {
            _itemCache.Clear();

            Debug.Assert(_builtinModule != null, "LoadInitialKnownTypes was not called");
            if (_builtinModule == null)
            {
                LoadInitialKnownTypes();
            }

            var moduleRef = await Modules.TryImportAsync(_builtinName).ConfigureAwait(false);

            if (moduleRef != null)
            {
                _builtinModule = (BuiltinModule)moduleRef.Module;
            }
            else
            {
                Modules[_builtinName] = new ModuleReference(_builtinModule);
            }

            FinishLoadKnownTypes(null);

            var sysModule = await _modules.TryImportAsync("sys").ConfigureAwait(false);

            if (sysModule != null)
            {
                var bm = sysModule.AnalysisModule as BuiltinModule;
                if (bm != null)
                {
                    sysModule.Module = new SysModuleInfo(bm);
                }
            }
        }
Example #5
0
        public override bool Walk(ImportStatement node)
        {
            var iinfo = new ImportInfo("", node.Span);

            GlobalScope.Imports[node] = iinfo;
            int len = Math.Min(node.Names.Count, node.AsNames.Count);

            for (int i = 0; i < len; i++)
            {
                var impNode    = node.Names[i];
                var newName    = node.AsNames[i];
                var strImpName = impNode.MakeString();
                iinfo.Types.Add(new[] { strImpName, newName });

                var             saveName = (String.IsNullOrEmpty(newName)) ? strImpName : newName;
                ModuleReference modRef;

                var def = Scopes[Scopes.Length - 1].CreateVariable(impNode, _unit, saveName);
                if (!ProjectState.Modules.TryGetValue(strImpName, out modRef))
                {
                    var builtinModule = ProjectState.ImportModule(strImpName, impNode.Names.Count > 1 && !String.IsNullOrEmpty(newName));

                    if (builtinModule != null)
                    {
                        builtinModule.InterpreterModule.Imported(_unit.DeclaringModule.InterpreterContext);

                        def.AddTypes(impNode, _unit, builtinModule.SelfSet);
                        continue;
                    }
                }

                if (modRef != null)
                {
                    if (modRef.Module != null)
                    {
                        ModuleInfo mi = modRef.Module as ModuleInfo;
                        if (mi != null)
                        {
                            mi.ModuleDefinition.AddDependency(_unit);
                        }

                        BuiltinModule builtinModule = modRef.Module as BuiltinModule;
                        if (builtinModule != null)
                        {
                            builtinModule.InterpreterModule.Imported(_unit.DeclaringModule.InterpreterContext);
                        }

                        def.AddTypes(impNode, _unit, modRef.Module.SelfSet);
                        continue;
                    }
                }
                else
                {
                    ProjectState.Modules[strImpName] = modRef = new ModuleReference();
                }
            }
            return(true);
        }
Example #6
0
        private void WalkFromImportWorker(FromImportStatement node, Namespace userMod, object[] mods, string impName, string newName)
        {
            var saveName = (newName == null) ? impName : newName;

            GlobalScope.Imports[node].Types.Add(new[] { impName, newName });

            // TODO: Better node would be the name node but we don't have a name node (they're just strings in the AST w/ no position info)
            var variable = Scopes[Scopes.Length - 1].CreateVariable(node, _unit, saveName);

            ISet <Namespace> newTypes = EmptySet <Namespace> .Instance;
            bool             madeSet  = false;

            // look for builtin / user-defined modules first
            ModuleInfo module = userMod as ModuleInfo;

            if (module != null)
            {
                var modVal = module.Scope.CreateVariable(node, _unit, impName);
                modVal.AddDependency(_unit);

                newTypes = newTypes.Union(modVal.Types, ref madeSet);
            }

            BuiltinModule builtinModule = userMod as BuiltinModule;

            if (builtinModule != null)
            {
                var modVal = builtinModule.GetMember(node, _unit, impName);

                newTypes = newTypes.Union(modVal, ref madeSet);
            }

            // then look for .NET reflected namespace
            if (mods != null)
            {
                var mems = new List <object>(mods.Length);
                foreach (var mod in mods)
                {
                    object val;
                    if (ProjectState.TryGetMember(mod, impName, out val) && val != null)
                    {
                        mems.Add(val);
                    }
                }

                if (mems.Count > 0)
                {
                    GlobalScope.ShowClr = true;
                    var ns = ProjectState.GetNamespaceFromObjects(mems);
                    newTypes = newTypes.Union(ns.SelfSet, ref madeSet);
                }
            }

            variable.AddTypes(node, _unit, newTypes);
        }
Example #7
0
        public PythonAnalyzer(IPythonInterpreter pythonInterpreter, PythonLanguageVersion langVersion)
        {
            _langVersion       = langVersion;
            _interpreter       = pythonInterpreter;
            _modules           = new Dictionary <string, ModuleReference>();
            _modulesByFilename = new Dictionary <string, ModuleInfo>(StringComparer.OrdinalIgnoreCase);
            _itemCache         = new Dictionary <object, object>();

            InitializeBuiltinModules();
            pythonInterpreter.ModuleNamesChanged += new EventHandler(ModuleNamesChanged);

            _types           = new KnownTypes(this);
            _builtinModule   = (BuiltinModule)Modules["__builtin__"].Module;
            _propertyObj     = GetBuiltin("property");
            _classmethodObj  = GetBuiltin("classmethod");
            _staticmethodObj = GetBuiltin("staticmethod");
            _typeObj         = GetBuiltin("type");
            _intType         = GetBuiltin("int");
            _stringType      = (BuiltinClassInfo)GetBuiltin("str");

            _objectSet = new HashSet <Namespace>(new[] { GetBuiltin("object") });

            _setType       = (BuiltinClassInfo)GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.Set));
            _rangeFunc     = GetBuiltin("range");
            _frozensetType = GetBuiltin("frozenset");
            _functionType  = GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.Function));
            _generatorType = (BuiltinClassInfo)GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.Generator));
            _dictType      = (BuiltinClassInfo)GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.Dict));
            _boolType      = (BuiltinClassInfo)GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.Bool));
            _noneInst      = (ConstantInfo)GetNamespaceFromObjects(null);
            _listType      = (BuiltinClassInfo)GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.List));
            _tupleType     = (BuiltinClassInfo)GetNamespaceFromObjects(_interpreter.GetBuiltinType(BuiltinTypeId.Tuple));

            _queue = new Deque <AnalysisUnit>();

            SpecializeFunction("__builtin__", "range", (n, unit, args) => unit.DeclaringModule.GetOrMakeNodeVariable(n, (nn) => new RangeInfo(_types.List, unit.ProjectState).SelfSet));
            SpecializeFunction("__builtin__", "min", ReturnUnionOfInputs);
            SpecializeFunction("__builtin__", "max", ReturnUnionOfInputs);

            pythonInterpreter.Initialize(this);

            _defaultContext = pythonInterpreter.CreateModuleContext();

            // cached for quick checks to see if we're a call to clr.AddReference

            try {
                SpecializeFunction("wpf", "LoadComponent", LoadComponent);
            } catch (KeyNotFoundException) {
                // IronPython.Wpf.dll isn't available...
            }
        }
Example #8
0
        private void LoadInitialKnownTypes()
        {
            if (_builtinModule != null)
            {
                Debug.Fail("LoadInitialKnownTypes should only be called once");
                return;
            }

            var fallbackDb = PythonTypeDatabase.CreateDefaultTypeDatabase(LanguageVersion.ToVersion());

            _builtinModule = _modules.GetBuiltinModule(fallbackDb.GetModule(SharedDatabaseState.BuiltinName2x));

            FinishLoadKnownTypes(fallbackDb);
        }
Example #9
0
        internal BuiltinModule GetBuiltinModule(IPythonModule attr)
        {
            if (attr == null)
            {
                return(null);
            }
            BuiltinModule res;

            if (!_builtinModuleTable.TryGetValue(attr, out res))
            {
                _builtinModuleTable[attr] = res = new BuiltinModule(attr, _analyzer);
            }
            return(res);
        }
Example #10
0
        private IPythonType RunObjectTypeFixups()
        {
            var objectType = Volatile.Read(ref _objectType);

            if (objectType == null)
            {
                var newObjectType = BuiltinModule.GetAnyMember(GetBuiltinTypeName(BuiltinTypeId.Object)) as IPythonType;

                if (newObjectType == null)
                {
                    // No type available, so don't do fixups
                    return(null);
                }

                // Either set _objectType to newObjectType, or replace our local
                // objectType with whatever got there first
                objectType = Interlocked.CompareExchange(ref _objectType, newObjectType, null) ?? newObjectType;
            }

            var fixups = _objectTypeFixups;

            if (fixups != null)
            {
                lock (fixups) {
                    if (!ReferenceEquals(_objectTypeFixups, fixups))
                    {
                        // _objectTypeFixups changed while we waited to lock
                        // This means someone else now owns the list and will
                        // run the fixups, so we can just return the new object
                        // type.
                        return(objectType);
                    }
                    _objectTypeFixups = null;
                }

                // At this point, nobody else has a reference to the list. If
                // they did (see AddObjectTypeFixup), they just entered the lock
                // now and discovered that _objectTypeFixups has changed and
                // will not use the reference they have.
                foreach (var assign in fixups)
                {
                    assign(objectType);
                }
            }

            return(objectType);
        }
Example #11
0
        private ICollection <string> GetModuleKeys(Namespace userMod)
        {
            ModuleInfo mi = userMod as ModuleInfo;

            if (mi != null)
            {
                return(mi.Scope.Variables.Keys);
            }

            BuiltinModule bmi = userMod as BuiltinModule;

            if (bmi != null)
            {
                return(bmi.GetMemberNames(GlobalScope.InterpreterContext).ToArray());
            }

            return(new string[0]);
        }
Example #12
0
        private ICollection <string> GetModuleKeys(Namespace userMod)
        {
            ModuleInfo mi = userMod as ModuleInfo;

            if (mi != null)
            {
                return(mi.Scope.Variables.Keys);
            }

            BuiltinModule bmi = userMod as BuiltinModule;

            if (bmi != null)
            {
                return(bmi.VariableDict.Keys);
            }

            return(new string[0]);
        }
Example #13
0
            internal override bool ModuleContainsMember(IModuleContext context, string name)
            {
                BuiltinModule builtin = Module as BuiltinModule;

                if (builtin != null)
                {
                    return(BuiltinModuleContainsMember(context, name, builtin.InterpreterModule));
                }

                ModuleInfo modInfo = Module as ModuleInfo;

                if (modInfo != null)
                {
                    VariableDef varDef;
                    if (modInfo.Scope.Variables.TryGetValue(name, out varDef) &&
                        varDef.VariableStillExists)
                    {
                        var types = varDef.TypesNoCopy;
                        if (types.Count > 0)
                        {
                            foreach (var type in types)
                            {
                                if (type is ModuleInfo || type is BuiltinModule)
                                {
                                    // we find modules via our modules list, dont duplicate these
                                    return(false);
                                }

                                foreach (var location in type.Locations)
                                {
                                    if (location.ProjectEntry != modInfo.ProjectEntry)
                                    {
                                        // declared in another module
                                        return(false);
                                    }
                                }
                            }
                        }

                        return(true);
                    }
                }
                return(false);
            }
        internal void ImportChildren(IPythonModule interpreterModule)
        {
            BuiltinModule builtinModule = null;

            foreach (var child in interpreterModule.GetChildrenModules())
            {
                builtinModule = builtinModule ?? GetBuiltinModule(interpreterModule);
                var fullname = builtinModule.Name + "." + child;

                if (!_modules.TryGetValue(fullname, out var modRef) || modRef?.Module == null)
                {
                    if (builtinModule.TryGetMember(child, out var value) && value is IModule module)
                    {
                        SetModule(fullname, module);
                        _analyzer?.DoDelayedSpecialization(fullname);
                    }
                }
            }
        }
        internal BuiltinModule GetBuiltinModule(IPythonModule attr)
        {
            if (attr == null)
            {
                return(null);
            }

            if (!_builtinModuleTable.TryGetValue(attr, out var res))
            {
                res = new BuiltinModule(attr, _analyzer);
                if (_builtinModuleType.TryGetValue(attr.Name, out var wrap) && wrap != null)
                {
                    res = wrap(res);
                }
                _builtinModuleTable[attr] = res;
            }

            return(res);
        }
 private void RunObjectTypeFixups()
 {
     if (_objectType == null)
     {
         _objectType = BuiltinModule.GetAnyMember(GetBuiltinTypeName(BuiltinTypeId.Object)) as IPythonType;
         if (_objectType != null)
         {
             var fixups = _objectTypeFixups;
             _objectTypeFixups = null;
             if (fixups != null)
             {
                 foreach (var assign in fixups)
                 {
                     assign(_objectType);
                 }
             }
         }
     }
 }
Example #17
0
        /// <summary>
        /// Replaces a built-in function (specified by module name and function name) with a customized
        /// delegate which provides specific behavior for handling when that function is called.
        ///
        /// Currently this just provides a hook when the function is called - it could be expanded
        /// to providing the interpretation of when the function is called as well.
        /// </summary>
        private void SpecializeFunction(string moduleName, string name, Func <CallExpression, AnalysisUnit, ISet <Namespace>[], ISet <Namespace> > dlg)
        {
            var module = Modules[moduleName];

            BuiltinModule builtin = module.Module as BuiltinModule;

            Debug.Assert(builtin != null);
            if (builtin != null)
            {
                foreach (var v in builtin[name])
                {
                    BuiltinFunctionInfo funcInfo = v as BuiltinFunctionInfo;
                    if (funcInfo != null)
                    {
                        builtin[name] = new SpecializedBuiltinFunction(this, funcInfo.Function, dlg).SelfSet;
                        break;
                    }
                }
            }
        }
        private void LoadKnownTypes()
        {
            _itemCache.Clear();

            ModuleReference moduleRef;

            if (Modules.TryImport(_builtinName, out moduleRef))
            {
                _builtinModule = (BuiltinModule)moduleRef.Module;
            }
            else
            {
                var fallbackDb = PythonTypeDatabase.CreateDefaultTypeDatabase(LanguageVersion.ToVersion());
                _builtinModule        = _modules.GetBuiltinModule(fallbackDb.GetModule(SharedDatabaseState.BuiltinName2x));
                Modules[_builtinName] = new ModuleReference(_builtinModule);
            }

            Types      = new KnownTypes(this);
            ClassInfos = (IKnownClasses)Types;

            _noneInst = (ConstantInfo)GetCached(_nullKey, () => new ConstantInfo(ClassInfos[BuiltinTypeId.NoneType], (object)null));

            DoNotUnionInMro = AnalysisSet.Create(new AnalysisValue[] {
                ClassInfos[BuiltinTypeId.Object],
                ClassInfos[BuiltinTypeId.Type]
            });

            AddBuiltInSpecializations();

            ModuleReference sysModule;

            if (_modules.TryImport("sys", out sysModule))
            {
                var bm = sysModule.AnalysisModule as BuiltinModule;
                if (bm != null)
                {
                    sysModule.Module = new SysModuleInfo(bm);
                }
            }
        }
Example #19
0
        /// <summary>
        /// Looks up a type and queues a fixup if the type is not yet available.  Receives a delegate
        /// which assigns the value to the appropriate field.
        /// </summary>
        public void LookupType(object type, Action <CPythonType> assign)
        {
            var value = LookupType(type);

            if (value == null)
            {
                AddFixup(
                    () => {
                    var delayedType = LookupType(type);
                    if (delayedType == null)
                    {
                        delayedType = BuiltinModule.GetAnyMember("object") as CPythonType;
                    }
                    Debug.Assert(delayedType != null);
                    assign(delayedType);
                }
                    );
            }
            else
            {
                assign(value);
            }
        }
Example #20
0
        internal void ImportChildren(IPythonModule interpreterModule)
        {
            BuiltinModule module = null;

            foreach (var child in interpreterModule.GetChildrenModules())
            {
                module = module ?? GetBuiltinModule(interpreterModule);
                ModuleReference modRef;
                var             fullname = module.Name + "." + child;
                if (!_modules.TryGetValue(fullname, out modRef) || modRef == null || modRef.Module == null)
                {
                    IAnalysisSet value;
                    if (module.TryGetMember(child, out value))
                    {
                        var mod = value as IModule;
                        if (mod != null)
                        {
                            _modules[fullname] = new ModuleReference(mod);
                        }
                    }
                }
            }
        }
        private async Task LoadKnownTypesAsync(CancellationToken token)
        {
            _itemCache.Clear();

            var fallback = new FallbackBuiltinModule(LanguageVersion);

            if (Modules.TryImport(_builtinName, out var moduleRef))
            {
                _builtinModule = (BuiltinModule)moduleRef.Module;
            }
            else
            {
                _builtinModule = new BuiltinModule(fallback, this);
                Modules.SetModule(_builtinName, BuiltinModule);
            }
            _builtinModule.InterpreterModule.Imported(_defaultContext);

            var builtinModuleNamesMember = ((IBuiltinPythonModule)_builtinModule.InterpreterModule).GetAnyMember("__builtin_module_names__");

            if (builtinModuleNamesMember is Interpreter.Ast.AstPythonStringLiteral builtinModuleNamesLiteral && builtinModuleNamesLiteral.Value != null)
            {
                var builtinModuleNames = builtinModuleNamesLiteral.Value.Split(',').Select(n => n.Trim());
                _pathResolver.SetBuiltins(builtinModuleNames);
            }

            Modules.AddBuiltinModuleWrapper("sys", SysModuleInfo.Wrap);
            Modules.AddBuiltinModuleWrapper("typing", TypingModuleInfo.Wrap);

            _knownTypes = KnownTypes.Create(this, fallback);

            _noneInst = (ConstantInfo)GetCached(
                _nullKey,
                () => new ConstantInfo(ClassInfos[BuiltinTypeId.NoneType], null, PythonMemberType.Constant)
                );

            AddBuiltInSpecializations();
        }
Example #22
0
        private void WalkFromImportWorker(FromImportStatement node, Namespace userMod, string impName, string newName)
        {
            var saveName = (newName == null) ? impName : newName;

            GlobalScope.Imports[node].Types.Add(new[] { impName, newName });

            // TODO: Better node would be the name node but we don't have a name node (they're just strings in the AST w/ no position info)
            var variable = Scopes[Scopes.Length - 1].CreateVariable(node, _unit, saveName);

            ISet <Namespace> newTypes = EmptySet <Namespace> .Instance;
            bool             madeSet  = false;

            // look for builtin / user-defined modules first
            ModuleInfo module = userMod as ModuleInfo;

            if (module != null)
            {
                var modVal = module.Scope.CreateVariable(node, _unit, impName);
                Scopes[Scopes.Length - 1].GetLinkedVariables(saveName).Add(modVal);

                newTypes = newTypes.Union(modVal.Types, ref madeSet);
            }

            BuiltinModule builtinModule = userMod as BuiltinModule;

            if (builtinModule != null)
            {
                var modVal = builtinModule.GetMember(node, _unit, impName);

                newTypes = newTypes.Union(modVal, ref madeSet);

                builtinModule.InterpreterModule.Imported(_unit.DeclaringModule.InterpreterContext);
            }

            variable.AddTypes(node, _unit, newTypes);
        }
Example #23
0
        private async Task LoadKnownTypesAsync()
        {
            _itemCache.Clear();

            var fallback = new FallbackBuiltinModule(_langVersion);

            var moduleRef = await Modules.TryImportAsync(_builtinName).ConfigureAwait(false);

            if (moduleRef != null)
            {
                _builtinModule = (BuiltinModule)moduleRef.Module;
            }
            else
            {
                _builtinModule        = new BuiltinModule(fallback, this);
                Modules[_builtinName] = new ModuleReference(_builtinModule, _builtinName);
            }

            Modules.AddBuiltinModuleWrapper("sys", SysModuleInfo.Wrap);
            Modules.AddBuiltinModuleWrapper("typing", TypingModuleInfo.Wrap);

            Types = KnownTypes.Create(this, fallback);

            ClassInfos = (IKnownClasses)Types;
            _noneInst  = (ConstantInfo)GetCached(
                _nullKey,
                () => new ConstantInfo(ClassInfos[BuiltinTypeId.NoneType], null, PythonMemberType.Constant)
                );

            DoNotUnionInMro = AnalysisSet.Create(new AnalysisValue[] {
                ClassInfos[BuiltinTypeId.Object],
                ClassInfos[BuiltinTypeId.Type]
            });

            AddBuiltInSpecializations();
        }
Example #24
0
        /// <summary>
        /// returns the MemberResults associated with modules in the specified
        /// list of names.  The list of names is the path through the module, for example
        /// ['System', 'Runtime']
        /// </summary>
        /// <returns></returns>
        public MemberResult[] GetModuleMembers(IModuleContext moduleContext, string[] names, bool bottom)
        {
            IDictionary <string, ISet <Namespace> > d = null;

            ModuleReference moduleRef;

            if (Modules.TryGetValue(names[0], out moduleRef) && moduleRef.Module != null)
            {
                var module = moduleRef.Module;
                d = new Dictionary <string, ISet <Namespace> >();

                var mod = module.SelfSet;
                if (bottom)
                {
                    for (int i = 1; i < names.Length; i++)
                    {
                        var next = names[i];
                        // import Foo.Bar as Baz, we need to get Bar
                        VariableDef      def;
                        ISet <Namespace> newMod  = EmptySet <Namespace> .Instance;
                        bool             madeSet = false;
                        foreach (var modItem in mod)
                        {
                            BuiltinModule builtinMod = modItem as BuiltinModule;
                            if (builtinMod != null)
                            {
                                var mem = builtinMod._type.GetMember(moduleContext, next);
                                if (mem != null)
                                {
                                    newMod = newMod.Union(GetNamespaceFromObjects(mem), ref madeSet);
                                }
                            }
                            else
                            {
                                ModuleInfo userMod = modItem as ModuleInfo;
                                if (userMod != null && userMod.Scope.Variables.TryGetValue(next, out def))
                                {
                                    newMod = newMod.Union(def.Types, ref madeSet);
                                }
                            }
                        }

                        mod = newMod;
                        if (mod.Count == 0)
                        {
                            break;
                        }
                    }
                }

                foreach (var modItem in mod)
                {
                    Update(d, modItem.GetAllMembers(moduleContext));
                }
            }

            MemberResult[] result;
            if (d != null)
            {
                result = new MemberResult[d.Count];
                int pos = 0;
                foreach (var kvp in d)
                {
                    result[pos++] = new MemberResult(kvp.Key, kvp.Value);
                }
            }
            else
            {
                result = new MemberResult[0];
            }

            return(result);
        }
Example #25
0
 public SysModuleInfo(BuiltinModule inner)
     : base(inner.InterpreterModule, inner.ProjectState) {
 }
        /// <summary>
        /// Looks up a type and queues a fixup if the type is not yet available.
        /// Receives a delegate which assigns the value to the appropriate field.
        /// </summary>
        public void LookupType(object typeRefOrList, Action <IPythonType> assign)
        {
            var typeRef = typeRefOrList as object[];

            if (typeRef != null && typeRef.Length >= 2)
            {
                string        modName = null, typeName = null;
                List <object> indexTypes = null;
                IPythonType   res        = null;

                modName  = typeRef[0] as string;
                typeName = typeRef[1] as string;
                if (typeRef.Length > 2)
                {
                    indexTypes = typeRef[2] as List <object>;
                }

                if (typeName == null)
                {
                    Debug.Assert(modName == null, "moduleref should not be passed to LookupType");
                    AddObjectTypeFixup(assign);
                    return;
                }
                else
                {
                    IPythonModule module;
                    if (modName == null)
                    {
                        res = BuiltinModule.GetAnyMember(typeName) as IPythonType;
                        if (res != null)
                        {
                            assign(res);
                        }
                        else
                        {
                            AddObjectTypeFixup(assign);
                        }
                    }
                    else
                    {
                        string alternateTypeName = typeName;
                        module = GetModuleOrClass(modName, ref alternateTypeName);
                        if (module == null)
                        {
                            AddFixup(() => {
                                // Fixup 1: Module was not found.
                                var mod2 = GetModuleOrClass(modName, ref alternateTypeName);
                                if (mod2 != null)
                                {
                                    AssignMemberFromModule(mod2, alternateTypeName, null, indexTypes, assign, true);
                                }
                            });
                            return;
                        }
                        AssignMemberFromModule(module, alternateTypeName, null, indexTypes, assign, true);
                    }
                }
                return;
            }

            var multiple = typeRefOrList as List <object>;

            if (multiple != null)
            {
                foreach (var typeInfo in multiple)
                {
                    LookupType(typeInfo, assign);
                }
            }
        }
        public ProjectState(ScriptEngine pythonEngine)
        {
            _pythonEngine      = pythonEngine;
            _projectEntries    = new List <ProjectEntry>();
            _modules           = new Dictionary <string, ModuleReference>();
            _modulesByFilename = new Dictionary <string, ModuleInfo>(StringComparer.OrdinalIgnoreCase);
            _itemCache         = new Dictionary <object, object>();

            var pythonContext = HostingHelpers.GetLanguageContext(_pythonEngine) as PythonContext;

            _codeContextCls = new ModuleContext(new PythonDictionary(), pythonContext).GlobalContext;
            _codeContextCls.ModuleContext.ShowCls = true;

            _codeContext = new ModuleContext(
                new PythonDictionary(),
                HostingHelpers.GetLanguageContext(_pythonEngine) as PythonContext
                ).GlobalContext;

            InitializeBuiltinModules();

            // TODO: Use reflection-only!
            _references = new List <KeyValuePair <Assembly, TopNamespaceTracker> >();
            AddAssembly(LoadAssemblyInfo(typeof(string).Assembly));
            AddAssembly(LoadAssemblyInfo(typeof(Debug).Assembly));


            // cached for quick checks to see if we're a call to clr.AddReference
            SpecializeFunction("clr", "AddReference", (n, unit, args) => AddReference(n, null));
            SpecializeFunction("clr", "AddReferenceByPartialName", (n, unit, args) => AddReference(n, ClrModule.LoadAssemblyByPartialName));
            SpecializeFunction("clr", "AddReferenceByName", (n, unit, args) => AddReference(n, null));
            SpecializeFunction("clr", "AddReferenceToFile", (n, unit, args) => AddReference(n, (s) => ClrModule.LoadAssemblyFromFile(_codeContext, s)));
            SpecializeFunction("clr", "AddReferenceToFileAndPath", (n, unit, args) => AddReference(n, (s) => ClrModule.LoadAssemblyFromFileWithPath(_codeContext, s)));

            try {
                SpecializeFunction("wpf", "LoadComponent", LoadComponent);
            } catch (KeyNotFoundException) {
                // IronPython.Wpf.dll isn't available...
            }

            SpecializeFunction("__builtin__", "range", (n, unit, args) => unit.DeclaringModule.GetOrMakeNodeVariable(n, (nn) => new RangeInfo(ClrModule.GetPythonType(typeof(List)), unit.ProjectState).SelfSet));
            SpecializeFunction("__builtin__", "min", ReturnUnionOfInputs);
            SpecializeFunction("__builtin__", "max", ReturnUnionOfInputs);

            _builtinModule   = (BuiltinModule)Modules["__builtin__"].Module;
            _propertyObj     = GetBuiltin("property");
            _classmethodObj  = GetBuiltin("classmethod");
            _staticmethodObj = GetBuiltin("staticmethod");
            _typeObj         = GetBuiltin("type");
            _intType         = GetBuiltin("int");
            _stringType      = (BuiltinClassInfo)GetBuiltin("str");

            _objectSet = new HashSet <Namespace>(new[] { GetBuiltin("object") });

            _setType       = (BuiltinClassInfo)GetNamespaceFromObjects(TypeCache.Set);
            _rangeFunc     = GetBuiltin("range");
            _frozensetType = GetBuiltin("frozenset");
            _functionType  = GetNamespaceFromObjects(TypeCache.Function);
            _generatorType = (BuiltinClassInfo)GetNamespaceFromObjects(DynamicHelpers.GetPythonTypeFromType(typeof(PythonGenerator)));
            _dictType      = (BuiltinClassInfo)GetNamespaceFromObjects(TypeCache.Dict);
            _boolType      = (BuiltinClassInfo)GetNamespaceFromObjects(TypeCache.Boolean);
            _noneInst      = (ConstantInfo)GetNamespaceFromObjects(new object[] { null });
            _listType      = (BuiltinClassInfo)GetNamespaceFromObjects(TypeCache.List);
            _tupleType     = (BuiltinClassInfo)GetNamespaceFromObjects(TypeCache.PythonTuple);

            _queue = new Queue <AnalysisUnit>();

            _docProvider = CodeContext.LanguageContext.GetService <DocumentationProvider>();
        }