Ejemplo n.º 1
0
        public ClosureExpression(Ast.PythonVariable/*!*/ variable, Expression/*!*/ closureCell, ParameterExpression parameter) {
            Assert.NotNull(closureCell);

            _variable = variable;
            _closureCell = closureCell;
            _parameter = parameter;
        }
Ejemplo n.º 2
0
 public override MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal global) {
     return new LookupGlobalVariable(
         globalContext,
         variable.Name,
         variable.Kind == VariableKind.Local
     );
 }
        public PythonGlobalVariableExpression(Expression/*!*/ globalExpr, Ast.PythonVariable/*!*/ variable, PythonGlobal/*!*/ global) {
            Assert.NotNull(globalExpr, variable);

            _target = globalExpr;
            _global = global;
            _variable = variable;
        }
Ejemplo n.º 4
0
        public ClosureExpression(Ast.PythonVariable /*!*/ variable, Expression /*!*/ closureCell, ParameterExpression parameter)
        {
            Assert.NotNull(closureCell);

            _variable    = variable;
            _closureCell = closureCell;
            _parameter   = parameter;
        }
        public PythonGlobalVariableExpression(Expression /*!*/ globalExpr, Ast.PythonVariable /*!*/ variable, PythonGlobal /*!*/ global)
        {
            Assert.NotNull(globalExpr, variable);

            _target   = globalExpr;
            _global   = global;
            _variable = variable;
        }
        internal PythonGlobalVariableExpression(Expression/*!*/ globalExpr, Ast.PythonVariable/*!*/ variable, PythonGlobal/*!*/ global, bool lightEh) {
            Assert.NotNull(globalExpr, variable);

            _target = globalExpr;
            _global = global;
            _variable = variable;
            _lightEh = lightEh;
        }
Ejemplo n.º 7
0
        internal PythonGlobalVariableExpression(Expression /*!*/ globalExpr, Ast.PythonVariable /*!*/ variable, PythonGlobal /*!*/ global, bool lightEh)
        {
            Assert.NotNull(globalExpr, variable);

            _target   = globalExpr;
            _global   = global;
            _variable = variable;
            _lightEh  = lightEh;
        }
        public override MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal global) {
            Assert.NotNull(global);

            return new PythonGlobalVariableExpression(
                Ast.ArrayIndex(
                    PythonAst._globalArray,
                    Ast.Constant(arrayIndex)
                ),
                variable,
                global
            );
        }
Ejemplo n.º 9
0
        // GlobalStatement
        public override bool Walk(GlobalStatement node)
        {
            node.Parent = _currentScope;

            foreach (string n in node.Names)
            {
                PythonVariable conflict;
                // Check current scope for conflicting variable
                if (_currentScope.TryGetVariable(n, out conflict))
                {
                    // conflict?
                    switch (conflict.Kind)
                    {
                    case VariableKind.Global:
                        break;

                    case VariableKind.Local:
                        ReportSyntaxError($"name '{n}' is assigned to before global declaration", node);
                        break;

                    case VariableKind.Parameter:
                        ReportSyntaxError($"name '{n}' is parameter and global", node);
                        break;
                    }
                }

                // Check for the name being referenced previously
                if (_currentScope.IsReferenced(n))
                {
                    ReportSyntaxError($"name '{n}' is used prior to global declaration", 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 defined variables, add it to the current scope
                    _currentScope.AddGlobalVariable(variable);
                }
            }
            return(true);
        }
Ejemplo n.º 10
0
        internal virtual void AddFreeVariable(PythonVariable variable, bool accessedInScope)
        {
            Debug.Assert(variable?.Kind is VariableKind.Local or VariableKind.Parameter);

            if (_freeVars == null)
            {
                _freeVars = new List <PythonVariable>();
            }

            if (!_freeVars.Contains(variable))
            {
                _freeVars.Add(variable);
                if (TryGetVariable(variable.Name, out PythonVariable nonlocal) &&
                    nonlocal.Kind is VariableKind.Nonlocal &&
                    nonlocal.MaybeDeleted)
                {
                    variable.RegisterDeletion();
                }
            }
        }
Ejemplo n.º 11
0
        // FromImportStatement
        public override bool Walk(FromImportStatement node)
        {
            node.Parent = _currentScope;

            if (node.Names != FromImportStatement.Star)
            {
                PythonVariable[] variables = new PythonVariable[node.Names.Count];
                for (int i = 0; i < node.Names.Count; i++)
                {
                    string name = node.AsNames[i] != null ? node.AsNames[i] : node.Names[i];
                    variables[i] = DefineName(name);
                }
                node.Variables = variables;
            }
            else
            {
                Debug.Assert(_currentScope != null);
                _currentScope.ContainsImportStar       = true;
                _currentScope.NeedsLocalsDictionary    = true;
                _currentScope.HasLateBoundVariableSets = true;
            }
            return(true);
        }
Ejemplo n.º 12
0
        internal override Ast GetVariableExpression(PythonVariable variable) {
            if (variable.IsGlobal) {
                return GlobalParent.ModuleVariables[variable];
            }

            Ast expr;
            if (_variableMapping.TryGetValue(variable, out expr)) {
                return expr;
            }

            return _comprehension.Parent.GetVariableExpression(variable);
        }
Ejemplo n.º 13
0
        // GlobalStatement
        public override bool Walk(GlobalStatement node)
        {
            node.Parent = _currentScope;

            foreach (string n in node.Names)
            {
                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.AddGlobalVariable(variable);
                }
            }
            return(true);
        }
Ejemplo n.º 14
0
        public int TupleIndex(PythonVariable var) {
            Debug.Assert(_parent._liftedVars != null);

            var vars = _parent._liftedVars;
            for (int i = 0; i < vars.Count; i++) {
                if (vars[i].PythonVariable == var) {
                    return i;
                }
            }
            
            throw new InvalidOperationException();
        }
Ejemplo n.º 15
0
 private void SetAssigned(PythonVariable/*!*/ variable, bool value) {
     _bits.Set(variable.Index * 2, value);
 }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
0
 private bool IsInitialized(PythonVariable/*!*/ variable) {
     return _bits.Get(variable.Index * 2 + 1);
 }
Ejemplo n.º 18
0
        public MSAst.Expression/*!*/ GetVariable(AstGenerator/*!*/ ag, PythonVariable/*!*/ variable) {
            Assert.NotNull(ag, variable);

            MSAst.Expression res;
            if(_variables.TryGetValue(variable, out res)) {
                return res;
            }

            return ag.LocalLifted[variable];
        }
Ejemplo n.º 19
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;
            }
        }
Ejemplo n.º 20
0
        public MSAst.Expression/*!*/ CreateVariable(AstGenerator/*!*/ ag, PythonVariable/*!*/ variable, bool emitDictionary) {
            Assert.NotNull(ag, variable);

            Debug.Assert(variable.Kind != VariableKind.Parameter);

            string name = SymbolTable.IdToString(variable.Name);
            switch (variable.Kind) {
                case VariableKind.Global:
                case VariableKind.GlobalLocal:
                    return _variables[variable] = GetGlobal(name, ag, false);
                case VariableKind.Local:
                case VariableKind.HiddenLocal:
                    if (ag.IsGlobal) {
                        return _variables[variable] = GetGlobal(name, ag, true);
                    } else if (variable.AccessedInNestedScope || (emitDictionary && variable.Kind != VariableKind.HiddenLocal)) {
                        return ag.SetLocalLiftedVariable(variable, ag.LiftedVariable(variable, name, variable.AccessedInNestedScope));
                    } else {
                        return _variables[variable] = ag.Variable(typeof(object), name);
                    }
                default:
                    throw Assert.Unreachable;
            }
        }
Ejemplo n.º 21
0
        public void SetParameter(PythonVariable/*!*/ variable, MSAst.Expression/*!*/ parameter) {
            Assert.NotNull(variable, parameter);

            _variables[variable] = parameter;
        }
Ejemplo n.º 22
0
 internal override MSAst.Expression GetVariableExpression(PythonVariable variable)
 {
     Debug.Assert(_globalVariables.ContainsKey(variable));
     return(_globalVariables[variable]);
 }
Ejemplo n.º 23
0
 internal override bool TryBindOuter(SymbolId name, out PythonVariable variable) {
     // Functions expose their locals to direct access
     ContainsNestedFreeVariables = true;
     return TryGetVariable(name, out variable);
 }
Ejemplo n.º 24
0
 private bool IsAssigned(PythonVariable/*!*/ variable) {
     return _bits.Get(variable.Index * 2);
 }
Ejemplo n.º 25
0
        // ImportStatement
        public override bool Walk(ImportStatement node)
        {
            node.Parent = _currentScope;

            PythonVariable[] variables = new PythonVariable[node.Names.Count];
            for (int i = 0; i < node.Names.Count; i++) {
                string name = node.AsNames[i] != null ? node.AsNames[i] : node.Names[i].Names[0];
                variables[i] = DefineName(name);
                node.Names[i].Parent = _currentScope;
            }
            node.Variables = variables;
            return true;
        }
Ejemplo n.º 26
0
 internal override bool ExposesLocalVariable(PythonVariable variable) {
     return NeedsLocalsDictionary; 
 }
Ejemplo n.º 27
0
        public override MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal global)
        {
            Assert.NotNull(global);

            return(new PythonGlobalVariableExpression(
                       Ast.ArrayIndex(
                           PythonAst._globalArray,
                           Ast.Constant(arrayIndex)
                           ),
                       variable,
                       global
                       ));
        }
Ejemplo n.º 28
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;
        }
Ejemplo n.º 29
0
 internal override bool ExposesLocalVariable(PythonVariable variable)
 {
     return(true);
 }
Ejemplo n.º 30
0
        internal override bool ExposesLocalVariable(PythonVariable variable) {
            if (variable.Name == Symbols.Module) {
                return false;
            }

            return true;
        }
Ejemplo n.º 31
0
        internal MSAst.Expression SetLocalLiftedVariable(PythonVariable/*!*/ variable, MSAst.Expression/*!*/ expr) {
            if (_localLifted == null) {
                _localLifted = new Dictionary<PythonVariable, MSAst.Expression>();
            }

            return _localLifted[variable] = expr;
        }
Ejemplo n.º 32
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));
 }
Ejemplo n.º 33
0
 public PythonGlobalVariableExpression(Expression /*!*/ globalExpr, Ast.PythonVariable /*!*/ variable, PythonGlobal /*!*/ global)
     : this(globalExpr, variable, global, false)
 {
 }
Ejemplo n.º 34
0
 internal override bool ExposesLocalVariable(PythonVariable variable) => true;
        public override MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal /*!*/ global)
        {
            Assert.NotNull(global);

            lock (StorageData.Globals) {
                ConstantInfo info = NextGlobal(0);

                StorageData.GlobalStorageType(StorageData.GlobalCount + 1);

                PublishWorker(StorageData.GlobalCount, StorageData.GlobalTypes, info, global, StorageData.Globals);

                StorageData.GlobalCount += 1;

                return(new PythonGlobalVariableExpression(info.Expression, variable, global));
            }
        }
Ejemplo n.º 36
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));
        }
Ejemplo n.º 37
0
 public ClosureInfo(PythonVariable variable, bool accessedInScope) {
     Variable = variable;
     AccessedInScope = accessedInScope;
 }
 internal override bool ExposesLocalVariable(PythonVariable variable)
 {
     throw new InvalidOperationException();
 }
Ejemplo n.º 39
0
 internal override bool ExposesLocalVariable(PythonVariable variable) {
     if (NeedsLocalsDictionary) {
         return true;
     } else if (variable.Scope == this) {
         return false;
     }
     return _comprehension.Parent.ExposesLocalVariable(variable);
 }
Ejemplo n.º 40
0
 private void SetInitialized(PythonVariable /*!*/ variable, bool value)
 {
     _bits.Set(variable.Index * 2 + 1, value);
 }
Ejemplo n.º 41
0
        // FromImportStatement
        public override bool Walk(FromImportStatement node)
        {
            node.Parent = _currentScope;

            if (node.Names != FromImportStatement.Star) {
                PythonVariable[] variables = new PythonVariable[node.Names.Count];
                node.Root.Parent = _currentScope;
                for (int i = 0; i < node.Names.Count; i++) {
                    string name = node.AsNames[i] != null ? node.AsNames[i] : node.Names[i];
                    variables[i] = DefineName(name);
                }
                node.Variables = variables;
            } else {
                Debug.Assert(_currentScope != null);
                _currentScope.ContainsImportStar = true;
                _currentScope.NeedsLocalsDictionary = true;
                _currentScope.HasLateBoundVariableSets = true;
            }
            return true;
        }
Ejemplo n.º 42
0
 private bool IsInitialized(PythonVariable /*!*/ variable)
 {
     return(_bits.Get(variable.Index * 2 + 1));
 }
Ejemplo n.º 43
0
 internal override bool ExposesLocalVariable(PythonVariable variable) {
     throw new InvalidOperationException();
 }
Ejemplo n.º 44
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);
 }
Ejemplo n.º 45
0
 internal override bool ExposesLocalVariable(PythonVariable variable)
 {
     return(NeedsLocalsDictionary);
 }
Ejemplo n.º 46
0
 public ClosureInfo(PythonVariable variable, bool accessedInScope)
 {
     Variable        = variable;
     AccessedInScope = accessedInScope;
 }
Ejemplo n.º 47
0
 public abstract MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal global);
Ejemplo n.º 48
0
 private void SetInitialized(PythonVariable/*!*/ variable, bool value) {
     _bits.Set(variable.Index * 2 + 1, value);
 }
Ejemplo n.º 49
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);
            }
        }
Ejemplo n.º 50
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);
        }
Ejemplo n.º 51
0
 private void SetAssigned(PythonVariable /*!*/ variable, bool value)
 {
     _bits.Set(variable.Index * 2, value);
 }
Ejemplo n.º 52
0
 internal bool IsFreeVariable(PythonVariable variable)
 {
     return(FreeVariables?.Contains(variable?.LimitVariable) ?? false);
 }
Ejemplo n.º 53
0
 private bool IsAssigned(PythonVariable /*!*/ variable)
 {
     return(_bits.Get(variable.Index * 2));
 }
Ejemplo n.º 54
0
 internal virtual Ast LookupVariableExpression(PythonVariable variable)
 => GetVariableExpression(variable);
Ejemplo n.º 55
0
 internal abstract bool ExposesLocalVariable(PythonVariable variable);
Ejemplo n.º 56
0
        internal ClosureExpression/*!*/ LiftedVariable(PythonVariable/*!*/ variable, string/*!*/ name, bool accessInNestedScope) {
            ParameterExpression result = HiddenVariable(typeof(ClosureCell), name);

            ClosureExpression closureVar = new ClosureExpression(variable, result, null);
            EnsureLiftedVars();
            _liftedVars.Add(new DefinitionClosureInfo(closureVar, true));
            return closureVar;
        }
Ejemplo n.º 57
0
 internal void AddGlobalVariable(PythonVariable variable)
 {
     EnsureVariables();
     Variables[variable.Name] = variable;
 }
Ejemplo n.º 58
0
        internal ClosureExpression/*!*/ LiftedParameter(PythonVariable variable, string name) {
            ParameterExpression result = Ast.Variable(typeof(object), name);
            _params.Add(result);

            ClosureExpression closureVar = new ClosureExpression(variable, HiddenVariable(typeof(ClosureCell), name), result);
            EnsureLiftedVars();

            _liftedVars.Add(new DefinitionClosureInfo(closureVar, true));
            return closureVar;
        }
Ejemplo n.º 59
0
 internal override bool ExposesLocalVariable(PythonVariable variable) {
     return true;
 }
Ejemplo n.º 60
0
        internal void ReferenceVariable(PythonVariable variable, int index, MSAst.Expression localTuple, bool accessedInThisScope) {
            EnsureLiftedVars();

            _liftedVars.Add(new ReferenceClosureInfo(variable, index, localTuple, accessedInThisScope));
        }