Exemplo n.º 1
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());
                    }
                }
            }

            _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();
        }
        private IMember GetValueFromCallable(CallExpression expr, LookupOptions options)
        {
            if (expr == null || expr.Target == null)
            {
                return(null);
            }

            var     m     = GetValueFromExpression(expr.Target);
            IMember value = null;

            switch (m)
            {
            case IPythonFunction pf:
                value = GetValueFromPropertyOrFunction(pf, expr);
                break;

            case IPythonType type when type == Interpreter.GetBuiltinType(BuiltinTypeId.Type) && expr.Args.Count >= 1:
                value = GetTypeFromValue(GetValueFromExpression(expr.Args[0].Expression, options));
                break;

            case IPythonType type:
                value = new AstPythonConstant(type, GetLoc(expr));
                break;

            default:
                break;
            }

            if (value == null)
            {
                _log?.Log(TraceLevel.Verbose, "UnknownCallable", expr.Target.ToCodeString(Ast).Trim());
            }
            return(value);
        }
Exemplo n.º 3
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());
                }
            }
        protected override void PostWalk(PythonWalker walker)
        {
            IPythonType boolType = null;
            IPythonType noneType = null;

            foreach (BuiltinTypeId typeId in Enum.GetValues(typeof(BuiltinTypeId)))
            {
                if (_members.TryGetValue("__{0}__".FormatInvariant(typeId), out var m) && m is AstPythonType biType && biType.IsBuiltin)
                {
                    if (typeId != BuiltinTypeId.Str && typeId != BuiltinTypeId.StrIterator)
                    {
                        biType.TrySetTypeId(typeId);
                    }

                    if (biType.IsHidden)
                    {
                        _hiddenNames.Add(biType.Name);
                    }
                    _hiddenNames.Add("__{0}__".FormatInvariant(typeId));

                    if (typeId == BuiltinTypeId.Bool)
                    {
                        boolType = boolType ?? biType;
                    }

                    if (typeId == BuiltinTypeId.NoneType)
                    {
                        noneType = noneType ?? biType;
                    }
                }
            }
            _hiddenNames.Add("__builtin_module_names__");

            if (boolType != null)
            {
                _members["True"] = _members["False"] = new AstPythonConstant(boolType);
            }

            if (noneType != null)
            {
                _members["None"] = new AstPythonConstant(noneType);
            }

            base.PostWalk(walker);
        }
Exemplo n.º 6
0
        public IMember Get()
        {
            var m = _realMember;

            if (m != null)
            {
                return(m);
            }

            var interp = _context as AstPythonInterpreter;

            if (interp == null)
            {
                return(null);
            }

            // Set an "unknown" value to prevent recursion
            var locs     = ImportLocation == null?Array.Empty <LocationInfo>() : new[] { ImportLocation };
            var sentinel = new AstPythonConstant(interp.GetBuiltinType(BuiltinTypeId.Unknown), locs);

            m = Interlocked.CompareExchange(ref _realMember, sentinel, null);
            if (m != null)
            {
                // We raced and someone else set a value, so just return that
                return(m);
            }

            Module.Imported(_context);
            m = Module.GetMember(_context, Name) ?? interp.ImportModule(Module.Name + "." + Name);
            if (m != null)
            {
                (m as IPythonModule)?.Imported(_context);
                var current = Interlocked.CompareExchange(ref _realMember, m, sentinel);
                if (current == sentinel)
                {
                    return(m);
                }
                return(current);
            }

            // Did not find a better member, so keep the sentinel
            return(sentinel);
        }
        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);
        }
Exemplo n.º 8
0
        protected override void PostWalk(PythonWalker walker)
        {
            IPythonType boolType = null;

            foreach (BuiltinTypeId typeId in Enum.GetValues(typeof(BuiltinTypeId)))
            {
                IMember m;
                AstPythonBuiltinType biType;
                if (_members.TryGetValue($"__{typeId}__", out m) && (biType = m as AstPythonBuiltinType) != null)
                {
                    if (typeId != BuiltinTypeId.Str &&
                        typeId != BuiltinTypeId.StrIterator)
                    {
                        biType.TrySetTypeId(typeId);
                    }

                    if (biType.IsHidden)
                    {
                        _hiddenNames.Add(biType.Name);
                    }
                    _hiddenNames.Add($"__{typeId}__");

                    if (typeId == BuiltinTypeId.Bool)
                    {
                        boolType = m as IPythonType;
                    }
                }
            }
            _hiddenNames.Add("__builtin_module_names__");

            if (boolType != null)
            {
                _members["True"] = _members["False"] = new AstPythonConstant(boolType);
            }

            base.PostWalk(walker);
        }