public IPythonType GetTypeFromString(string typeString) { // Type alone is not a valid syntax, so we need to simulate the annotation. typeString = $"x: {typeString}"; using (var sr = new StringReader(typeString)) { var sink = new CollectingErrorSink(); var parser = Parser.CreateParser(sr, Module.Interpreter.LanguageVersion, new ParserOptions { ErrorSink = sink }); var ast = parser.ParseFile(); var exprStatement = (ast?.Body as SuiteStatement)?.Statements?.FirstOrDefault() as ExpressionStatement; if (!(Statement.GetExpression(exprStatement) is ExpressionWithAnnotation annExpr) || sink.Errors.Count > 0) { return(null); } var ann = new TypeAnnotation(Ast.LanguageVersion, annExpr.Annotation); var value = ann.GetValue(new TypeAnnotationConverter(this)); var t = value.GetPythonType(); if (!t.IsUnknown()) { return(t); } } return(null); }
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) { _returnTypes.AddRange(mm.Members.OfType <IPythonType>()); } else if (m is IPythonType type) { _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(); }
protected override void LLWrite(ICodeWriter writer, object o) { writer.BeginNewLine(true); if (Name != null) { Name.WriteAll(writer); } else { Subscript.WriteAll(writer); } if (TypeAnnotation != null) { writer.Write(": ", true); TypeAnnotation.WriteAll(writer); } if (Value != null) { writer.Write(" = ", true); Value.WriteAll(writer); } writer.EndLine(); }
public IPythonType GetTypeFromAnnotation(Expression expr, out bool isGeneric, LookupOptions options = LookupOptions.Global | LookupOptions.Builtins) { isGeneric = false; switch (expr) { case null: return(null); case CallExpression callExpr: // x: NamedTuple(...) return(GetValueFromCallable(callExpr)?.GetPythonType() ?? UnknownType); case IndexExpression indexExpr: // Try generics var target = GetValueFromExpression(indexExpr.Target); var result = GetValueFromGeneric(target, indexExpr); if (result != null) { isGeneric = true; return(result.GetPythonType()); } break; } // Look at specialization and typing first var ann = new TypeAnnotation(Ast.LanguageVersion, expr); return(ann.GetValue(new TypeAnnotationConverter(this, options))); }
public TypeAnnotation[] ReadAnnotations(ushort count) { var typeAnnotations = new TypeAnnotation[count]; for (var i = 0; i < count; i++) { typeAnnotations[i] = new TypeAnnotation().Read(_classData); } return(typeAnnotations); }
public void Walk() { IMember self = null; bool classmethod, staticmethod; GetMethodType(_target, out classmethod, out staticmethod); if (!staticmethod) { self = _scope.LookupNameInScopes("__class__", NameLookupContext.LookupOptions.Local); if (!classmethod) { var cls = self as IPythonType; if (cls == null) { self = null; } else { self = new AstPythonConstant(cls, ((cls as ILocatedMember)?.Locations).MaybeEnumerate().ToArray()); } } } 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) { _returnTypes.AddRange(mm.Members.OfType <IPythonType>()); } else if (m is IPythonType type) { _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 async Task <IPythonType> GetTypeFromAnnotationAsync(Expression expr, CancellationToken cancellationToken = default, LookupOptions options = LookupOptions.Global | LookupOptions.Builtins) { if (expr == null) { return(null); } if (expr is CallExpression callExpr) { // x: NamedTuple(...) return((await GetValueFromCallableAsync(callExpr, cancellationToken))?.GetPythonType() ?? UnknownType); } // Look at specialization and typing first var ann = new TypeAnnotation(Ast.LanguageVersion, expr); return(ann.GetValue(new TypeAnnotationConverter(this, options))); }
protected override void LLWrite(ICodeWriter writer, object o) { if (Name != null) { Name.WriteAll(writer); } if (TypeAnnotation != null) { if (Name != null) { writer.Write(": ", true); } if (ParameterKind != SLParameterKind.None) { writer.Write(ToParameterKindString(ParameterKind), false); writer.Write(' ', false); } TypeAnnotation.WriteAll(writer); } }
public IEnumerable <IPythonType> GetTypesFromAnnotation(Expression expr) { if (expr == null) { return(Enumerable.Empty <IPythonType>()); } var ann = new TypeAnnotation(Ast.LanguageVersion, expr); var m = ann.GetValue(new AstTypeAnnotationConverter(this)); if (m is IPythonMultipleMembers mm) { return(mm.Members.OfType <IPythonType>()); } else if (m is IPythonType type) { return(Enumerable.Repeat(type, 1)); } return(Enumerable.Empty <IPythonType>()); }
internal override void ToString(StringBuilder sb) { if (TypeAnnotation is not null) { sb.Append(TypeAnnotation.ToString()); sb.Append(' '); } sb.Append(Name); if (DefaultValue is not null) { sb.Append(" = "); DefaultValue.ToString(sb); } if (IsVarArgs) { sb.Append("..."); } }
private MichelinePrim ReadMichelinePrimitive(int argsLength, bool annotations = false) { var primTag = (PrimType)ReadByte(); var prim = new MichelinePrim { Prim = primTag }; if (0 < argsLength && argsLength < 3) { prim.Args = new MichelineArray(argsLength); for (var i = 0; i < argsLength; i++) { prim.Args.Add(ReadMicheline()); } } else if (argsLength == 3) { prim.Args = ReadMichelineArray(); } else if (argsLength != 0) { throw new ArgumentException($"Unexpected args length {argsLength}", nameof(argsLength)); } if (annotations) { var annots = ReadString(); if (annots.Length > 0) { prim.Annots = annots .Split(' ') .Select(a => { var annotVal = a.Substring(1); IAnnotation annotation; switch (a[0]) { case FieldAnnotation.Prefix: annotation = new FieldAnnotation(annotVal); break; case TypeAnnotation.Prefix: annotation = new TypeAnnotation(annotVal); break; case VariableAnnotation.Prefix: annotation = new VariableAnnotation(annotVal); break; default: throw new InvalidOperationException($"Unknown annotation type: {a[0]}"); } return(annotation); }) .ToList(); } } return(prim); }
public TypeAnnotation(Qualident qualident, TypeAnnotation next) => (Qualident, Next) = (qualident, next);
internal static void specific_type_parameter_annotation(this TextWriter trapFile, TypeParameterConstraints constraints, Type baseType, TypeAnnotation annotation) { trapFile.WriteTuple("specific_type_parameter_annotation", constraints, baseType, (int)annotation); }
internal static Tuple cil_type_annotation(IExtractedEntity receiver, TypeAnnotation annotation) => new Tuple("cil_type_annotation", receiver, (int)annotation);
internal static Tuple specific_type_parameter_annotation(TypeParameterConstraints constraints, Type baseType, TypeAnnotation annotation) => new Tuple("specific_type_parameter_annotation", constraints, baseType, (int)annotation);
static bool IsIterableType(TypeAnnotation type) { return type.ObjType == ObjectTypes.Dict || type.ObjType == ObjectTypes.List || type.ObjType == ObjectTypes.Tuple; }