예제 #1
0
                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);
                }
예제 #2
0
                // 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);
                    }
                }
예제 #3
0
                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);
                }
예제 #4
0
        /// <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();
            }
        }
예제 #5
0
 private void InitializeInferredTypes(IEnumerable<IGenericParameter> parameters)
 {
     foreach (IGenericParameter gp in parameters)
     {
         _inferredTypes[gp] = new InferredType();
     }
 }
예제 #6
0
 private bool HasNoDependencies(InferredType inferredType)
 {
     return !inferredType.HasDependencies;
 }
예제 #7
0
 private bool HasDependantsAndBounds(InferredType inferredType)
 {
     return inferredType.HasDependants && inferredType.HasBounds;
 }
예제 #8
0
 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;
 }
예제 #9
0
        /// <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(')');
        }