Beispiel #1
0
                public override void AppendName(StringBuilder sb, GenericParameterDesc type)
                {
                    string prefix = type.Kind == GenericParameterKind.Type ? "!" : "!!";

                    sb.Append(prefix);
                    sb.Append(type.Name);
                }
        private static void GetInstantiatedConstraintsRecursive(InstantiationContext typeContext, TypeDesc type, ref ArrayBuilder <TypeDesc> instantiatedConstraints)
        {
            if (!type.IsGenericParameter || typeContext == null)
            {
                return;
            }

            GenericParameterDesc genericParam = (GenericParameterDesc)type;

            foreach (var constraint in genericParam.TypeConstraints)
            {
                var instantiatedType = constraint.InstantiateSignature(typeContext.TypeInstantiation, typeContext.MethodInstantiation);

                if (instantiatedType.IsGenericParameter)
                {
                    // Make sure it is save to call this method recursively
                    if (!instantiatedConstraints.Contains(instantiatedType))
                    {
                        instantiatedConstraints.Add(instantiatedType);

                        // Constraints of this constraint apply to 'genericParam' too
                        GetInstantiatedConstraintsRecursive(typeContext, instantiatedType, ref instantiatedConstraints);
                    }
                }
                else
                {
                    instantiatedConstraints.Add(instantiatedType);
                }
            }
        }
Beispiel #3
0
        private static bool IsConstrainedAsGCPointer(GenericParameterDesc type)
        {
            foreach (var typeConstraint in type.TypeConstraints)
            {
                if (typeConstraint.IsGenericParameter)
                {
                    if (IsConstrainedAsGCPointer((GenericParameterDesc)typeConstraint))
                    {
                        return(true);
                    }
                }

                if (!typeConstraint.IsInterface && typeConstraint.IsGCPointer)
                {
                    // Object, ValueType, and Enum are GCPointers but they do not constrain the type to GCPointer!
                    if (!typeConstraint.IsWellKnownType(WellKnownType.Object) &&
                        !typeConstraint.IsWellKnownType(WellKnownType.ValueType) &&
                        !typeConstraint.IsWellKnownType(WellKnownType.Enum))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Beispiel #4
0
        public string FormatName(GenericParameterDesc type)
        {
            StringBuilder sb = new StringBuilder();

            AppendName(sb, type);
            return(sb.ToString());
        }
Beispiel #5
0
        private GenericParameter HandleGenericParameter(Cts.GenericParameterDesc genParam)
        {
            var result = new GenericParameter
            {
                Kind = genParam.Kind == Cts.GenericParameterKind.Type ?
                       GenericParameterKind.GenericTypeParameter : GenericParameterKind.GenericMethodParameter,
                Number = checked ((ushort)genParam.Index),
            };

            foreach (Cts.TypeDesc constraint in genParam.TypeConstraints)
            {
                result.Constraints.Add(HandleType(constraint));
            }

            var ecmaGenParam = genParam as Cts.Ecma.EcmaGenericParameter;

            if (ecmaGenParam != null)
            {
                Ecma.MetadataReader   reader      = ecmaGenParam.MetadataReader;
                Ecma.GenericParameter genParamDef = reader.GetGenericParameter(ecmaGenParam.Handle);

                result.Flags = genParamDef.Attributes;
                result.Name  = HandleString(reader.GetString(genParamDef.Name));
            }
            else
            {
                throw new NotImplementedException();
            }

            // TODO: CustomAttributes

            return(result);
        }
Beispiel #6
0
        private static bool CanCastGenericParameterTo(this GenericParameterDesc thisType, TypeDesc otherType, StackOverflowProtect protect)
        {
            // A boxed variable type can be cast to any of its constraints, or object, if none are specified
            if (otherType.IsObject)
            {
                return(true);
            }

            if (thisType.HasNotNullableValueTypeConstraint &&
                otherType.IsWellKnownType(WellKnownType.ValueType))
            {
                return(true);
            }

            foreach (var typeConstraint in thisType.TypeConstraints)
            {
                if (typeConstraint.CanCastToInternal(otherType, protect))
                {
                    return(true);
                }
            }

            return(false);
        }
 public RuntimeDeterminedType(DefType rawCanonType, GenericParameterDesc runtimeDeterminedDetailsType)
 {
     _rawCanonType = rawCanonType;
     _runtimeDeterminedDetailsType = runtimeDeterminedDetailsType;
 }
 public override void AppendName(StringBuilder sb, GenericParameterDesc type)
 {
     string prefix = type.Kind == GenericParameterKind.Type ? "!" : "!!";
     sb.Append(prefix);
     sb.Append(type.Name);
 }
Beispiel #9
0
 public override Void AppendName(StringBuilder sb, GenericParameterDesc type, FormatOptions options)
 {
     sb.Append(type.DiagnosticName);
     return(Void.Value);
 }
Beispiel #10
0
 public RuntimeDeterminedType GetRuntimeDeterminedType(DefType plainCanonType, GenericParameterDesc detailsType)
 {
     return(_runtimeDeterminedTypes.GetOrCreateValue(new RuntimeDeterminedTypeKey(plainCanonType, detailsType)));
 }
Beispiel #11
0
        private static bool VerifyGenericParamConstraint(Instantiation typeInstantiation, Instantiation methodInstantiation, GenericParameterDesc genericParam, TypeDesc instantiationParam)
        {
            GenericConstraints constraints = genericParam.Constraints;

            // Check class constraint
            if ((constraints & GenericConstraints.ReferenceTypeConstraint) != 0)
            {
                if (!instantiationParam.IsGCPointer &&
                    !CheckGenericSpecialConstraint(instantiationParam, GenericConstraints.ReferenceTypeConstraint))
                {
                    return(false);
                }
            }

            // Check default constructor constraint
            if ((constraints & GenericConstraints.DefaultConstructorConstraint) != 0)
            {
                if (!instantiationParam.HasExplicitOrImplicitDefaultConstructor() &&
                    !CheckGenericSpecialConstraint(instantiationParam, GenericConstraints.DefaultConstructorConstraint))
                {
                    return(false);
                }
            }

            // Check struct constraint
            if ((constraints & GenericConstraints.NotNullableValueTypeConstraint) != 0)
            {
                if ((!instantiationParam.IsValueType || instantiationParam.IsNullable) &&
                    !CheckGenericSpecialConstraint(instantiationParam, GenericConstraints.NotNullableValueTypeConstraint))
                {
                    return(false);
                }
            }

            foreach (var constraintType in genericParam.TypeConstraints)
            {
                var instantiatedType = constraintType.InstantiateSignature(typeInstantiation, methodInstantiation);
                if (!instantiationParam.CanCastTo(instantiatedType))
                {
                    return(false);
                }
            }

            return(true);
        }
        private static bool VerifyGenericParamConstraint(Instantiation typeInstantiation, Instantiation methodInstantiation, GenericParameterDesc genericParam, TypeDesc instantiationParam)
        {
            // Check class constraint
            if (genericParam.HasReferenceTypeConstraint && !instantiationParam.IsGCPointer)
            {
                return(false);
            }

            // Check default constructor constraint
            if (genericParam.HasDefaultConstructorConstraint)
            {
                if (!instantiationParam.IsDefType)
                {
                    return(false);
                }

                if (!instantiationParam.IsValueType && instantiationParam.GetDefaultConstructor() == null)
                {
                    return(false);
                }
            }

            // Check struct constraint
            if (genericParam.HasNotNullableValueTypeConstraint)
            {
                if (!instantiationParam.IsValueType)
                {
                    return(false);
                }

                if (instantiationParam.IsNullable)
                {
                    return(false);
                }
            }

            foreach (var constraintType in genericParam.TypeConstraints)
            {
                var instantiatedType = constraintType.InstantiateSignature(typeInstantiation, methodInstantiation);
                if (!instantiationParam.CanCastTo(instantiatedType))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #13
0
 public abstract void AppendName(StringBuilder sb, GenericParameterDesc type);
Beispiel #14
0
 public string FormatName(GenericParameterDesc type)
 {
     StringBuilder sb = new StringBuilder();
     AppendName(sb, type);
     return sb.ToString();
 }
        private static bool VerifyGenericParamConstraint(InstantiationContext genericParamContext, GenericParameterDesc genericParam,
                                                         InstantiationContext instantiationParamContext, TypeDesc instantiationParam)
        {
            GenericConstraints constraints = genericParam.Constraints;

            // Check class constraint
            if ((constraints & GenericConstraints.ReferenceTypeConstraint) != 0)
            {
                if (!instantiationParam.IsGCPointer &&
                    !CheckGenericSpecialConstraint(instantiationParam, GenericConstraints.ReferenceTypeConstraint))
                {
                    return(false);
                }
            }

            // Check default constructor constraint
            if ((constraints & GenericConstraints.DefaultConstructorConstraint) != 0)
            {
                if (!instantiationParam.HasExplicitOrImplicitDefaultConstructor() &&
                    !CheckGenericSpecialConstraint(instantiationParam, GenericConstraints.DefaultConstructorConstraint))
                {
                    return(false);
                }
            }

            // Check struct constraint
            if ((constraints & GenericConstraints.NotNullableValueTypeConstraint) != 0)
            {
                if ((!instantiationParam.IsValueType || instantiationParam.IsNullable) &&
                    !CheckGenericSpecialConstraint(instantiationParam, GenericConstraints.NotNullableValueTypeConstraint))
                {
                    return(false);
                }
            }

            var instantiatedConstraints = new ArrayBuilder <TypeDesc>();

            GetInstantiatedConstraintsRecursive(instantiationParamContext, instantiationParam, ref instantiatedConstraints);

            foreach (var constraintType in genericParam.TypeConstraints)
            {
                var instantiatedType = constraintType.InstantiateSignature(genericParamContext.TypeInstantiation, genericParamContext.MethodInstantiation);
                if (CanCastConstraint(ref instantiatedConstraints, instantiatedType))
                {
                    continue;
                }

                if (!instantiationParam.CanCastTo(instantiatedType))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #16
0
        private static bool CanCastByVarianceToInterfaceOrDelegate(this TypeDesc thisType, TypeDesc otherType, StackOverflowProtect protectInput)
        {
            if (!thisType.HasSameTypeDefinition(otherType))
            {
                return(false);
            }

            var stackOverflowProtectKey = new CastingPair(thisType, otherType);

            if (protectInput != null)
            {
                if (protectInput.Contains(stackOverflowProtectKey))
                {
                    return(false);
                }
            }

            StackOverflowProtect protect = new StackOverflowProtect(stackOverflowProtectKey, protectInput);

            Instantiation instantiationThis   = thisType.Instantiation;
            Instantiation instantiationTarget = otherType.Instantiation;
            Instantiation instantiationOpen   = thisType.GetTypeDefinition().Instantiation;

            Debug.Assert(instantiationThis.Length == instantiationTarget.Length &&
                         instantiationThis.Length == instantiationOpen.Length);

            for (int i = 0; i < instantiationThis.Length; i++)
            {
                TypeDesc arg       = instantiationThis[i];
                TypeDesc targetArg = instantiationTarget[i];

                if (arg != targetArg)
                {
                    GenericParameterDesc openArgType = (GenericParameterDesc)instantiationOpen[i];

                    switch (openArgType.Variance)
                    {
                    case GenericVariance.Covariant:
                        if (!arg.IsBoxedAndCanCastTo(targetArg, protect))
                        {
                            return(false);
                        }
                        break;

                    case GenericVariance.Contravariant:
                        if (!targetArg.IsBoxedAndCanCastTo(arg, protect))
                        {
                            return(false);
                        }
                        break;

                    default:
                        // non-variant
                        Debug.Assert(openArgType.Variance == GenericVariance.None);
                        return(false);
                    }
                }
            }

            return(true);
        }
 public RuntimeDeterminedType(DefType rawCanonType, GenericParameterDesc runtimeDeterminedDetailsType)
 {
     _rawCanonType = rawCanonType;
     _runtimeDeterminedDetailsType = runtimeDeterminedDetailsType;
 }
Beispiel #18
0
 public abstract void AppendName(StringBuilder sb, GenericParameterDesc type);
Beispiel #19
0
 private InstantiatedGenericParameter(GenericParameterDesc genericParam)
 {
     Debug.Assert(!(genericParam is InstantiatedGenericParameter));
     _genericParam = genericParam;
 }
Beispiel #20
0
 public override IAssemblyDesc AppendName(StringBuilder sb, GenericParameterDesc type, bool assemblyQualify)
 {
     throw new NotSupportedException();
 }
Beispiel #21
0
 public RuntimeDeterminedTypeKey(DefType plainCanonType, GenericParameterDesc detailsType)
 {
     _plainCanonType = plainCanonType;
     _detailsType    = detailsType;
 }