Exemple #1
0
        public override void ValidateConstraintForType(TypeVariableReference type, ITypeUnificationResult unificationResult)
        {
            NIType implementedIteratorInterface;

            // TODO: using NITypes here to destructure the iterator interface and reconstruct a TypeReference for the item type
            // is an incomplete solution; it will not work for item types that have bounded lifetimes. Also, currently there's
            // no way to create TypeReferences for non-reference types that have bounded lifetimes, as an iterator implementation
            // whose items are references will necessarily have.
            //
            // What is needed is a way of defining a generic parameterized TypeVariableReference, and then a reference to a specialization of that type.
            // Then, as long as the parameterized type can include an interface implementation, we should be able to get the specialized
            // interface implementation from the specialization.
            if (type.RenderNIType().TryGetImplementedIteratorInterface(out implementedIteratorInterface))
            {
                NIType itemType;
                implementedIteratorInterface.TryDestructureIteratorType(out itemType);
                TypeVariableReference itemTypeReference = type.TypeVariableSet.CreateTypeVariableReferenceFromNIType(
                    itemType,
                    new Dictionary <NIType, TypeVariableReference>());
                type.TypeVariableSet.Unify(_itemTypeVariable, itemTypeReference, unificationResult);
            }
            else
            {
                unificationResult.AddFailedTypeConstraint(this);
            }
        }
Exemple #2
0
 public override void ValidateConstraintForType(TypeVariableReference type, ITypeUnificationResult unificationResult)
 {
     if (!type.RenderNIType().TypeHasDisplayTrait())
     {
         unificationResult.AddFailedTypeConstraint(this);
     }
 }
Exemple #3
0
 public override void ValidateConstraintForType(TypeVariableReference type, ITypeUnificationResult unificationResult)
 {
     if (!type.Lifetime.DoesOutlastLifetimeGraph(_lifetimeGraph))
     {
         unificationResult.AddFailedTypeConstraint(this);
     }
 }
Exemple #4
0
        public VariableReference CreateNewVariable(int diagramId, TypeVariableReference variableType, bool mutable = false)
        {
            int      variableId = _currentVariableReferenceId++;
            Variable variable   = CreateNewVariable(mutable, variableId, diagramId, variableType);

            SetVariableAtReferenceIndex(variable, variableId);
            return(new VariableReference(this, variableId));
        }
Exemple #5
0
 public override void ValidateConstraintForType(TypeVariableReference type, ITypeUnificationResult unificationResult)
 {
     // TODO: probably not great to render an NIType at this stage
     if (!type.RenderNIType().WireTypeMayFork())
     {
         unificationResult.AddFailedTypeConstraint(this);
     }
 }
Exemple #6
0
 public Variable(int id, int firstReferenceIndex, int diagramId, TypeVariableReference variableType, bool mutable)
 {
     Id = id;
     FirstReferenceIndex   = firstReferenceIndex;
     DiagramId             = diagramId;
     TypeVariableReference = variableType;
     Mutable = mutable;
 }
Exemple #7
0
        private void UnifyTypeVariableWithNonTypeVariable(TypeVariableReference typeVariable, TypeVariableReference nonTypeVariable, ITypeUnificationResult unificationResult)
        {
            var t = (TypeVariable)GetTypeForTypeVariableReference(typeVariable);

            foreach (Constraint constraint in t.Constraints)
            {
                constraint.ValidateConstraintForType(nonTypeVariable, unificationResult);
            }
            MergeTypeVariableIntoTypeVariable(typeVariable, nonTypeVariable);
        }
Exemple #8
0
        public bool GetMutable(TypeVariableReference type)
        {
            var mutabilityType = GetTypeForTypeVariableReference(type) as MutabilityTypeVariable;

            if (mutabilityType == null)
            {
                throw new ArgumentException("Type should be a mutability type");
            }
            return(mutabilityType.Mutable);
        }
Exemple #9
0
        public void AndWith(TypeVariableReference type, bool value)
        {
            var mutabilityType = GetTypeForTypeVariableReference(type) as MutabilityTypeVariable;

            if (mutabilityType == null)
            {
                throw new ArgumentException("Type should be a mutability type");
            }
            mutabilityType.AndWith(value);
        }
Exemple #10
0
        internal TypeVariableReference GetTypeVariableReference(VariableReference variableReference)
        {
            Variable variable = GetVariableForVariableReference(variableReference);
            TypeVariableReference typeVariableReference = variable.TypeVariableReference;

            if (typeVariableReference.TypeVariableSet == null)
            {
                throw new ArgumentException("Getting TypeVariableReference for a variable that hasn't set one.");
            }
            return(typeVariableReference);
        }
Exemple #11
0
        public bool TryGetLiteralType(TypeVariableReference type, out NIType literalType)
        {
            TypeBase typeBase             = GetTypeForTypeVariableReference(type);
            var      literalTypeReference = typeBase as LiteralType;

            if (literalTypeReference == null)
            {
                literalType = NIType.Unset;
                return(false);
            }
            literalType = literalTypeReference.Type;
            return(true);
        }
Exemple #12
0
        public bool TryDecomposeConstructorType(TypeVariableReference type, out string constructorName, out TypeVariableReference argument)
        {
            TypeBase typeBase        = GetTypeForTypeVariableReference(type);
            var      constructorType = typeBase as ConstructorType;

            if (constructorType == null)
            {
                constructorName = null;
                argument        = default(TypeVariableReference);
                return(false);
            }
            constructorName = constructorType.ConstructorName;
            argument        = constructorType.Argument;
            return(true);
        }
Exemple #13
0
        public bool TryDecomposeReferenceType(TypeVariableReference type, out TypeVariableReference underlyingType, out TypeVariableReference lifetimeType, out bool mutable)
        {
            TypeBase typeBase      = GetTypeForTypeVariableReference(type);
            var      referenceType = typeBase as ReferenceType;

            if (referenceType == null)
            {
                underlyingType = lifetimeType = default(TypeVariableReference);
                mutable        = false;
                return(false);
            }
            underlyingType = referenceType.UnderlyingType;
            lifetimeType   = referenceType.LifetimeType;
            mutable        = referenceType.Mutable;
            return(true);
        }
Exemple #14
0
        private void MergeTypeVariableIntoTypeVariable(TypeVariableReference toMerge, TypeVariableReference mergeInto)
        {
            TypeBase typeToMerge     = GetTypeForTypeVariableReference(toMerge),
                     typeToMergeInto = GetTypeForTypeVariableReference(mergeInto);

            if (typeToMerge != null && typeToMergeInto != null)
            {
                for (int i = 0; i < _typeReferences.Count; ++i)
                {
                    if (_typeReferences[i] == typeToMerge)
                    {
                        _typeReferences[i] = typeToMergeInto;
                    }
                }
            }
            _types.Remove(typeToMerge);
        }
Exemple #15
0
        public static TypeVariableReference CreateReferenceToGenericTypeSpecializedWithTypeParameters(
            this TypeVariableSet typeVariableSet,
            NIType genericTypeDefinition,
            params TypeVariableReference[] typeParameters)
        {
            if (typeParameters.Length != genericTypeDefinition.GetGenericParameters().Count)
            {
                throw new ArgumentException("Wrong number of parameter type variables; expected " + genericTypeDefinition.GetGenericParameters().Count);
            }
            var genericTypeParameters = new Dictionary <NIType, TypeVariableReference>();

            foreach (var pair in genericTypeDefinition.GetGenericParameters().Zip(typeParameters))
            {
                NIType genericParameter = pair.Key;
                TypeVariableReference parameterTypeVariable = pair.Value;
                genericTypeParameters[genericParameter] = parameterTypeVariable;
            }
            return(typeVariableSet.CreateTypeVariableReferenceFromNIType(genericTypeDefinition, genericTypeParameters));
        }
Exemple #16
0
        public static TypeVariableReference CreateTypeVariableReferenceFromNIType(
            this TypeVariableSet typeVariableSet,
            NIType type,
            Dictionary <NIType, TypeVariableReference> genericTypeParameters)
        {
            if (type.IsGenericParameter())
            {
                return(genericTypeParameters[type]);
            }

            if (type.IsInterface())
            {
                return(typeVariableSet.CreateTypeVariableReferenceFromInterfaceNIType(type, genericTypeParameters));
            }

            if (type.IsCluster())
            {
                TypeVariableReference[] elementTypes = type.GetFields()
                                                       .Select(t => typeVariableSet.CreateTypeVariableReferenceFromNIType(t.GetDataType(), genericTypeParameters))
                                                       .ToArray();
                return(typeVariableSet.CreateReferenceToTupleType(elementTypes));
            }

            if (!type.IsClass() && !type.IsUnion())
            {
                return(typeVariableSet.CreateTypeVariableReferenceFromPrimitiveType(type, genericTypeParameters));
            }

            if (type.IsRebarReferenceType())
            {
                TypeVariableReference referentType = typeVariableSet.CreateTypeVariableReferenceFromNIType(type.GetReferentType(), genericTypeParameters);
                TypeVariableReference lifetimeType = typeVariableSet.CreateTypeVariableReferenceFromNIType(type.GetReferenceLifetimeType(), genericTypeParameters);
                if (type.IsPolymorphicReferenceType())
                {
                    TypeVariableReference mutabilityType = typeVariableSet.CreateTypeVariableReferenceFromNIType(type.GetReferenceMutabilityType(), genericTypeParameters);
                    return(typeVariableSet.CreateReferenceToPolymorphicReferenceType(mutabilityType, referentType, lifetimeType));
                }
                return(typeVariableSet.CreateReferenceToReferenceType(type.IsMutableReferenceType(), referentType, lifetimeType));
            }

            return(typeVariableSet.CreateTypeVariableReferenceFromClassOrUnionNIType(type, genericTypeParameters));
        }
Exemple #17
0
 public static TypeVariableReference CreateTypeVariableReferenceFromNIType(
     this TypeVariableSet typeVariableSet,
     NIType type,
     Dictionary <NIType, TypeVariableReference> genericTypeParameters)
 {
     if (type.IsGenericParameter())
     {
         return(genericTypeParameters[type]);
     }
     else if (!type.IsGenericType())
     {
         return(typeVariableSet.CreateReferenceToLiteralType(type));
     }
     else
     {
         if (type.IsRebarReferenceType())
         {
             TypeVariableReference referentType = typeVariableSet.CreateTypeVariableReferenceFromNIType(type.GetReferentType(), genericTypeParameters);
             TypeVariableReference lifetimeType = typeVariableSet.CreateTypeVariableReferenceFromNIType(type.GetReferenceLifetimeType(), genericTypeParameters);
             if (type.IsPolymorphicReferenceType())
             {
                 TypeVariableReference mutabilityType = typeVariableSet.CreateTypeVariableReferenceFromNIType(type.GetReferenceMutabilityType(), genericTypeParameters);
                 return(typeVariableSet.CreateReferenceToPolymorphicReferenceType(mutabilityType, referentType, lifetimeType));
             }
             return(typeVariableSet.CreateReferenceToReferenceType(type.IsMutableReferenceType(), referentType, lifetimeType));
         }
         string constructorTypeName   = type.GetName();
         var    constructorParameters = type.GetGenericParameters();
         if (constructorParameters.Count == 1)
         {
             TypeVariableReference parameterType = typeVariableSet.CreateTypeVariableReferenceFromNIType(constructorParameters.ElementAt(0), genericTypeParameters);
             return(typeVariableSet.CreateReferenceToConstructorType(constructorTypeName, parameterType));
         }
         throw new NotImplementedException();
     }
 }
Exemple #18
0
 private TypeBase GetTypeForTypeVariableReference(TypeVariableReference typeVariableReference)
 {
     return(_typeReferences[typeVariableReference.ReferenceIndex]);
 }
Exemple #19
0
 public ConstructorType(string constructorName, TypeVariableReference argument)
 {
     ConstructorName = constructorName;
     Argument        = argument;
 }
Exemple #20
0
        public void Unify(TypeVariableReference toUnify, TypeVariableReference toUnifyWith, ITypeUnificationResult unificationResult)
        {
            TypeBase toUnifyTypeBase     = GetTypeForTypeVariableReference(toUnify),
                     toUnifyWithTypeBase = GetTypeForTypeVariableReference(toUnifyWith);

            LiteralType toUnifyLiteral     = toUnifyTypeBase as LiteralType,
                        toUnifyWithLiteral = toUnifyWithTypeBase as LiteralType;

            if (toUnifyLiteral != null && toUnifyWithLiteral != null)
            {
                if (toUnifyLiteral.Type == toUnifyWithLiteral.Type)
                {
                    MergeTypeVariableIntoTypeVariable(toUnify, toUnifyWith);
                    return;
                }
                unificationResult.SetTypeMismatch();
                return;
            }

            ConstructorType toUnifyConstructor     = toUnifyTypeBase as ConstructorType,
                            toUnifyWithConstructor = toUnifyWithTypeBase as ConstructorType;

            if (toUnifyConstructor != null && toUnifyWithConstructor != null)
            {
                if (toUnifyConstructor.ConstructorName == toUnifyWithConstructor.ConstructorName)
                {
                    Unify(toUnifyConstructor.Argument, toUnifyWithConstructor.Argument, unificationResult);
                    MergeTypeVariableIntoTypeVariable(toUnify, toUnifyWith);
                    return;
                }
                unificationResult.SetTypeMismatch();
                return;
            }

            ReferenceType toUnifyReference     = toUnifyTypeBase as ReferenceType,
                          toUnifyWithReference = toUnifyWithTypeBase as ReferenceType;

            if (toUnifyReference != null && toUnifyWithReference != null)
            {
                toUnifyReference.UnifyMutability(toUnifyWithReference);
                Unify(toUnifyReference.UnderlyingType, toUnifyWithReference.UnderlyingType, unificationResult);
                Unify(toUnifyReference.LifetimeType, toUnifyWithReference.LifetimeType, unificationResult);
                return;
            }

            LifetimeTypeContainer toUnifyLifetime     = toUnifyTypeBase as LifetimeTypeContainer,
                                  toUnifyWithLifetime = toUnifyWithTypeBase as LifetimeTypeContainer;

            if (toUnifyLifetime != null && toUnifyWithLifetime != null)
            {
                // toUnify is the possible supertype container here
                toUnifyLifetime.AdoptLifetimeIfPossible(toUnifyWithLifetime.LifetimeValue);
                return;
            }

            TypeVariable toUnifyTypeVariable     = toUnifyTypeBase as TypeVariable,
                         toUnifyWithTypeVariable = toUnifyWithTypeBase as TypeVariable;

            if (toUnifyTypeVariable != null && toUnifyWithTypeVariable != null)
            {
                toUnifyWithTypeVariable.AdoptConstraintsFromVariable(toUnifyTypeVariable);
                MergeTypeVariableIntoTypeVariable(toUnify, toUnifyWith);
                return;
            }
            if (toUnifyTypeVariable != null)
            {
                UnifyTypeVariableWithNonTypeVariable(toUnify, toUnifyWith, unificationResult);
                return;
            }
            if (toUnifyWithTypeVariable != null)
            {
                UnifyTypeVariableWithNonTypeVariable(toUnifyWith, toUnify, unificationResult);
                return;
            }

            unificationResult.SetTypeMismatch();
            return;
        }
Exemple #21
0
 public IteratorTraitConstraint(TypeVariableReference itemTypeVariable)
 {
     _itemTypeVariable = itemTypeVariable;
 }
Exemple #22
0
        public NIType RenderNIType(TypeVariableReference typeVariableReference)
        {
            TypeBase typeBase = GetTypeForTypeVariableReference(typeVariableReference);

            return(typeBase?.RenderNIType() ?? PFTypes.Void);
        }
Exemple #23
0
 public TypeVariableReference CreateReferenceToPolymorphicReferenceType(TypeVariableReference mutabilityType, TypeVariableReference underlyingType, TypeVariableReference lifetimeType)
 {
     return(CreateReferenceToNewType(new ReferenceType(mutabilityType, underlyingType, lifetimeType)));
 }
Exemple #24
0
        private Variable CreateNewVariable(bool mutableVariable, int firstReferenceIndex, int diagramId, TypeVariableReference variableType)
        {
            int variableId = _currentVariableId;

            _currentVariableId++;
            var variable = new Variable(variableId, firstReferenceIndex, diagramId, variableType, mutableVariable);

            _variables.Add(variable);
            return(variable);
        }
Exemple #25
0
        public string GetDebuggerDisplay(TypeVariableReference typeVariableReference)
        {
            TypeBase typeBase = GetTypeForTypeVariableReference(typeVariableReference);

            return(typeBase?.DebuggerDisplay ?? "invalid");
        }
Exemple #26
0
        public Lifetime GetLifetime(TypeVariableReference typeVariableReference)
        {
            TypeBase typeBase = GetTypeForTypeVariableReference(typeVariableReference);

            return(typeBase?.Lifetime ?? Lifetime.Empty);
        }
Exemple #27
0
 public static TypeVariableReference CreateReferenceToOptionType(this TypeVariableSet typeVariableSet, TypeVariableReference innerType)
 {
     return(typeVariableSet.CreateReferenceToGenericTypeSpecializedWithTypeParameters(DataTypes.OptionGenericType, innerType));
 }
Exemple #28
0
 public bool GetIsOrContainsTypeVariable(TypeVariableReference type)
 {
     return(GetTypeForTypeVariableReference(type).IsOrContainsTypeVariable());
 }
Exemple #29
0
 public abstract void ValidateConstraintForType(TypeVariableReference type, ITypeUnificationResult unificationResult);
Exemple #30
0
 public TypeVariableReference CreateReferenceToReferenceType(bool mutable, TypeVariableReference underlyingType, TypeVariableReference lifetimeType)
 {
     return(CreateReferenceToNewType(new ReferenceType(mutable, underlyingType, lifetimeType)));
 }