PageRenderTime 41ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 1ms

/Microsoft.CCI/MetadataHelper/Types.cs

http://github.com/vc3/Afterthought
C# | 4025 lines | 2488 code | 586 blank | 951 comment | 467 complexity | 88e6d4ea84510bcff03e936a84b21488 MD5 | raw file
  1. //-----------------------------------------------------------------------------
  2. //
  3. // Copyright (c) Microsoft. All rights reserved.
  4. // This code is licensed under the Microsoft Public License.
  5. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  6. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  7. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  8. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  9. //
  10. //-----------------------------------------------------------------------------
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.Text;
  15. using System.Diagnostics.Contracts;
  16. //^ using Microsoft.Contracts;
  17. #pragma warning disable 1591
  18. namespace Microsoft.Cci.Immutable {
  19. public abstract class ArrayType : SystemDefinedStructuralType, IArrayType {
  20. internal ArrayType(ITypeReference elementType, IInternFactory internFactory)
  21. : base(internFactory) {
  22. this.elementType = elementType;
  23. }
  24. public override IEnumerable<ITypeReference> BaseClasses {
  25. get { return IteratorHelper.GetSingletonEnumerable<ITypeReference>(this.PlatformType.SystemArray); }
  26. }
  27. [Pure]
  28. public override bool Contains(ITypeDefinitionMember member) {
  29. foreach (ITypeDefinitionMember mem in this.Members)
  30. if (mem == member) return true;
  31. return false;
  32. }
  33. /// <summary>
  34. /// Calls visitor.Visit(IArrayTypeReference)
  35. /// </summary>
  36. public override void Dispatch(IMetadataVisitor visitor) {
  37. visitor.Visit(this);
  38. }
  39. /// <summary>
  40. /// Calls visitor.Visit(IArrayTypeReference)
  41. /// </summary>
  42. public override void DispatchAsReference(IMetadataVisitor visitor) {
  43. visitor.Visit(this);
  44. }
  45. public ITypeReference ElementType {
  46. get { return this.elementType; }
  47. }
  48. readonly ITypeReference elementType;
  49. protected virtual IEnumerable<ITypeReference> GetInterfaceList() {
  50. var coreAssembly = TypeHelper.GetDefiningUnitReference(this.PlatformType.SystemArray) as IAssemblyReference;
  51. var version = coreAssembly == null ? new Version(4, 0) : coreAssembly.AssemblyIdentity.Version;
  52. List<ITypeReference> interfaces = new List<ITypeReference>(6);
  53. interfaces.Add(this.PlatformType.SystemICloneable);
  54. interfaces.Add(this.PlatformType.SystemCollectionsIEnumerable);
  55. interfaces.Add(this.PlatformType.SystemCollectionsICollection);
  56. interfaces.Add(this.PlatformType.SystemCollectionsIList);
  57. if (version.Major >= 2) {
  58. interfaces.Add(this.PlatformType.SystemCollectionsIStructuralComparable);
  59. interfaces.Add(this.PlatformType.SystemCollectionsIStructuralEquatable);
  60. }
  61. return interfaces.AsReadOnly();
  62. }
  63. public override IEnumerable<ITypeDefinitionMember> GetMatchingMembersNamed(IName name, bool ignoreCase, Function<ITypeDefinitionMember, bool> predicate) {
  64. foreach (ITypeDefinitionMember member in this.Members) {
  65. if (name.UniqueKey != member.Name.UniqueKey || (ignoreCase && name.UniqueKeyIgnoringCase == member.Name.UniqueKeyIgnoringCase)) {
  66. if (predicate(member)) yield return member;
  67. }
  68. }
  69. }
  70. public override IEnumerable<ITypeDefinitionMember> GetMatchingMembers(Function<ITypeDefinitionMember, bool> predicate) {
  71. foreach (ITypeDefinitionMember member in this.Members) {
  72. if (predicate(member)) yield return member;
  73. }
  74. }
  75. public override IEnumerable<ITypeDefinitionMember> GetMembersNamed(IName name, bool ignoreCase) {
  76. foreach (ITypeDefinitionMember member in this.Members) {
  77. if (name.UniqueKey != member.Name.UniqueKey || (ignoreCase && name.UniqueKeyIgnoringCase == member.Name.UniqueKeyIgnoringCase)) {
  78. yield return member;
  79. }
  80. }
  81. }
  82. public override IEnumerable<ITypeReference> Interfaces {
  83. get {
  84. if (this.interfaces == null) {
  85. lock (GlobalLock.LockingObject) {
  86. if (this.interfaces == null) {
  87. this.interfaces = this.GetInterfaceList();
  88. }
  89. }
  90. }
  91. return this.interfaces;
  92. }
  93. }
  94. IEnumerable<ITypeReference>/*?*/ interfaces;
  95. public override bool IsReferenceType {
  96. get { return true; }
  97. }
  98. public virtual bool IsVector {
  99. get { return this.Rank == 1; }
  100. }
  101. public virtual IEnumerable<int> LowerBounds {
  102. get { return Enumerable<int>.Empty; }
  103. }
  104. public override IEnumerable<ITypeDefinitionMember> Members {
  105. get { return Enumerable<ITypeDefinitionMember>.Empty; }
  106. }
  107. public override IPlatformType PlatformType {
  108. get { return this.ElementType.PlatformType; }
  109. }
  110. public virtual uint Rank {
  111. get { return 1; }
  112. }
  113. public virtual IEnumerable<ulong> Sizes {
  114. get { return Enumerable<ulong>.Empty; }
  115. }
  116. //^ [Confined]
  117. public override string ToString() {
  118. return TypeHelper.GetTypeName(this, NameFormattingOptions.None);
  119. }
  120. #region ITypeDefinition Members
  121. IEnumerable<ITypeReference> ITypeDefinition.BaseClasses {
  122. get {
  123. return this.BaseClasses;
  124. }
  125. }
  126. IEnumerable<IGenericTypeParameter> ITypeDefinition.GenericParameters {
  127. get { return Enumerable<IGenericTypeParameter>.Empty; }
  128. }
  129. ushort ITypeDefinition.GenericParameterCount {
  130. get {
  131. //^ assume this.IsGeneric == ((ITypeDefinition)this).IsGeneric;
  132. return 0;
  133. }
  134. }
  135. #endregion
  136. #region IContainer<ITypeDefinitionMember> Members
  137. IEnumerable<ITypeDefinitionMember> IContainer<ITypeDefinitionMember>.Members {
  138. get { return Enumerable<ITypeDefinitionMember>.Empty; }
  139. }
  140. #endregion
  141. #region IDefinition Members
  142. IEnumerable<ICustomAttribute> IReference.Attributes {
  143. get { return Enumerable<ICustomAttribute>.Empty; }
  144. }
  145. #endregion
  146. #region IScope<ITypeDefinitionMember> Members
  147. [Pure]
  148. IEnumerable<ITypeDefinitionMember> IScope<ITypeDefinitionMember>.GetMatchingMembersNamed(IName name, bool ignoreCase, Function<ITypeDefinitionMember, bool> predicate) {
  149. return Enumerable<ITypeDefinitionMember>.Empty;
  150. }
  151. [Pure]
  152. IEnumerable<ITypeDefinitionMember> IScope<ITypeDefinitionMember>.GetMatchingMembers(Function<ITypeDefinitionMember, bool> predicate) {
  153. return Enumerable<ITypeDefinitionMember>.Empty;
  154. }
  155. [Pure]
  156. IEnumerable<ITypeDefinitionMember> IScope<ITypeDefinitionMember>.GetMembersNamed(IName name, bool ignoreCase) {
  157. return Enumerable<ITypeDefinitionMember>.Empty;
  158. }
  159. IEnumerable<ITypeDefinitionMember> IScope<ITypeDefinitionMember>.Members {
  160. get { return Enumerable<ITypeDefinitionMember>.Empty; }
  161. }
  162. #endregion
  163. }
  164. public class CustomModifier : ICustomModifier {
  165. public CustomModifier(bool isOptional, ITypeReference modifier) {
  166. this.isOptional = isOptional;
  167. this.modifier = modifier;
  168. }
  169. public bool IsOptional {
  170. get { return this.isOptional; }
  171. }
  172. readonly bool isOptional;
  173. public ITypeReference Modifier {
  174. get { return this.modifier; }
  175. }
  176. readonly ITypeReference modifier;
  177. /// <summary>
  178. /// Returns a deep copy of a customer modifier. In the copy, every reference to a partially specialized type parameter defined by
  179. /// the partially specialized version of targetContainer or of one of targetContainer's parents (if the parent is a SpecializedNestedTypeDefinition
  180. /// and generic) will be replaced with the specialized type parameter, defined by targetContainer or its parents.
  181. /// </summary>
  182. /// <param name="customModifier">An array type reference to be deep copied. </param>
  183. /// <param name="targetContainer">A specialized nested type definition whose or whose parents' (specialized) type parameters will
  184. /// replace the occurrences of matching type parameters in <paramref name="customModifier"/>.</param>
  185. /// <param name="internFactory">An intern factory. </param>
  186. internal static ICustomModifier CopyModifierToNewContainer(ICustomModifier customModifier, SpecializedNestedTypeDefinition targetContainer, IInternFactory internFactory) {
  187. ITypeReference copiedModifier = TypeDefinition.DeepCopyTypeReference(customModifier.Modifier, targetContainer, internFactory);
  188. if (copiedModifier == customModifier.Modifier) return customModifier;
  189. return new CustomModifier(customModifier.IsOptional, copiedModifier);
  190. }
  191. internal static ICustomModifier Specialize(ICustomModifier customModifier, ITypeReference targetContainer, IInternFactory internFactory) {
  192. ITypeReference copiedModifier = TypeHelper.SpecializeTypeReference(customModifier.Modifier, targetContainer, internFactory);
  193. if (copiedModifier == customModifier.Modifier) return customModifier;
  194. return new CustomModifier(customModifier.IsOptional, copiedModifier);
  195. }
  196. internal static ICustomModifier Specialize(ICustomModifier customModifier, IMethodReference targetContainer, IInternFactory internFactory) {
  197. ITypeReference copiedModifier = TypeHelper.SpecializeTypeReference(customModifier.Modifier, targetContainer, internFactory);
  198. if (copiedModifier == customModifier.Modifier) return customModifier;
  199. return new CustomModifier(customModifier.IsOptional, copiedModifier);
  200. }
  201. /// <summary>
  202. /// If the given custom modifier has a modifier that involves a type parameter from the generic method from which the given method was instantiated,
  203. /// then return a new custom modifier using a modifier type that has been specialized with the type arguments of the given generic method instance.
  204. /// </summary>
  205. public static ICustomModifier SpecializeIfConstructedFromApplicableTypeParameter(ICustomModifier customModifier, IGenericMethodInstanceReference containingMethodInstance, IInternFactory internFactory) {
  206. ITypeReference copiedModifier = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(customModifier.Modifier, containingMethodInstance, internFactory);
  207. if (copiedModifier == customModifier.Modifier) return customModifier;
  208. return new CustomModifier(customModifier.IsOptional, copiedModifier);
  209. }
  210. /// <summary>
  211. /// If the given custom modifier has a modifier that involves a type parameter from the generic type from which the given type was instantiated,
  212. /// then return a new custom modifier using a modifier type that has been specialized with the type arguments of the given generic type instance.
  213. /// </summary>
  214. public static ICustomModifier SpecializeIfConstructedFromApplicableTypeParameter(ICustomModifier customModifier, IGenericTypeInstanceReference containingTypeInstance, IInternFactory internFactory) {
  215. ITypeReference copiedModifier = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(customModifier.Modifier, containingTypeInstance, internFactory);
  216. if (copiedModifier == customModifier.Modifier) return customModifier;
  217. return new CustomModifier(customModifier.IsOptional, copiedModifier);
  218. }
  219. /// <summary>
  220. /// If the given custom modifier has a modifier that involves a method type parameter of the partially specialized version of specializedMethodReference,
  221. /// then return a new custom modifier using a modifier type that is the corresponding method type parameter from specializedMethodReference.
  222. /// </summary>
  223. internal static ICustomModifier SpecializeIfConstructedFromApplicableMethodTypeParameter(ICustomModifier customModifier, ISpecializedMethodReference specializedMethodReference, IInternFactory internFactory) {
  224. ITypeReference copiedModifier = TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(customModifier.Modifier, specializedMethodReference, internFactory);
  225. if (copiedModifier == customModifier.Modifier) return customModifier;
  226. return new CustomModifier(customModifier.IsOptional, copiedModifier);
  227. }
  228. }
  229. public class FunctionPointerType : SystemDefinedStructuralType, IFunctionPointer {
  230. public FunctionPointerType(ISignature signature, IInternFactory internFactory)
  231. : base(internFactory) {
  232. this.callingConvention = signature.CallingConvention;
  233. if (signature.ReturnValueIsModified)
  234. this.returnValueCustomModifiers = signature.ReturnValueCustomModifiers;
  235. this.returnValueIsByRef = signature.ReturnValueIsByRef;
  236. this.type = signature.Type;
  237. this.parameters = signature.Parameters;
  238. this.extraArgumentTypes = Enumerable<IParameterTypeInformation>.Empty;
  239. }
  240. public FunctionPointerType(CallingConvention callingConvention, bool returnValueIsByRef, ITypeReference type,
  241. IEnumerable<ICustomModifier>/*?*/ returnValueCustomModifiers, IEnumerable<IParameterTypeInformation> parameters, IEnumerable<IParameterTypeInformation>/*?*/ extraArgumentTypes,
  242. IInternFactory internFactory)
  243. : base(internFactory) {
  244. this.callingConvention = callingConvention;
  245. this.returnValueCustomModifiers = returnValueCustomModifiers;
  246. this.returnValueIsByRef = returnValueIsByRef;
  247. this.type = type;
  248. this.parameters = parameters;
  249. if (extraArgumentTypes == null)
  250. this.extraArgumentTypes = Enumerable<IParameterTypeInformation>.Empty;
  251. else
  252. this.extraArgumentTypes = extraArgumentTypes;
  253. }
  254. public CallingConvention CallingConvention {
  255. get { return this.callingConvention; }
  256. }
  257. readonly CallingConvention callingConvention;
  258. /// <summary>
  259. /// Calls visitor.Visit(IFunctionPointerTypeReference)
  260. /// </summary>
  261. public override void Dispatch(IMetadataVisitor visitor) {
  262. visitor.Visit(this);
  263. }
  264. /// <summary>
  265. /// Calls visitor.Visit(IFunctionPointerTypeReference)
  266. /// </summary>
  267. public override void DispatchAsReference(IMetadataVisitor visitor) {
  268. visitor.Visit(this);
  269. }
  270. public IEnumerable<IParameterTypeInformation> ExtraArgumentTypes {
  271. get { return this.extraArgumentTypes; }
  272. }
  273. readonly IEnumerable<IParameterTypeInformation> extraArgumentTypes;
  274. public override IPlatformType PlatformType {
  275. get { return this.Type.PlatformType; }
  276. }
  277. public IEnumerable<IParameterTypeInformation> Parameters {
  278. get { return this.parameters; }
  279. }
  280. readonly IEnumerable<IParameterTypeInformation> parameters;
  281. public IEnumerable<ICustomModifier> ReturnValueCustomModifiers {
  282. get {
  283. if (this.returnValueCustomModifiers == null) return Enumerable<ICustomModifier>.Empty; //shouldn't get here if the precondition is respected.
  284. return this.returnValueCustomModifiers;
  285. }
  286. }
  287. readonly IEnumerable<ICustomModifier>/*?*/ returnValueCustomModifiers;
  288. public bool ReturnValueIsByRef {
  289. get { return this.returnValueIsByRef; }
  290. }
  291. readonly bool returnValueIsByRef;
  292. public bool ReturnValueIsModified {
  293. get { return this.returnValueCustomModifiers != null; }
  294. }
  295. public ITypeReference Type {
  296. get { return this.type; }
  297. }
  298. readonly ITypeReference type;
  299. public override PrimitiveTypeCode TypeCode {
  300. get { return PrimitiveTypeCode.Pointer; }
  301. }
  302. #region ISignature Members
  303. bool ISignature.IsStatic {
  304. get { return this.IsStatic; }
  305. }
  306. ITypeReference ISignature.Type {
  307. get { return this.Type; }
  308. }
  309. #endregion
  310. }
  311. public class GenericTypeInstance : Scope<ITypeDefinitionMember>, IGenericTypeInstance {
  312. public static GenericTypeInstance GetGenericTypeInstance(INamedTypeReference genericType, IEnumerable<ITypeReference> genericArguments, IInternFactory internFactory)
  313. //^ requires genericType.ResolvedType.IsGeneric;
  314. //^ ensures !result.IsGeneric;
  315. {
  316. Contract.Requires(!(genericType is Dummy));
  317. Contract.Ensures(Contract.Result<GenericTypeInstance>() != null);
  318. return new GenericTypeInstance(genericType, genericArguments, internFactory);
  319. }
  320. private GenericTypeInstance(INamedTypeReference genericType, IEnumerable<ITypeReference> genericArguments, IInternFactory internFactory)
  321. //^ requires genericType.ResolvedType.IsGeneric;
  322. {
  323. Contract.Requires(!(genericType is Dummy));
  324. this.genericType = genericType;
  325. this.genericArguments = genericArguments;
  326. this.internFactory = internFactory;
  327. }
  328. public ushort Alignment {
  329. get { return this.GenericType.ResolvedType.Alignment; }
  330. }
  331. public virtual IEnumerable<ICustomAttribute> Attributes {
  332. get { return Enumerable<ICustomAttribute>.Empty; }
  333. }
  334. public IEnumerable<ITypeReference> BaseClasses {
  335. get {
  336. foreach (ITypeReference baseClassRef in this.GenericType.ResolvedType.BaseClasses) {
  337. ITypeReference specializedBaseClass = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(baseClassRef, this, this.InternFactory);
  338. yield return specializedBaseClass;
  339. }
  340. }
  341. }
  342. /// <summary>
  343. /// Calls this.Visit(IGenericTypeInstanceReference).
  344. /// </summary>
  345. public void Dispatch(IMetadataVisitor visitor) {
  346. visitor.Visit(this);
  347. }
  348. /// <summary>
  349. /// Calls this.Visit(IGenericTypeInstanceReference).
  350. /// </summary>
  351. public void DispatchAsReference(IMetadataVisitor visitor) {
  352. visitor.Visit(this);
  353. }
  354. public IEnumerable<IEventDefinition> Events {
  355. get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, IEventDefinition>(this.Members); }
  356. }
  357. public IEnumerable<IFieldDefinition> Fields {
  358. get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, IFieldDefinition>(this.Members); }
  359. }
  360. public IEnumerable<ILocation> Locations {
  361. get { return Enumerable<ILocation>.Empty; }
  362. }
  363. public IEnumerable<IMethodImplementation> ExplicitImplementationOverrides {
  364. get {
  365. foreach (var methodImplementation in this.GenericType.ResolvedType.ExplicitImplementationOverrides)
  366. yield return new SpecializedMethodImplementation(this, methodImplementation, this.InternFactory);
  367. }
  368. }
  369. public IEnumerable<ITypeReference> GenericArguments {
  370. get { return this.genericArguments; }
  371. }
  372. readonly IEnumerable<ITypeReference> genericArguments;
  373. public INamedTypeReference GenericType {
  374. get { return this.genericType; }
  375. }
  376. readonly INamedTypeReference genericType; //^ invariant genericType.ResolvedType.IsGeneric;
  377. protected override void InitializeIfNecessary() {
  378. if (this.initialized) return;
  379. lock (GlobalLock.LockingObject) {
  380. if (this.initialized) return;
  381. foreach (ITypeDefinitionMember unspecializedMember in this.GenericType.ResolvedType.Members) {
  382. //^ assume unspecializedMember is IEventDefinition || unspecializedMember is IFieldDefinition || unspecializedMember is IMethodDefinition ||
  383. //^ unspecializedMember is IPropertyDefinition || unspecializedMember is INestedTypeDefinition; //follows from informal post condition on Members property.
  384. this.AddMemberToCache(this.SpecializeMember(unspecializedMember, this.InternFactory));
  385. }
  386. this.initialized = true;
  387. }
  388. }
  389. private bool initialized;
  390. public IGenericTypeInstanceReference InstanceType {
  391. get { return this; }
  392. }
  393. public IEnumerable<ITypeReference> Interfaces {
  394. get {
  395. foreach (ITypeReference ifaceRef in this.GenericType.ResolvedType.Interfaces) {
  396. ITypeReference specializedIface = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(ifaceRef, this, this.InternFactory);
  397. yield return specializedIface;
  398. }
  399. }
  400. }
  401. public bool IsAbstract {
  402. get { return this.GenericType.ResolvedType.IsAbstract; }
  403. }
  404. public bool IsClass {
  405. get { return this.GenericType.ResolvedType.IsClass; }
  406. }
  407. public bool IsDelegate {
  408. get { return this.GenericType.ResolvedType.IsDelegate; }
  409. }
  410. public bool IsEnum {
  411. get { return false; }
  412. }
  413. public bool IsGeneric {
  414. get
  415. //^ ensures result == false;
  416. {
  417. return false;
  418. }
  419. }
  420. public bool IsInterface {
  421. get { return this.GenericType.ResolvedType.IsInterface; }
  422. }
  423. public bool IsReferenceType {
  424. get { return this.GenericType.ResolvedType.IsReferenceType; }
  425. }
  426. public bool IsSealed {
  427. get { return this.GenericType.ResolvedType.IsSealed; }
  428. }
  429. public bool IsStatic {
  430. get { return this.GenericType.ResolvedType.IsStatic; }
  431. }
  432. public bool IsValueType {
  433. get { return this.GenericType.IsValueType; }
  434. }
  435. public bool IsStruct {
  436. get { return this.GenericType.ResolvedType.IsStruct; }
  437. }
  438. public IEnumerable<IMethodDefinition> Methods {
  439. get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, IMethodDefinition>(this.Members); }
  440. }
  441. public IEnumerable<INestedTypeDefinition> NestedTypes {
  442. get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, INestedTypeDefinition>(this.Members); }
  443. }
  444. public IPlatformType PlatformType {
  445. get { return this.GenericType.PlatformType; }
  446. }
  447. public IEnumerable<ITypeDefinitionMember> PrivateHelperMembers {
  448. get {
  449. //TODO: specialize and cache the private helper members of the generic type template.
  450. return Enumerable<ITypeDefinitionMember>.Empty;
  451. }
  452. }
  453. public IEnumerable<IPropertyDefinition> Properties {
  454. get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, IPropertyDefinition>(this.Members); }
  455. }
  456. /// <summary>
  457. /// Returns a deep copy of a generic type instance reference. In the copy, every reference to a partially specialized type parameter defined by
  458. /// the partially specialized version of targetContainer or of one of targetContainer's parents (if the parent is a SpecializedNestedTypeDefinition
  459. /// and generic) will be replaced with the specialized type parameter, defined by targetContainer or its parents.
  460. /// </summary>
  461. /// <param name="genericTypeInstance">An array type reference to be deep copied. </param>
  462. /// <param name="targetContainer">A specialized nested type definition whose or whose parents' (specialized) type parameters will
  463. /// replace the occurrences of matching type parameters in <paramref name="genericTypeInstance"/>.</param>
  464. /// /// <param name="internFactory">An intern factory. </param>
  465. internal static ITypeReference DeepCopyTypeReference(IGenericTypeInstanceReference genericTypeInstance, SpecializedNestedTypeDefinition targetContainer, IInternFactory internFactory) {
  466. var copiedGenericType = (INamedTypeReference)TypeDefinition.DeepCopyTypeReference(genericTypeInstance.GenericType, targetContainer, internFactory);
  467. List<ITypeReference>/*?*/ copiedArguments = null;
  468. int i = 0;
  469. foreach (ITypeReference argType in genericTypeInstance.GenericArguments) {
  470. ITypeReference copiedArgType = TypeDefinition.DeepCopyTypeReference(argType, targetContainer, internFactory);
  471. if (argType != copiedArgType) {
  472. if (copiedArguments == null) copiedArguments = new List<ITypeReference>(genericTypeInstance.GenericArguments);
  473. //^ assume 0 <= i && i < specializedArguments.Count; //Since genericTypeInstance.GenericArguments is immutable
  474. copiedArguments[i] = copiedArgType;
  475. }
  476. i++;
  477. }
  478. if (copiedArguments == null) {
  479. if (copiedGenericType == genericTypeInstance.GenericType) return genericTypeInstance;
  480. return GetGenericTypeInstance(copiedGenericType, genericTypeInstance.GenericArguments, internFactory);
  481. }
  482. return GetGenericTypeInstance(copiedGenericType, copiedArguments, internFactory);
  483. }
  484. internal static ITypeReference SpecializeTypeReference(IGenericTypeInstanceReference genericTypeInstance, ITypeReference targetContainer, IInternFactory internFactory) {
  485. var copiedGenericType = (INamedTypeReference)TypeHelper.SpecializeTypeReference(genericTypeInstance.GenericType, targetContainer, internFactory);
  486. List<ITypeReference>/*?*/ copiedArguments = null;
  487. int i = 0;
  488. foreach (ITypeReference argType in genericTypeInstance.GenericArguments) {
  489. ITypeReference copiedArgType = TypeHelper.SpecializeTypeReference(argType, targetContainer, internFactory);
  490. if (argType != copiedArgType) {
  491. if (copiedArguments == null) copiedArguments = new List<ITypeReference>(genericTypeInstance.GenericArguments);
  492. //^ assume 0 <= i && i < specializedArguments.Count; //Since genericTypeInstance.GenericArguments is immutable
  493. copiedArguments[i] = copiedArgType;
  494. }
  495. i++;
  496. }
  497. if (copiedArguments == null) {
  498. if (copiedGenericType == genericTypeInstance.GenericType) return genericTypeInstance;
  499. return GetGenericTypeInstance(copiedGenericType, genericTypeInstance.GenericArguments, internFactory);
  500. }
  501. return GetGenericTypeInstance(copiedGenericType, copiedArguments, internFactory);
  502. }
  503. internal static ITypeReference SpecializeTypeReference(IGenericTypeInstanceReference genericTypeInstance, IMethodReference targetContainer, IInternFactory internFactory) {
  504. var copiedGenericType = (INamedTypeReference)TypeHelper.SpecializeTypeReference(genericTypeInstance.GenericType, targetContainer, internFactory);
  505. List<ITypeReference>/*?*/ copiedArguments = null;
  506. int i = 0;
  507. foreach (ITypeReference argType in genericTypeInstance.GenericArguments) {
  508. ITypeReference copiedArgType = TypeHelper.SpecializeTypeReference(argType, targetContainer, internFactory);
  509. if (argType != copiedArgType) {
  510. if (copiedArguments == null) copiedArguments = new List<ITypeReference>(genericTypeInstance.GenericArguments);
  511. //^ assume 0 <= i && i < specializedArguments.Count; //Since genericTypeInstance.GenericArguments is immutable
  512. copiedArguments[i] = copiedArgType;
  513. }
  514. i++;
  515. }
  516. if (copiedArguments == null) {
  517. if (copiedGenericType == genericTypeInstance.GenericType) return genericTypeInstance;
  518. return GetGenericTypeInstance(copiedGenericType, genericTypeInstance.GenericArguments, internFactory);
  519. }
  520. return GetGenericTypeInstance(copiedGenericType, copiedArguments, internFactory);
  521. }
  522. /// <summary>
  523. /// Specialize component type references of genericTypeInstance and (if necessary) return a new instance of the
  524. /// specialized version of genericTypeInstance.GenericType using the specialized type arguments. Specialization here
  525. /// means replacing any references to the generic type parameters of containingMethodInstance.GenericMethod with the
  526. /// corresponding values of containingMethodInstance.GenericArguments.
  527. /// </summary>
  528. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IGenericTypeInstanceReference genericTypeInstance, IGenericMethodInstanceReference containingMethodInstance, IInternFactory internFactory) {
  529. var specializedGenericType = (INamedTypeReference)TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(genericTypeInstance.GenericType, containingMethodInstance, internFactory);
  530. List<ITypeReference>/*?*/ specializedArguments = null;
  531. int i = 0;
  532. foreach (ITypeReference argType in genericTypeInstance.GenericArguments) {
  533. ITypeReference specializedArgType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(argType, containingMethodInstance, internFactory);
  534. if (argType != specializedArgType) {
  535. if (specializedArguments == null) specializedArguments = new List<ITypeReference>(genericTypeInstance.GenericArguments);
  536. //^ assume 0 <= i && i < specializedArguments.Count; //Since genericTypeInstance.GenericArguments is immutable
  537. specializedArguments[i] = specializedArgType;
  538. }
  539. i++;
  540. }
  541. if (specializedArguments == null) {
  542. if (specializedGenericType == genericTypeInstance.GenericType) return genericTypeInstance;
  543. else return GetGenericTypeInstance(specializedGenericType, genericTypeInstance.GenericArguments, internFactory);
  544. }
  545. return GetGenericTypeInstance(specializedGenericType, specializedArguments, internFactory);
  546. }
  547. /// <summary>
  548. /// Specialize the type arguments of genericTypeIntance and (if necessary) return a new instance of containingTypeInstance.GenericType using
  549. /// the specialized type arguments. Specialization means replacing any references to the type parameters of containingTypeInstance.GenericType with the
  550. /// corresponding values of containingTypeInstance.GenericArguments.
  551. /// </summary>
  552. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IGenericTypeInstanceReference genericTypeInstance, IGenericTypeInstanceReference containingTypeInstance, IInternFactory internFactory) {
  553. var specializedGenericType = (INamedTypeReference)TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(genericTypeInstance.GenericType, containingTypeInstance, internFactory);
  554. List<ITypeReference>/*?*/ specializedArguments = null;
  555. int i = 0;
  556. foreach (ITypeReference argType in genericTypeInstance.GenericArguments) {
  557. ITypeReference specializedArgType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(argType, containingTypeInstance, internFactory);
  558. if (argType != specializedArgType) {
  559. if (specializedArguments == null) specializedArguments = new List<ITypeReference>(genericTypeInstance.GenericArguments);
  560. //^ assume 0 <= i && i < specializedArguments.Count; //Since genericTypeInstance.GenericArguments is immutable
  561. specializedArguments[i] = specializedArgType;
  562. }
  563. i++;
  564. }
  565. if (specializedArguments == null) {
  566. if (specializedGenericType == genericTypeInstance.GenericType) return genericTypeInstance;
  567. else return GetGenericTypeInstance(specializedGenericType, genericTypeInstance.GenericArguments, internFactory);
  568. }
  569. return GetGenericTypeInstance(specializedGenericType, specializedArguments, internFactory);
  570. }
  571. /// <summary>
  572. /// Specialize the type arguments of genericTypeIntance and (if necessary) return a new instance of containingTypeInstance.GenericType using
  573. /// the specialized type arguments. Specialization means replacing any references to the method type parameters of
  574. /// the partially specialized version of specializedMethodReference with the corresponding references to method type parameters of specializedMethodReference.
  575. /// </summary>
  576. internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(IGenericTypeInstanceReference genericTypeInstance, ISpecializedMethodReference specializedMethodReference, IInternFactory internFactory) {
  577. var specializedGenericType = (INamedTypeReference)TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(genericTypeInstance.GenericType, specializedMethodReference, internFactory);
  578. List<ITypeReference>/*?*/ specializedArguments = null;
  579. int i = 0;
  580. foreach (ITypeReference argType in genericTypeInstance.GenericArguments) {
  581. ITypeReference specializedArgType = TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(argType, specializedMethodReference, internFactory);
  582. if (argType != specializedArgType) {
  583. if (specializedArguments == null) specializedArguments = new List<ITypeReference>(genericTypeInstance.GenericArguments);
  584. //^ assume 0 <= i && i < specializedArguments.Count; //Since genericTypeInstance.GenericArguments is immutable
  585. specializedArguments[i] = specializedArgType;
  586. }
  587. i++;
  588. }
  589. if (specializedArguments == null) {
  590. if (specializedGenericType == genericTypeInstance.GenericType) return genericTypeInstance;
  591. else return GetGenericTypeInstance(specializedGenericType, genericTypeInstance.GenericArguments, internFactory);
  592. }
  593. return GetGenericTypeInstance(specializedGenericType, specializedArguments, internFactory);
  594. }
  595. public ITypeDefinitionMember SpecializeMember(ITypeDefinitionMember unspecializedMember, IInternFactory internFactory)
  596. //^ requires unspecializedMember is IEventDefinition || unspecializedMember is IFieldDefinition || unspecializedMember is IMethodDefinition ||
  597. //^ unspecializedMember is IPropertyDefinition || unspecializedMember is INestedTypeDefinition;
  598. //^ ensures unspecializedMember is IEventDefinition ==> result is IEventDefinition;
  599. //^ ensures unspecializedMember is IFieldDefinition ==> result is IFieldDefinition;
  600. //^ ensures unspecializedMember is IMethodDefinition ==> result is IMethodDefinition;
  601. //^ ensures unspecializedMember is IPropertyDefinition ==> result is IPropertyDefinition;
  602. //^ ensures unspecializedMember is INestedTypeDefinition ==> result is INestedTypeDefinition;
  603. {
  604. IEventDefinition/*?*/ eventDef = unspecializedMember as IEventDefinition;
  605. if (eventDef != null) {
  606. var unspecializedEventDef = eventDef;
  607. var specializedEventDef = eventDef as ISpecializedEventDefinition;
  608. if (specializedEventDef != null) unspecializedEventDef = specializedEventDef.UnspecializedVersion;
  609. return new SpecializedEventDefinition(unspecializedEventDef, eventDef, this, this);
  610. }
  611. IFieldDefinition/*?*/ fieldDef = unspecializedMember as IFieldDefinition;
  612. if (fieldDef != null) {
  613. var unspecializedFieldDef = fieldDef;
  614. var specializedFieldDef = fieldDef as ISpecializedFieldDefinition;
  615. if (specializedFieldDef != null) unspecializedFieldDef = specializedFieldDef.UnspecializedVersion;
  616. return new SpecializedFieldDefinition(unspecializedFieldDef, fieldDef, this, this);
  617. }
  618. IMethodDefinition/*?*/ methodDef = unspecializedMember as IMethodDefinition;
  619. if (methodDef != null) {
  620. var unspecializedMethodDef = methodDef;
  621. var specializedMethodDef = methodDef as ISpecializedMethodDefinition;
  622. if (specializedMethodDef != null) unspecializedMethodDef = specializedMethodDef.UnspecializedVersion;
  623. return new SpecializedMethodDefinition(unspecializedMethodDef, methodDef, this, this);
  624. }
  625. IPropertyDefinition/*?*/ propertyDef = unspecializedMember as IPropertyDefinition;
  626. if (propertyDef != null) {
  627. var unspecializedPropertyDef = propertyDef;
  628. var specializedPropertyDef = propertyDef as ISpecializedPropertyDefinition;
  629. if (specializedPropertyDef != null) unspecializedPropertyDef = specializedPropertyDef.UnspecializedVersion;
  630. return new SpecializedPropertyDefinition(unspecializedPropertyDef, propertyDef, this, this);
  631. }
  632. //^ assert unspecializedMember is INestedTypeDefinition;
  633. INestedTypeDefinition nestedTypeDef = (INestedTypeDefinition)unspecializedMember;
  634. var unspecializedTypeDef = nestedTypeDef;
  635. var specializedTypeDef = nestedTypeDef as ISpecializedNestedTypeDefinition;
  636. if (specializedTypeDef != null) unspecializedTypeDef = specializedTypeDef.UnspecializedVersion;
  637. return new SpecializedNestedTypeDefinition(unspecializedTypeDef, nestedTypeDef, this, this, internFactory);
  638. }
  639. public uint SizeOf {
  640. get { return this.GenericType.ResolvedType.SizeOf; }
  641. }
  642. //^ [Confined]
  643. public override string ToString() {
  644. StringBuilder sb = new StringBuilder();
  645. sb.Append(this.GenericType.ResolvedType.ToString());
  646. sb.Append('<');
  647. foreach (ITypeReference arg in this.GenericArguments) {
  648. if (sb[sb.Length - 1] != '<') sb.Append(',');
  649. sb.Append(arg.ResolvedType.ToString());
  650. }
  651. sb.Append('>');
  652. return sb.ToString();
  653. }
  654. public PrimitiveTypeCode TypeCode {
  655. get { return PrimitiveTypeCode.NotPrimitive; }
  656. }
  657. public ITypeReference UnderlyingType {
  658. get { return this; }
  659. }
  660. public LayoutKind Layout {
  661. get
  662. //^ ensures result == this.GenericType.ResolvedType.Layout;
  663. {
  664. return this.GenericType.ResolvedType.Layout;
  665. }
  666. }
  667. public bool IsSpecialName {
  668. get { return this.GenericType.ResolvedType.IsSpecialName; }
  669. }
  670. public bool IsComObject {
  671. get { return this.GenericType.ResolvedType.IsComObject; }
  672. }
  673. public bool IsSerializable {
  674. get { return this.GenericType.ResolvedType.IsSerializable; }
  675. }
  676. public bool IsBeforeFieldInit {
  677. get { return this.GenericType.ResolvedType.IsBeforeFieldInit; }
  678. }
  679. public StringFormatKind StringFormat {
  680. get { return this.GenericType.ResolvedType.StringFormat; }
  681. }
  682. public bool IsRuntimeSpecial {
  683. get { return this.GenericType.ResolvedType.IsRuntimeSpecial; }
  684. }
  685. public bool HasDeclarativeSecurity {
  686. get { return this.GenericType.ResolvedType.HasDeclarativeSecurity; }
  687. }
  688. #region ITypeDefinition Members
  689. IEnumerable<IGenericTypeParameter> ITypeDefinition.GenericParameters {
  690. get { return Enumerable<IGenericTypeParameter>.Empty; }
  691. }
  692. ushort ITypeDefinition.GenericParameterCount {
  693. get {
  694. return 0;
  695. }
  696. }
  697. IEnumerable<ITypeDefinitionMember> ITypeDefinition.Members {
  698. get {
  699. return this.Members;
  700. }
  701. }
  702. IEnumerable<ISecurityAttribute> ITypeDefinition.SecurityAttributes {
  703. get { return Enumerable<ISecurityAttribute>.Empty; }
  704. }
  705. #endregion
  706. #region IContainer<ITypeDefinitionMember>
  707. IEnumerable<ITypeDefinitionMember> IContainer<ITypeDefinitionMember>.Members {
  708. get {
  709. return this.Members;
  710. }
  711. }
  712. #endregion
  713. #region ITypeReference Members
  714. public bool IsAlias {
  715. get { return false; }
  716. }
  717. public IAliasForType AliasForType {
  718. get { return Dummy.AliasForType; }
  719. }
  720. ITypeDefinition ITypeReference.ResolvedType {
  721. get { return this; }
  722. }
  723. /// <summary>
  724. /// A collection of methods that associate unique integers with metadata model entities.
  725. /// The association is based on the identities of the entities and the factory does not retain
  726. /// references to the given metadata model objects.
  727. /// </summary>
  728. public IInternFactory InternFactory {
  729. get { return this.internFactory; }
  730. }
  731. readonly IInternFactory internFactory;
  732. public uint InternedKey {
  733. get {
  734. if (this.internedKey == 0) {
  735. this.internedKey = this.InternFactory.GetTypeReferenceInternedKey(this);
  736. }
  737. return this.internedKey;
  738. }
  739. }
  740. uint internedKey;
  741. #endregion
  742. }
  743. public class GenericTypeInstanceReference : IGenericTypeInstanceReference {
  744. public GenericTypeInstanceReference(INamedTypeReference genericType, IEnumerable<ITypeReference> genericArguments, IInternFactory internFactory) {
  745. this.genericType = genericType;
  746. this.genericArguments = genericArguments;
  747. this.internFactory = internFactory;
  748. }
  749. #region IGenericTypeInstanceReference Members
  750. public IEnumerable<ITypeReference> GenericArguments {
  751. get { return this.genericArguments; }
  752. }
  753. readonly IEnumerable<ITypeReference> genericArguments;
  754. public INamedTypeReference GenericType {
  755. get { return this.genericType; }
  756. }
  757. readonly INamedTypeReference genericType; //^ invariant genericType.ResolvedType.IsGeneric;
  758. #endregion
  759. #region ITypeReference Members
  760. public IAliasForType AliasForType {
  761. get { return Dummy.AliasForType; }
  762. }
  763. /// <summary>
  764. /// A collection of methods that associate unique integers with metadata model entities.
  765. /// The association is based on the identities of the entities and the factory does not retain
  766. /// references to the given metadata model objects.
  767. /// </summary>
  768. public IInternFactory InternFactory {
  769. get { return this.internFactory; }
  770. }
  771. readonly IInternFactory internFactory;
  772. public uint InternedKey {
  773. get {
  774. if (this.internedKey == 0) {
  775. this.internedKey = this.InternFactory.GetTypeReferenceInternedKey(this);
  776. }
  777. return this.internedKey;
  778. }
  779. }
  780. uint internedKey;
  781. public bool IsAlias {
  782. get { return false; }
  783. }
  784. public bool IsEnum {
  785. get { return false; }
  786. }
  787. public bool IsValueType {
  788. get { return this.GenericType.IsValueType; }
  789. }
  790. public IPlatformType PlatformType {
  791. get { return this.GenericType.PlatformType; }
  792. }
  793. public ITypeDefinition ResolvedType {
  794. get {
  795. if (this.resolvedType == null) {
  796. var template = this.GenericType.ResolvedType;
  797. if (template == Dummy.NamedTypeDefinition)
  798. this.resolvedType = Dummy.Type;
  799. else
  800. this.resolvedType = GenericTypeInstance.GetGenericTypeInstance(template, this.GenericArguments, this.InternFactory);
  801. }
  802. return this.resolvedType;
  803. }
  804. }
  805. ITypeDefinition/*?*/ resolvedType;
  806. public PrimitiveTypeCode TypeCode {
  807. get { return PrimitiveTypeCode.NotPrimitive; }
  808. }
  809. public override string ToString() {
  810. return TypeHelper.GetTypeName(this);
  811. }
  812. #endregion
  813. #region IReference Members
  814. public virtual IEnumerable<ICustomAttribute> Attributes {
  815. get { return Enumerable<ICustomAttribute>.Empty; }
  816. }
  817. public void Dispatch(IMetadataVisitor visitor) {
  818. visitor.Visit(this);
  819. }
  820. public void DispatchAsReference(IMetadataVisitor visitor) {
  821. visitor.Visit(this);
  822. }
  823. #endregion
  824. #region IObjectWithLocations Members
  825. public virtual IEnumerable<ILocation> Locations {
  826. get { return Enumerable<ILocation>.Empty; }
  827. }
  828. #endregion
  829. }
  830. internal static class GenericParameter {
  831. /// <summary>
  832. /// If the genericTypeParameter is a type parameter of the targetContainer, or a type parameter of a containing, generic, specialized
  833. /// nested type of the targetContainer, return the specialized version of the type parameter.
  834. /// </summary>
  835. /// <remarks>:
  836. /// Example of how a type parameter is from the containing type of the targetContainer:
  837. /// class Outer[A] {
  838. /// class Mid[T] {
  839. /// class Inner {
  840. /// T f;
  841. /// }
  842. /// }
  843. /// }
  844. /// Consider Outer[char].Mid[int].Inner.f. It is a specialized field, whose ContainingGenericTypeInstance = Outer[char].Mid[int]
  845. /// and whose partiallySpecializedVersion is another specialized field, which we call SF1.
  846. ///
  847. /// SF1's ContainingGenericTypeInstance is Outer[char]; its ContainingTypeDefinition is Outer[char].Mid.Inner. Its type should be
  848. /// a (specialized) copy of T defined by Outer[char].Mid, which is a specialized nested type definition. Note that the targetContainer
  849. /// for SF1 is Outer[char].Mid.Inner. To look for specialized version of T, we need to go to the parent of the targetContainer.
  850. /// </remarks>
  851. /// <param name="genericTypeParameter">A reference to a generic type parameter that occurs inside orginal container.</param>
  852. /// <param name="targetContainer">A specialized nested type definition whose or whose parent's (specialized) type parameters
  853. /// are used to replace <paramref name="genericTypeParameter"/>. </param>
  854. public static ITypeReference DeepCopyTypeReference(IGenericTypeParameterReference genericTypeParameter, SpecializedNestedTypeDefinition targetContainer) {
  855. var nestedTypeDefinition = targetContainer;
  856. while (nestedTypeDefinition != null) {
  857. if (genericTypeParameter.DefiningType.InternedKey == nestedTypeDefinition.partiallySpecializedVersion.InternedKey) {
  858. int i = 0;
  859. var genericParameters = nestedTypeDefinition.GenericParameters.GetEnumerator();
  860. while (genericParameters.MoveNext()) {
  861. if (i++ == genericTypeParameter.Index) {
  862. return genericParameters.Current;
  863. }
  864. }
  865. }
  866. nestedTypeDefinition = nestedTypeDefinition.ContainingTypeDefinition as SpecializedNestedTypeDefinition;
  867. }
  868. return genericTypeParameter;
  869. }
  870. internal static ITypeReference SpecializeTypeReference(IGenericTypeParameterReference genericTypeParameter, ITypeReference targetContainer) {
  871. var index = genericTypeParameter.Index;
  872. while (true) {
  873. var specializedNestedType = targetContainer as ISpecializedNestedTypeReference;
  874. if (specializedNestedType != null) { targetContainer = specializedNestedType.ContainingType; continue; }
  875. var genericTypeInstance = targetContainer as IGenericTypeInstanceReference;
  876. if (genericTypeInstance == null) return genericTypeParameter;
  877. var genericType = genericTypeInstance.GenericType;
  878. specializedNestedType = genericType as ISpecializedNestedTypeReference;
  879. if (specializedNestedType != null) genericType = specializedNestedType.UnspecializedVersion;
  880. if (!TypeHelper.TypesAreEquivalent(genericType, genericTypeParameter.DefiningType)) {
  881. targetContainer = genericTypeInstance.GenericType; continue;
  882. }
  883. var i = 0;
  884. foreach (var typeRef in genericTypeInstance.GenericArguments) if (index == i++) return typeRef;
  885. Contract.Assume(false); //The type parameter claims it is defined by genericTypeInstance.GenericType, so we should never get here legitimately.
  886. return Dummy.TypeReference;
  887. }
  888. }
  889. internal static ITypeReference SpecializeTypeReference(IGenericTypeParameterReference genericTypeParameter, IMethodReference targetContainer) {
  890. return SpecializeTypeReference(genericTypeParameter, targetContainer.ContainingType);
  891. }
  892. internal static ITypeReference SpecializeTypeReference(IGenericMethodParameterReference genericMethodParameter, IMethodReference targetContainer, IInternFactory internFactory) {
  893. return new SpecializedGenericMethodParameterReference(targetContainer, genericMethodParameter, internFactory);
  894. }
  895. /// <summary>
  896. /// If the given generic parameter is a generic parameter of the generic method of which the given method is an instance, then return the corresponding type argument that
  897. /// was used to create the method instance.
  898. /// </summary>
  899. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IGenericMethodParameterReference genericMethodParameter, IGenericMethodInstanceReference containingMethodInstance) {
  900. if (genericMethodParameter.DefiningMethod.InternedKey == containingMethodInstance.GenericMethod.InternedKey) {
  901. ushort i = 0;
  902. ushort n = genericMethodParameter.Index;
  903. IEnumerator<ITypeReference> genericArguments = containingMethodInstance.GenericArguments.GetEnumerator();
  904. while (genericArguments.MoveNext()) {
  905. if (i++ == n) return genericArguments.Current;
  906. }
  907. }
  908. return genericMethodParameter;
  909. }
  910. /// <summary>
  911. /// If the given generic parameter is a generic parameter of the generic type of which the given type is an instance, then return the corresponding type argument that
  912. /// was used to create the type instance.
  913. /// </summary>
  914. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IGenericTypeParameterReference genericTypeParameter, IGenericTypeInstanceReference containingTypeInstance) {
  915. if (genericTypeParameter.DefiningType.InternedKey == containingTypeInstance.GenericType.InternedKey) {
  916. ushort i = 0;
  917. ushort n = genericTypeParameter.Index;
  918. IEnumerator<ITypeReference> genericArguments = containingTypeInstance.GenericArguments.GetEnumerator();
  919. while (genericArguments.MoveNext()) {
  920. if (i++ == n) return genericArguments.Current;
  921. }
  922. }
  923. return genericTypeParameter;
  924. }
  925. /// <summary>
  926. /// If the given genericMethodParameter is a generic method parameter of the unspecialized version of specializedMethodDefinition,
  927. /// then return the corresponding generic method parameter of specializedMethodDefinition.
  928. /// </summary>
  929. internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(IGenericMethodParameterReference genericMethodParameter, ISpecializedMethodReference specializedMethodReference) {
  930. var specializedMethodDefinition = specializedMethodReference as SpecializedMethodDefinition;
  931. if (specializedMethodDefinition != null && genericMethodParameter.DefiningMethod.InternedKey == specializedMethodDefinition.PartiallySpecializedVersion.InternedKey) {
  932. ushort i = 0;
  933. ushort n = genericMethodParameter.Index;
  934. IEnumerator<IGenericMethodParameter> genericParameters = specializedMethodDefinition.GenericParameters.GetEnumerator();
  935. while (genericParameters.MoveNext()) {
  936. if (i++ == n) return genericParameters.Current;
  937. }
  938. }
  939. return genericMethodParameter;
  940. }
  941. /// <summary>
  942. /// If the given genericTypeParameter is a generic type parameter
  943. /// of a containing specialized nested type definition of the specializedMethodReference, then return the specialized version
  944. /// of the type parameter.
  945. /// </summary>
  946. internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(IGenericTypeParameterReference genericTypeParameter, ISpecializedMethodReference specializedMethodReference) {
  947. var specializedNestedType = specializedMethodReference.ContainingType as SpecializedNestedTypeDefinition;
  948. if (specializedNestedType != null) return DeepCopyTypeReference(genericTypeParameter, specializedNestedType);
  949. return genericTypeParameter;
  950. }
  951. }
  952. public class ManagedPointerType : SystemDefinedStructuralType, IManagedPointerType {
  953. protected ManagedPointerType(ITypeReference targetType, IInternFactory internFactory)
  954. : base(internFactory) {
  955. this.targetType = targetType;
  956. }
  957. /// <summary>
  958. /// Calls visitor.Visit(IManagedPointerTypeReference)
  959. /// </summary>
  960. public override void Dispatch(IMetadataVisitor visitor) {
  961. visitor.Visit(this);
  962. }
  963. /// <summary>
  964. /// Calls visitor.Visit(IManagedPointerTypeReference)
  965. /// </summary>
  966. public override void DispatchAsReference(IMetadataVisitor visitor) {
  967. visitor.Visit(this);
  968. }
  969. public static ManagedPointerType GetManagedPointerType(ITypeReference targetType, IInternFactory internFactory) {
  970. Contract.Ensures(Contract.Result<ManagedPointerType>() != null);
  971. ManagedPointerType result = new ManagedPointerType(targetType, internFactory);
  972. return result;
  973. }
  974. public override IPlatformType PlatformType {
  975. get { return this.TargetType.PlatformType; }
  976. }
  977. /// <summary>
  978. /// Returns a deep copy of a managed pointer type reference. In the copy, every reference to a partially specialized type parameter defined by
  979. /// the partially specialized version of targetContainer or of one of targetContainer's parents (if the parent is a SpecializedNestedTypeDefinition
  980. /// and generic) will be replaced with the specialized type parameter, defined by targetContainer or its parents.
  981. /// </summary>
  982. /// <param name="pointer">An array type reference to be deep copied. </param>
  983. /// <param name="targetContainer">A specialized nested type definition whose or whose parents' (specialized) type parameters will
  984. /// replace the occurrences of matching type parameters in <paramref name="pointer"/>.</param>
  985. /// /// <param name="internFactory">An intern factory. </param>
  986. internal static ITypeReference DeepCopyTypeReference(IManagedPointerTypeReference pointer, SpecializedNestedTypeDefinition targetContainer, IInternFactory internFactory) {
  987. ITypeReference targetType = pointer.TargetType;
  988. ITypeReference specializedtargetType = TypeDefinition.DeepCopyTypeReference(targetType, targetContainer, internFactory);
  989. if (targetType == specializedtargetType) return pointer;
  990. return GetManagedPointerType(specializedtargetType, internFactory);
  991. }
  992. internal static ITypeReference SpecializeTypeReference(IManagedPointerTypeReference pointer, ITypeReference targetContainer, IInternFactory internFactory) {
  993. ITypeReference targetType = pointer.TargetType;
  994. ITypeReference specializedtargetType = TypeHelper.SpecializeTypeReference(targetType, targetContainer, internFactory);
  995. if (targetType == specializedtargetType) return pointer;
  996. return GetManagedPointerType(specializedtargetType, internFactory);
  997. }
  998. internal static ITypeReference SpecializeTypeReference(IManagedPointerTypeReference pointer, IMethodReference targetContainer, IInternFactory internFactory) {
  999. ITypeReference targetType = pointer.TargetType;
  1000. ITypeReference specializedtargetType = TypeHelper.SpecializeTypeReference(targetType, targetContainer, internFactory);
  1001. if (targetType == specializedtargetType) return pointer;
  1002. return GetManagedPointerType(specializedtargetType, internFactory);
  1003. }
  1004. /// <summary>
  1005. /// If the given managed pointer has a target type that involves a type parameter from the generic method from which the given method was instantiated,
  1006. /// then return a new pointer using a target type that has been specialized with the type arguments of the given generic method instance.
  1007. /// </summary>
  1008. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IManagedPointerTypeReference pointer, IGenericMethodInstanceReference containingMethodInstance, IInternFactory internFactory) {
  1009. ITypeReference targetType = pointer.TargetType;
  1010. ITypeReference specializedtargetType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(targetType, containingMethodInstance, internFactory);
  1011. if (targetType == specializedtargetType) return pointer;
  1012. return GetManagedPointerType(specializedtargetType, internFactory);
  1013. }
  1014. /// <summary>
  1015. /// If the given managed pointer has a target type that involves a type parameter from the generic type from which the given type was instantiated,
  1016. /// then return a new pointer using a target type that has been specialized with the type arguments of the given generic type instance.
  1017. /// </summary>
  1018. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IManagedPointerTypeReference pointer, IGenericTypeInstanceReference containingTypeInstance, IInternFactory internFactory) {
  1019. ITypeReference targetType = pointer.TargetType;
  1020. ITypeReference specializedtargetType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(targetType, containingTypeInstance, internFactory);
  1021. if (targetType == specializedtargetType) return pointer;
  1022. return GetManagedPointerType(specializedtargetType, internFactory);
  1023. }
  1024. /// <summary>
  1025. /// If the given managed pointer has a target type that involves a method type parameter of the partially specialized version of specializedMethodReference,
  1026. /// then return a new pointer using a target type that is the corresponding method type parameter from specializedMethodReference.
  1027. /// </summary>
  1028. internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(IManagedPointerTypeReference pointer, ISpecializedMethodReference specializedMethodReference, IInternFactory internFactory) {
  1029. ITypeReference targetType = pointer.TargetType;
  1030. ITypeReference specializedtargetType = TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(targetType, specializedMethodReference, internFactory);
  1031. if (targetType == specializedtargetType) return pointer;
  1032. return GetManagedPointerType(specializedtargetType, internFactory);
  1033. }
  1034. //^ [Confined]
  1035. public override string ToString() {
  1036. return this.TargetType.ToString() + "&";
  1037. }
  1038. public ITypeReference TargetType {
  1039. get { return this.targetType; }
  1040. }
  1041. readonly ITypeReference targetType;
  1042. public override PrimitiveTypeCode TypeCode {
  1043. get { return PrimitiveTypeCode.Reference; }
  1044. }
  1045. }
  1046. public class Matrix : ArrayType {
  1047. protected Matrix(ITypeReference elementType, uint rank, IEnumerable<int>/*?*/ lowerBounds, IEnumerable<ulong>/*?*/ sizes, IInternFactory internFactory)
  1048. : base(elementType, internFactory) {
  1049. this.rank = rank;
  1050. this.lowerBounds = lowerBounds;
  1051. this.sizes = sizes;
  1052. }
  1053. public static Matrix GetMatrix(ITypeReference elementType, uint rank, IInternFactory internFactory) {
  1054. Contract.Ensures(Contract.Result<Matrix>() != null);
  1055. return new Matrix(elementType, rank, null, null, internFactory);
  1056. }
  1057. public static Matrix GetMatrix(ITypeReference elementType, uint rank, IEnumerable<int>/*?*/ lowerBounds, IEnumerable<ulong>/*?*/ sizes, IInternFactory internFactory) {
  1058. Contract.Ensures(Contract.Result<Matrix>() != null);
  1059. return new Matrix(elementType, rank, lowerBounds, sizes, internFactory);
  1060. }
  1061. public override bool IsVector {
  1062. get { return false; }
  1063. }
  1064. public override IEnumerable<int> LowerBounds {
  1065. get {
  1066. if (this.lowerBounds == null) return base.LowerBounds;
  1067. return this.lowerBounds;
  1068. }
  1069. }
  1070. IEnumerable<int>/*?*/ lowerBounds;
  1071. public override uint Rank {
  1072. get { return this.rank; }
  1073. }
  1074. readonly uint rank;
  1075. public override IEnumerable<ulong> Sizes {
  1076. get {
  1077. if (this.sizes == null) return base.Sizes;
  1078. return this.sizes;
  1079. }
  1080. }
  1081. IEnumerable<ulong>/*?*/ sizes;
  1082. /// <summary>
  1083. /// Returns a deep copy of an array type (a vector). In the copy, every reference to a partially specialized type parameter defined by
  1084. /// the partially specialized version of targetContainer or of one of targetContainer's parents (if the parent is a SpecializedNestedTypeDefinition
  1085. /// and generic) will be replaced with the specialized type parameter, defined by targetContainer or its parents.
  1086. /// </summary>
  1087. /// <param name="array">An array type reference to be deep copied. </param>
  1088. /// <param name="targetContainer">A specialized nested type definition whose or whose parents' (specialized) type parameters will
  1089. /// replace the occurrences of matching type parameters in <paramref name="array"/>.</param>
  1090. /// /// <param name="internFactory">An intern factory. </param>
  1091. internal static ITypeReference DeepCopyTypeReference(IArrayTypeReference array, SpecializedNestedTypeDefinition targetContainer, IInternFactory internFactory)
  1092. //^ requires !array.IsVector;
  1093. {
  1094. ITypeReference elementType = array.ElementType;
  1095. ITypeReference specializedElementType = TypeDefinition.DeepCopyTypeReference(elementType, targetContainer, internFactory);
  1096. if (elementType == specializedElementType) return array;
  1097. return GetMatrix(specializedElementType, array.Rank, array.LowerBounds, array.Sizes, internFactory);
  1098. }
  1099. /// <summary>
  1100. /// Returns a deep copy of an array type (a vector). In the copy, every reference to a partially specialized type parameter defined by
  1101. /// the partially specialized version of targetContainer or of one of targetContainer's parents (if the parent is a SpecializedNestedTypeDefinition
  1102. /// and generic) will be replaced with the specialized type parameter, defined by targetContainer or its parents.
  1103. /// </summary>
  1104. /// <param name="array">An array type reference to be deep copied. </param>
  1105. /// <param name="targetContainer">A specialized nested type definition whose or whose parents' (specialized) type parameters will
  1106. /// replace the occurrences of matching type parameters in <paramref name="array"/>.</param>
  1107. /// /// <param name="internFactory">An intern factory. </param>
  1108. internal static ITypeReference SpecializeTypeReference(IArrayTypeReference array, ITypeReference targetContainer, IInternFactory internFactory)
  1109. //^ requires !array.IsVector;
  1110. {
  1111. ITypeReference elementType = array.ElementType;
  1112. ITypeReference specializedElementType = TypeHelper.SpecializeTypeReference(elementType, targetContainer, internFactory);
  1113. if (elementType == specializedElementType) return array;
  1114. return GetMatrix(specializedElementType, array.Rank, array.LowerBounds, array.Sizes, internFactory);
  1115. }
  1116. internal static ITypeReference SpecializeTypeReference(IArrayTypeReference array, IMethodReference targetContainer, IInternFactory internFactory)
  1117. //^ requires !array.IsVector;
  1118. {
  1119. ITypeReference elementType = array.ElementType;
  1120. ITypeReference specializedElementType = TypeHelper.SpecializeTypeReference(elementType, targetContainer, internFactory);
  1121. if (elementType == specializedElementType) return array;
  1122. return GetMatrix(specializedElementType, array.Rank, array.LowerBounds, array.Sizes, internFactory);
  1123. }
  1124. /// <summary>
  1125. /// If the given matrix has an element type that involves a type parameter from the generic method from which the given method was instantiated,
  1126. /// then return a new matrix using an element type that has been specialized with the type arguments of the given generic method instance.
  1127. /// </summary>
  1128. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IArrayTypeReference array, IGenericMethodInstanceReference containingMethodInstance, IInternFactory internFactory)
  1129. //^ requires !array.IsVector;
  1130. {
  1131. ITypeReference elementType = array.ElementType;
  1132. ITypeReference specializedElementType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(elementType, containingMethodInstance, internFactory);
  1133. if (elementType == specializedElementType) return array;
  1134. return GetMatrix(specializedElementType, array.Rank, array.LowerBounds, array.Sizes, internFactory);
  1135. }
  1136. /// <summary>
  1137. /// If the given matrix has an element type that involves a type parameter from the generic type from which the given type was instantiated,
  1138. /// then return a new matrix using an element type that has been specialized with the type arguments of the given generic type instance.
  1139. /// </summary>
  1140. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IArrayTypeReference array, IGenericTypeInstanceReference containingTypeInstance, IInternFactory internFactory)
  1141. //^ requires !array.IsVector;
  1142. {
  1143. ITypeReference elementType = array.ElementType;
  1144. ITypeReference specializedElementType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(elementType, containingTypeInstance, internFactory);
  1145. if (elementType == specializedElementType) return array;
  1146. return GetMatrix(specializedElementType, array.Rank, array.LowerBounds, array.Sizes, internFactory);
  1147. }
  1148. /// <summary>
  1149. /// If the given matrix has an element type that involves a method type parameter from the partially specialized version of specializedMethodReference,
  1150. /// then return a new matrix using an element type that has been specialized with the corresponding method type parameter from specializedMethodReference.
  1151. /// </summary>
  1152. internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(IArrayTypeReference array, ISpecializedMethodReference specializedMethodReference, IInternFactory internFactory)
  1153. //^ requires !array.IsVector;
  1154. {
  1155. ITypeReference elementType = array.ElementType;
  1156. ITypeReference specializedElementType = TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(elementType, specializedMethodReference, internFactory);
  1157. if (elementType == specializedElementType) return array;
  1158. return GetMatrix(specializedElementType, array.Rank, array.LowerBounds, array.Sizes, internFactory);
  1159. }
  1160. }
  1161. public class PointerType : SystemDefinedStructuralType, IPointerType {
  1162. protected PointerType(ITypeReference targetType, IInternFactory internFactory)
  1163. : base(internFactory) {
  1164. this.targetType = targetType;
  1165. }
  1166. /// <summary>
  1167. /// Calls visitor.Visit(IPointerTypeReference)
  1168. /// </summary>
  1169. public override void Dispatch(IMetadataVisitor visitor) {
  1170. visitor.Visit(this);
  1171. }
  1172. /// <summary>
  1173. /// Calls visitor.Visit(IPointerTypeReference)
  1174. /// </summary>
  1175. public override void DispatchAsReference(IMetadataVisitor visitor) {
  1176. visitor.Visit(this);
  1177. }
  1178. public static PointerType GetPointerType(ITypeReference targetType, IInternFactory internFactory) {
  1179. Contract.Ensures(Contract.Result<PointerType>() != null);
  1180. return new PointerType(targetType, internFactory);
  1181. }
  1182. public override IPlatformType PlatformType {
  1183. get { return this.TargetType.PlatformType; }
  1184. }
  1185. /// <summary>
  1186. /// Returns a deep copy of a pointer type reference. In the copy, every reference to a partially specialized type parameter defined by
  1187. /// the partially specialized version of targetContainer or of one of targetContainer's parents (if the parent is a SpecializedNestedTypeDefinition
  1188. /// and generic) will be replaced with the specialized type parameter, defined by targetContainer or its parents.
  1189. /// </summary>
  1190. /// <param name="pointer">An array type reference to be deep copied. </param>
  1191. /// <param name="targetContainer">A specialized nested type definition whose or whose parents' (specialized) type parameters will
  1192. /// replace the occurrences of matching type parameters in <paramref name="pointer"/>.</param>
  1193. /// /// <param name="internFactory">An intern factory. </param>
  1194. internal static ITypeReference DeepCopyTypeReference(IPointerTypeReference pointer, SpecializedNestedTypeDefinition targetContainer, IInternFactory internFactory) {
  1195. ITypeReference targetType = pointer.TargetType;
  1196. ITypeReference specializedtargetType = TypeDefinition.DeepCopyTypeReference(targetType, targetContainer, internFactory);
  1197. if (targetType == specializedtargetType) return pointer;
  1198. return GetPointerType(specializedtargetType, internFactory);
  1199. }
  1200. internal static ITypeReference SpecializeTypeReference(IPointerTypeReference pointer, ITypeReference targetContainer, IInternFactory internFactory) {
  1201. ITypeReference targetType = pointer.TargetType;
  1202. ITypeReference specializedtargetType = TypeHelper.SpecializeTypeReference(targetType, targetContainer, internFactory);
  1203. if (targetType == specializedtargetType) return pointer;
  1204. return GetPointerType(specializedtargetType, internFactory);
  1205. }
  1206. internal static ITypeReference SpecializeTypeReference(IPointerTypeReference pointer, IMethodReference targetContainer, IInternFactory internFactory) {
  1207. ITypeReference targetType = pointer.TargetType;
  1208. ITypeReference specializedtargetType = TypeHelper.SpecializeTypeReference(targetType, targetContainer, internFactory);
  1209. if (targetType == specializedtargetType) return pointer;
  1210. return GetPointerType(specializedtargetType, internFactory);
  1211. }
  1212. /// <summary>
  1213. /// If the given pointer has a target type that involves a type parameter from the generic method from which the given method was instantiated,
  1214. /// then return a new pointer using a target type that has been specialized with the type arguments of the given generic method instance.
  1215. /// </summary>
  1216. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IPointerTypeReference pointer, IGenericMethodInstanceReference containingMethodInstance, IInternFactory internFactory) {
  1217. ITypeReference targetType = pointer.TargetType;
  1218. ITypeReference specializedtargetType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(targetType, containingMethodInstance, internFactory);
  1219. if (targetType == specializedtargetType) return pointer;
  1220. return GetPointerType(specializedtargetType, internFactory);
  1221. }
  1222. /// <summary>
  1223. /// If the given pointer has a target type that involves a type parameter from the generic type from which the given type was instantiated,
  1224. /// then return a new pointer using a target type that has been specialized with the type arguments of the given generic type instance.
  1225. /// </summary>
  1226. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IPointerTypeReference pointer, IGenericTypeInstanceReference containingTypeInstance, IInternFactory internFactory) {
  1227. ITypeReference targetType = pointer.TargetType;
  1228. ITypeReference specializedtargetType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(targetType, containingTypeInstance, internFactory);
  1229. if (targetType == specializedtargetType) return pointer;
  1230. return GetPointerType(specializedtargetType, internFactory);
  1231. }
  1232. /// <summary>
  1233. /// If the given pointer has a target type that involves a method type parameter of the partially specialized version of specializedMethodReference,
  1234. /// then return a new pointer using a target type that is the corresponding method type parameter from specializedMethodReference.
  1235. /// </summary>
  1236. internal static ITypeReference DeepCopyTypeReferenceReplacingGenericMethodParameter(IPointerTypeReference pointer, ISpecializedMethodReference specializedMethodReference, IInternFactory internFactory) {
  1237. ITypeReference targetType = pointer.TargetType;
  1238. ITypeReference specializedtargetType = TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(targetType, specializedMethodReference, internFactory);
  1239. if (targetType == specializedtargetType) return pointer;
  1240. return GetPointerType(specializedtargetType, internFactory);
  1241. }
  1242. public ITypeReference TargetType {
  1243. get { return this.targetType; }
  1244. }
  1245. readonly ITypeReference targetType;
  1246. //^ [Confined]
  1247. public override string ToString() {
  1248. return this.TargetType.ResolvedType.ToString() + "*";
  1249. }
  1250. public override PrimitiveTypeCode TypeCode {
  1251. get { return PrimitiveTypeCode.Pointer; }
  1252. }
  1253. }
  1254. public class ModifiedPointerType : PointerType, IModifiedTypeReference {
  1255. private ModifiedPointerType(ITypeReference targetType, IEnumerable<ICustomModifier> customModifiers, IInternFactory internFactory)
  1256. : base(targetType, internFactory) {
  1257. this.customModifiers = customModifiers;
  1258. }
  1259. public override bool IsModified {
  1260. get { return true; }
  1261. }
  1262. /// <summary>
  1263. /// Returns a deep copy of a modified pointer type. In the copy, every reference to a partially specialized type parameter defined by
  1264. /// the partially specialized version of targetContainer or of one of targetContainer's parents (if the parent is a SpecializedNestedTypeDefinition
  1265. /// and generic) will be replaced with the specialized type parameter, defined by targetContainer or its parents.
  1266. /// </summary>
  1267. /// <param name="modifiedPointer">An array type reference to be deep copied. </param>
  1268. /// <param name="targetContainer">A specialized nested type definition whose or whose parents' (specialized) type parameters will
  1269. /// replace the occurrences of matching type parameters in <paramref name="modifiedPointer"/>.</param>
  1270. /// /// <param name="internFactory">An intern factory. </param>
  1271. internal static ITypeReference DeepCopyTypeReference(ModifiedPointerType modifiedPointer, SpecializedNestedTypeDefinition targetContainer, IInternFactory internFactory) {
  1272. var copiedTargetType = TypeDefinition.DeepCopyTypeReference(modifiedPointer.TargetType, targetContainer, internFactory);
  1273. List<ICustomModifier>/*?*/ copiedModifiers = null;
  1274. int i = 0;
  1275. foreach (var modifier in modifiedPointer.CustomModifiers) {
  1276. var copiedModifier = CustomModifier.CopyModifierToNewContainer(modifier, targetContainer, internFactory);
  1277. if (modifier != copiedModifier) {
  1278. if (copiedModifiers == null) copiedModifiers = new List<ICustomModifier>(modifiedPointer.CustomModifiers);
  1279. copiedModifiers[i] = copiedModifier;
  1280. }
  1281. i++;
  1282. }
  1283. if (copiedModifiers == null) {
  1284. if (copiedTargetType == modifiedPointer.TargetType) return modifiedPointer;
  1285. return GetModifiedPointerType(copiedTargetType, modifiedPointer.CustomModifiers, internFactory);
  1286. }
  1287. return GetModifiedPointerType(copiedTargetType, copiedModifiers, internFactory);
  1288. }
  1289. internal static ITypeReference SpecializeTypeReference(ModifiedPointerType modifiedPointer, ITypeReference targetContainer, IInternFactory internFactory) {
  1290. var copiedTargetType = TypeHelper.SpecializeTypeReference(modifiedPointer.TargetType, targetContainer, internFactory);
  1291. List<ICustomModifier>/*?*/ copiedModifiers = null;
  1292. int i = 0;
  1293. foreach (var modifier in modifiedPointer.CustomModifiers) {
  1294. var copiedModifier = CustomModifier.Specialize(modifier, targetContainer, internFactory);
  1295. if (modifier != copiedModifier) {
  1296. if (copiedModifiers == null) copiedModifiers = new List<ICustomModifier>(modifiedPointer.CustomModifiers);
  1297. copiedModifiers[i] = copiedModifier;
  1298. }
  1299. i++;
  1300. }
  1301. if (copiedModifiers == null) {
  1302. if (copiedTargetType == modifiedPointer.TargetType) return modifiedPointer;
  1303. return GetModifiedPointerType(copiedTargetType, modifiedPointer.CustomModifiers, internFactory);
  1304. }
  1305. return GetModifiedPointerType(copiedTargetType, copiedModifiers, internFactory);
  1306. }
  1307. internal static ITypeReference SpecializeTypeReference(ModifiedPointerType modifiedPointer, IMethodReference targetContainer, IInternFactory internFactory) {
  1308. var copiedTargetType = TypeHelper.SpecializeTypeReference(modifiedPointer.TargetType, targetContainer, internFactory);
  1309. List<ICustomModifier>/*?*/ copiedModifiers = null;
  1310. int i = 0;
  1311. foreach (var modifier in modifiedPointer.CustomModifiers) {
  1312. var copiedModifier = CustomModifier.Specialize(modifier, targetContainer, internFactory);
  1313. if (modifier != copiedModifier) {
  1314. if (copiedModifiers == null) copiedModifiers = new List<ICustomModifier>(modifiedPointer.CustomModifiers);
  1315. copiedModifiers[i] = copiedModifier;
  1316. }
  1317. i++;
  1318. }
  1319. if (copiedModifiers == null) {
  1320. if (copiedTargetType == modifiedPointer.TargetType) return modifiedPointer;
  1321. return GetModifiedPointerType(copiedTargetType, modifiedPointer.CustomModifiers, internFactory);
  1322. }
  1323. return GetModifiedPointerType(copiedTargetType, copiedModifiers, internFactory);
  1324. }
  1325. /// <summary>
  1326. /// If the given pointer has a target type that involves a type parameter from the generic method from which the given method was instantiated,
  1327. /// then return a new pointer using a target type that has been specialized with the type arguments of the given generic method instance.
  1328. /// </summary>
  1329. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(ModifiedPointerType modifiedPointer, IGenericMethodInstanceReference containingMethodInstance, IInternFactory internFactory) {
  1330. var copiedTargetType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(modifiedPointer.TargetType, containingMethodInstance, internFactory);
  1331. List<ICustomModifier>/*?*/ copiedModifiers = null;
  1332. int i = 0;
  1333. foreach (var modifier in modifiedPointer.CustomModifiers) {
  1334. var copiedModifier = CustomModifier.SpecializeIfConstructedFromApplicableTypeParameter(modifier, containingMethodInstance, internFactory);
  1335. if (modifier != copiedModifier) {
  1336. if (copiedModifiers == null) copiedModifiers = new List<ICustomModifier>(modifiedPointer.CustomModifiers);
  1337. copiedModifiers[i] = copiedModifier;
  1338. }
  1339. i++;
  1340. }
  1341. if (copiedModifiers == null) {
  1342. if (copiedTargetType == modifiedPointer.TargetType) return modifiedPointer;
  1343. return GetModifiedPointerType(copiedTargetType, modifiedPointer.CustomModifiers, internFactory);
  1344. }
  1345. return GetModifiedPointerType(copiedTargetType, copiedModifiers, internFactory);
  1346. }
  1347. /// <summary>
  1348. /// If the given modified pointer has a target type that involves a type parameter from the generic type from which the given type was instantiated,
  1349. /// then return a new pointer using a target type that has been specialized with the type arguments of the given generic type instance.
  1350. /// </summary>
  1351. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(ModifiedPointerType modifiedPointer, IGenericTypeInstanceReference containingTypeInstance, IInternFactory internFactory) {
  1352. var copiedTargetType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(modifiedPointer.TargetType, containingTypeInstance, internFactory);
  1353. List<ICustomModifier>/*?*/ copiedModifiers = null;
  1354. int i = 0;
  1355. foreach (var modifier in modifiedPointer.CustomModifiers) {
  1356. var copiedModifier = CustomModifier.SpecializeIfConstructedFromApplicableTypeParameter(modifier, containingTypeInstance, internFactory);
  1357. if (modifier != copiedModifier) {
  1358. if (copiedModifiers == null) copiedModifiers = new List<ICustomModifier>(modifiedPointer.CustomModifiers);
  1359. copiedModifiers[i] = copiedModifier;
  1360. }
  1361. i++;
  1362. }
  1363. if (copiedModifiers == null) {
  1364. if (copiedTargetType == modifiedPointer.TargetType) return modifiedPointer;
  1365. return GetModifiedPointerType(copiedTargetType, modifiedPointer.CustomModifiers, internFactory);
  1366. }
  1367. return GetModifiedPointerType(copiedTargetType, copiedModifiers, internFactory);
  1368. }
  1369. /// <summary>
  1370. /// If the given modified pointer has a target type that involves a method type parameter of the partially specialized version of specializedMethodReference,
  1371. /// then return a new pointer using a target type that is the corresponding method type parameter from specializedMethodReference.
  1372. /// </summary>
  1373. internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(ModifiedPointerType modifiedPointer, ISpecializedMethodReference specializedMethodReference, IInternFactory internFactory) {
  1374. var copiedTargetType = TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(modifiedPointer.TargetType, specializedMethodReference, internFactory);
  1375. List<ICustomModifier>/*?*/ copiedModifiers = null;
  1376. int i = 0;
  1377. foreach (var modifier in modifiedPointer.CustomModifiers) {
  1378. var copiedModifier = CustomModifier.SpecializeIfConstructedFromApplicableMethodTypeParameter(modifier, specializedMethodReference, internFactory);
  1379. if (modifier != copiedModifier) {
  1380. if (copiedModifiers == null) copiedModifiers = new List<ICustomModifier>(modifiedPointer.CustomModifiers);
  1381. copiedModifiers[i] = copiedModifier;
  1382. }
  1383. i++;
  1384. }
  1385. if (copiedModifiers == null) {
  1386. if (copiedTargetType == modifiedPointer.TargetType) return modifiedPointer;
  1387. return GetModifiedPointerType(copiedTargetType, modifiedPointer.CustomModifiers, internFactory);
  1388. }
  1389. return GetModifiedPointerType(copiedTargetType, copiedModifiers, internFactory);
  1390. }
  1391. public static ModifiedPointerType GetModifiedPointerType(ITypeReference targetType, IEnumerable<ICustomModifier> customModifiers, IInternFactory internFactory) {
  1392. return new ModifiedPointerType(targetType, customModifiers, internFactory);
  1393. }
  1394. public override IEnumerable<ICustomModifier> CustomModifiers {
  1395. get { return this.customModifiers; }
  1396. }
  1397. readonly IEnumerable<ICustomModifier> customModifiers;
  1398. public ITypeReference UnmodifiedType {
  1399. get { return this; }
  1400. }
  1401. }
  1402. public class ModifiedTypeReference : IModifiedTypeReference {
  1403. private ModifiedTypeReference(IInternFactory internFactory, ITypeReference unmodifiedType, IEnumerable<ICustomModifier> customModifiers) {
  1404. this.internFactory = internFactory;
  1405. this.unmodifiedType = unmodifiedType;
  1406. this.customModifiers = customModifiers;
  1407. }
  1408. IInternFactory internFactory;
  1409. public IEnumerable<ICustomModifier> CustomModifiers {
  1410. get { return this.customModifiers; }
  1411. }
  1412. IEnumerable<ICustomModifier> customModifiers;
  1413. public ITypeReference UnmodifiedType {
  1414. get { return this.unmodifiedType; }
  1415. }
  1416. readonly ITypeReference unmodifiedType;
  1417. public static ModifiedTypeReference GetModifiedTypeReference(ITypeReference unmodifiedType, IEnumerable<ICustomModifier> customModifiers, IInternFactory internFactory) {
  1418. return new ModifiedTypeReference(internFactory, unmodifiedType, customModifiers);
  1419. }
  1420. /// <summary>
  1421. /// Returns a deep copy of a modified type reference. In the copy, every reference to a partially specialized type parameter defined by
  1422. /// the partially specialized version of targetContainer or of one of targetContainer's parents (if the parent is a SpecializedNestedTypeDefinition
  1423. /// and generic) will be replaced with the specialized type parameter, defined by targetContainer or its parents.
  1424. /// </summary>
  1425. /// <param name="modifiedTypeReference">An array type reference to be deep copied. </param>
  1426. /// <param name="targetContainer">A specialized nested type definition whose or whose parents' (specialized) type parameters will
  1427. /// replace the occurrences of matching type parameters in <paramref name="modifiedTypeReference"/>.</param>
  1428. /// /// <param name="internFactory">An intern factory. </param>
  1429. internal static ITypeReference DeepCopyTypeReference(IModifiedTypeReference modifiedTypeReference, SpecializedNestedTypeDefinition targetContainer, IInternFactory internFactory) {
  1430. ITypeReference copiedUnmodifiedType = TypeDefinition.DeepCopyTypeReference(modifiedTypeReference.UnmodifiedType, targetContainer, internFactory);
  1431. List<ICustomModifier>/*?*/ copiedModifiers = null;
  1432. int i = 0;
  1433. foreach (var modifier in modifiedTypeReference.CustomModifiers) {
  1434. var copiedModifier = CustomModifier.CopyModifierToNewContainer(modifier, targetContainer, internFactory);
  1435. if (modifier != copiedModifier) {
  1436. if (copiedModifiers == null) copiedModifiers = new List<ICustomModifier>(modifiedTypeReference.CustomModifiers);
  1437. copiedModifiers[i] = copiedModifier;
  1438. }
  1439. i++;
  1440. }
  1441. if (copiedModifiers == null) {
  1442. if (copiedUnmodifiedType == modifiedTypeReference.UnmodifiedType) return modifiedTypeReference;
  1443. return GetModifiedTypeReference(copiedUnmodifiedType, modifiedTypeReference.CustomModifiers, internFactory);
  1444. }
  1445. return GetModifiedTypeReference(copiedUnmodifiedType, copiedModifiers, internFactory);
  1446. }
  1447. internal static ITypeReference SpecializeTypeReference(IModifiedTypeReference modifiedTypeReference, ITypeReference targetContainer, IInternFactory internFactory) {
  1448. ITypeReference copiedUnmodifiedType = TypeHelper.SpecializeTypeReference(modifiedTypeReference.UnmodifiedType, targetContainer, internFactory);
  1449. List<ICustomModifier>/*?*/ copiedModifiers = null;
  1450. int i = 0;
  1451. foreach (var modifier in modifiedTypeReference.CustomModifiers) {
  1452. var copiedModifier = CustomModifier.Specialize(modifier, targetContainer, internFactory);
  1453. if (modifier != copiedModifier) {
  1454. if (copiedModifiers == null) copiedModifiers = new List<ICustomModifier>(modifiedTypeReference.CustomModifiers);
  1455. copiedModifiers[i] = copiedModifier;
  1456. }
  1457. i++;
  1458. }
  1459. if (copiedModifiers == null) {
  1460. if (copiedUnmodifiedType == modifiedTypeReference.UnmodifiedType) return modifiedTypeReference;
  1461. return GetModifiedTypeReference(copiedUnmodifiedType, modifiedTypeReference.CustomModifiers, internFactory);
  1462. }
  1463. return GetModifiedTypeReference(copiedUnmodifiedType, copiedModifiers, internFactory);
  1464. }
  1465. internal static ITypeReference SpecializeTypeReference(IModifiedTypeReference modifiedTypeReference, IMethodReference targetContainer, IInternFactory internFactory) {
  1466. ITypeReference copiedUnmodifiedType = TypeHelper.SpecializeTypeReference(modifiedTypeReference.UnmodifiedType, targetContainer, internFactory);
  1467. List<ICustomModifier>/*?*/ copiedModifiers = null;
  1468. int i = 0;
  1469. foreach (var modifier in modifiedTypeReference.CustomModifiers) {
  1470. var copiedModifier = CustomModifier.Specialize(modifier, targetContainer, internFactory);
  1471. if (modifier != copiedModifier) {
  1472. if (copiedModifiers == null) copiedModifiers = new List<ICustomModifier>(modifiedTypeReference.CustomModifiers);
  1473. copiedModifiers[i] = copiedModifier;
  1474. }
  1475. i++;
  1476. }
  1477. if (copiedModifiers == null) {
  1478. if (copiedUnmodifiedType == modifiedTypeReference.UnmodifiedType) return modifiedTypeReference;
  1479. return GetModifiedTypeReference(copiedUnmodifiedType, modifiedTypeReference.CustomModifiers, internFactory);
  1480. }
  1481. return GetModifiedTypeReference(copiedUnmodifiedType, copiedModifiers, internFactory);
  1482. }
  1483. /// <summary>
  1484. /// If the given pointer has a target type that involves a type parameter from the generic method from which the given method was instantiated,
  1485. /// then return a new pointer using a target type that has been specialized with the type arguments of the given generic method instance.
  1486. /// </summary>
  1487. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IModifiedTypeReference modifiedTypeReference, IGenericMethodInstanceReference containingMethodInstance, IInternFactory internFactory) {
  1488. ITypeReference copiedUnmodifiedType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(modifiedTypeReference.UnmodifiedType, containingMethodInstance, internFactory);
  1489. List<ICustomModifier>/*?*/ copiedModifiers = null;
  1490. int i = 0;
  1491. foreach (var modifier in modifiedTypeReference.CustomModifiers) {
  1492. var copiedModifier = CustomModifier.SpecializeIfConstructedFromApplicableTypeParameter(modifier, containingMethodInstance, internFactory);
  1493. if (modifier != copiedModifier) {
  1494. if (copiedModifiers == null) copiedModifiers = new List<ICustomModifier>(modifiedTypeReference.CustomModifiers);
  1495. copiedModifiers[i] = copiedModifier;
  1496. }
  1497. i++;
  1498. }
  1499. if (copiedModifiers == null) {
  1500. if (copiedUnmodifiedType == modifiedTypeReference.UnmodifiedType) return modifiedTypeReference;
  1501. return GetModifiedTypeReference(copiedUnmodifiedType, modifiedTypeReference.CustomModifiers, internFactory);
  1502. }
  1503. return GetModifiedTypeReference(copiedUnmodifiedType, copiedModifiers, internFactory);
  1504. }
  1505. /// <summary>
  1506. /// If the given modified pointer has a target type that involves a type parameter from the generic type from which the given type was instantiated,
  1507. /// then return a new pointer using a target type that has been specialized with the type arguments of the given generic type instance.
  1508. /// </summary>
  1509. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IModifiedTypeReference modifiedTypeReference, IGenericTypeInstanceReference containingTypeInstance, IInternFactory internFactory) {
  1510. ITypeReference copiedUnmodifiedType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(modifiedTypeReference.UnmodifiedType, containingTypeInstance, internFactory);
  1511. List<ICustomModifier>/*?*/ copiedModifiers = null;
  1512. int i = 0;
  1513. foreach (var modifier in modifiedTypeReference.CustomModifiers) {
  1514. var copiedModifier = CustomModifier.SpecializeIfConstructedFromApplicableTypeParameter(modifier, containingTypeInstance, internFactory);
  1515. if (modifier != copiedModifier) {
  1516. if (copiedModifiers == null) copiedModifiers = new List<ICustomModifier>(modifiedTypeReference.CustomModifiers);
  1517. copiedModifiers[i] = copiedModifier;
  1518. }
  1519. i++;
  1520. }
  1521. if (copiedModifiers == null) {
  1522. if (copiedUnmodifiedType == modifiedTypeReference.UnmodifiedType) return modifiedTypeReference;
  1523. return GetModifiedTypeReference(copiedUnmodifiedType, modifiedTypeReference.CustomModifiers, internFactory);
  1524. }
  1525. return GetModifiedTypeReference(copiedUnmodifiedType, copiedModifiers, internFactory);
  1526. }
  1527. /// <summary>
  1528. /// If the given modified type reference has a target type that involves a method type parameter of the partially specialized version of specializedMethodReference,
  1529. /// then return a new type reference using a target type that is the corresponding method type parameter from specializedMethodReference.
  1530. /// </summary>
  1531. internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(IModifiedTypeReference modifiedTypeReference, ISpecializedMethodReference specializedMethodReference, IInternFactory internFactory) {
  1532. ITypeReference copiedUnmodifiedType = TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(modifiedTypeReference.UnmodifiedType, specializedMethodReference, internFactory);
  1533. List<ICustomModifier>/*?*/ copiedModifiers = null;
  1534. int i = 0;
  1535. foreach (var modifier in modifiedTypeReference.CustomModifiers) {
  1536. var copiedModifier = CustomModifier.SpecializeIfConstructedFromApplicableMethodTypeParameter(modifier, specializedMethodReference, internFactory);
  1537. if (modifier != copiedModifier) {
  1538. if (copiedModifiers == null) copiedModifiers = new List<ICustomModifier>(modifiedTypeReference.CustomModifiers);
  1539. copiedModifiers[i] = copiedModifier;
  1540. }
  1541. i++;
  1542. }
  1543. if (copiedModifiers == null) {
  1544. if (copiedUnmodifiedType == modifiedTypeReference.UnmodifiedType) return modifiedTypeReference;
  1545. return GetModifiedTypeReference(copiedUnmodifiedType, modifiedTypeReference.CustomModifiers, internFactory);
  1546. }
  1547. return GetModifiedTypeReference(copiedUnmodifiedType, copiedModifiers, internFactory);
  1548. }
  1549. #region ITypeReference Members
  1550. public IAliasForType AliasForType {
  1551. get { return Dummy.AliasForType; }
  1552. }
  1553. public uint InternedKey {
  1554. get {
  1555. if (this.internedKey == 0) {
  1556. this.internedKey = this.internFactory.GetTypeReferenceInternedKey(this);
  1557. }
  1558. return this.internedKey;
  1559. }
  1560. }
  1561. uint internedKey;
  1562. public bool IsAlias {
  1563. get { return false; }
  1564. }
  1565. public bool IsEnum {
  1566. get { return this.UnmodifiedType.IsEnum; }
  1567. }
  1568. public bool IsValueType {
  1569. get { return this.UnmodifiedType.IsValueType; }
  1570. }
  1571. public IPlatformType PlatformType {
  1572. get { return this.UnmodifiedType.PlatformType; }
  1573. }
  1574. public ITypeDefinition ResolvedType {
  1575. get { return this.UnmodifiedType.ResolvedType; }
  1576. }
  1577. public PrimitiveTypeCode TypeCode {
  1578. get { return this.UnmodifiedType.TypeCode; }
  1579. }
  1580. #endregion
  1581. #region IReference Members
  1582. public IEnumerable<ICustomAttribute> Attributes {
  1583. get { return Enumerable<ICustomAttribute>.Empty; }
  1584. }
  1585. /// <summary>
  1586. /// Calls visitor.Visit(IModifiedTypeReference).
  1587. /// </summary>
  1588. public void Dispatch(IMetadataVisitor visitor) {
  1589. visitor.Visit(this);
  1590. }
  1591. /// <summary>
  1592. /// Calls visitor.Visit(IModifiedTypeReference).
  1593. /// </summary>
  1594. public void DispatchAsReference(IMetadataVisitor visitor) {
  1595. visitor.Visit(this);
  1596. }
  1597. public IEnumerable<ILocation> Locations {
  1598. get { return Enumerable<ILocation>.Empty; }
  1599. }
  1600. #endregion
  1601. }
  1602. /// <summary>
  1603. /// A collection of named members, with routines to search and maintain the collection. The search routines have sublinear complexity, typically close to constant time.
  1604. /// </summary>
  1605. /// <typeparam name="MemberType">The type of the members of this scope.</typeparam>
  1606. public abstract class Scope<MemberType> : IScope<MemberType>
  1607. where MemberType : class, INamedEntity {
  1608. private Dictionary<int, List<MemberType>> caseSensitiveMemberNameToMemberListMap = new Dictionary<int, List<MemberType>>();
  1609. private Dictionary<int, List<MemberType>> caseInsensitiveMemberNameToMemberListMap = new Dictionary<int, List<MemberType>>();
  1610. //TODO: replace BCL Dictionary with a private implementation that is thread safe and does not need a new list to be allocated for each name
  1611. /// <summary>
  1612. /// Adds a member to the scope. Does nothing if the member is already in the scope.
  1613. /// </summary>
  1614. /// <param name="member">The member to add to the scope.</param>
  1615. protected void AddMemberToCache(MemberType/*!*/ member)
  1616. //^ ensures this.Contains(member);
  1617. {
  1618. List<MemberType>/*?*/ members;
  1619. if (this.caseInsensitiveMemberNameToMemberListMap.TryGetValue(member.Name.UniqueKeyIgnoringCase, out members)) {
  1620. //^ assume members != null; //Follows from the way Dictionary is instantiated, but the verifier is ignorant of this.
  1621. if (!members.Contains(member)) members.Add(member);
  1622. } else {
  1623. this.caseInsensitiveMemberNameToMemberListMap[member.Name.UniqueKeyIgnoringCase] = members = new List<MemberType>();
  1624. members.Add(member);
  1625. }
  1626. if (this.caseSensitiveMemberNameToMemberListMap.TryGetValue(member.Name.UniqueKey, out members)) {
  1627. //^ assume members != null; //Follows from the way Dictionary is instantiated, but the verifier is ignorant of this.
  1628. if (!members.Contains(member)) members.Add(member);
  1629. } else {
  1630. this.caseSensitiveMemberNameToMemberListMap[member.Name.UniqueKey] = members = new List<MemberType>();
  1631. members.Add(member);
  1632. }
  1633. //^ assume this.Contains(member);
  1634. }
  1635. /// <summary>
  1636. /// Return true if the given member instance is a member of this scope.
  1637. /// </summary>
  1638. [Pure]
  1639. public bool Contains(MemberType/*!*/ member)
  1640. // ^ ensures result == exists{MemberType mem in this.Members; mem == member};
  1641. {
  1642. foreach (MemberType mem in this.GetMembersNamed(member.Name, false))
  1643. if (mem == member) return true;
  1644. return false;
  1645. }
  1646. /// <summary>
  1647. /// Returns the list of members with the given name that also satisfy the given predicate.
  1648. /// </summary>
  1649. [Pure]
  1650. public IEnumerable<MemberType> GetMatchingMembersNamed(IName name, bool ignoreCase, Function<MemberType, bool> predicate) {
  1651. foreach (MemberType member in this.GetMembersNamed(name, ignoreCase))
  1652. if (predicate(member)) yield return member;
  1653. }
  1654. /// <summary>
  1655. /// Returns the list of members that satisfy the given predicate.
  1656. /// </summary>
  1657. [Pure]
  1658. public IEnumerable<MemberType> GetMatchingMembers(Function<MemberType, bool> predicate)
  1659. // ^ ensures forall{MemberType member in result; member.Name.UniqueKey == name.UniqueKey && predicate(member) && this.Contains(member)};
  1660. // ^ ensures forall{MemberType member in this.Members; member.Name.UniqueKey == name.UniqueKey && predicate(member) ==>
  1661. // ^ exists{INamespaceMember mem in result; mem == member}};
  1662. {
  1663. foreach (MemberType member in this.Members)
  1664. if (predicate(member)) yield return member;
  1665. }
  1666. /// <summary>
  1667. /// Returns the list of members with the given name.
  1668. /// </summary>
  1669. /// <param name="name">The name of the members to retrieve.</param>
  1670. /// <param name="ignoreCase">True if the case of the name must be ignored when retrieving the members.</param>
  1671. [Pure]
  1672. public IEnumerable<MemberType> GetMembersNamed(IName name, bool ignoreCase)
  1673. // ^ ensures forall{MemberType member in result; member.Name.UniqueKey == name.UniqueKey && this.Contains(member)};
  1674. // ^ ensures forall{MemberType member in this.Members; member.Name.UniqueKey == name.UniqueKey ==>
  1675. // ^ exists{INamespaceMember mem in result; mem == member}};
  1676. {
  1677. this.InitializeIfNecessary();
  1678. Dictionary<int, List<MemberType>> nameToMemberListMap = ignoreCase ? this.caseInsensitiveMemberNameToMemberListMap : this.caseSensitiveMemberNameToMemberListMap;
  1679. int key = ignoreCase ? name.UniqueKeyIgnoringCase : name.UniqueKey;
  1680. List<MemberType>/*?*/ members;
  1681. if (!nameToMemberListMap.TryGetValue(key, out members)) return emptyList;
  1682. //^ assume members != null; //Follows from the way Dictionary is instantiated, but the verifier is ignorant of this.
  1683. return members.AsReadOnly();
  1684. }
  1685. private static readonly IEnumerable<MemberType> emptyList = (new List<MemberType>(0)).AsReadOnly();
  1686. /// <summary>
  1687. /// Provides a derived class with an opportunity to lazily initialize the scope's data structures via calls to AddMemberToCache.
  1688. /// </summary>
  1689. protected virtual void InitializeIfNecessary() { }
  1690. /// <summary>
  1691. /// The collection of member instances that are members of this scope.
  1692. /// </summary>
  1693. public virtual IEnumerable<MemberType> Members {
  1694. get {
  1695. this.InitializeIfNecessary();
  1696. foreach (IEnumerable<MemberType> namedMemberList in this.caseSensitiveMemberNameToMemberListMap.Values)
  1697. foreach (MemberType member in namedMemberList)
  1698. yield return member;
  1699. }
  1700. }
  1701. }
  1702. /// <summary>
  1703. ///
  1704. /// </summary>
  1705. /// <typeparam name="ParameterType"></typeparam>
  1706. public abstract class SpecializedGenericParameter<ParameterType> : IGenericParameter
  1707. where ParameterType : IGenericParameter {
  1708. /// <summary>
  1709. ///
  1710. /// </summary>
  1711. /// <param name="partiallySpecializedParameter"></param>
  1712. /// <param name="internFactory"></param>
  1713. protected SpecializedGenericParameter(ParameterType/*!*/ partiallySpecializedParameter, IInternFactory internFactory) {
  1714. this.unspecializedParameter = partiallySpecializedParameter;
  1715. this.internFactory = internFactory;
  1716. }
  1717. /// <summary>
  1718. /// Zero or more classes from which this type is derived.
  1719. /// For CLR types this collection is empty for interfaces and System.Object and populated with exactly one base type for all other types.
  1720. /// </summary>
  1721. /// <value></value>
  1722. public IEnumerable<ITypeReference> BaseClasses {
  1723. get { return Enumerable<ITypeReference>.Empty; }
  1724. }
  1725. /// <summary>
  1726. /// A list of classes or interfaces. All type arguments matching this parameter must be derived from all of the classes and implement all of the interfaces.
  1727. /// </summary>
  1728. /// <value></value>
  1729. public abstract IEnumerable<ITypeReference> Constraints { get; }
  1730. /// <summary>
  1731. /// Zero or more parameters that can be used as type annotations.
  1732. /// </summary>
  1733. /// <value></value>
  1734. public IEnumerable<IGenericTypeParameter> GenericParameters {
  1735. get { return Enumerable<IGenericTypeParameter>.Empty; }
  1736. }
  1737. /// <summary>
  1738. /// Zero or more interfaces implemented by this type.
  1739. /// </summary>
  1740. /// <value></value>
  1741. public IEnumerable<ITypeReference> Interfaces {
  1742. get { return Enumerable<ITypeReference>.Empty; }
  1743. }
  1744. /// <summary>
  1745. /// An instance of this generic type that has been obtained by using the generic parameters as the arguments.
  1746. /// Use this instance to look up members
  1747. /// </summary>
  1748. /// <value></value>
  1749. public IGenericTypeInstanceReference InstanceType {
  1750. get { return Dummy.GenericTypeInstance; }
  1751. }
  1752. /// <summary>
  1753. /// A way to get to platform types such as System.Object.
  1754. /// </summary>
  1755. /// <value></value>
  1756. public IPlatformType PlatformType {
  1757. get { return this.PartiallySpecializedParameter.PlatformType; }
  1758. }
  1759. public ParameterType/*!*/ PartiallySpecializedParameter {
  1760. get {
  1761. return this.unspecializedParameter;
  1762. }
  1763. }
  1764. readonly ParameterType/*!*/ unspecializedParameter;
  1765. /// <summary>
  1766. /// Zero or more implementation overrides provided by the class.
  1767. /// </summary>
  1768. /// <value></value>
  1769. public IEnumerable<IMethodImplementation> ExplicitImplementationOverrides {
  1770. get { return Enumerable<IMethodImplementation>.Empty; }
  1771. }
  1772. #region IGenericParameter Members
  1773. /// <summary>
  1774. /// True if all type arguments matching this parameter are constrained to be reference types.
  1775. /// </summary>
  1776. /// <value></value>
  1777. public bool MustBeReferenceType {
  1778. get { return this.PartiallySpecializedParameter.MustBeReferenceType; }
  1779. }
  1780. /// <summary>
  1781. /// True if all type arguments matching this parameter are constrained to be value types.
  1782. /// </summary>
  1783. /// <value></value>
  1784. public bool MustBeValueType {
  1785. get { return this.PartiallySpecializedParameter.MustBeValueType; }
  1786. }
  1787. /// <summary>
  1788. /// True if all type arguments matching this parameter are constrained to be value types or concrete classes with visible default constructors.
  1789. /// </summary>
  1790. /// <value></value>
  1791. public bool MustHaveDefaultConstructor {
  1792. get { return this.PartiallySpecializedParameter.MustHaveDefaultConstructor; }
  1793. }
  1794. /// <summary>
  1795. /// Indicates if the generic type or method with this type parameter is co-, contra-, or non variant with respect to this type parameter.
  1796. /// </summary>
  1797. /// <value></value>
  1798. public TypeParameterVariance Variance {
  1799. get { return this.PartiallySpecializedParameter.Variance; }
  1800. }
  1801. #endregion
  1802. #region ITypeDefinition Members
  1803. /// <summary>
  1804. /// The byte alignment that values of the given type ought to have. Must be a power of 2. If zero, the alignment is decided at runtime.
  1805. /// </summary>
  1806. /// <value></value>
  1807. public ushort Alignment {
  1808. get { return this.PartiallySpecializedParameter.Alignment; }
  1809. }
  1810. /// <summary>
  1811. /// Zero or more events defined by this type.
  1812. /// </summary>
  1813. /// <value></value>
  1814. public IEnumerable<IEventDefinition> Events {
  1815. get { return Enumerable<IEventDefinition>.Empty; }
  1816. }
  1817. /// <summary>
  1818. /// Zero or more fields defined by this type.
  1819. /// </summary>
  1820. /// <value></value>
  1821. public IEnumerable<IFieldDefinition> Fields {
  1822. get { return Enumerable<IFieldDefinition>.Empty; }
  1823. }
  1824. /// <summary>
  1825. /// The number of generic parameters. Zero if the type is not generic.
  1826. /// </summary>
  1827. /// <value></value>
  1828. public ushort GenericParameterCount {
  1829. get { return this.PartiallySpecializedParameter.GenericParameterCount; }
  1830. }
  1831. /// <summary>
  1832. /// True if the type may not be instantiated.
  1833. /// </summary>
  1834. /// <value></value>
  1835. public bool IsAbstract {
  1836. get { return this.PartiallySpecializedParameter.IsAbstract; }
  1837. }
  1838. /// <summary>
  1839. /// True if the type is a class (it is not an interface or type parameter and does not extend a special base class).
  1840. /// Corresponds to C# class.
  1841. /// </summary>
  1842. /// <value></value>
  1843. public bool IsClass {
  1844. get { return this.PartiallySpecializedParameter.IsClass; }
  1845. }
  1846. /// <summary>
  1847. /// True if the type is a delegate (it extends System.MultiCastDelegate). Corresponds to C# delegate
  1848. /// </summary>
  1849. /// <value></value>
  1850. public bool IsDelegate {
  1851. get { return this.PartiallySpecializedParameter.IsDelegate; }
  1852. }
  1853. /// <summary>
  1854. /// True if the type is an enumeration (it extends System.Enum and is sealed). Corresponds to C# enum.
  1855. /// </summary>
  1856. /// <value></value>
  1857. public bool IsEnum {
  1858. get { return this.PartiallySpecializedParameter.IsEnum; }
  1859. }
  1860. /// <summary>
  1861. /// True if this type is parameterized (this.GenericParameters is a non empty collection).
  1862. /// </summary>
  1863. /// <value></value>
  1864. public bool IsGeneric {
  1865. get { return this.PartiallySpecializedParameter.IsGeneric; }
  1866. }
  1867. /// <summary>
  1868. /// True if the type is an interface.
  1869. /// </summary>
  1870. /// <value></value>
  1871. public bool IsInterface {
  1872. get { return this.PartiallySpecializedParameter.IsInterface; }
  1873. }
  1874. /// <summary>
  1875. /// True if the type is a reference type. A reference type is non static class or interface or a suitably constrained type parameter.
  1876. /// A type parameter for which MustBeReferenceType (the class constraint in C#) is true returns true for this property
  1877. /// as does a type parameter with a constraint that is a class.
  1878. /// </summary>
  1879. /// <value></value>
  1880. public bool IsReferenceType {
  1881. get { return this.PartiallySpecializedParameter.IsReferenceType; }
  1882. }
  1883. /// <summary>
  1884. /// True if the type may not be subtyped.
  1885. /// </summary>
  1886. /// <value></value>
  1887. public bool IsSealed {
  1888. get { return this.PartiallySpecializedParameter.IsSealed; }
  1889. }
  1890. /// <summary>
  1891. /// True if the type is an abstract sealed class that directly extends System.Object and declares no constructors.
  1892. /// </summary>
  1893. /// <value></value>
  1894. public bool IsStatic {
  1895. get { return this.PartiallySpecializedParameter.IsStatic; }
  1896. }
  1897. /// <summary>
  1898. /// True if the type is a value type.
  1899. /// Value types are sealed and extend System.ValueType or System.Enum.
  1900. /// A type parameter for which MustBeValueType (the struct constraint in C#) is true also returns true for this property.
  1901. /// </summary>
  1902. /// <value></value>
  1903. public bool IsValueType {
  1904. get { return this.PartiallySpecializedParameter.IsValueType; }
  1905. }
  1906. /// <summary>
  1907. /// True if the type is a struct (its not Primitive, is sealed and base is System.ValueType).
  1908. /// </summary>
  1909. /// <value></value>
  1910. public bool IsStruct {
  1911. get { return this.PartiallySpecializedParameter.IsStruct; }
  1912. }
  1913. /// <summary>
  1914. /// The collection of member instances that are members of this scope.
  1915. /// </summary>
  1916. /// <value></value>
  1917. public IEnumerable<ITypeDefinitionMember> Members {
  1918. get { return Enumerable<ITypeDefinitionMember>.Empty; }
  1919. }
  1920. /// <summary>
  1921. /// Zero or more methods defined by this type.
  1922. /// </summary>
  1923. /// <value></value>
  1924. public IEnumerable<IMethodDefinition> Methods {
  1925. get { return Enumerable<IMethodDefinition>.Empty; }
  1926. }
  1927. /// <summary>
  1928. /// Zero or more nested types defined by this type.
  1929. /// </summary>
  1930. /// <value></value>
  1931. public IEnumerable<INestedTypeDefinition> NestedTypes {
  1932. get { return Enumerable<INestedTypeDefinition>.Empty; }
  1933. }
  1934. /// <summary>
  1935. /// Zero or more properties defined by this type.
  1936. /// </summary>
  1937. /// <value></value>
  1938. public IEnumerable<IPropertyDefinition> Properties {
  1939. get { return Enumerable<IPropertyDefinition>.Empty; }
  1940. }
  1941. /// <summary>
  1942. /// Size of an object of this type. In bytes. If zero, the size is unspecified and will be determined at runtime.
  1943. /// </summary>
  1944. /// <value></value>
  1945. public uint SizeOf {
  1946. get { return this.PartiallySpecializedParameter.SizeOf; }
  1947. }
  1948. /// <summary>
  1949. /// Declarative security actions for this type. Will be empty if this.HasSecurity is false.
  1950. /// </summary>
  1951. /// <value></value>
  1952. public IEnumerable<ISecurityAttribute> SecurityAttributes {
  1953. get { return this.PartiallySpecializedParameter.SecurityAttributes; }
  1954. }
  1955. /// <summary>
  1956. /// Returns a reference to the underlying (integral) type on which this (enum) type is based.
  1957. /// </summary>
  1958. /// <value></value>
  1959. public ITypeReference UnderlyingType {
  1960. get { return this.PartiallySpecializedParameter.UnderlyingType; }
  1961. }
  1962. /// <summary>
  1963. /// Unless the value of TypeCode is PrimitiveTypeCode.NotPrimitive, the type corresponds to a "primitive" CLR type (such as System.Int32) and
  1964. /// the type code identifies which of the primitive types it corresponds to.
  1965. /// </summary>
  1966. /// <value></value>
  1967. public PrimitiveTypeCode TypeCode {
  1968. get { return this.PartiallySpecializedParameter.TypeCode; }
  1969. }
  1970. /// <summary>
  1971. /// A potentially empty collection of locations that correspond to this instance.
  1972. /// </summary>
  1973. /// <value></value>
  1974. public IEnumerable<ILocation> Locations {
  1975. get { return this.PartiallySpecializedParameter.Locations; }
  1976. }
  1977. /// <summary>
  1978. /// Layout of the type.
  1979. /// </summary>
  1980. /// <value></value>
  1981. public LayoutKind Layout {
  1982. get { return this.PartiallySpecializedParameter.Layout; }
  1983. }
  1984. /// <summary>
  1985. /// True if the type has special name.
  1986. /// </summary>
  1987. /// <value></value>
  1988. public bool IsSpecialName {
  1989. get { return this.PartiallySpecializedParameter.IsSpecialName; }
  1990. }
  1991. /// <summary>
  1992. /// Is this imported from COM type library
  1993. /// </summary>
  1994. /// <value></value>
  1995. public bool IsComObject {
  1996. get { return this.PartiallySpecializedParameter.IsComObject; }
  1997. }
  1998. /// <summary>
  1999. /// True if this type is serializable.
  2000. /// </summary>
  2001. /// <value></value>
  2002. public bool IsSerializable {
  2003. get { return this.PartiallySpecializedParameter.IsSerializable; }
  2004. }
  2005. /// <summary>
  2006. /// Is type initialized anytime before first access to static field
  2007. /// </summary>
  2008. /// <value></value>
  2009. public bool IsBeforeFieldInit {
  2010. get { return this.PartiallySpecializedParameter.IsBeforeFieldInit; }
  2011. }
  2012. /// <summary>
  2013. /// Default marshalling of the Strings in this class.
  2014. /// </summary>
  2015. /// <value></value>
  2016. public StringFormatKind StringFormat {
  2017. get { return this.PartiallySpecializedParameter.StringFormat; }
  2018. }
  2019. /// <summary>
  2020. /// True if this type gets special treatment from the runtime.
  2021. /// </summary>
  2022. /// <value></value>
  2023. public bool IsRuntimeSpecial {
  2024. get { return this.PartiallySpecializedParameter.IsRuntimeSpecial; }
  2025. }
  2026. /// <summary>
  2027. /// True if this type has a non empty collection of SecurityAttributes or the System.Security.SuppressUnmanagedCodeSecurityAttribute.
  2028. /// </summary>
  2029. /// <value></value>
  2030. public bool HasDeclarativeSecurity {
  2031. get { return this.PartiallySpecializedParameter.HasDeclarativeSecurity; }
  2032. }
  2033. /// <summary>
  2034. /// Zero or more private type members generated by the compiler for implementation purposes. These members
  2035. /// are only available after a complete visit of all of the other members of the type, including the bodies of methods.
  2036. /// </summary>
  2037. /// <value></value>
  2038. public IEnumerable<ITypeDefinitionMember> PrivateHelperMembers {
  2039. get { return this.PartiallySpecializedParameter.PrivateHelperMembers; }
  2040. }
  2041. #endregion
  2042. #region IDefinition Members
  2043. /// <summary>
  2044. /// A collection of metadata custom attributes that are associated with this definition.
  2045. /// </summary>
  2046. /// <value></value>
  2047. public IEnumerable<ICustomAttribute> Attributes {
  2048. get { return this.PartiallySpecializedParameter.Attributes; }
  2049. }
  2050. #endregion
  2051. #region IDoubleDispatcher Members
  2052. /// <summary>
  2053. /// Calls the visitor.Visit(T) method where T is the most derived object model node interface type implemented by the concrete type
  2054. /// of the object implementing IReference. The dispatch method does nothing else.
  2055. /// </summary>
  2056. public abstract void Dispatch(IMetadataVisitor visitor);
  2057. /// <summary>
  2058. /// Calls the visitor.Visit(T) method where T is the most derived object model node interface type implemented by the concrete type
  2059. /// of the object implementing IReference, which is not derived from IDefinition. For example an object implemeting IArrayType will
  2060. /// call visitor.Visit(IArrayTypeReference) and not visitor.Visit(IArrayType).
  2061. /// The dispatch method does nothing else.
  2062. /// </summary>
  2063. public abstract void DispatchAsReference(IMetadataVisitor visitor);
  2064. #endregion
  2065. #region IParameterListEntry Members
  2066. /// <summary>
  2067. /// The position in the parameter list where this instance can be found.
  2068. /// </summary>
  2069. /// <value></value>
  2070. public ushort Index {
  2071. get { return this.PartiallySpecializedParameter.Index; }
  2072. }
  2073. #endregion
  2074. #region INamedEntity Members
  2075. /// <summary>
  2076. /// The name of the entity.
  2077. /// </summary>
  2078. /// <value></value>
  2079. public IName Name {
  2080. get { return this.PartiallySpecializedParameter.Name; }
  2081. }
  2082. #endregion
  2083. #region IScope<ITypeDefinitionMember> Members
  2084. /// <summary>
  2085. /// Return true if the given member instance is a member of this scope.
  2086. /// </summary>
  2087. /// <param name="member"></param>
  2088. /// <returns></returns>
  2089. [Pure]
  2090. public bool Contains(ITypeDefinitionMember member) {
  2091. return false;
  2092. }
  2093. /// <summary>
  2094. /// Returns the list of members with the given name that also satisfy the given predicate.
  2095. /// </summary>
  2096. /// <param name="name"></param>
  2097. /// <param name="ignoreCase"></param>
  2098. /// <param name="predicate"></param>
  2099. /// <returns></returns>
  2100. [Pure]
  2101. public IEnumerable<ITypeDefinitionMember> GetMatchingMembersNamed(IName name, bool ignoreCase, Function<ITypeDefinitionMember, bool> predicate) {
  2102. return Enumerable<ITypeDefinitionMember>.Empty;
  2103. }
  2104. /// <summary>
  2105. /// Returns the list of members that satisfy the given predicate.
  2106. /// </summary>
  2107. /// <param name="predicate"></param>
  2108. /// <returns></returns>
  2109. [Pure]
  2110. public IEnumerable<ITypeDefinitionMember> GetMatchingMembers(Function<ITypeDefinitionMember, bool> predicate) {
  2111. return Enumerable<ITypeDefinitionMember>.Empty;
  2112. }
  2113. /// <summary>
  2114. /// Returns the list of members with the given name.
  2115. /// </summary>
  2116. /// <param name="name"></param>
  2117. /// <param name="ignoreCase"></param>
  2118. /// <returns></returns>
  2119. [Pure]
  2120. public IEnumerable<ITypeDefinitionMember> GetMembersNamed(IName name, bool ignoreCase) {
  2121. return Enumerable<ITypeDefinitionMember>.Empty;
  2122. }
  2123. #endregion
  2124. #region ITypeReference Members
  2125. /// <summary>
  2126. /// Indicates if this type reference resolved to an alias rather than a type
  2127. /// </summary>
  2128. /// <value></value>
  2129. public bool IsAlias {
  2130. get { return false; }
  2131. }
  2132. /// <summary>
  2133. /// Gives the alias for the type
  2134. /// </summary>
  2135. /// <value></value>
  2136. public IAliasForType AliasForType {
  2137. get { return Dummy.AliasForType; }
  2138. }
  2139. public IEnumerable<ICustomModifier> CustomModifiers {
  2140. get { return Enumerable<ICustomModifier>.Empty; }
  2141. }
  2142. public bool IsModified {
  2143. get { return false; }
  2144. }
  2145. /// <summary>
  2146. /// The type definition being referred to.
  2147. /// In case this type was alias, this is also the type of the aliased type
  2148. /// </summary>
  2149. /// <value></value>
  2150. ITypeDefinition ITypeReference.ResolvedType {
  2151. get { return this; }
  2152. }
  2153. /// <summary>
  2154. /// A collection of methods that associate unique integers with metadata model entities.
  2155. /// The association is based on the identities of the entities and the factory does not retain
  2156. /// references to the given metadata model objects.
  2157. /// </summary>
  2158. public IInternFactory InternFactory {
  2159. get { return this.internFactory; }
  2160. }
  2161. readonly IInternFactory internFactory;
  2162. /// <summary>
  2163. /// Returns the unique interned key associated with the type. This takes unification/aliases/custom modifiers into account.
  2164. /// </summary>
  2165. /// <value></value>
  2166. public uint InternedKey {
  2167. get {
  2168. if (this.internedKey == 0) {
  2169. this.internedKey = this.InternFactory.GetTypeReferenceInternedKey(this);
  2170. }
  2171. return this.internedKey;
  2172. }
  2173. }
  2174. uint internedKey;
  2175. #endregion
  2176. #region INamedTypeReference Members
  2177. /// <summary>
  2178. /// If true, the persisted type name is mangled by appending "`n" where n is the number of type parameters, if the number of type parameters is greater than 0.
  2179. /// </summary>
  2180. /// <value></value>
  2181. public bool MangleName {
  2182. get { return false; }
  2183. }
  2184. /// <summary>
  2185. /// The type definition being referred to.
  2186. /// In case this type was alias, this is also the type of the aliased type
  2187. /// </summary>
  2188. /// <value></value>
  2189. public INamedTypeDefinition ResolvedType {
  2190. get { return this; }
  2191. }
  2192. #endregion
  2193. }
  2194. /// <summary>
  2195. ///
  2196. /// </summary>
  2197. public class SpecializedGenericTypeParameter : SpecializedGenericParameter<IGenericTypeParameter>, IGenericTypeParameter {
  2198. /// <summary>
  2199. ///
  2200. /// </summary>
  2201. /// <param name="partiallySpecializedParameter"></param>
  2202. /// <param name="definingTypeInstance"></param>
  2203. /// <param name="internFactory"></param>
  2204. public SpecializedGenericTypeParameter(IGenericTypeParameter partiallySpecializedParameter, ISpecializedNestedTypeReference definingTypeInstance, IInternFactory internFactory)
  2205. : base(partiallySpecializedParameter, internFactory) {
  2206. this.definingType = definingTypeInstance;
  2207. }
  2208. /// <summary>
  2209. /// Return the innermost containing generic type instance.
  2210. /// </summary>
  2211. public IGenericTypeInstanceReference ContainingGenericTypeInstance {
  2212. get {
  2213. if (this.containingGenericTypeInstance == null) {
  2214. ITypeReference typeReference = this.DefiningType.ContainingType;
  2215. var containingGenericTypeInstance = typeReference as IGenericTypeInstanceReference;
  2216. if (containingGenericTypeInstance == null) {
  2217. ISpecializedNestedTypeReference nested = typeReference as ISpecializedNestedTypeReference;
  2218. while (containingGenericTypeInstance == null && nested != null) {
  2219. containingGenericTypeInstance = nested.ContainingType as IGenericTypeInstanceReference;
  2220. nested = nested.ContainingType as ISpecializedNestedTypeReference;
  2221. }
  2222. Debug.Assert(containingGenericTypeInstance != null);
  2223. }
  2224. this.containingGenericTypeInstance = containingGenericTypeInstance;
  2225. }
  2226. return this.containingGenericTypeInstance;
  2227. }
  2228. }
  2229. private IGenericTypeInstanceReference containingGenericTypeInstance;
  2230. /// <summary>
  2231. /// A list of classes or interfaces. All type arguments matching this parameter must be derived from all of the classes and implement all of the interfaces.
  2232. /// </summary>
  2233. /// <value></value>
  2234. public override IEnumerable<ITypeReference> Constraints {
  2235. get {
  2236. if (this.constraints == null) {
  2237. var constrs = new List<ITypeReference>();
  2238. foreach (ITypeReference partiallySpecializedConstraint in this.PartiallySpecializedParameter.Constraints)
  2239. constrs.Add(TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(partiallySpecializedConstraint, this.ContainingGenericTypeInstance, this.InternFactory));
  2240. constrs.TrimExcess();
  2241. this.constraints = constrs.AsReadOnly();
  2242. }
  2243. return this.constraints;
  2244. }
  2245. }
  2246. IEnumerable<ITypeReference>/*?*/ constraints;
  2247. /// <summary>
  2248. /// Calls the visitor.Visit(IGenericTypeParameter) method.
  2249. /// </summary>
  2250. public override void Dispatch(IMetadataVisitor visitor) {
  2251. visitor.Visit(this);
  2252. }
  2253. /// <summary>
  2254. /// Calls the visitor.Visit(IGenericTypeParameterReference) method.
  2255. /// </summary>
  2256. public override void DispatchAsReference(IMetadataVisitor visitor) {
  2257. visitor.Visit((IGenericTypeParameterReference)this);
  2258. }
  2259. /// <summary>
  2260. /// The generic type that defines this type parameter.
  2261. /// </summary>
  2262. /// <value></value>
  2263. public ISpecializedNestedTypeReference DefiningType {
  2264. get { return this.definingType; }
  2265. }
  2266. readonly ISpecializedNestedTypeReference definingType;
  2267. #region IGenericTypeParameter Members
  2268. ITypeDefinition IGenericTypeParameter.DefiningType {
  2269. get { return this.DefiningType.ResolvedType; }
  2270. }
  2271. #endregion
  2272. #region IGenericTypeParameterReference Members
  2273. ITypeReference IGenericTypeParameterReference.DefiningType {
  2274. get { return this.DefiningType; }
  2275. }
  2276. IGenericTypeParameter IGenericTypeParameterReference.ResolvedType {
  2277. get { return this; }
  2278. }
  2279. #endregion
  2280. }
  2281. /// <summary>
  2282. /// A type definition that is a specialized nested type. That is, the type definition is a member of a generic type instance, or of another specialized nested type.
  2283. /// It is specialized, because if it had any references to the type parameters of the generic type, then those references have been replaced with the type arguments of the instance.
  2284. /// In other words, it may be less generic than before, and hence it has been "specialized".
  2285. /// </summary>
  2286. public class SpecializedNestedTypeDefinition : Scope<ITypeDefinitionMember>, ISpecializedNestedTypeDefinition, ISpecializedNestedTypeReference {
  2287. /// <summary>
  2288. /// Allocates a type definition that is a specialized nested type. That is, the type definition is a member of a generic type instance, or of another specialized nested type.
  2289. /// It is specialized, because if it had any references to the type parameters of the generic type, then those references have been replaced with the type arguments of the instance.
  2290. /// In other words, it may be less generic than before, and hence it has been "specialized".
  2291. /// </summary>
  2292. /// <param name="unspecializedVersion">The most generic version of the nested type. In other words, the one that the programmer wrote. In persisted metadata, type references are
  2293. /// always to instantiations of the unspecialized version, with all inherited type arguments repeated inside the reference.</param>
  2294. /// <param name="partiallySpecializedVersion">If containingGenericTypeInstance is an instance of a specialized nested generic type, then its members already have been specialized as
  2295. /// part of the specialization of the generic (template) type. In that case, partiallySpecializedVersion is different from unspecialized version. At any rate, the thing to actually
  2296. /// specialize if partiallySpecializedVersion. unspecializedVersion is used mainly for mapping to the CLR PE file format.</param>
  2297. /// <param name="containingGenericTypeInstance">The generic type instance that supplies the type arguments that will be substituted for type parameters to create the specialized nested
  2298. /// type created by this construtor.</param>
  2299. /// <param name="containingTypeDefinition">The actual type that contains the specialized member constructed by this constructor. It can either be a generic type instance, or specialized
  2300. /// nested type.</param>
  2301. /// <param name="internFactory">The intern factory to use for computing the interned identity of this type and any types and members referenced by it.</param>
  2302. public SpecializedNestedTypeDefinition(INestedTypeDefinition unspecializedVersion, INestedTypeDefinition partiallySpecializedVersion, ITypeDefinition containingTypeDefinition, GenericTypeInstance containingGenericTypeInstance, IInternFactory internFactory) {
  2303. this.unspecializedVersion = unspecializedVersion;
  2304. this.partiallySpecializedVersion = partiallySpecializedVersion;
  2305. this.containingGenericTypeInstance = containingGenericTypeInstance;
  2306. this.containingTypeDefinition = containingTypeDefinition;
  2307. this.internFactory = internFactory;
  2308. }
  2309. /// <summary>
  2310. /// Zero or more classes from which this type is derived.
  2311. /// For CLR types this collection is empty for interfaces and System.Object and populated with exactly one base type for all other types.
  2312. /// </summary>
  2313. public IEnumerable<ITypeReference> BaseClasses {
  2314. get {
  2315. if (this.baseClasses == null) {
  2316. var bclasses = new List<ITypeReference>(1);
  2317. foreach (var partiallySpecializedBaseClassRef in this.partiallySpecializedVersion.BaseClasses)
  2318. bclasses.Add(this.CopyAndSpecialize(partiallySpecializedBaseClassRef));
  2319. bclasses.TrimExcess();
  2320. this.baseClasses = bclasses.AsReadOnly();
  2321. }
  2322. return this.baseClasses;
  2323. }
  2324. }
  2325. IEnumerable<ITypeReference>/*?*/ baseClasses;
  2326. internal static ITypeReference DeepCopyTypeReference(INestedTypeReference nestedType, SpecializedNestedTypeDefinition targetContainer, IInternFactory internFactory) {
  2327. var parentCopy = TypeDefinition.DeepCopyTypeReference(nestedType.ContainingType, targetContainer, internFactory);
  2328. if (parentCopy == nestedType.ContainingType) return nestedType;
  2329. return new SpecializedNestedTypeReference(TypeHelper.Unspecialize(nestedType), parentCopy, internFactory);
  2330. }
  2331. internal static ITypeReference SpecializeTypeReference(INestedTypeReference nestedType, ITypeReference targetContainer, IInternFactory internFactory) {
  2332. var parentCopy = TypeHelper.SpecializeTypeReference(nestedType.ContainingType, targetContainer, internFactory);
  2333. if (parentCopy == nestedType.ContainingType) return nestedType;
  2334. return new SpecializedNestedTypeReference(TypeHelper.Unspecialize(nestedType), parentCopy, internFactory);
  2335. }
  2336. internal static ITypeReference SpecializeTypeReference(INestedTypeReference nestedType, IMethodReference targetContainer, IInternFactory internFactory) {
  2337. var parentCopy = TypeHelper.SpecializeTypeReference(nestedType.ContainingType, targetContainer, internFactory);
  2338. if (parentCopy == nestedType.ContainingType) return nestedType;
  2339. return new SpecializedNestedTypeReference(TypeHelper.Unspecialize(nestedType), parentCopy, internFactory);
  2340. }
  2341. internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(INestedTypeReference nestedType, ISpecializedMethodReference specializedMethodReference, IInternFactory internFactory) {
  2342. var specializedParent = TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(nestedType.ContainingType, specializedMethodReference, internFactory);
  2343. if (specializedParent == nestedType.ContainingType) return nestedType;
  2344. return new SpecializedNestedTypeReference(TypeHelper.Unspecialize(nestedType), specializedParent, internFactory);
  2345. }
  2346. internal static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(INestedTypeReference nestedType, IGenericMethodInstanceReference genericMethodInstance, IInternFactory internFactory) {
  2347. var specializedParent = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(nestedType.ContainingType, genericMethodInstance, internFactory);
  2348. if (specializedParent == nestedType.ContainingType) return nestedType;
  2349. return new SpecializedNestedTypeReference(TypeHelper.Unspecialize(nestedType), specializedParent, internFactory);
  2350. }
  2351. internal static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(INestedTypeReference nestedType, IGenericTypeInstanceReference containingTypeInstance, IInternFactory internFactory) {
  2352. var specializedParent = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(nestedType.ContainingType, containingTypeInstance, internFactory);
  2353. if (specializedParent == nestedType.ContainingType) return nestedType;
  2354. return new SpecializedNestedTypeReference(TypeHelper.Unspecialize(nestedType), specializedParent, internFactory);
  2355. }
  2356. /// <summary>
  2357. /// Zero or more parameters that can be used as type annotations.
  2358. /// </summary>
  2359. public IEnumerable<IGenericTypeParameter> GenericParameters {
  2360. get {
  2361. if (this.genericParameters == null) {
  2362. lock (GlobalLock.LockingObject) {
  2363. if (this.genericParameters == null) {
  2364. var gpars = new List<IGenericTypeParameter>(this.GenericParameterCount);
  2365. foreach (IGenericTypeParameter parameter in this.partiallySpecializedVersion.GenericParameters)
  2366. gpars.Add(new SpecializedGenericTypeParameter(parameter, this, this.InternFactory));
  2367. this.genericParameters = gpars.AsReadOnly();
  2368. }
  2369. }
  2370. }
  2371. return this.genericParameters;
  2372. }
  2373. }
  2374. IEnumerable<IGenericTypeParameter>/*?*/ genericParameters;
  2375. protected override void InitializeIfNecessary() {
  2376. if (this.initialized) return;
  2377. lock (GlobalLock.LockingObject) {
  2378. if (this.initialized) return;
  2379. foreach (ITypeDefinitionMember partiallySpecializedMember in this.partiallySpecializedVersion.Members) {
  2380. //^ assume unspecializedMember is IEventDefinition || unspecializedMember is IFieldDefinition || unspecializedMember is IMethodDefinition ||
  2381. //^ unspecializedMember is IPropertyDefinition || unspecializedMember is INestedTypeDefinition; //follows from informal post condition on Members property.
  2382. this.AddMemberToCache(this.SpecializeMember(partiallySpecializedMember, this.InternFactory));
  2383. }
  2384. this.initialized = true;
  2385. }
  2386. }
  2387. private bool initialized;
  2388. /// <summary>
  2389. /// The generic type instance that supplies the mapping from type parameters to type arguments that is used to create this specialized type definition.
  2390. /// Any type references made by this nested type, such as to its base class, interfaces, or made by members of this nested type, are created by
  2391. /// specializing the corresponding reference from the partially specialized version of this type, using the mapping defined by this instance.
  2392. /// </summary>
  2393. public GenericTypeInstance ContainingGenericTypeInstance {
  2394. get { return this.containingGenericTypeInstance; }
  2395. }
  2396. readonly GenericTypeInstance containingGenericTypeInstance;
  2397. /// <summary>
  2398. /// Zero or more implementation overrides provided by the class.
  2399. /// </summary>
  2400. /// <value></value>
  2401. public IEnumerable<IMethodImplementation> ExplicitImplementationOverrides {
  2402. get {
  2403. foreach (var methodImplementation in this.UnspecializedVersion.ExplicitImplementationOverrides)
  2404. yield return new SpecializedMethodImplementation(this, methodImplementation, this.InternFactory);
  2405. }
  2406. }
  2407. /// <summary>
  2408. /// Zero or more interfaces implemented by this type.
  2409. /// </summary>
  2410. /// <value></value>
  2411. public IEnumerable<ITypeReference> Interfaces {
  2412. get {
  2413. if (this.interfaces == null) {
  2414. var ifaces = new List<ITypeReference>();
  2415. foreach (ITypeReference partiallySpecializedInterfaceRef in this.partiallySpecializedVersion.Interfaces)
  2416. ifaces.Add(this.CopyAndSpecialize(partiallySpecializedInterfaceRef));
  2417. ifaces.TrimExcess();
  2418. this.interfaces = ifaces.AsReadOnly();
  2419. }
  2420. return this.interfaces;
  2421. }
  2422. }
  2423. IEnumerable<ITypeReference>/*?*/ interfaces;
  2424. /// <summary>
  2425. /// An instance of this generic type that has been obtained by using the generic parameters as the arguments.
  2426. /// Use this instance to look up members
  2427. /// </summary>
  2428. /// <value></value>
  2429. public IGenericTypeInstanceReference InstanceType {
  2430. get {
  2431. //^ requires this.IsGeneric;
  2432. if (instanceType == null) {
  2433. lock (GlobalLock.LockingObject) {
  2434. if (this.instanceType == null) {
  2435. List<ITypeReference> arguments = new List<ITypeReference>();
  2436. foreach (IGenericTypeParameter gpar in this.GenericParameters) arguments.Add(gpar);
  2437. this.instanceType = GenericTypeInstance.GetGenericTypeInstance(this, arguments, this.InternFactory);
  2438. }
  2439. }
  2440. }
  2441. return instanceType;
  2442. }
  2443. }
  2444. IGenericTypeInstanceReference/*?*/ instanceType= null;
  2445. /// <summary>
  2446. /// Zero or more methods defined by this type.
  2447. /// </summary>
  2448. /// <value></value>
  2449. public IEnumerable<IMethodDefinition> Methods {
  2450. get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, IMethodDefinition>(this.Members); }
  2451. }
  2452. internal readonly INestedTypeDefinition partiallySpecializedVersion;
  2453. /// <summary>
  2454. /// A way to get to platform types such as System.Object.
  2455. /// </summary>
  2456. /// <value></value>
  2457. public IPlatformType PlatformType {
  2458. get { return this.partiallySpecializedVersion.PlatformType; }
  2459. }
  2460. /// <summary>
  2461. /// Zero or more nested types defined by this type.
  2462. /// </summary>
  2463. /// <value></value>
  2464. public IEnumerable<INestedTypeDefinition> NestedTypes {
  2465. get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, INestedTypeDefinition>(this.Members); }
  2466. }
  2467. /// <summary>
  2468. /// Zero or more private type members generated by the compiler for implementation purposes. These members
  2469. /// are only available after a complete visit of all of the other members of the type, including the bodies of methods.
  2470. /// </summary>
  2471. /// <value></value>
  2472. public IEnumerable<ITypeDefinitionMember> PrivateHelperMembers {
  2473. get { return Enumerable<ITypeDefinitionMember>.Empty; }
  2474. }
  2475. /// <summary>
  2476. /// Zero or more properties defined by this type.
  2477. /// </summary>
  2478. /// <value></value>
  2479. public IEnumerable<IPropertyDefinition> Properties {
  2480. get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, IPropertyDefinition>(this.Members); }
  2481. }
  2482. /// <summary>
  2483. /// Makes a copy of the given type reference, making sure that any references to this.partiallySpecializedVersion.ContainingType or something defined, directly or indirectly,
  2484. /// by this.partiallySpecializedVersion.Containing type are replaced with the equivalent reference to this.ContainingType or something defined, directly or indirectly
  2485. /// by this.ContainingType. Also replaces all references to type parameters of this.ContainingGenericTypeInstance with the corresponding type arguments.
  2486. /// </summary>
  2487. /// <param name="partiallySpecializedTypeReference">A type reference obtained from some part of this.unspecializedVersion.</param>
  2488. private ITypeReference CopyAndSpecialize(ITypeReference partiallySpecializedTypeReference) {
  2489. partiallySpecializedTypeReference = TypeDefinition.DeepCopyTypeReference(partiallySpecializedTypeReference, this, this.InternFactory);
  2490. return TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(partiallySpecializedTypeReference, this.ContainingGenericTypeInstance, this.InternFactory);
  2491. }
  2492. /// <summary>
  2493. /// Returns a reference to the underlying (integral) type on which this (enum) type is based.
  2494. /// </summary>
  2495. /// <value></value>
  2496. public ITypeReference UnderlyingType {
  2497. get { return this.UnspecializedVersion.UnderlyingType; }
  2498. }
  2499. /// <summary>
  2500. /// The nested type that has been specialized to obtain this nested type. When the containing type is an instance of type which is itself a specialized member (i.e. it is a nested
  2501. /// type of a generic type instance), then the unspecialized member refers to a member from the unspecialized containing type. (I.e. the unspecialized member always
  2502. /// corresponds to a definition that is not obtained via specialization.)
  2503. /// </summary>
  2504. /// <value></value>
  2505. public INestedTypeDefinition UnspecializedVersion {
  2506. get { return this.unspecializedVersion; }
  2507. }
  2508. readonly INestedTypeDefinition unspecializedVersion;
  2509. /// <summary>
  2510. /// Indicates if the member is public or confined to its containing type, derived types and/or declaring assembly.
  2511. /// </summary>
  2512. /// <value></value>
  2513. public TypeMemberVisibility Visibility {
  2514. get {
  2515. if (this.visibility == TypeMemberVisibility.Default) {
  2516. this.visibility = TypeHelper.VisibilityIntersection(this.partiallySpecializedVersion.Visibility,
  2517. TypeHelper.TypeVisibilityAsTypeMemberVisibility(this.ContainingGenericTypeInstance));
  2518. }
  2519. return this.visibility;
  2520. }
  2521. }
  2522. TypeMemberVisibility visibility = TypeMemberVisibility.Default;
  2523. #region INestedTypeDefinition Members
  2524. /// <summary>
  2525. /// The type definition that contains this member.
  2526. /// </summary>
  2527. /// <value></value>
  2528. public ITypeDefinition ContainingTypeDefinition {
  2529. get { return this.containingTypeDefinition; }
  2530. }
  2531. ITypeDefinition containingTypeDefinition;
  2532. #endregion
  2533. #region ITypeDefinition Members
  2534. /// <summary>
  2535. /// The byte alignment that values of the given type ought to have. Must be a power of 2. If zero, the alignment is decided at runtime.
  2536. /// </summary>
  2537. /// <value></value>
  2538. public ushort Alignment {
  2539. get { return this.UnspecializedVersion.Alignment; }
  2540. }
  2541. /// <summary>
  2542. /// Zero or more events defined by this type.
  2543. /// </summary>
  2544. /// <value></value>
  2545. public IEnumerable<IEventDefinition> Events {
  2546. get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, IEventDefinition>(this.Members); }
  2547. }
  2548. /// <summary>
  2549. /// Zero or more fields defined by this type.
  2550. /// </summary>
  2551. /// <value></value>
  2552. public IEnumerable<IFieldDefinition> Fields {
  2553. get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, IFieldDefinition>(this.Members); }
  2554. }
  2555. /// <summary>
  2556. /// The number of generic parameters. Zero if the type is not generic.
  2557. /// </summary>
  2558. /// <value></value>
  2559. public ushort GenericParameterCount {
  2560. get { return this.UnspecializedVersion.GenericParameterCount; }
  2561. }
  2562. /// <summary>
  2563. /// True if the type may not be instantiated.
  2564. /// </summary>
  2565. /// <value></value>
  2566. public bool IsAbstract {
  2567. get { return this.UnspecializedVersion.IsAbstract; }
  2568. }
  2569. /// <summary>
  2570. /// True if the type is a class (it is not an interface or type parameter and does not extend a special base class).
  2571. /// Corresponds to C# class.
  2572. /// </summary>
  2573. /// <value></value>
  2574. public bool IsClass {
  2575. get { return this.UnspecializedVersion.IsClass; }
  2576. }
  2577. /// <summary>
  2578. /// True if the type is a delegate (it extends System.MultiCastDelegate). Corresponds to C# delegate
  2579. /// </summary>
  2580. /// <value></value>
  2581. public bool IsDelegate {
  2582. get { return this.UnspecializedVersion.IsDelegate; }
  2583. }
  2584. /// <summary>
  2585. /// True if the type is an enumeration (it extends System.Enum and is sealed). Corresponds to C# enum.
  2586. /// </summary>
  2587. /// <value></value>
  2588. public bool IsEnum {
  2589. get { return this.UnspecializedVersion.IsEnum; }
  2590. }
  2591. /// <summary>
  2592. /// True if this type is parameterized (this.GenericParameters is a non empty collection).
  2593. /// </summary>
  2594. /// <value></value>
  2595. public bool IsGeneric {
  2596. get { return this.UnspecializedVersion.IsGeneric; }
  2597. }
  2598. /// <summary>
  2599. /// True if the type is an interface.
  2600. /// </summary>
  2601. /// <value></value>
  2602. public bool IsInterface {
  2603. get { return this.UnspecializedVersion.IsInterface; }
  2604. }
  2605. /// <summary>
  2606. /// True if the type is a reference type. A reference type is non static class or interface or a suitably constrained type parameter.
  2607. /// A type parameter for which MustBeReferenceType (the class constraint in C#) is true returns true for this property
  2608. /// as does a type parameter with a constraint that is a class.
  2609. /// </summary>
  2610. /// <value></value>
  2611. public bool IsReferenceType {
  2612. get { return this.UnspecializedVersion.IsReferenceType; }
  2613. }
  2614. /// <summary>
  2615. /// True if the type may not be subtyped.
  2616. /// </summary>
  2617. /// <value></value>
  2618. public bool IsSealed {
  2619. get { return this.UnspecializedVersion.IsSealed; }
  2620. }
  2621. /// <summary>
  2622. /// True if the type is an abstract sealed class that directly extends System.Object and declares no constructors.
  2623. /// </summary>
  2624. /// <value></value>
  2625. public bool IsStatic {
  2626. get { return this.UnspecializedVersion.IsStatic; }
  2627. }
  2628. /// <summary>
  2629. /// True if the type is a value type.
  2630. /// Value types are sealed and extend System.ValueType or System.Enum.
  2631. /// A type parameter for which MustBeValueType (the struct constraint in C#) is true also returns true for this property.
  2632. /// </summary>
  2633. /// <value></value>
  2634. public bool IsValueType {
  2635. get { return this.UnspecializedVersion.IsValueType; }
  2636. }
  2637. /// <summary>
  2638. /// True if the type is a struct (its not Primitive, is sealed and base is System.ValueType).
  2639. /// </summary>
  2640. /// <value></value>
  2641. public bool IsStruct {
  2642. get { return this.UnspecializedVersion.IsStruct; }
  2643. }
  2644. /// <summary>
  2645. /// Size of an object of this type. In bytes. If zero, the size is unspecified and will be determined at runtime.
  2646. /// </summary>
  2647. /// <value></value>
  2648. public uint SizeOf {
  2649. get { return this.UnspecializedVersion.SizeOf; }
  2650. }
  2651. /// <summary>
  2652. /// Declarative security actions for this type. Will be empty if this.HasSecurity is false.
  2653. /// </summary>
  2654. /// <value></value>
  2655. public IEnumerable<ISecurityAttribute> SecurityAttributes {
  2656. get
  2657. //^^ requires this.HasSecurityAttributes;
  2658. {
  2659. return this.UnspecializedVersion.SecurityAttributes;
  2660. }
  2661. }
  2662. public ITypeDefinitionMember SpecializeMember(ITypeDefinitionMember unspecializedMember, IInternFactory internFactory)
  2663. //^ requires unspecializedMember is IEventDefinition || unspecializedMember is IFieldDefinition || unspecializedMember is IMethodDefinition ||
  2664. //^ unspecializedMember is IPropertyDefinition || unspecializedMember is INestedTypeDefinition;
  2665. //^ ensures unspecializedMember is IEventDefinition ==> result is IEventDefinition;
  2666. //^ ensures unspecializedMember is IFieldDefinition ==> result is IFieldDefinition;
  2667. //^ ensures unspecializedMember is IMethodDefinition ==> result is IMethodDefinition;
  2668. //^ ensures unspecializedMember is IPropertyDefinition ==> result is IPropertyDefinition;
  2669. //^ ensures unspecializedMember is INestedTypeDefinition ==> result is INestedTypeDefinition;
  2670. {
  2671. IEventDefinition/*?*/ eventDef = unspecializedMember as IEventDefinition;
  2672. if (eventDef != null) {
  2673. var unspecializedEventDef = eventDef;
  2674. var specializedEventDef = eventDef as ISpecializedEventDefinition;
  2675. if (specializedEventDef != null) unspecializedEventDef = specializedEventDef.UnspecializedVersion;
  2676. return new SpecializedEventDefinition(unspecializedEventDef, eventDef, this, this.ContainingGenericTypeInstance);
  2677. }
  2678. IFieldDefinition/*?*/ fieldDef = unspecializedMember as IFieldDefinition;
  2679. if (fieldDef != null) {
  2680. var unspecializedFieldDef = fieldDef;
  2681. var specializedFieldDef = fieldDef as ISpecializedFieldDefinition;
  2682. if (specializedFieldDef != null) unspecializedFieldDef = specializedFieldDef.UnspecializedVersion;
  2683. return new SpecializedFieldDefinition(unspecializedFieldDef, fieldDef, this, this.ContainingGenericTypeInstance);
  2684. }
  2685. IMethodDefinition/*?*/ methodDef = unspecializedMember as IMethodDefinition;
  2686. if (methodDef != null) {
  2687. var unspecializedMethodDef = methodDef;
  2688. var specializedMethodDef = methodDef as ISpecializedMethodDefinition;
  2689. if (specializedMethodDef != null) unspecializedMethodDef = specializedMethodDef.UnspecializedVersion;
  2690. return new SpecializedMethodDefinition(unspecializedMethodDef, methodDef, this, this.ContainingGenericTypeInstance);
  2691. }
  2692. IPropertyDefinition/*?*/ propertyDef = unspecializedMember as IPropertyDefinition;
  2693. if (propertyDef != null) {
  2694. var unspecializedPropertyDef = propertyDef;
  2695. var specializedPropertyDef = propertyDef as ISpecializedPropertyDefinition;
  2696. if (specializedPropertyDef != null) unspecializedPropertyDef = specializedPropertyDef.UnspecializedVersion;
  2697. return new SpecializedPropertyDefinition(unspecializedPropertyDef, propertyDef, this, this.ContainingGenericTypeInstance);
  2698. }
  2699. //^ assert unspecializedMember is INestedTypeDefinition;
  2700. INestedTypeDefinition nestedTypeDef = (INestedTypeDefinition)unspecializedMember;
  2701. var unspecializedTypeDef = nestedTypeDef;
  2702. var specializedTypeDef = nestedTypeDef as ISpecializedNestedTypeDefinition;
  2703. if (specializedTypeDef != null) unspecializedTypeDef = specializedTypeDef.UnspecializedVersion;
  2704. return new SpecializedNestedTypeDefinition(unspecializedTypeDef, nestedTypeDef, this, this.ContainingGenericTypeInstance, internFactory);
  2705. }
  2706. /// <summary>
  2707. /// Unless the value of TypeCode is PrimitiveTypeCode.NotPrimitive, the type corresponds to a "primitive" CLR type (such as System.Int32) and
  2708. /// the type code identifies which of the primitive types it corresponds to.
  2709. /// </summary>
  2710. /// <value></value>
  2711. public PrimitiveTypeCode TypeCode {
  2712. get { return this.UnspecializedVersion.TypeCode; }
  2713. }
  2714. public LayoutKind Layout {
  2715. get { return this.UnspecializedVersion.Layout; }
  2716. }
  2717. public bool IsSpecialName {
  2718. get { return this.UnspecializedVersion.IsSpecialName; }
  2719. }
  2720. public bool IsComObject {
  2721. get { return this.UnspecializedVersion.IsComObject; }
  2722. }
  2723. public bool IsSerializable {
  2724. get { return this.UnspecializedVersion.IsSerializable; }
  2725. }
  2726. public bool IsBeforeFieldInit {
  2727. get { return this.UnspecializedVersion.IsBeforeFieldInit; }
  2728. }
  2729. public StringFormatKind StringFormat {
  2730. get { return this.UnspecializedVersion.StringFormat; }
  2731. }
  2732. public bool IsRuntimeSpecial {
  2733. get { return this.UnspecializedVersion.IsRuntimeSpecial; }
  2734. }
  2735. public bool HasDeclarativeSecurity {
  2736. get
  2737. //^ ensures result == this.UnspecializedVersion.HasDeclarativeSecurity;
  2738. {
  2739. return this.UnspecializedVersion.HasDeclarativeSecurity;
  2740. }
  2741. }
  2742. /// <summary>
  2743. /// Returns a <see cref="System.String"/> that represents this instance.
  2744. /// </summary>
  2745. /// <returns>
  2746. /// A <see cref="System.String"/> that represents this instance.
  2747. /// </returns>
  2748. public override string ToString() {
  2749. return TypeHelper.GetTypeName(this);
  2750. }
  2751. #endregion
  2752. #region IDefinition Members
  2753. public IEnumerable<ICustomAttribute> Attributes {
  2754. get { return this.UnspecializedVersion.Attributes; }
  2755. }
  2756. public IEnumerable<ILocation> Locations {
  2757. get { return this.UnspecializedVersion.Locations; }
  2758. }
  2759. #endregion
  2760. #region IDoubleDispatcher Members
  2761. /// <summary>
  2762. /// Calls visitor.Visit(INestedTypeDefinition).
  2763. /// </summary>
  2764. public void Dispatch(IMetadataVisitor visitor) {
  2765. visitor.Visit(this);
  2766. }
  2767. /// <summary>
  2768. /// Calls visitor.Visit(INestedTypeReference).
  2769. /// </summary>
  2770. public void DispatchAsReference(IMetadataVisitor visitor) {
  2771. visitor.Visit((ISpecializedNestedTypeReference)this);
  2772. }
  2773. #endregion
  2774. #region IContainer<ITypeDefinitionMember>
  2775. IEnumerable<ITypeDefinitionMember> IContainer<ITypeDefinitionMember>.Members {
  2776. get {
  2777. return this.Members;
  2778. }
  2779. }
  2780. #endregion
  2781. #region IContainerMember<ITypeDefinition> Members
  2782. public ITypeDefinition Container {
  2783. get { return this.ContainingTypeDefinition; }
  2784. }
  2785. #endregion
  2786. #region INamedEntity Members
  2787. public IName Name {
  2788. get { return this.UnspecializedVersion.Name; }
  2789. }
  2790. #endregion
  2791. #region IScopeMember<IScope<ITypeDefinitionMember>> Members
  2792. public IScope<ITypeDefinitionMember> ContainingScope {
  2793. get { return this.ContainingTypeDefinition; }
  2794. }
  2795. #endregion
  2796. #region ITypeReference Members
  2797. public bool IsAlias {
  2798. get { return false; }
  2799. }
  2800. public IAliasForType AliasForType {
  2801. get { return Dummy.AliasForType; }
  2802. }
  2803. ITypeDefinition ITypeReference.ResolvedType {
  2804. get { return this; }
  2805. }
  2806. #endregion
  2807. #region ITypeMemberReference Members
  2808. ITypeReference ITypeMemberReference.ContainingType {
  2809. get { return this.ContainingTypeDefinition; }
  2810. }
  2811. public ITypeDefinitionMember ResolvedTypeDefinitionMember {
  2812. get { return this; }
  2813. }
  2814. #endregion
  2815. #region INestedTypeReference Members
  2816. INestedTypeDefinition INestedTypeReference.ResolvedType {
  2817. get { return this; }
  2818. }
  2819. #endregion
  2820. #region ITypeReference Members
  2821. /// <summary>
  2822. /// The intern factory to use for computing the interned identity of this type and any types and members referenced by it.
  2823. /// </summary>
  2824. public IInternFactory InternFactory {
  2825. get { return this.internFactory; }
  2826. }
  2827. readonly IInternFactory internFactory;
  2828. public uint InternedKey {
  2829. get {
  2830. if (this.internedKey == 0) {
  2831. this.internedKey = this.InternFactory.GetTypeReferenceInternedKey(this);
  2832. }
  2833. return this.internedKey;
  2834. }
  2835. }
  2836. uint internedKey;
  2837. #endregion
  2838. #region ISpecializedNestedTypeReference Members
  2839. INestedTypeReference ISpecializedNestedTypeReference.UnspecializedVersion {
  2840. get { return this.UnspecializedVersion; }
  2841. }
  2842. #endregion
  2843. #region INamedTypeReference Members
  2844. public bool MangleName {
  2845. get { return this.UnspecializedVersion.MangleName; }
  2846. }
  2847. public INamedTypeDefinition ResolvedType {
  2848. get { return this; }
  2849. }
  2850. #endregion
  2851. }
  2852. public class SpecializedNestedTypeReference : ISpecializedNestedTypeReference {
  2853. /// <summary>
  2854. /// A reference to a field of a generic type instance. It is specialized because any occurrences of the type parameters have been replaced with the
  2855. /// corresponding type arguments from the instance.
  2856. /// </summary>
  2857. /// <param name="unspecializedVersion"></param>
  2858. /// <param name="containingType"></param>
  2859. /// <param name="internFactory"></param>
  2860. public SpecializedNestedTypeReference(INestedTypeReference unspecializedVersion, ITypeReference containingType, IInternFactory internFactory) {
  2861. Contract.Requires(!(unspecializedVersion is ISpecializedNestedTypeReference));
  2862. Contract.Requires(!(unspecializedVersion.ContainingType is ISpecializedNestedTypeReference));
  2863. Contract.Requires(!(unspecializedVersion.ContainingType is IGenericTypeInstanceReference));
  2864. this.unspecializedVersion = unspecializedVersion;
  2865. this.containingType = containingType;
  2866. this.internFactory = internFactory;
  2867. }
  2868. IInternFactory internFactory;
  2869. /// <summary>
  2870. /// The corresponding (unspecialized) member from the generic type (template) that was instantiated to obtain the containing type
  2871. /// of this member.
  2872. /// </summary>
  2873. public INestedTypeReference/*!*/ UnspecializedVersion {
  2874. get { return this.unspecializedVersion; }
  2875. }
  2876. readonly INestedTypeReference/*!*/ unspecializedVersion;
  2877. #region INestedTypeReference Members
  2878. public ushort GenericParameterCount {
  2879. get { return this.UnspecializedVersion.GenericParameterCount; }
  2880. }
  2881. public INestedTypeDefinition ResolvedType {
  2882. get {
  2883. if (this.resolvedType == null)
  2884. this.resolvedType = TypeHelper.GetNestedType(this.ContainingType.ResolvedType, this.Name, this.GenericParameterCount);
  2885. return this.resolvedType;
  2886. }
  2887. }
  2888. INestedTypeDefinition resolvedType;
  2889. public override string ToString() {
  2890. return TypeHelper.GetTypeName(this);
  2891. }
  2892. #endregion
  2893. #region INamedTypeReference Members
  2894. public bool MangleName {
  2895. get { return this.UnspecializedVersion.MangleName; }
  2896. }
  2897. INamedTypeDefinition INamedTypeReference.ResolvedType {
  2898. get {
  2899. if (this.ResolvedType == Dummy.NestedType) return Dummy.NamedTypeDefinition;
  2900. return this.ResolvedType;
  2901. }
  2902. }
  2903. #endregion
  2904. #region ITypeMemberReference Members
  2905. /// <summary>
  2906. /// A reference to the containing type of the referenced type member.
  2907. /// </summary>
  2908. /// <value></value>
  2909. public ITypeReference ContainingType {
  2910. get { return this.containingType; }
  2911. }
  2912. ITypeReference containingType;
  2913. /// <summary>
  2914. /// The type definition member this reference resolves to.
  2915. /// </summary>
  2916. /// <value></value>
  2917. public ITypeDefinitionMember ResolvedTypeDefinitionMember {
  2918. get {
  2919. return this.ResolvedType;
  2920. }
  2921. }
  2922. #endregion
  2923. #region ITypeReference Members
  2924. public IAliasForType AliasForType {
  2925. get { return Dummy.AliasForType; }
  2926. }
  2927. public uint InternedKey {
  2928. get {
  2929. if (this.internedKey == 0)
  2930. this.internedKey = this.internFactory.GetNestedTypeReferenceInternedKey(this.ContainingType, this.Name, this.GenericParameterCount);
  2931. return this.internedKey;
  2932. }
  2933. }
  2934. uint internedKey;
  2935. public bool IsAlias {
  2936. get { return false; }
  2937. }
  2938. public bool IsEnum {
  2939. get { return false; }
  2940. }
  2941. public bool IsValueType {
  2942. get { return this.UnspecializedVersion.IsValueType; }
  2943. }
  2944. public IPlatformType PlatformType {
  2945. get { return this.UnspecializedVersion.PlatformType; }
  2946. }
  2947. ITypeDefinition ITypeReference.ResolvedType {
  2948. get {
  2949. if (this.ResolvedType == Dummy.NestedType) return Dummy.Type;
  2950. return this.ResolvedType;
  2951. }
  2952. }
  2953. public PrimitiveTypeCode TypeCode {
  2954. get { return PrimitiveTypeCode.NotPrimitive; }
  2955. }
  2956. #endregion
  2957. #region IReference Members
  2958. /// <summary>
  2959. /// A collection of metadata custom attributes that are associated with this definition.
  2960. /// </summary>
  2961. /// <value></value>
  2962. public IEnumerable<ICustomAttribute> Attributes {
  2963. get { return this.UnspecializedVersion.Attributes; }
  2964. }
  2965. /// <summary>
  2966. /// Calls visitor.Visit(ISpecializedNestedTypeReference).
  2967. /// </summary>
  2968. public void Dispatch(IMetadataVisitor visitor) {
  2969. visitor.Visit(this);
  2970. }
  2971. /// <summary>
  2972. /// Calls visitor.Visit(ISpecializedNestedTypeReference).
  2973. /// </summary>
  2974. public void DispatchAsReference(IMetadataVisitor visitor) {
  2975. visitor.Visit(this);
  2976. }
  2977. /// <summary>
  2978. /// A potentially empty collection of locations that correspond to this instance.
  2979. /// </summary>
  2980. /// <value></value>
  2981. public IEnumerable<ILocation> Locations {
  2982. get { return this.UnspecializedVersion.Locations; }
  2983. }
  2984. #endregion
  2985. #region INamedEntity Members
  2986. /// <summary>
  2987. /// The name of the entity.
  2988. /// </summary>
  2989. public IName Name {
  2990. get { return this.UnspecializedVersion.Name; }
  2991. }
  2992. #endregion
  2993. }
  2994. public abstract class SystemDefinedStructuralType : ITypeDefinition {
  2995. protected SystemDefinedStructuralType(IInternFactory internFactory) {
  2996. this.internFactory = internFactory;
  2997. }
  2998. #region ITypeDefinition Members
  2999. public ushort Alignment {
  3000. get { return 0; }
  3001. }
  3002. public virtual IEnumerable<ITypeReference> BaseClasses {
  3003. get { return Enumerable<ITypeReference>.Empty; }
  3004. }
  3005. public IEnumerable<IEventDefinition> Events {
  3006. get { return Enumerable<IEventDefinition>.Empty; }
  3007. }
  3008. public IEnumerable<IMethodImplementation> ExplicitImplementationOverrides {
  3009. get { return Enumerable<IMethodImplementation>.Empty; }
  3010. }
  3011. public IEnumerable<IFieldDefinition> Fields {
  3012. get { return Enumerable<IFieldDefinition>.Empty; }
  3013. }
  3014. public IEnumerable<IGenericTypeParameter> GenericParameters {
  3015. get { return Enumerable<IGenericTypeParameter>.Empty; }
  3016. }
  3017. public ushort GenericParameterCount {
  3018. get { return 0; }
  3019. }
  3020. public bool HasDeclarativeSecurity {
  3021. get { return true; }
  3022. }
  3023. public virtual IEnumerable<ITypeReference> Interfaces {
  3024. get { return Enumerable<ITypeReference>.Empty; }
  3025. }
  3026. public IGenericTypeInstanceReference InstanceType {
  3027. get { return Dummy.GenericTypeInstance; }
  3028. }
  3029. public bool IsAbstract {
  3030. get { return false; }
  3031. }
  3032. public bool IsBeforeFieldInit {
  3033. get { return false; }
  3034. }
  3035. public bool IsClass {
  3036. get { return false; }
  3037. }
  3038. public bool IsComObject {
  3039. get { return false; }
  3040. }
  3041. public bool IsDelegate {
  3042. get { return false; }
  3043. }
  3044. public bool IsEnum {
  3045. get { return false; }
  3046. }
  3047. public bool IsGeneric {
  3048. get
  3049. //^ ensures result == false;
  3050. {
  3051. return false;
  3052. }
  3053. }
  3054. public bool IsInterface {
  3055. get { return false; }
  3056. }
  3057. public virtual bool IsReferenceType {
  3058. get { return false; }
  3059. }
  3060. public bool IsRuntimeSpecial {
  3061. get { return false; }
  3062. }
  3063. public bool IsSerializable {
  3064. get { return false; }
  3065. }
  3066. public bool IsSpecialName {
  3067. get { return false; }
  3068. }
  3069. public bool IsStruct {
  3070. get { return false; }
  3071. }
  3072. public bool IsValueType {
  3073. get { return false; }
  3074. }
  3075. public bool IsSealed {
  3076. get { return false; }
  3077. }
  3078. public bool IsStatic {
  3079. get { return false; }
  3080. }
  3081. public LayoutKind Layout {
  3082. get { return LayoutKind.Auto; }
  3083. }
  3084. public virtual IEnumerable<ITypeDefinitionMember> Members {
  3085. get { return Enumerable<ITypeDefinitionMember>.Empty; }
  3086. }
  3087. public IEnumerable<IMethodDefinition> Methods {
  3088. get { return Enumerable<IMethodDefinition>.Empty; }
  3089. }
  3090. public IEnumerable<INestedTypeDefinition> NestedTypes {
  3091. get { return Enumerable<INestedTypeDefinition>.Empty; }
  3092. }
  3093. public abstract IPlatformType PlatformType { get; }
  3094. public IEnumerable<ITypeDefinitionMember> PrivateHelperMembers {
  3095. get { return this.Members; }
  3096. }
  3097. public IEnumerable<IPropertyDefinition> Properties {
  3098. get { return Enumerable<IPropertyDefinition>.Empty; }
  3099. }
  3100. public IEnumerable<ISecurityAttribute> SecurityAttributes {
  3101. get { return Enumerable<ISecurityAttribute>.Empty; }
  3102. }
  3103. public uint SizeOf {
  3104. get { return 0; }
  3105. }
  3106. public StringFormatKind StringFormat {
  3107. get { return StringFormatKind.AutoChar; }
  3108. }
  3109. public virtual PrimitiveTypeCode TypeCode {
  3110. get { return PrimitiveTypeCode.NotPrimitive; }
  3111. }
  3112. public ITypeReference UnderlyingType {
  3113. get { return Dummy.TypeReference; }
  3114. }
  3115. #endregion
  3116. #region IDefinition Members
  3117. public IEnumerable<ICustomAttribute> Attributes {
  3118. get { return Enumerable<ICustomAttribute>.Empty; }
  3119. }
  3120. /// <summary>
  3121. /// Calls the visitor.Visit(T) method where T is the most derived object model node interface type implemented by the concrete type
  3122. /// of the object implementing IReference. The dispatch method does nothing else.
  3123. /// </summary>
  3124. public abstract void Dispatch(IMetadataVisitor visitor);
  3125. /// <summary>
  3126. /// Calls the visitor.Visit(T) method where T is the most derived object model node interface type implemented by the concrete type
  3127. /// of the object implementing IReference, which is not derived from IDefinition. For example an object implemeting IArrayType will
  3128. /// call visitor.Visit(IArrayTypeReference) and not visitor.Visit(IArrayType).
  3129. /// The dispatch method does nothing else.
  3130. /// </summary>
  3131. public abstract void DispatchAsReference(IMetadataVisitor visitor);
  3132. public IEnumerable<ILocation> Locations {
  3133. get { return Enumerable<ILocation>.Empty; }
  3134. }
  3135. #endregion
  3136. #region IScope<ITypeDefinitionMember> Members
  3137. [Pure]
  3138. public virtual bool Contains(ITypeDefinitionMember member) {
  3139. return false;
  3140. }
  3141. public virtual IEnumerable<ITypeDefinitionMember> GetMatchingMembersNamed(IName name, bool ignoreCase, Function<ITypeDefinitionMember, bool> predicate) {
  3142. return Enumerable<ITypeDefinitionMember>.Empty;
  3143. }
  3144. public virtual IEnumerable<ITypeDefinitionMember> GetMatchingMembers(Function<ITypeDefinitionMember, bool> predicate) {
  3145. return Enumerable<ITypeDefinitionMember>.Empty;
  3146. }
  3147. public virtual IEnumerable<ITypeDefinitionMember> GetMembersNamed(IName name, bool ignoreCase) {
  3148. return Enumerable<ITypeDefinitionMember>.Empty;
  3149. }
  3150. [Pure]
  3151. IEnumerable<ITypeDefinitionMember> IScope<ITypeDefinitionMember>.GetMatchingMembersNamed(IName name, bool ignoreCase, Function<ITypeDefinitionMember, bool> predicate) {
  3152. return Enumerable<ITypeDefinitionMember>.Empty;
  3153. }
  3154. [Pure]
  3155. IEnumerable<ITypeDefinitionMember> IScope<ITypeDefinitionMember>.GetMatchingMembers(Function<ITypeDefinitionMember, bool> predicate) {
  3156. return Enumerable<ITypeDefinitionMember>.Empty;
  3157. }
  3158. [Pure]
  3159. IEnumerable<ITypeDefinitionMember> IScope<ITypeDefinitionMember>.GetMembersNamed(IName name, bool ignoreCase) {
  3160. return Enumerable<ITypeDefinitionMember>.Empty;
  3161. }
  3162. #endregion
  3163. #region ITypeReference Members
  3164. public bool IsAlias {
  3165. get { return false; }
  3166. }
  3167. public IAliasForType AliasForType {
  3168. get { return Dummy.AliasForType; }
  3169. }
  3170. public virtual IEnumerable<ICustomModifier> CustomModifiers {
  3171. get { return Enumerable<ICustomModifier>.Empty; }
  3172. }
  3173. public virtual bool IsModified {
  3174. get { return false; }
  3175. }
  3176. public ITypeDefinition ResolvedType {
  3177. get { return this; }
  3178. }
  3179. /// <summary>
  3180. /// A collection of methods that associate unique integers with metadata model entities.
  3181. /// The association is based on the identities of the entities and the factory does not retain
  3182. /// references to the given metadata model objects.
  3183. /// </summary>
  3184. public IInternFactory InternFactory {
  3185. get { return this.internFactory; }
  3186. }
  3187. readonly IInternFactory internFactory;
  3188. public uint InternedKey {
  3189. get {
  3190. if (this.internedKey == 0) {
  3191. this.internedKey = this.InternFactory.GetTypeReferenceInternedKey(this);
  3192. }
  3193. return this.internedKey;
  3194. }
  3195. }
  3196. uint internedKey;
  3197. #endregion
  3198. }
  3199. internal static class TypeDefinition {
  3200. /// <summary>
  3201. /// Returns a deep copy of partiallySpecializedTypeReference. In the copy, every reference to a partially specialized type parameter
  3202. /// of targetContainer or of one of targetContainer's parents (if the parent is a SpecializedNestedTypeDefinition and generic) is
  3203. /// replaced with the specialized type parameter defined by targetContainer or its parents.
  3204. /// </summary>
  3205. /// <remarks>
  3206. /// The deep copy happens when we create a specialized member, such as a specialized method, property, field, parameters and so on.
  3207. /// We must obtain a copy of any type reference in the member, which should not share nodes with the original type reference (called
  3208. /// the partially specialized version) if the node contains generic parameters that may get specialized. Without such a copy,
  3209. /// the instantiation of a generic type or method will not work. In the new copy, the references to (partially specialized) generic
  3210. /// parameters must be replaced by the specialized version defined by the specialized parents of the specialized member.
  3211. ///
  3212. /// For example, consider A[int].B[T1 => T1+], where A[int] is a generic type instance which contains a specialized nested type
  3213. /// B, which has a generic type parameter T1 that is specialized to T1+. Now any type reference inside B[T1=>T1+], such as types
  3214. /// of fields, methods, properties, and so on, must be copied and have any reference to T1 replaced by T1+.
  3215. ///
  3216. /// Similar deep copy happens when specializing a method definition. <seealso cref="DeepCopyTypeReferenceWRTSpecializedMethod"/>.
  3217. /// </remarks>
  3218. /// <param name="partiallySpecializedTypeReference">A type reference to be deep copied. </param>
  3219. /// <param name="targetContainer">A specialized nested type definition whose or whose parents' (specialized) type parameters will replace the occurrences
  3220. /// of matching type parameters in partiallySpecializedTypeReference. </param>
  3221. /// <param name="internFactory">An intern factory.</param>
  3222. internal static ITypeReference DeepCopyTypeReference(ITypeReference partiallySpecializedTypeReference, SpecializedNestedTypeDefinition targetContainer, IInternFactory internFactory) {
  3223. var arrayType = partiallySpecializedTypeReference as IArrayTypeReference;
  3224. if (arrayType != null) {
  3225. if (arrayType.IsVector) return Vector.DeepCopyTypeReference(arrayType, targetContainer, internFactory);
  3226. return Matrix.DeepCopyTypeReference(arrayType, targetContainer, internFactory);
  3227. }
  3228. var genericTypeParameter = partiallySpecializedTypeReference as IGenericTypeParameterReference;
  3229. if (genericTypeParameter != null) return GenericParameter.DeepCopyTypeReference(genericTypeParameter, targetContainer);
  3230. var genericTypeInstance = partiallySpecializedTypeReference as IGenericTypeInstanceReference;
  3231. if (genericTypeInstance != null) return GenericTypeInstance.DeepCopyTypeReference(genericTypeInstance, targetContainer, internFactory);
  3232. var managedPointerType = partiallySpecializedTypeReference as IManagedPointerTypeReference;
  3233. if (managedPointerType != null) return ManagedPointerType.DeepCopyTypeReference(managedPointerType, targetContainer, internFactory);
  3234. var modifiedPointer = partiallySpecializedTypeReference as ModifiedPointerType;
  3235. if (modifiedPointer != null) return ModifiedPointerType.DeepCopyTypeReference(modifiedPointer, targetContainer, internFactory);
  3236. var modifiedType = partiallySpecializedTypeReference as IModifiedTypeReference;
  3237. if (modifiedType != null) return ModifiedTypeReference.DeepCopyTypeReference(modifiedType, targetContainer, internFactory);
  3238. var nestedType = partiallySpecializedTypeReference as INestedTypeReference;
  3239. if (nestedType != null) return SpecializedNestedTypeDefinition.DeepCopyTypeReference(nestedType, targetContainer, internFactory);
  3240. var pointerType = partiallySpecializedTypeReference as IPointerTypeReference;
  3241. if (pointerType != null) return PointerType.DeepCopyTypeReference(pointerType, targetContainer, internFactory);
  3242. return partiallySpecializedTypeReference;
  3243. }
  3244. /// <summary>
  3245. /// If the given partially specialized type reference is a constructed type, such as an instance of IArrayTypeReference or IPointerTypeReference
  3246. /// or IGenericTypeInstanceReference or INestedTypeReference, then return a new instance (if necessary) in which all refererences to the type
  3247. /// parameters of containingMethodInstance.GenericMethod.GenericParameters have been replaced with the corresponding values from containingMethodInstance.GenericArguments.
  3248. /// If the type is not a constructed type the method just returns the type. For the purpose of this method, an instance of IGenericParameter is regarded as a constructed type.
  3249. /// </summary>
  3250. internal static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(ITypeReference partiallySpecializedTypeReference, IGenericMethodInstanceReference containingMethodInstance, IInternFactory internFactory) {
  3251. var arrayType = partiallySpecializedTypeReference as IArrayTypeReference;
  3252. if (arrayType != null) {
  3253. if (arrayType.IsVector) return Vector.SpecializeIfConstructedFromApplicableTypeParameter(arrayType, containingMethodInstance, internFactory);
  3254. return Matrix.SpecializeIfConstructedFromApplicableTypeParameter(arrayType, containingMethodInstance, internFactory);
  3255. }
  3256. var genericMethodParameter = partiallySpecializedTypeReference as IGenericMethodParameterReference;
  3257. if (genericMethodParameter != null) return GenericParameter.SpecializeIfConstructedFromApplicableTypeParameter(genericMethodParameter, containingMethodInstance);
  3258. var genericTypeInstance = partiallySpecializedTypeReference as IGenericTypeInstanceReference;
  3259. if (genericTypeInstance != null) return GenericTypeInstance.SpecializeIfConstructedFromApplicableTypeParameter(genericTypeInstance, containingMethodInstance, internFactory);
  3260. var managedPointerType = partiallySpecializedTypeReference as IManagedPointerTypeReference;
  3261. if (managedPointerType != null) return ManagedPointerType.SpecializeIfConstructedFromApplicableTypeParameter(managedPointerType, containingMethodInstance, internFactory);
  3262. var modifiedPointer = partiallySpecializedTypeReference as ModifiedPointerType;
  3263. if (modifiedPointer != null) return ModifiedPointerType.SpecializeIfConstructedFromApplicableTypeParameter(modifiedPointer, containingMethodInstance, internFactory);
  3264. var modifiedType = partiallySpecializedTypeReference as IModifiedTypeReference;
  3265. if (modifiedType != null) return ModifiedTypeReference.SpecializeIfConstructedFromApplicableTypeParameter(modifiedType, containingMethodInstance, internFactory);
  3266. var nestedType = partiallySpecializedTypeReference as INestedTypeReference;
  3267. if (nestedType != null) return SpecializedNestedTypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(nestedType, containingMethodInstance, internFactory);
  3268. var pointerType = partiallySpecializedTypeReference as IPointerTypeReference;
  3269. if (pointerType != null) return PointerType.SpecializeIfConstructedFromApplicableTypeParameter(pointerType, containingMethodInstance, internFactory);
  3270. return partiallySpecializedTypeReference;
  3271. }
  3272. /// <summary>
  3273. /// If the given partially specialized type reference is a constructed type, such as an instance of IArrayType or IPointerType or
  3274. /// IGenericTypeInstance or INestedTypeReference, then return a new instance (if necessary) in which all refererences to the type
  3275. /// parameters of containingTypeInstance.GenericType have been replaced with the corresponding values from containingTypeInstance.GenericArguments.
  3276. /// If the type is not a constructed type the method just returns the type. For the purpose of this method, an instance of IGenericTypeParameterReference is regarded as a constructed type.
  3277. /// </summary>
  3278. internal static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(ITypeReference partiallySpecializedTypeReference, IGenericTypeInstanceReference containingTypeInstance, IInternFactory internFactory) {
  3279. var arrayType = partiallySpecializedTypeReference as IArrayTypeReference;
  3280. if (arrayType != null) {
  3281. if (arrayType.IsVector) return Vector.SpecializeIfConstructedFromApplicableTypeParameter(arrayType, containingTypeInstance, internFactory);
  3282. return Matrix.SpecializeIfConstructedFromApplicableTypeParameter(arrayType, containingTypeInstance, internFactory);
  3283. }
  3284. var genericTypeParameter = partiallySpecializedTypeReference as IGenericTypeParameterReference;
  3285. if (genericTypeParameter != null) return GenericParameter.SpecializeIfConstructedFromApplicableTypeParameter(genericTypeParameter, containingTypeInstance);
  3286. var genericTypeInstance = partiallySpecializedTypeReference as IGenericTypeInstanceReference;
  3287. if (genericTypeInstance != null) return GenericTypeInstance.SpecializeIfConstructedFromApplicableTypeParameter(genericTypeInstance, containingTypeInstance, internFactory);
  3288. var managedPointerType = partiallySpecializedTypeReference as IManagedPointerTypeReference;
  3289. if (managedPointerType != null) return ManagedPointerType.SpecializeIfConstructedFromApplicableTypeParameter(managedPointerType, containingTypeInstance, internFactory);
  3290. var modifiedPointer = partiallySpecializedTypeReference as ModifiedPointerType;
  3291. if (modifiedPointer != null) return ModifiedPointerType.SpecializeIfConstructedFromApplicableTypeParameter(modifiedPointer, containingTypeInstance, internFactory);
  3292. var modifiedType = partiallySpecializedTypeReference as IModifiedTypeReference;
  3293. if (modifiedType != null) return ModifiedTypeReference.SpecializeIfConstructedFromApplicableTypeParameter(modifiedType, containingTypeInstance, internFactory);
  3294. var nestedType = partiallySpecializedTypeReference as INestedTypeReference;
  3295. if (nestedType != null) return SpecializedNestedTypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(nestedType, containingTypeInstance, internFactory);
  3296. var pointerType = partiallySpecializedTypeReference as IPointerTypeReference;
  3297. if (pointerType != null) return PointerType.SpecializeIfConstructedFromApplicableTypeParameter(pointerType, containingTypeInstance, internFactory);
  3298. return partiallySpecializedTypeReference;
  3299. }
  3300. /// <summary>
  3301. /// Make a copy of partiallySpecializedTypeReference so that the references to the partially specialized version of generic parameters defined in
  3302. /// specializedMethodDefinition and its containing specialized nested type definition (if any) are replaced with the specialized version.
  3303. /// </summary>
  3304. /// <remarks>
  3305. /// Consider specialized method A[T1 -> T1].B[T2=> T2+].bar[T3 => T3+], where A[T1->T1] is the generic instance obtained from A using its generic parameter
  3306. /// as the generic argument, A[T1->T1].B[T2=>T2+] is a specialized nested type whose containing type is A[T1->T1] and whose generic type parameter
  3307. /// T2 is specialized to T2+, and bar[T3=>T3] is further a specialized member of A[T1->T1].B[T2=>T2+] with its generic parameter T3 specialized to
  3308. /// T3+.
  3309. ///
  3310. /// Suppose bar has a parameter whose type is X[T2, T3] for some generic type X. The corresponding parameter of of A[T1 -> T1].B[T2=> T2+].bar[T3 => T3+]
  3311. /// is "specialized". The specialized parameter's type will participate future resolution and should not share nodes that may have been specialized with
  3312. /// the original type reference X[T2, T3] (called partially specialized version). We must obtain a copy of the partially specialized version in which we replace
  3313. /// the references of T2, T3 with their corresponding specialized versions, namely T2+ and T3+.
  3314. /// </remarks>
  3315. internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(ITypeReference partiallySpecializedTypeReference, ISpecializedMethodReference specializedMethodReference, IInternFactory internFactory) {
  3316. var arrayType = partiallySpecializedTypeReference as IArrayTypeReference;
  3317. if (arrayType != null) {
  3318. if (arrayType.IsVector) return Vector.DeepCopyTypeReferenceWRTSpecializedMethod(arrayType, specializedMethodReference, internFactory);
  3319. return Matrix.DeepCopyTypeReferenceWRTSpecializedMethod(arrayType, specializedMethodReference, internFactory);
  3320. }
  3321. var genericMethodParameter = partiallySpecializedTypeReference as IGenericMethodParameterReference;
  3322. if (genericMethodParameter != null) return GenericParameter.DeepCopyTypeReferenceWRTSpecializedMethod(genericMethodParameter, specializedMethodReference);
  3323. var genericTypeParameter = partiallySpecializedTypeReference as IGenericTypeParameterReference;
  3324. if (genericTypeParameter != null) return GenericParameter.DeepCopyTypeReferenceWRTSpecializedMethod(genericTypeParameter, specializedMethodReference);
  3325. var genericTypeInstance = partiallySpecializedTypeReference as IGenericTypeInstanceReference;
  3326. if (genericTypeInstance != null) return GenericTypeInstance.DeepCopyTypeReferenceWRTSpecializedMethod(genericTypeInstance, specializedMethodReference, internFactory);
  3327. var managedPointerType = partiallySpecializedTypeReference as IManagedPointerTypeReference;
  3328. if (managedPointerType != null) return ManagedPointerType.DeepCopyTypeReferenceWRTSpecializedMethod(managedPointerType, specializedMethodReference, internFactory);
  3329. var modifiedPointer = partiallySpecializedTypeReference as ModifiedPointerType;
  3330. if (modifiedPointer != null) return ModifiedPointerType.DeepCopyTypeReferenceWRTSpecializedMethod(modifiedPointer, specializedMethodReference, internFactory);
  3331. var modifiedType = partiallySpecializedTypeReference as IModifiedTypeReference;
  3332. if (modifiedType != null) return ModifiedTypeReference.DeepCopyTypeReferenceWRTSpecializedMethod(modifiedType, specializedMethodReference, internFactory);
  3333. var pointerType = partiallySpecializedTypeReference as IPointerTypeReference;
  3334. if (pointerType != null) return PointerType.DeepCopyTypeReferenceReplacingGenericMethodParameter(pointerType, specializedMethodReference, internFactory);
  3335. var nestedType = partiallySpecializedTypeReference as INestedTypeReference;
  3336. if (nestedType != null) return SpecializedNestedTypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(nestedType, specializedMethodReference, internFactory);
  3337. return partiallySpecializedTypeReference;
  3338. }
  3339. }
  3340. public class Vector : ArrayType {
  3341. protected Vector(ITypeReference elementType, IInternFactory internFactory)
  3342. : base(elementType, internFactory) {
  3343. }
  3344. protected override IEnumerable<ITypeReference> GetInterfaceList() {
  3345. var coreAssembly = TypeHelper.GetDefiningUnitReference(this.PlatformType.SystemArray) as IAssemblyReference;
  3346. var version = coreAssembly == null ? new Version(4, 0) : coreAssembly.AssemblyIdentity.Version;
  3347. List<ITypeReference> interfaces = new List<ITypeReference>(9);
  3348. interfaces.Add(this.PlatformType.SystemICloneable);
  3349. interfaces.Add(this.PlatformType.SystemCollectionsIEnumerable);
  3350. interfaces.Add(this.PlatformType.SystemCollectionsICollection);
  3351. interfaces.Add(this.PlatformType.SystemCollectionsIList);
  3352. if (version.Major >= 4) {
  3353. interfaces.Add(this.PlatformType.SystemCollectionsIStructuralComparable);
  3354. interfaces.Add(this.PlatformType.SystemCollectionsIStructuralEquatable);
  3355. }
  3356. if (version.Major >= 2) {
  3357. var argTypes = IteratorHelper.GetSingletonEnumerable<ITypeReference>(this.ElementType);
  3358. interfaces.Add(GenericTypeInstance.GetGenericTypeInstance(this.PlatformType.SystemCollectionsGenericIList, argTypes, this.InternFactory));
  3359. interfaces.Add(GenericTypeInstance.GetGenericTypeInstance(this.PlatformType.SystemCollectionsGenericICollection, argTypes, this.InternFactory));
  3360. interfaces.Add(GenericTypeInstance.GetGenericTypeInstance(this.PlatformType.SystemCollectionsGenericIEnumerable, argTypes, this.InternFactory));
  3361. }
  3362. return interfaces.AsReadOnly();
  3363. }
  3364. /// <summary>
  3365. /// Returns a deep copy the array type reference. In the copy, every reference to a partially specialized type parameter defined by
  3366. /// the partially specialized version of targetContainer or of one of targetContainer's parents (if the parent is a SpecializedNestedTypeDefinition
  3367. /// and generic) will be replaced with the specialized type parameter, defined by targetContainer or its parents.
  3368. /// </summary>
  3369. /// <param name="array">An array type reference to be deep copied. </param>
  3370. /// <param name="targetContainer">A specialized nested type definition whose or whose parents' (specialized) type parameters will
  3371. /// replace the occurrences of matching type parameters in <paramref name="array"/>.</param>
  3372. /// /// <param name="internFactory">An intern factory. </param>
  3373. public static ITypeReference DeepCopyTypeReference(IArrayTypeReference array, SpecializedNestedTypeDefinition targetContainer, IInternFactory internFactory)
  3374. //^ requires array.IsVector;
  3375. {
  3376. ITypeReference elementType = array.ElementType;
  3377. ITypeReference specializedElementType = TypeDefinition.DeepCopyTypeReference(elementType, targetContainer, internFactory);
  3378. if (elementType == specializedElementType) return array;
  3379. return GetVector(specializedElementType, internFactory);
  3380. }
  3381. internal static ITypeReference SpecializeTypeReference(IArrayTypeReference array, ITypeReference targetContainer, IInternFactory internFactory)
  3382. //^ requires array.IsVector;
  3383. {
  3384. ITypeReference elementType = array.ElementType;
  3385. ITypeReference specializedElementType = TypeHelper.SpecializeTypeReference(elementType, targetContainer, internFactory);
  3386. if (elementType == specializedElementType) return array;
  3387. return GetVector(specializedElementType, internFactory);
  3388. }
  3389. internal static ITypeReference SpecializeTypeReference(IArrayTypeReference array, IMethodReference targetContainer, IInternFactory internFactory)
  3390. //^ requires array.IsVector;
  3391. {
  3392. ITypeReference elementType = array.ElementType;
  3393. ITypeReference specializedElementType = TypeHelper.SpecializeTypeReference(elementType, targetContainer, internFactory);
  3394. if (elementType == specializedElementType) return array;
  3395. return GetVector(specializedElementType, internFactory);
  3396. }
  3397. public static Vector GetVector(ITypeReference elementType, IInternFactory internFactory) {
  3398. Contract.Ensures(Contract.Result<Vector>() != null);
  3399. return new Vector(elementType, internFactory);
  3400. }
  3401. /// <summary>
  3402. /// If the given vector has an element type that involves a type parameter from the generic method from which the given method was instantiated,
  3403. /// then return a new vector using an element type that has been specialized with the type arguments of the given generic method instance.
  3404. /// </summary>
  3405. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IArrayTypeReference array, IGenericMethodInstanceReference method, IInternFactory internFactory)
  3406. //^ requires array.IsVector;
  3407. {
  3408. ITypeReference elementType = array.ElementType;
  3409. ITypeReference specializedElementType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(elementType, method, internFactory);
  3410. if (elementType == specializedElementType) return array;
  3411. return GetVector(specializedElementType, internFactory);
  3412. }
  3413. /// <summary>
  3414. /// If the given vector has an element type that involves a type parameter from the generic type from which the given type was instantiated,
  3415. /// then return a new vector using an element type that has been specialized with the type arguments of the given generic type instance.
  3416. /// </summary>
  3417. public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IArrayTypeReference array, IGenericTypeInstanceReference type, IInternFactory internFactory)
  3418. //^ requires array.IsVector;
  3419. {
  3420. ITypeReference elementType = array.ElementType;
  3421. ITypeReference specializedElementType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(elementType, type, internFactory);
  3422. if (elementType == specializedElementType) return array;
  3423. return GetVector(specializedElementType, internFactory);
  3424. }
  3425. /// <summary>
  3426. /// If the given vector has an element type that involves a method type parameter from the partially specialized version of specializedMethodReference,
  3427. /// then return a new vector using an element type that is the corresponding method type parameter from specializedMethodDefinition.
  3428. /// </summary>
  3429. internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(IArrayTypeReference array, ISpecializedMethodReference specializedMethodReference, IInternFactory internFactory)
  3430. //^ requires array.IsVector;
  3431. {
  3432. ITypeReference elementType = array.ElementType;
  3433. ITypeReference specializedElementType = TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(elementType, specializedMethodReference, internFactory);
  3434. if (elementType == specializedElementType) return array;
  3435. return GetVector(specializedElementType, internFactory);
  3436. }
  3437. }
  3438. }
  3439. #pragma warning restore 1591