Debian Codegraph @

mcs/tools/cil-strip/Mono.Cecil/GenericParameter.cs

raw
1 //
2 // GenericParameter.cs
3 //
4 // Author:
5 // Jb Evain ([email protected])
6 //
7 // (C) 2005 Jb Evain
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 namespace Mono.Cecil {
30
31 using System;
32
33 internal sealed class GenericParameter : TypeReference {
34
35 int m_position;
36 string m_name;
37 GenericParameterAttributes m_attributes;
38 IGenericParameterProvider m_owner;
39 ConstraintCollection m_constraints;
40
41 public int Position {
42 get { return m_position; }
43 set { m_position = value; }
44 }
45
46 public GenericParameterAttributes Attributes {
47 get { return m_attributes; }
48 set { m_attributes = value; }
49 }
50
51 public IGenericParameterProvider Owner {
52 get { return m_owner; }
53 }
54
55 public bool HasConstraints {
56 get { return (m_constraints == null) ? false : (m_constraints.Count > 0); }
57 }
58
59 public ConstraintCollection Constraints {
60 get {
61 if (m_constraints == null)
62 m_constraints = new ConstraintCollection (this);
63
64 return m_constraints;
65 }
66 }
67
68 public override IMetadataScope Scope {
69 get {
70 if (m_owner is TypeReference)
71 return ((TypeReference) m_owner).Scope;
72 if (m_owner is MethodReference)
73 return ((MethodReference) m_owner).DeclaringType.Scope;
74
75 throw new InvalidOperationException ();
76 }
77 }
78
79 public override ModuleDefinition Module {
80 get {
81 if (m_owner is TypeReference)
82 return ((TypeReference) m_owner).Module;
83 if (m_owner is MethodReference)
84 return ((MethodReference) m_owner).DeclaringType.Module;
85
86 throw new InvalidOperationException ();
87 }
88 }
89
90 public override string Name {
91 get {
92 if (m_name != null)
93 return m_name;
94
95 if (m_owner is TypeReference)
96 return string.Concat ("!", m_position.ToString ());
97 else if (m_owner is MethodReference)
98 return string.Concat ("!!", m_position.ToString ());
99 else
100 throw new InvalidOperationException ();
101 }
102 set { m_name = value; }
103 }
104
105 public override string Namespace {
106 get { return string.Empty; }
107 set { throw new InvalidOperationException (); }
108 }
109
110 public override string FullName {
111 get { return Name; }
112 }
113
114 #region GenericParameterAttributes
115
116 public bool IsNonVariant {
117 get { return (m_attributes & GenericParameterAttributes.VarianceMask) == GenericParameterAttributes.NonVariant; }
118 set {
119 if (value) {
120 m_attributes &= ~GenericParameterAttributes.VarianceMask;
121 m_attributes |= GenericParameterAttributes.NonVariant;
122 } else
123 m_attributes &= ~(GenericParameterAttributes.VarianceMask & GenericParameterAttributes.NonVariant);
124 }
125 }
126
127 public bool IsCovariant {
128 get { return (m_attributes & GenericParameterAttributes.VarianceMask) == GenericParameterAttributes.Covariant; }
129 set {
130 if (value) {
131 m_attributes &= ~GenericParameterAttributes.VarianceMask;
132 m_attributes |= GenericParameterAttributes.Covariant;
133 } else
134 m_attributes &= ~(GenericParameterAttributes.VarianceMask & GenericParameterAttributes.Covariant);
135 }
136 }
137
138 public bool IsContravariant {
139 get { return (m_attributes & GenericParameterAttributes.VarianceMask) == GenericParameterAttributes.Contravariant; }
140 set {
141 if (value) {
142 m_attributes &= ~GenericParameterAttributes.VarianceMask;
143 m_attributes |= GenericParameterAttributes.Contravariant;
144 } else
145 m_attributes &= ~(GenericParameterAttributes.VarianceMask & GenericParameterAttributes.Contravariant);
146 }
147 }
148
149 public bool HasReferenceTypeConstraint {
150 get { return (m_attributes & GenericParameterAttributes.ReferenceTypeConstraint) != 0; }
151 set {
152 if (value) {
153 m_attributes |= GenericParameterAttributes.ReferenceTypeConstraint;
154 } else
155 m_attributes &= ~GenericParameterAttributes.ReferenceTypeConstraint;
156 }
157 }
158
159 public bool HasNotNullableValueTypeConstraint {
160 get { return (m_attributes & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0; }
161 set {
162 if (value) {
163 m_attributes |= GenericParameterAttributes.NotNullableValueTypeConstraint;
164 } else
165 m_attributes &= ~GenericParameterAttributes.NotNullableValueTypeConstraint;
166 }
167 }
168
169 public bool HasDefaultConstructorConstraint {
170 get { return (m_attributes & GenericParameterAttributes.DefaultConstructorConstraint) != 0; }
171 set {
172 if (value) {
173 m_attributes |= GenericParameterAttributes.DefaultConstructorConstraint;
174 } else
175 m_attributes &= ~GenericParameterAttributes.DefaultConstructorConstraint;
176 }
177 }
178
179 #endregion
180
181 internal GenericParameter (int pos, IGenericParameterProvider owner) :
182 base (string.Empty, string.Empty)
183 {
184 m_position = pos;
185 m_owner = owner;
186 }
187
188 public GenericParameter (string name, IGenericParameterProvider owner) :
189 base (string.Empty, string.Empty)
190 {
191 m_name = name;
192 m_owner = owner;
193 }
194
195 public override TypeDefinition Resolve ()
196 {
197 return null;
198 }
199
200 internal static void CloneInto (IGenericParameterProvider old, IGenericParameterProvider np, ImportContext context)
201 {
202 foreach (GenericParameter gp in old.GenericParameters) {
203 GenericParameter ngp = Clone (gp, context);
204 np.GenericParameters.Add (ngp);
205 CloneConstraints (gp, ngp, context);
206 }
207 }
208
209 internal static GenericParameter Clone (GenericParameter gp, ImportContext context)
210 {
211 GenericParameter ngp;
212 if (gp.Owner is TypeReference)
213 ngp = new GenericParameter (gp.m_name, context.GenericContext.Type);
214 else if (gp.Owner is MethodReference)
215 ngp = new GenericParameter (gp.m_name, context.GenericContext.Method);
216 else
217 throw new NotSupportedException ();
218
219 ngp.Position = gp.Owner.GenericParameters.IndexOf (gp);
220 ngp.Attributes = gp.Attributes;
221
222 if (gp.HasCustomAttributes) {
223 foreach (CustomAttribute ca in gp.CustomAttributes)
224 ngp.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
225 }
226
227 return ngp;
228 }
229
230 static void CloneConstraints (GenericParameter gp, GenericParameter ngp, ImportContext context)
231 {
232 if (gp.HasConstraints) {
233 foreach (TypeReference constraint in gp.Constraints)
234 ngp.Constraints.Add (context.Import (constraint));
235 }
236 }
237 }
238 }