5 // Jb Evain (jbevain@gmail.com)
7 // Copyright (c) 2008 - 2010 Jb Evain
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.
31 using Mono.Collections.Generic;
33 using Mono.Cecil.Metadata;
35 namespace Mono.Cecil {
37 public sealed class GenericParameter : TypeReference, ICustomAttributeProvider {
39 readonly IGenericParameterProvider owner;
42 Collection<TypeReference> constraints;
43 Collection<CustomAttribute> custom_attributes;
45 public GenericParameterAttributes Attributes {
46 get { return (GenericParameterAttributes) attributes; }
47 set { attributes = (ushort) value; }
55 return owner.GenericParameters.IndexOf (this);
59 public IGenericParameterProvider Owner {
63 public bool HasConstraints {
65 if (constraints != null)
66 return constraints.Count > 0;
69 return Module.Read (this, (generic_parameter, reader) => reader.HasGenericConstraints (generic_parameter));
75 public Collection<TypeReference> Constraints {
77 if (constraints != null)
81 return constraints = Module.Read (this, (generic_parameter, reader) => reader.ReadGenericConstraints (generic_parameter));
83 return constraints = new Collection<TypeReference> ();
87 public bool HasCustomAttributes {
89 if (custom_attributes != null)
90 return custom_attributes.Count > 0;
92 return this.GetHasCustomAttributes (Module);
96 public Collection<CustomAttribute> CustomAttributes {
97 get { return custom_attributes ?? (custom_attributes = this.GetCustomAttributes (Module)); }
100 internal new bool HasImage {
101 get { return Module != null && Module.HasImage; }
104 public override IMetadataScope Scope {
106 if (owner.GenericParameterType == GenericParameterType.Method)
107 return ((MethodReference) owner).DeclaringType.Scope;
109 return ((TypeReference) owner).Scope;
113 public override ModuleDefinition Module {
114 get { return ((MemberReference) owner).Module; }
117 public override string Name {
119 if (!string.IsNullOrEmpty (base.Name))
122 return base.Name = (owner.GenericParameterType == GenericParameterType.Type ? "!" : "!!") + Position;
126 public override string Namespace {
127 get { return string.Empty; }
128 set { throw new InvalidOperationException (); }
131 public override string FullName {
135 public override bool IsGenericParameter {
139 internal override bool ContainsGenericParameter {
143 public override MetadataType MetadataType {
144 get { return (MetadataType) etype; }
147 #region GenericParameterAttributes
149 public bool IsNonVariant {
150 get { return attributes.GetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.NonVariant); }
151 set { attributes = attributes.SetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.NonVariant, value); }
154 public bool IsCovariant {
155 get { return attributes.GetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Covariant); }
156 set { attributes = attributes.SetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Covariant, value); }
159 public bool IsContravariant {
160 get { return attributes.GetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Contravariant); }
161 set { attributes = attributes.SetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Contravariant, value); }
164 public bool HasReferenceTypeConstraint {
165 get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.ReferenceTypeConstraint); }
166 set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.ReferenceTypeConstraint, value); }
169 public bool HasNotNullableValueTypeConstraint {
170 get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.NotNullableValueTypeConstraint); }
171 set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.NotNullableValueTypeConstraint, value); }
174 public bool HasDefaultConstructorConstraint {
175 get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.DefaultConstructorConstraint); }
176 set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.DefaultConstructorConstraint, value); }
181 public GenericParameter (IGenericParameterProvider owner)
182 : this (string.Empty, owner)
186 public GenericParameter (string name, IGenericParameterProvider owner)
187 : base (string.Empty, name)
190 throw new ArgumentNullException ();
193 this.etype = owner.GenericParameterType == GenericParameterType.Type ? ElementType.Var : ElementType.MVar;
196 public override TypeDefinition Resolve ()