Copy() public method

Makes a deep copy of the given contract element.
public Copy ( IContractElement contractElement ) : ContractElement
contractElement IContractElement
return Microsoft.Cci.MutableContracts.ContractElement
示例#1
0
    /// <summary>
    /// Given a type contract (<paramref name="typeContract"/> for a unspecialized/uninstantiated type reference/definition
    /// specialize and instantiate (i.e., the generics) in the contract so that it is a contract
    /// relative to the specialized/instantiated type reference/definition (<paramref name="context"/>).
    /// </summary>
    /// <param name="host"></param>
    /// <param name="typeContract"></param>
    /// <param name="context"></param>
    /// <returns>
    /// A deep copy of <paramref name="typeContract"/>, properly specialized and instantiated.
    /// </returns>
    public static TypeContract InstantiateAndSpecializeContract(IMetadataHost host, ITypeContract typeContract, ITypeReference context) {
      Contract.Requires(host != null);
      Contract.Requires(typeContract != null);
      Contract.Requires(context != null);

      var copier = new CodeAndContractDeepCopier(host);
      var mutableContract = copier.Copy(typeContract);

      var specializer = new TypeContractSpecializer(host, context);
      mutableContract = (TypeContract)specializer.Rewrite(mutableContract);
      return mutableContract;
    }
示例#2
0
    /// <summary>
    /// Given a method contract (<paramref name="methodContract"/> for a unspecialized/uninstantiated method reference/definition
    /// (<paramref name="unspec"/>), specialize and instantiate (i.e., the generics) in the contract so that it is a contract
    /// relative to the specialized/instantiated method reference/definition (<paramref name="mr"/>).
    /// </summary>
    /// <param name="host"></param>
    /// <param name="methodContract"></param>
    /// <param name="mr"></param>
    /// <param name="unspec"></param>
    /// <returns>
    /// A deep copy of <paramref name="methodContract"/>, properly specialized and instantiated.
    /// </returns>
    public static MethodContract InstantiateAndSpecializeContract(IMetadataHost host, IMethodContract methodContract, IMethodReference mr, IMethodReference unspec) {
      //Contract.Requires(mr is IGenericMethodInstanceReference || mr is ISpecializedMethodReference);

      var copier = new CodeAndContractDeepCopier(host);
      var mutableContract = copier.Copy(methodContract);

      var unspecializedTypeReferences = new List<ITypeReference>();
      var specializedTypeReferences = new List<ITypeReference>();

      var copyOfUnspecializedContainingType = copier.Copy(unspec.ContainingType);
      unspecializedTypeReferences.Add(copyOfUnspecializedContainingType);
      var containingType = mr.ContainingType;
      var gtir2 = containingType as IGenericTypeInstanceReference;
      var copyOfSpecializedContainingType = gtir2 != null ? copier.Copy(gtir2) : copier.Copy(containingType);
      specializedTypeReferences.Add(copyOfSpecializedContainingType);

      var unspecializedMethodDefinition = unspec as IMethodDefinition;

      #region Map generic type parameters
      var smr = mr as ISpecializedMethodReference;
      if (smr != null) {
        if (unspecializedMethodDefinition != null) {
          foreach (var t in unspecializedMethodDefinition.ContainingTypeDefinition.GenericParameters) {
            // without the cast, the copy is not object equals to references within the contract
            var copyOfT = copier.Copy((IGenericTypeParameterReference)t);
            unspecializedTypeReferences.Add(copyOfT);
          }
          var gtir = smr.ContainingType as IGenericTypeInstanceReference;
          if (gtir != null) {
            foreach (var t in gtir.GenericArguments) {
              var copyOfT = copier.Copy(t);
              specializedTypeReferences.Add(copyOfT);
            }
          }
        }
      }
      #endregion
      #region Map generic method parameters
      var gmir = mr as IGenericMethodInstanceReference;
      if (gmir != null) {
        var unspecialized = gmir.GenericMethod.ResolvedMethod;
        foreach (var t in unspecialized.GenericParameters) {
          var copyOfT = copier.Copy(t);
          unspecializedTypeReferences.Add(copyOfT);
        }
        foreach (var t in gmir.GenericArguments) {
          var copyOfT = copier.Copy(t);
          specializedTypeReferences.Add(copyOfT);
        }
      }
      #endregion
      var d = new Dictionary<uint, ITypeReference>();
      IteratorHelper.Zip(unspecializedTypeReferences, specializedTypeReferences, (u, s) => d.Add(u.InternedKey, s));
      var specializer = new CodeSpecializer(host, d);

      mutableContract = (MethodContract)specializer.Rewrite(mutableContract);
      mutableContract = (MethodContract)ContractHelper.CopyContractIntoNewContext(host, mutableContract, smr != null ? smr.ResolvedMethod : mr.ResolvedMethod, unspecializedMethodDefinition);
      return mutableContract;
    }