// FunctionDefinition
 public bool Walk(IronPython.Compiler.Ast.FunctionDefinition node)
 {
     current = node; return Process(node);
 }
 private void AssertCurrent(ScopeStatement node)
 {
     Debug.Assert((object)node == (object)current);
 }
Beispiel #3
0
 // FunctionDefinition
 public override void PostWalk(FunctionDefinition node) {
     Debug.Assert(_currentScope == node);
     _scopes.Add(_currentScope);
     _currentScope = _currentScope.Parent;
 }
 public void PostWalk(IronPython.Compiler.Ast.FunctionDefinition node)
 {
     AssertCurrent(node); current = current.Parent;
 }
Beispiel #5
0
 private FlowChecker(ScopeStatement scope) {
     _variables = scope.Variables;
     _bits = new BitArray(_variables.Count * 2);
     int index = 0;
     foreach (var binding in _variables) {
         binding.Value.Index = index++;
     }
     _scope = scope;
     _fdef = new FlowDefiner(this);
     _fdel = new FlowDeleter(this);
 }
Beispiel #6
0
        private void Bind(PythonAst unboundAst) {
            Assert.NotNull(unboundAst);

            _currentScope = _globalScope = unboundAst;

            // Find all scopes and variables
            unboundAst.Walk(this);

            // Bind
            foreach (ScopeStatement scope in _scopes) {
                scope.Bind(this);
            }

            // Finish the globals
            unboundAst.Bind(this);

            // Run flow checker
            foreach (ScopeStatement scope in _scopes) {
                FlowChecker.Check(scope);
            }
        }
Beispiel #7
0
 public void SetLoc(PythonAst globalParent, IndexSpan span)
 {
     _span   = span;
     _parent = globalParent;
 }
 private FlowChecker(ScopeStatement scope)
 {
     bindings = scope.Bindings;
     bits = new BitArray(bindings.Count * 2);
     int index = 0;
     foreach (KeyValuePair<SymbolId, Binding> binding in bindings) {
         binding.Value.Index = index++;
     }
     this.scope = scope;
     this.fdef = new FlowDefiner(this);
     this.fdel = new FlowDeleter(this);
 }
 public DelayedProfiling(ScopeStatement ast, MSAst.Expression body, MSAst.ParameterExpression tick)
 {
     _ast  = ast;
     _body = body;
     _tick = tick;
 }
Beispiel #10
0
 public void SetLoc(PythonAst globalParent, int start, int end)
 {
     _span   = new IndexSpan(start, end > start ? end - start : start);
     _parent = globalParent;
 }
 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);
 }
Beispiel #12
0
            protected override MSAst.Expression VisitExtension(MSAst.Expression node)
            {
                if (node == _globalContext)
                {
                    return(PythonAst._globalContext);
                }

                // we need to re-write nested scoeps
                ScopeStatement scope = node as ScopeStatement;

                if (scope != null)
                {
                    return(base.VisitExtension(VisitScope(scope)));
                }

                LambdaExpression lambda = node as LambdaExpression;

                if (lambda != null)
                {
                    return(base.VisitExtension(new LambdaExpression((FunctionDefinition)VisitScope(lambda.Function))));
                }

                GeneratorExpression generator = node as GeneratorExpression;

                if (generator != null)
                {
                    return(base.VisitExtension(new GeneratorExpression((FunctionDefinition)VisitScope(generator.Function), generator.Iterable)));
                }

                // update the global get/set/raw gets variables
                PythonGlobalVariableExpression global = node as PythonGlobalVariableExpression;

                if (global != null)
                {
                    return(new LookupGlobalVariable(
                               _curScope == null ? PythonAst._globalContext : _curScope.LocalContext,
                               global.Variable.Name,
                               global.Variable.Kind == VariableKind.Local
                               ));
                }

                // set covers sets and deletes
                var setGlobal = node as PythonSetGlobalVariableExpression;

                if (setGlobal != null)
                {
                    if (setGlobal.Value == PythonGlobalVariableExpression.Uninitialized)
                    {
                        return(new LookupGlobalVariable(
                                   _curScope == null ? PythonAst._globalContext : _curScope.LocalContext,
                                   setGlobal.Global.Variable.Name,
                                   setGlobal.Global.Variable.Kind == VariableKind.Local
                                   ).Delete());
                    }
                    else
                    {
                        return(new LookupGlobalVariable(
                                   _curScope == null ? PythonAst._globalContext : _curScope.LocalContext,
                                   setGlobal.Global.Variable.Name,
                                   setGlobal.Global.Variable.Kind == VariableKind.Local
                                   ).Assign(Visit(setGlobal.Value)));
                    }
                }

                var rawValue = node as PythonRawGlobalValueExpression;

                if (rawValue != null)
                {
                    return(new LookupGlobalVariable(
                               _curScope == null ? PythonAst._globalContext : _curScope.LocalContext,
                               rawValue.Global.Variable.Name,
                               rawValue.Global.Variable.Kind == VariableKind.Local
                               ));
                }

                return(base.VisitExtension(node));
            }
Beispiel #13
0
 public LookupVisitor(PythonAst ast, MSAst.Expression globalContext)
 {
     _globalContext = globalContext;
     _curScope      = ast;
 }
Beispiel #14
0
 private void PopScope()
 {
     _scopes.Add(_currentScope);
     _currentScope = _currentScope.Parent;
     _finallyCount.RemoveAt(_finallyCount.Count - 1);
 }
Beispiel #15
0
        internal void UpdateReferencedVariables(string name, PythonVariable variable, ScopeStatement parent) {
            if (variable.Kind == VariableKind.Global || variable.Kind == VariableKind.GlobalLocal) {
                AddReferencedGlobal(name);
            } else {
                name = AddFreeVariable(name);

                for (ScopeStatement innerParent = Parent; innerParent != parent; innerParent = innerParent.Parent) {
                    innerParent.AddFreeVariable(name);
                }
            }
        }
		/// <summary>
		/// Gets the region of the scope statement (typically a ClassDefinition). 
		/// </summary>
		/// <remarks>
		/// A class region includes the body.
		/// </remarks>
		DomRegion GetRegion(ScopeStatement statement)
		{
			return new DomRegion(statement.Start.Line, statement.Start.Column, statement.End.Line, statement.End.Column);
		}
Beispiel #17
0
 public override void PostWalk(ClassDefinition node)
 {
     Debug.Assert(node == current);
     current = current.Parent;
 }
 public static void Check(ScopeStatement scope)
 {
     FlowChecker fc = new FlowChecker(scope);
     scope.Walk(fc);
 }
Beispiel #19
0
 public override void PostWalk(FunctionDefinition node)
 {
     Debug.Assert(current == node);
     current = current.Parent;
 }
Beispiel #20
0
 public static void Check(ScopeStatement scope) {
     if (scope.Variables != null) {
         FlowChecker fc = new FlowChecker(scope);
         scope.Walk(fc);
     }
 }
Beispiel #21
0
 // GlobalSuite
 public override void PostWalk(GlobalSuite node)
 {
     current = current.Parent;
 }
Beispiel #22
0
 private void PushScope(ScopeStatement node) {
     node.Parent = _currentScope;
     _currentScope = node;
 }
Beispiel #23
0
        // ClassDef
        public override bool Walk(ClassDefinition node)
        {
            Define(node.Name);

            // Base references are in the outer scope
            foreach (Expression b in node.Bases) b.Walk(this);

            // And so is the __name__ reference
            Reference(SymbolTable.Name);

            node.Parent = current;
            current = node;

            // define the __doc__ and the __module__
            Define(SymbolTable.Doc);
            Define(SymbolTable.Module);

            // Walk the body
            node.Body.Walk(this);
            processed.Add(node);
            return false;
        }
Beispiel #24
0
 // PythonAst
 public override void PostWalk(PythonAst node) {
     // Do not add the global suite to the list of processed nodes,
     // the publishing must be done after the class local binding.
     Debug.Assert(_currentScope == node);
     _currentScope = _currentScope.Parent;
 }
Beispiel #25
0
        // FuncDef
        public override bool Walk(FunctionDefinition node)
        {
            // Name is defined in the enclosing scope
            Define(node.Name);

            // process the default arg values in the outer scope
            foreach (Expression e in node.Defaults) {
                e.Walk(this);
            }
            // process the decorators in the outer scope
            if (node.Decorators != null) {
                node.Decorators.Walk(this);
            }

            node.Parent = current;
            current = node;
            foreach (Expression e in node.Parameters) {
                e.Walk(parameter);
            }

            node.Body.Walk(this);
            processed.Add(node);

            return false;
        }
 public void PostWalk(GlobalSuite node)
 {
     AssertCurrent(node); current = current.Parent;
 }
Beispiel #27
0
        private GlobalSuite DoBind(Statement root)
        {
            GlobalSuite global = new GlobalSuite(root);
            current = global;

            // Detect all local names
            root.Walk(this);

            // Binding the free variables
            foreach (ScopeStatement scope in processed) {
                scope.BindNames(global, this);
            }

            // Validate
            foreach (ScopeStatement scope in processed) {
                if ((scope.ScopeInfo &
                    (ScopeStatement.ScopeAttributes.ContainsFreeVariables |
                    ScopeStatement.ScopeAttributes.ContainsImportStar |
                    ScopeStatement.ScopeAttributes.ContainsUnqualifiedExec |
                    ScopeStatement.ScopeAttributes.ContainsNestedFreeVariables)) == 0) {
                    continue;
                }

                FunctionDefinition func;
                if ((func = scope as FunctionDefinition) != null) {
                    if (func.ContainsImportStar && func.IsClosure) {
                        ReportSyntaxError(String.Format("import * is not allowed in function '{0}' because it is a nested function", func.Name.GetString()), func);
                    }
                    if (func.ContainsImportStar && func.Parent is FunctionDefinition) {
                        ReportSyntaxError(String.Format("import * is not allowed in function '{0}' because it is a nested function", func.Name.GetString()), func);
                    }
                    if (func.ContainsImportStar && func.ContainsNestedFreeVariables) {
                        ReportSyntaxError(String.Format("import * is not allowed in function '{0}' because it contains a nested function with free variables", func.Name.GetString()), func);
                    }
                    if (func.ContainsUnqualifiedExec && func.ContainsNestedFreeVariables) {
                        ReportSyntaxError(String.Format("unqualified exec is not allowed in function '{0}' it contains a nested function with free variables", func.Name.GetString()), func);
                    }
                    if (func.ContainsUnqualifiedExec && func.IsClosure) {
                        ReportSyntaxError(String.Format("unqualified exec is not allowed in function '{0}' it is a nested function", func.Name.GetString()), func);
                    }
                }

                ClassDefinition cls;
                if ((cls = scope as ClassDefinition) != null) {
                    if (cls.ContainsImportStar) {
                        // warning
                    }

                }
            }
            return global;
        }
 // GlobalSuite
 public bool Walk(GlobalSuite node)
 {
     current = node; return Process(node);
 }
Beispiel #29
0
		/// <summary>
		/// Gets the region of the scope statement (ClassDefinition). 
		/// </summary>
		/// <remarks>
		/// A class region includes the body.
		/// </remarks>
		void GetRegion(ScopeStatement statement)
		{
			Region = new DomRegion(statement.Start.Line, statement.Start.Column, statement.End.Line, statement.End.Column);
		}
        private void SaveCandidate(Node node)
        {
            if (candidateNode == null || Better(node, candidateNode))
            {
                Debug.Print("Candidate: {0} at {1}:{2}-{3}:{4} ({5}:{6})",
                    node, node.Start.Line, node.Start.Column, node.End.Line, node.End.Column,
                    location.Line, location.Column);

                candidateNode = node;
                candidateScope = current;
                candidateContext = context != null && context.Count > 0 ? context.Peek() : null;
            }
        }
Beispiel #31
0
 private void PushScope(ScopeStatement node)
 {
     node.Parent   = _currentScope;
     _currentScope = node;
     _finallyCount.Add(0);
 }