Esempio n. 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.Name, true, out variable))
                {
                    return(variable);
                }
            }

            return(null);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        internal PythonReference Reference(string /*!*/ name)
        {
            if (_references == null)
            {
                _references = new Dictionary <string, List <PythonReference> >(StringComparer.Ordinal);
            }

            if (!_references.TryGetValue(name, out var references))
            {
                _references[name] = references = new List <PythonReference>();
            }
            var reference = new PythonReference(name);

            references.Add(reference);
            return(reference);
        }
Esempio n. 4
0
        // 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);
        }
Esempio n. 5
0
 // 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;
 }
Esempio n. 6
0
 // 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;
 }
Esempio n. 7
0
                internal bool WalkName(NameExpression node, PythonReference reference) {
                    if (_collector.ReadFromExtractedCode(reference.Variable)) {
                        if ((!_collector._inputCollector._allReads.Contains(reference) &&
                            !_collector._inputCollector._allWrites.Contains(reference))) {

                            // the variable is assigned outside the refactored code
                            if (node.StartIndex < _collector._target.StartIncludingIndentation) {
                                // it's assigned before the extracted code
                                _collector._inputVars.Add(reference.Variable);
                            } else {
                                Debug.Assert(node.EndIndex > _collector._target.End);
                                // it's assigned afterwards, we don't care...
                            }
                        } else if (_collector._readBeforeInitialized.Contains(reference.Variable) &&
                            _collector._inputCollector._allWrites.Contains(reference) &&
                            _collector._inLoop) {
                            // we read an un-initialized value, so it needs to be passed in.  If we
                            // write to it as well then we need to pass it back out for future calls.
                            _collector._outputVars.Add(reference.Variable);
                        }
                    }

                    return false;
                }
Esempio n. 8
0
 internal PythonReference Reference(string/*!*/ name) {
     if (_references == null) {
         _references = new Dictionary<string, List<PythonReference>>(StringComparer.Ordinal);
     }
     List<PythonReference> references;
     if (!_references.TryGetValue(name, out references)) {
         _references[name] = references = new List<PythonReference>();
     }
     var reference = new PythonReference(name);
     references.Add(reference);
     return reference;
 }
Esempio n. 9
0
 internal override PythonVariable BindReference(PythonNameBinder binder, PythonReference reference)
 {
     return(EnsureVariable(reference.Name));
 }
Esempio n. 10
0
 internal abstract PythonVariable BindReference(PythonNameBinder binder, PythonReference reference);