protected override IExpression GetExpression(CSharpElementFactory factory, IExpression contractExpression)
        {
            var expression = isDictionary
                ? factory.CreateExpression(
                    string.Format("$0.{0}(pair => pair.{1} != null)", nameof(Enumerable.All), nameof(KeyValuePair<int, int>.Value)),
                    contractExpression)
                : factory.CreateExpression(string.Format("$0.{0}(item => item != null)", nameof(Enumerable.All)), contractExpression);

            var invokedExpression = (IReferenceExpression)((IInvocationExpression)expression).InvokedExpression;

            Debug.Assert(invokedExpression != null);

            var allMethodReference = invokedExpression.Reference;

            var enumerableType = new DeclaredTypeFromCLRName(ClrTypeNames.Enumerable, Provider.PsiModule).GetTypeElement();

            Debug.Assert(enumerableType != null);

            var allMethod = enumerableType.Methods.First(method => method.AssertNotNull().ShortName == nameof(Enumerable.All));

            Debug.Assert(allMethod != null);

            allMethodReference.BindTo(allMethod);

            return expression;
        }
        public override void AddContracts(
            ICSharpContextActionDataProvider provider,
            Func<IExpression, IExpression> getContractExpression,
            out ICollection<ICSharpStatement> firstNonContractStatements)
        {
            if (declaration.Body != null)
            {
                var factory = CSharpElementFactory.GetInstance(provider.PsiModule);

                var contractType = new DeclaredTypeFromCLRName(ClrTypeNames.Contract, provider.PsiModule).GetTypeElement();

                Debug.Assert(declaration.DeclaredElement != null);

                var expression = factory.CreateExpression(
                    string.Format("$0.{0}<$1>()", nameof(Contract.Result)),
                    contractType,
                    declaration.DeclaredElement.ReturnType);

                ICSharpStatement firstNonContractStatement;
                AddContract(
                    ContractKind.Ensures,
                    declaration.Body,
                    provider.PsiModule,
                    () => getContractExpression(expression),
                    out firstNonContractStatement);
                firstNonContractStatements = firstNonContractStatement != null ? new[] { firstNonContractStatement } : null;
            }
            else
            {
                firstNonContractStatements = null;
            }
        }
        static bool IsAvailableForType([NotNull] IType type, [NotNull] ITreeNode context)
        {
            if (type.IsGenericEnumerableOrDescendant() || type.IsGenericArray(context))
            {
                var elementType = CollectionTypeUtil.ElementTypeByCollectionType(type, context);
                if (elementType != null && elementType.Classify == TypeClassification.REFERENCE_TYPE)
                {
                    return true;
                }
            }

            if (type.IsGenericTask())
            {
                var resultType = type.GetTaskUnderlyingType();
                if (resultType != null && resultType.Classify == TypeClassification.REFERENCE_TYPE)
                {
                    return true;
                }
            }

            if (type.IsLazy())
            {
                var typeElement = new DeclaredTypeFromCLRName(PredefinedType.LAZY_FQN, context.GetPsiModule()).GetTypeElement();
                var valueType = type.GetGenericUnderlyingType(typeElement);
                if (valueType != null && valueType.Classify == TypeClassification.REFERENCE_TYPE)
                {
                    return true;
                }
            }

            return false;
        }
        static void AddContractForEnsures(
            [NotNull] ICSharpContextActionDataProvider provider,
            [NotNull] Func<IExpression, IExpression> getContractExpression,
            [NotNull] IParameter parameter,
            [NotNull] IBlock body,
            out ICSharpStatement firstNonContractStatement)
        {
            var factory = CSharpElementFactory.GetInstance(provider.PsiModule);

            var contractType = new DeclaredTypeFromCLRName(ClrTypeNames.Contract, provider.PsiModule).GetTypeElement();

            var parameterExpression = factory.CreateExpression("$0", parameter);

            var expression = factory.CreateExpression(
                string.Format("$0.{0}(out $1)", nameof(Contract.ValueAtReturn)),
                contractType,
                parameterExpression);

            AddContract(ContractKind.Ensures, body, provider.PsiModule, () => getContractExpression(expression), out firstNonContractStatement);
        }
        public override void AddContracts(
            ICSharpContextActionDataProvider provider,
            Func<IExpression, IExpression> getContractExpression,
            out ICollection<ICSharpStatement> firstNonContractStatements)
        {
            var factory = CSharpElementFactory.GetInstance(provider.PsiModule);

            var propertyDeclaration = declaration as IPropertyDeclaration;
            if (propertyDeclaration != null && propertyDeclaration.IsAuto)
            {
                var classLikeDeclaration = (IClassLikeDeclaration)declaration.GetContainingTypeDeclaration();

                Debug.Assert(classLikeDeclaration != null);

                var contractInvariantMethodDeclaration = classLikeDeclaration.EnsureContractInvariantMethod(provider.PsiModule);

                if (contractInvariantMethodDeclaration.Body != null)
                {
                    var expression = factory.CreateExpression("$0", declaration.DeclaredElement);

                    ICSharpStatement firstNonContractStatement;
                    AddContract(
                        ContractKind.Invariant,
                        contractInvariantMethodDeclaration.Body,
                        provider.PsiModule,
                        () => getContractExpression(expression),
                        out firstNonContractStatement);
                    firstNonContractStatements = firstNonContractStatement != null ? new[] { firstNonContractStatement } : null;
                }
                else
                {
                    firstNonContractStatements = null;
                }

                return;
            }

            IEnumerable<IAccessorDeclaration> accessorDeclarations;

            if (declaration.IsAbstract)
            {
                IAccessorOwnerDeclaration overriddenAccessorOwnerDeclaration = null;

                var containingTypeDeclaration = declaration.GetContainingTypeDeclaration();

                Debug.Assert(containingTypeDeclaration != null);

                var contractClassDeclaration = containingTypeDeclaration.EnsureContractClass(provider.PsiModule);

                if (propertyDeclaration != null)
                {
                    overriddenAccessorOwnerDeclaration = propertyDeclaration.EnsureOverriddenPropertyInContractClass(
                        contractClassDeclaration, provider.PsiModule);
                }

                var indexerDeclaration = declaration as IIndexerDeclaration;
                if (indexerDeclaration != null)
                {
                    overriddenAccessorOwnerDeclaration = indexerDeclaration.EnsureOverriddenIndexerInContractClass(
                        contractClassDeclaration, provider.PsiModule);
                }

                Debug.Assert(overriddenAccessorOwnerDeclaration != null);

                accessorDeclarations = overriddenAccessorOwnerDeclaration.AccessorDeclarations;
            }
            else
            {
                accessorDeclarations = declaration.AccessorDeclarations;
            }

            firstNonContractStatements = new List<ICSharpStatement>(2);

            foreach (var accessorDeclaration in accessorDeclarations)
            {
                Debug.Assert(accessorDeclaration != null);

                if (accessorDeclaration.Body != null)
                {
                    switch (accessorDeclaration.Kind)
                    {
                        case AccessorKind.GETTER:
                        {
                            var contractType = new DeclaredTypeFromCLRName(ClrTypeNames.Contract, provider.PsiModule).GetTypeElement();

                            var resultExpression = factory.CreateExpression(
                                string.Format("$0.{0}<$1>()", nameof(Contract.Result)),
                                contractType,
                                Type);

                            ICSharpStatement firstNonContractStatement;
                            AddContract(
                                ContractKind.Ensures,
                                accessorDeclaration.Body,
                                provider.PsiModule,
                                () => getContractExpression(resultExpression),
                                out firstNonContractStatement);

                            if (firstNonContractStatement != null)
                            {
                                firstNonContractStatements.Add(firstNonContractStatement);
                            }
                            break;
                        }

                        case AccessorKind.SETTER:
                        {
                            var valueExpression = factory.CreateExpression("value");

                            ICSharpStatement firstNonContractStatement;
                            AddContract(
                                ContractKind.Requires,
                                accessorDeclaration.Body,
                                provider.PsiModule,
                                () => getContractExpression(valueExpression),
                                out firstNonContractStatement);

                            if (firstNonContractStatement != null)
                            {
                                firstNonContractStatements.Add(firstNonContractStatement);
                            }
                            break;
                        }
                    }
                }
            }

            if (firstNonContractStatements.Count == 0)
            {
                firstNonContractStatements = null;
            }
        }
Пример #6
0
        private static ICollection<IClass> GetTypesNonCached([NotNull] IArgumentsOwner argumentsOwner)
        {
            argumentsOwner.AssertIsValid("argumentsOwner is invalid");
            var invocationExpression = argumentsOwner as IInvocationExpression;
            if (invocationExpression != null)
            {
                var referenceExpression = invocationExpression.InvokedExpression as IReferenceExpression;
                if (referenceExpression != null)
                {
                    var typeofExpression = referenceExpression.QualifierExpression as ITypeofExpression;
                    if (typeofExpression != null)
                    {
                        IType type = typeofExpression.ArgumentType;
                        IClass @class = type.GetClassType();
                        return new[] {@class};
                    }
                    var gettypeExpression = referenceExpression.QualifierExpression as IInvocationExpression;
                    if (gettypeExpression != null && gettypeExpression.Reference != null && gettypeExpression.Reference.GetName() == "GetType")
                    {
                        var method = gettypeExpression.Reference.Resolve().DeclaredElement as IMethod;
                        if (method != null)
                        {
                            var containingType = method.GetContainingType();
                            if (containingType != null)
                            {
                                var containingTypeName = containingType.GetClrName().FullName;
                                if (containingTypeName == "System.Object")
                                {
                                    var typeDeclaration = gettypeExpression.GetContainingNode<ITypeDeclaration>();
                                    if (typeDeclaration != null)
                                    {
                                        return new[] {typeDeclaration.DeclaredElement as IClass};
                                    }
                                }
                                if (containingTypeName == "System.Type" && method.IsStatic)
                                {
                                    if (gettypeExpression.Arguments.Count > 0)
                                    {
                                        var literalExpression = gettypeExpression.Arguments[0].Expression as ICSharpLiteralExpression;
                                        if (literalExpression != null && literalExpression.IsConstantValue() && literalExpression.ConstantValue.IsString())
                                        {
                                            var typeName = new ClrTypeName((string) literalExpression.ConstantValue.Value);

                                            var name = new DeclaredTypeFromCLRName(typeName, gettypeExpression.GetPsiModule(), gettypeExpression.GetResolveContext());
                                            return new[] { name.GetTypeElement() as IClass };
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return EmptyList<IClass>.InstanceList;
        }