Ejemplo n.º 1
0
        public bool IsSubclassOf(IType other)
        {
            if (null == other)
            {
                return(false);
            }

            if (BaseType != null && (BaseType == other || BaseType.IsSubclassOf(other)))
            {
                return(true);
            }

            if (other.IsInterface && Array.Exists(
                    GetInterfaces(),
                    i => TypeCompatibilityRules.IsAssignableFrom(other, i)))
            {
                return(true);
            }

            if (null != other.ConstructedInfo &&
                ConstructedInfo.GenericDefinition == other.ConstructedInfo.GenericDefinition)
            {
                for (int i = 0; i < ConstructedInfo.GenericArguments.Length; ++i)
                {
                    if (!ConstructedInfo.GenericArguments[i].IsSubclassOf(other.ConstructedInfo.GenericArguments[i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        private int CalculateArgumentScore(IType parameterType, IType argumentType)
        {
            if (parameterType == argumentType || (TypeSystemServices.IsSystemObject(argumentType) && TypeSystemServices.IsSystemObject(parameterType)))
            {
                return(parameterType is ICallableType ? CallableExactMatchScore : ExactMatchScore);
            }

            if (TypeCompatibilityRules.IsAssignableFrom(parameterType, argumentType))
            {
                var callableType = parameterType as ICallableType;
                var callableArg  = argumentType as ICallableType;
                if (callableType != null && callableArg != null)
                {
                    return(CalculateCallableScore(callableType, callableArg));
                }
                return(UpCastScore);
            }

            if (TypeSystemServices.FindImplicitConversionOperator(argumentType, parameterType) != null)
            {
                return(ImplicitConversionScore);
            }

            if (TypeSystemServices.CanBeReachedByPromotion(parameterType, argumentType))
            {
                return(IsWideningPromotion(parameterType, argumentType) ? WideningPromotion : NarrowingPromotion);
            }

            if (MyDowncastPermissions().CanBeReachedByDowncast(parameterType, argumentType))
            {
                return(DowncastScore);
            }

            return(-1);
        }
Ejemplo n.º 3
0
        internal static bool ValidAsyncTypeBound(Method value)
        {
            var ret = (IType)value.ReturnType.Entity;
            var tss = My <TypeSystemServices> .Instance;

            return(ret == tss.VoidType ||
                   ret == tss.TaskType ||
                   TypeCompatibilityRules.IsAssignableFrom(tss.GenericTaskType, ret));
        }
Ejemplo n.º 4
0
        private void CreateDispose()
        {
            BooMethodBuilder dispose = _enumerator.AddVirtualMethod("Dispose", TypeSystemServices.VoidType);

            if (TypeCompatibilityRules.IsAssignableFrom(TypeSystemServices.IDisposableType, _sourceEnumeratorType))
            {
                dispose.Body.Add(CodeBuilder.CreateMethodInvocation(
                                     CodeBuilder.CreateReference(_enumeratorField),
                                     Methods.InstanceActionOf <IDisposable>(d => d.Dispose)));
            }
        }
Ejemplo n.º 5
0
        public bool IsSubclassOf(IType other)
        {
            if (null == other)
            {
                return(false);
            }

            if (BaseType != null && (BaseType == other || BaseType.IsSubclassOf(other)))
            {
                return(true);
            }

            return(other.IsInterface && Array.Exists(
                       GetInterfaces(),
                       i => TypeCompatibilityRules.IsAssignableFrom(other, i)));
        }
Ejemplo n.º 6
0
        private static bool SameOrEquivalentGenericTypes(IType t1, IType t2, ref bool genericType)
        {
            if (t1 == t2)
            {
                return(true);
            }
            var g1 = t1 as IGenericParameter;
            var g2 = t2 as IGenericParameter;

            if (g1 == null || g2 == null)
            {
                var c1 = t1 as IConstructedTypeInfo;
                var c2 = t2 as IConstructedTypeInfo;
                if (c1 == null || c2 == null)
                {
                    return(false);
                }
                if (c1.GenericDefinition != c2.GenericDefinition)
                {
                    return(false);
                }
                for (var i = 0; i < c1.GenericArguments.Length; ++i)
                {
                    if (!SameOrEquivalentGenericTypes(c1.GenericArguments[i], c2.GenericArguments[i], ref genericType))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            genericType = true;
            var constraints = g2.GetTypeConstraints();

            if (constraints.Length > 0 && !constraints.Any(c => TypeCompatibilityRules.IsAssignableFrom(g1, c)))
            {
                return(false);
            }
            return(g1.Variance == g2.Variance && g1.MustHaveDefaultConstructor == g2.MustHaveDefaultConstructor);
        }
Ejemplo n.º 7
0
 private bool IsAssignableFrom(IType expectedType, IType actualType)
 {
     return(TypeCompatibilityRules.IsAssignableFrom(expectedType, actualType));
 }
Ejemplo n.º 8
0
        public bool IsAssignableFrom(IType other)
        {
            if (other == this)
            {
                return(true);
            }

            if (other.IsNull())
            {
                return(IsClass);
            }

            var otherParameter = other as IGenericParameter;

            if (otherParameter != null && Array.Exists(otherParameter.GetTypeConstraints(), constraint => TypeCompatibilityRules.IsAssignableFrom(this, constraint)))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 9
0
 bool IsAstAttribute(IType type)
 {
     return(TypeCompatibilityRules.IsAssignableFrom(_astAttributeInterface, type));
 }
Ejemplo n.º 10
0
        private bool MaintainsParameterConstraints(IGenericParameter parameter, IType argument)
        {
            if (argument == null || TypeSystemServices.IsError(argument))
            {
                return(true);
            }

            if (argument == parameter)
            {
                return(true);
            }

            if (argument == _typeSystemServices.VoidType)
            {
                Errors.Add(CompilerErrorFactory.InvalidGenericParameterType(ConstructionNode, argument));
                return(false);
            }

            bool valid = true;

            // Check type semantics constraints
            if (parameter.IsClass && !(argument.IsClass || argument.IsInterface))
            {
                Errors.Add(CompilerErrorFactory.GenericArgumentMustBeReferenceType(ConstructionNode, parameter, argument));
                valid = false;
            }

            if (parameter.IsValueType && !argument.IsValueType)
            {
                Errors.Add(CompilerErrorFactory.GenericArgumentMustBeValueType(ConstructionNode, parameter, argument));
                valid = false;
            }
            // Don't check for default constructor constraint if value type constraint failed
            else if (parameter.MustHaveDefaultConstructor && !HasDefaultConstructor(argument))
            {
                Errors.Add(CompilerErrorFactory.GenericArgumentMustHaveDefaultConstructor(ConstructionNode, parameter, argument));
                valid = false;
            }

            // Check base type constraints
            IType[] baseTypes = parameter.GetTypeConstraints();
            if (baseTypes != null)
            {
                foreach (IType baseType in baseTypes)
                {
                    // Foo<T> where T : Foo<T>
                    if (null != _definition &&
                        TypeCompatibilityRules.IsAssignableFrom(baseType, _definition) &&
                        argument == _constructionNode.ParentNode.Entity)
                    {
                        continue;
                    }

                    // Don't check for System.ValueType supertype constraint
                    // if parameter also has explicit value type constraint
                    if (baseType == _typeSystemServices.ValueTypeType && parameter.IsValueType)
                    {
                        continue;
                    }

                    if (!TypeCompatibilityRules.IsAssignableFrom(baseType, argument))
                    {
                        Errors.Add(CompilerErrorFactory.GenericArgumentMustHaveBaseType(ConstructionNode, parameter, argument, baseType));
                        valid = false;
                    }
                }
            }

            return(valid);
        }
Ejemplo n.º 11
0
 private bool IsAssignableFrom(IType t1, IType t2)
 {
     return(TypeCompatibilityRules.IsAssignableFrom(t1, t2));
 }