Пример #1
0
        public override bool Walk(IronPython.Compiler.Ast.FunctionDefinition node)
        {
            FunctionNode functionNode = new FunctionNode(node);

            AddNode(functionNode);
            return(true);
        }
Пример #2
0
 public override void PostWalk(FunctionDefinition node)
 {
     if (node.Body != null && node.Name != null) {
         _scopes.Pop();
         _scopeTree.Pop();
         _curUnit = _analysisStack.Pop();
     }
 }
Пример #3
0
 public void Visit(PyAst.FunctionDefinition node)
 {
     WriteDecorators(node.Decorators ?? Array.Empty <PyAst.Expression>());
     AppendLineWithIndentation($"def {node.Name}({ string.Join(", ", node.Parameters.Select(p => Visit(p)).ToArray()) }):");
     using (new Indenter(this)) {
         Visit(node.Body);
     }
 }
 public override void PostWalk(IronPython.Compiler.Ast.FunctionDefinition node)
 {
     if (node == null)
     {
         return;
     }
     Trace.TraceInformation("in postWalk(FunctionDefinition node) for node " + node.Name);
     m_currentParent = m_savedParents.Pop();
     Trace.TraceInformation("restored m_currentParent to " + m_currentParent.Name);
     base.PostWalk(node);
 }
Пример #5
0
		public PythonMethod(IClass declaringType, FunctionDefinition methodDefinition, string name)
			: base(declaringType, name)
		{
			ReturnType = new DefaultReturnType(declaringType);
			Modifiers = ModifierEnum.Public;
			
			GetMethodRegions(methodDefinition);
			AddParameters(methodDefinition);
			
			declaringType.Methods.Add(this);
		}
        public void Analyze(IronPython.Compiler.Ast.FunctionDefinition function)
        {
            this.argument = 0;
            this.function = function;

            foreach (Expression param in function.Parameters)
            {
                param.Walk(this);
                argument++;
            }
        }
		public override bool Walk(FunctionDefinition node)
		{
			if (IsInitializeComponentMethod(node)) {
				Type type = GetComponentType();
				component = componentCreator.CreateComponent(type, componentName);
				IResourceReader reader = componentCreator.GetResourceReader(CultureInfo.InvariantCulture);
				if (reader != null) {
					reader.Dispose();
				}
				node.Body.Walk(this);
			}
			return false;
		}
		public override bool Walk(FunctionDefinition functionDefinition)
		{
			if (functionDefinition.Body == null) {
				return false;
			}
			
			IClass c = GetClassBeingWalked();
			
			PythonMethodDefinition methodDefinition = new PythonMethodDefinition(functionDefinition);
			PythonMethod method = methodDefinition.CreateMethod(c);
			if (method is PythonConstructor) {
				FindFields(c, functionDefinition);
			}
			return false;
		}
        /// <summary>
        /// This function overrides the behavior when the node being walked is a FunctionDefinition.
        /// </summary>
        /// <param name="node"> the python ast node of type FunctionDefinition. </param>
        /// <returns> boolean value. not important for current implementation. it is taken care of by a call to the base class's walk method.</returns>
        public override bool Walk(IronPython.Compiler.Ast.FunctionDefinition node)
        {
            if (node == null)
            {
                return(false);
            }
            FunctionDefinition fn = PythonEntityCreationHelper.createFunction(node, m_currentCodeFile, m_currentParent);

            node.Parameters.ToList().ForEach(param => fn.AddFormalParameter(new FormalParameter("", param.Name)));
            m_currentParent.AddChild(fn);
            fn.Parent = m_currentParent;
            m_savedParents.Push(m_currentParent);
            m_currentParent = fn;

            return(true);
        }
Пример #10
0
        /// <summary>
        /// <summary>
        /// </summary>
        /// <param name="node">The FunctionDefinition node</param>
        /// <param name="currentCodeFile">The current CodeFile</param>
        /// <param name="parent">The parent of the current CodeFile</param>
        /// <returns>The function object.</returns>
        internal static FunctionDefinition createFunction(IronPython.Compiler.Ast.FunctionDefinition node, CodeFile currentCodeFile, ISyntaxEntity parent)
        {
            FunctionDefinition fn = new FunctionDefinition(node.Name, new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile);

            if (parent.Kind == SyntaxEntityKind.Class)
            {
                fn.TypeOfFunction = FunctionTypes.MemberFunction;
            }
            else if (parent.Kind == SyntaxEntityKind.Function)
            {
                fn.TypeOfFunction = FunctionTypes.AnonymousFunction;
            }
            else
            {
                fn.TypeOfFunction = FunctionTypes.GlobalFunction;
            }

            return(fn);
        }
Пример #11
0
        // FunctionDefinition
        public override bool Walk(IronPython.Compiler.Ast.FunctionDefinition node)
        {
            // Name is defined in the enclosing scope
            Define(node.Name, new FunctionDefinition(node));

            // 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);
            }

            PushScope(node);

            argumentAnalyzer.Analyze(node);
            return(true);
        }
Пример #12
0
		public PythonMethod(IClass declaringType, FunctionDefinition methodDefinition)
			: this(declaringType, methodDefinition, methodDefinition.Name)
		{
		}
Пример #13
0
 private void PushFunction(FunctionDefinition function) {
     if (_functions == null) {
         _functions = new Stack<FunctionDefinition>();
     }
     _functions.Push(function);
 }
Пример #14
0
        private Expression ParseLambdaHelperEnd(FunctionDefinition func, Expression expr) {
            // Pep 342 in Python 2.5 allows Yield Expressions, which can occur inside a Lambda body. 
            // In this case, the lambda is a generator and will yield it's final result instead of just return it.
            Statement body;
            if (func.IsGenerator) {
                YieldExpression y = new YieldExpression(expr);
                y.SetLoc(_globalParent, expr.IndexSpan);
                body = new ExpressionStatement(y);
            } else {
                body = new ReturnStatement(expr);
            }
            body.SetLoc(_globalParent, expr.StartIndex, expr.EndIndex);

            FunctionDefinition func2 = PopFunction();
            System.Diagnostics.Debug.Assert(func == func2);

            func.Body = body;
            func.EndIndex = GetEnd();

            LambdaExpression ret = new LambdaExpression(func);
            func.SetLoc(_globalParent, func.IndexSpan);
            ret.SetLoc(_globalParent, func.IndexSpan);
            return ret;
        }
Пример #15
0
        // funcdef: [decorators] 'def' NAME parameters ':' suite
        // parameters: '(' [varargslist] ')'
        // this gets called with "def" as the look-ahead
        private FunctionDefinition ParseFuncDef() {
            Eat(TokenKind.KeywordDef);
            var start = GetStart();
            string name = ReadName();

            Eat(TokenKind.LeftParenthesis);

            var lStart = GetStart();
            var lEnd = GetEnd();
            int grouping = _tokenizer.GroupingLevel;

            Parameter[] parameters = ParseVarArgsList(TokenKind.RightParenthesis);
            FunctionDefinition ret;
            if (parameters == null) {
                // error in parameters
                ret = new FunctionDefinition(name, new Parameter[0]);
                ret.SetLoc(_globalParent, start, lEnd);
                return ret;
            }

            var rStart = GetStart();
            var rEnd = GetEnd();

            ret = new FunctionDefinition(name, parameters);
            PushFunction(ret);


            Statement body = ParseClassOrFuncBody();
            FunctionDefinition ret2 = PopFunction();
            System.Diagnostics.Debug.Assert(ret == ret2);

            ret.Body = body;
            ret.HeaderIndex = rEnd;

            if (_sink != null) {
                _sink.MatchPair(
                    new SourceSpan(_tokenizer.IndexToLocation(lStart), _tokenizer.IndexToLocation(lEnd)), 
                    new SourceSpan(_tokenizer.IndexToLocation(rStart), _tokenizer.IndexToLocation(rEnd)), 
                    grouping
                );
            }

            ret.SetLoc(_globalParent, start, body.EndIndex);

            return ret;
        }
Пример #16
0
        // funcdef: [decorators] 'def' NAME parameters ':' suite
        // parameters: '(' [varargslist] ')'
        // this gets called with "def" as the look-ahead
        private FunctionDefinition ParseFuncDef() {
            Eat(TokenKind.KeywordDef);
            SourceLocation start = GetStart();
            string name = ReadName();

            Eat(TokenKind.LeftParenthesis);

            SourceLocation lStart = GetStart(), lEnd = GetEnd();
            int grouping = _tokenizer.GroupingLevel;

            Parameter[] parameters = ParseVarArgsList(TokenKind.RightParenthesis);
            if (parameters == null) {
                // error in parameters
                return new FunctionDefinition(name, new Parameter[0], _sourceUnit);
            }

            SourceLocation rStart = GetStart(), rEnd = GetEnd();

            FunctionDefinition ret = new FunctionDefinition(name, parameters, _sourceUnit);
            PushFunction(ret);


            Statement body = ParseClassOrFuncBody();
            FunctionDefinition ret2 = PopFunction();
            System.Diagnostics.Debug.Assert(ret == ret2);

            ret.Body = body;
            ret.Header = rEnd;

            _sink.MatchPair(new SourceSpan(lStart, lEnd), new SourceSpan(rStart, rEnd), grouping);

            ret.SetLoc(start, body.End);

            return ret;
        }
Пример #17
0
 // FunctionDefinition
 public override void PostWalk(FunctionDefinition node)
 {
     Debug.Assert(_currentScope == node);
     PopScope();
 }
		static bool IsInitializeComponentMethod(FunctionDefinition node)
		{
			string name = node.Name.ToString().ToLowerInvariant();
			return name == "initializecomponent" || name == "initializecomponents";
		}
Пример #19
0
 public FunctionDefinitionInfo(IronPython.Compiler.Ast.FunctionDefinition function)
 {
     this.function = function;
 }
Пример #20
0
 public static string Format(FunctionDefinition node)
 {
     return("def " + node.Name + "(" + node.Parameters.Format() + "):"
            + Environment.NewLine + Format(node.Body) + Environment.NewLine);
 }
Пример #21
0
 public LambdaExpression(FunctionDefinition function)
 {
     _function = function;
 }
Пример #22
0
 public SelfNameFinder(FunctionDefinition function, Parameter self)
 {
     _function = function;
     _self     = self;
 }
Пример #23
0
 private void PushScope(IronPython.Compiler.Ast.FunctionDefinition func)
 {
     PushScope(new FunctionScope(module, current, func));
 }
Пример #24
0
 public ArgumentDefinition(SymbolId name, IronPython.Compiler.Ast.FunctionDefinition function, int argument)
 {
     this.name     = name;
     this.function = function;
     this.argument = argument;
 }
Пример #25
0
 public GeneratorExpression(FunctionDefinition function, Expression iterable)
 {
     _function = function;
     _iterable = iterable;
 }
Пример #26
0
 public override bool Walk(FunctionDefinition node) => false;
Пример #27
0
        // FunctionDefinition
        public override bool Walk(FunctionDefinition node)
        {
            node._nameVariable = _globalScope.EnsureGlobalVariable("__name__");

            // Name is defined in the enclosing context
            if (!node.IsLambda) {
                node.PythonVariable = DefineName(node.Name);
            }

            // process the default arg values in the outer context
            foreach (Parameter p in node.Parameters) {
                if (p.DefaultValue != null) {
                    p.DefaultValue.Walk(this);
                }
            }
            // process the decorators in the outer context
            if (node.Decorators != null) {
                foreach (Expression dec in node.Decorators) {
                    dec.Walk(this);
                }
            }

            PushScope(node);

            foreach (Parameter p in node.Parameters) {
                p.Walk(_parameter);
            }

            node.Body.Walk(this);
            return false;
        }
Пример #28
0
 public FunctionScope(Module module, Scope parent, IronPython.Compiler.Ast.FunctionDefinition statement)
     : base(module, parent)
 {
     this.statement = statement;
 }
Пример #29
0
 public override bool Walk(FunctionDefinition node)
 {
     return(false);
 }
Пример #30
0
 public FunctionDefinitionInfo(IronPython.Compiler.Ast.FunctionDefinition function)
 {
     this.function = function;
 }
Пример #31
0
 public FunctionNode(IronPython.Compiler.Ast.FunctionDefinition functionDefinition)
 {
     this.func = functionDefinition;
 }
Пример #32
0
 // FunctionDefinition
 public override void PostWalk(FunctionDefinition node)
 {
     Debug.Assert(_currentScope == node);
     PopScope();
 }
Пример #33
0
 public override void PostWalk(IronPython.Compiler.Ast.FunctionDefinition node)
 {
     scopes.Pop();
 }
Пример #34
0
            internal FunctionDef(FunctionDefinition def)
                : this() {
                _name = def.Name;
                _args = new arguments(def.Parameters);
                _body = ConvertStatements(def.Body);

                if (def.Decorators != null) {
                    _decorators = PythonOps.MakeEmptyList(def.Decorators.Count);
                    foreach (Compiler.Ast.Expression expr in def.Decorators)
                        _decorators.Add(Convert(expr));
                } else
                    _decorators = PythonOps.MakeEmptyList(0);
            }
Пример #35
0
 // FunctionDefinition
 public bool Walk(IronPython.Compiler.Ast.FunctionDefinition node)
 {
     current = node; return(Process(node));
 }
Пример #36
0
        // Helpers for parsing lambda expressions. 
        // Usage
        //   FunctionDefinition f = ParseLambdaHelperStart(string);
        //   Expression expr = ParseXYZ();
        //   return ParseLambdaHelperEnd(f, expr);
        FunctionDefinition ParseLambdaHelperStart(string name) {
            SourceLocation start = GetStart();
            Parameter[] parameters;
            parameters = ParseVarArgsList(TokenKind.Colon);
            SourceLocation mid = GetEnd();

            FunctionDefinition func = new FunctionDefinition(name, parameters, _sourceUnit);
            func.Header = mid;
            func.Start = start;

            // Push the lambda function on the stack so that it's available for any yield expressions to mark it as a generator.
            PushFunction(func);

            return func;
        }
Пример #37
0
		void GetMethodRegions(FunctionDefinition methodDefinition)
		{
			GetBodyRegion(methodDefinition);
			GetMethodRegion(methodDefinition);
		}
Пример #38
0
        // Helpers for parsing lambda expressions. 
        // Usage
        //   FunctionDefinition f = ParseLambdaHelperStart(string);
        //   Expression expr = ParseXYZ();
        //   return ParseLambdaHelperEnd(f, expr);
        private FunctionDefinition ParseLambdaHelperStart(string name) {
            var start = GetStart();
            Parameter[] parameters;
            parameters = ParseVarArgsList(TokenKind.Colon);
            var mid = GetEnd();

            FunctionDefinition func = new FunctionDefinition(name, parameters ?? new Parameter[0]); // new Parameter[0] for error handling of incomplete lambda
            func.HeaderIndex =  mid;
            func.StartIndex = start;

            // Push the lambda function on the stack so that it's available for any yield expressions to mark it as a generator.
            PushFunction(func);

            return func;
        }
Пример #39
0
		/// <summary>
		/// Gets the region of a method. This does not include the body.
		/// </summary>
		void GetMethodRegion(FunctionDefinition methodDefinition)
		{
			SourceLocation start = methodDefinition.Start;
			SourceLocation end = methodDefinition.Header;
			Region = new DomRegion(start.Line, start.Column, end.Line, end.Column + 1);
		}
Пример #40
0
        //  genexpr_for  ::= "for" target_list "in" or_test [genexpr_iter]
        //  genexpr_iter ::= (genexpr_for | genexpr_if) *
        //
        //  "for" has NOT been eaten before entering this method
        private Expression ParseGeneratorExpression(Expression expr) {
            ForStatement root = ParseGenExprFor();
            Statement current = root;

            for (; ; ) {
                if (PeekToken(Tokens.KeywordForToken)) {
                    current = NestGenExpr(current, ParseGenExprFor());
                } else if (PeekToken(Tokens.KeywordIfToken)) {
                    current = NestGenExpr(current, ParseGenExprIf());
                } else {
                    // Generator Expressions have an implicit function definition and yield around their expression.
                    //  (x for i in R)
                    // becomes:
                    //   def f(): 
                    //     for i in R: yield (x)
                    ExpressionStatement ys = new ExpressionStatement(new YieldExpression(expr));
                    ys.Expression.SetLoc(_globalParent, expr.IndexSpan);
                    ys.SetLoc(_globalParent, expr.IndexSpan);
                    NestGenExpr(current, ys);
                    break;
                }
            }

            // We pass the outermost iterable in as a parameter because Python semantics
            // say that this one piece is computed at definition time rather than iteration time
            const string fname = "<genexpr>";
            Parameter parameter = new Parameter("__gen_$_parm__", 0);
            FunctionDefinition func = new FunctionDefinition(fname, new Parameter[] { parameter }, root);
            func.IsGenerator = true;
            func.SetLoc(_globalParent, root.StartIndex, GetEnd());
            func.HeaderIndex = root.EndIndex;

            //  Transform the root "for" statement
            Expression outermost = root.List;
            NameExpression ne = new NameExpression("__gen_$_parm__");
            ne.SetLoc(_globalParent, outermost.IndexSpan);
            root.List = ne;

            GeneratorExpression ret = new GeneratorExpression(func, outermost);
            ret.SetLoc(_globalParent, expr.StartIndex, GetEnd());
            return ret;
        }
Пример #41
0
		void AddParameters(FunctionDefinition methodDefinition, bool ignoreFirstMethodParameter)
		{
			foreach (IParameter parameter in ConvertParameters(methodDefinition.Parameters, ignoreFirstMethodParameter)) {
				Parameters.Add(parameter);
			}
		}
Пример #42
0
 private static ImageListKind GetImageListKind(FunctionDefinition funcDef)
 {
     ImageListKind imageKind = ImageListKind.Method;
     if (funcDef.Decorators != null && funcDef.Decorators.Count == 1) {
         foreach (var decorator in funcDef.Decorators) {
             NameExpression nameExpr = decorator as NameExpression;
             if (nameExpr != null) {
                 if (nameExpr.Name == "property") {
                     imageKind = ImageListKind.Property;
                     break;
                 } else if (nameExpr.Name == "staticmethod") {
                     imageKind = ImageListKind.StaticMethod;
                     break;
                 } else if (nameExpr.Name == "classmethod") {
                     imageKind = ImageListKind.ClassMethod;
                     break;
                 }
             }
         }
     }
     return imageKind;
 }
Пример #43
0
            public static string[] FindNames(FunctionDefinition function) {
                var parameters = function.Parameters;

                if (parameters.Count > 0) {
                    SelfNameFinder finder = new SelfNameFinder(function, parameters[0]);
                    function.Body.Walk(finder);
                    return ArrayUtils.ToArray(finder._names.Keys);
                } else {
                    // no point analyzing function with no parameters
                    return ArrayUtils.EmptyStrings;
                }
            }
Пример #44
0
 public void PostWalk(IronPython.Compiler.Ast.FunctionDefinition node)
 {
     AssertCurrent(node); current = current.Parent;
 }
Пример #45
0
 private List <Inferred> InferFuncDef(IronPython.Compiler.Ast.FunctionDefinition node, Scope scope)
 {
     Debug.Print("Not implemented: InferFuncDefIn");
     return(null);
 }
Пример #46
0
		void GetBodyRegion(FunctionDefinition methodDefinition)
		{
			BodyRegion = PythonMethodOrClassBodyRegion.GetBodyRegion(methodDefinition);
		}
Пример #47
0
		public PythonMethodDefinition(FunctionDefinition methodDefinition)
		{
			this.methodDefinition = methodDefinition;
		}
Пример #48
0
		void AddParameters(FunctionDefinition methodDefinition)
		{
			bool ignoreFirstMethodParameter = !DeclaringTypeIsPythonModule;
			AddParameters(methodDefinition, ignoreFirstMethodParameter);
		}
Пример #49
0
 private YieldLabelBuilder(FunctionDefinition func, CodeGen cg)
 {
     this.func = func;
     this.cg = cg;
     this.topYields = new YieldTarget[func.YieldCount];
 }
Пример #50
0
 public SelfNameFinder(FunctionDefinition function, Parameter self) {
     _function = function;
     _self = self;
 }
Пример #51
0
 public static YieldTarget[] BuildYieldTargets(FunctionDefinition func, CodeGen cg)
 {
     YieldLabelBuilder b = new YieldLabelBuilder(func, cg);
     func.Walk(b);
     return b.topYields;
 }
Пример #52
0
 public override bool Walk(FunctionDefinition node) {
     return false;
 }
Пример #53
0
 public override bool Walk(FunctionDefinition node)
 {
     // Do not recurse into nested functions
     return node == func;
 }