Esempio n. 1
0
 public ExtractedMethodCreator(PythonAst ast, ScopeStatement[] scopes, HashSet<PythonVariable> inputVariables, HashSet<PythonVariable> outputVariables, SelectionTarget target, int indentSize, bool insertTabs, string newline) {
     _ast = ast;
     _scopes = scopes;
     _inputVars = new List<PythonVariable>(inputVariables);
     _outputVars = new List<PythonVariable>(outputVariables);
     _inputVars.Sort(CompareVariables);
     _outputVars.Sort(CompareVariables);
     _target = target;
     _indentSize = indentSize;
     _insertTabs = insertTabs;
     _newline = newline;
 }
Esempio n. 2
0
 // PythonAst
 public override void PostWalk(PythonAst node) {
     // Do not add the global suite to the list of processed nodes,
     // the publishing must be done after the class local binding.
     Debug.Assert(_currentScope == node);
     _currentScope = _currentScope.Parent;
     _finallyCount.RemoveAt(_finallyCount.Count - 1);
 }
Esempio n. 3
0
 private void PopScope() {
     _scopes.Add(_currentScope);
     _currentScope = _currentScope.Parent;
     _finallyCount.RemoveAt(_finallyCount.Count - 1);
 }
Esempio n. 4
0
 private void PushScope(ScopeStatement node) {
     node.Parent = _currentScope;
     _currentScope = node;
     _finallyCount.Add(0);
 }
Esempio n. 5
0
        private void Bind(PythonAst unboundAst) {
            _currentScope = _globalScope = unboundAst;
            _finallyCount.Add(0);

            // Find all scopes and variables
            unboundAst.Walk(this);

            // Bind
            foreach (ScopeStatement scope in _scopes) {
                scope.Bind(this);
            }

            // Finish the globals
            unboundAst.Bind(this);

            // Finish Binding w/ outer most scopes first.
            for (int i = _scopes.Count - 1; i >= 0; i--) {
                _scopes[i].FinishBind(this);
            }

            // Finish the globals
            unboundAst.FinishBind(this);
        }
Esempio n. 6
0
        public FlowChecker(ScopeStatement scope) {
            var scopeVars = scope.ScopeVariables;

            _variables = new Dictionary<string, PythonVariable>(scopeVars.Count);
            foreach (var scopeVar in scopeVars) {
                _variables[scopeVar.Name] = scopeVar;
            }

            _bits = new BitArray(_variables.Count * 2);
            int index = 0;
            _variableIndices = new Dictionary<PythonVariable, int>(_variables.Count);
            foreach (var binding in _variables) {
                _variableIndices[binding.Value] = index++;
            }
            _scope = scope;
            _fdef = new FlowDefiner(this);
            _fdel = new FlowDeleter(this);
        }
Esempio n. 7
0
 public SelectionTarget(Dictionary<ScopeStatement, int> insertLocations, ScopeStatement[] parents) {
     _parents = parents;
     _insertLocations = insertLocations;
 }
Esempio n. 8
0
        internal override bool TryBindOuter(ScopeStatement from, string name, bool allowGlobals, out PythonVariable variable) {
            // Functions expose their locals to direct access
            ContainsNestedFreeVariables = true;
            if (TryGetVariable(name, out variable)) {
                variable.AccessedInNestedScope = true;

                if (variable.Kind == VariableKind.Local || variable.Kind == VariableKind.Parameter) {
                    from.AddFreeVariable(variable, true);

                    for (ScopeStatement scope = from.Parent; scope != this; scope = scope.Parent) {
                        scope.AddFreeVariable(variable, false);
                    }

                    AddCellVariable(variable);
                } else if(allowGlobals) {
                    from.AddReferencedGlobal(name);
                }
                return true;
            }
            return false;
        }
Esempio n. 9
0
        internal override bool TryBindOuter(ScopeStatement from, string name, bool allowGlobals, out PythonVariable variable)
        {
            if (allowGlobals) {
                // Unbound variable
                from.AddReferencedGlobal(name);

                if (from.HasLateBoundVariableSets) {
                    // If the context contains unqualified exec, new locals can be introduced
                    // Therefore we need to turn this into a fully late-bound lookup which
                    // happens when we don't have a PythonVariable.
                    variable = null;
                    return false;
                } else {
                    // Create a global variable to bind to.
                    variable = EnsureGlobalVariable(name);
                    return true;
                }
            }
            variable = null;
            return false;
        }
Esempio n. 10
0
        internal static string GetQualifiedFunctionName(ScopeStatement statement) {
            if (statement is PythonAst || statement is ClassDefinition) {
                return null;
            }

            var baseName = GetQualifiedFunctionName(statement.Parent);

            if (baseName == null) {
                return statement.Name;
            }

            return baseName + "." + statement.Name;
        }
Esempio n. 11
0
        internal static string GetQualifiedName(ScopeStatement statement) {
            if (statement is PythonAst) {
                return null;
            }

            var baseName = GetQualifiedName(statement.Parent);

            if (baseName == null) {
                return statement.Name;
            }

            return baseName + "." + statement.Name;
        }
Esempio n. 12
0
 public ExtractMethodRequest(ScopeStatement targetScope, string name, string[] parameters) {
     _name = name;
     _parameters = parameters;
     _targetScope = targetScope;
 }
Esempio n. 13
0
        private string GetScopeType(ScopeStatement scope) {
            if (scope is ClassDefinition) {
                return "class";
            } else if (scope is FunctionDefinition) {
                return "function";
            }

            return "global";
        }
Esempio n. 14
0
        private string[] GetScopeVariables(ScopeStatement scope, HashSet<PythonVariable> inputVars) {
            List<string> res = new List<string>();
            foreach (var variable in inputVars) {
                var variableScope = variable.Scope;

                var parentScope = scope;
                // are these variables a child of the target scope so we can close over them?
                while (parentScope != null && parentScope != variableScope) {
                    parentScope = parentScope.Parent;
                }

                if (parentScope != null) {
                    res.Add(variable.Name);
                }
            }
            return res.ToArray();
        }
Esempio n. 15
0
 public static void Check(ScopeStatement scope) {
     if (scope.ScopeVariables != null) {
         FlowChecker fc = new FlowChecker(scope);
         scope.Walk(fc);
     }
 }
Esempio n. 16
0
        internal override bool TryBindOuter(ScopeStatement from, string name, bool allowGlobals, out PythonVariable variable) {
            if (name == "__class__" && _classVariable != null) {
                // 3.x has a cell var called __class__ which can be bound by inner scopes
                variable = _classVariable;
                return true;
            }

            return base.TryBindOuter(from, name, allowGlobals, out variable);
        }
Esempio n. 17
0
 public ImportWalker(ScopeStatement targetStmt) {
     _targetStmt = targetStmt;
 }
Esempio n. 18
0
 public CoverageScope(ScopeStatement node) {
     Statement = node;
 }
Esempio n. 19
0
 public SuiteTarget(Dictionary<ScopeStatement, int> insertLocations, ScopeStatement[] parents, SuiteStatement suite, SuiteStatement[] followingSuites, Span selectedSpan, int startIndex, int endIndex)
     : base(insertLocations, parents) {
     _suite = suite;
     _start = startIndex;
     _end = endIndex;
     _followingSuites = followingSuites;
     _selectedSpan = selectedSpan;
 }
Esempio n. 20
0
        private CoverageScope WalkScope(ScopeStatement node) {
            var prevScope = CurScope;
            var newScope = new CoverageScope(node);
            CurScopes.Add(newScope);
            var wasCovered = _blockCovered;
            _blockCovered = null;
            if (node.Body != null) {
                node.Body.Walk(this);
            }

            CurScopes.RemoveAt(CurScopes.Count - 1);
            _blockCovered = wasCovered;
            return newScope;
        }
Esempio n. 21
0
 public NodeTarget(Dictionary<ScopeStatement, int> insertLocations, ScopeStatement[] parents, Node node)
     : base(insertLocations, parents) {
     _node = node;
 }
Esempio n. 22
0
 internal virtual bool TryBindOuter(ScopeStatement from, string name, bool allowGlobals, out PythonVariable variable) {
     // Hide scope contents by default (only functions expose their locals)
     variable = null;
     return false;
 }