Exemplo n.º 1
0
        public override void TraverseChildren(IMethodDefinition methodDefinition)
        {
            if (AttributeHelper.Contains(methodDefinition.Attributes, this.host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute))
            {
                return;
            }
            if (ContractHelper.IsInvariantMethod(this.host, methodDefinition))
            {
                return;
            }
            if (IsGetter(methodDefinition) || IsSetter(methodDefinition))
            {
                return;
            }
            IMethodContract methodContract;

            if (this.showInherited)
            {
                methodContract = ContractHelper.GetMethodContractForIncludingInheritedContracts(this.host, methodDefinition);
            }
            else
            {
                methodContract = ContractHelper.GetMethodContractFor(this.host, methodDefinition);
            }
            Indent();
            var methodSig = MemberHelper.GetMethodSignature(methodDefinition, NameFormattingOptions.Signature | NameFormattingOptions.ParameterName | NameFormattingOptions.ParameterModifiers);

            Console.WriteLine(methodSig);
            this.indentLevel++;
            PrintMethodContract(methodContract);
            this.indentLevel--;
        }
Exemplo n.º 2
0
        /// <summary>
        ///     If the <paramref name="typeDefinition" /> has a type contract, generate a
        ///     contract invariant method and add it to the Methods of the <paramref name="typeDefinition" />.
        /// </summary>
        protected void VisitTypeDefinition(ITypeDefinition typeDefinition, ITypeContract typeContract)
        {
            if (typeContract == null)
            {
                return;
            }

            #region Define the method

            var statements = new List <IStatement>();
            var methodBody = new SourceMethodBody(host)
            {
                LocalsAreZeroed = true,
                Block           = new BlockStatement {
                    Statements = statements
                }
            };
            var attributes = new List <ICustomAttribute>();
            var m          = new MethodDefinition
            {
                Attributes               = attributes,
                Body                     = methodBody,
                CallingConvention        = CallingConvention.HasThis,
                ContainingTypeDefinition = typeDefinition,
                InternFactory            = host.InternFactory,
                IsStatic                 = false,
                Name                     = host.NameTable.GetNameFor("$InvariantMethod$"),
                Type                     = systemVoid,
                Visibility               = TypeMemberVisibility.Private
            };
            methodBody.MethodDefinition = m;

            #region Add calls to Contract.Invariant

            foreach (var inv in typeContract.Invariants)
            {
                var methodCall = new MethodCall
                {
                    Arguments = new List <IExpression> {
                        inv.Condition
                    },
                    IsStaticCall = true,
                    MethodToCall = contractProvider.ContractMethods.Invariant,
                    Type         = systemVoid,
                    Locations    = new List <ILocation>(inv.Locations)
                };
                var es = new ExpressionStatement
                {
                    Expression = methodCall
                };
                statements.Add(es);
            }
            statements.Add(new ReturnStatement());

            #endregion Add calls to Contract.Invariant

            #region Add [ContractInvariantMethod]

            var contractInvariantMethodType = new NamespaceTypeReference(
                host,
                host.PlatformType.SystemDiagnosticsContractsContract.ContainingUnitNamespace,
                host.NameTable.GetNameFor("ContractInvariantMethodAttribute"),
                0,
                false,
                false,
                true,
                PrimitiveTypeCode.NotPrimitive
                );
            var contractInvariantMethodCtor = new Microsoft.Cci.MutableCodeModel.MethodReference
            {
                CallingConvention     = CallingConvention.HasThis,
                ContainingType        = contractInvariantMethodType,
                GenericParameterCount = 0,
                InternFactory         = host.InternFactory,
                Name = host.NameTable.Ctor,
                Type = host.PlatformType.SystemVoid
            };
            var contractInvariantMethodAttribute = new CustomAttribute();
            contractInvariantMethodAttribute.Constructor = contractInvariantMethodCtor;
            attributes.Add(contractInvariantMethodAttribute);

            #endregion Add [ContractInvariantMethod]

            var namedTypeDefinition = (NamedTypeDefinition)typeDefinition;

            var newMethods = new List <IMethodDefinition>(namedTypeDefinition.Methods == null ? 1 : namedTypeDefinition.Methods.Count() + 1);
            if (namedTypeDefinition.Methods != null)
            {
                foreach (var meth in namedTypeDefinition.Methods)
                {
                    if (!ContractHelper.IsInvariantMethod(host, meth))
                    {
                        newMethods.Add(meth);
                    }
                }
            }
            namedTypeDefinition.Methods = newMethods;
            namedTypeDefinition.Methods.Add(m);

            #endregion Define the method
        }