protected override TypeVariableReference ComputeTypeToUnifyWith(VariableReference inputFacadeVariable, out bool setExpectedMutable)
            {
                setExpectedMutable = false;
                TypeVariableReference other = inputFacadeVariable.TypeVariableReference;
                TypeVariableReference u, l;
                bool otherIsMutableReference;
                bool otherIsReference                = TypeVariableSet.TryDecomposeReferenceType(other, out u, out l, out otherIsMutableReference);
                TypeVariableReference underlyingType = otherIsReference ? u : other;

                NIType literalType;
                bool   underlyingTypeIsLiteral   = TypeVariableSet.TryGetLiteralType(underlyingType, out literalType);
                bool   inputCoercesToStringSlice = underlyingTypeIsLiteral &&
                                                   (literalType == PFTypes.String || literalType == DataTypes.StringSliceType);
                bool needsBorrow = !otherIsReference || !underlyingTypeIsLiteral || literalType != DataTypes.StringSliceType;
                TypeVariableReference lifetimeType = otherIsReference
                    ? l
                    : TypeVariableSet.CreateReferenceToLifetimeType(_group.BorrowLifetime);

                if (needsBorrow)
                {
                    _stringToSliceNeeded = true;
                    _group.SetBorrowRequired(false);
                }
                // If the input is allowed to coerce to a str, we can unify with str;
                // otherwise, unify with the underlying type to get a type mismatch.
                TypeVariableReference toUnifyUnderlyingType = inputCoercesToStringSlice
                    ? TypeVariableSet.CreateReferenceToLiteralType(DataTypes.StringSliceType)
                    : underlyingType;

                return(TypeVariableSet.CreateReferenceToReferenceType(
                           false,
                           toUnifyUnderlyingType,
                           lifetimeType));
            }
            private void InsertStringToSliceAheadOfTerminal(Terminal sliceReceiver, out VariableReference stringReferenceVariable, out Terminal stringReferenceTerminal)
            {
                FunctionalNode stringToSlice                = new FunctionalNode(sliceReceiver.ParentDiagram, Signatures.StringToSliceType);
                Terminal       stringToSliceInput           = stringToSlice.InputTerminals[0],
                               stringToSliceOutput          = stringToSlice.OutputTerminals[0];
                VariableReference sliceReceiverTrueVariable = sliceReceiver.GetTrueVariable();

                TypeVariableSet       typeVariableSet = stringToSliceInput.GetTypeVariableSet();
                TypeVariableReference stringSliceReferenceType = sliceReceiverTrueVariable.TypeVariableReference;
                TypeVariableReference u, lifetime;
                bool m;

                typeVariableSet.TryDecomposeReferenceType(stringSliceReferenceType, out u, out lifetime, out m);
                TypeVariableReference stringReferenceType = typeVariableSet.CreateReferenceToReferenceType(
                    false,
                    typeVariableSet.CreateReferenceToLiteralType(PFTypes.String),
                    lifetime);

                AutoBorrowNodeFacade stringToSliceFacade = AutoBorrowNodeFacade.GetNodeFacade(stringToSlice);

                stringToSliceFacade[stringToSliceInput]  = new SimpleTerminalFacade(stringToSliceInput, stringReferenceType);
                stringToSliceFacade[stringToSliceOutput] = new SimpleTerminalFacade(stringToSliceOutput, default(TypeVariableReference));

                sliceReceiver.ConnectedTerminal.ConnectTo(stringToSliceInput);
                stringToSliceOutput.WireTogether(sliceReceiver, SourceModelIdSource.NoSourceModelId);

                stringToSliceOutput.GetFacadeVariable().MergeInto(sliceReceiverTrueVariable);
                stringReferenceVariable = stringToSliceInput.GetFacadeVariable();
                stringReferenceTerminal = stringToSliceInput;
            }
Exemplo n.º 3
0
        bool IDfirNodeVisitor <bool> .VisitConstant(Constant constant)
        {
            Terminal valueOutput = constant.OutputTerminals.ElementAt(0);
            TypeVariableReference constantTypeReference;

            if (constant.DataType.IsRebarReferenceType())
            {
                constantTypeReference = _typeVariableSet.CreateReferenceToReferenceType(
                    false,
                    // TODO: this is not always correct; need a more general way of turning NITypes into TypeVariableReferences
                    _typeVariableSet.CreateTypeVariableReferenceFromNIType(constant.DataType.GetReferentType()),
                    // Assume for now that the reference will be in Lifetime.Static
                    _typeVariableSet.CreateReferenceToLifetimeType(Lifetime.Static));
            }
            else
            {
                constantTypeReference = _typeVariableSet.CreateTypeVariableReferenceFromNIType(constant.DataType);
            }
            _nodeFacade[valueOutput] = new SimpleTerminalFacade(valueOutput, constantTypeReference);
            return(true);
        }
Exemplo n.º 4
0
        public void TypeVariableWithCopyConstraintAndNonCopyableType_Unify_FailedConstraintReported()
        {
            TypeVariableSet       typeVariableSet  = new TypeVariableSet();
            TypeVariableReference literalReference = typeVariableSet.CreateReferenceToReferenceType(
                true,
                typeVariableSet.CreateTypeVariableReferenceFromNIType(NITypes.Int32),
                typeVariableSet.CreateReferenceToLifetimeType(Lifetime.Static));
            var constraint = new SimpleTraitConstraint("Copy");
            TypeVariableReference typeVariable = typeVariableSet.CreateReferenceToNewTypeVariable(constraint.ToEnumerable());
            var testTypeUnificationResult      = new TestTypeUnificationResult();

            typeVariableSet.Unify(typeVariable, literalReference, testTypeUnificationResult);

            Assert.IsTrue(testTypeUnificationResult.FailedConstraints.Contains(constraint));
        }
        public void AddTerminalFacade(Terminal inputTerminal, TypeVariableReference referentTypeReference, TypeVariableReference mutabilityTypeReference, Terminal terminateLifetimeOutputTerminal = null)
        {
            TypeVariableReference referenceType;
            TypeVariableSet       typeVariableSet = inputTerminal.GetTypeVariableSet();

            if (_mutability == InputReferenceMutability.Polymorphic)
            {
                referenceType = typeVariableSet.CreateReferenceToPolymorphicReferenceType(
                    mutabilityTypeReference,
                    referentTypeReference,
                    LifetimeType);
            }
            else
            {
                referenceType = typeVariableSet.CreateReferenceToReferenceType(
                    (_mutability != InputReferenceMutability.AllowImmutable),
                    referentTypeReference,
                    LifetimeType);
            }
            if (_lazyBorrowLifetime.IsValueCreated)
            {
                throw new InvalidOperationException("Cannot add borrowed variables after creating new lifetime.");
            }

            NIType literalReferentType;
            bool   isStringSliceReference = typeVariableSet.TryGetLiteralType(referentTypeReference, out literalReferentType) &&
                                            literalReferentType == DataTypes.StringSliceType;
            TerminalFacade terminalFacade;

            if (isStringSliceReference)
            {
                terminalFacade = new StringSliceReferenceInputTerminalFacade(inputTerminal, this, referenceType);
            }
            else
            {
                terminalFacade = new ReferenceInputTerminalFacade(inputTerminal, _mutability, this, referenceType);
            }
            _nodeFacade[inputTerminal] = terminalFacade;
            _facades.Add(terminalFacade);
            if (terminateLifetimeOutputTerminal != null)
            {
                var outputFacade = new TerminateLifetimeOutputTerminalFacade(terminateLifetimeOutputTerminal, terminalFacade);
                _nodeFacade[terminateLifetimeOutputTerminal] = outputFacade;
            }
        }
            protected override TypeVariableReference ComputeTypeToUnifyWith(VariableReference inputFacadeVariable, out bool setExpectedMutable)
            {
                TypeVariableReference other = inputFacadeVariable.TypeVariableReference;
                TypeVariableReference u, otherReferenceLifetime;
                bool otherIsMutableReference;
                bool otherIsReference                = TypeVariableSet.TryDecomposeReferenceType(other, out u, out otherReferenceLifetime, out otherIsMutableReference);
                TypeVariableReference underlyingType = otherIsReference ? u : other;
                bool mutable = otherIsReference ? otherIsMutableReference : inputFacadeVariable.Mutable;

                TypeVariableReference typeToUnifyWith = default(TypeVariableReference);

                setExpectedMutable = false;
                switch (_mutability)
                {
                case InputReferenceMutability.RequireMutable:
                {
                    TypeVariableReference lifetimeType;
                    if (!otherIsReference)
                    {
                        _group.SetBorrowRequired(true);
                        lifetimeType = TypeVariableSet.CreateReferenceToLifetimeType(_group.BorrowLifetime);
                    }
                    else
                    {
                        lifetimeType = otherReferenceLifetime;
                    }
                    typeToUnifyWith    = TypeVariableSet.CreateReferenceToReferenceType(true, underlyingType, lifetimeType);
                    setExpectedMutable = !mutable;
                    break;
                }

                case InputReferenceMutability.AllowImmutable:
                {
                    bool needsBorrow = !(otherIsReference && !otherIsMutableReference);
                    TypeVariableReference lifetimeType;
                    if (needsBorrow)
                    {
                        lifetimeType = TypeVariableSet.CreateReferenceToLifetimeType(_group.BorrowLifetime);
                        _group.SetBorrowRequired(false);
                    }
                    else
                    {
                        lifetimeType = otherReferenceLifetime;
                    }
                    typeToUnifyWith = TypeVariableSet.CreateReferenceToReferenceType(false, underlyingType, lifetimeType);
                    break;
                }

                case InputReferenceMutability.Polymorphic:
                {
                    TypeVariableReference lifetimeType;
                    if (!otherIsReference)
                    {
                        _group.SetBorrowRequired(false /* TODO depends on current state and input mutability */);
                        lifetimeType = TypeVariableSet.CreateReferenceToLifetimeType(_group.BorrowLifetime);
                    }
                    else
                    {
                        // TODO: if TrueVariable.TypeVariableReference is already known to be immutable and
                        // wire reference is mutable, then we need a borrow
                        lifetimeType = otherReferenceLifetime;
                    }
                    typeToUnifyWith = TypeVariableSet.CreateReferenceToReferenceType(mutable, underlyingType, lifetimeType);
                    break;
                }
                }
                return(typeToUnifyWith);
            }