Exemplo n.º 1
0
        public override void OnArrayLiteralExpression(ArrayLiteralExpression node)
        {
            if (node.Type != null)
            {
                MakeResult(ConvertType(node.Type));
                return;
            }
            IReturnType elementType = null;

            foreach (Expression expr in node.Items)
            {
                ClearResult();
                expr.Accept(this);
                IReturnType thisType = (resolveResult != null) ? resolveResult.ResolvedType : null;
                if (elementType == null)
                {
                    elementType = thisType;
                }
                else if (thisType != null)
                {
                    elementType = MemberLookupHelper.GetCommonType(projectContent, elementType, thisType);
                }
            }
            if (elementType == null)
            {
                elementType = ConvertVisitor.GetDefaultReturnType(projectContent);
            }
            MakeResult(new ArrayReturnType(projectContent, elementType, 1));
        }
Exemplo n.º 2
0
        public override object VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, object data)
        {
            switch (binaryOperatorExpression.Op)
            {
            case BinaryOperatorType.NullCoalescing:
                return(binaryOperatorExpression.Right.AcceptVisitor(this, data));

            case BinaryOperatorType.DivideInteger:
                return(resolver.ProjectContent.SystemTypes.Int32);

            case BinaryOperatorType.Concat:
                return(resolver.ProjectContent.SystemTypes.String);

            case BinaryOperatorType.Equality:
            case BinaryOperatorType.InEquality:
            case BinaryOperatorType.ReferenceEquality:
            case BinaryOperatorType.ReferenceInequality:
            case BinaryOperatorType.LogicalAnd:
            case BinaryOperatorType.LogicalOr:
            case BinaryOperatorType.LessThan:
            case BinaryOperatorType.LessThanOrEqual:
            case BinaryOperatorType.GreaterThan:
            case BinaryOperatorType.GreaterThanOrEqual:
                return(resolver.ProjectContent.SystemTypes.Boolean);

            default:
                return(MemberLookupHelper.GetCommonType(resolver.ProjectContent,
                                                        binaryOperatorExpression.Left.AcceptVisitor(this, data) as IReturnType,
                                                        binaryOperatorExpression.Right.AcceptVisitor(this, data) as IReturnType));
            }
        }
Exemplo n.º 3
0
        bool Fix(TP X)
        {
            Log("Trying to fix " + X);
            Log("  bounds = ", X.Bounds);
            List <IReturnType> candidates = new List <IReturnType>(X.Bounds);

            foreach (IReturnType U in X.Bounds)
            {
                candidates.RemoveAll((IReturnType candidate) => !MemberLookupHelper.ConversionExists(U, candidate));
            }
            Log("  candidates after removal round = ", candidates);
            if (candidates.Count == 0)
            {
                return(false);
            }
            var results = candidates.Where(
                c1 => candidates.All(c2 => MemberLookupHelper.ConversionExists(c1, c2))
                ).ToList();

            Log("  possible solutions (should be exactly one) = ", candidates);
            if (results.Count == 1)
            {
                X.FixedTo = results[0];
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        public IProperty GetIndexer(IndexerExpression indexerExpression)
        {
            IReturnType type = (IReturnType)indexerExpression.TargetObject.AcceptVisitor(this, null);

            if (type == null)
            {
                return(null);
            }
            List <IProperty> indexers = type.GetProperties();

            // remove non-indexers:
            for (int i = 0; i < indexers.Count; i++)
            {
                if (!indexers[i].IsIndexer)
                {
                    indexers.RemoveAt(i--);
                }
            }
            IReturnType[] parameters = new IReturnType[indexerExpression.Indexes.Count];
            for (int i = 0; i < parameters.Length; i++)
            {
                Expression expr = indexerExpression.Indexes[i] as Expression;
                if (expr != null)
                {
                    parameters[i] = (IReturnType)expr.AcceptVisitor(this, null);
                }
            }
            return(MemberLookupHelper.FindOverload(indexers.ToArray(), parameters));
        }
Exemplo n.º 5
0
        public void LocalVariableAndFieldAreNotSimilarMembers()
        {
            IField field = new DefaultField(dummyClass.DefaultReturnType, "Test", ModifierEnum.None, DomRegion.Empty, dummyClass);
            IField local = new DefaultField.LocalVariableField(dummyClass.DefaultReturnType, "Test", DomRegion.Empty, dummyClass);

            Assert.IsFalse(MemberLookupHelper.IsSimilarMember(local, field));
        }
Exemplo n.º 6
0
 public override object VisitConditionalExpression(ConditionalExpression conditionalExpression, object data)
 {
     return(CreateResolveResult(MemberLookupHelper.GetCommonType(
                                    resolver.ProjectContent,
                                    ResolveType(conditionalExpression.TrueExpression),
                                    ResolveType(conditionalExpression.FalseExpression))));
 }
Exemplo n.º 7
0
        internal ResolveResult ResolveConstructorOverload(IReturnType rt, List <Expression> arguments)
        {
            if (rt == null)
            {
                return(null);
            }

            List <IMethod> methods = rt.GetMethods().Where(m => m.IsConstructor && !m.IsStatic).ToList();

            IReturnType[] argumentTypes = arguments.Select <Expression, IReturnType>(ResolveType).ToArray();
            bool          resultIsAcceptable;
            IMethod       result = MemberLookupHelper.FindOverload(methods, argumentTypes, out resultIsAcceptable);

            if (result == null)
            {
                IClass c = rt.GetUnderlyingClass();
                if (c != null)
                {
                    result = Constructor.CreateDefault(c);
                }
            }
            ResolveResult rr = CreateMemberResolveResult(result);

            if (rr != null)
            {
                rr.ResolvedType = rt;
            }
            return(rr);
        }
Exemplo n.º 8
0
        static bool AllowsHelper(string baseClass, string methodName, IReturnType resolvedType, IProjectContent content)
        {
            IClass extensions = content.GetClass("System.Xml.Linq.Extensions", 0);

            if (extensions == null)
            {
                return(false);
            }
            IMethod descendents = extensions.Methods.FirstOrDefault(m => m.Name == methodName);

            if (descendents == null)
            {
                return(false);
            }
            IParameter param = descendents.Parameters.FirstOrDefault();

            if (param == null)
            {
                return(false);
            }
            IClass resolvedTypeClass = resolvedType.GetUnderlyingClass();

            if (resolvedTypeClass == null)
            {
                return(false);
            }
            return(MemberLookupHelper.IsApplicable(resolvedType, param, descendents) ||
                   resolvedTypeClass.IsTypeInInheritanceTree(content.GetClass(baseClass, 0)));
        }
Exemplo n.º 9
0
        void FindOverrides(object sender, EventArgs e)
        {
            MenuCommand              item           = (MenuCommand)sender;
            IMember                  member         = (IMember)item.Tag;
            IEnumerable <IClass>     derivedClasses = RefactoringService.FindDerivedClasses(member.DeclaringType, ParserService.AllProjectContents, false);
            List <SearchResultMatch> results        = new List <SearchResultMatch>();
            IAmbience                ambience       = AmbienceService.GetCurrentAmbience();

            ambience.ConversionFlags = ConversionFlags.UseFullyQualifiedMemberNames | ConversionFlags.ShowTypeParameterList;
            foreach (IClass derivedClass in derivedClasses)
            {
                if (derivedClass.CompilationUnit == null)
                {
                    continue;
                }
                if (derivedClass.CompilationUnit.FileName == null)
                {
                    continue;
                }
                IMember m = MemberLookupHelper.FindSimilarMember(derivedClass, member);
                if (m != null && !m.Region.IsEmpty)
                {
                    string            matchText = ambience.Convert(m);
                    SearchResultMatch res       = new SimpleSearchResultMatch(matchText, new TextLocation(m.Region.BeginColumn - 1, m.Region.BeginLine - 1));
                    res.ProvidedDocumentInformation = FindReferencesAndRenameHelper.GetDocumentInformation(m.DeclaringType.CompilationUnit.FileName);
                    results.Add(res);
                }
            }
            SearchResultPanel.Instance.ShowSearchResults(new SearchResult(
                                                             StringParser.Parse("${res:SharpDevelop.Refactoring.OverridesOf}", new string[, ] {
                { "Name", member.Name }
            }),
                                                             results
                                                             ));
        }
Exemplo n.º 10
0
        public void GetCommonTypeOfNullAndString()
        {
            IReturnType res = MemberLookupHelper.GetCommonType(msc,
                                                               NullReturnType.Instance,
                                                               msc.GetClass("System.String", 0).DefaultReturnType);

            Assert.AreEqual("System.String", res.FullyQualifiedName);
        }
Exemplo n.º 11
0
        public void GetCommonType()
        {
            IReturnType res = MemberLookupHelper.GetCommonType(msc,
                                                               swf.GetClass("System.Windows.Forms.ToolStripButton", 0).DefaultReturnType,
                                                               swf.GetClass("System.Windows.Forms.ToolStripSeparator", 0).DefaultReturnType);

            Assert.AreEqual("System.Windows.Forms.ToolStripItem", res.FullyQualifiedName);
        }
Exemplo n.º 12
0
        public void TypeParameterPassedToBaseClassSameClass()
        {
            IReturnType[] stringArr = { msc.SystemTypes.String };
            IReturnType   rrt       = new ConstructedReturnType(EnumerableClass.DefaultReturnType, stringArr);
            IReturnType   res       = MemberLookupHelper.GetTypeParameterPassedToBaseClass(rrt, EnumerableClass, 0);

            Assert.AreEqual("System.String", res.FullyQualifiedName);
        }
Exemplo n.º 13
0
        public void ArrayOfStringIsApplicableOnArrayOfT()
        {
            Assert.IsTrue(IsApplicable(new ArrayReturnType(msc, msc.SystemTypes.String, 1),
                                       new ArrayReturnType(msc, CreateT(), 1)));

            Assert.IsFalse(MemberLookupHelper.ConversionExists(new ArrayReturnType(msc, msc.SystemTypes.String, 1),
                                                               new ArrayReturnType(msc, CreateT(), 1)));
        }
Exemplo n.º 14
0
        public void DisposableClassIsApplicableOnDisposableT()
        {
            Assert.IsFalse(MemberLookupHelper.ConversionExists(msc.GetClass("System.CharEnumerator", 0).DefaultReturnType,
                                                               CreateTWithDisposableConstraint()));

            Assert.IsTrue(IsApplicable(msc.GetClass("System.CharEnumerator", 0).DefaultReturnType,
                                       CreateTWithDisposableConstraint()));
        }
Exemplo n.º 15
0
        public void TypeParameterPassedToBaseClassTestClassDerivingFromList()
        {
            DefaultClass listDerivingClass = CreateClassDerivingFromListOfString();

            IReturnType res = MemberLookupHelper.GetTypeParameterPassedToBaseClass(listDerivingClass.DefaultReturnType,
                                                                                   EnumerableClass, 0);

            Assert.AreEqual("System.String", res.FullyQualifiedName);
        }
Exemplo n.º 16
0
        public void StringIsApplicableOnT()
        {
            // no conversion exists
            Assert.IsFalse(MemberLookupHelper.ConversionExists(msc.SystemTypes.String,
                                                               CreateT()));

            // but it is applicable
            Assert.IsTrue(IsApplicable(msc.SystemTypes.String,
                                       CreateT()));
        }
Exemplo n.º 17
0
        public void NoConversionExistsFromStringToDisposableT()
        {
            // no conversion exists
            Assert.IsFalse(MemberLookupHelper.ConversionExists(msc.SystemTypes.String,
                                                               CreateTWithDisposableConstraint()));

            // but it is applicable (applicability ignores constraints)
            Assert.IsTrue(IsApplicable(msc.SystemTypes.String,
                                       CreateTWithDisposableConstraint()));
        }
Exemplo n.º 18
0
        static IEnumerable <IMember> GetOverridableMembers(IClass c)
        {
            if (c == null)
            {
                throw new ArgumentException("c");
            }

            return(MemberLookupHelper.GetAccessibleMembers(c.BaseType, c, c.ProjectContent.Language, true)
                   .Where(m => m.IsOverridable && !m.IsConst));
        }
Exemplo n.º 19
0
        public void NoConversionExistsFromParameterlessAnonymousDelegateToSystemPredicate()
        {
            AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(new DefaultCompilationUnit(msc));

            amrt.MethodParameters = new List <IParameter>();
            Assert.IsFalse(MemberLookupHelper.ConversionExists(
                               amrt,
                               new GetClassReturnType(msc, "System.Predicate", 1)
                               ));
        }
Exemplo n.º 20
0
 IProperty GetIndexer(IReturnType target, List <Expression> indexes)
 {
     if (target == null)
     {
         return(null);
     }
     return(MemberLookupHelper.FindOverload(
                target.GetProperties().Where((IProperty p) => p.IsIndexer).ToList(),
                indexes.Select <Expression, IReturnType>(ResolveType).ToArray()
                ));
 }
Exemplo n.º 21
0
        void GoToBase(object sender, EventArgs e)
        {
            MenuCommand item       = (MenuCommand)sender;
            IMember     member     = (IMember)item.Tag;
            IMember     baseMember = MemberLookupHelper.FindBaseMember(member);

            if (baseMember != null)
            {
                FindReferencesAndRenameHelper.JumpToDefinition(baseMember);
            }
        }
Exemplo n.º 22
0
        public static void ImplementAbstractClass(IRefactoringDocument doc, IClass target, IReturnType abstractClass)
        {
            CodeGenerator generator = target.ProjectContent.Language.CodeGenerator;
            var           pos       = doc.OffsetToPosition(doc.PositionToOffset(target.BodyRegion.EndLine, target.BodyRegion.EndColumn) - 1);
            ClassFinder   context   = new ClassFinder(target, pos.Line, pos.Column);

            foreach (IMember member in MemberLookupHelper.GetAccessibleMembers(abstractClass, target, LanguageProperties.CSharp, true)
                     .Where(m => m.IsAbstract && !target.HasMember(m)))
            {
                generator.InsertCodeAtEnd(target.BodyRegion, doc, generator.GetOverridingMethod(member, context));
            }
        }
Exemplo n.º 23
0
        public void TypeParameterPassedToBaseClassTestGenericClassDerivingFromList()
        {
            DefaultClass listDerivingClass = CreateGenericClassDerivingFromList();

            ConstructedReturnType testType = new ConstructedReturnType(listDerivingClass.DefaultReturnType,
                                                                       new IReturnType[] { msc.SystemTypes.String });

            IReturnType res = MemberLookupHelper.GetTypeParameterPassedToBaseClass(testType,
                                                                                   EnumerableClass, 0);

            Assert.AreEqual("System.String", res.FullyQualifiedName);
        }
Exemplo n.º 24
0
        public void TypeParameterPassedToBaseClassTestDictionary()
        {
            IReturnType[] stringInt = { msc.SystemTypes.String, msc.SystemTypes.Int32 };
            IReturnType   rrt       = new ConstructedReturnType(DictionaryRT, stringInt);
            IReturnType   res       = MemberLookupHelper.GetTypeParameterPassedToBaseClass(rrt, EnumerableClass, 0);

            Assert.AreEqual("System.Collections.Generic.KeyValuePair", res.FullyQualifiedName);
            ConstructedReturnType resc = res.CastToConstructedReturnType();

            Assert.AreEqual("System.String", resc.TypeArguments[0].FullyQualifiedName);
            Assert.AreEqual("System.Int32", resc.TypeArguments[1].FullyQualifiedName);
        }
Exemplo n.º 25
0
        public void ConversionDoesNotExistFromAnonymousDelegateWithParameterToSystemPredicateWhenParameterTypeIsIncompatible()
        {
            AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(new DefaultCompilationUnit(msc));

            amrt.MethodReturnType = msc.SystemTypes.Boolean;
            amrt.MethodParameters = new List <IParameter>();
            amrt.MethodParameters.Add(new DefaultParameter("test", msc.SystemTypes.String, DomRegion.Empty));
            Assert.IsFalse(MemberLookupHelper.ConversionExists(
                               amrt,
                               new ConstructedReturnType(new GetClassReturnType(msc, "System.Predicate", 1),
                                                         new IReturnType[] { msc.SystemTypes.Int32 })
                               ));
        }
Exemplo n.º 26
0
        public override IReturnType ResolveReturnType(IReturnType[] parameterTypes)
        {
            if (lambdaExpression == null)
            {
                return(ResolveReturnType());
            }

            try {
                MemberLookupHelper.Log("LambdaReturnType: SetImplicitLambdaParameterTypes ", parameterTypes);
                resolver.SetImplicitLambdaParameterTypes(lambdaExpression, parameterTypes);
                return(ResolveReturnType());
            } finally {
                resolver.UnsetImplicitLambdaParameterTypes(lambdaExpression);
            }
        }
Exemplo n.º 27
0
            protected ContextActionViewModel MakeGoToMemberAction(IClass containingClass, ObservableCollection <ContextActionViewModel> childActions)
            {
                var overridenMember = MemberLookupHelper.FindSimilarMember(containingClass, this.member);

                if (overridenMember == null || overridenMember.Region.IsEmpty)
                {
                    return(null);
                }

                return(new ContextActionViewModel {
                    Action = new GoToMemberAction(overridenMember, this.LabelAmbience),
                    Image = ClassBrowserIconService.GetIcon(overridenMember).ImageSource,
                    Comment = string.Format("(in {0})", containingClass.FullyQualifiedName),
                    ChildActions = childActions
                });
            }
Exemplo n.º 28
0
        public override void OnBinaryExpression(BinaryExpression node)
        {
            switch (node.Operator)
            {
            case BinaryOperatorType.GreaterThan:
            case BinaryOperatorType.GreaterThanOrEqual:
            case BinaryOperatorType.Equality:
            case BinaryOperatorType.Inequality:
            case BinaryOperatorType.LessThan:
            case BinaryOperatorType.LessThanOrEqual:
            case BinaryOperatorType.Match:
            case BinaryOperatorType.Member:
            case BinaryOperatorType.NotMatch:
            case BinaryOperatorType.NotMember:
            case BinaryOperatorType.ReferenceEquality:
            case BinaryOperatorType.ReferenceInequality:
            case BinaryOperatorType.TypeTest:
                MakeResult(projectContent.SystemTypes.Boolean);
                break;

            default:
                if (node.Left == null)
                {
                    if (node.Right == null)
                    {
                        ClearResult();
                    }
                    else
                    {
                        node.Right.Accept(this);
                    }
                    return;
                }
                else if (node.Right == null)
                {
                    node.Left.Accept(this);
                    return;
                }
                node.Left.Accept(this);
                IReturnType left = (resolveResult != null) ? resolveResult.ResolvedType : null;
                node.Right.Accept(this);
                IReturnType right = (resolveResult != null) ? resolveResult.ResolvedType : null;
                MakeResult(MemberLookupHelper.GetCommonType(projectContent, left, right));
                break;
            }
        }
Exemplo n.º 29
0
        public IMethod FindOverload(List <IMethod> methods, IReturnType[] typeParameters, IList <Expression> arguments, object data)
        {
            if (methods.Count <= 0)
            {
                return(null);
            }
            // We can't use this shortcut because MemberLookupHelper does type inference and
            // type substitution for us
            //if (methods.Count == 1)
            //	return methods[0];

            IReturnType[] types = new IReturnType[arguments.Count];
            for (int i = 0; i < types.Length; ++i)
            {
                types[i] = arguments[i].AcceptVisitor(this, data) as IReturnType;
            }
            return(MemberLookupHelper.FindOverload(methods, typeParameters, types));
        }
Exemplo n.º 30
0
        public void GetTypeInheritanceTreeOfStringArray()
        {
            List <string> results = new List <IReturnType>(
                MemberLookupHelper.GetTypeInheritanceTree(new ArrayReturnType(msc, msc.SystemTypes.String, 1))
                ).ConvertAll <string>(delegate(IReturnType rt) { return(rt.DotNetName); });

            results.Sort();             // order is not guaranteed, so sort for the unit test

            Assert.AreEqual("System.Collections.Generic.ICollection{System.String};" +
                            "System.Collections.Generic.IEnumerable{System.String};" +
                            "System.Collections.Generic.IList{System.String};" +
                            "System.Collections.ICollection;" +
                            "System.Collections.IEnumerable;" +
                            "System.Collections.IList;" +
                            "System.Object;" +
                            "System.String[]",
                            string.Join(";", results.ToArray()));
        }