Ejemplo n.º 1
0
        private TypeParameterConstraintClause(
            TypeParameterConstraintKind constraints,
            ImmutableArray <TypeWithAnnotations> constraintTypes,
            bool ignoresNullableContext)
        {
#if DEBUG
            switch (constraints & TypeParameterConstraintKind.AllReferenceTypeKinds)
            {
            case TypeParameterConstraintKind.None:
            case TypeParameterConstraintKind.ReferenceType:
            case TypeParameterConstraintKind.NullableReferenceType:
            case TypeParameterConstraintKind.NotNullableReferenceType:
                break;

            default:
                ExceptionUtilities.UnexpectedValue(constraints);     // This call asserts.
                break;
            }

            Debug.Assert((constraints & TypeParameterConstraintKind.ObliviousNullabilityIfReferenceType) == 0 ||
                         (constraints & ~(TypeParameterConstraintKind.ObliviousNullabilityIfReferenceType | TypeParameterConstraintKind.Constructor | TypeParameterConstraintKind.Default)) == 0);
#endif
            this.Constraints            = constraints;
            this.ConstraintTypes        = constraintTypes;
            this.IgnoresNullableContext = ignoresNullableContext;
        }
Ejemplo n.º 2
0
 internal static void CheckConstraints(
     ITypeParameterSymbol symbol,
     TypeParameterConstraintKind constraints,
     params string[] constraintTypes
     )
 {
     Assert.Equal(constraints, GetTypeParameterConstraints(symbol));
     CheckISymbols(symbol.ConstraintTypes, constraintTypes);
 }
Ejemplo n.º 3
0
        protected bool?CalculateReferenceTypeConstraintIsNullable(TypeParameterConstraintKind constraints)
        {
            if ((constraints & TypeParameterConstraintKind.ReferenceType) == 0)
            {
                return(false);
            }

            switch (constraints & TypeParameterConstraintKind.AllReferenceTypeKinds)
            {
            case TypeParameterConstraintKind.NullableReferenceType:
                return(true);

            case TypeParameterConstraintKind.NotNullableReferenceType:
                return(false);
            }

            return(null);
        }
Ejemplo n.º 4
0
        protected bool?CalculateReferenceTypeConstraintIsNullable(TypeParameterConstraintKind constraints)
        {
            if ((constraints & TypeParameterConstraintKind.ReferenceType) == 0)
            {
                return(false);
            }

            if ((constraints & TypeParameterConstraintKind.NullableReferenceType) == TypeParameterConstraintKind.NullableReferenceType)
            {
                return(true);
            }

            if (NonNullTypes == true)
            {
                return(false);
            }

            return(null);
        }
Ejemplo n.º 5
0
        internal static TypeParameterConstraintClause Create(
            TypeParameterConstraintKind constraints,
            ImmutableArray <TypeWithAnnotations> constraintTypes)
        {
            Debug.Assert(!constraintTypes.IsDefault);
            if (constraintTypes.IsEmpty)
            {
                switch (constraints)
                {
                case TypeParameterConstraintKind.None:
                    return(Empty);

                case TypeParameterConstraintKind.ObliviousNullabilityIfReferenceType:
                    return(ObliviousNullabilityIfReferenceType);
                }
            }

            return(new TypeParameterConstraintClause(constraints, constraintTypes));
        }
Ejemplo n.º 6
0
 private static void CheckConstraints(
     TypeParameterSymbol typeParameter,
     TypeParameterConstraintKind constraints,
     bool isValueType,
     bool isReferenceType,
     string effectiveBaseClassDescription,
     string deducedBaseTypeDescription,
     params string[] constraintTypeDescriptions)
 {
     Assert.Equal(constraints, Utils.GetTypeParameterConstraints(typeParameter));
     Assert.Equal(typeParameter.IsValueType, isValueType);
     Assert.Equal(typeParameter.IsReferenceType, isReferenceType);
     Assert.Null(typeParameter.BaseType);
     Assert.Equal(typeParameter.Interfaces.Length, 0);
     Utils.CheckSymbol(typeParameter.EffectiveBaseClassNoUseSiteDiagnostics, effectiveBaseClassDescription);
     Utils.CheckSymbol(typeParameter.DeducedBaseTypeNoUseSiteDiagnostics, deducedBaseTypeDescription);
     Utils.CheckSymbols(typeParameter.ConstraintTypes, constraintTypeDescriptions);
 }
 public TypeParameterConstraintClause(TypeParameterConstraintKind constraints, ImmutableArray<TypeSymbol> constraintTypes)
 {
     Debug.Assert(!constraintTypes.IsDefault);
     this.Constraints = constraints;
     this.ConstraintTypes = constraintTypes;
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Returns true if the constraint is valid. Otherwise
        /// returns false and generates a diagnostic.
        /// </summary>
        internal static bool IsValidConstraint(
            string typeParameterName,
            TypeConstraintSyntax syntax,
            TypeSymbolWithAnnotations type,
            TypeParameterConstraintKind constraints,
            ArrayBuilder <TypeSymbolWithAnnotations> constraintTypes,
            DiagnosticBag diagnostics)
        {
            if (!IsValidConstraintType(syntax, type, diagnostics))
            {
                return(false);
            }

            // Ignore nullability when comparing constraints.
            if (constraintTypes.Contains(c => type.Equals(c, TypeCompareKind.IgnoreNullableModifiersForReferenceTypes)))
            {
                // "Duplicate constraint '{0}' for type parameter '{1}'"
                Error(diagnostics, ErrorCode.ERR_DuplicateBound, syntax, type.TypeSymbol.SetUnknownNullabilityForReferenceTypes(), typeParameterName);
                return(false);
            }

            if (type.TypeKind == TypeKind.Class)
            {
                // If there is already a struct or class constraint (class constraint could be
                // 'class' or explicit type), report an error and drop this class. If we don't
                // drop this additional class, we may end up with conflicting class constraints.

                if (constraintTypes.Count > 0)
                {
                    // "The class type constraint '{0}' must come before any other constraints"
                    Error(diagnostics, ErrorCode.ERR_ClassBoundNotFirst, syntax, type.TypeSymbol);
                    return(false);
                }

                if ((constraints & (TypeParameterConstraintKind.ReferenceType)) != 0)
                {
                    switch (type.SpecialType)
                    {
                    case SpecialType.System_Enum:
                    case SpecialType.System_Delegate:
                    case SpecialType.System_MulticastDelegate:
                        break;

                    default:
                        // "'{0}': cannot specify both a constraint class and the 'class' or 'struct' constraint"
                        Error(diagnostics, ErrorCode.ERR_RefValBoundWithClass, syntax, type.TypeSymbol);
                        return(false);
                    }
                }
                else if (type.SpecialType != SpecialType.System_Enum)
                {
                    if ((constraints & TypeParameterConstraintKind.ValueType) != 0)
                    {
                        // "'{0}': cannot specify both a constraint class and the 'class' or 'struct' constraint"
                        Error(diagnostics, ErrorCode.ERR_RefValBoundWithClass, syntax, type.TypeSymbol);
                        return(false);
                    }
                    else if ((constraints & TypeParameterConstraintKind.Unmanaged) != 0)
                    {
                        // "'{0}': cannot specify both a constraint class and the 'unmanaged' constraint"
                        Error(diagnostics, ErrorCode.ERR_UnmanagedBoundWithClass, syntax, type.TypeSymbol);
                        return(false);
                    }
                }
            }

            return(true);
        }
 public TypeParameterConstraintClause(TypeParameterConstraintKind constraints, ImmutableArray <TypeSymbol> constraintTypes)
 {
     Debug.Assert(!constraintTypes.IsDefault);
     this.Constraints     = constraints;
     this.ConstraintTypes = constraintTypes;
 }
Ejemplo n.º 10
0
        public System.Reflection.GenericParameterAttributes GetGenericParameterAttributes(TypeParameterConstraintKind attributes)
        {
            var x = System.Reflection.GenericParameterAttributes.None;

            if ((attributes & TypeParameterConstraintKind.DefaultConstructorConstraint) == TypeParameterConstraintKind.DefaultConstructorConstraint)
            {
                x |= System.Reflection.GenericParameterAttributes.DefaultConstructorConstraint;
            }
            if ((attributes & TypeParameterConstraintKind.NotNullableValueTypeConstraint) == TypeParameterConstraintKind.NotNullableValueTypeConstraint)
            {
                x |= System.Reflection.GenericParameterAttributes.NotNullableValueTypeConstraint;
            }
            if ((attributes & TypeParameterConstraintKind.ReferenceTypeConstraint) == TypeParameterConstraintKind.ReferenceTypeConstraint)
            {
                x |= System.Reflection.GenericParameterAttributes.ReferenceTypeConstraint;
            }
            if ((attributes & TypeParameterConstraintKind.Contravariant) == TypeParameterConstraintKind.Contravariant)
            {
                x |= System.Reflection.GenericParameterAttributes.Contravariant;
            }
            if ((attributes & TypeParameterConstraintKind.Covariant) == TypeParameterConstraintKind.Covariant)
            {
                x |= System.Reflection.GenericParameterAttributes.Covariant;
            }

            return(x);
        }