public void Walk() { var self = GetSelf(); _selfType = (self as AstPythonConstant)?.Type as AstPythonType; if (_target.ReturnAnnotation != null) { var retAnn = new TypeAnnotation(_scope.Ast.LanguageVersion, _target.ReturnAnnotation); var m = retAnn.GetValue(new AstTypeAnnotationConverter(_scope)); if (m is IPythonMultipleMembers mm) { _overload.ReturnTypes.AddRange(mm.Members.OfType <IPythonType>()); } else if (m is IPythonType type) { _overload.ReturnTypes.Add(type); } } _scope.PushScope(); if (self != null) { var p0 = _target.ParametersInternal?.FirstOrDefault(); if (p0 != null && !string.IsNullOrEmpty(p0.Name)) { _scope.SetInScope(p0.Name, self); } } _target.Walk(this); _scope.PopScope(); }
public override bool Walk(ClassDefinition node) { var member = _scope.GetInScope(node.Name); AstPythonType t = member as AstPythonType; if (t == null && member is IPythonMultipleMembers mm) { t = mm.Members.OfType <AstPythonType>().FirstOrDefault(pt => pt.StartIndex == node.StartIndex); } if (t == null) { t = CreateType(node); _scope.SetInScope(node.Name, t); } if (t.Bases == null) { var bases = node.Bases.Where(a => string.IsNullOrEmpty(a.Name)) .Select(a => _scope.GetValueFromExpression(a.Expression)) .OfType <IPythonType>() .ToArray(); try { t.SetBases(_interpreter, bases); } catch (InvalidOperationException) { // Bases were set while we were working } } _scope.PushScope(); _scope.SetInScope("__class__", t); return(true); }
public IPythonType GetBuiltinType(BuiltinTypeId id) { if (id < 0 || id > BuiltinTypeIdExtensions.LastTypeId) { throw new KeyNotFoundException("(BuiltinTypeId)({0})".FormatInvariant((int)id)); } IPythonType res; lock (_builtinTypes) { if (!_builtinTypes.TryGetValue(id, out res)) { var bm = ImportModule(BuiltinModuleName) as AstBuiltinsPythonModule; res = bm?.GetAnyMember("__{0}__".FormatInvariant(id)) as IPythonType; if (res == null) { var name = id.GetTypeName(_factory.Configuration.Version); if (string.IsNullOrEmpty(name)) { Debug.Assert(id == BuiltinTypeId.Unknown, $"no name for {id}"); if (!_builtinTypes.TryGetValue(BuiltinTypeId.Unknown, out res)) { _builtinTypes[BuiltinTypeId.Unknown] = res = new AstPythonType("<unknown>"); } } else { res = new AstPythonType(name); } } _builtinTypes[id] = res; } } return(res); }
/// <summary> /// Provides type factory. Similar to __metaclass__ but does not expose full /// metaclass functionality. Used in cases when function has to return a class /// rather than the class instance. Example: function annotated as '-> Type[T]' /// can be called as a T constructor so func() constructs class instance rather than invoking /// call on an existing instance. See also collections/namedtuple typing in the Typeshed. /// </summary> internal AstPythonType GetTypeFactory() { var clone = new AstPythonType(Name, DeclaringModule, Documentation, Locations.OfType <LocationInfo>().FirstOrDefault(), TypeId == BuiltinTypeId.Unknown ? BuiltinTypeId.Type : TypeId, true); clone.AddMembers(Members, true); return(clone); }
public override bool Walk(ClassDefinition node) { var m = _scope.Peek(); if (m != null) { var n = new Dictionary <string, IMember>(); AstPythonType t = new AstPythonType(_ast, _module, node, GetDoc(node.Body as SuiteStatement), GetLoc(node)); m[node.Name] = n["__class__"] = t; _scope.Push(n); return(true); } return(false); }
public override bool Walk(ClassDefinition node) { var m = _scope.Peek(); if (m != null) { var n = new Dictionary <string, IMember>(); var mro = node.Bases.Where(a => string.IsNullOrEmpty(a.Name)) .Select(a => GetNameFromExpression(a.Expression)) .Where(a => !string.IsNullOrEmpty(a)) .Select(a => new AstPythonType(a)); var t = new AstPythonType(_ast, _module, node, GetDoc(node.Body as SuiteStatement), GetLoc(node), mro); m[node.Name] = n["__class__"] = t; _scope.Push(n); return(true); } return(false); }
public void Walk() { var self = GetSelf(); _selfType = (self as AstPythonConstant)?.Type as AstPythonType; _overload.ReturnTypes.AddRange(_scope.GetTypesFromAnnotation(_target.ReturnAnnotation)); _scope.PushScope(); if (self != null) { var p0 = _target.Parameters.FirstOrDefault(); if (p0 != null && !string.IsNullOrEmpty(p0.Name)) { _scope.SetInScope(p0.Name, self); } } _target.Walk(this); _scope.PopScope(); }
public void Walk() { var self = GetSelf(); _selfType = (self as AstPythonConstant)?.Type as AstPythonType; var annotationTypes = _scope.GetTypesFromAnnotation(Target.ReturnAnnotation).ExcludeDefault(); _overload.ReturnTypes.AddRange(annotationTypes); _scope.PushScope(); // Declare self, if any var skip = 0; if (self != null) { var p0 = Target.Parameters.FirstOrDefault(); if (p0 != null && !string.IsNullOrEmpty(p0.Name)) { _scope.SetInScope(p0.Name, self); skip++; } } // Declare parameters in scope foreach (var p in Target.Parameters.Skip(skip).Where(p => !string.IsNullOrEmpty(p.Name))) { var value = _scope.GetValueFromExpression(p.DefaultValue); _scope.SetInScope(p.Name, value ?? _scope.UnknownType); } // return type from the annotation always wins, no need to walk the body. if (!annotationTypes.Any()) { Target.Walk(this); } _scope.PopScope(); }
public override bool Walk(ClassDefinition node) { var m = _scope.Peek(); if (m != null) { var n = new Dictionary<string, IMember>(); AstPythonType t = new AstPythonType(_ast, _module, node, GetDoc(node.Body as SuiteStatement), GetLoc(node)); m[node.Name] = n["__class__"] = t; _scope.Push(n); return true; } return false; }