Пример #1
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 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.Specialize(name, analysis.Document, string.Empty);
         f.AddOverload(new PythonFunctionOverload(f, new Location(analysis.Document)));
         analysis.GlobalScope.DeclareVariable(name, f, VariableSource.Declaration);
     }
     return(f);
 }
        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);
        }
        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);
        }
Пример #5
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);
        }