/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. using System;
  29. using Mono.Collections.Generic;
  30. using Mono.Cecil.Metadata;
  31. namespace Mono.Cecil {
  32. public sealed class GenericParameter : TypeReference, ICustomAttributeProvider {
  33. internal int position;
  34. internal GenericParameterType type;
  35. internal IGenericParameterProvider owner;
  36. ushort attributes;
  37. Collection<TypeReference> constraints;
  38. Collection<CustomAttribute> custom_attributes;
  39. public GenericParameterAttributes Attributes {
  40. get { return (GenericParameterAttributes) attributes; }
  41. set { attributes = (ushort) value; }
  42. }
  43. public int Position {
  44. get { return position; }
  45. }
  46. public GenericParameterType Type {
  47. get { return type; }
  48. }
  49. public IGenericParameterProvider Owner {
  50. get { return owner; }
  51. }
  52. public bool HasConstraints {
  53. get {
  54. if (constraints != null)
  55. return constraints.Count > 0;
  56. if (HasImage)
  57. return Module.Read (this, (generic_parameter, reader) => reader.HasGenericConstraints (generic_parameter));
  58. return false;
  59. }
  60. }
  61. public Collection<TypeReference> Constraints {
  62. get {
  63. if (constraints != null)
  64. return constraints;
  65. if (HasImage)
  66. return Module.Read (ref constraints, this, (generic_parameter, reader) => reader.ReadGenericConstraints (generic_parameter));
  67. return constraints = new Collection<TypeReference> ();
  68. }
  69. }
  70. public bool HasCustomAttributes {
  71. get {
  72. if (custom_attributes != null)
  73. return custom_attributes.Count > 0;
  74. return this.GetHasCustomAttributes (Module);
  75. }
  76. }
  77. public Collection<CustomAttribute> CustomAttributes {
  78. get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, Module)); }
  79. }
  80. public override IMetadataScope Scope {
  81. get {
  82. if (owner == null)
  83. return null;
  84. return owner.GenericParameterType == GenericParameterType.Method
  85. ? ((MethodReference) owner).DeclaringType.Scope
  86. : ((TypeReference) owner).Scope;
  87. }
  88. }
  89. public override ModuleDefinition Module {
  90. get { return module ?? owner.Module; }
  91. }
  92. public override string Name {
  93. get {
  94. if (!string.IsNullOrEmpty (base.Name))
  95. return base.Name;
  96. return base.Name = (type == GenericParameterType.Method ? "!!" : "!") + position;
  97. }
  98. }
  99. public override string Namespace {
  100. get { return string.Empty; }
  101. set { throw new InvalidOperationException (); }
  102. }
  103. public override string FullName {
  104. get { return Name; }
  105. }
  106. public override bool IsGenericParameter {
  107. get { return true; }
  108. }
  109. internal override bool ContainsGenericParameter {
  110. get { return true; }
  111. }
  112. public override MetadataType MetadataType {
  113. get { return (MetadataType) etype; }
  114. }
  115. #region GenericParameterAttributes
  116. public bool IsNonVariant {
  117. get { return attributes.GetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.NonVariant); }
  118. set { attributes = attributes.SetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.NonVariant, value); }
  119. }
  120. public bool IsCovariant {
  121. get { return attributes.GetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Covariant); }
  122. set { attributes = attributes.SetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Covariant, value); }
  123. }
  124. public bool IsContravariant {
  125. get { return attributes.GetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Contravariant); }
  126. set { attributes = attributes.SetMaskedAttributes ((ushort) GenericParameterAttributes.VarianceMask, (ushort) GenericParameterAttributes.Contravariant, value); }
  127. }
  128. public bool HasReferenceTypeConstraint {
  129. get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.ReferenceTypeConstraint); }
  130. set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.ReferenceTypeConstraint, value); }
  131. }
  132. public bool HasNotNullableValueTypeConstraint {
  133. get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.NotNullableValueTypeConstraint); }
  134. set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.NotNullableValueTypeConstraint, value); }
  135. }
  136. public bool HasDefaultConstructorConstraint {
  137. get { return attributes.GetAttributes ((ushort) GenericParameterAttributes.DefaultConstructorConstraint); }
  138. set { attributes = attributes.SetAttributes ((ushort) GenericParameterAttributes.DefaultConstructorConstraint, value); }
  139. }
  140. #endregion
  141. public GenericParameter (IGenericParameterProvider owner)
  142. : this (string.Empty, owner)
  143. {
  144. }
  145. public GenericParameter (string name, IGenericParameterProvider owner)
  146. : base (string.Empty, name)
  147. {
  148. if (owner == null)
  149. throw new ArgumentNullException ();
  150. this.position = -1;
  151. this.owner = owner;
  152. this.type = owner.GenericParameterType;
  153. this.etype = ConvertGenericParameterType (this.type);
  154. }
  155. public GenericParameter (int position, GenericParameterType type, ModuleDefinition module)
  156. : base (string.Empty, string.Empty)
  157. {
  158. if (module == null)
  159. throw new ArgumentNullException ();
  160. this.position = position;
  161. this.type = type;
  162. this.etype = ConvertGenericParameterType (type);
  163. this.module = module;
  164. }
  165. static ElementType ConvertGenericParameterType (GenericParameterType type)
  166. {
  167. switch (type) {
  168. case GenericParameterType.Type:
  169. return ElementType.Var;
  170. case GenericParameterType.Method:
  171. return ElementType.MVar;
  172. }
  173. throw new ArgumentOutOfRangeException ();
  174. }
  175. public override TypeDefinition Resolve ()
  176. {
  177. return null;
  178. }
  179. }
  180. sealed class GenericParameterCollection : Collection<GenericParameter> {
  181. readonly IGenericParameterProvider owner;
  182. internal GenericParameterCollection (IGenericParameterProvider owner)
  183. {
  184. this.owner = owner;
  185. }
  186. internal GenericParameterCollection (IGenericParameterProvider owner, int capacity)
  187. : base (capacity)
  188. {
  189. this.owner = owner;
  190. }
  191. protected override void OnAdd (GenericParameter item, int index)
  192. {
  193. UpdateGenericParameter (item, index);
  194. }
  195. protected override void OnInsert (GenericParameter item, int index)
  196. {
  197. UpdateGenericParameter (item, index);
  198. for (int i = index; i < size; i++)
  199. items[i].position = i + 1;
  200. }
  201. protected override void OnSet (GenericParameter item, int index)
  202. {
  203. UpdateGenericParameter (item, index);
  204. }
  205. void UpdateGenericParameter (GenericParameter item, int index)
  206. {
  207. item.owner = owner;
  208. item.position = index;
  209. item.type = owner.GenericParameterType;
  210. }
  211. protected override void OnRemove (GenericParameter item, int index)
  212. {
  213. item.owner = null;
  214. item.position = -1;
  215. item.type = GenericParameterType.Type;
  216. for (int i = index + 1; i < size; i++)
  217. items[i].position = i - 1;
  218. }
  219. }
  220. }