Example #1
0
 public PythonVariable(SymbolId name, Type/*!*/ type, VariableKind kind, ScopeStatement/*!*/ scope) {
     Assert.NotNull(type, scope);
     _name = name;
     _type = type;
     _kind = kind;
     _scope = scope;
 }
Example #2
0
 public static void Check(ScopeStatement scope)
 {
     if (scope.Variables != null)
     {
         FlowChecker fc = new FlowChecker(scope);
         scope.Walk(fc);
     }
 }
Example #3
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);
 }
Example #4
0
            private ScopeStatement VisitScope(ScopeStatement scope) {
                var newScope = scope.CopyForRewrite();
                ScopeStatement prevScope = _curScope;
                try {
                    // rewrite the method body
                    _curScope = newScope;
                    newScope.Parent = prevScope;

                    newScope.RewriteBody(this);
                } finally {
                    _curScope = prevScope;
                }
                return newScope;
            }
Example #5
0
 public LookupVisitor(PythonAst ast, MSAst.Expression globalContext) {
     _globalContext = globalContext;
     _curScope = ast;
 }
        private MarkupContent HandleImport(ImportStatement imp, SourceLocation location, ScopeStatement scope, IDocumentAnalysis analysis)
        {
            // 'import A.B, B.C, D.E as F, G, H'
            var eval            = analysis.ExpressionEvaluator;
            var position        = location.ToIndex(analysis.Ast);
            var dottedNameIndex = imp.Names.IndexOf(n => n.StartIndex <= position && position < n.EndIndex);

            if (dottedNameIndex >= 0)
            {
                var dottedName = imp.Names[dottedNameIndex];
                var module     = GetModule(dottedName.MakeString(), dottedName.Names, position, analysis);
                module = module ?? GetModuleFromDottedName(dottedName.Names, position, eval);
                return(module != null?_docSource.GetHover(module.Name, module) : null);
            }
            // Are we over 'D'?
            var nameIndex = imp.AsNames.ExcludeDefault().IndexOf(n => n.StartIndex <= position && position < n.EndIndex);

            if (nameIndex >= 0)
            {
                using (eval.OpenScope(analysis.Document, scope)) {
                    var variableName = imp.AsNames[nameIndex].Name;
                    var m            = eval.LookupNameInScopes(variableName, out _);
                    if (m != null)
                    {
                        return(_docSource.GetHover(variableName, m));
                    }
                }
            }
            return(null);
        }
 // ScopeStatement
 protected internal virtual bool Walk(ScopeStatement node) { return true; }
Example #8
0
 public void SetLoc(PythonAst globalParent, IndexSpan span) {
     _span = span;
     _parent = globalParent;
 }
Example #9
0
 private void PushScope(ScopeStatement node)
 {
     node.Parent = _currentScope;
     _currentScope = node;
     _finallyCount.Add(0);
 }
Example #10
0
 public CoverageScope(ScopeStatement node)
 {
     Statement = node;
 }
Example #11
0
        internal override bool TryBindOuter(ScopeStatement from, string name, out TotemVariable variable)
        {
            // Functions expose their locals to direct access
            ContainsNestedFreeVariables = true;
            if (TryGetVariable(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(name);
                }
                return true;
            }
            return false;
        }
Example #12
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);
                }
            }
        }
 protected internal virtual void PostWalk(ScopeStatement node)
 {
 }
 // ScopeStatement
 protected internal virtual bool Walk(ScopeStatement node)
 {
     return(true);
 }
        public void Get(int startIndex, int endIndex, out Node node, out Node statement, out ScopeStatement scope)
        {
            ExpressionWalker walker;

            if (Options.Keywords)
            {
                walker = new KeywordWalker(Ast, startIndex, endIndex);
                Ast.Walk(walker);
                if (walker.Expression != null)
                {
                    node      = walker.Expression;
                    statement = walker.Statement;
                    scope     = walker.Scope;
                    return;
                }
            }
            if (Options.MemberTarget)
            {
                walker = new MemberTargetExpressionWalker(Ast, startIndex);
            }
            else
            {
                walker = new NormalExpressionWalker(Ast, startIndex, endIndex, Options);
            }
            Ast.Walk(walker);
            node      = walker.Expression;
            statement = walker.Statement;
            scope     = walker.Scope;
        }
 public ImportWalker(PythonAst ast, ScopeStatement targetStmt)
 {
     _ast        = ast;
     _targetStmt = targetStmt;
 }
Example #17
0
 public IDisposable OpenScope(IPythonModule module, ScopeStatement scope) => OpenScope(module, scope, out _);
Example #18
0
        private void Bind(PythonAst unboundAst)
        {
            Assert.NotNull(unboundAst);

            _currentScope = _globalScope = unboundAst;
            _finallyCount.Add(0);

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

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

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

            // Finish Binding w/ outer most scopes first.
            for (int i = _scopes.Count - 1; i >= 0; i--) {
                _scopes[i].FinishBind(this);
            }

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

            // Run flow checker
            foreach (ScopeStatement scope in _scopes) {
                FlowChecker.Check(scope);
            }
        }
Example #19
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;
 }
        private MarkupContent HandleFromImport(FromImportStatement fi, SourceLocation location, ScopeStatement scope, IDocumentAnalysis analysis)
        {
            var eval     = analysis.ExpressionEvaluator;
            var position = location.ToIndex(analysis.Ast);

            // 'from A.B import C as D'
            if (fi.Root.StartIndex <= position && position < fi.Root.EndIndex)
            {
                // We are over A.B
                var module = GetModule(fi.Root.MakeString(), fi.Root.Names, position, analysis);
                module = module ?? GetModuleFromDottedName(fi.Root.Names, position, eval);
                return(module != null?_docSource.GetHover(module.Name, module) : null);
            }
            // Are we over 'C'?
            var nameIndex = fi.Names.ExcludeDefault().IndexOf(n => n.StartIndex <= position && position < n.EndIndex);

            if (nameIndex >= 0)
            {
                var module = eval.Interpreter.ModuleResolution.GetImportedModule(fi.Root.MakeString());
                module = module ?? GetModuleFromDottedName(fi.Root.Names, -1, eval);
                if (module != null)
                {
                    var memberName = fi.Names[nameIndex].Name;
                    var m          = module.GetMember(memberName);
                    return(m != null?_docSource.GetHover(memberName, m) : null);
                }
            }
            // Are we over 'D'?
            nameIndex = fi.AsNames.ExcludeDefault().IndexOf(n => n.StartIndex <= position && position < n.EndIndex);
            if (nameIndex >= 0)
            {
                using (eval.OpenScope(analysis.Document, scope)) {
                    var variableName = fi.AsNames[nameIndex].Name;
                    var m            = eval.LookupNameInScopes(variableName, out _);
                    return(m != null?_docSource.GetHover(variableName, m) : null);
                }
            }
            return(null);
        }
Example #21
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;
     _finallyCount.RemoveAt(_finallyCount.Count - 1);
 }
Example #22
0
 public DelayedProfiling(ScopeStatement ast, MSAst.Expression body, MSAst.ParameterExpression tick) {
     _ast = ast;
     _body = body;
     _tick = tick;
 }
Example #23
0
 private void PopScope()
 {
     _scopes.Add(_currentScope);
     _currentScope = _currentScope.Parent;
     _finallyCount.RemoveAt(_finallyCount.Count - 1);
 }
Example #24
0
        private int _index;                     // Index used for tracking in the flow checker

        public PythonVariable(string name, VariableKind kind, ScopeStatement/*!*/ scope) {
            Assert.NotNull(scope);
            _name = name;
            _kind = kind;
            _scope = scope;
        }
Example #25
0
 public void SetLoc(PythonAst globalParent, int start, int end) {
     _span = new IndexSpan(start, end > start ? end - start : start);
     _parent = globalParent;
 }
Example #26
0
        private bool _accessedInNestedScope;    // the variable is accessed in a nested scope and therefore needs to be a closure var

        internal PythonVariable(string name, VariableKind kind, ScopeStatement/*!*/ scope) {
            _name = name;
            _kind = kind;
            _scope = scope;
        }
Example #27
0
        internal static MSAst.Expression TransformFor(ScopeStatement parent, MSAst.ParameterExpression enumerator,
                                                    Expression list, Expression left, MSAst.Expression body,
                                                    Statement else_, SourceSpan span, SourceLocation header,
                                                    MSAst.LabelTarget breakLabel, MSAst.LabelTarget continueLabel, bool isStatement) {
            // enumerator, isDisposable = Dynamic(GetEnumeratorBinder, list)
            MSAst.Expression init = Ast.Assign(
                    enumerator,
                    new PythonDynamicExpression1<KeyValuePair<IEnumerator, IDisposable>>(
                        Binders.UnaryOperationBinder(
                            parent.GlobalParent.PyContext,
                            PythonOperationKind.GetEnumeratorForIteration
                        ), 
                        parent.GlobalParent.CompilationMode, 
                        AstUtils.Convert(list, typeof(object))
                    )
                );

            // while enumerator.MoveNext():
            //    left = enumerator.Current
            //    body
            // else:
            //    else
            MSAst.Expression ls = AstUtils.Loop(
                    parent.GlobalParent.AddDebugInfo(
                        Ast.Call(
                            Ast.Property(
                                enumerator,
                                typeof(KeyValuePair<IEnumerator, IDisposable>).GetProperty("Key")
                            ),
                            typeof(IEnumerator).GetMethod("MoveNext")
                        ),
                        left.Span
                    ),
                    null,
                    Ast.Block(
                        left.TransformSet(
                            SourceSpan.None,
                            Ast.Call(
                                Ast.Property(
                                    enumerator,
                                    typeof(KeyValuePair<IEnumerator, IDisposable>).GetProperty("Key")
                                ),
                                typeof(IEnumerator).GetProperty("Current").GetGetMethod()
                            ),
                            PythonOperationKind.None
                        ),
                        body,
                        isStatement ? UpdateLineNumber(parent.GlobalParent.IndexToLocation(list.StartIndex).Line) : AstUtils.Empty(),
                        AstUtils.Empty()
                    ),
                    else_,
                    breakLabel,
                    continueLabel
            );

            return Ast.Block(
                init,
                Ast.TryFinally(
                    ls,
                    Ast.Call(AstMethods.ForLoopDispose, enumerator)
                )
            );
        }
Example #28
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;
            }
        }
 protected internal virtual void PostWalk(ScopeStatement node) { }
Example #30
0
 public virtual object Visit(ScopeStatement that, object value)
 {
     throw new System.NotImplementedException();
 }