internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) { PythonVariable variable; // Python semantics: The variables bound local in the class // scope are accessed by name - the dictionary behavior of classes if (TryGetVariable(reference.Name, out variable)) { // TODO: This results in doing a dictionary lookup to get/set the local, // when it should probably be an uninitialized check / global lookup for gets // and a direct set if (variable.Kind == VariableKind.Global) { AddReferencedGlobal(reference.Name); } else if (variable.Kind == VariableKind.Local) { return(null); } return(variable); } // Try to bind in outer scopes, if we have an unqualified exec we need to leave the // variables as free for the same reason that locals are accessed by name. for (ScopeStatement parent = Parent; parent != null; parent = parent.Parent) { if (parent.TryBindOuter(this, reference, out variable)) { return(variable); } } return(null); }
internal override bool TryBindOuter(ScopeStatement from, PythonReference reference, out PythonVariable variable) { // Functions expose their locals to direct access if (TryGetVariable(reference.Name, out variable) && variable.Kind != VariableKind.Nonlocal) { 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); ContainsNestedFreeVariables = true; } else { from.AddReferencedGlobal(reference.Name); } return(true); } return(false); }
internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) { PythonVariable variable; // First try variables local to this scope if (TryGetVariable(reference.Name, out variable)) { if (variable.Kind == VariableKind.Global) { AddReferencedGlobal(reference.Name); } if (variable.Kind != VariableKind.Nonlocal) { return(variable); } } // Try to bind in outer scopes bool stopAtGlobal = variable?.Kind == VariableKind.Nonlocal; for (ScopeStatement parent = Parent; parent != null && !(stopAtGlobal && parent.IsGlobal); parent = parent.Parent) { if (parent.TryBindOuter(this, reference, out variable)) { return(variable); } } return(null); }
internal override bool TryBindOuter(ScopeStatement from, PythonReference reference, out PythonVariable variable) { ContainsNestedFreeVariables = true; if (TryGetVariable(reference.Name, out variable)) { Debug.Assert(variable.Kind != VariableKind.Nonlocal, "there should be no nonlocals in a comprehension"); 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 { from.AddReferencedGlobal(reference.Name); } return(true); } return(false); }
internal override bool TryBindOuter(ScopeStatement from, PythonReference reference, out PythonVariable variable) { if (reference.Name == "__class__") { variable = from.EnsureVariable(reference.Name); return(true); } return(base.TryBindOuter(from, reference, out variable)); }
internal PythonReference Reference(string name) { if (_references == null) { _references = new Dictionary<string, PythonReference>(StringComparer.Ordinal); } PythonReference reference; if (!_references.TryGetValue(name, out reference)) { _references[name] = reference = new PythonReference(name); } return reference; }
private PythonVariable CreateNonlocalVariable(string name) { EnsureVariables(); Debug.Assert(!Variables.ContainsKey(name)); Debug.Assert(!IsReferenced(name)); PythonReference reference = Reference(name); PythonVariable variable; Variables[name] = variable = new PythonReferenceVariable(reference, this); return(variable); }
internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) { if (TryGetVariable(reference.Name, out PythonVariable variable)) { if (variable.Kind == VariableKind.Global) { AddReferencedGlobal(reference.Name); } return(variable); } // then bind in our parent scope return(_comprehension.Parent.BindReference(binder, reference)); }
internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) { if (TryGetVariable(reference.Name, out PythonVariable variable)) { if (variable.Kind == VariableKind.Global) { AddReferencedGlobal(reference.Name); } Debug.Assert(variable.Kind != VariableKind.Nonlocal, "there should be no nonlocals in a comprehension"); return(variable); } // then bind in our parent scope return(Parent.BindReference(binder, reference)); }
internal override bool TryBindOuter(ScopeStatement from, PythonReference reference, out PythonVariable variable) { if (reference.Name == "__class__") { needClassCell = true; ClassCellVariable = EnsureVariable("__classcell__"); ClassVariable = variable = EnsureVariable(reference.Name); variable.AccessedInNestedScope = true; from.AddFreeVariable(variable, true); for (ScopeStatement scope = from.Parent; scope != this; scope = scope.Parent) { scope.AddFreeVariable(variable, false); } AddCellVariable(variable); return(true); } return(base.TryBindOuter(from, reference, out variable)); }
internal override bool TryBindOuter(ScopeStatement from, PythonReference reference, out PythonVariable variable) { // Unbound variable from.AddReferencedGlobal(reference.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(reference.Name); return(true); } }
internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) { // First try variables local to this scope if (TryGetVariable(reference.Name, out PythonVariable variable)) { if (variable.Kind == VariableKind.Global) { AddReferencedGlobal(reference.Name); } Debug.Assert(variable.Kind != VariableKind.Nonlocal, "there should be no nonlocals in a comprehension"); return(variable); } // then try to bind in outer scopes for (ScopeStatement parent = Parent; parent != null; parent = parent.Parent) { if (parent.TryBindOuter(this, reference, out variable)) { return(variable); } } return(null); }
internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) { return(EnsureVariable(reference.Name)); }
internal PythonReferenceVariable(PythonReference reference, ScopeStatement scope) : base(reference.Name, VariableKind.Nonlocal, scope) { Reference = reference; }
internal virtual bool TryBindOuter(ScopeStatement from, PythonReference reference, out PythonVariable variable) { // Hide scope contents by default (only functions expose their locals) variable = null; return false; }
internal abstract PythonVariable BindReference(PythonNameBinder binder, PythonReference reference);
internal abstract PythonVariable BindReference(PythonNameBinder binder, PythonReference reference);
internal PythonReference Reference(string name) { if (_references == null) { _references = new Dictionary<string, PythonReference>(StringComparer.Ordinal); } PythonReference reference; if (!_references.TryGetValue(name, out reference)) { _references[name] = reference = new PythonReference(name); } return reference; }
internal PythonReference Reference(SymbolId name) { if (_references == null) { _references = new Dictionary<SymbolId, PythonReference>(); } PythonReference reference; if (!_references.TryGetValue(name, out reference)) { _references[name] = reference = new PythonReference(name); } return reference; }
internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) { PythonVariable variable; // First try variables local to this scope if (TryGetVariable(reference.Name, out variable)) { if (variable.Kind == VariableKind.Global) { AddReferencedGlobal(reference.Name); } return variable; } // Try to bind in outer scopes for (ScopeStatement parent = Parent; parent != null; parent = parent.Parent) { if (parent.TryBindOuter(this, reference, out variable)) { return variable; } } return null; }
internal override bool TryBindOuter(ScopeStatement from, PythonReference reference, out PythonVariable variable) { // Functions expose their locals to direct access ContainsNestedFreeVariables = true; if (TryGetVariable(reference.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 { from.AddReferencedGlobal(reference.Name); } return true; } return false; }
internal override bool TryBindOuter(ScopeStatement from, PythonReference reference, out PythonVariable variable) { // Unbound variable from.AddReferencedGlobal(reference.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(reference.Name); return true; } }
internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) { return EnsureVariable(reference.Name); }
internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) { throw new InvalidOperationException(); }
internal virtual bool TryBindOuter(ScopeStatement from, PythonReference reference, out PythonVariable variable) { // Hide scope contents by default (only functions expose their locals) variable = null; return(false); }
internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) { PythonVariable variable; if (TryGetVariable(reference.Name, out variable)) { if (variable.Kind == VariableKind.Global) { AddReferencedGlobal(reference.Name); } return variable; } // then bind in our parent scope return _comprehension.Parent.BindReference(binder, reference); }
internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) { PythonVariable variable; // Python semantics: The variables bound local in the class // scope are accessed by name - the dictionary behavior of classes if (TryGetVariable(reference.Name, out variable)) { // TODO: This results in doing a dictionary lookup to get/set the local, // when it should probably be an uninitialized check / global lookup for gets // and a direct set if (variable.Kind == VariableKind.Global) { AddReferencedGlobal(reference.Name); } else if (variable.Kind == VariableKind.Local) { return null; } return variable; } // Try to bind in outer scopes, if we have an unqualified exec we need to leave the // variables as free for the same reason that locals are accessed by name. for (ScopeStatement parent = Parent; parent != null; parent = parent.Parent) { if (parent.TryBindOuter(this, reference, out variable)) { return variable; } } return null; }
internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) { throw new InvalidOperationException(); }