public IReadOnlyDictionary <NameKey, GenericTypeParameter> HasGenerics(IValue value, NameKey[] keys) { InferredType?inferredType; if (value.Hopeful.Is(out var innerInferredType)) { inferredType = innerInferredType; } else { var inferredMethodType = new InferredType(this, "generated infered method type"); value.Hopeful = Possibly.Is(inferredMethodType); inferredType = inferredMethodType; } if (inferredType.Generics.Count > 0) { throw new Exception("this already has generics!"); } var i = 0; foreach (var key in keys) { inferredType.Generics.Add(key, new GenericTypeParameter(this, $"generic-parameter-{key}", i++, Prototypist.Toolbox.OrType.Make <MethodType, Type, Method, InferredType>(inferredType))); } return(inferredType.Generics); }
// TODO // {A48C75B3-07B4-4D84-8803-250D6406695D} // this is a bit weird // it is get or create // but like if something as already made a hopeful member it will just throw // that is probably a bug // x.y := 5 // 5 > x // boom, probably public TransientMember GetReturns(IValue value) { if (value.Hopeful is IIsDefinately <InferredType> inferredType) { return(inferredType.Value.Returns.GetOrThrow()); } else { var inferredMethodType = new InferredType(this, "generated infered method type"); value.Hopeful = Possibly.Is(inferredMethodType); // shared code {A9E37392-760B-427D-852E-8829EEFCAE99} // we don't use has member input/output doesn't go in the member list // it is not a public member // and infered to do not have private members var methodInputKey = new NameKey("generated infered method input - " + Guid.NewGuid()); var inputMember = new Member(this, methodInputKey.ToString() !); inputMember.LooksUp = Possibly.Is(Prototypist.Toolbox.OrType.Make <MethodType, Type, Object, OrType, InferredType, GenericTypeParameter, IError>(new InferredType(this, "implicit input"))); inferredMethodType.Input = Possibly.Is(inputMember); var returnMember = new TransientMember(this, "generated infered method return -" + Guid.NewGuid()); returnMember.LooksUp = Possibly.Is(Prototypist.Toolbox.OrType.Make <MethodType, Type, Object, OrType, InferredType, GenericTypeParameter, IError>(new InferredType(this, "implicit return"))); inferredMethodType.Returns = Possibly.Is(returnMember); return(returnMember); } }
public Member HasHopefulMember(IValue parent, IKey key, Func <Member> member) { if (!parent.Hopeful.Is(out var inferredType)) { inferredType = new InferredType(this, $"hopeful of {parent}"); parent.Hopeful = Possibly.Is(inferredType); } if (inferredType.PublicMembers.TryGetValue(key, out var res)) { return(res); } res = member(); inferredType.PublicMembers.Add(key, res); return(res); }
/// <summary> /// Write human readable output. /// </summary> public override void WriteTo(ITextOutput output, FormattingOptions format) { if (format.HasFlag(FormattingOptions.ShowHasSeqPoint) && SourceLocation != NoSource) { output.Write(SourceLocation.IsSpecial ? "!" : "~"); } if (Operand is AstVariable && ((AstVariable)Operand).IsGenerated) { if (Code == AstCode.Stloc && InferredType == null) { output.Write(((AstVariable)Operand).Name); output.Write(" = "); Arguments.First().WriteTo(output, format); return; } if (Code == AstCode.Ldloc) { output.Write(((AstVariable)Operand).Name); if (InferredType != null) { output.Write(':'); InferredType.WriteTo(output, AstNameSyntax.ShortTypeName); if ((ExpectedType != null) && (ExpectedType.FullName != this.InferredType.FullName)) { output.Write("[exp:"); ExpectedType.WriteTo(output, AstNameSyntax.ShortTypeName); output.Write(']'); } } return; } } if (Prefixes != null) { foreach (var prefix in Prefixes) { output.Write(prefix.Code.GetName()); output.Write(". "); } } output.Write(Code.GetName()); if (InferredType != null) { output.Write(':'); InferredType.WriteTo(output, AstNameSyntax.ShortTypeName); if ((ExpectedType != null) && (ExpectedType.FullName != InferredType.FullName)) { output.Write("[exp:"); ExpectedType.WriteTo(output, AstNameSyntax.ShortTypeName); output.Write(']'); } } else if (ExpectedType != null) { output.Write("[exp:"); ExpectedType.WriteTo(output, AstNameSyntax.ShortTypeName); output.Write(']'); } output.Write('('); var first = true; if (Operand != null) { if (Operand is AstLabel) { output.WriteReference(((AstLabel)Operand).Name, Operand); } else if (Operand is AstLabel[]) { var labels = (AstLabel[])Operand; for (var i = 0; i < labels.Length; i++) { if (i > 0) { output.Write(", "); } output.WriteReference(labels[i].Name, labels[i]); } } else if (Operand is AstLabelKeyPair[]) { var pairs = (AstLabelKeyPair[])Operand; for (var i = 0; i < pairs.Length; i++) { if (i > 0) { output.Write(", "); } output.Write("{0} -> ", pairs[i].Key); output.WriteReference(pairs[i].Label.Name, pairs[i].Label); } } else if (Operand is XMethodReference) { var method = (XMethodReference)Operand; if (method.DeclaringType != null) { method.DeclaringType.WriteTo(output, AstNameSyntax.ShortTypeName); output.Write("::"); } output.WriteReference(method.Name, method); } else if (Operand is XFieldReference) { var field = (XFieldReference)Operand; field.DeclaringType.WriteTo(output, AstNameSyntax.ShortTypeName); output.Write("::"); output.WriteReference(field.Name, field); } else { DisassemblerHelpers.WriteOperand(output, Operand); } first = false; } bool firstArg = true; foreach (var arg in Arguments) { if (!first) { output.Write(", "); } if ((format & FormattingOptions.BreakExpressions) != 0) { if (firstArg) { output.Indent(); } output.WriteLine(); } arg.WriteTo(output, format); first = false; firstArg = false; } output.Write(')'); if ((format & FormattingOptions.BreakExpressions) != 0 && !firstArg) { output.Unindent(); } }
private void InitializeInferredTypes(IEnumerable<IGenericParameter> parameters) { foreach (IGenericParameter gp in parameters) { _inferredTypes[gp] = new InferredType(); } }
private bool HasNoDependencies(InferredType inferredType) { return !inferredType.HasDependencies; }
private bool HasDependantsAndBounds(InferredType inferredType) { return inferredType.HasDependants && inferredType.HasBounds; }
private bool Fix(IGenericParameter genericParameter, InferredType inferredType) { if (inferredType.Fix()) { _context.TraceVerbose("Generic parameter {0} fixed to {1}.", genericParameter.Name, inferredType.ResultingType); return true; } return false; }
/// <summary> /// Write human readable output. /// </summary> public override void WriteTo(ITextOutput output) { if (Operand is AstVariable && ((AstVariable)Operand).IsGenerated) { if (Code == AstCode.Stloc && InferredType == null) { output.Write(((AstVariable)Operand).Name); output.Write(" = "); Arguments.First().WriteTo(output); return; } if (Code == AstCode.Ldloc) { output.Write(((AstVariable)Operand).Name); if (InferredType != null) { output.Write(':'); InferredType.WriteTo(output, AstNameSyntax.ShortTypeName); if ((ExpectedType != null) && (ExpectedType.FullName != this.InferredType.FullName)) { output.Write("[exp:"); ExpectedType.WriteTo(output, AstNameSyntax.ShortTypeName); output.Write(']'); } } return; } } if (Prefixes != null) { foreach (var prefix in Prefixes) { output.Write(prefix.Code.GetName()); output.Write(". "); } } output.Write(Code.GetName()); if (InferredType != null) { output.Write(':'); InferredType.WriteTo(output, AstNameSyntax.ShortTypeName); if ((ExpectedType != null) && (ExpectedType.FullName != InferredType.FullName)) { output.Write("[exp:"); ExpectedType.WriteTo(output, AstNameSyntax.ShortTypeName); output.Write(']'); } } else if (ExpectedType != null) { output.Write("[exp:"); ExpectedType.WriteTo(output, AstNameSyntax.ShortTypeName); output.Write(']'); } output.Write('('); var first = true; if (Operand != null) { if (Operand is AstLabel) { output.WriteReference(((AstLabel)Operand).Name, Operand); } else if (Operand is AstLabel[]) { var labels = (AstLabel[])Operand; for (var i = 0; i < labels.Length; i++) { if (i > 0) { output.Write(", "); } output.WriteReference(labels[i].Name, labels[i]); } } else if (Operand is AstLabelKeyPair[]) { var pairs = (AstLabelKeyPair[])Operand; for (var i = 0; i < pairs.Length; i++) { if (i > 0) { output.Write(", "); } output.Write("{0} -> ", pairs[i].Key); output.WriteReference(pairs[i].Label.Name, pairs[i].Label); } } else if (Operand is XMethodReference) { var method = (XMethodReference)Operand; if (method.DeclaringType != null) { method.DeclaringType.WriteTo(output, AstNameSyntax.ShortTypeName); output.Write("::"); } output.WriteReference(method.Name, method); } else if (Operand is XFieldReference) { var field = (XFieldReference)Operand; field.DeclaringType.WriteTo(output, AstNameSyntax.ShortTypeName); output.Write("::"); output.WriteReference(field.Name, field); } else { DisassemblerHelpers.WriteOperand(output, Operand); } first = false; } foreach (var arg in Arguments) { if (!first) { output.Write(", "); } arg.WriteTo(output); first = false; } output.Write(')'); }