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 public 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 string Name {
84 if (m_owner is TypeReference)
85 return string.Concat ("!", m_position.ToString ());
86 else if (m_owner is MethodReference)
87 return string.Concat ("!!", m_position.ToString ());
89 throw new InvalidOperationException ();
91 set { m_name = value; }
94 public override string Namespace {
95 get { return string.Empty; }
96 set { throw new InvalidOperationException (); }
99 public override string FullName {
103 #region GenericParameterAttributes
105 public bool IsNonVariant {
106 get { return (m_attributes & GenericParameterAttributes.VarianceMask) == GenericParameterAttributes.NonVariant; }
109 m_attributes &= ~GenericParameterAttributes.VarianceMask;
110 m_attributes |= GenericParameterAttributes.NonVariant;
112 m_attributes &= ~(GenericParameterAttributes.VarianceMask & GenericParameterAttributes.NonVariant);
116 public bool IsCovariant {
117 get { return (m_attributes & GenericParameterAttributes.VarianceMask) == GenericParameterAttributes.Covariant; }
120 m_attributes &= ~GenericParameterAttributes.VarianceMask;
121 m_attributes |= GenericParameterAttributes.Covariant;
123 m_attributes &= ~(GenericParameterAttributes.VarianceMask & GenericParameterAttributes.Covariant);
127 public bool IsContravariant {
128 get { return (m_attributes & GenericParameterAttributes.VarianceMask) == GenericParameterAttributes.Contravariant; }
131 m_attributes &= ~GenericParameterAttributes.VarianceMask;
132 m_attributes |= GenericParameterAttributes.Contravariant;
134 m_attributes &= ~(GenericParameterAttributes.VarianceMask & GenericParameterAttributes.Contravariant);
138 public bool HasReferenceTypeConstraint {
139 get { return (m_attributes & GenericParameterAttributes.ReferenceTypeConstraint) != 0; }
142 m_attributes |= GenericParameterAttributes.ReferenceTypeConstraint;
144 m_attributes &= ~GenericParameterAttributes.ReferenceTypeConstraint;
148 public bool HasNotNullableValueTypeConstraint {
149 get { return (m_attributes & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0; }
152 m_attributes |= GenericParameterAttributes.NotNullableValueTypeConstraint;
154 m_attributes &= ~GenericParameterAttributes.NotNullableValueTypeConstraint;
158 public bool HasDefaultConstructorConstraint {
159 get { return (m_attributes & GenericParameterAttributes.DefaultConstructorConstraint) != 0; }
162 m_attributes |= GenericParameterAttributes.DefaultConstructorConstraint;
164 m_attributes &= ~GenericParameterAttributes.DefaultConstructorConstraint;
170 internal GenericParameter (int pos, IGenericParameterProvider owner) :
171 base (string.Empty, string.Empty)
177 public GenericParameter (string name, IGenericParameterProvider owner) :
178 base (string.Empty, string.Empty)
184 public override TypeDefinition Resolve ()
189 internal static void CloneInto (IGenericParameterProvider old, IGenericParameterProvider np, ImportContext context)
191 foreach (GenericParameter gp in old.GenericParameters) {
192 GenericParameter ngp = Clone (gp, context);
193 np.GenericParameters.Add (ngp);
194 CloneConstraints (gp, ngp, context);
198 internal static GenericParameter Clone (GenericParameter gp, ImportContext context)
200 GenericParameter ngp;
201 if (gp.Owner is TypeReference)
202 ngp = new GenericParameter (gp.m_name, context.GenericContext.Type);
203 else if (gp.Owner is MethodReference)
204 ngp = new GenericParameter (gp.m_name, context.GenericContext.Method);
206 throw new NotSupportedException ();
208 ngp.Position = gp.Owner.GenericParameters.IndexOf (gp);
209 ngp.Attributes = gp.Attributes;
211 if (gp.HasCustomAttributes) {
212 foreach (CustomAttribute ca in gp.CustomAttributes)
213 ngp.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
219 static void CloneConstraints (GenericParameter gp, GenericParameter ngp, ImportContext context)
221 if (gp.HasConstraints) {
222 foreach (TypeReference constraint in gp.Constraints)
223 ngp.Constraints.Add (context.Import (constraint));