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; }
// 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); }
private void PopScope() { _scopes.Add(_currentScope); _currentScope = _currentScope.Parent; _finallyCount.RemoveAt(_finallyCount.Count - 1); }
private void PushScope(ScopeStatement node) { node.Parent = _currentScope; _currentScope = node; _finallyCount.Add(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); }
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); }
public SelectionTarget(Dictionary<ScopeStatement, int> insertLocations, ScopeStatement[] parents) { _parents = parents; _insertLocations = insertLocations; }
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; }
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; }
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; }
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; }
public ExtractMethodRequest(ScopeStatement targetScope, string name, string[] parameters) { _name = name; _parameters = parameters; _targetScope = targetScope; }
private string GetScopeType(ScopeStatement scope) { if (scope is ClassDefinition) { return "class"; } else if (scope is FunctionDefinition) { return "function"; } return "global"; }
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(); }
public static void Check(ScopeStatement scope) { if (scope.ScopeVariables != null) { FlowChecker fc = new FlowChecker(scope); scope.Walk(fc); } }
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); }
public ImportWalker(ScopeStatement targetStmt) { _targetStmt = targetStmt; }
public CoverageScope(ScopeStatement node) { Statement = node; }
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; }
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; }
public NodeTarget(Dictionary<ScopeStatement, int> insertLocations, ScopeStatement[] parents, Node node) : base(insertLocations, parents) { _node = node; }
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; }