public override bool Walk(IronPython.Compiler.Ast.FunctionDefinition node) { FunctionNode functionNode = new FunctionNode(node); AddNode(functionNode); return(true); }
public override void PostWalk(FunctionDefinition node) { if (node.Body != null && node.Name != null) { _scopes.Pop(); _scopeTree.Pop(); _curUnit = _analysisStack.Pop(); } }
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); }
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); }
/// <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); }
// 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); }
public PythonMethod(IClass declaringType, FunctionDefinition methodDefinition) : this(declaringType, methodDefinition, methodDefinition.Name) { }
private void PushFunction(FunctionDefinition function) { if (_functions == null) { _functions = new Stack<FunctionDefinition>(); } _functions.Push(function); }
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; }
// 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; }
// 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; }
// 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"; }
public FunctionDefinitionInfo(IronPython.Compiler.Ast.FunctionDefinition function) { this.function = function; }
public static string Format(FunctionDefinition node) { return("def " + node.Name + "(" + node.Parameters.Format() + "):" + Environment.NewLine + Format(node.Body) + Environment.NewLine); }
public LambdaExpression(FunctionDefinition function) { _function = function; }
public SelfNameFinder(FunctionDefinition function, Parameter self) { _function = function; _self = self; }
private void PushScope(IronPython.Compiler.Ast.FunctionDefinition func) { PushScope(new FunctionScope(module, current, func)); }
public ArgumentDefinition(SymbolId name, IronPython.Compiler.Ast.FunctionDefinition function, int argument) { this.name = name; this.function = function; this.argument = argument; }
public GeneratorExpression(FunctionDefinition function, Expression iterable) { _function = function; _iterable = iterable; }
public override bool Walk(FunctionDefinition node) => false;
// 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; }
public FunctionScope(Module module, Scope parent, IronPython.Compiler.Ast.FunctionDefinition statement) : base(module, parent) { this.statement = statement; }
public override bool Walk(FunctionDefinition node) { return(false); }
public FunctionDefinitionInfo(IronPython.Compiler.Ast.FunctionDefinition function) { this.function = function; }
public FunctionNode(IronPython.Compiler.Ast.FunctionDefinition functionDefinition) { this.func = functionDefinition; }
// FunctionDefinition public override void PostWalk(FunctionDefinition node) { Debug.Assert(_currentScope == node); PopScope(); }
public override void PostWalk(IronPython.Compiler.Ast.FunctionDefinition node) { scopes.Pop(); }
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); }
// FunctionDefinition public bool Walk(IronPython.Compiler.Ast.FunctionDefinition node) { current = node; return(Process(node)); }
// 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; }
void GetMethodRegions(FunctionDefinition methodDefinition) { GetBodyRegion(methodDefinition); GetMethodRegion(methodDefinition); }
// 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; }
/// <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); }
// 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; }
void AddParameters(FunctionDefinition methodDefinition, bool ignoreFirstMethodParameter) { foreach (IParameter parameter in ConvertParameters(methodDefinition.Parameters, ignoreFirstMethodParameter)) { Parameters.Add(parameter); } }
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; }
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; } }
public void PostWalk(IronPython.Compiler.Ast.FunctionDefinition node) { AssertCurrent(node); current = current.Parent; }
private List <Inferred> InferFuncDef(IronPython.Compiler.Ast.FunctionDefinition node, Scope scope) { Debug.Print("Not implemented: InferFuncDefIn"); return(null); }
void GetBodyRegion(FunctionDefinition methodDefinition) { BodyRegion = PythonMethodOrClassBodyRegion.GetBodyRegion(methodDefinition); }
public PythonMethodDefinition(FunctionDefinition methodDefinition) { this.methodDefinition = methodDefinition; }
void AddParameters(FunctionDefinition methodDefinition) { bool ignoreFirstMethodParameter = !DeclaringTypeIsPythonModule; AddParameters(methodDefinition, ignoreFirstMethodParameter); }
private YieldLabelBuilder(FunctionDefinition func, CodeGen cg) { this.func = func; this.cg = cg; this.topYields = new YieldTarget[func.YieldCount]; }
public SelfNameFinder(FunctionDefinition function, Parameter self) { _function = function; _self = self; }
public static YieldTarget[] BuildYieldTargets(FunctionDefinition func, CodeGen cg) { YieldLabelBuilder b = new YieldLabelBuilder(func, cg); func.Walk(b); return b.topYields; }
public override bool Walk(FunctionDefinition node) { return false; }
public override bool Walk(FunctionDefinition node) { // Do not recurse into nested functions return node == func; }