Example #1
0
            public override IType VisitParameterizedType(ParameterizedType type)
            {
                IType newType = base.VisitParameterizedType(type);

                if (newType != type && ConstraintsValid)
                {
                    // something was changed, so we need to validate the constraints
                    ParameterizedType newParameterizedType = newType as ParameterizedType;
                    if (newParameterizedType != null)
                    {
                        // C# 4.0 spec: §4.4.4 Satisfying constraints
                        var typeParameters = newParameterizedType.GetDefinition().TypeParameters;
                        var substitution   = newParameterizedType.GetSubstitution();
                        for (int i = 0; i < typeParameters.Count; i++)
                        {
                            if (!ValidateConstraints(typeParameters[i], newParameterizedType.GetTypeArgument(i), substitution, conversions))
                            {
                                ConstraintsValid = false;
                                break;
                            }
                        }
                    }
                }
                return(newType);
            }
Example #2
0
            public override IType VisitParameterizedType(ParameterizedType type)
            {
                IType newType = base.VisitParameterizedType(type);

                if (newType != type && ConstraintsValid)
                {
                    // something was changed, so we need to validate the constraints
                    ParameterizedType newParameterizedType = newType as ParameterizedType;
                    if (newParameterizedType != null)
                    {
                        // C# 4.0 spec: §4.4.4 Satisfying constraints
                        var typeParameters = newParameterizedType.GetDefinition().TypeParameters;
                        for (int i = 0; i < typeParameters.Count; i++)
                        {
                            ITypeParameter tp      = typeParameters[i];
                            IType          typeArg = newParameterizedType.GetTypeArgument(i);
                            switch (typeArg.Kind)                               // void, null, and pointers cannot be used as type arguments
                            {
                            case TypeKind.Void:
                            case TypeKind.Null:
                            case TypeKind.Pointer:
                                ConstraintsValid = false;
                                break;
                            }
                            if (tp.HasReferenceTypeConstraint)
                            {
                                if (typeArg.IsReferenceType != true)
                                {
                                    ConstraintsValid = false;
                                }
                            }
                            if (tp.HasValueTypeConstraint)
                            {
                                if (!NullableType.IsNonNullableValueType(typeArg))
                                {
                                    ConstraintsValid = false;
                                }
                            }
                            if (tp.HasDefaultConstructorConstraint)
                            {
                                ITypeDefinition def = typeArg.GetDefinition();
                                if (def != null && def.IsAbstract)
                                {
                                    ConstraintsValid = false;
                                }
                                ConstraintsValid &= typeArg.GetConstructors(
                                    m => m.Parameters.Count == 0 && m.Accessibility == Accessibility.Public,
                                    GetMemberOptions.IgnoreInheritedMembers | GetMemberOptions.ReturnMemberDefinitions
                                    ).Any();
                            }
                            foreach (IType constraintType in tp.DirectBaseTypes)
                            {
                                IType c = constraintType.AcceptVisitor(newParameterizedType.GetSubstitution());
                                ConstraintsValid &= conversions.IsConstraintConvertible(typeArg, c);
                            }
                        }
                    }
                }
                return(newType);
            }
Example #3
0
        static IEnumerable<IMethod> GetMethodsImpl(IType baseType, IList<IType> methodTypeArguments, Predicate<IUnresolvedMethod> filter, GetMemberOptions options)
        {
            IEnumerable<IMethod> declaredMethods = baseType.GetMethods(filter, options | declaredMembers);

            ParameterizedType pt = baseType as ParameterizedType;
            if ((options & GetMemberOptions.ReturnMemberDefinitions) == 0
                && (pt != null || (methodTypeArguments != null && methodTypeArguments.Count > 0)))
            {
                TypeParameterSubstitution substitution = null;
                foreach (IMethod m in declaredMethods) {
                    if (methodTypeArguments != null && methodTypeArguments.Count > 0) {
                        if (m.TypeParameters.Count != methodTypeArguments.Count)
                            continue;
                    }
                    if (substitution == null) {
                        if (pt != null)
                            substitution = pt.GetSubstitution(methodTypeArguments);
                        else
                            substitution = new TypeParameterSubstitution(null, methodTypeArguments);
                    }
                    yield return new SpecializedMethod(m, substitution);
                }
            } else {
                foreach (IMethod m in declaredMethods) {
                    yield return m;
                }
            }
        }
Example #4
0
        internal static TypeVisitor GetSubstitution(IType declaringType)
        {
            ParameterizedType pt = declaringType as ParameterizedType;

            if (pt != null)
            {
                return(pt.GetSubstitution());
            }
            else
            {
                return(null);
            }
        }
Example #5
0
        static IEnumerable<IMethod> GetConstructorsOrAccessorsImpl(IType baseType, IEnumerable<IMethod> declaredMembers, Predicate<IUnresolvedMethod> filter, GetMemberOptions options)
        {
            if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions) {
                return declaredMembers;
            }

            ParameterizedType pt = baseType as ParameterizedType;
            if (pt != null) {
                var substitution = pt.GetSubstitution();
                return declaredMembers.Select(m => new SpecializedMethod(m, substitution) { DeclaringType = pt });
            } else {
                return declaredMembers;
            }
        }
Example #6
0
        static IEnumerable<IEvent> GetEventsImpl(IType baseType, Predicate<IUnresolvedEvent> filter, GetMemberOptions options)
        {
            IEnumerable<IEvent> declaredEvents = baseType.GetEvents(filter, options | declaredMembers);
            if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions) {
                return declaredEvents;
            }

            ParameterizedType pt = baseType as ParameterizedType;
            if (pt != null) {
                var substitution = pt.GetSubstitution();
                return declaredEvents.Select(m => new SpecializedEvent(m, substitution) { DeclaringType = pt });
            } else {
                return declaredEvents;
            }
        }
Example #7
0
        internal static TypeVisitor GetSubstitution(IType declaringType, IList <IType> typeArguments)
        {
            ParameterizedType pt = declaringType as ParameterizedType;

            if (pt != null)
            {
                return(pt.GetSubstitution(typeArguments));
            }
            else if (typeArguments != null)
            {
                return(new TypeParameterSubstitution(null, typeArguments));
            }
            else
            {
                return(null);
            }
        }
Example #8
0
        static IEnumerable <IEvent> GetEventsImpl(IType baseType, ITypeResolveContext context, Predicate <IEvent> filter, GetMemberOptions options)
        {
            IEnumerable <IEvent> declaredEvents = baseType.GetEvents(context, filter, options | declaredMembers);

            if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
            {
                return(declaredEvents);
            }

            ParameterizedType pt = baseType as ParameterizedType;

            if (pt != null)
            {
                var substitution = pt.GetSubstitution();
                return(declaredEvents.Select(m => new SpecializedEvent(pt, m, substitution, context)));
            }
            else
            {
                return(declaredEvents);
            }
        }
Example #9
0
        static IEnumerable <IField> GetFieldsImpl(IType baseType, Predicate <IUnresolvedField> filter, GetMemberOptions options)
        {
            IEnumerable <IField> declaredFields = baseType.GetFields(filter, options | declaredMembers);

            if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
            {
                return(declaredFields);
            }

            ParameterizedType pt = baseType as ParameterizedType;

            if (pt != null)
            {
                var substitution = pt.GetSubstitution();
                return(declaredFields.Select(m => new SpecializedField(pt, m, substitution)));
            }
            else
            {
                return(declaredFields);
            }
        }
Example #10
0
        static IEnumerable <IProperty> GetPropertiesImpl(IType baseType, Predicate <IProperty> filter, GetMemberOptions options)
        {
            IEnumerable <IProperty> declaredProperties = baseType.GetProperties(filter, options | declaredMembers);

            if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions)
            {
                return(declaredProperties);
            }

            ParameterizedType pt = baseType as ParameterizedType;

            if (pt != null)
            {
                var substitution = pt.GetSubstitution();
                return(declaredProperties.Select(m => new SpecializedProperty(m, substitution)
                {
                    DeclaringType = pt
                }));
            }
            else
            {
                return(declaredProperties);
            }
        }