Beispiel #1
0
        private static TypeVariableReference CreateTypeVariableReferenceFromPrimitiveType(
            this TypeVariableSet typeVariableSet,
            NIType type,
            Dictionary <NIType, TypeVariableReference> genericTypeParameters)
        {
            var implementedTraits = new List <TypeVariableReference>();

            if (type.IsInteger() || type.IsBoolean())
            {
                // TODO: cache parameterless trait references?
                implementedTraits.Add(typeVariableSet.CreateReferenceToParameterlessTraitType("Display"));
                implementedTraits.Add(typeVariableSet.CreateReferenceToParameterlessTraitType("Clone"));
                implementedTraits.Add(typeVariableSet.CreateReferenceToParameterlessTraitType("Copy"));
            }
            else if (type.IsString())
            {
                implementedTraits.Add(typeVariableSet.CreateReferenceToParameterlessTraitType("Display"));
                implementedTraits.Add(typeVariableSet.CreateReferenceToParameterlessTraitType("Clone"));
            }
            else
            {
                throw new NotSupportedException("Unknown non-class type: " + type);
            }
            return(typeVariableSet.CreateReferenceToConcreteType(
                       type,
                       new TypeVariableReference[0],
                       new Dictionary <string, TypeVariableReference>(),
                       implementedTraits.ToArray()));
        }
Beispiel #2
0
        private static TypeVariableReference CreateTypeVariableReferenceFromClassOrUnionNIType(
            this TypeVariableSet typeVariableSet,
            NIType type,
            Dictionary <NIType, TypeVariableReference> genericTypeParameters)
        {
            TypeVariableReference[] parameterTypeVariables = type.IsGenericType()
                ? type.GetGenericParameters().Select(t => typeVariableSet.CreateTypeVariableReferenceFromNIType(t, genericTypeParameters)).ToArray()
                : new TypeVariableReference[0];
            TypeVariableReference[] traits = typeVariableSet.GetImplementedTraitTypeVariables(type, genericTypeParameters).ToArray();
            var fieldTypes = new Dictionary <string, TypeVariableReference>();

            foreach (NIType fieldType in type.GetFields())
            {
                fieldTypes.Add(fieldType.GetName(), typeVariableSet.CreateTypeVariableReferenceFromNIType(fieldType.GetDataType(), genericTypeParameters));
            }

            // TODO: eventually it would be nice to decorate the generic type definition with [Derive] attributes
            // that say which traits to derive from the inner type, so that this can be made more generic.
            TraitDeriver traitDeriver = null;

            if (type.GetName() == "Option")
            {
                traitDeriver = new TraitDeriver(parameterTypeVariables[0], "Copy");
            }
            if (type.GetName() == "Vector")
            {
                // Vector<T> is Clone only for T : Clone
                traitDeriver = new TraitDeriver(parameterTypeVariables[0], "Clone");
            }

            return(typeVariableSet.CreateReferenceToConcreteType(type, parameterTypeVariables, fieldTypes, traits, traitDeriver));
        }
Beispiel #3
0
        public static Dictionary <NIType, TypeVariableReference> CreateTypeVariablesForGenericParameters(
            this TypeVariableSet typeVariableSet,
            NIType genericType,
            Func <NIType, TypeVariableReference> createLifetimeTypeReference)
        {
            Dictionary <NIType, TypeVariableReference> genericTypeParameters = new Dictionary <NIType, TypeVariableReference>();

            foreach (NIType genericParameterNIType in genericType.GetGenericParameters())
            {
                if (genericParameterNIType.IsGenericParameter())
                {
                    if (genericParameterNIType.IsLifetimeType())
                    {
                        genericTypeParameters[genericParameterNIType] = createLifetimeTypeReference(genericParameterNIType);
                    }
                    else if (genericParameterNIType.IsMutabilityType())
                    {
                        genericTypeParameters[genericParameterNIType] = typeVariableSet.CreateReferenceToMutabilityType();
                    }
                    else
                    {
                        var typeConstraints = genericParameterNIType.GetConstraints().Select(CreateConstraintFromGenericNITypeConstraint).ToList();
                        genericTypeParameters[genericParameterNIType] = typeVariableSet.CreateReferenceToNewTypeVariable(typeConstraints);
                    }
                }
            }
            return(genericTypeParameters);
        }
Beispiel #4
0
        private static TypeVariableReference CreateTypeVariableReferenceFromInterfaceNIType(
            this TypeVariableSet typeVariableSet,
            NIType type,
            Dictionary <NIType, TypeVariableReference> genericTypeParameters)
        {
            string typeName = type.GetName();

            if (type.IsGenericType())
            {
                TypeVariableReference[] parameterTypeVariables = type
                                                                 .GetGenericParameters()
                                                                 .Select(t => typeVariableSet.CreateTypeVariableReferenceFromNIType(t, genericTypeParameters))
                                                                 .ToArray();
                return(typeVariableSet.CreateReferenceToTraitType(typeName, parameterTypeVariables));
            }
            return(typeVariableSet.CreateReferenceToParameterlessTraitType(typeName));
        }
Beispiel #5
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));
        }
Beispiel #6
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));
        }
Beispiel #7
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();
     }
 }
Beispiel #8
0
 public static TypeVariableReference CreateTypeVariableReferenceFromNIType(
     this TypeVariableSet typeVariableSet,
     NIType type)
 {
     return(typeVariableSet.CreateTypeVariableReferenceFromNIType(type, new Dictionary <NIType, TypeVariableReference>()));
 }
Beispiel #9
0
 public static TypeVariableReference CreateReferenceToOptionType(this TypeVariableSet typeVariableSet, TypeVariableReference innerType)
 {
     return(typeVariableSet.CreateReferenceToGenericTypeSpecializedWithTypeParameters(DataTypes.OptionGenericType, innerType));
 }
Beispiel #10
0
 private static IEnumerable <TypeVariableReference> GetImplementedTraitTypeVariables(this TypeVariableSet typeVariableSet, NIType type, Dictionary <NIType, TypeVariableReference> genericTypeParameters)
 {
     if (!type.IsClassOrInterface())
     {
         return(Enumerable.Empty <TypeVariableReference>());
     }
     return(type
            .GetInterfaces()
            .Select(i => typeVariableSet.CreateTypeVariableReferenceFromNIType(i, genericTypeParameters)));
 }
Beispiel #11
0
 public VariableSet(TypeVariableSet typeVariableSet)
 {
     TypeVariableSet = typeVariableSet;
 }
Beispiel #12
0
 // TODO: these two should hopefully be temporary
 public void AndWith(bool value) => TypeVariableSet.AndWith(this, value);
Beispiel #13
0
 public NIType RenderNIType() => TypeVariableSet.RenderNIType(this);
Beispiel #14
0
 public TypeVariableReference(TypeVariableSet typeVariableSet, int referenceIndex)
 {
     TypeVariableSet = typeVariableSet;
     ReferenceIndex  = referenceIndex;
 }