コード例 #1
0
        private void AddOverload(FunctionDefinition fd, IPythonClassMember function, Action <IPythonFunctionOverload> addOverload)
        {
            // Check if function exists in stubs. If so, take overload from stub
            // and the documentation from this actual module.
            if (!_table.ReplacedByStubs.Contains(fd))
            {
                var stubOverload = GetOverloadFromStub(fd);
                if (stubOverload != null)
                {
                    if (!string.IsNullOrEmpty(fd.GetDocumentation()))
                    {
                        stubOverload.SetDocumentationProvider(_ => fd.GetDocumentation());
                    }
                    addOverload(stubOverload);
                    _table.ReplacedByStubs.Add(fd);
                    return;
                }
            }

            if (!_table.Contains(fd))
            {
                // Do not evaluate parameter types just yet. During light-weight top-level information
                // collection types cannot be determined as imports haven't been processed.
                var overload = new PythonFunctionOverload(fd, function, _eval.GetLocationOfName(fd));
                addOverload(overload);
                _table.Add(new FunctionEvaluator(_eval, overload));
            }
        }
コード例 #2
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);
            }
        }
コード例 #3
0
 public FunctionEvaluator(ExpressionEval eval, PythonFunctionOverload overload)
     : base(eval, overload.FunctionDefinition)
 {
     _overload          = overload;
     _function          = overload.ClassMember ?? throw new ArgumentNullException(nameof(overload.ClassMember));
     _self              = _function.DeclaringType as PythonClassType;
     FunctionDefinition = overload.FunctionDefinition ?? throw new ArgumentNullException(nameof(overload.FunctionDefinition));
 }
コード例 #4
0
        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);
        }
コード例 #5
0
        public static IPythonPropertyType Property(string name, IPythonModule declaringModule, IPythonType declaringType, IMember returnValue)
        {
            var prop = new PythonPropertyType(name, declaringModule, declaringType, false, LocationInfo.Empty);
            var o    = new PythonFunctionOverload(prop.Name, declaringModule, LocationInfo.Empty);

            o.AddReturnValue(returnValue);
            prop.AddOverload(o);
            return(prop);
        }
コード例 #6
0
        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);
        }
コード例 #7
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);
        }
コード例 #8
0
        public override void Populate(ModuleFactory mf, IPythonType declaringType, IGlobalScope gs)
        {
            _property.SetDocumentation(Documentation);

            var o = new PythonFunctionOverload(_property, mf.DefaultLocation);

            o.SetDocumentation(Documentation);
            o.SetReturnValue(mf.ConstructMember(ReturnType), true);
            _property.AddOverload(o);
        }
コード例 #9
0
        public PythonLazyOverload(OverloadModel model, ModuleFactory mf, IPythonClassMember cm)
        {
            _model = model;
            _mf    = mf;

            ClassMember   = cm;
            Documentation = model.Documentation;

            _overload = new PythonFunctionOverload(cm, new Location(mf.Module, default));
            _overload.SetDocumentation(cm.Documentation);
        }
コード例 #10
0
 public FunctionEvaluator(
     ExpressionEval eval,
     FunctionDefinition targetFunction,
     PythonFunctionOverload overload,
     IPythonClassMember function
     ) : base(eval, targetFunction)
 {
     FunctionDefinition = targetFunction ?? throw new ArgumentNullException(nameof(targetFunction));
     _overload          = overload ?? throw new ArgumentNullException(nameof(overload));
     _function          = function ?? throw new ArgumentNullException(nameof(function));
     _self = function.DeclaringType as PythonClassType;
 }
コード例 #11
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);
        }
コード例 #12
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;
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        public PythonLazyPropertyType(PropertyModel model, ModuleFactory mf, IGlobalScope gs, IPythonType declaringType)
            : base(model, mf, gs, declaringType)
        {
            var location = new Location(mf.Module, model.IndexSpan.ToSpan());

            _property = new PythonPropertyType(model.Name, location, model.Documentation, declaringType,
                                               model.Attributes.HasFlag(FunctionAttributes.Abstract));

            // parameters and return type just to look at them.
            var o = new PythonFunctionOverload(_property, location);

            o.SetDocumentation(model.Documentation);
            _property.AddOverload(o);

            IsReadOnly = model.IsReadOnly;
            SetInnerType(_property);
        }
コード例 #16
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;
        }
コード例 #17
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);
            }
        }
コード例 #18
0
        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);
        }
コード例 #19
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);
        }