Пример #1
0
 ITypeReference IModule.PlatformType(PlatformType t)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Adds the rsd contract to the method, it is added as a postcondition where the rsd field of the type is less than or equal to limit
        /// </summary>
        protected FieldDefinition AddRsdContractToMethod(IMethodDefinition method, ITypeReference type, string rsdName, IExpression limit, IExpression cond)
        {
            var rsdField = GetRsdField(method, rsdName, type);

            var newContract = new Microsoft.Cci.MutableContracts.MethodContract();
            var postconditions = new List<IPostcondition>();

            var limitCondition = new LessThanOrEqual()
            {
                LeftOperand = new BoundExpression()
                {
                    Definition = rsdField,
                    Type = rsdField.Type
                },
                RightOperand = limit,
                Type = _host.PlatformType.SystemBoolean,
            };

            var int32Type = new PlatformType(_host).SystemInt32;

            if (cond != null)
            {
                //cond ==> tmp <= limit =~=
                //!cond || tmp <= limit =~=
                //!cond ? tmp <= limit : true
                postconditions.Add(
                    new Microsoft.Cci.MutableContracts.PostCondition()
                    {
                        Condition = new Conditional()
                        {
                            Condition = new LogicalNot() { Operand = cond },
                            Type = int32Type,
                            ResultIfTrue = new CompileTimeConstant() { Type = int32Type, Value = 1 },
                            ResultIfFalse = limitCondition
                        },
                        OriginalSource = "!cond ? rsd <= limit : true",
                    }
                );
            }
            else
            {
                postconditions.Add(
                    new Microsoft.Cci.MutableContracts.PostCondition()
                    {
                        Condition = limitCondition,
                        OriginalSource = "rsd <= limit",
                    }
                );
            }

            newContract.Postconditions = postconditions;

            var contract = _contractProvider.GetMethodContractFor(method);
            if (contract != null)
            {
                Microsoft.Cci.MutableContracts.ContractHelper.AddMethodContract(newContract, contract);
            }
            _contractProvider.AssociateMethodWithContract(method, newContract);

            return rsdField;
        }