Beispiel #1
0
        Conversion MethodGroupConversion(ResolveResult resolveResult, IType toType)
        {
            // C# 4.0 spec §6.6 Method group conversions
            MethodGroupResolveResult rr = resolveResult as MethodGroupResolveResult;

            if (rr == null)
            {
                return(Conversion.None);
            }
            IMethod m = toType.GetDelegateInvokeMethod();

            if (m == null)
            {
                return(Conversion.None);
            }

            ResolveResult[] args = new ResolveResult[m.Parameters.Count];
            for (int i = 0; i < args.Length; i++)
            {
                IParameter param         = m.Parameters[i];
                IType      parameterType = param.Type;
                if ((param.IsRef || param.IsOut) && parameterType.Kind == TypeKind.ByReference)
                {
                    parameterType = ((ByReferenceType)parameterType).ElementType;
                    args[i]       = new ByReferenceResolveResult(parameterType, param.IsOut);
                }
                else
                {
                    args[i] = new ResolveResult(parameterType);
                }
            }
            var or = rr.PerformOverloadResolution(compilation, args, allowExpandingParams: false, conversions: this);

            if (or.FoundApplicableCandidate)
            {
                IMethod method    = (IMethod)or.GetBestCandidateWithSubstitutedTypeArguments();
                var     thisRR    = rr.TargetResult as ThisResolveResult;
                bool    isVirtual = method.IsOverridable && !(thisRR != null && thisRR.CausesNonVirtualInvocation);
                return(Conversion.MethodGroupConversion(method, isVirtual));
            }
            else
            {
                return(Conversion.None);
            }
        }
Beispiel #2
0
        Conversion MethodGroupConversion(ResolveResult resolveResult, IType toType)
        {
            // C# 4.0 spec §6.6 Method group conversions
            MethodGroupResolveResult rr = resolveResult as MethodGroupResolveResult;

            if (rr == null)
            {
                return(Conversion.None);
            }
            IMethod m = toType.GetDelegateInvokeMethod();

            if (m == null)
            {
                return(Conversion.None);
            }

            ResolveResult[] args = new ResolveResult[m.Parameters.Count];
            for (int i = 0; i < args.Length; i++)
            {
                IParameter param         = m.Parameters[i];
                IType      parameterType = param.Type.Resolve(context);
                if ((param.IsRef || param.IsOut) && parameterType.Kind == TypeKind.ByReference)
                {
                    parameterType = ((ByReferenceType)parameterType).ElementType;
                    args[i]       = new ByReferenceResolveResult(parameterType, param.IsOut);
                }
                else
                {
                    args[i] = new ResolveResult(parameterType);
                }
            }
            var or = rr.PerformOverloadResolution(context, args, allowExpandingParams: false);

            if (or.FoundApplicableCandidate)
            {
                return(Conversion.MethodGroupConversion((IMethod)or.BestCandidate));
            }
            else
            {
                return(Conversion.None);
            }
        }
Beispiel #3
0
        void MakeOutputTypeInference(ResolveResult e, IType t)
        {
            Log.WriteLine(" MakeOutputTypeInference from " + e + " to " + t);
            // If E is an anonymous function with inferred return type  U (§7.5.2.12) and T is a delegate type or expression
            // tree type with return type Tb, then a lower-bound inference (§7.5.2.9) is made from U to Tb.
            LambdaResolveResult lrr = e as LambdaResolveResult;

            if (lrr != null)
            {
                IMethod m = GetDelegateOrExpressionTreeSignature(t);
                if (m != null)
                {
                    IType inferredReturnType;
                    if (lrr.IsImplicitlyTyped)
                    {
                        if (m.Parameters.Count != lrr.Parameters.Count)
                        {
                            return;                             // cannot infer due to mismatched parameter lists
                        }
                        TypeParameterSubstitution substitution = GetSubstitutionForFixedTPs();
                        IType[] inferredParameterTypes         = new IType[m.Parameters.Count];
                        for (int i = 0; i < inferredParameterTypes.Length; i++)
                        {
                            IType parameterType = m.Parameters[i].Type;
                            inferredParameterTypes[i] = parameterType.AcceptVisitor(substitution);
                        }
                        inferredReturnType = lrr.GetInferredReturnType(inferredParameterTypes);
                    }
                    else
                    {
                        inferredReturnType = lrr.GetInferredReturnType(null);
                    }
                    MakeLowerBoundInference(inferredReturnType, m.ReturnType);
                    return;
                }
            }
            // Otherwise, if E is a method group and T is a delegate type or expression tree type
            // with parameter types T1…Tk and return type Tb, and overload resolution
            // of E with the types T1…Tk yields a single method with return type U, then a lower­-bound
            // inference is made from U to Tb.
            MethodGroupResolveResult mgrr = e as MethodGroupResolveResult;

            if (mgrr != null)
            {
                IMethod m = GetDelegateOrExpressionTreeSignature(t);
                if (m != null)
                {
                    ResolveResult[]           args         = new ResolveResult[m.Parameters.Count];
                    TypeParameterSubstitution substitution = GetSubstitutionForFixedTPs();
                    for (int i = 0; i < args.Length; i++)
                    {
                        IParameter param         = m.Parameters[i];
                        IType      parameterType = param.Type.AcceptVisitor(substitution);
                        if ((param.IsRef || param.IsOut) && parameterType.Kind == TypeKind.ByReference)
                        {
                            parameterType = ((ByReferenceType)parameterType).ElementType;
                            args[i]       = new ByReferenceResolveResult(parameterType, param.IsOut);
                        }
                        else
                        {
                            args[i] = new ResolveResult(parameterType);
                        }
                    }
                    var or = mgrr.PerformOverloadResolution(compilation,
                                                            args,
                                                            allowExpandingParams: false, allowOptionalParameters: false);
                    if (or.FoundApplicableCandidate && or.BestCandidateAmbiguousWith == null)
                    {
                        IType returnType = or.GetBestCandidateWithSubstitutedTypeArguments().ReturnType;
                        MakeLowerBoundInference(returnType, m.ReturnType);
                    }
                }
                return;
            }
            // Otherwise, if E is an expression with type U, then a lower-bound inference is made from U to T.
            if (IsValidType(e.Type))
            {
                MakeLowerBoundInference(e.Type, t);
            }
        }