PageRenderTime 42ms CodeModel.GetById 18ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Mono.Cecil/Mono.Cecil/GenericParameter.cs

http://github.com/icsharpcode/ILSpy
C# | 277 lines | 193 code | 57 blank | 27 comment | 20 complexity | 7af3759c58b3d032b20e1bc75ff5bd5f MD5 | raw file
  1//
  2// GenericParameter.cs
  3//
  4// Author:
  5//   Jb Evain (jbevain@gmail.com)
  6//
  7// Copyright (c) 2008 - 2011 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
 29using System;
 30
 31using Mono.Collections.Generic;
 32
 33using Mono.Cecil.Metadata;
 34
 35namespace Mono.Cecil {
 36
 37	public sealed class GenericParameter : TypeReference, ICustomAttributeProvider {
 38
 39		internal int position;
 40		internal GenericParameterType type;
 41		internal IGenericParameterProvider owner;
 42
 43		ushort attributes;
 44		Collection<TypeReference> constraints;
 45		Collection<CustomAttribute> custom_attributes;
 46
 47		public GenericParameterAttributes Attributes {
 48			get { return (GenericParameterAttributes) attributes; }
 49			set { attributes = (ushort) value; }
 50		}
 51
 52		public int Position {
 53			get { return position; }
 54		}
 55
 56		public GenericParameterType Type {
 57			get { return type; }
 58		}
 59
 60		public IGenericParameterProvider Owner {
 61			get { return owner; }
 62		}
 63
 64		public bool HasConstraints {
 65			get {
 66				if (constraints != null)
 67					return constraints.Count > 0;
 68
 69				if (HasImage)
 70					return Module.Read (this, (generic_parameter, reader) => reader.HasGenericConstraints (generic_parameter));
 71
 72				return false;
 73			}
 74		}
 75
 76		public Collection<TypeReference> Constraints {
 77			get {
 78				if (constraints != null)
 79					return constraints;
 80
 81				if (HasImage)
 82					return Module.Read (ref constraints, this, (generic_parameter, reader) => reader.ReadGenericConstraints (generic_parameter));
 83
 84				return constraints = new Collection<TypeReference> ();
 85			}
 86		}
 87
 88		public bool HasCustomAttributes {
 89			get {
 90				if (custom_attributes != null)
 91					return custom_attributes.Count > 0;
 92
 93				return this.GetHasCustomAttributes (Module);
 94			}
 95		}
 96
 97		public Collection<CustomAttribute> CustomAttributes {
 98			get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, Module)); }
 99		}
100
101		public override IMetadataScope Scope {
102			get {
103				if (owner == null)
104					return null;
105
106				return owner.GenericParameterType == GenericParameterType.Method
107					? ((MethodReference) owner).DeclaringType.Scope
108					: ((TypeReference) owner).Scope;
109			}
110		}
111
112		public override ModuleDefinition Module {
113			get { return module ?? owner.Module; }
114		}
115
116		public override string Name {
117			get {
118				if (!string.IsNullOrEmpty (base.Name))
119					return base.Name;
120
121				return base.Name = (type == GenericParameterType.Method ? "!!" : "!") + position;
122			}
123		}
124
125		public override string Namespace {
126			get { return string.Empty; }
127			set { throw new InvalidOperationException (); }
128		}
129
130		public override string FullName {
131			get { return Name; }
132		}
133
134		public override bool IsGenericParameter {
135			get { return true; }
136		}
137
138		internal override bool ContainsGenericParameter {
139			get { return true; }
140		}
141
142		public override MetadataType MetadataType {
143			get { return (MetadataType) etype; }
144		}
145
146		#region GenericParameterAttributes
147
148		public bool IsNonVariant {
149			get { return attributes.GetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.NonVariant); }
150			set { attributes = attributes.SetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.NonVariant, value); }
151		}
152
153		public bool IsCovariant {
154			get { return attributes.GetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Covariant); }
155			set { attributes = attributes.SetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Covariant, value); }
156		}
157
158		public bool IsContravariant {
159			get { return attributes.GetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Contravariant); }
160			set { attributes = attributes.SetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Contravariant, value); }
161		}
162
163		public bool HasReferenceTypeConstraint {
164			get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.ReferenceTypeConstraint); }
165			set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.ReferenceTypeConstraint, value); }
166		}
167
168		public bool HasNotNullableValueTypeConstraint {
169			get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.NotNullableValueTypeConstraint); }
170			set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.NotNullableValueTypeConstraint, value); }
171		}
172
173		public bool HasDefaultConstructorConstraint {
174			get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.DefaultConstructorConstraint); }
175			set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.DefaultConstructorConstraint, value); }
176		}
177
178		#endregion
179
180		public GenericParameter (IGenericParameterProvider owner)
181			: this (string.Empty, owner)
182		{
183		}
184
185		public GenericParameter (string name, IGenericParameterProvider owner)
186			: base (string.Empty, name)
187		{
188			if (owner == null)
189				throw new ArgumentNullException ();
190
191			this.position = -1;
192			this.owner = owner;
193			this.type = owner.GenericParameterType;
194			this.etype = ConvertGenericParameterType (this.type);
195		}
196
197		public GenericParameter (int position, GenericParameterType type, ModuleDefinition module)
198			: base (string.Empty, string.Empty)
199		{
200			if (module == null)
201				throw new ArgumentNullException ();
202
203			this.position = position;
204			this.type = type;
205			this.etype = ConvertGenericParameterType (type);
206			this.module = module;
207		}
208
209		static ElementType ConvertGenericParameterType (GenericParameterType type)
210		{
211			switch (type) {
212			case GenericParameterType.Type:
213				return ElementType.Var;
214			case GenericParameterType.Method:
215				return ElementType.MVar;
216			}
217
218			throw new ArgumentOutOfRangeException ();
219		}
220
221		public override TypeDefinition Resolve ()
222		{
223			return null;
224		}
225	}
226
227	sealed class GenericParameterCollection : Collection<GenericParameter> {
228
229		readonly IGenericParameterProvider owner;
230
231		internal GenericParameterCollection (IGenericParameterProvider owner)
232		{
233			this.owner = owner;
234		}
235
236		internal GenericParameterCollection (IGenericParameterProvider owner, int capacity)
237			: base (capacity)
238		{
239			this.owner = owner;
240		}
241
242		protected override void OnAdd (GenericParameter item, int index)
243		{
244			UpdateGenericParameter (item, index);
245		}
246
247		protected override void OnInsert (GenericParameter item, int index)
248		{
249			UpdateGenericParameter (item, index);
250
251			for (int i = index; i < size; i++)
252				items[i].position = i + 1;
253		}
254
255		protected override void OnSet (GenericParameter item, int index)
256		{
257			UpdateGenericParameter (item, index);
258		}
259
260		void UpdateGenericParameter (GenericParameter item, int index)
261		{
262			item.owner = owner;
263			item.position = index;
264			item.type = owner.GenericParameterType;
265		}
266
267		protected override void OnRemove (GenericParameter item, int index)
268		{
269			item.owner = null;
270			item.position = -1;
271			item.type = GenericParameterType.Type;
272
273			for (int i = index + 1; i < size; i++)
274				items[i].position = i - 1;
275		}
276	}
277}