Exemple #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 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);
        }
        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);
        }
Exemple #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);
 }
Exemple #5
0
        public static IPythonFunctionType Function(string name, IPythonModule declaringModule, string documentation, IMember returnValue)
        {
            var location = new Location(declaringModule);
            var prop     = PythonFunctionType.Specialize(name, declaringModule, documentation);
            var o        = new PythonFunctionOverload(prop.Name, location);

            o.AddReturnValue(returnValue);
            prop.AddOverload(o);
            return(prop);
        }
 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));
 }
Exemple #7
0
 private static PythonFunctionType GetOrCreateFunction(this IDocumentAnalysis analysis, string name)
 {
     // We DO want to replace class by function. Consider type() in builtins.
     // 'type()' in code is a function call, not a type class instantiation.
     if (!(analysis.GlobalScope.Variables[name]?.Value is PythonFunctionType f))
     {
         f = PythonFunctionType.ForSpecialization(name, analysis.Document);
         f.AddOverload(new PythonFunctionOverload(name, analysis.Document, _ => LocationInfo.Empty));
         analysis.GlobalScope.DeclareVariable(name, f, VariableSource.Declaration, LocationInfo.Empty);
     }
     return(f);
 }
 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));
 }
Exemple #9
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);
        }
        private PythonFunctionType GetOrCreateFunction(string name)
        {
            var f = Analysis.GlobalScope.Variables[name]?.Value as PythonFunctionType;

            // We DO want to replace class by function. Consider type() in builtins.
            // 'type()' in code is a function call, not a type class instantiation.
            if (f == null)
            {
                f = PythonFunctionType.ForSpecialization(name, this);
                f.AddOverload(new PythonFunctionOverload(name, this, LocationInfo.Empty));
                Analysis.GlobalScope.DeclareVariable(name, f, LocationInfo.Empty);
            }
            return(f);
        }
Exemple #11
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;
        }
        private IPythonType SpecializeNewType(Location location)
        {
            var fn = PythonFunctionType.Specialize("NewType", this, GetMemberDocumentation("NewType"));
            var o  = new PythonFunctionOverload(fn, location);

            // When called, create generic parameter type. For documentation
            // use original TypeVar declaration so it appear as a tooltip.
            o.SetReturnValueProvider((interpreter, overload, args, indexSpan) => CreateTypeAlias(args));
            o.SetParameters(new[] {
                new ParameterInfo("name", Interpreter.GetBuiltinType(BuiltinTypeId.Str), ParameterKind.Normal, null),
                new ParameterInfo("tp", Interpreter.GetBuiltinType(BuiltinTypeId.Type), ParameterKind.Normal, null),
            });
            fn.AddOverload(o);
            return(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);
        }
Exemple #14
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));
        }
        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;
        }
        private void SpecializeMembers()
        {
            var location = new Location(this);

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

            o.SetParameters(new List <ParameterInfo> {
                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.Str), ParameterKind.KeywordOnly, new PythonConstant(null, Interpreter.GetBuiltinType(BuiltinTypeId.NoneType))),
                new ParameterInfo("covariant", Interpreter.GetBuiltinType(BuiltinTypeId.Bool), ParameterKind.KeywordOnly, new PythonConstant(false, Interpreter.GetBuiltinType(BuiltinTypeId.Bool))),
                new ParameterInfo("contravariant", Interpreter.GetBuiltinType(BuiltinTypeId.Bool), ParameterKind.KeywordOnly, new PythonConstant(false, Interpreter.GetBuiltinType(BuiltinTypeId.Bool)))
            });

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

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

            // NewType
            _members["NewType"] = SpecializeNewType(location);

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

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

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

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

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

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

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

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

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

            _members["Counter"] = Specialized.Function("Counter", this, GetMemberDocumentation("Counter"),
                                                       Interpreter.GetBuiltinType(BuiltinTypeId.Int)).CreateInstance(ArgumentSet.WithoutContext);

            // TODO: make these classes that support __float__, etc per spec.
            //_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 = PythonFunctionType.Specialize("NamedTuple", this, GetMemberDocumentation("NamedTuple"));
            o  = new PythonFunctionOverload(fn, location);
            o.SetReturnValueProvider((declaringModule, overload, args, indexSpan)
                                     => CreateNamedTuple(args.Values <IMember>(), declaringModule, indexSpan));
            fn.AddOverload(o);
            _members["NamedTuple"] = fn;

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

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

            _members["Generic"] = new SpecializedGenericType("Generic", CreateGenericClassBase, this);
        }
Exemple #18
0
        private void SpecializeMembers()
        {
            var location = new Location(this, default);

            // TypeVar
            var fn = PythonFunctionType.Specialize("TypeVar", this, GetMemberDocumentation("TypeVar"));
            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 = PythonFunctionType.Specialize("NewType", this, GetMemberDocumentation("NewType"));
            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;

            // Type
            fn = PythonFunctionType.Specialize("Type", this, GetMemberDocumentation("Type"));
            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, GetMemberDocumentation("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 = PythonFunctionType.Specialize("NamedTuple", this, GetMemberDocumentation("NamedTuple"));
            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);
        }
Exemple #19
0
 public override IMember Create(ModuleFactory mf, IPythonType declaringType, IGlobalScope gs)
 => _function ?? (_function = new PythonFunctionType(Name, new Location(mf.Module, IndexSpan.ToSpan()), declaringType, Documentation));