public ClosureExpression(Ast.PythonVariable/*!*/ variable, Expression/*!*/ closureCell, ParameterExpression parameter) { Assert.NotNull(closureCell); _variable = variable; _closureCell = closureCell; _parameter = parameter; }
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; }
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; }
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 ); }
// 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); }
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(); } } }
// 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); }
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); }
// 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); }
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(); }
private void SetAssigned(PythonVariable/*!*/ variable, bool value) { _bits.Set(variable.Index * 2, value); }
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); }
private bool IsInitialized(PythonVariable/*!*/ variable) { return _bits.Get(variable.Index * 2 + 1); }
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]; }
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; } }
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; } }
public void SetParameter(PythonVariable/*!*/ variable, MSAst.Expression/*!*/ parameter) { Assert.NotNull(variable, parameter); _variables[variable] = parameter; }
internal override MSAst.Expression GetVariableExpression(PythonVariable variable) { Debug.Assert(_globalVariables.ContainsKey(variable)); return(_globalVariables[variable]); }
internal override bool TryBindOuter(SymbolId name, out PythonVariable variable) { // Functions expose their locals to direct access ContainsNestedFreeVariables = true; return TryGetVariable(name, out variable); }
private bool IsAssigned(PythonVariable/*!*/ variable) { return _bits.Get(variable.Index * 2); }
// 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; }
internal override bool ExposesLocalVariable(PythonVariable variable) { return NeedsLocalsDictionary; }
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 )); }
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 ExposesLocalVariable(PythonVariable variable) { return(true); }
internal override bool ExposesLocalVariable(PythonVariable variable) { if (variable.Name == Symbols.Module) { return false; } return true; }
internal MSAst.Expression SetLocalLiftedVariable(PythonVariable/*!*/ variable, MSAst.Expression/*!*/ expr) { if (_localLifted == null) { _localLifted = new Dictionary<PythonVariable, MSAst.Expression>(); } return _localLifted[variable] = expr; }
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)); }
public PythonGlobalVariableExpression(Expression /*!*/ globalExpr, Ast.PythonVariable /*!*/ variable, PythonGlobal /*!*/ global) : this(globalExpr, variable, global, false) { }
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)); } }
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)); }
public ClosureInfo(PythonVariable variable, bool accessedInScope) { Variable = variable; AccessedInScope = accessedInScope; }
internal override bool ExposesLocalVariable(PythonVariable variable) { throw new InvalidOperationException(); }
internal override bool ExposesLocalVariable(PythonVariable variable) { if (NeedsLocalsDictionary) { return true; } else if (variable.Scope == this) { return false; } return _comprehension.Parent.ExposesLocalVariable(variable); }
private void SetInitialized(PythonVariable /*!*/ variable, bool value) { _bits.Set(variable.Index * 2 + 1, value); }
// 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; }
private bool IsInitialized(PythonVariable /*!*/ variable) { return(_bits.Get(variable.Index * 2 + 1)); }
internal override bool ExposesLocalVariable(PythonVariable variable) { 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 bool ExposesLocalVariable(PythonVariable variable) { return(NeedsLocalsDictionary); }
public ClosureInfo(PythonVariable variable, bool accessedInScope) { Variable = variable; AccessedInScope = accessedInScope; }
public abstract MSAst.Expression GetGlobal(MSAst.Expression globalContext, int arrayIndex, PythonVariable variable, PythonGlobal global);
private void SetInitialized(PythonVariable/*!*/ variable, bool value) { _bits.Set(variable.Index * 2 + 1, value); }
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 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); }
private void SetAssigned(PythonVariable /*!*/ variable, bool value) { _bits.Set(variable.Index * 2, value); }
internal bool IsFreeVariable(PythonVariable variable) { return(FreeVariables?.Contains(variable?.LimitVariable) ?? false); }
private bool IsAssigned(PythonVariable /*!*/ variable) { return(_bits.Get(variable.Index * 2)); }
internal virtual Ast LookupVariableExpression(PythonVariable variable) => GetVariableExpression(variable);
internal abstract bool ExposesLocalVariable(PythonVariable variable);
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; }
internal void AddGlobalVariable(PythonVariable variable) { EnsureVariables(); Variables[variable.Name] = variable; }
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; }
internal override bool ExposesLocalVariable(PythonVariable variable) { return true; }
internal void ReferenceVariable(PythonVariable variable, int index, MSAst.Expression localTuple, bool accessedInThisScope) { EnsureLiftedVars(); _liftedVars.Add(new ReferenceClosureInfo(variable, index, localTuple, accessedInThisScope)); }