示例#1
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));
        }
        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);
        }
 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()
                ));
 }
示例#4
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));
        }
示例#5
0
        public override void OnSlicingExpression(SlicingExpression node)
        {
            ClearResult();
            Visit(node.Target);
            if (node.Indices.Count > 0)
            {
                Slice slice = node.Indices[0];
                if (slice.End != null)
                {
                    // Boo slice, returns a part of the source -> same type as source
                    return;
                }
            }
            IReturnType rt = (resolveResult != null) ? resolveResult.ResolvedType : null;

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

            // remove non-indexers:
            for (int i = 0; i < indexers.Count; i++)
            {
                if (!indexers[i].IsIndexer)
                {
                    indexers.RemoveAt(i--);
                }
            }
            IReturnType[] parameters = new IReturnType[node.Indices.Count];
            for (int i = 0; i < parameters.Length; i++)
            {
                Expression expr = node.Indices[i].Begin as Expression;
                if (expr != null)
                {
                    ClearResult();
                    Visit(expr);
                    parameters[i] = (resolveResult != null) ? resolveResult.ResolvedType : null;
                }
            }
            MakeResult(MemberLookupHelper.FindOverload(indexers.ToArray(), parameters));
        }
示例#6
0
        void ResolveInvocation(List <IMethod> methods, ExpressionCollection arguments)
        {
            ClearResult();
            if (methods.Count == 0)
            {
                return;
            }
            // MemberLookupHelper does type argument inference and type substitution for us
            IReturnType[] types = new IReturnType[arguments.Count];
            for (int i = 0; i < types.Length; ++i)
            {
                arguments[i].Accept(this);
                types[i] = (resolveResult != null) ? resolveResult.ResolvedType : null;
                ClearResult();
            }
            bool resultIsAcceptable;

            MakeResult(MemberLookupHelper.FindOverload(methods, types, out resultIsAcceptable));
        }
示例#7
0
        ResolveResult FallbackResolveMethod(InvocationExpression invocation, MethodGroupResolveResult mgrr, IReturnType[] argumentTypes)
        {
            // method not found, let's try if we can find a method if we violate the
            // accessibility rules
            MemberReferenceExpression mre = invocation.TargetObject as MemberReferenceExpression;

            if (mre != null)
            {
                List <IMethod> methods = mgrr.ContainingType.GetMethods().Where(m => resolver.IsSameName(m.Name, mre.MemberName)).ToList();
                bool           resultIsAcceptable;
                IMethod        result = MemberLookupHelper.FindOverload(
                    methods, argumentTypes, out resultIsAcceptable);
                if (result != null)
                {
                    return(CreateMemberResolveResult(result));
                }
            }

            return(resolver.CreateUnknownMethodResolveResult(invocation));
        }
        public override object VisitInvocationExpression(InvocationExpression invocationExpression, object data)
        {
            if (resolver.Language == SupportedLanguage.CSharp && resolver.CallingClass != null)
            {
                if (invocationExpression.TargetObject is ThisReferenceExpression)
                {
                    // call to constructor
                    return(ResolveConstructorOverload(resolver.CallingClass, invocationExpression.Arguments));
                }
                else if (invocationExpression.TargetObject is BaseReferenceExpression)
                {
                    return(ResolveConstructorOverload(resolver.CallingClass.BaseType, invocationExpression.Arguments));
                }
            }

            ResolveResult      rr      = Resolve(invocationExpression.TargetObject);
            MixedResolveResult mixedRR = rr as MixedResolveResult;

            if (mixedRR != null)
            {
                rr = mixedRR.PrimaryResult;
            }
            MethodGroupResolveResult mgrr = rr as MethodGroupResolveResult;

            if (mgrr != null)
            {
                if (resolver.Language == SupportedLanguage.VBNet && mgrr.Methods.All(mg => mg.Count == 0))
                {
                    return(CreateMemberResolveResult(GetVisualBasicIndexer(invocationExpression)));
                }

                IReturnType[] argumentTypes = invocationExpression.Arguments.Select <Expression, IReturnType>(ResolveType).ToArray();

                MemberResolveResult firstResult = null;
                foreach (MethodGroup methodGroup in mgrr.Methods)
                {
                    bool    resultIsAcceptable;
                    IMethod method;
                    if (methodGroup.IsExtensionMethodGroup)
                    {
                        IReturnType[] extendedTypes = new IReturnType[argumentTypes.Length + 1];
                        extendedTypes[0] = mgrr.ContainingType;
                        argumentTypes.CopyTo(extendedTypes, 1);
                        method = MemberLookupHelper.FindOverload(methodGroup, extendedTypes, out resultIsAcceptable);
                    }
                    else
                    {
                        method = MemberLookupHelper.FindOverload(methodGroup, argumentTypes, out resultIsAcceptable);
                    }
                    MemberResolveResult result = CreateMemberResolveResult(method);
                    if (result != null && methodGroup.IsExtensionMethodGroup)
                    {
                        result.IsExtensionMethodCall = true;
                    }
                    if (resultIsAcceptable)
                    {
                        return(result);
                    }
                    if (firstResult == null)
                    {
                        firstResult = result;
                    }
                }
                if (firstResult != null)
                {
                    return(firstResult);
                }
                else
                {
                    return(FallbackResolveMethod(invocationExpression, mgrr, argumentTypes));
                }
            }
            else if (rr != null && rr.ResolvedType != null)
            {
                IClass c = rr.ResolvedType.GetUnderlyingClass();
                if (c != null && c.ClassType == ClassType.Delegate)
                {
                    // We don't want to show "System.EventHandler.Invoke" in the tooltip
                    // of "EventCall(this, EventArgs.Empty)", we just show the event/delegate for now
                    // but for DelegateCall(params).* completion, we use the delegate's
                    // return type instead of the delegate type itself

                    IMethod method = rr.ResolvedType.GetMethods().FirstOrDefault(innerMethod => innerMethod.Name == "Invoke");
                    if (method != null)
                    {
                        return(new DelegateCallResolveResult(rr, method));
                    }
                }
            }
            if (resolver.Language == SupportedLanguage.VBNet)
            {
                return(CreateMemberResolveResult(GetVisualBasicIndexer(invocationExpression)));
            }
            return(null);
        }