Exemple #1
0
        public override bool Walk(FunctionDefinition node)
        {
            if (node.IsLambda)
            {
                return(false);
            }

            var existing = _scope.LookupNameInScopes(node.Name, NameLookupContext.LookupOptions.Local) as AstPythonFunction;

            if (existing == null)
            {
                existing = new AstPythonFunction(_ast, _module, CurrentClass, node, GetLoc(node));
                _scope.SetInScope(node.Name, existing);
            }

            var funcScope = _scope.Clone();

            if (CreateBuiltinTypes)
            {
                funcScope.SuppressBuiltinLookup = true;
            }
            var funcWalk = new AstAnalysisFunctionWalker(funcScope, node);

            _postWalkers.Add(funcWalk);
            existing.AddOverload(funcWalk.Overload);

            // Do not recurse into functions
            return(false);
        }
Exemple #2
0
        public override bool Walk(FunctionDefinition node)
        {
            if (node.IsLambda)
            {
                return(false);
            }

            var dec = (node.Decorators?.DecoratorsInternal).MaybeEnumerate();

            if (dec.OfType <NameExpression>().Any(n => n.Name == "property"))
            {
                AddProperty(node);
                return(false);
            }
            foreach (var setter in dec.OfType <MemberExpression>().Where(n => n.Name == "setter"))
            {
                if (setter.Target is NameExpression src)
                {
                    var existingProp = _scope.LookupNameInScopes(src.Name, NameLookupContext.LookupOptions.Local) as AstPythonProperty;
                    if (existingProp != null)
                    {
                        // Setter for an existing property, so don't create a function
                        existingProp.MakeSettable();
                        return(false);
                    }
                }
            }

            var existing = _scope.LookupNameInScopes(node.Name, NameLookupContext.LookupOptions.Local) as AstPythonFunction;

            if (existing == null)
            {
                existing = new AstPythonFunction(_ast, _module, CurrentClass, node, GetLoc(node));
                _scope.SetInScope(node.Name, existing);
            }

            var funcScope = _scope.Clone();

            if (CreateBuiltinTypes)
            {
                funcScope.SuppressBuiltinLookup = true;
            }
            var funcWalk = new AstAnalysisFunctionWalker(funcScope, node);

            _postWalkers.Add(funcWalk);
            existing.AddOverload(funcWalk.Overload);

            // Do not recurse into functions
            return(false);
        }
        public void Walk()
        {
            IMember self = null;
            bool    classmethod, staticmethod;

            GetMethodType(_target, out classmethod, out staticmethod);
            if (!staticmethod)
            {
                self = _scope.LookupNameInScopes("__class__", NameLookupContext.LookupOptions.Local);
                if (!classmethod)
                {
                    var cls = self as IPythonType;
                    if (cls == null)
                    {
                        self = null;
                    }
                    else
                    {
                        self = new AstPythonConstant(cls, ((cls as ILocatedMember)?.Locations).MaybeEnumerate().ToArray());
                    }
                }
            }

            _scope.PushScope();
            if (self != null)
            {
                var p0 = _target.Parameters?.FirstOrDefault();
                if (p0 != null && !string.IsNullOrEmpty(p0.Name))
                {
                    _scope.SetInScope(p0.Name, self);
                }
            }
            _target.Walk(this);
            _scope.PopScope();
        }
Exemple #4
0
        public void Walk()
        {
            IMember self = null;
            bool    classmethod, staticmethod;

            GetMethodType(_target, out classmethod, out staticmethod);
            if (!staticmethod)
            {
                self = _scope.LookupNameInScopes("__class__", NameLookupContext.LookupOptions.Local);
                if (!classmethod)
                {
                    var cls = self as IPythonType;
                    if (cls == null)
                    {
                        self = null;
                    }
                    else
                    {
                        self = new AstPythonConstant(cls, ((cls as ILocatedMember)?.Locations).MaybeEnumerate().ToArray());
                    }
                }
            }

            if (_target.ReturnAnnotation != null)
            {
                var retAnn = new TypeAnnotation(_scope.Ast.LanguageVersion, _target.ReturnAnnotation);
                var m      = retAnn.GetValue(new AstTypeAnnotationConverter(_scope));
                if (m is IPythonMultipleMembers mm)
                {
                    _returnTypes.AddRange(mm.Members.OfType <IPythonType>());
                }
                else if (m is IPythonType type)
                {
                    _returnTypes.Add(type);
                }
            }

            _scope.PushScope();
            if (self != null)
            {
                var p0 = _target.ParametersInternal?.FirstOrDefault();
                if (p0 != null && !string.IsNullOrEmpty(p0.Name))
                {
                    _scope.SetInScope(p0.Name, self);
                }
            }
            _target.Walk(this);
            _scope.PopScope();
        }
        private IMember GetSelf()
        {
            GetMethodType(Target, out var classmethod, out var staticmethod);
            var self = _scope.LookupNameInScopes("__class__", NameLookupContext.LookupOptions.Local);

            if (!staticmethod && !classmethod)
            {
                if (!(self is IPythonType cls))
                {
                    self = null;
                }
                else
                {
                    self = new AstPythonConstant(cls, ((cls as ILocatedMember)?.Locations).MaybeEnumerate().ToArray());
                }
            }
        private IMember GetSelf()
        {
            bool classmethod, staticmethod;

            GetMethodType(_target, out classmethod, out staticmethod);
            var self = _scope.LookupNameInScopes("__class__", NameLookupContext.LookupOptions.Local);

            if (!staticmethod && !classmethod)
            {
                var cls = self as IPythonType;
                if (cls == null)
                {
                    self = null;
                }
                else
                {
                    self = new AstPythonConstant(cls, ((cls as ILocatedMember)?.Locations).MaybeEnumerate().ToArray());
                }
            }
            return(self);
        }