public override bool Walk(IronPython.Compiler.Ast.ClassDefinition node) { ClassNode classNode = new ClassNode(node); AddNode(classNode); return(true); }
public override bool Walk(ClassDefinition node) { classDefinition = node; componentName = node.Name; node.Body.Walk(this); return false; }
public override void PostWalk(IronPython.Compiler.Ast.ClassDefinition node) { Trace.TraceInformation("in method postWalk(ClassDefinition node)"); m_currentParent = m_savedParents.Pop(); Trace.TraceInformation("restored m_current parent to " + m_currentParent.Name); base.PostWalk(node); }
public override bool Walk(ClassDefinition classDefinition) { if (classDefinition.Parent != null) { PythonClass c = new PythonClass(compilationUnit, classDefinition); WalkClassBody(c, classDefinition.Body); } return false; }
public override void PostWalk(ClassDefinition node) { if (node.Body != null && node.Name != null) { _scopes.Pop(); _scopeTree.Pop(); _curUnit = _analysisStack.Pop(); } }
public PythonClass(ICompilationUnit compilationUnit, ClassDefinition classDefinition) : base(compilationUnit, String.Empty) { GetFullyQualifiedName(classDefinition); GetClassRegions(classDefinition); AddBaseTypes(classDefinition.Bases); compilationUnit.Classes.Add(this); }
public void Visit(PyAst.ClassDefinition node) { WriteDecorators(node.Decorators ?? Array.Empty <PyAst.Expression>()); var basesPart = node.Bases.Any() ? $"({VisitExpressionsList(node.Bases)})" : ""; AppendLineWithIndentation($"class {node.Name}{basesPart}:"); using (new Indenter(this)) { Visit(node.Body); } }
/// <summary> /// This function overrides the behavior when the current node being walked is a ClassDefinition /// </summary> /// <param name="node"> the ClassDefinition node</param> /// <returns> boolean value. not important for this implementation. it is handeled by a call to the base Walk() method.</returns> public override bool Walk(IronPython.Compiler.Ast.ClassDefinition node) { UserDefinedType udt = PythonEntityCreationHelper.createClassUDT(node, m_currentCodeFile, m_currentParent); m_currentParent.AddChild(udt); udt.Parent = m_currentParent; m_savedParents.Push(m_currentParent); m_currentParent = udt; return(true); }
/// <summary> /// Gets the fully qualified name of the base class. /// </summary> public static string GetBaseClassName(ClassDefinition classDefinition) { if (classDefinition.Bases.Count > 0) { Expression baseClassExpression = classDefinition.Bases[0]; NameExpression nameExpression = baseClassExpression as NameExpression; MemberExpression memberExpression = baseClassExpression as MemberExpression; if (nameExpression != null) { return nameExpression.Name; } return PythonControlFieldExpression.GetMemberName(memberExpression); } return String.Empty; }
/// <summary> /// Walks a class definition. /// </summary> public override bool Walk(ClassDefinition node) { DefaultClass c = new DefaultClass(compilationUnit, GetFullyQualifiedClassName(node)); c.Region = GetRegion(node); c.BodyRegion = GetBodyRegion(node.Body, node.Header); AddBaseTypes(c, node.Bases); // Save the class. compilationUnit.Classes.Add(c); // Walk through all the class items. currentClass = c; node.Body.Walk(this); currentClass = null; return false; }
// ClassDefinition public override bool Walk(IronPython.Compiler.Ast.ClassDefinition node) { Define(node.Name, new ClassDefinition(node)); // 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); PushScope(node); // define the __doc__ SymbolId doc = SymbolTable.Doc; Define(doc, new DirectDefinition(typeof(string))); // Walk the body node.Body.Walk(this); return(false); }
public ClassNode(IronPython.Compiler.Ast.ClassDefinition cls) { this.cls = cls; }
void CreateClassWithNullBody() { ClassDefinition classDef = new ClassDefinition("classWithNoBody", null, null); }
/// <summary> /// This helper function creates a UDT of udtTypes.class /// </summary> /// <param name="node"> The ClassDefinition node to create the class UDT</param> /// <param name="currentCodeFile">The CodeFile</param> /// <param name="parent">the parent</param> /// <returns>the UDT from the ClassDefinition node</returns> internal static UserDefinedType createClassUDT(IronPython.Compiler.Ast.ClassDefinition node, CodeFile currentCodeFile, ISyntaxEntity parent) { UserDefinedType classObject = new Entities.UDT.ClassDefinition(node.Name, new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); return(classObject); }
public override bool Walk(ClassDefinition node) { CheckForIllegalWords(node, node.Name); CommonWalk(node); return true; }
// ClassDefinition public bool Walk(IronPython.Compiler.Ast.ClassDefinition node) { current = node; return(Process(node)); }
public static DomRegion GetBodyRegion(ClassDefinition classDefinition) { return GetBodyRegion(classDefinition.Body, classDefinition.Header); }
private List <Inferred> InferClassDef(IronPython.Compiler.Ast.ClassDefinition node, Scope scope) { return(Engine.MakeList <Inferred>(module.GetClass(node))); }
public ClassDefinition(IronPython.Compiler.Ast.ClassDefinition @class) { this.@class = @class; }
internal ClassDef(ClassDefinition def) : this() { _name = def.Name; _bases = PythonOps.MakeEmptyList(def.Bases.Count); foreach (AstExpression expr in def.Bases) _bases.Add(Convert(expr)); _body = ConvertStatements(def.Body); if (def.Decorators != null) { _decorator_list = PythonOps.MakeEmptyList(def.Decorators.Count); foreach (AstExpression expr in def.Decorators) _decorator_list.Add(Convert(expr)); } else _decorator_list = PythonOps.MakeEmptyList(0); }
public override void PostWalk(ClassDefinition node) { containers.Pop (); }
public override bool Walk(ClassDefinition node) { var klass = new PythonClass () { Name = node.Name, Region = GetDomRegion (node), Documentation = node.Documentation }; containers.Peek ().Classes.Add (klass); containers.Push (klass); return base.Walk (node); }
// ClassDefinition public override void PostWalk(ClassDefinition node) { Debug.Assert(node == _currentScope); PopScope(); }
/// <summary> /// Adds the namespace to the class name taken from the class definition. /// </summary> string GetFullyQualifiedClassName(ClassDefinition classDef) { return String.Concat(compilationUnit.UsingScope.NamespaceName, ".", classDef.Name); }
public ClassScope(Module module, Scope parent, IronPython.Compiler.Ast.ClassDefinition statement) : base(module, parent) { this.statement = statement; }
public override void PostWalk(IronPython.Compiler.Ast.ClassDefinition node) { scopes.Pop(); }
// ClassDefinition public override void PostWalk(ClassDefinition node) { Debug.Assert(node == _currentScope); _scopes.Add(_currentScope); _currentScope = _currentScope.Parent; }
public ClassScopeNode(ClassDefinition klass) { _klass = klass; }
internal override Statement Revert() { ClassDefinition cd = new ClassDefinition(name, expr.RevertExprs(bases), RevertStmts(body)); if (decorator_list.Count != 0) cd.Decorators = expr.RevertExprs(decorator_list); return cd; }
private void PushScope(IronPython.Compiler.Ast.ClassDefinition cls) { PushScope(new ClassScope(module, current, cls)); }
/// <summary> /// Creates the LambdaExpression which implements the body of the function. /// /// The functions signature is either "object Function(PythonFunction, ...)" /// where there is one object parameter for each user defined parameter or /// object Function(PythonFunction, object[]) for functions which take more /// than PythonCallTargets.MaxArgs arguments. /// </summary> private LightLambdaExpression CreateFunctionLambda() { bool needsWrapperMethod = _parameters.Length > PythonCallTargets.MaxArgs; Type delegateType = GetDelegateType(_parameters, needsWrapperMethod, out _); MSAst.ParameterExpression localContext = null; ReadOnlyCollectionBuilder <MSAst.ParameterExpression> locals = new ReadOnlyCollectionBuilder <MSAst.ParameterExpression>(); if (NeedsLocalContext) { localContext = LocalCodeContextVariable; locals.Add(localContext); } MSAst.ParameterExpression[] parameters = CreateParameters(needsWrapperMethod, locals); List <MSAst.Expression> init = new List <MSAst.Expression>(); foreach (var param in _parameters) { if (GetVariableExpression(param.PythonVariable) is IPythonVariableExpression pyVar) { var varInit = pyVar.Create(); if (varInit != null) { init.Add(varInit); } } } // Transform the parameters. init.Add(Ast.ClearDebugInfo(GlobalParent.Document)); locals.Add(PythonAst._globalContext); init.Add(Ast.Assign(PythonAst._globalContext, new GetGlobalContextExpression(_parentContext))); GlobalParent.PrepareScope(locals, init); // Create variables and references. Since references refer to // parameters, do this after parameters have been created. CreateFunctionVariables(locals, init); // If the __class__ variable is used in a class method then we need to initialize it. // This must be done before parameter initialization (in case one of the parameters is called __class__). ClassDefinition parent = FindParentOfType <ClassDefinition>(); if (parent != null && TryGetVariable("__class__", out PythonVariable pVar)) { init.Add( AssignValue( GetVariableExpression(pVar), Ast.Call(AstMethods.LookupName, parent.Parent.LocalContext, Ast.Constant(parent.Name)) ) ); } // Initialize parameters - unpack tuples. // Since tuples unpack into locals, this must be done after locals have been created. InitializeParameters(init, needsWrapperMethod, parameters); List <MSAst.Expression> statements = new List <MSAst.Expression>(); // add beginning sequence point var start = GlobalParent.IndexToLocation(StartIndex); statements.Add(GlobalParent.AddDebugInfo( AstUtils.Empty(), new SourceSpan(new SourceLocation(0, start.Line, start.Column), new SourceLocation(0, start.Line, int.MaxValue)))); // For generators, we need to do a check before the first statement for Generator.Throw() / Generator.Close(). // The exception traceback needs to come from the generator's method body, and so we must do the check and throw // from inside the generator. if (IsGenerator) { MSAst.Expression s1 = YieldExpression.CreateCheckThrowExpression(SourceSpan.None); statements.Add(s1); } if (Body.CanThrow && !(Body is SuiteStatement) && Body.StartIndex != -1) { statements.Add(UpdateLineNumber(GlobalParent.IndexToLocation(Body.StartIndex).Line)); } statements.Add(Body); MSAst.Expression body = Ast.Block(statements); if (Body.CanThrow && GlobalParent.PyContext.PythonOptions.Frames) { body = AddFrame(LocalContext, Ast.Property(_functionParam, typeof(PythonFunction).GetProperty(nameof(PythonFunction.__code__))), body); locals.Add(FunctionStackVariable); } body = AddProfiling(body); body = WrapScopeStatements(body, Body.CanThrow); body = Ast.Block(body, AstUtils.Empty()); body = AddReturnTarget(body); MSAst.Expression bodyStmt = body; if (localContext != null) { var createLocal = CreateLocalContext(_parentContext); init.Add( Ast.Assign( localContext, createLocal ) ); } init.Add(bodyStmt); bodyStmt = Ast.Block(init); // wrap a scope if needed bodyStmt = Ast.Block(locals.ToReadOnlyCollection(), bodyStmt); return(AstUtils.LightLambda( typeof(object), delegateType, AddDefaultReturn(bodyStmt, typeof(object)), Name + "$" + Interlocked.Increment(ref _lambdaId), parameters )); }
public InferredClass GetClass(IronPython.Compiler.Ast.ClassDefinition cls) { Debug.Assert(scopes.ContainsKey(cls)); return(new InferredClass(scopes[cls] as ClassScope)); }
//classdef: 'class' NAME ['(' testlist ')'] ':' suite private ClassDefinition ParseClassDef() { Eat(TokenKind.KeywordClass); SourceLocation start = GetStart(); SymbolId name = ReadName(); if (name == SymbolId.Empty) { // no name, assume there's no class. return new ClassDefinition(SymbolId.Empty, new Expression[0], new ExpressionStatement(new ErrorExpression())); } Expression[] bases = new Expression[0]; if (MaybeEat(TokenKind.LeftParenthesis)) { List<Expression> l = ParseTestList(); if (l.Count == 1 && l[0] is ErrorExpression) { // error handling, classes is incomplete. return new ClassDefinition(name, new Expression[0], new ExpressionStatement(new ErrorExpression())); } bases = l.ToArray(); Eat(TokenKind.RightParenthesis); } SourceLocation mid = GetEnd(); // Save private prefix string savedPrefix = SetPrivatePrefix(name); // Parse the class body Statement body = ParseSuite(); // Restore the private prefix _privatePrefix = savedPrefix; ClassDefinition ret = new ClassDefinition(name, bases, body); ret.Header = mid; ret.SetLoc(start, GetEnd()); return ret; }
// ClassDefinition public override bool Walk(ClassDefinition node) { node.PythonVariable = DefineName(node.Name); // Base references are in the outer context foreach (Expression b in node.Bases) b.Walk(this); // process the decorators in the outer context if (node.Decorators != null) { foreach (Expression dec in node.Decorators) { dec.Walk(this); } } PushScope(node); node.ModuleNameVariable = _globalScope.EnsureGlobalVariable("__name__"); // define the __doc__ and the __module__ if (node.Body.Documentation != null) { node.DocVariable = DefineName("__doc__"); } node.ModVariable = DefineName("__module__"); // Walk the body node.Body.Walk(this); return false; }
public override void PostWalk(ClassDefinition node) { SuiteStatement body = node.Body as SuiteStatement; int count = body == null ? 1 : body.Statements.Count; var methods = body != null ? body.Statements .OfType<FunctionDefinition>() .ToList() : new List<FunctionDefinition>(); var mDic = methods.ToDictionary( k => (Statement)k, v => -1 ); FunctionDefinition constructor = methods .FirstOrDefault(n => n.Name == "__init__"); FunctionDefinition toString = methods .FirstOrDefault(n => n.Name == "__str__"); List<string> statements = new List<string>(); for (int i = count - 1; i >= 0; i--) { string st = Content(); if (!String.IsNullOrWhiteSpace(st)) { if (body != null && body.Statements[i] is FunctionDefinition) { mDic[body.Statements[i]] = i; } statements.Add(st); } } statements.Reverse(); StringBuilder sb = new StringBuilder(); string[] constructorArgs = constructor != null ? constructor.Parameters .Skip(1) .Select(p => p.Name) .ToArray() : new string[0]; string factoryName = String.Format("{0}Class", node.Name); sb.AppendFormat("{0} = function({2}) {{ return new {1}({2}); }};", node.Name, factoryName, String.Join(", ", constructorArgs)); sb.AppendLine(); if (constructor != null) { sb.AppendFormat("{0} = {1}", factoryName, statements[mDic[constructor]]); } else { sb.AppendFormat("{0} = function() {{ }};", factoryName); } sb.AppendLine(); for (int i = 0; i < statements.Count; i++) { if (mDic.Values.Contains(i)) { continue; } sb.AppendFormat("{0}.prototype.{1}", factoryName, statements[i]); sb.AppendLine(); sb.AppendLine(); } if (toString != null) { sb.AppendFormat("{0}.prototype.toString = {1}", factoryName, statements[mDic[toString]]); sb.AppendLine(); } methods.Remove(constructor); methods.Remove(toString); foreach (var m in methods) { sb.AppendFormat("{0}.prototype.{1} = {2}", factoryName, m.Name, statements[mDic[m]]); sb.AppendLine(); } Content(sb.ToString()); CommonPostWalk(node, true); }
//classdef: 'class' NAME ['(' testlist ')'] ':' suite private ClassDefinition ParseClassDef() { Eat(TokenKind.KeywordClass); var start = GetStart(); string name = ReadName(); if (name == null) { // no name, assume there's no class. return new ClassDefinition(null, new Expression[0], ErrorStmt()); } Expression[] bases = new Expression[0]; if (MaybeEat(TokenKind.LeftParenthesis)) { List<Expression> l = ParseTestList(); if (l.Count == 1 && l[0] is ErrorExpression) { // error handling, classes is incomplete. return new ClassDefinition(name, new Expression[0], ErrorStmt()); } bases = l.ToArray(); Eat(TokenKind.RightParenthesis); } var mid = GetEnd(); // Save private prefix string savedPrefix = SetPrivatePrefix(name); // Parse the class body Statement body = ParseClassOrFuncBody(); // Restore the private prefix _privatePrefix = savedPrefix; ClassDefinition ret = new ClassDefinition(name, bases, body); ret.HeaderIndex = mid; ret.SetLoc(_globalParent, start, GetEnd()); return ret; }
public void PostWalk(IronPython.Compiler.Ast.ClassDefinition node) { AssertCurrent(node); current = current.Parent; }
/// <summary> /// Creates the LambdaExpression which implements the body of the function. /// /// The functions signature is either "object Function(PythonFunction, ...)" /// where there is one object parameter for each user defined parameter or /// object Function(PythonFunction, object[]) for functions which take more /// than PythonCallTargets.MaxArgs arguments. /// </summary> private LightLambdaExpression CreateFunctionLambda() { bool needsWrapperMethod = _parameters.Length > PythonCallTargets.MaxArgs; Delegate originalDelegate; Type delegateType = GetDelegateType(_parameters, needsWrapperMethod, out originalDelegate); MSAst.ParameterExpression localContext = null; ReadOnlyCollectionBuilder <MSAst.ParameterExpression> locals = new ReadOnlyCollectionBuilder <MSAst.ParameterExpression>(); if (NeedsLocalContext) { localContext = LocalCodeContextVariable; locals.Add(localContext); } MSAst.ParameterExpression[] parameters = CreateParameters(needsWrapperMethod, locals); List <MSAst.Expression> init = new List <MSAst.Expression>(); foreach (var param in _parameters) { IPythonVariableExpression pyVar = GetVariableExpression(param.PythonVariable) as IPythonVariableExpression; if (pyVar != null) { var varInit = pyVar.Create(); if (varInit != null) { init.Add(varInit); } } } // Transform the parameters. init.Add(Ast.ClearDebugInfo(GlobalParent.Document)); locals.Add(PythonAst._globalContext); init.Add(Ast.Assign(PythonAst._globalContext, new GetGlobalContextExpression(_parentContext))); GlobalParent.PrepareScope(locals, init); // Create variables and references. Since references refer to // parameters, do this after parameters have been created. CreateFunctionVariables(locals, init); // If the __class__ variable is used the a class method then we need to initialize it. // This must be done before parameter initialization (in case one of the parameters is called __class__). ClassDefinition parent = FindParentOfType <ClassDefinition>(); PythonVariable pVar; if (parent != null && TryGetVariable("__class__", out pVar)) { init.Add( AssignValue( GetVariableExpression(pVar), Ast.Call(AstMethods.LookupName, parent.Parent.LocalContext, Ast.Constant(parent.Name)) ) ); } // Initialize parameters - unpack tuples. // Since tuples unpack into locals, this must be done after locals have been created. InitializeParameters(init, needsWrapperMethod, parameters); List <MSAst.Expression> statements = new List <MSAst.Expression>(); // add beginning sequence point var start = GlobalParent.IndexToLocation(StartIndex); statements.Add(GlobalParent.AddDebugInfo( AstUtils.Empty(), new SourceSpan(new SourceLocation(0, start.Line, start.Column), new SourceLocation(0, start.Line, Int32.MaxValue)))); // For generators, we need to do a check before the first statement for Generator.Throw() / Generator.Close(). // The exception traceback needs to come from the generator's method body, and so we must do the check and throw // from inside the generator. if (IsGenerator) { MSAst.Expression s1 = YieldExpression.CreateCheckThrowExpression(SourceSpan.None); statements.Add(s1); } MSAst.ParameterExpression extracted = null; if (!IsGenerator && _canSetSysExcInfo) { // need to allocate the exception here so we don't share w/ exceptions made & freed // during the body. extracted = Ast.Parameter(typeof(Exception), "$ex"); locals.Add(extracted); } if (_body.CanThrow && !(_body is SuiteStatement) && _body.StartIndex != -1) { statements.Add(UpdateLineNumber(GlobalParent.IndexToLocation(_body.StartIndex).Line)); } statements.Add(Body); MSAst.Expression body = Ast.Block(statements); // If this function can modify sys.exc_info() (_canSetSysExcInfo), then it must restore the result on finish. // // Wrap in // $temp = PythonOps.SaveCurrentException() // <body> // PythonOps.RestoreCurrentException($temp) // Skip this if we're a generator. For generators, the try finally is handled by the PythonGenerator class // before it's invoked. This is because the restoration must occur at every place the function returns from // a yield point. That's different than the finally semantics in a generator. if (extracted != null) { MSAst.Expression s = AstUtils.Try( Ast.Assign( extracted, Ast.Call(AstMethods.SaveCurrentException) ), body ).Finally( Ast.Call( AstMethods.RestoreCurrentException, extracted ) ); body = s; } if (_body.CanThrow && GlobalParent.PyContext.PythonOptions.Frames) { body = AddFrame(LocalContext, Ast.Property(_functionParam, typeof(PythonFunction).GetProperty("__code__")), body); locals.Add(FunctionStackVariable); } body = AddProfiling(body); body = WrapScopeStatements(body, _body.CanThrow); body = Ast.Block(body, AstUtils.Empty()); body = AddReturnTarget(body); MSAst.Expression bodyStmt = body; if (localContext != null) { var createLocal = CreateLocalContext(_parentContext); init.Add( Ast.Assign( localContext, createLocal ) ); } init.Add(bodyStmt); bodyStmt = Ast.Block(init); // wrap a scope if needed bodyStmt = Ast.Block(locals.ToReadOnlyCollection(), bodyStmt); return(AstUtils.LightLambda( typeof(object), delegateType, AddDefaultReturn(bodyStmt, typeof(object)), Name + "$" + Interlocked.Increment(ref _lambdaId), parameters )); }
// Don't recurse into class or function definitions public override bool Walk(ClassDefinition node) { return(false); }
// ClassDef public override bool Walk(ClassDefinition node) { if (_scope == node) { // the class body is being analyzed, go deep: return true; } else { // analyze the class definition itself (it is visited while analyzing parent scope): Define(node.Name); foreach (Expression e in node.Bases) { e.Walk(this); } return false; } }
public override bool Walk(ClassDefinition node) { // Do not recurse into nested classes return false; }
internal ClassDef(ClassDefinition def) : this() { _name = def.Name; _bases = PythonOps.MakeEmptyList(def.Bases.Count); foreach (Compiler.Ast.Expression expr in def.Bases) _bases.Add(Convert(expr)); _body = ConvertStatements(def.Body); _decorator_list = new PythonList(); // TODO Actually fill in the decorators here }
// TODO: What about names being redefined? // remember classes/functions as they start new scopes public override bool Walk(ClassDefinition node) { if (node.Body == null || node.Name == null) { return false; } var queue = _entry.ProjectState.Queue; var scopes = new InterpreterScope[_scopes.Count + 1]; _scopes.CopyTo(scopes); _analysisStack.Push(_curUnit); var unit = _curUnit = new AnalysisUnit(node, scopes, _curUnit); var klass = new ClassInfo(unit, _entry); var classScope = klass.Scope; var scope = _scopes.Peek(); scope.SetVariable(node, unit, node.Name, klass.SelfSet); _scopes.Push(classScope); scopes[scopes.Length - 1] = classScope; // TODO: Add parameters for __new__/__init__ PushPositionScope(node, classScope); return true; }
// Don't recurse into class or function definitions public override bool Walk(ClassDefinition node) { return false; }
// ClassDef public override bool Walk(ClassDefinition node) { if (scope == node) { return true; } else { Define(node.Name); return false; } }