Represents a reference to a name. A PythonReference is created for each name referred to in a scope (global, class, or function).
Пример #1
0
        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);
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
        }
Пример #5
0
 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));
 }
Пример #6
0
 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;
 }
Пример #7
0
        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);
        }
Пример #8
0
        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));
        }
Пример #9
0
        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));
        }
Пример #10
0
        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));
        }
Пример #11
0
        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);
            }
        }
Пример #12
0
        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);
        }
Пример #13
0
 internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference)
 {
     return(EnsureVariable(reference.Name));
 }
Пример #14
0
 internal PythonReferenceVariable(PythonReference reference, ScopeStatement scope)
     : base(reference.Name, VariableKind.Nonlocal, scope)
 {
     Reference = reference;
 }
Пример #15
0
 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;
 }
Пример #16
0
 internal abstract PythonVariable BindReference(PythonNameBinder binder, PythonReference reference);
Пример #17
0
 internal abstract PythonVariable BindReference(PythonNameBinder binder, PythonReference reference);
Пример #18
0
 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;
 }
Пример #19
0
 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;
 }
Пример #20
0
        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;
        }
Пример #21
0
        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;
        }
Пример #22
0
        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;
            }
        }
Пример #23
0
 internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) {
     return EnsureVariable(reference.Name);
 }
 internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference)
 {
     throw new InvalidOperationException();
 }
Пример #25
0
 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);
 }
Пример #26
0
        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);
        }
Пример #27
0
        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;
        }
Пример #28
0
 internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference) {
     throw new InvalidOperationException();
 }