5 // Jb Evain (jbevain@gmail.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 namespace Mono.Cecil {
33 internal sealed class GenericParameter : TypeReference {
37 GenericParameterAttributes m_attributes;
38 IGenericParameterProvider m_owner;
39 ConstraintCollection m_constraints;
42 get { return m_position; }
43 set { m_position = value; }
46 public GenericParameterAttributes Attributes {
47 get { return m_attributes; }
48 set { m_attributes = value; }
51 public IGenericParameterProvider Owner {
52 get { return m_owner; }
55 public bool HasConstraints {
56 get { return (m_constraints == null) ? false : (m_constraints.Count > 0); }
59 public ConstraintCollection Constraints {
61 if (m_constraints == null)
62 m_constraints = new ConstraintCollection (this);
68 public override IMetadataScope Scope {
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;
75 throw new InvalidOperationException ();
79 public override ModuleDefinition Module {
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;
86 throw new InvalidOperationException ();
90 public override string Name {
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 ());
100 throw new InvalidOperationException ();
102 set { m_name = value; }
105 public override string Namespace {
106 get { return string.Empty; }
107 set { throw new InvalidOperationException (); }
110 public override string FullName {
114 #region GenericParameterAttributes
116 public bool IsNonVariant {
117 get { return (m_attributes & GenericParameterAttributes.VarianceMask) == GenericParameterAttributes.NonVariant; }
120 m_attributes &= ~GenericParameterAttributes.VarianceMask;
121 m_attributes |= GenericParameterAttributes.NonVariant;
123 m_attributes &= ~(GenericParameterAttributes.VarianceMask & GenericParameterAttributes.NonVariant);
127 public bool IsCovariant {
128 get { return (m_attributes & GenericParameterAttributes.VarianceMask) == GenericParameterAttributes.Covariant; }
131 m_attributes &= ~GenericParameterAttributes.VarianceMask;
132 m_attributes |= GenericParameterAttributes.Covariant;
134 m_attributes &= ~(GenericParameterAttributes.VarianceMask & GenericParameterAttributes.Covariant);
138 public bool IsContravariant {
139 get { return (m_attributes & GenericParameterAttributes.VarianceMask) == GenericParameterAttributes.Contravariant; }
142 m_attributes &= ~GenericParameterAttributes.VarianceMask;
143 m_attributes |= GenericParameterAttributes.Contravariant;
145 m_attributes &= ~(GenericParameterAttributes.VarianceMask & GenericParameterAttributes.Contravariant);
149 public bool HasReferenceTypeConstraint {
150 get { return (m_attributes & GenericParameterAttributes.ReferenceTypeConstraint) != 0; }
153 m_attributes |= GenericParameterAttributes.ReferenceTypeConstraint;
155 m_attributes &= ~GenericParameterAttributes.ReferenceTypeConstraint;
159 public bool HasNotNullableValueTypeConstraint {
160 get { return (m_attributes & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0; }
163 m_attributes |= GenericParameterAttributes.NotNullableValueTypeConstraint;
165 m_attributes &= ~GenericParameterAttributes.NotNullableValueTypeConstraint;
169 public bool HasDefaultConstructorConstraint {
170 get { return (m_attributes & GenericParameterAttributes.DefaultConstructorConstraint) != 0; }
173 m_attributes |= GenericParameterAttributes.DefaultConstructorConstraint;
175 m_attributes &= ~GenericParameterAttributes.DefaultConstructorConstraint;
181 internal GenericParameter (int pos, IGenericParameterProvider owner) :
182 base (string.Empty, string.Empty)
188 public GenericParameter (string name, IGenericParameterProvider owner) :
189 base (string.Empty, string.Empty)
195 public override TypeDefinition Resolve ()
200 internal static void CloneInto (IGenericParameterProvider old, IGenericParameterProvider np, ImportContext context)
202 foreach (GenericParameter gp in old.GenericParameters) {
203 GenericParameter ngp = Clone (gp, context);
204 np.GenericParameters.Add (ngp);
205 CloneConstraints (gp, ngp, context);
209 internal static GenericParameter Clone (GenericParameter gp, ImportContext context)
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);
217 throw new NotSupportedException ();
219 ngp.Position = gp.Owner.GenericParameters.IndexOf (gp);
220 ngp.Attributes = gp.Attributes;
222 if (gp.HasCustomAttributes) {
223 foreach (CustomAttribute ca in gp.CustomAttributes)
224 ngp.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
230 static void CloneConstraints (GenericParameter gp, GenericParameter ngp, ImportContext context)
232 if (gp.HasConstraints) {
233 foreach (TypeReference constraint in gp.Constraints)
234 ngp.Constraints.Add (context.Import (constraint));