Пример #1
0
        private void SpecializeFuture(FromImportStatement node)
        {
            if (Interpreter.LanguageVersion.Is3x())
            {
                return;
            }

            var printNameExpression = node.Names.FirstOrDefault(n => n?.Name == "print_function");

            if (printNameExpression != null)
            {
                var fn         = new PythonFunctionType("print", new Location(Module), null, string.Empty);
                var o          = new PythonFunctionOverload(fn.Name, new Location(Module));
                var parameters = new List <ParameterInfo> {
                    new ParameterInfo("*values", Interpreter.GetBuiltinType(BuiltinTypeId.Object), ParameterKind.List, null),
                    new ParameterInfo("sep", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.KeywordOnly, null),
                    new ParameterInfo("end", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.KeywordOnly, null),
                    new ParameterInfo("file", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.KeywordOnly, null)
                };
                o.SetParameters(parameters);
                o.SetReturnValue(Interpreter.GetBuiltinType(BuiltinTypeId.NoneType), true);
                fn.AddOverload(o);
                Eval.DeclareVariable("print", fn, VariableSource.Import, printNameExpression);
            }
        }
        public static IPythonFunctionType Function(string name, IPythonModule declaringModule, IPythonType declaringType, string documentation, IMember returnValue)
        {
            var prop = new PythonFunctionType(name, declaringModule, declaringType, documentation, LocationInfo.Empty);
            var o    = new PythonFunctionOverload(prop.Name, declaringModule, _ => LocationInfo.Empty);

            o.AddReturnValue(returnValue);
            prop.AddOverload(o);
            return(prop);
        }
        public static IMember __iter__(IPythonInterpreter interpreter, BuiltinTypeId contentTypeId)
        {
            var fn = new PythonFunctionType(@"__iter__", interpreter.ModuleResolution.BuiltinsModule, null, string.Empty, LocationInfo.Empty);
            var o  = new PythonFunctionOverload(fn.Name, interpreter.ModuleResolution.BuiltinsModule, _ => fn.Location);

            o.AddReturnValue(PythonTypeIterator.FromTypeId(interpreter, contentTypeId));
            fn.AddOverload(o);
            return(fn);
        }
Пример #4
0
 private IMember AddFunction(FunctionDefinition node, IPythonType declaringType, LocationInfo loc)
 {
     if (!(_eval.LookupNameInScopes(node.Name, LookupOptions.Local) is PythonFunctionType existing))
     {
         existing = new PythonFunctionType(node, _eval.Module, declaringType, loc);
         _eval.DeclareVariable(node.Name, existing, VariableSource.Declaration, loc);
     }
     AddOverload(node, existing, o => existing.AddOverload(o));
     return(existing);
 }
 private void AddFunction(FunctionDefinition fd, IPythonType declaringType)
 {
     if (!(_eval.LookupNameInScopes(fd.Name, LookupOptions.Local) is PythonFunctionType existing))
     {
         existing = new PythonFunctionType(fd, declaringType, _eval.GetLocationOfName(fd));
         // The variable is transient (non-user declared) hence it does not have location.
         // Function type is tracking locations for references and renaming.
         _eval.DeclareVariable(fd.Name, existing, VariableSource.Declaration);
     }
     AddOverload(fd, existing, o => existing.AddOverload(o));
 }
 private void AddFunction(FunctionDefinition fd, PythonType declaringType)
 {
     if (!(_eval.LookupNameInScopes(fd.Name, LookupOptions.Local) is PythonFunctionType f))
     {
         f = new PythonFunctionType(fd, declaringType, _eval.GetLocationOfName(fd));
         // The variable is transient (non-user declared) hence it does not have location.
         // Function type is tracking locations for references and renaming.
         _eval.DeclareVariable(fd.Name, f, VariableSource.Declaration);
         _typeMap[fd] = f;
         declaringType?.AddMember(f.Name, f, overwrite: true);
     }
     AddOverload(fd, f, o => f.AddOverload(o));
 }
Пример #7
0
        public IMember GetValueFromLambda(LambdaExpression expr)
        {
            if (expr == null)
            {
                return(null);
            }

            var location = GetLocationOfName(expr.Function);
            var ft       = new PythonFunctionType(expr.Function, null, location);
            var overload = new PythonFunctionOverload(expr.Function, ft, location);

            ft.AddOverload(overload);
            return(ft);
        }
Пример #8
0
        public NamedTuple(IPythonModule declaringModule) : base(BuiltinTypeId.Tuple, declaringModule)
        {
            var interpreter = DeclaringModule.Interpreter;

            var fn = new PythonFunctionType("__init__", new Location(declaringModule), this, "NamedTuple");
            var o  = new PythonFunctionOverload(fn, new Location(DeclaringModule));

            o.SetParameters(new List <ParameterInfo> {
                new ParameterInfo("self", this, ParameterKind.Normal, this),
                new ParameterInfo("name", interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, null),
                new ParameterInfo("members", interpreter.GetBuiltinType(BuiltinTypeId.List), ParameterKind.Normal, null)
            });
            fn.AddOverload(o);
            _constructor = fn;
        }
        public IMember GetValueFromLambda(LambdaExpression expr)
        {
            if (expr == null)
            {
                return(null);
            }

            var fd       = expr.Function;
            var location = GetLocationOfName(fd);
            var ft       = new PythonFunctionType(fd, null, location);
            var overload = new PythonFunctionOverload(ft, fd, location, expr.Function.ReturnAnnotation?.ToCodeString(Ast));

            overload.SetParameters(CreateFunctionParameters(null, ft, fd, false));
            ft.AddOverload(overload);
            return(ft);
        }
Пример #10
0
        public PythonLazyFunctionType(FunctionModel model, ModuleFactory mf, IGlobalScope gs, IPythonType declaringType)
            : base(model, mf, gs, declaringType)
        {
            var location = new Location(mf.Module, model.IndexSpan.ToSpan());

            _function = new PythonFunctionType(model.Name, location, declaringType, model.Documentation);

            // TODO: restore signature string so hover (tooltip) documentation won't have to restore the function.
            // parameters and return type just to look at them.
            foreach (var om in model.Overloads)
            {
                var o = new PythonLazyOverload(om, mf, _function);
                _function.AddOverload(o);
            }
            SetInnerType(_function);
        }
        private void AddFunction(FunctionDefinition fd, PythonType declaringType)
        {
            if (!(_eval.LookupNameInScopes(fd.Name, LookupOptions.Local) is PythonFunctionType f))
            {
                f = new PythonFunctionType(fd, declaringType, _eval.GetLocationOfName(fd));
                // The variable is transient (non-user declared) hence it does not have location.
                // Function type is tracking locations for references and renaming.

                // if there are multiple functions with same name exist, only the very first one will be
                // maintained in the scope. we should improve this if possible.
                // https://github.com/microsoft/python-language-server/issues/1693
                _eval.DeclareVariable(fd.Name, f, VariableSource.Declaration);
                _typeMap[fd] = f;
                declaringType?.AddMember(f.Name, f, overwrite: true);
            }
            AddOverload(fd, f, o => f.AddOverload(o));
        }
Пример #12
0
        public TypeVar(IPythonModule declaringModule) : base(BuiltinTypeId.Type, declaringModule)
        {
            var interpreter = DeclaringModule.Interpreter;

            var fn = new PythonFunctionType("__init__", new Location(declaringModule), this, "TypeVar");
            var o  = new PythonFunctionOverload(fn, new Location(DeclaringModule));

            var boolType = interpreter.GetBuiltinType(BuiltinTypeId.Bool);

            o.SetParameters(new List <ParameterInfo> {
                new ParameterInfo("self", this, ParameterKind.Normal, this),
                new ParameterInfo("name", interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, null),
                new ParameterInfo("constraints", interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.List, null),
                new ParameterInfo("bound", interpreter.GetBuiltinType(BuiltinTypeId.Type), ParameterKind.KeywordOnly, new PythonConstant(null, interpreter.GetBuiltinType(BuiltinTypeId.None))),
                new ParameterInfo("covariant", boolType, ParameterKind.KeywordOnly, new PythonConstant(false, boolType)),
                new ParameterInfo("contravariant", boolType, ParameterKind.KeywordOnly, new PythonConstant(false, boolType))
            });
            fn.AddOverload(o);
            _constructor = fn;
        }
Пример #13
0
        public override void Populate(ModuleFactory mf, IPythonType declaringType, IGlobalScope gs)
        {
            // Create inner functions and classes first since function may be returning one of them.
            var all = Classes.Concat <MemberModel>(Functions).ToArray();

            foreach (var model in all)
            {
                _function.AddMember(Name, model.Create(mf, _function, gs), overwrite: true);
            }
            foreach (var model in all)
            {
                model.Populate(mf, _function, gs);
            }

            foreach (var om in Overloads)
            {
                var o = new PythonFunctionOverload(_function, new Location(mf.Module, IndexSpan.ToSpan()));
                o.SetDocumentation(Documentation);
                o.SetReturnValue(mf.ConstructMember(om.ReturnType), true);
                o.SetParameters(om.Parameters.Select(p => ConstructParameter(mf, p)).ToArray());
                _function.AddOverload(o);
            }
        }
Пример #14
0
        private void SpecializeMembers()
        {
            var location = new Location(this, default);

            // TypeVar
            var fn = new PythonFunctionType("TypeVar", location, null, GetMemberDocumentation);
            var o  = new PythonFunctionOverload(fn.Name, location);

            // When called, create generic parameter type. For documentation
            // use original TypeVar declaration so it appear as a tooltip.
            o.SetReturnValueProvider((interpreter, overload, args)
                                     => GenericTypeParameter.FromTypeVar(args, interpreter));

            fn.AddOverload(o);
            _members["TypeVar"] = fn;

            // NewType
            fn = new PythonFunctionType("NewType", location, null, GetMemberDocumentation);
            o  = new PythonFunctionOverload(fn.Name, location);
            // When called, create generic parameter type. For documentation
            // use original TypeVar declaration so it appear as a tooltip.
            o.SetReturnValueProvider((interpreter, overload, args) => CreateTypeAlias(args.Values <IMember>()));
            fn.AddOverload(o);
            _members["NewType"] = fn;

            // NewType
            fn = new PythonFunctionType("Type", location, null, GetMemberDocumentation);
            o  = new PythonFunctionOverload(fn.Name, location);
            // When called, create generic parameter type. For documentation
            // use original TypeVar declaration so it appear as a tooltip.
            o.SetReturnValueProvider((interpreter, overload, args) => {
                var a = args.Values <IMember>();
                return(a.Count == 1 ? a[0] : Interpreter.UnknownType);
            });
            fn.AddOverload(o);
            _members["Type"] = fn;

            _members["Iterator"] = new GenericType("Iterator", CreateIteratorType, this);

            _members["Iterable"]        = new GenericType("Iterable", typeArgs => CreateListType("Iterable", BuiltinTypeId.List, typeArgs, false), this);
            _members["Sequence"]        = new GenericType("Sequence", typeArgs => CreateListType("Sequence", BuiltinTypeId.List, typeArgs, false), this);
            _members["MutableSequence"] = new GenericType("MutableSequence",
                                                          typeArgs => CreateListType("MutableSequence", BuiltinTypeId.List, typeArgs, true), this);
            _members["List"] = new GenericType("List",
                                               typeArgs => CreateListType("List", BuiltinTypeId.List, typeArgs, true), this);

            _members["MappingView"] = new GenericType("MappingView",
                                                      typeArgs => CreateDictionary("MappingView", typeArgs, false), this);

            _members["KeysView"]   = new GenericType("KeysView", CreateKeysViewType, this);
            _members["ValuesView"] = new GenericType("ValuesView", CreateValuesViewType, this);
            _members["ItemsView"]  = new GenericType("ItemsView", CreateItemsViewType, this);

            _members["Set"] = new GenericType("Set",
                                              typeArgs => CreateListType("Set", BuiltinTypeId.Set, typeArgs, true), this);
            _members["MutableSet"] = new GenericType("MutableSet",
                                                     typeArgs => CreateListType("MutableSet", BuiltinTypeId.Set, typeArgs, true), this);
            _members["FrozenSet"] = new GenericType("FrozenSet",
                                                    typeArgs => CreateListType("FrozenSet", BuiltinTypeId.Set, typeArgs, false), this);

            _members["Tuple"] = new GenericType("Tuple", CreateTupleType, this);

            _members["Mapping"] = new GenericType("Mapping",
                                                  typeArgs => CreateDictionary("Mapping", typeArgs, false), this);
            _members["MutableMapping"] = new GenericType("MutableMapping",
                                                         typeArgs => CreateDictionary("MutableMapping", typeArgs, true), this);
            _members["Dict"] = new GenericType("Dict",
                                               typeArgs => CreateDictionary("Dict", typeArgs, true), this);
            _members["OrderedDict"] = new GenericType("OrderedDict",
                                                      typeArgs => CreateDictionary("OrderedDict", typeArgs, true), this);
            _members["DefaultDict"] = new GenericType("DefaultDict",
                                                      typeArgs => CreateDictionary("DefaultDict", typeArgs, true), this);

            _members["Union"] = new GenericType("Union", CreateUnion, this);

            _members["Counter"] = Specialized.Function("Counter", this, null, "Counter",
                                                       new PythonInstance(Interpreter.GetBuiltinType(BuiltinTypeId.Int)));

            _members["SupportsInt"]     = Interpreter.GetBuiltinType(BuiltinTypeId.Int);
            _members["SupportsFloat"]   = Interpreter.GetBuiltinType(BuiltinTypeId.Float);
            _members["SupportsComplex"] = Interpreter.GetBuiltinType(BuiltinTypeId.Complex);
            _members["SupportsBytes"]   = Interpreter.GetBuiltinType(BuiltinTypeId.Bytes);
            _members["ByteString"]      = Interpreter.GetBuiltinType(BuiltinTypeId.Bytes);

            fn = new PythonFunctionType("NamedTuple", location, null, GetMemberDocumentation);
            o  = new PythonFunctionOverload(fn.Name, location);
            o.SetReturnValueProvider((interpreter, overload, args) => CreateNamedTuple(args.Values <IMember>()));
            fn.AddOverload(o);
            _members["NamedTuple"] = fn;

            _members["Any"] = new AnyType(this);

            // AnyStr
            var str        = Interpreter.GetBuiltinType(BuiltinTypeId.Str);
            var bytes      = Interpreter.GetBuiltinType(BuiltinTypeId.Bytes);
            var unicode    = Interpreter.GetBuiltinType(BuiltinTypeId.Unicode);
            var anyStrName = new PythonConstant("AnyStr", str);

            var anyStrArgs = Interpreter.LanguageVersion.Is3x()
                ? new IMember[] { anyStrName, str, bytes }
                : new IMember[] { anyStrName, str, unicode };

            _members["AnyStr"] = GenericTypeParameter.FromTypeVar(new ArgumentSet(anyStrArgs), this);

            _members["Optional"] = new GenericType("Optional", CreateOptional, this);
            _members["Type"]     = new GenericType("Type", CreateType, this);

            _members["Generic"] = new GenericType("Generic", CreateGenericClassParameter, this);
        }