예제 #1
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));
        }
예제 #2
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)));
 }
예제 #3
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));
        }
예제 #4
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();
     }
 }
예제 #5
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));
        }
예제 #6
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));
        }
예제 #7
0
 public static TypeVariableReference CreateTypeVariableReferenceFromNIType(
     this TypeVariableSet typeVariableSet,
     NIType type)
 {
     return(typeVariableSet.CreateTypeVariableReferenceFromNIType(type, new Dictionary <NIType, TypeVariableReference>()));
 }