コード例 #1
0
        void RunTypeInference(Candidate candidate)
        {
            IMethod method = candidate.Member as IMethod;

            if (method == null || method.TypeParameters.Count == 0)
            {
                if (explicitlyGivenTypeArguments != null)
                {
                    // method does not expect type arguments, but was given some
                    candidate.AddError(OverloadResolutionErrors.WrongNumberOfTypeArguments);
                }
                return;
            }
            // The method is generic:
            if (explicitlyGivenTypeArguments != null)
            {
                if (explicitlyGivenTypeArguments.Length == method.TypeParameters.Count)
                {
                    candidate.InferredTypes = explicitlyGivenTypeArguments;
                }
                else
                {
                    candidate.AddError(OverloadResolutionErrors.WrongNumberOfTypeArguments);
                    // wrong number of type arguments given, so truncate the list or pad with UnknownType
                    candidate.InferredTypes = new IType[method.TypeParameters.Count];
                    for (int i = 0; i < candidate.InferredTypes.Length; i++)
                    {
                        if (i < explicitlyGivenTypeArguments.Length)
                        {
                            candidate.InferredTypes[i] = explicitlyGivenTypeArguments[i];
                        }
                        else
                        {
                            candidate.InferredTypes[i] = SharedTypes.UnknownType;
                        }
                    }
                }
            }
            else
            {
                TypeInference ti = new TypeInference(context, conversions);
                bool          success;
                candidate.InferredTypes = ti.InferTypeArguments(method.TypeParameters, arguments, candidate.ParameterTypes, out success);
                if (!success)
                {
                    candidate.AddError(OverloadResolutionErrors.TypeInferenceFailed);
                }
            }
            // Now substitute in the formal parameters:
            var substitution = new ConstraintValidatingSubstitution(candidate.InferredTypes, this);

            for (int i = 0; i < candidate.ParameterTypes.Length; i++)
            {
                candidate.ParameterTypes[i] = candidate.ParameterTypes[i].AcceptVisitor(substitution);
            }
            if (!substitution.ConstraintsValid)
            {
                candidate.AddError(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint);
            }
        }
コード例 #2
0
        void RunTypeInference(Candidate candidate)
        {
            if (candidate.TypeParameters == null)
            {
                if (explicitlyGivenTypeArguments != null)
                {
                    // method does not expect type arguments, but was given some
                    candidate.AddError(OverloadResolutionErrors.WrongNumberOfTypeArguments);
                }
                // Grab new parameter types:
                ResolveParameterTypes(candidate, true);
                return;
            }
            ParameterizedType parameterizedDeclaringType = candidate.Member.DeclaringType as ParameterizedType;
            IList <IType>     classTypeArguments;

            if (parameterizedDeclaringType != null)
            {
                classTypeArguments = parameterizedDeclaringType.TypeArguments;
            }
            else
            {
                classTypeArguments = null;
            }
            // The method is generic:
            if (explicitlyGivenTypeArguments != null)
            {
                if (explicitlyGivenTypeArguments.Length == candidate.TypeParameters.Count)
                {
                    candidate.InferredTypes = explicitlyGivenTypeArguments;
                }
                else
                {
                    candidate.AddError(OverloadResolutionErrors.WrongNumberOfTypeArguments);
                    // wrong number of type arguments given, so truncate the list or pad with UnknownType
                    candidate.InferredTypes = new IType[candidate.TypeParameters.Count];
                    for (int i = 0; i < candidate.InferredTypes.Length; i++)
                    {
                        if (i < explicitlyGivenTypeArguments.Length)
                        {
                            candidate.InferredTypes[i] = explicitlyGivenTypeArguments[i];
                        }
                        else
                        {
                            candidate.InferredTypes[i] = SpecialType.UnknownType;
                        }
                    }
                }
            }
            else
            {
                TypeInference ti = new TypeInference(compilation, conversions);
                bool          success;
                candidate.InferredTypes = ti.InferTypeArguments(candidate.TypeParameters, arguments, candidate.ParameterTypes, out success, classTypeArguments);
                if (!success)
                {
                    candidate.AddError(OverloadResolutionErrors.TypeInferenceFailed);
                }
            }
            // Now substitute in the formal parameters:
            var substitution = new ConstraintValidatingSubstitution(classTypeArguments, candidate.InferredTypes, this);

            for (int i = 0; i < candidate.ParameterTypes.Length; i++)
            {
                candidate.ParameterTypes[i] = candidate.ParameterTypes[i].AcceptVisitor(substitution);
            }
            if (!substitution.ConstraintsValid)
            {
                candidate.AddError(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint);
            }
        }
コード例 #3
0
ファイル: OverloadResolution.cs プロジェクト: Netring/ILSpy
		void RunTypeInference(Candidate candidate)
		{
			IMethod method = candidate.Member as IMethod;
			if (method == null || method.TypeParameters.Count == 0) {
				if (explicitlyGivenTypeArguments != null) {
					// method does not expect type arguments, but was given some
					candidate.AddError(OverloadResolutionErrors.WrongNumberOfTypeArguments);
				}
				return;
			}
			ParameterizedType parameterizedDeclaringType = candidate.Member.DeclaringType as ParameterizedType;
			IList<IType> classTypeArguments;
			if (parameterizedDeclaringType != null) {
				classTypeArguments = parameterizedDeclaringType.TypeArguments;
			} else {
				classTypeArguments = null;
			}
			// The method is generic:
			if (explicitlyGivenTypeArguments != null) {
				if (explicitlyGivenTypeArguments.Length == method.TypeParameters.Count) {
					candidate.InferredTypes = explicitlyGivenTypeArguments;
				} else {
					candidate.AddError(OverloadResolutionErrors.WrongNumberOfTypeArguments);
					// wrong number of type arguments given, so truncate the list or pad with UnknownType
					candidate.InferredTypes = new IType[method.TypeParameters.Count];
					for (int i = 0; i < candidate.InferredTypes.Length; i++) {
						if (i < explicitlyGivenTypeArguments.Length)
							candidate.InferredTypes[i] = explicitlyGivenTypeArguments[i];
						else
							candidate.InferredTypes[i] = SpecialType.UnknownType;
					}
				}
			} else {
				TypeInference ti = new TypeInference(compilation, conversions);
				bool success;
				candidate.InferredTypes = ti.InferTypeArguments(candidate.TypeParameters, arguments, candidate.ParameterTypes, out success, classTypeArguments);
				if (!success)
					candidate.AddError(OverloadResolutionErrors.TypeInferenceFailed);
			}
			// Now substitute in the formal parameters:
			var substitution = new ConstraintValidatingSubstitution(classTypeArguments, candidate.InferredTypes, this);
			for (int i = 0; i < candidate.ParameterTypes.Length; i++) {
				candidate.ParameterTypes[i] = candidate.ParameterTypes[i].AcceptVisitor(substitution);
			}
			if (!substitution.ConstraintsValid)
				candidate.AddError(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint);
		}