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));
        }