Ejemplo n.º 1
0
 private static string GetExceptionId(IPrecondition precondition)
 {
     Contract.Requires(precondition != null);
     if (precondition.ExceptionToThrow != null)
     {
         ITypeOf        asTypeOf = precondition.ExceptionToThrow as ITypeOf;
         ITypeReference unspecializedExceptionType;
         if (asTypeOf != null)
         {
             unspecializedExceptionType = MethodHelper.Unspecialize(asTypeOf.TypeToGet);
         }
         else//Legacy contracts package exceptions differently then regular contracts.
         {
             unspecializedExceptionType = MethodHelper.Unspecialize(precondition.ExceptionToThrow.Type);
         }
         return(TypeHelper.GetTypeName(unspecializedExceptionType, NameFormattingOptions.DocumentationId));
     }
     return(null);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new package for an IThrownException contract.
        /// </summary>
        public XThrownException(IMetadataHost host, IThrownException thrownException, string inheritedFrom, string inheritedFromTypeName, DocTracker docTracker)
            : base(host, inheritedFrom, inheritedFromTypeName, docTracker)
        {
            Contract.Requires(docTracker != null);
            Contract.Requires(thrownException != null);
            Contract.Requires(thrownException.Postcondition != null);
            Contract.Requires(thrownException.ExceptionType != null);
            var post = thrownException.Postcondition;

            this.thrownException = thrownException;
            Contract.Assert(this.thrownException.Postcondition != null);
            var unspecializedExceptionType = MethodHelper.Unspecialize(thrownException.ExceptionType);

            Contract.Assert(this.thrownException.Postcondition != null);
            this.exception = TypeHelper.GetTypeName(unspecializedExceptionType, NameFormattingOptions.DocumentationId);
            Contract.Assert(this.thrownException == thrownException);
            Contract.Assert(thrownException.Postcondition == post);
            Contract.Assert(this.thrownException.Postcondition != null);
        }
            public void PackageMethodContracts(IMethodDefinition method, bool isPure)
            {
                Contract.Requires(method != null);

                #region Package purity
                if (isPure)
                {
                    PackageContract(new XPure(this.host, isPure, docTracker));
                }
                //Don't package anything if it isn't pure. (That way it is easyer to deal with on the Sandcastle side)
                #endregion
                #region Package contracts from this implementation
                IMethodContract thisContract;
                if (CCDocContractHelper.TryGetMethodContract(host, method, out thisContract, docTracker))
                {
                    PackageMethodContracts(thisContract, null, null);
                }
                #endregion
                #region Package contracts from base overrides
                if (!method.IsNewSlot) // REVIEW: Is there a better test?
                {
                    IMethodDefinition overriddenMethod = MemberHelper.GetImplicitlyOverriddenBaseClassMethod(method) as IMethodDefinition;
                    while (overriddenMethod != null && overriddenMethod != Dummy.Method)
                    {
                        IMethodContract /*?*/ overriddenContract;
                        if (CCDocContractHelper.TryGetMethodContract(host, overriddenMethod, out overriddenContract, docTracker))
                        {
                            SubstituteParameters sps         = new SubstituteParameters(this.host, method, overriddenMethod);
                            IMethodContract      newContract = sps.Rewrite(overriddenContract) as MethodContract;
                            var unspecializedMethod          = MethodHelper.Unspecialize(overriddenMethod);
                            var methodId = MemberHelper.GetMemberSignature(unspecializedMethod, NameFormattingOptions.DocumentationId);
                            var typeName = TypeHelper.GetTypeName(unspecializedMethod.ContainingType, NameFormattingOptions.OmitContainingNamespace | NameFormattingOptions.OmitContainingType);
                            PackageMethodContracts(newContract, methodId, typeName);
                        }
                        overriddenMethod = MemberHelper.GetImplicitlyOverriddenBaseClassMethod(overriddenMethod) as IMethodDefinition;
                    }
                }
                #endregion
                #region Package contracts from implicit interface implementations
                foreach (IMethodDefinition ifaceMethod in MemberHelper.GetImplicitlyImplementedInterfaceMethods(method))
                {
                    IMethodContract /*?*/ ifaceContract;
                    if (!CCDocContractHelper.TryGetMethodContract(host, ifaceMethod, out ifaceContract, docTracker))
                    {
                        continue;
                    }
                    SubstituteParameters sps         = new SubstituteParameters(this.host, method, ifaceMethod);
                    IMethodContract      newContract = sps.Rewrite(ifaceContract) as MethodContract;
                    var unspecializedMethod          = MethodHelper.Unspecialize(ifaceMethod);
                    var methodId = MemberHelper.GetMemberSignature(unspecializedMethod, NameFormattingOptions.DocumentationId);
                    var typeName = TypeHelper.GetTypeName(unspecializedMethod.ContainingType, NameFormattingOptions.OmitContainingNamespace | NameFormattingOptions.OmitContainingType);
                    PackageMethodContracts(newContract, methodId, typeName);
                }
                #endregion
                #region Package contracts from explicit interface implementations
                // REVIEW: Why does GetExplicitlyOverriddenMethods return IMethodReference when GetImplicitlyImplementedInterfaceMethods
                // returns IMethodDefinition?
                foreach (IMethodReference ifaceMethodRef in MemberHelper.GetExplicitlyOverriddenMethods(method))
                {
                    IMethodDefinition /*?*/ ifaceMethod = ifaceMethodRef.ResolvedMethod;
                    if (ifaceMethod == null)
                    {
                        continue;
                    }
                    IMethodContract /*?*/ ifaceContract;
                    if (!CCDocContractHelper.TryGetMethodContract(host, ifaceMethod, out ifaceContract, docTracker))
                    {
                        continue;
                    }
                    SubstituteParameters sps         = new SubstituteParameters(this.host, method, ifaceMethod);
                    IMethodContract      newContract = sps.Rewrite(ifaceContract) as MethodContract;
                    var unspecializedMethod          = MethodHelper.Unspecialize(ifaceMethod);
                    var methodId = MemberHelper.GetMemberSignature(unspecializedMethod, NameFormattingOptions.DocumentationId);
                    var typeName = TypeHelper.GetTypeName(unspecializedMethod.ContainingType, NameFormattingOptions.OmitContainingNamespace | NameFormattingOptions.OmitContainingType);
                    PackageMethodContracts(newContract, methodId, typeName);;
                }
                #endregion
            }