Example #1
0
        public OverviewWalker(ProjectEntry entry, AnalysisUnit topAnalysis)
        {
            _entry = entry;
            _curUnit = topAnalysis;

            _scope = topAnalysis.Scope;
        }
Example #2
0
        public override void PostWalk(GeneratorExpression node)
        {
            Debug.Assert(_scope is ComprehensionScope);
            _scope = _scope.OuterScope;

            base.PostWalk(node);
        }
Example #3
0
 public FunctionScope(FunctionInfo function, Node node, InterpreterScope declScope)
     : base(function, node, declScope)
 {
     ReturnValue = new VariableDef();
     if (Function.FunctionDefinition.IsGenerator) {
         Generator = new GeneratorInfo(function.ProjectState, Function.FunctionDefinition);
         ReturnValue.AddTypes(function.ProjectEntry, Generator.SelfSet, false);
     }
 }
Example #4
0
 public override void PostWalk(FunctionDefinition node)
 {
     if (node.Body != null && node.Name != null) {
         Debug.Assert(_scope.Node == node);
         Debug.Assert(_scope.OuterScope.Node != node);
         _scope = _scope.OuterScope;
         _curUnit = _analysisStack.Pop();
         Debug.Assert(_scope.EnumerateTowardsGlobal.Contains(_curUnit.Scope));
     }
 }
Example #5
0
        internal FunctionInfo(FunctionDefinition node, AnalysisUnit declUnit, InterpreterScope declScope)
        {
            _projectEntry = declUnit.ProjectEntry;
            _functionDefinition = node;
            _declVersion = declUnit.ProjectEntry.AnalysisVersion;

            if (Name == "__new__") {
                IsClassMethod = true;
            }

            _analysisUnit = new FunctionAnalysisUnit(this, declUnit, declScope);
        }
Example #6
0
        internal FunctionAnalysisUnit(FunctionInfo function, AnalysisUnit declUnit, InterpreterScope declScope)
            : base(function.FunctionDefinition, null)
        {
            _declUnit = declUnit;
            Function = function;
            _decoratorCalls = new Dictionary<Node, Expression>();

            var scope = new FunctionScope(Function, Function.FunctionDefinition, declScope);
            scope.EnsureParameters(this, null);
            _scope = scope;

            AnalysisLog.NewUnit(this);
        }
Example #7
0
        public InterpreterScope(Namespace ns, Node ast, InterpreterScope outerScope)
        {
            _ns = ns;
            _node = ast;
            OuterScope = outerScope;

            _nodeScopes = new Dictionary<Node, InterpreterScope>();
            _nodeValues = new Dictionary<Node, INamespaceSet>();
            _variables = new Dictionary<string, VariableDef>();

            #if DEBUG
            NodeScopes = new ReadOnlyDictionary<Node, InterpreterScope>(_nodeScopes);
            NodeValues = new ReadOnlyDictionary<Node, INamespaceSet>(_nodeValues);
            Variables = new ReadOnlyDictionary<string, VariableDef>(_variables);
            #endif
        }
Example #8
0
 public virtual InterpreterScope AddNodeScope(Node node, InterpreterScope scope)
 {
     return _nodeScopes[node] = scope;
 }
Example #9
0
 internal bool TryGetNodeScope(Node node, out InterpreterScope scope)
 {
     foreach (var s in EnumerateTowardsGlobal) {
         if (s._nodeScopes.TryGetValue(node, out scope)) {
             return true;
         }
     }
     scope = null;
     return false;
 }
Example #10
0
 public ComprehensionScope(Namespace comprehensionResult, Comprehension comprehension, InterpreterScope outerScope)
     : base(comprehensionResult, comprehension, outerScope)
 {
 }
Example #11
0
 public override InterpreterScope AddNodeScope(Node node, InterpreterScope scope)
 {
     return OuterScope.AddNodeScope(node, scope);
 }
Example #12
0
 // TODO: What about names being redefined?
 // remember classes/functions as they start new scopes
 public override bool Walk(ClassDefinition node)
 {
     var cls = AddClass(node, _curUnit);
     if (cls != null) {
         _analysisStack.Push(_curUnit);
         _curUnit = cls.AnalysisUnit;
         Debug.Assert(_scope.EnumerateTowardsGlobal.Contains(cls.AnalysisUnit.Scope.OuterScope));
         _scope = cls.AnalysisUnit.Scope;
         return true;
     }
     return false;
 }
Example #13
0
        public override void PostWalk(DictionaryComprehension node)
        {
            Debug.Assert(_scope is ComprehensionScope);
            _scope = _scope.OuterScope;

            base.PostWalk(node);
        }
Example #14
0
        private void PushIsInstanceScope(Node node, KeyValuePair<NameExpression, Expression>[] isInstanceNames, SuiteStatement effectiveSuite)
        {
            InterpreterScope scope;
            if (!_curUnit.Scope.TryGetNodeScope(node, out scope)) {
                if (_scope is IsInstanceScope) {
                    // Reuse the current scope
                    _curUnit.Scope.AddNodeScope(node, _scope);
                    return;
                }

                // find our parent scope, it may not be just the last entry in _scopes
                // because that can be a StatementScope and we would start a new range.
                var declScope = _scope.EnumerateTowardsGlobal.FirstOrDefault(s => !(s is StatementScope));

                scope = new IsInstanceScope(node.StartIndex, effectiveSuite, declScope);

                declScope.Children.Add(scope);
                declScope.AddNodeScope(node, scope);
                _scope = scope;
            }
        }
Example #15
0
 public StatementScope(int index, InterpreterScope outerScope)
     : base(null, outerScope)
 {
     _startIndex = _endIndex = index;
 }
Example #16
0
        private IEnumerable<IAnalysisVariable> GetVariablesInScope(NameExpression name, InterpreterScope scope)
        {
            foreach (var res in scope.GetMergedVariables(name.Name).SelectMany(ToVariables)) {
                yield return res;
            }

            // if a variable is imported from another module then also yield the defs/refs for the
            // value in the defining module.
            var linked = scope.GetLinkedVariablesNoCreate(name.Name);
            if (linked != null) {
                foreach (var linkedVar in linked) {
                    foreach (var res in ToVariables(linkedVar)) {
                        yield return res;
                    }
                }
            }

            var classScope = scope as ClassScope;
            if (classScope != null) {
                // if the member is defined in a base class as well include the base class member and references
                var cls = classScope.Class;
                if (cls.Push()) {
                    try {
                        foreach (var baseNs in cls.Bases.SelectMany(c => c)) {
                            if (baseNs.Push()) {
                                try {
                                    ClassInfo baseClassNs = baseNs as ClassInfo;
                                    if (baseClassNs != null) {
                                        foreach (var res in baseClassNs.Scope.GetMergedVariables(name.Name).SelectMany(ToVariables)) {
                                            yield return res;
                                        }
                                    }
                                } finally {
                                    baseNs.Pop();
                                }
                            }
                        }
                    } finally {
                        cls.Pop();
                    }
                }
            }
        }
Example #17
0
        /// <summary>
        /// Makes sure we create a scope for a comprehension (generator, set, dict, or list comprehension in 3.x) where
        /// the variables which are assigned will be stored.  
        /// </summary>
        private void EnsureComprehensionScope(Comprehension node, Func<Comprehension, ComprehensionScope> makeScope)
        {
            InterpreterScope scope, declScope = _scope;
            if (!declScope.TryGetNodeScope(node, out scope)) {
                scope = makeScope(node);

                declScope.AddNodeScope(node, scope);
                declScope.Children.Add(scope);
            }
            _scope = scope;
        }
Example #18
0
        internal static FunctionInfo AddFunction(FunctionDefinition node, AnalysisUnit outerUnit, InterpreterScope prevScope)
        {
            InterpreterScope scope;
            if (!prevScope.TryGetNodeScope(node, out scope)) {
                if (node.Body == null || node.Name == null) {
                    return null;
                }

                var func = new FunctionInfo(node, outerUnit, prevScope);
                var unit = func.AnalysisUnit;
                scope = unit.Scope;

                prevScope.Children.Add(scope);
                prevScope.AddNodeScope(node, scope);

                if (!node.IsLambda && node.Name != "<genexpr>") {
                    // lambdas don't have their names published

                    var funcVar = prevScope.AddLocatedVariable(node.Name, node.NameExpression, unit);
                    // Decorated functions don't have their type set yet
                    if (node.Decorators == null) {
                        funcVar.AddTypes(unit, func.SelfSet);
                    }
                }

                unit.Enqueue();
            }
            return scope.Namespace as FunctionInfo;
        }
Example #19
0
        public override bool Walk(SuiteStatement node)
        {
            var prevSuite = _curSuite;
            _curSuite = node;

            // recursively walk the statements in the suite
            if (node.Statements != null) {
                foreach (var innerNode in node.Statements) {
                    innerNode.Walk(this);
                }
            }

            _curSuite = prevSuite;

            // then check if we encountered an assert which added an isinstance scope.
            IsInstanceScope isInstanceScope = _scope as IsInstanceScope;
            if (isInstanceScope != null && isInstanceScope._effectiveSuite == node) {
                // pop the isinstance scope
                _scope = _scope.OuterScope;
                var declScope = _curUnit.Scope;
                // transform back into a line number and start the new statement scope on the line
                // after the suite statement.
                var lineNo = _entry.Tree.IndexToLocation(node.EndIndex).Line;

                int offset;
                if (_entry.Tree._lineLocations.Length == 0) {
                    // single line input
                    offset = 0;
                } else {
                    offset = lineNo < _entry.Tree._lineLocations.Length ? _entry.Tree._lineLocations[lineNo] : _entry.Tree._lineLocations[_entry.Tree._lineLocations.Length - 1];
                }
                var closingScope = new StatementScope(offset, declScope);
                _scope = closingScope;
                declScope.Children.Add(closingScope);
            }
            return false;
        }
Example #20
0
 public override bool Walk(FunctionDefinition node)
 {
     var function = AddFunction(node, _curUnit);
     if (function != null) {
         _analysisStack.Push(_curUnit);
         _curUnit = function.AnalysisUnit;
         Debug.Assert(_scope.EnumerateTowardsGlobal.Contains(function.AnalysisUnit.Scope.OuterScope));
         _scope = function.AnalysisUnit.Scope;
         return true;
     }
     return false;
 }
Example #21
0
 public InterpreterScope(Namespace ns, InterpreterScope outerScope)
     : this(ns, null, outerScope)
 {
 }
Example #22
0
 INamespaceSet IModule.GetModuleMember(Node node, AnalysisUnit unit, string name, bool addRef, InterpreterScope linkedScope, string linkedName)
 {
     var res = NamespaceSet.Empty;
     foreach (var member in _members.OfType<IModule>()) {
         res = res.Union(member.GetModuleMember(node, unit, name, addRef, linkedScope, linkedName));
     }
     return res;
 }
Example #23
0
 /// <summary>
 /// Finds the best available analysis unit for lookup. This will be the one that is provided
 /// by the nearest enclosing scope that is capable of providing one.
 /// </summary>
 private AnalysisUnit GetNearestEnclosingAnalysisUnit(InterpreterScope scopes)
 {
     var units = from scope in scopes.EnumerateTowardsGlobal
                 let ns = scope.Namespace
                 where ns != null
                 let unit = ns.AnalysisUnit
                 where unit != null
                 select unit;
     return units.FirstOrDefault() ?? _unit;
 }
Example #24
0
 public override void PostWalk(SuiteStatement node)
 {
     while (_scope is StatementScope) {
         _scope = _scope.OuterScope;
     }
     base.PostWalk(node);
 }
Example #25
0
 public IsInstanceScope(int startIndex, SuiteStatement effectiveSuite, InterpreterScope outerScope)
     : base(null, null, outerScope)
 {
     _startIndex = _endIndex = startIndex;
     _effectiveSuite = effectiveSuite;
 }
Example #26
0
        private IEnumerable<MemberResult> GetKeywordMembers(GetMemberOptions options, InterpreterScope scope)
        {
            if (options.ExpressionKeywords()) {
                // keywords available in any context
                yield return new MemberResult("and", JMemberType.Keyword);
                yield return new MemberResult("as", JMemberType.Keyword);
                yield return new MemberResult("else", JMemberType.Keyword);
                yield return new MemberResult("for", JMemberType.Keyword);
                yield return new MemberResult("if", JMemberType.Keyword);
                yield return new MemberResult("in", JMemberType.Keyword);
                yield return new MemberResult("is", JMemberType.Keyword);
                yield return new MemberResult("lambda", JMemberType.Keyword);
                yield return new MemberResult("not", JMemberType.Keyword);
                yield return new MemberResult("or", JMemberType.Keyword);
            }

            bool isStmtContext = options.StatementKeywords();

            // and now the keywords...
            if (scope is FunctionScope) {
                if (isStmtContext) {
                    yield return new MemberResult("return", JMemberType.Keyword);
                }

                // yield is always available as an expression in 2.5+
                yield return new MemberResult("yield", JMemberType.Keyword);
            }

            if (isStmtContext) {
                // statement context only
                yield return new MemberResult("assert", JMemberType.Keyword);
                yield return new MemberResult("break", JMemberType.Keyword);
                yield return new MemberResult("continue", JMemberType.Keyword);
                yield return new MemberResult("class", JMemberType.Keyword);
                yield return new MemberResult("def", JMemberType.Keyword);
                yield return new MemberResult("del", JMemberType.Keyword);
                yield return new MemberResult("elif", JMemberType.Keyword);
                yield return new MemberResult("except", JMemberType.Keyword);
                yield return new MemberResult("finally", JMemberType.Keyword);
                yield return new MemberResult("from", JMemberType.Keyword);
                yield return new MemberResult("global", JMemberType.Keyword);
                yield return new MemberResult("import", JMemberType.Keyword);
                yield return new MemberResult("pass", JMemberType.Keyword);
                yield return new MemberResult("raise", JMemberType.Keyword);
                yield return new MemberResult("try", JMemberType.Keyword);
                yield return new MemberResult("while", JMemberType.Keyword);
                yield return new MemberResult("with", JMemberType.Keyword);
            }

            if (ProjectState.LanguageVersion.Is7x()) {
                yield return new MemberResult("False", JMemberType.Keyword);
                yield return new MemberResult("None", JMemberType.Keyword);
                yield return new MemberResult("True", JMemberType.Keyword);

                // statement context only
                if (isStmtContext) {
                    yield return new MemberResult("nonlocal", JMemberType.Keyword);
                }
            }

            if (ProjectState.LanguageVersion.Is6x() && isStmtContext) {
                // statement context only
                yield return new MemberResult("exec", JMemberType.Keyword);
                yield return new MemberResult("print", JMemberType.Keyword);
            }
        }
Example #27
0
 public override void PostWalk(ListComprehension node)
 {
     if (_curUnit.ProjectState.LanguageVersion.Is7x()) {
         Debug.Assert(_scope is ComprehensionScope);
         _scope = _scope.OuterScope;
     }
     base.PostWalk(node);
 }
Example #28
0
 public ClassScope(ClassInfo classInfo, ClassDefinition ast, InterpreterScope outerScope)
     : base(classInfo, ast, outerScope)
 {
     classInfo.Scope = this;
 }
Example #29
0
        // Returns False if no more parameters can be updated for this unit.
        private bool UpdateSingleDefaultParameter(AnalysisUnit unit, InterpreterScope scope, int index, IParameterInfo info)
        {
            if (index >= FunctionDefinition.Parameters.Count) {
                return false;
            }
            VariableDef param;
            var name = FunctionDefinition.Parameters[index].Name;
            if (scope.Variables.TryGetValue(name, out param)) {
                var ns = ProjectState.GetNamespaceFromObjects(info.ParameterType);

                if ((info.IsParamArray && !(param is ListParameterVariableDef)) ||
                    (info.IsKeywordDict && !(param is DictParameterVariableDef))) {
                    return false;
                }

                param.AddTypes(unit, ns);
            }

            return true;
        }
Example #30
0
        private static bool IsFirstLineOfFunction(InterpreterScope innerScope, InterpreterScope outerScope, int index)
        {
            if (innerScope.OuterScope == outerScope && innerScope is FunctionScope) {
                var funcScope = (FunctionScope)innerScope;
                var def = funcScope.Function.FunctionDefinition;

                // TODO: Use indexes rather than lines to check location
                int lineNo = def.GlobalParent.IndexToLocation(index).Line;
                if (lineNo == def.GetStart(def.GlobalParent).Line) {
                    return true;
                }
            }
            return false;
        }