コード例 #1
0
        private NamespaceTypeDefinition CreateContractClass(UnitNamespace unitNamespace)
        {
            var contractTypeName      = this.host.NameTable.GetNameFor("Contract");
            var contractNamespaceName = this.host.NameTable.GetNameFor("System.Diagnostics.Contracts");

            Microsoft.Cci.MethodReference compilerGeneratedCtor =
                new Microsoft.Cci.MethodReference(
                    this.host,
                    this.compilerGeneratedAttributeType,
                    CallingConvention.HasThis,
                    this.systemVoidType,
                    this.host.NameTable.Ctor,
                    0);
            CustomAttribute compilerGeneratedAttribute = new CustomAttribute();

            compilerGeneratedAttribute.Constructor = compilerGeneratedCtor;

            var contractsNs = new NestedUnitNamespace()
            {
                ContainingUnitNamespace = unitNamespace,
                Name = contractNamespaceName,
            };
            NamespaceTypeDefinition result = new NamespaceTypeDefinition()
            {
                // NB: The string name must be kept in sync with the code that recognizes contract
                // methods!!
                Name       = contractTypeName,
                Attributes = new List <ICustomAttribute> {
                    compilerGeneratedAttribute
                },
                BaseClasses = new List <ITypeReference> {
                    this.systemObjectType
                },
                ContainingUnitNamespace = contractsNs,
                InternFactory           = this.host.InternFactory,
                IsBeforeFieldInit       = true,
                IsClass      = true,
                IsSealed     = true,
                Layout       = LayoutKind.Auto,
                StringFormat = StringFormatKind.Ansi,
            };

            return(result);
        }
コード例 #2
0
ファイル: Prune.cs プロジェクト: zhuyue1314/CodeContracts
        public override void RewriteChildren(UnitNamespace unitNamespace)
        {
            Contract.Assume(this.copier.OriginalFor.ContainsKey(unitNamespace));
            var origNs = this.copier.OriginalFor[unitNamespace];

            Contract.Assume(this.thingsToKeep.Contains(origNs));

            var members_prime = new List <INamespaceMember>();

            foreach (var m in unitNamespace.Members)
            {
                Contract.Assume(this.copier.OriginalFor.ContainsKey(m));
                var orig = this.copier.OriginalFor[m];
                if (this.thingsToKeep.Contains(orig))
                {
                    members_prime.Add(m);
                    base.Rewrite(m);
                }
            }
            unitNamespace.Members = members_prime;
        }
コード例 #3
0
        public virtual void VisitNamespace(UnitNamespace unitNamespace)
        {
            if (unitNamespace.Members == null)
            {
                return;
            }

            for (int i = 0; i < unitNamespace.Members.Count; i++)
            {
                INamespaceMember member = unitNamespace.Members[i];

                MethodDefinition methodDef = member as MethodDefinition;

                if (methodDef != null)
                {
                    VisitMethod(methodDef, null);
                    continue;
                }

                NamespaceTypeDefinition typeDef = member as NamespaceTypeDefinition;

                if (typeDef != null)
                {
                    VisitType(typeDef);
                    continue;
                }

                UnitNamespace ns = member as UnitNamespace;

                if (ns != null)
                {
                    VisitNamespace(ns);
                }
                else
                {
                    throw new InvalidOperationException("INamespaceMember");
                }
            }
        }
コード例 #4
0
        private void MergeNamespaces(UnitNamespace result, IUnitNamespace namespaceToMerge)
        {
            Contract.Requires(result != null);
            Contract.Requires(namespaceToMerge != null);

            foreach (var member in namespaceToMerge.Members)
            {
                var nestedNs = member as NestedUnitNamespace;
                if (nestedNs != null)
                {
                    UnitNamespace nestedResult = null;
                    foreach (var rmember in result.Members)
                    {
                        Contract.Assume(rmember != null);
                        if (rmember.Name != nestedNs.Name)
                        {
                            continue;
                        }
                        nestedResult = rmember as UnitNamespace;
                        if (nestedResult != null)
                        {
                            break;
                        }
                    }
                    if (nestedResult == null)
                    {
                        nestedResult = new NestedUnitNamespace()
                        {
                            ContainingUnitNamespace = result, Name = nestedNs.Name
                        }
                    }
                    ;
                    this.MergeNamespaces(nestedResult, nestedNs);
                    continue;
                }
                result.Members.Add(member);
            }
        }
コード例 #5
0
ファイル: Rewriter.cs プロジェクト: xornand/cci
    private NamespaceTypeDefinition CreateContractClass(UnitNamespace unitNamespace) {

      var contractTypeName = this.host.NameTable.GetNameFor("Contract");
      var contractNamespaceName = this.host.NameTable.GetNameFor("System.Diagnostics.Contracts");

      Microsoft.Cci.MethodReference compilerGeneratedCtor =
        new Microsoft.Cci.MethodReference(
          this.host,
          this.compilerGeneratedAttributeType,
          CallingConvention.HasThis,
          this.systemVoidType,
          this.host.NameTable.Ctor,
          0);
      CustomAttribute compilerGeneratedAttribute = new CustomAttribute();
      compilerGeneratedAttribute.Constructor = compilerGeneratedCtor;

      var contractsNs = new NestedUnitNamespace() {
        ContainingUnitNamespace = unitNamespace,
        Name = contractNamespaceName,
      };
      NamespaceTypeDefinition result = new NamespaceTypeDefinition() {
        // NB: The string name must be kept in sync with the code that recognizes contract
        // methods!!
        Name = contractTypeName,
        Attributes = new List<ICustomAttribute>{ compilerGeneratedAttribute },
        BaseClasses = new List<ITypeReference>{ this.systemObjectType },
        ContainingUnitNamespace = contractsNs,
        InternFactory = this.host.InternFactory,
        IsBeforeFieldInit = true,
        IsClass = true,
        IsSealed = true,
        Layout = LayoutKind.Auto,
        StringFormat = StringFormatKind.Ansi,
      };
      return result;
    }
コード例 #6
0
ファイル: Prune.cs プロジェクト: asvishnyakov/CodeContracts
    public override void RewriteChildren(UnitNamespace unitNamespace) {

      Contract.Assume(this.copier.OriginalFor.ContainsKey(unitNamespace));
      var origNs = this.copier.OriginalFor[unitNamespace];
      Contract.Assume(this.thingsToKeep.Contains(origNs));

      var members_prime = new List<INamespaceMember>();
      foreach (var m in unitNamespace.Members) {
        Contract.Assume(this.copier.OriginalFor.ContainsKey(m));
        var orig = this.copier.OriginalFor[m];
        if (this.thingsToKeep.Contains(orig)) {
          members_prime.Add(m);
          base.Rewrite(m);
        }
      }
      unitNamespace.Members = members_prime;
    }
コード例 #7
0
        private void ReparentMembersAndDealWithDuplicates(UnitNamespace unitNamespace)
        {
            Contract.Requires(unitNamespace != null);

            var members = unitNamespace.Members;

            for (int i = 0, n = members.Count; i < n; i++)
            {
                var member = members[i];
                var nsType = member as NamespaceTypeDefinition;
                if (nsType != null)
                {
                    nsType.ContainingUnitNamespace = unitNamespace;
                    for (int j = i + 1; j < n; j++)
                    {
                        var laterNsType = members[j] as NamespaceTypeDefinition;
                        if (laterNsType == null || laterNsType.Name != nsType.Name)
                        {
                            continue;
                        }
                        if (nsType.MangleName != laterNsType.MangleName)
                        {
                            continue;
                        }
                        if (nsType.MangleName && nsType.GenericParameterCount != laterNsType.GenericParameterCount)
                        {
                            continue;
                        }
                        this.ReportNameCollisionAndRename(nsType, laterNsType, j);
                    }
                    continue;
                }
                var globalField = member as GlobalFieldDefinition;
                if (globalField != null)
                {
                    globalField.ContainingTypeDefinition = this.resultModuleType;
                    for (int j = i + 1; j < n; j++)
                    {
                        var laterField = members[j] as GlobalFieldDefinition;
                        if (laterField == null || laterField.Name != globalField.Name)
                        {
                            continue;
                        }
                        if (!TypeHelper.TypesAreEquivalent(globalField.Type, laterField.Type))
                        {
                            continue;
                        }
                        //TODO: check custom modifiers
                        this.ReportNameCollisionAndRename(globalField, laterField, j);
                    }
                    continue;
                }
                var globalMethod = member as GlobalMethodDefinition;
                if (globalMethod != null)
                {
                    globalMethod.ContainingTypeDefinition = this.resultModuleType;
                    for (int j = i + 1; j < n; j++)
                    {
                        var laterMethod = members[j] as GlobalMethodDefinition;
                        if (laterMethod == null || laterMethod.Name != globalMethod.Name)
                        {
                            continue;
                        }
                        if (!MemberHelper.SignaturesAreEqual(globalMethod, laterMethod, resolveTypes: false))
                        {
                            continue;
                        }
                        this.ReportNameCollisionAndRename(globalMethod, laterMethod, j);
                    }
                    continue;
                }
                var alias = member as NamespaceAliasForType;
                if (alias != null)
                {
                    alias.ContainingNamespace = unitNamespace;
                    for (int j = i + 1; j < n; j++)
                    {
                        var laterAlias = members[j] as NamespaceAliasForType;
                        if (laterAlias == null || laterAlias.Name != alias.Name || laterAlias.GenericParameterCount != alias.GenericParameterCount)
                        {
                            continue;                                                                                                             //Note: these names are not unmangled
                        }
                        this.ReportNameCollision(alias, laterAlias, j);
                    }
                    continue;
                }
            }
        }