Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GenericParameterDesc.cs « Common « TypeSystem « src « Common « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d98497921d899afdef875d88f6d5d1b53209058b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;

namespace Internal.TypeSystem
{
    public enum GenericParameterKind
    {
        Type,
        Method,
    }

    /// <summary>
    /// Describes the variance on a generic type parameter of a generic type or method.
    /// </summary>
    public enum GenericVariance
    {
        None = 0,
        
        /// <summary>
        /// The generic type parameter is covariant. A covariant type parameter can appear
        /// as the result type of a method, the type of a read-only field, a declared base
        /// type, or an implemented interface.
        /// </summary>
        Covariant = 1,

        /// <summary>
        /// The generic type parameter is contravariant. A contravariant type parameter can
        /// appear as a parameter type in method signatures.
        /// </summary>
        Contravariant = 2
    }

    /// <summary>
    /// Describes the constraints on a generic type parameter of a generic type or method.
    /// </summary>
    [Flags]
    public enum GenericConstraints
    {
        None = 0,

        /// <summary>
        /// A type can be substituted for the generic type parameter only if it is a reference type.
        /// </summary>
        ReferenceTypeConstraint = 0x04,
        
        /// <summary>
        // A type can be substituted for the generic type parameter only if it is a value
        // type and is not nullable.
        /// </summary>
        NotNullableValueTypeConstraint = 0x08,

        /// <summary>
        /// A type can be substituted for the generic type parameter only if it has a parameterless
        /// constructor.
        /// </summary>
        DefaultConstructorConstraint = 0x10,
    }

    public abstract partial class GenericParameterDesc : TypeDesc
    {
        /// <summary>
        /// Gets the name of the generic parameter as defined in the metadata.
        /// </summary>
        public virtual string Name
        {
            get
            {
                return String.Concat("T", Index.ToStringInvariant());
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is a type or method generic parameter.
        /// </summary>
        public abstract GenericParameterKind Kind { get; }
        
        /// <summary>
        /// Gets the zero based index of the generic parameter within the declaring type or method.
        /// </summary>
        public abstract int Index { get; }

        /// <summary>
        /// Gets a value indicating the variance of this generic parameter.
        /// </summary>
        public virtual GenericVariance Variance
        {
            get
            {
                return GenericVariance.None;
            }
        }

        /// <summary>
        /// Gets a value indicating generic constraints of this generic parameter.
        /// </summary>
        public virtual GenericConstraints Constraints
        {
            get
            {
                return GenericConstraints.None;
            }
        }
        
        /// <summary>
        /// Gets type constraints imposed on substitutions.
        /// </summary>
        public virtual IEnumerable<TypeDesc> TypeConstraints
        {
            get
            {
                return TypeDesc.EmptyTypes;
            }
        }

        public bool HasNotNullableValueTypeConstraint
        {
            get
            {
                return (Constraints & GenericConstraints.NotNullableValueTypeConstraint) != 0;
            }
        }

        public bool HasReferenceTypeConstraint
        {
            get
            {
                return (Constraints & GenericConstraints.ReferenceTypeConstraint) != 0;
            }
        }

        public bool HasDefaultConstructorConstraint
        {
            get
            {
                return (Constraints & GenericConstraints.DefaultConstructorConstraint) != 0;
            }
        }

        public bool IsCovariant
        {
            get
            {
                return (Variance & GenericVariance.Covariant) != 0;
            }
        }

        public bool IsContravariant
        {
            get
            {
                return (Variance & GenericVariance.Contravariant) != 0;
            }
        }

        protected sealed override TypeFlags ComputeTypeFlags(TypeFlags mask)
        {
            TypeFlags flags = 0;

            flags |= TypeFlags.GenericParameter;

            flags |= TypeFlags.HasGenericVarianceComputed;

            flags |= TypeFlags.IsByRefLikeComputed;

            return flags;
        }

        public sealed override int GetHashCode()
        {
            // TODO: Determine what a the right hash function should be. Use stable hashcode based on the type name?
            // For now, use the same hash as a SignatureVariable type.
            return Internal.NativeFormat.TypeHashingAlgorithms.ComputeSignatureVariableHashCode(Index, Kind == GenericParameterKind.Method);
        }
    }
}