PageRenderTime 62ms CodeModel.GetById 27ms RepoModel.GetById 0ms 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

Large files files are truncated, but you can click here to view the full 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(IManagedPointerTyp…

Large files files are truncated, but you can click here to view the full file