// ImportStatement public override bool Walk(ImportStatement node) { var variables = new PythonVariable[node.Names.Count]; PythonReference[] references = null; if (BindReferences) { references = new PythonReference[variables.Length]; } for (var i = 0; i < node.Names.Count; i++) { string name; if (node.AsNames[i] != null) { name = node.AsNames[i].Name; } else if (node.Names[i].Names.Count > 0) { name = node.Names[i].Names[0].Name; } else { name = null; } if (name != null) { variables[i] = DefineName(name); if (references != null) { references[i] = Reference(name); } } } node.Variables = variables; node.AddVariableReference(_ast, BindReferences, references); return(true); }
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); }
// ImportStatement public override bool Walk(ImportStatement node) { PythonVariable[] variables = new PythonVariable[node.Names.Count]; PythonReference[] references = null; if (_bindRefs) { references = new PythonReference[variables.Length]; } for (int i = 0; i < node.Names.Count; i++) { string name; if(node.AsNames[i] != null) { name = node.AsNames[i].Name; } else if (node.Names[i].Names.Count > 0) { name = node.Names[i].Names[0].Name; } else { name = null; } if (name != null) { variables[i] = DefineName(name); if (references != null) { references[i] = Reference(name); } } } node.Variables = variables; node.AddVariableReference(_ast, _bindRefs, references); return true; }
public ClosureInfo(PythonVariable variable, bool accessedInScope) { Variable = variable; AccessedInScope = accessedInScope; }
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); }
private void SetInitialized(PythonVariable /*!*/ variable, bool value) { _bits.Set(variable.Index * 2 + 1, value); }
public bool TryGetVariable(string name, out PythonVariable variable) { if (_variables != null && name != null) { return _variables.TryGetValue(name, out variable); } else { variable = null; return false; } }
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 void AddCellVariable(PythonVariable variable) { if (_cellVars == null) { _cellVars = new List<string>(); } if (!_cellVars.Contains(variable.Name)) { _cellVars.Add(variable.Name); } }
internal abstract bool ExposesLocalVariable(PythonVariable variable);
internal void AddFreeVariable(PythonVariable variable, bool accessedInScope) { if (_freeVars == null) { _freeVars = new List<PythonVariable>(); } if (!_freeVars.Contains(variable)) { _freeVars.Add(variable); } }
private bool IsInitialized(PythonVariable /*!*/ variable) { return(_bits.Get(variable.Index * 2 + 1)); }
private bool IsAssigned(PythonVariable /*!*/ variable) { return(_bits.Get(variable.Index * 2)); }
// GlobalStatement public override bool Walk(GlobalStatement node) { foreach (NameExpression nameNode in node.Names) { string n = nameNode.Name; if (n == null) { continue; } PythonVariable conflict; // Check current scope for conflicting variable bool assignedGlobal = false; if (_currentScope.TryGetVariable(n, out conflict)) { // conflict? switch (conflict.Kind) { case VariableKind.Global: case VariableKind.Local: assignedGlobal = true; ReportSyntaxWarning( String.Format( System.Globalization.CultureInfo.InvariantCulture, "name '{0}' is assigned to before global declaration", n ), node ); break; case VariableKind.Parameter: ReportSyntaxError( String.Format( System.Globalization.CultureInfo.InvariantCulture, "Name '{0}' is a function parameter and declared global", n), node); break; } } // Check for the name being referenced previously. If it has been, issue warning. if (_currentScope.IsReferenced(n) && !assignedGlobal) { ReportSyntaxWarning( String.Format( System.Globalization.CultureInfo.InvariantCulture, "name '{0}' is used prior to global declaration", n), node); } // Create the variable in the global context and mark it as global PythonVariable variable = _globalScope.EnsureGlobalVariable(n); variable.Kind = VariableKind.Global; if (conflict == null) { // no previously definied variables, add it to the current scope _currentScope.AddVariable(variable); } nameNode.AddVariableReference(_globalScope, _bindRefs, Reference(n)); } return(true); }
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; }
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); }
private static bool HasClosureVariable(List<ClosureInfo> closureVariables, PythonVariable variable) { if (closureVariables == null) { return false; } for (int i = 0; i < closureVariables.Count; i++) { if (closureVariables[i].Variable == variable) { return true; } } return false; }
internal override bool ExposesLocalVariable(PythonVariable variable) { return(NeedsLocalsDictionary); }
internal void AddVariable(PythonVariable variable) { EnsureVariables(); _variables[variable.Name] = variable; }
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 PythonVariable/*!*/ CreateVariable(string name, VariableKind kind) { EnsureVariables(); PythonVariable variable; _variables[name] = variable = new PythonVariable(name, kind, this); return variable; }
private bool ReadBeforeInitialized(PythonVariable variable) { return _readBeforeInitialized.Contains(variable); }
// FromImportStatement public override bool Walk(FromImportStatement node) { if (node.Names.Count != 1 || node.Names[0].Name !="*") { PythonVariable[] variables = new PythonVariable[node.Names.Count]; PythonReference[] references = null; if (_bindRefs) { references = new PythonReference[node.Names.Count]; } for (int i = 0; i < node.Names.Count; i++) { variables[i] = DefineName(node.AsNames[i] != null ? node.AsNames[i].Name : node.Names[i].Name); if (references != null) { references[i] = Reference(variables[i].Name); } } node.Variables = variables; node.AddVariableReference(_ast, _bindRefs, references); } else { Debug.Assert(_currentScope != null); _currentScope.ContainsImportStar = true; _currentScope.NeedsLocalsDictionary = true; _currentScope.HasLateBoundVariableSets = true; } return true; }
private void SetAssigned(PythonVariable/*!*/ variable, bool value) { _bits.Set(_variableIndices[variable] * 2, value); }
internal override bool ExposesLocalVariable(PythonVariable variable) { return true; }
private void SetInitialized(PythonVariable/*!*/ variable, bool value) { _bits.Set(_variableIndices[variable] * 2 + 1, value); }
private static int CompareVariables(PythonVariable left, PythonVariable right) { return String.Compare(left.Name, right.Name); }
internal bool IsAssigned(PythonVariable/*!*/ variable) { return _bits.Get(_variableIndices[variable] * 2); }
internal override bool ExposesLocalVariable(PythonVariable variable) { return(true); }
internal bool IsInitialized(PythonVariable/*!*/ variable) { return _bits.Get(_variableIndices[variable] * 2 + 1); }
internal override bool ExposesLocalVariable(PythonVariable variable) { return NeedsLocalsDictionary; }
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 static void AddVariable(this Parameter node, PythonAst ast, bool bindNames, PythonVariable variable) { if (bindNames) { ast.SetAttribute(node, Variable, variable); } }
private bool ReadFromExtractedCode(PythonVariable variable) { return _readBeforeInitialized.Contains(variable) && _inputCollector._allReadVariables.Contains(variable); }
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); }
private void SetAssigned(PythonVariable /*!*/ variable, bool value) { _bits.Set(variable.Index * 2, value); }