/Microsoft.CCI/MetadataHelper/Types.cs
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
- //-----------------------------------------------------------------------------
- //
- // Copyright (c) Microsoft. All rights reserved.
- // This code is licensed under the Microsoft Public License.
- // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
- // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
- // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
- // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
- //
- //-----------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Text;
- using System.Diagnostics.Contracts;
- //^ using Microsoft.Contracts;
- #pragma warning disable 1591
- namespace Microsoft.Cci.Immutable {
- public abstract class ArrayType : SystemDefinedStructuralType, IArrayType {
- internal ArrayType(ITypeReference elementType, IInternFactory internFactory)
- : base(internFactory) {
- this.elementType = elementType;
- }
- public override IEnumerable<ITypeReference> BaseClasses {
- get { return IteratorHelper.GetSingletonEnumerable<ITypeReference>(this.PlatformType.SystemArray); }
- }
- [Pure]
- public override bool Contains(ITypeDefinitionMember member) {
- foreach (ITypeDefinitionMember mem in this.Members)
- if (mem == member) return true;
- return false;
- }
- /// <summary>
- /// Calls visitor.Visit(IArrayTypeReference)
- /// </summary>
- public override void Dispatch(IMetadataVisitor visitor) {
- visitor.Visit(this);
- }
- /// <summary>
- /// Calls visitor.Visit(IArrayTypeReference)
- /// </summary>
- public override void DispatchAsReference(IMetadataVisitor visitor) {
- visitor.Visit(this);
- }
- public ITypeReference ElementType {
- get { return this.elementType; }
- }
- readonly ITypeReference elementType;
- protected virtual IEnumerable<ITypeReference> GetInterfaceList() {
- var coreAssembly = TypeHelper.GetDefiningUnitReference(this.PlatformType.SystemArray) as IAssemblyReference;
- var version = coreAssembly == null ? new Version(4, 0) : coreAssembly.AssemblyIdentity.Version;
- List<ITypeReference> interfaces = new List<ITypeReference>(6);
- interfaces.Add(this.PlatformType.SystemICloneable);
- interfaces.Add(this.PlatformType.SystemCollectionsIEnumerable);
- interfaces.Add(this.PlatformType.SystemCollectionsICollection);
- interfaces.Add(this.PlatformType.SystemCollectionsIList);
- if (version.Major >= 2) {
- interfaces.Add(this.PlatformType.SystemCollectionsIStructuralComparable);
- interfaces.Add(this.PlatformType.SystemCollectionsIStructuralEquatable);
- }
- return interfaces.AsReadOnly();
- }
- public override IEnumerable<ITypeDefinitionMember> GetMatchingMembersNamed(IName name, bool ignoreCase, Function<ITypeDefinitionMember, bool> predicate) {
- foreach (ITypeDefinitionMember member in this.Members) {
- if (name.UniqueKey != member.Name.UniqueKey || (ignoreCase && name.UniqueKeyIgnoringCase == member.Name.UniqueKeyIgnoringCase)) {
- if (predicate(member)) yield return member;
- }
- }
- }
- public override IEnumerable<ITypeDefinitionMember> GetMatchingMembers(Function<ITypeDefinitionMember, bool> predicate) {
- foreach (ITypeDefinitionMember member in this.Members) {
- if (predicate(member)) yield return member;
- }
- }
- public override IEnumerable<ITypeDefinitionMember> GetMembersNamed(IName name, bool ignoreCase) {
- foreach (ITypeDefinitionMember member in this.Members) {
- if (name.UniqueKey != member.Name.UniqueKey || (ignoreCase && name.UniqueKeyIgnoringCase == member.Name.UniqueKeyIgnoringCase)) {
- yield return member;
- }
- }
- }
- public override IEnumerable<ITypeReference> Interfaces {
- get {
- if (this.interfaces == null) {
- lock (GlobalLock.LockingObject) {
- if (this.interfaces == null) {
- this.interfaces = this.GetInterfaceList();
- }
- }
- }
- return this.interfaces;
- }
- }
- IEnumerable<ITypeReference>/*?*/ interfaces;
- public override bool IsReferenceType {
- get { return true; }
- }
- public virtual bool IsVector {
- get { return this.Rank == 1; }
- }
- public virtual IEnumerable<int> LowerBounds {
- get { return Enumerable<int>.Empty; }
- }
- public override IEnumerable<ITypeDefinitionMember> Members {
- get { return Enumerable<ITypeDefinitionMember>.Empty; }
- }
- public override IPlatformType PlatformType {
- get { return this.ElementType.PlatformType; }
- }
- public virtual uint Rank {
- get { return 1; }
- }
- public virtual IEnumerable<ulong> Sizes {
- get { return Enumerable<ulong>.Empty; }
- }
- //^ [Confined]
- public override string ToString() {
- return TypeHelper.GetTypeName(this, NameFormattingOptions.None);
- }
- #region ITypeDefinition Members
- IEnumerable<ITypeReference> ITypeDefinition.BaseClasses {
- get {
- return this.BaseClasses;
- }
- }
- IEnumerable<IGenericTypeParameter> ITypeDefinition.GenericParameters {
- get { return Enumerable<IGenericTypeParameter>.Empty; }
- }
- ushort ITypeDefinition.GenericParameterCount {
- get {
- //^ assume this.IsGeneric == ((ITypeDefinition)this).IsGeneric;
- return 0;
- }
- }
- #endregion
- #region IContainer<ITypeDefinitionMember> Members
- IEnumerable<ITypeDefinitionMember> IContainer<ITypeDefinitionMember>.Members {
- get { return Enumerable<ITypeDefinitionMember>.Empty; }
- }
- #endregion
- #region IDefinition Members
- IEnumerable<ICustomAttribute> IReference.Attributes {
- get { return Enumerable<ICustomAttribute>.Empty; }
- }
- #endregion
- #region IScope<ITypeDefinitionMember> Members
- [Pure]
- IEnumerable<ITypeDefinitionMember> IScope<ITypeDefinitionMember>.GetMatchingMembersNamed(IName name, bool ignoreCase, Function<ITypeDefinitionMember, bool> predicate) {
- return Enumerable<ITypeDefinitionMember>.Empty;
- }
- [Pure]
- IEnumerable<ITypeDefinitionMember> IScope<ITypeDefinitionMember>.GetMatchingMembers(Function<ITypeDefinitionMember, bool> predicate) {
- return Enumerable<ITypeDefinitionMember>.Empty;
- }
- [Pure]
- IEnumerable<ITypeDefinitionMember> IScope<ITypeDefinitionMember>.GetMembersNamed(IName name, bool ignoreCase) {
- return Enumerable<ITypeDefinitionMember>.Empty;
- }
- IEnumerable<ITypeDefinitionMember> IScope<ITypeDefinitionMember>.Members {
- get { return Enumerable<ITypeDefinitionMember>.Empty; }
- }
- #endregion
- }
- public class CustomModifier : ICustomModifier {
- public CustomModifier(bool isOptional, ITypeReference modifier) {
- this.isOptional = isOptional;
- this.modifier = modifier;
- }
- public bool IsOptional {
- get { return this.isOptional; }
- }
- readonly bool isOptional;
- public ITypeReference Modifier {
- get { return this.modifier; }
- }
- readonly ITypeReference modifier;
- /// <summary>
- /// Returns a deep copy of a customer modifier. In the copy, every reference to a partially specialized type parameter defined by
- /// the partially specialized version of targetContainer or of one of targetContainer's parents (if the parent is a SpecializedNestedTypeDefinition
- /// and generic) will be replaced with the specialized type parameter, defined by targetContainer or its parents.
- /// </summary>
- /// <param name="customModifier">An array type reference to be deep copied. </param>
- /// <param name="targetContainer">A specialized nested type definition whose or whose parents' (specialized) type parameters will
- /// replace the occurrences of matching type parameters in <paramref name="customModifier"/>.</param>
- /// <param name="internFactory">An intern factory. </param>
- internal static ICustomModifier CopyModifierToNewContainer(ICustomModifier customModifier, SpecializedNestedTypeDefinition targetContainer, IInternFactory internFactory) {
- ITypeReference copiedModifier = TypeDefinition.DeepCopyTypeReference(customModifier.Modifier, targetContainer, internFactory);
- if (copiedModifier == customModifier.Modifier) return customModifier;
- return new CustomModifier(customModifier.IsOptional, copiedModifier);
- }
- internal static ICustomModifier Specialize(ICustomModifier customModifier, ITypeReference targetContainer, IInternFactory internFactory) {
- ITypeReference copiedModifier = TypeHelper.SpecializeTypeReference(customModifier.Modifier, targetContainer, internFactory);
- if (copiedModifier == customModifier.Modifier) return customModifier;
- return new CustomModifier(customModifier.IsOptional, copiedModifier);
- }
- internal static ICustomModifier Specialize(ICustomModifier customModifier, IMethodReference targetContainer, IInternFactory internFactory) {
- ITypeReference copiedModifier = TypeHelper.SpecializeTypeReference(customModifier.Modifier, targetContainer, internFactory);
- if (copiedModifier == customModifier.Modifier) return customModifier;
- return new CustomModifier(customModifier.IsOptional, copiedModifier);
- }
- /// <summary>
- /// If the given custom modifier has a modifier that involves a type parameter from the generic method from which the given method was instantiated,
- /// then return a new custom modifier using a modifier type that has been specialized with the type arguments of the given generic method instance.
- /// </summary>
- public static ICustomModifier SpecializeIfConstructedFromApplicableTypeParameter(ICustomModifier customModifier, IGenericMethodInstanceReference containingMethodInstance, IInternFactory internFactory) {
- ITypeReference copiedModifier = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(customModifier.Modifier, containingMethodInstance, internFactory);
- if (copiedModifier == customModifier.Modifier) return customModifier;
- return new CustomModifier(customModifier.IsOptional, copiedModifier);
- }
- /// <summary>
- /// If the given custom modifier has a modifier that involves a type parameter from the generic type from which the given type was instantiated,
- /// then return a new custom modifier using a modifier type that has been specialized with the type arguments of the given generic type instance.
- /// </summary>
- public static ICustomModifier SpecializeIfConstructedFromApplicableTypeParameter(ICustomModifier customModifier, IGenericTypeInstanceReference containingTypeInstance, IInternFactory internFactory) {
- ITypeReference copiedModifier = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(customModifier.Modifier, containingTypeInstance, internFactory);
- if (copiedModifier == customModifier.Modifier) return customModifier;
- return new CustomModifier(customModifier.IsOptional, copiedModifier);
- }
- /// <summary>
- /// If the given custom modifier has a modifier that involves a method type parameter of the partially specialized version of specializedMethodReference,
- /// then return a new custom modifier using a modifier type that is the corresponding method type parameter from specializedMethodReference.
- /// </summary>
- internal static ICustomModifier SpecializeIfConstructedFromApplicableMethodTypeParameter(ICustomModifier customModifier, ISpecializedMethodReference specializedMethodReference, IInternFactory internFactory) {
- ITypeReference copiedModifier = TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(customModifier.Modifier, specializedMethodReference, internFactory);
- if (copiedModifier == customModifier.Modifier) return customModifier;
- return new CustomModifier(customModifier.IsOptional, copiedModifier);
- }
- }
- public class FunctionPointerType : SystemDefinedStructuralType, IFunctionPointer {
- public FunctionPointerType(ISignature signature, IInternFactory internFactory)
- : base(internFactory) {
- this.callingConvention = signature.CallingConvention;
- if (signature.ReturnValueIsModified)
- this.returnValueCustomModifiers = signature.ReturnValueCustomModifiers;
- this.returnValueIsByRef = signature.ReturnValueIsByRef;
- this.type = signature.Type;
- this.parameters = signature.Parameters;
- this.extraArgumentTypes = Enumerable<IParameterTypeInformation>.Empty;
- }
- public FunctionPointerType(CallingConvention callingConvention, bool returnValueIsByRef, ITypeReference type,
- IEnumerable<ICustomModifier>/*?*/ returnValueCustomModifiers, IEnumerable<IParameterTypeInformation> parameters, IEnumerable<IParameterTypeInformation>/*?*/ extraArgumentTypes,
- IInternFactory internFactory)
- : base(internFactory) {
- this.callingConvention = callingConvention;
- this.returnValueCustomModifiers = returnValueCustomModifiers;
- this.returnValueIsByRef = returnValueIsByRef;
- this.type = type;
- this.parameters = parameters;
- if (extraArgumentTypes == null)
- this.extraArgumentTypes = Enumerable<IParameterTypeInformation>.Empty;
- else
- this.extraArgumentTypes = extraArgumentTypes;
- }
- public CallingConvention CallingConvention {
- get { return this.callingConvention; }
- }
- readonly CallingConvention callingConvention;
- /// <summary>
- /// Calls visitor.Visit(IFunctionPointerTypeReference)
- /// </summary>
- public override void Dispatch(IMetadataVisitor visitor) {
- visitor.Visit(this);
- }
- /// <summary>
- /// Calls visitor.Visit(IFunctionPointerTypeReference)
- /// </summary>
- public override void DispatchAsReference(IMetadataVisitor visitor) {
- visitor.Visit(this);
- }
- public IEnumerable<IParameterTypeInformation> ExtraArgumentTypes {
- get { return this.extraArgumentTypes; }
- }
- readonly IEnumerable<IParameterTypeInformation> extraArgumentTypes;
- public override IPlatformType PlatformType {
- get { return this.Type.PlatformType; }
- }
- public IEnumerable<IParameterTypeInformation> Parameters {
- get { return this.parameters; }
- }
- readonly IEnumerable<IParameterTypeInformation> parameters;
- public IEnumerable<ICustomModifier> ReturnValueCustomModifiers {
- get {
- if (this.returnValueCustomModifiers == null) return Enumerable<ICustomModifier>.Empty; //shouldn't get here if the precondition is respected.
- return this.returnValueCustomModifiers;
- }
- }
- readonly IEnumerable<ICustomModifier>/*?*/ returnValueCustomModifiers;
- public bool ReturnValueIsByRef {
- get { return this.returnValueIsByRef; }
- }
- readonly bool returnValueIsByRef;
- public bool ReturnValueIsModified {
- get { return this.returnValueCustomModifiers != null; }
- }
- public ITypeReference Type {
- get { return this.type; }
- }
- readonly ITypeReference type;
- public override PrimitiveTypeCode TypeCode {
- get { return PrimitiveTypeCode.Pointer; }
- }
- #region ISignature Members
- bool ISignature.IsStatic {
- get { return this.IsStatic; }
- }
- ITypeReference ISignature.Type {
- get { return this.Type; }
- }
- #endregion
- }
- public class GenericTypeInstance : Scope<ITypeDefinitionMember>, IGenericTypeInstance {
- public static GenericTypeInstance GetGenericTypeInstance(INamedTypeReference genericType, IEnumerable<ITypeReference> genericArguments, IInternFactory internFactory)
- //^ requires genericType.ResolvedType.IsGeneric;
- //^ ensures !result.IsGeneric;
- {
- Contract.Requires(!(genericType is Dummy));
- Contract.Ensures(Contract.Result<GenericTypeInstance>() != null);
- return new GenericTypeInstance(genericType, genericArguments, internFactory);
- }
- private GenericTypeInstance(INamedTypeReference genericType, IEnumerable<ITypeReference> genericArguments, IInternFactory internFactory)
- //^ requires genericType.ResolvedType.IsGeneric;
- {
- Contract.Requires(!(genericType is Dummy));
- this.genericType = genericType;
- this.genericArguments = genericArguments;
- this.internFactory = internFactory;
- }
- public ushort Alignment {
- get { return this.GenericType.ResolvedType.Alignment; }
- }
- public virtual IEnumerable<ICustomAttribute> Attributes {
- get { return Enumerable<ICustomAttribute>.Empty; }
- }
- public IEnumerable<ITypeReference> BaseClasses {
- get {
- foreach (ITypeReference baseClassRef in this.GenericType.ResolvedType.BaseClasses) {
- ITypeReference specializedBaseClass = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(baseClassRef, this, this.InternFactory);
- yield return specializedBaseClass;
- }
- }
- }
- /// <summary>
- /// Calls this.Visit(IGenericTypeInstanceReference).
- /// </summary>
- public void Dispatch(IMetadataVisitor visitor) {
- visitor.Visit(this);
- }
- /// <summary>
- /// Calls this.Visit(IGenericTypeInstanceReference).
- /// </summary>
- public void DispatchAsReference(IMetadataVisitor visitor) {
- visitor.Visit(this);
- }
- public IEnumerable<IEventDefinition> Events {
- get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, IEventDefinition>(this.Members); }
- }
- public IEnumerable<IFieldDefinition> Fields {
- get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, IFieldDefinition>(this.Members); }
- }
- public IEnumerable<ILocation> Locations {
- get { return Enumerable<ILocation>.Empty; }
- }
- public IEnumerable<IMethodImplementation> ExplicitImplementationOverrides {
- get {
- foreach (var methodImplementation in this.GenericType.ResolvedType.ExplicitImplementationOverrides)
- yield return new SpecializedMethodImplementation(this, methodImplementation, this.InternFactory);
- }
- }
- public IEnumerable<ITypeReference> GenericArguments {
- get { return this.genericArguments; }
- }
- readonly IEnumerable<ITypeReference> genericArguments;
- public INamedTypeReference GenericType {
- get { return this.genericType; }
- }
- readonly INamedTypeReference genericType; //^ invariant genericType.ResolvedType.IsGeneric;
- protected override void InitializeIfNecessary() {
- if (this.initialized) return;
- lock (GlobalLock.LockingObject) {
- if (this.initialized) return;
- foreach (ITypeDefinitionMember unspecializedMember in this.GenericType.ResolvedType.Members) {
- //^ assume unspecializedMember is IEventDefinition || unspecializedMember is IFieldDefinition || unspecializedMember is IMethodDefinition ||
- //^ unspecializedMember is IPropertyDefinition || unspecializedMember is INestedTypeDefinition; //follows from informal post condition on Members property.
- this.AddMemberToCache(this.SpecializeMember(unspecializedMember, this.InternFactory));
- }
- this.initialized = true;
- }
- }
- private bool initialized;
- public IGenericTypeInstanceReference InstanceType {
- get { return this; }
- }
- public IEnumerable<ITypeReference> Interfaces {
- get {
- foreach (ITypeReference ifaceRef in this.GenericType.ResolvedType.Interfaces) {
- ITypeReference specializedIface = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(ifaceRef, this, this.InternFactory);
- yield return specializedIface;
- }
- }
- }
- public bool IsAbstract {
- get { return this.GenericType.ResolvedType.IsAbstract; }
- }
- public bool IsClass {
- get { return this.GenericType.ResolvedType.IsClass; }
- }
- public bool IsDelegate {
- get { return this.GenericType.ResolvedType.IsDelegate; }
- }
- public bool IsEnum {
- get { return false; }
- }
- public bool IsGeneric {
- get
- //^ ensures result == false;
- {
- return false;
- }
- }
- public bool IsInterface {
- get { return this.GenericType.ResolvedType.IsInterface; }
- }
- public bool IsReferenceType {
- get { return this.GenericType.ResolvedType.IsReferenceType; }
- }
- public bool IsSealed {
- get { return this.GenericType.ResolvedType.IsSealed; }
- }
- public bool IsStatic {
- get { return this.GenericType.ResolvedType.IsStatic; }
- }
- public bool IsValueType {
- get { return this.GenericType.IsValueType; }
- }
- public bool IsStruct {
- get { return this.GenericType.ResolvedType.IsStruct; }
- }
- public IEnumerable<IMethodDefinition> Methods {
- get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, IMethodDefinition>(this.Members); }
- }
- public IEnumerable<INestedTypeDefinition> NestedTypes {
- get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, INestedTypeDefinition>(this.Members); }
- }
- public IPlatformType PlatformType {
- get { return this.GenericType.PlatformType; }
- }
- public IEnumerable<ITypeDefinitionMember> PrivateHelperMembers {
- get {
- //TODO: specialize and cache the private helper members of the generic type template.
- return Enumerable<ITypeDefinitionMember>.Empty;
- }
- }
- public IEnumerable<IPropertyDefinition> Properties {
- get { return IteratorHelper.GetFilterEnumerable<ITypeDefinitionMember, IPropertyDefinition>(this.Members); }
- }
- /// <summary>
- /// Returns a deep copy of a generic type instance reference. In the copy, every reference to a partially specialized type parameter defined by
- /// the partially specialized version of targetContainer or of one of targetContainer's parents (if the parent is a SpecializedNestedTypeDefinition
- /// and generic) will be replaced with the specialized type parameter, defined by targetContainer or its parents.
- /// </summary>
- /// <param name="genericTypeInstance">An array type reference to be deep copied. </param>
- /// <param name="targetContainer">A specialized nested type definition whose or whose parents' (specialized) type parameters will
- /// replace the occurrences of matching type parameters in <paramref name="genericTypeInstance"/>.</param>
- /// /// <param name="internFactory">An intern factory. </param>
- internal static ITypeReference DeepCopyTypeReference(IGenericTypeInstanceReference genericTypeInstance, SpecializedNestedTypeDefinition targetContainer, IInternFactory internFactory) {
- var copiedGenericType = (INamedTypeReference)TypeDefinition.DeepCopyTypeReference(genericTypeInstance.GenericType, targetContainer, internFactory);
- List<ITypeReference>/*?*/ copiedArguments = null;
- int i = 0;
- foreach (ITypeReference argType in genericTypeInstance.GenericArguments) {
- ITypeReference copiedArgType = TypeDefinition.DeepCopyTypeReference(argType, targetContainer, internFactory);
- if (argType != copiedArgType) {
- if (copiedArguments == null) copiedArguments = new List<ITypeReference>(genericTypeInstance.GenericArguments);
- //^ assume 0 <= i && i < specializedArguments.Count; //Since genericTypeInstance.GenericArguments is immutable
- copiedArguments[i] = copiedArgType;
- }
- i++;
- }
- if (copiedArguments == null) {
- if (copiedGenericType == genericTypeInstance.GenericType) return genericTypeInstance;
- return GetGenericTypeInstance(copiedGenericType, genericTypeInstance.GenericArguments, internFactory);
- }
- return GetGenericTypeInstance(copiedGenericType, copiedArguments, internFactory);
- }
- internal static ITypeReference SpecializeTypeReference(IGenericTypeInstanceReference genericTypeInstance, ITypeReference targetContainer, IInternFactory internFactory) {
- var copiedGenericType = (INamedTypeReference)TypeHelper.SpecializeTypeReference(genericTypeInstance.GenericType, targetContainer, internFactory);
- List<ITypeReference>/*?*/ copiedArguments = null;
- int i = 0;
- foreach (ITypeReference argType in genericTypeInstance.GenericArguments) {
- ITypeReference copiedArgType = TypeHelper.SpecializeTypeReference(argType, targetContainer, internFactory);
- if (argType != copiedArgType) {
- if (copiedArguments == null) copiedArguments = new List<ITypeReference>(genericTypeInstance.GenericArguments);
- //^ assume 0 <= i && i < specializedArguments.Count; //Since genericTypeInstance.GenericArguments is immutable
- copiedArguments[i] = copiedArgType;
- }
- i++;
- }
- if (copiedArguments == null) {
- if (copiedGenericType == genericTypeInstance.GenericType) return genericTypeInstance;
- return GetGenericTypeInstance(copiedGenericType, genericTypeInstance.GenericArguments, internFactory);
- }
- return GetGenericTypeInstance(copiedGenericType, copiedArguments, internFactory);
- }
- internal static ITypeReference SpecializeTypeReference(IGenericTypeInstanceReference genericTypeInstance, IMethodReference targetContainer, IInternFactory internFactory) {
- var copiedGenericType = (INamedTypeReference)TypeHelper.SpecializeTypeReference(genericTypeInstance.GenericType, targetContainer, internFactory);
- List<ITypeReference>/*?*/ copiedArguments = null;
- int i = 0;
- foreach (ITypeReference argType in genericTypeInstance.GenericArguments) {
- ITypeReference copiedArgType = TypeHelper.SpecializeTypeReference(argType, targetContainer, internFactory);
- if (argType != copiedArgType) {
- if (copiedArguments == null) copiedArguments = new List<ITypeReference>(genericTypeInstance.GenericArguments);
- //^ assume 0 <= i && i < specializedArguments.Count; //Since genericTypeInstance.GenericArguments is immutable
- copiedArguments[i] = copiedArgType;
- }
- i++;
- }
- if (copiedArguments == null) {
- if (copiedGenericType == genericTypeInstance.GenericType) return genericTypeInstance;
- return GetGenericTypeInstance(copiedGenericType, genericTypeInstance.GenericArguments, internFactory);
- }
- return GetGenericTypeInstance(copiedGenericType, copiedArguments, internFactory);
- }
- /// <summary>
- /// Specialize component type references of genericTypeInstance and (if necessary) return a new instance of the
- /// specialized version of genericTypeInstance.GenericType using the specialized type arguments. Specialization here
- /// means replacing any references to the generic type parameters of containingMethodInstance.GenericMethod with the
- /// corresponding values of containingMethodInstance.GenericArguments.
- /// </summary>
- public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IGenericTypeInstanceReference genericTypeInstance, IGenericMethodInstanceReference containingMethodInstance, IInternFactory internFactory) {
- var specializedGenericType = (INamedTypeReference)TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(genericTypeInstance.GenericType, containingMethodInstance, internFactory);
- List<ITypeReference>/*?*/ specializedArguments = null;
- int i = 0;
- foreach (ITypeReference argType in genericTypeInstance.GenericArguments) {
- ITypeReference specializedArgType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(argType, containingMethodInstance, internFactory);
- if (argType != specializedArgType) {
- if (specializedArguments == null) specializedArguments = new List<ITypeReference>(genericTypeInstance.GenericArguments);
- //^ assume 0 <= i && i < specializedArguments.Count; //Since genericTypeInstance.GenericArguments is immutable
- specializedArguments[i] = specializedArgType;
- }
- i++;
- }
- if (specializedArguments == null) {
- if (specializedGenericType == genericTypeInstance.GenericType) return genericTypeInstance;
- else return GetGenericTypeInstance(specializedGenericType, genericTypeInstance.GenericArguments, internFactory);
- }
- return GetGenericTypeInstance(specializedGenericType, specializedArguments, internFactory);
- }
- /// <summary>
- /// Specialize the type arguments of genericTypeIntance and (if necessary) return a new instance of containingTypeInstance.GenericType using
- /// the specialized type arguments. Specialization means replacing any references to the type parameters of containingTypeInstance.GenericType with the
- /// corresponding values of containingTypeInstance.GenericArguments.
- /// </summary>
- public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IGenericTypeInstanceReference genericTypeInstance, IGenericTypeInstanceReference containingTypeInstance, IInternFactory internFactory) {
- var specializedGenericType = (INamedTypeReference)TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(genericTypeInstance.GenericType, containingTypeInstance, internFactory);
- List<ITypeReference>/*?*/ specializedArguments = null;
- int i = 0;
- foreach (ITypeReference argType in genericTypeInstance.GenericArguments) {
- ITypeReference specializedArgType = TypeDefinition.SpecializeIfConstructedFromApplicableTypeParameter(argType, containingTypeInstance, internFactory);
- if (argType != specializedArgType) {
- if (specializedArguments == null) specializedArguments = new List<ITypeReference>(genericTypeInstance.GenericArguments);
- //^ assume 0 <= i && i < specializedArguments.Count; //Since genericTypeInstance.GenericArguments is immutable
- specializedArguments[i] = specializedArgType;
- }
- i++;
- }
- if (specializedArguments == null) {
- if (specializedGenericType == genericTypeInstance.GenericType) return genericTypeInstance;
- else return GetGenericTypeInstance(specializedGenericType, genericTypeInstance.GenericArguments, internFactory);
- }
- return GetGenericTypeInstance(specializedGenericType, specializedArguments, internFactory);
- }
- /// <summary>
- /// Specialize the type arguments of genericTypeIntance and (if necessary) return a new instance of containingTypeInstance.GenericType using
- /// the specialized type arguments. Specialization means replacing any references to the method type parameters of
- /// the partially specialized version of specializedMethodReference with the corresponding references to method type parameters of specializedMethodReference.
- /// </summary>
- internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(IGenericTypeInstanceReference genericTypeInstance, ISpecializedMethodReference specializedMethodReference, IInternFactory internFactory) {
- var specializedGenericType = (INamedTypeReference)TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(genericTypeInstance.GenericType, specializedMethodReference, internFactory);
- List<ITypeReference>/*?*/ specializedArguments = null;
- int i = 0;
- foreach (ITypeReference argType in genericTypeInstance.GenericArguments) {
- ITypeReference specializedArgType = TypeDefinition.DeepCopyTypeReferenceWRTSpecializedMethod(argType, specializedMethodReference, internFactory);
- if (argType != specializedArgType) {
- if (specializedArguments == null) specializedArguments = new List<ITypeReference>(genericTypeInstance.GenericArguments);
- //^ assume 0 <= i && i < specializedArguments.Count; //Since genericTypeInstance.GenericArguments is immutable
- specializedArguments[i] = specializedArgType;
- }
- i++;
- }
- if (specializedArguments == null) {
- if (specializedGenericType == genericTypeInstance.GenericType) return genericTypeInstance;
- else return GetGenericTypeInstance(specializedGenericType, genericTypeInstance.GenericArguments, internFactory);
- }
- return GetGenericTypeInstance(specializedGenericType, specializedArguments, internFactory);
- }
- public ITypeDefinitionMember SpecializeMember(ITypeDefinitionMember unspecializedMember, IInternFactory internFactory)
- //^ requires unspecializedMember is IEventDefinition || unspecializedMember is IFieldDefinition || unspecializedMember is IMethodDefinition ||
- //^ unspecializedMember is IPropertyDefinition || unspecializedMember is INestedTypeDefinition;
- //^ ensures unspecializedMember is IEventDefinition ==> result is IEventDefinition;
- //^ ensures unspecializedMember is IFieldDefinition ==> result is IFieldDefinition;
- //^ ensures unspecializedMember is IMethodDefinition ==> result is IMethodDefinition;
- //^ ensures unspecializedMember is IPropertyDefinition ==> result is IPropertyDefinition;
- //^ ensures unspecializedMember is INestedTypeDefinition ==> result is INestedTypeDefinition;
- {
- IEventDefinition/*?*/ eventDef = unspecializedMember as IEventDefinition;
- if (eventDef != null) {
- var unspecializedEventDef = eventDef;
- var specializedEventDef = eventDef as ISpecializedEventDefinition;
- if (specializedEventDef != null) unspecializedEventDef = specializedEventDef.UnspecializedVersion;
- return new SpecializedEventDefinition(unspecializedEventDef, eventDef, this, this);
- }
- IFieldDefinition/*?*/ fieldDef = unspecializedMember as IFieldDefinition;
- if (fieldDef != null) {
- var unspecializedFieldDef = fieldDef;
- var specializedFieldDef = fieldDef as ISpecializedFieldDefinition;
- if (specializedFieldDef != null) unspecializedFieldDef = specializedFieldDef.UnspecializedVersion;
- return new SpecializedFieldDefinition(unspecializedFieldDef, fieldDef, this, this);
- }
- IMethodDefinition/*?*/ methodDef = unspecializedMember as IMethodDefinition;
- if (methodDef != null) {
- var unspecializedMethodDef = methodDef;
- var specializedMethodDef = methodDef as ISpecializedMethodDefinition;
- if (specializedMethodDef != null) unspecializedMethodDef = specializedMethodDef.UnspecializedVersion;
- return new SpecializedMethodDefinition(unspecializedMethodDef, methodDef, this, this);
- }
- IPropertyDefinition/*?*/ propertyDef = unspecializedMember as IPropertyDefinition;
- if (propertyDef != null) {
- var unspecializedPropertyDef = propertyDef;
- var specializedPropertyDef = propertyDef as ISpecializedPropertyDefinition;
- if (specializedPropertyDef != null) unspecializedPropertyDef = specializedPropertyDef.UnspecializedVersion;
- return new SpecializedPropertyDefinition(unspecializedPropertyDef, propertyDef, this, this);
- }
- //^ assert unspecializedMember is INestedTypeDefinition;
- INestedTypeDefinition nestedTypeDef = (INestedTypeDefinition)unspecializedMember;
- var unspecializedTypeDef = nestedTypeDef;
- var specializedTypeDef = nestedTypeDef as ISpecializedNestedTypeDefinition;
- if (specializedTypeDef != null) unspecializedTypeDef = specializedTypeDef.UnspecializedVersion;
- return new SpecializedNestedTypeDefinition(unspecializedTypeDef, nestedTypeDef, this, this, internFactory);
- }
- public uint SizeOf {
- get { return this.GenericType.ResolvedType.SizeOf; }
- }
- //^ [Confined]
- public override string ToString() {
- StringBuilder sb = new StringBuilder();
- sb.Append(this.GenericType.ResolvedType.ToString());
- sb.Append('<');
- foreach (ITypeReference arg in this.GenericArguments) {
- if (sb[sb.Length - 1] != '<') sb.Append(',');
- sb.Append(arg.ResolvedType.ToString());
- }
- sb.Append('>');
- return sb.ToString();
- }
- public PrimitiveTypeCode TypeCode {
- get { return PrimitiveTypeCode.NotPrimitive; }
- }
- public ITypeReference UnderlyingType {
- get { return this; }
- }
- public LayoutKind Layout {
- get
- //^ ensures result == this.GenericType.ResolvedType.Layout;
- {
- return this.GenericType.ResolvedType.Layout;
- }
- }
- public bool IsSpecialName {
- get { return this.GenericType.ResolvedType.IsSpecialName; }
- }
- public bool IsComObject {
- get { return this.GenericType.ResolvedType.IsComObject; }
- }
- public bool IsSerializable {
- get { return this.GenericType.ResolvedType.IsSerializable; }
- }
- public bool IsBeforeFieldInit {
- get { return this.GenericType.ResolvedType.IsBeforeFieldInit; }
- }
- public StringFormatKind StringFormat {
- get { return this.GenericType.ResolvedType.StringFormat; }
- }
- public bool IsRuntimeSpecial {
- get { return this.GenericType.ResolvedType.IsRuntimeSpecial; }
- }
- public bool HasDeclarativeSecurity {
- get { return this.GenericType.ResolvedType.HasDeclarativeSecurity; }
- }
- #region ITypeDefinition Members
- IEnumerable<IGenericTypeParameter> ITypeDefinition.GenericParameters {
- get { return Enumerable<IGenericTypeParameter>.Empty; }
- }
- ushort ITypeDefinition.GenericParameterCount {
- get {
- return 0;
- }
- }
- IEnumerable<ITypeDefinitionMember> ITypeDefinition.Members {
- get {
- return this.Members;
- }
- }
- IEnumerable<ISecurityAttribute> ITypeDefinition.SecurityAttributes {
- get { return Enumerable<ISecurityAttribute>.Empty; }
- }
- #endregion
- #region IContainer<ITypeDefinitionMember>
- IEnumerable<ITypeDefinitionMember> IContainer<ITypeDefinitionMember>.Members {
- get {
- return this.Members;
- }
- }
- #endregion
- #region ITypeReference Members
- public bool IsAlias {
- get { return false; }
- }
- public IAliasForType AliasForType {
- get { return Dummy.AliasForType; }
- }
- ITypeDefinition ITypeReference.ResolvedType {
- get { return this; }
- }
- /// <summary>
- /// A collection of methods that associate unique integers with metadata model entities.
- /// The association is based on the identities of the entities and the factory does not retain
- /// references to the given metadata model objects.
- /// </summary>
- public IInternFactory InternFactory {
- get { return this.internFactory; }
- }
- readonly IInternFactory internFactory;
- public uint InternedKey {
- get {
- if (this.internedKey == 0) {
- this.internedKey = this.InternFactory.GetTypeReferenceInternedKey(this);
- }
- return this.internedKey;
- }
- }
- uint internedKey;
- #endregion
- }
- public class GenericTypeInstanceReference : IGenericTypeInstanceReference {
- public GenericTypeInstanceReference(INamedTypeReference genericType, IEnumerable<ITypeReference> genericArguments, IInternFactory internFactory) {
- this.genericType = genericType;
- this.genericArguments = genericArguments;
- this.internFactory = internFactory;
- }
- #region IGenericTypeInstanceReference Members
- public IEnumerable<ITypeReference> GenericArguments {
- get { return this.genericArguments; }
- }
- readonly IEnumerable<ITypeReference> genericArguments;
- public INamedTypeReference GenericType {
- get { return this.genericType; }
- }
- readonly INamedTypeReference genericType; //^ invariant genericType.ResolvedType.IsGeneric;
- #endregion
- #region ITypeReference Members
- public IAliasForType AliasForType {
- get { return Dummy.AliasForType; }
- }
- /// <summary>
- /// A collection of methods that associate unique integers with metadata model entities.
- /// The association is based on the identities of the entities and the factory does not retain
- /// references to the given metadata model objects.
- /// </summary>
- public IInternFactory InternFactory {
- get { return this.internFactory; }
- }
- readonly IInternFactory internFactory;
- public uint InternedKey {
- get {
- if (this.internedKey == 0) {
- this.internedKey = this.InternFactory.GetTypeReferenceInternedKey(this);
- }
- return this.internedKey;
- }
- }
- uint internedKey;
- public bool IsAlias {
- get { return false; }
- }
- public bool IsEnum {
- get { return false; }
- }
- public bool IsValueType {
- get { return this.GenericType.IsValueType; }
- }
- public IPlatformType PlatformType {
- get { return this.GenericType.PlatformType; }
- }
- public ITypeDefinition ResolvedType {
- get {
- if (this.resolvedType == null) {
- var template = this.GenericType.ResolvedType;
- if (template == Dummy.NamedTypeDefinition)
- this.resolvedType = Dummy.Type;
- else
- this.resolvedType = GenericTypeInstance.GetGenericTypeInstance(template, this.GenericArguments, this.InternFactory);
- }
- return this.resolvedType;
- }
- }
- ITypeDefinition/*?*/ resolvedType;
- public PrimitiveTypeCode TypeCode {
- get { return PrimitiveTypeCode.NotPrimitive; }
- }
- public override string ToString() {
- return TypeHelper.GetTypeName(this);
- }
- #endregion
- #region IReference Members
- public virtual IEnumerable<ICustomAttribute> Attributes {
- get { return Enumerable<ICustomAttribute>.Empty; }
- }
- public void Dispatch(IMetadataVisitor visitor) {
- visitor.Visit(this);
- }
- public void DispatchAsReference(IMetadataVisitor visitor) {
- visitor.Visit(this);
- }
- #endregion
- #region IObjectWithLocations Members
- public virtual IEnumerable<ILocation> Locations {
- get { return Enumerable<ILocation>.Empty; }
- }
- #endregion
- }
- internal static class GenericParameter {
- /// <summary>
- /// If the genericTypeParameter is a type parameter of the targetContainer, or a type parameter of a containing, generic, specialized
- /// nested type of the targetContainer, return the specialized version of the type parameter.
- /// </summary>
- /// <remarks>:
- /// Example of how a type parameter is from the containing type of the targetContainer:
- /// class Outer[A] {
- /// class Mid[T] {
- /// class Inner {
- /// T f;
- /// }
- /// }
- /// }
- /// Consider Outer[char].Mid[int].Inner.f. It is a specialized field, whose ContainingGenericTypeInstance = Outer[char].Mid[int]
- /// and whose partiallySpecializedVersion is another specialized field, which we call SF1.
- ///
- /// SF1's ContainingGenericTypeInstance is Outer[char]; its ContainingTypeDefinition is Outer[char].Mid.Inner. Its type should be
- /// a (specialized) copy of T defined by Outer[char].Mid, which is a specialized nested type definition. Note that the targetContainer
- /// for SF1 is Outer[char].Mid.Inner. To look for specialized version of T, we need to go to the parent of the targetContainer.
- /// </remarks>
- /// <param name="genericTypeParameter">A reference to a generic type parameter that occurs inside orginal container.</param>
- /// <param name="targetContainer">A specialized nested type definition whose or whose parent's (specialized) type parameters
- /// are used to replace <paramref name="genericTypeParameter"/>. </param>
- public static ITypeReference DeepCopyTypeReference(IGenericTypeParameterReference genericTypeParameter, SpecializedNestedTypeDefinition targetContainer) {
- var nestedTypeDefinition = targetContainer;
- while (nestedTypeDefinition != null) {
- if (genericTypeParameter.DefiningType.InternedKey == nestedTypeDefinition.partiallySpecializedVersion.InternedKey) {
- int i = 0;
- var genericParameters = nestedTypeDefinition.GenericParameters.GetEnumerator();
- while (genericParameters.MoveNext()) {
- if (i++ == genericTypeParameter.Index) {
- return genericParameters.Current;
- }
- }
- }
- nestedTypeDefinition = nestedTypeDefinition.ContainingTypeDefinition as SpecializedNestedTypeDefinition;
- }
- return genericTypeParameter;
- }
- internal static ITypeReference SpecializeTypeReference(IGenericTypeParameterReference genericTypeParameter, ITypeReference targetContainer) {
- var index = genericTypeParameter.Index;
- while (true) {
- var specializedNestedType = targetContainer as ISpecializedNestedTypeReference;
- if (specializedNestedType != null) { targetContainer = specializedNestedType.ContainingType; continue; }
- var genericTypeInstance = targetContainer as IGenericTypeInstanceReference;
- if (genericTypeInstance == null) return genericTypeParameter;
- var genericType = genericTypeInstance.GenericType;
- specializedNestedType = genericType as ISpecializedNestedTypeReference;
- if (specializedNestedType != null) genericType = specializedNestedType.UnspecializedVersion;
- if (!TypeHelper.TypesAreEquivalent(genericType, genericTypeParameter.DefiningType)) {
- targetContainer = genericTypeInstance.GenericType; continue;
- }
- var i = 0;
- foreach (var typeRef in genericTypeInstance.GenericArguments) if (index == i++) return typeRef;
- Contract.Assume(false); //The type parameter claims it is defined by genericTypeInstance.GenericType, so we should never get here legitimately.
- return Dummy.TypeReference;
- }
- }
- internal static ITypeReference SpecializeTypeReference(IGenericTypeParameterReference genericTypeParameter, IMethodReference targetContainer) {
- return SpecializeTypeReference(genericTypeParameter, targetContainer.ContainingType);
- }
- internal static ITypeReference SpecializeTypeReference(IGenericMethodParameterReference genericMethodParameter, IMethodReference targetContainer, IInternFactory internFactory) {
- return new SpecializedGenericMethodParameterReference(targetContainer, genericMethodParameter, internFactory);
- }
- /// <summary>
- /// 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
- /// was used to create the method instance.
- /// </summary>
- public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IGenericMethodParameterReference genericMethodParameter, IGenericMethodInstanceReference containingMethodInstance) {
- if (genericMethodParameter.DefiningMethod.InternedKey == containingMethodInstance.GenericMethod.InternedKey) {
- ushort i = 0;
- ushort n = genericMethodParameter.Index;
- IEnumerator<ITypeReference> genericArguments = containingMethodInstance.GenericArguments.GetEnumerator();
- while (genericArguments.MoveNext()) {
- if (i++ == n) return genericArguments.Current;
- }
- }
- return genericMethodParameter;
- }
- /// <summary>
- /// 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
- /// was used to create the type instance.
- /// </summary>
- public static ITypeReference SpecializeIfConstructedFromApplicableTypeParameter(IGenericTypeParameterReference genericTypeParameter, IGenericTypeInstanceReference containingTypeInstance) {
- if (genericTypeParameter.DefiningType.InternedKey == containingTypeInstance.GenericType.InternedKey) {
- ushort i = 0;
- ushort n = genericTypeParameter.Index;
- IEnumerator<ITypeReference> genericArguments = containingTypeInstance.GenericArguments.GetEnumerator();
- while (genericArguments.MoveNext()) {
- if (i++ == n) return genericArguments.Current;
- }
- }
- return genericTypeParameter;
- }
- /// <summary>
- /// If the given genericMethodParameter is a generic method parameter of the unspecialized version of specializedMethodDefinition,
- /// then return the corresponding generic method parameter of specializedMethodDefinition.
- /// </summary>
- internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(IGenericMethodParameterReference genericMethodParameter, ISpecializedMethodReference specializedMethodReference) {
- var specializedMethodDefinition = specializedMethodReference as SpecializedMethodDefinition;
- if (specializedMethodDefinition != null && genericMethodParameter.DefiningMethod.InternedKey == specializedMethodDefinition.PartiallySpecializedVersion.InternedKey) {
- ushort i = 0;
- ushort n = genericMethodParameter.Index;
- IEnumerator<IGenericMethodParameter> genericParameters = specializedMethodDefinition.GenericParameters.GetEnumerator();
- while (genericParameters.MoveNext()) {
- if (i++ == n) return genericParameters.Current;
- }
- }
- return genericMethodParameter;
- }
- /// <summary>
- /// If the given genericTypeParameter is a generic type parameter
- /// of a containing specialized nested type definition of the specializedMethodReference, then return the specialized version
- /// of the type parameter.
- /// </summary>
- internal static ITypeReference DeepCopyTypeReferenceWRTSpecializedMethod(IGenericTypeParameterReference genericTypeParameter, ISpecializedMethodReference specializedMethodReference) {
- var specializedNestedType = specializedMethodReference.ContainingType as SpecializedNestedTypeDefinition;
- if (specializedNestedType != null) return DeepCopyTypeReference(genericTypeParameter, specializedNestedType);
- return genericTypeParameter;
- }
- }
- public class ManagedPointerType : SystemDefinedStructuralType, IManagedPointerType {
- protected ManagedPointerType(ITypeReference targetType, IInternFactory internFactory)
- : base(internFactory) {
- this.targetType = targetType;
- }
- /// <summary>
- /// Calls visitor.Visit(IManagedPointerTyp…
Large files files are truncated, but you can click here to view the full file