Exemplo n.º 1
0
//		/// <summary>
//		/// Looks up the <see cref="IConvertFrom"/> for the target type.
//		/// </summary>
//		/// <param name="target">The type to lookup the converter for.</param>
//		/// <returns>The converter for the specified type.</returns>
//		public static IConvertFrom GetTypeConverter(Type target)
//		{
//			IConvertFrom converter = ConverterRegistry.GetConverter(target);
//			if (converter == null)
//			{
//				throw new InvalidOperationException("No type converter defined for [" + target + "]");
//			}
//			return converter;
//		}

        /// <summary>
        /// Checks if there is an appropriate type conversion from the source type to the target type.
        /// </summary>
        /// <param name="sourceType">The type to convert from.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <returns><c>true</c> if there is a conversion from the source type to the target type.</returns>
        /// <remarks>
        /// Checks if there is an appropriate type conversion from the source type to the target type.
        /// <para>
        /// </para>
        /// </remarks>
        public static bool CanConvertTypeTo(Type sourceType, Type targetType)
        {
            if (sourceType == null || targetType == null)
            {
                return(false);
            }

            // Check if we can assign directly from the source type to the target type
            if (targetType.IsAssignableFrom(sourceType))
            {
                return(true);
            }

            // Look for a To converter
            IConvertTo tcSource = ConverterRegistry.GetConvertTo(sourceType, targetType);

            if (tcSource != null)
            {
                if (tcSource.CanConvertTo(targetType))
                {
                    return(true);
                }
            }

            // Look for a From converter
            IConvertFrom tcTarget = ConverterRegistry.GetConvertFrom(targetType);

            if (tcTarget != null)
            {
                if (tcTarget.CanConvertFrom(sourceType))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts an object to the target type.
        /// </summary>
        /// <param name="sourceInstance">The object to convert to the target type.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <returns>The converted object.</returns>
        /// <remarks>
        /// <para>
        /// Converts an object to the target type.
        /// </para>
        /// </remarks>
        public static object ConvertTypeTo(object sourceInstance, Type targetType)
        {
            Type sourceType = sourceInstance.GetType();

            // Check if we can assign directly from the source type to the target type
            if (targetType.IsAssignableFrom(sourceType))
            {
                return(sourceInstance);
            }

            // Look for a TO converter
            IConvertTo tcSource = ConverterRegistry.GetConvertTo(sourceType, targetType);

            if (tcSource != null)
            {
                if (tcSource.CanConvertTo(targetType))
                {
                    return(tcSource.ConvertTo(sourceInstance, targetType));
                }
            }

            // Look for a FROM converter
            IConvertFrom tcTarget = ConverterRegistry.GetConvertFrom(targetType);

            if (tcTarget != null)
            {
                if (tcTarget.CanConvertFrom(sourceType))
                {
                    return(tcTarget.ConvertFrom(sourceInstance));
                }
            }

            throw new ArgumentException(
                      "Cannot convert source object [" + sourceInstance + "] to target type [" + targetType.Name + "]",
                      "sourceInstance");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Checks if there is an appropriate type conversion from the source type to the target type.
        /// </summary>
        /// <param name="sourceType">The type to convert from.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <returns><c>true</c> if there is a conversion from the source type to the target type.</returns>
        /// <remarks>
        /// Checks if there is an appropriate type conversion from the source type to the target type.
        /// <para>
        /// </para>
        /// </remarks>
        public static bool CanConvertTypeTo(Type sourceType, Type targetType)
        {
            if ((object)sourceType == null || (object)targetType == null)
            {
                return(false);
            }
            if (CompatibilityExtensions.IsAssignableFrom(targetType, sourceType))
            {
                return(true);
            }
            IConvertTo convertTo = ConverterRegistry.GetConvertTo(sourceType, targetType);

            if (convertTo != null && convertTo.CanConvertTo(targetType))
            {
                return(true);
            }
            IConvertFrom convertFrom = ConverterRegistry.GetConvertFrom(targetType);

            if (convertFrom != null && convertFrom.CanConvertFrom(sourceType))
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 4
0
 public TypeAndConverter(NameKey key, IConvertTo <TypeProblem2.Type, IOrType <WeakTypeDefinition, WeakGenericTypeDefinition, IPrimitiveType> > converter)
 {
     this.key       = key ?? throw new ArgumentNullException(nameof(key));
     this.converter = converter ?? throw new ArgumentNullException(nameof(converter));
 }
Exemplo n.º 5
0
                public (Method, IOrType <TypeReference, IError>, IOrType <TypeReference, IError>) CreateGenericMethod(IStaticScope parent, Func <IStaticScope, IOrType <TypeReference, IError> > inputTypeBuilder, Func <IStaticScope, IOrType <TypeReference, IError> > outputTypeBuilder, string inputName, IConvertTo <Method, IOrType <WeakMethodDefinition, WeakImplementationDefinition, WeakEntryPointDefinition, WeakGenericMethodDefinition> > converter, IReadOnlyList <TypeAndConverter> placeholders)
                {
                    var(method, inputType, outputType) = CreateMethod(parent, inputTypeBuilder, outputTypeBuilder, inputName, converter);
                    var i = 0;

                    foreach (var placeholder in placeholders)
                    {
                        var placeholderType = new GenericTypeParameter(this, $"generic-parameter-{placeholder.key}", i++, Prototypist.Toolbox.OrType.Make <MethodType, Type, Method, InferredType>(method));

                        HasGenericType(Prototypist.Toolbox.OrType.Make <MethodType, Type, Method>(method), placeholder.key, placeholderType);
                    }

                    // why doesn't this have genericOverlays ({4BFD0274-B70F-4BD8-B290-63B69FF74FE7})
                    // the overlays are t1 and t2 of the base method "method [t1,t2]"
                    // they are never references anywhere so we don't care to overlay them
                    // we build the right MethodType
                    // at {2E20DFFB-7BD2-4351-9CAF-10A63491ABCF}
                    // this isn't great
                    // it would be a little work to build the genericOverlays
                    // genericOverlays take a looked up type not a key

                    return(method, inputType, outputType);
                }
Exemplo n.º 6
0
                public OrType CreateOrType(IStaticScope s, IKey key, IOrType <TypeReference, IError> setUpSideNode1, IOrType <TypeReference, IError> setUpSideNode2, IConvertTo <OrType, IFrontendType <IVerifiableType> > converter)
                {
                    if (!setUpSideNode1.Is1(out var node1))
                    {
                        throw new NotImplementedException();
                    }
                    if (!setUpSideNode2.Is1(out var node2))
                    {
                        throw new NotImplementedException();
                    }

                    var res = new OrType(this, $"{node1.DebugName} || {node2.DebugName}", converter);

                    Ors(res, node1, node2);
                    HasOrType(s, key, res);

                    return(res);
                }
Exemplo n.º 7
0
                // why do objects have keys?
                // that is wierd
                public Object CreateObjectOrModule(IStaticScope parent, IKey key, IConvertTo <Object, IOrType <WeakObjectDefinition, WeakRootScope> > converter, IConvertTo <Scope, IOrType <WeakBlockDefinition, WeakScope, WeakEntryPointDefinition> > innerConverter)
                {
                    var res = new Object(this, key.ToString() !, converter, innerConverter);

                    IsChildOf(parent, res);
                    HasObject(parent, key, res);
                    return(res);
                }
Exemplo n.º 8
0
                public Type CreateGenericType(IStaticScope parent, IOrType <NameKey, ImplicitKey> key, IReadOnlyList <TypeAndConverter> placeholders, IConvertTo <Type, IOrType <WeakTypeDefinition, WeakGenericTypeDefinition, Tac.SyntaxModel.Elements.AtomicTypes.IPrimitiveType> > converter)
                {
                    var res = new Type(
                        this,
                        $"generic-{key}-{placeholders.Aggregate("", (x, y) => x + "-" + y)}",
                        Possibly.Is(key),
                        converter,
                        Possibly.IsNot <Guid>(),
                        Possibly.IsNot <IInterfaceType>());

                    IsChildOf(parent, res);
                    HasType(parent, key.SwitchReturns <IKey>(x => x, x => x), res);
                    var i = 0;

                    foreach (var placeholder in placeholders)
                    {
                        var placeholderType = new TypeProblem2.GenericTypeParameter(this, $"generic-parameter-{placeholder.key}", i++, Prototypist.Toolbox.OrType.Make <MethodType, Type, Method, InferredType>(res));
                        //var placeholderType = new Type(
                        //    this,
                        //    $"generic-parameter-{placeholder.key}",
                        //    Possibly.Is(placeholder.key),
                        //    placeholder.converter,
                        //    Possibly.IsNot<Guid>(),
                        //    Possibly.IsNot<IInterfaceType>());
                        HasGenericType(Prototypist.Toolbox.OrType.Make <MethodType, Type, Method>(res), placeholder.key, placeholderType);
                    }
                    return(res);
                }
Exemplo n.º 9
0
                public Type CreateType(IStaticScope parent, IOrType <NameKey, ImplicitKey> key, IConvertTo <Type, IOrType <WeakTypeDefinition, WeakGenericTypeDefinition, Tac.SyntaxModel.Elements.AtomicTypes.IPrimitiveType> > converter, IIsPossibly <Guid> primitive)
                {
                    var res = new Type(this, key.ToString() !, Possibly.Is(key), converter, primitive, Possibly.IsNot <IInterfaceType>());

                    IsChildOf(parent, res);
                    HasType(parent, key.SwitchReturns <IKey>(x => x, x => x), res);
                    return(res);
                }
Exemplo n.º 10
0
 public Type CreateType(IStaticScope parent, IOrType <NameKey, ImplicitKey> key, IConvertTo <Type, IOrType <WeakTypeDefinition, WeakGenericTypeDefinition, Tac.SyntaxModel.Elements.AtomicTypes.IPrimitiveType> > converter)
 {
     return(CreateType(parent, key, converter, Possibly.IsNot <Guid>()));
 }
Exemplo n.º 11
0
                public TypeReference CreateTypeReference(IStaticScope context, IKey typeKey, IConvertTo <TypeReference, IFrontendType <IVerifiableType> > converter)
                {
                    var res = new TypeReference(this, typeKey.ToString() !, converter);

                    HasReference(context, res);
                    res.Context = Possibly.Is(context);
                    res.TypeKey = Prototypist.Toolbox.OrType.Make <IKey, IError, Unset>(typeKey);
                    return(res);
                }