Beispiel #1
0
        public override bool Walk(PythonAst node)
        {
            if (_ast != node)
            {
                throw new InvalidOperationException("walking wrong AST");
            }
            _scope.PushScope(_members);

            CollectAllClasses((node.Body as SuiteStatement)?.Statements);

            return(base.Walk(node));
        }
Beispiel #2
0
        public override bool Walk(PythonAst node)
        {
            if (_ast != node)
            {
                throw new InvalidOperationException("walking wrong AST");
            }

            FirstPassCollectClasses();

            _scope.PushScope(_members);

            return(base.Walk(node));
        }
        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();
        }
Beispiel #4
0
 public AstAnalysisWalker(
     IPythonInterpreter interpreter,
     PathResolverSnapshot pathResolver,
     PythonAst ast,
     IPythonModule module,
     string filePath,
     Uri documentUri,
     Dictionary <string, IMember> members,
     bool includeLocationInfo,
     bool warnAboutUndefinedValues,
     bool suppressBuiltinLookup,
     AnalysisLogWriter log = null
     )
 {
     _log     = log ?? (interpreter as AstPythonInterpreter)?.Log;
     _module  = module ?? throw new ArgumentNullException(nameof(module));
     _members = members ?? throw new ArgumentNullException(nameof(members));
     _scope   = new NameLookupContext(
         interpreter ?? throw new ArgumentNullException(nameof(interpreter)),
         interpreter.CreateModuleContext(),
         ast ?? throw new ArgumentNullException(nameof(ast)),
         _module,
         filePath,
         documentUri,
         includeLocationInfo,
         _functionWalkers,
         log: warnAboutUndefinedValues ? _log : null
         );
     _ast          = ast;
     _interpreter  = interpreter;
     _pathResolver = pathResolver;
     _scope.SuppressBuiltinLookup = suppressBuiltinLookup;
     _scope.PushScope(_typingScope);
     WarnAboutUndefinedValues = warnAboutUndefinedValues;
 }
Beispiel #5
0
        public void Walk()
        {
            var self = GetSelf();

            _selfType = (self as AstPythonConstant)?.Type as AstPythonType;

            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();
        }
Beispiel #6
0
        public override bool Walk(PythonAst node)
        {
            if (_ast != node)
            {
                throw new InvalidOperationException("walking wrong AST");
            }

            CollectTopLevelDefinitions();
            _scope.PushScope(_members);

            return(base.Walk(node));
        }
Beispiel #7
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();
        }
        public void Walk()
        {
            var self = GetSelf();

            _selfType = (self as AstPythonConstant)?.Type as AstPythonType;

            _overload.ReturnTypes.AddRange(_scope.GetTypesFromAnnotation(_target.ReturnAnnotation));

            _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();
        }
        public void Walk()
        {
            var self = GetSelf();

            _selfType = (self as AstPythonConstant)?.Type as AstPythonType;

            var annotationTypes = _scope.GetTypesFromAnnotation(Target.ReturnAnnotation).ExcludeDefault();

            _overload.ReturnTypes.AddRange(annotationTypes);

            _scope.PushScope();

            // Declare self, if any
            var skip = 0;

            if (self != null)
            {
                var p0 = Target.Parameters.FirstOrDefault();
                if (p0 != null && !string.IsNullOrEmpty(p0.Name))
                {
                    _scope.SetInScope(p0.Name, self);
                    skip++;
                }
            }

            // Declare parameters in scope
            foreach (var p in Target.Parameters.Skip(skip).Where(p => !string.IsNullOrEmpty(p.Name)))
            {
                var value = _scope.GetValueFromExpression(p.DefaultValue);
                _scope.SetInScope(p.Name, value ?? _scope.UnknownType);
            }

            // return type from the annotation always wins, no need to walk the body.
            if (!annotationTypes.Any())
            {
                Target.Walk(this);
            }
            _scope.PopScope();
        }