Ejemplo n.º 1
0
        public OverloadResolution(ICompilation compilation, AST.Expression[] arguments, string[] argumentNames = null, IType[] typeArguments = null, VSharpConversions conversions = null)
        {
            if (compilation == null)
            {
                throw new ArgumentNullException("compilation");
            }
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }
            if (argumentNames == null)
            {
                argumentNames = new string[arguments.Length];
            }
            else if (argumentNames.Length != arguments.Length)
            {
                throw new ArgumentException("argumentsNames.Length must be equal to arguments.Length");
            }
            this.compilation   = compilation;
            this.arguments     = arguments;
            this.argumentNames = argumentNames;

            // keep explicitlyGivenTypeArguments==null when no type arguments were specified
            if (typeArguments != null && typeArguments.Length > 0)
            {
                this.explicitlyGivenTypeArguments = typeArguments;
            }

            this.conversions             = conversions ?? VSharpConversions.Get(compilation);
            this.AllowExpandingParams    = true;
            this.AllowOptionalParameters = true;
        }
Ejemplo n.º 2
0
 internal TypeInference(ICompilation compilation, VSharpConversions conversions)
 {
     Debug.Assert(compilation != null);
     Debug.Assert(conversions != null);
     this.compilation = compilation;
     this.conversions = conversions;
 }
Ejemplo n.º 3
0
 public TypeInference(ICompilation compilation)
 {
     if (compilation == null)
     {
         throw new ArgumentNullException("compilation");
     }
     this.compilation = compilation;
     this.conversions = VSharpConversions.Get(compilation);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Validates whether the given type argument satisfies the constraints for the given type parameter.
 /// </summary>
 /// <param name="typeParameter">The type parameter.</param>
 /// <param name="typeArgument">The type argument.</param>
 /// <param name="substitution">The substitution that defines how type parameters are replaced with type arguments.
 /// The substitution is used to check constraints that depend on other type parameters (or recursively on the same type parameter).
 /// May be null if no substitution should be used.</param>
 /// <returns>True if the constraints are satisfied; false otherwise.</returns>
 public static bool ValidateConstraints(ITypeParameter typeParameter, IType typeArgument, TypeVisitor substitution = null)
 {
     if (typeParameter == null)
     {
         throw new ArgumentNullException("typeParameter");
     }
     if (typeArgument == null)
     {
         throw new ArgumentNullException("typeArgument");
     }
     return(ValidateConstraints(typeParameter, typeArgument, substitution, VSharpConversions.Get(typeParameter.Owner.Compilation)));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Gets whether the lambda body is valid for the given parameter types and return type.
 /// </summary>
 /// <returns>
 /// Produces a conversion with <see cref="Conversion.IsAnonymousFunctionConversion"/>=<c>true</c> if the lambda is valid;
 /// otherwise returns <see cref="Conversion.None"/>.
 /// </returns>
 public abstract Conversion IsValid(IType[] parameterTypes, IType returnType, VSharpConversions conversions);
Ejemplo n.º 6
0
        public OverloadResolution PerformOverloadResolution(ICompilation compilation, Expression[] arguments, string[] argumentNames = null,
                                                            bool allowExtensionMethods = true,
                                                            bool allowExpandingParams = true,
                                                            bool allowOptionalParameters = true,
                                                            bool checkForOverflow = false, VSharpConversions conversions = null)
        {

            var typeArgumentArray = this.TypeArguments.ToArray();
            OverloadResolution or = new OverloadResolution(compilation, arguments, argumentNames, typeArgumentArray, conversions);
            or.AllowExpandingParams = allowExpandingParams;
            or.AllowOptionalParameters = allowOptionalParameters;
            or.CheckForOverflow = checkForOverflow;

            or.AddMethodLists(methodLists);

            if (allowExtensionMethods && !or.FoundApplicableCandidate)
            {
                // No applicable match found, so let's try extension methods.

                var extensionMethods = this.GetExtensionMethods();

                if (extensionMethods.Any())
                {

                    Expression[] extArguments = new Expression[arguments.Length + 1];
                    extArguments[0] = new AST.Expression(this.TargetType);
                    arguments.CopyTo(extArguments, 1);
                    string[] extArgumentNames = null;
                    if (argumentNames != null)
                    {
                        extArgumentNames = new string[argumentNames.Length + 1];
                        argumentNames.CopyTo(extArgumentNames, 1);
                    }
                    var extOr = new OverloadResolution(compilation, extArguments, extArgumentNames, typeArgumentArray, conversions);
                    extOr.AllowExpandingParams = allowExpandingParams;
                    extOr.AllowOptionalParameters = allowOptionalParameters;
                    extOr.IsExtensionMethodInvocation = true;
                    extOr.CheckForOverflow = checkForOverflow;

                    foreach (var g in extensionMethods)
                    {
                        foreach (var method in g)
                            extOr.AddCandidate(method);

                        if (extOr.FoundApplicableCandidate)
                            break;
                    }
                    // For the lack of a better comparison function (the one within OverloadResolution
                    // cannot be used as it depends on the argument set):
                    if (extOr.FoundApplicableCandidate || or.BestCandidate == null)
                    {
                        // Consider an extension method result better than the normal result only
                        // if it's applicable; or if there is no normal result.
                        or = extOr;
                    }
                }
            }

            return or;
        }
Ejemplo n.º 7
0
 internal static bool ValidateConstraints(ITypeParameter typeParameter, IType typeArgument, TypeVisitor substitution, VSharpConversions conversions)
 {
     switch (typeArgument.Kind)
     { // void, null, and pointers cannot be used as type arguments
     case TypeKind.Void:
     case TypeKind.Null:
     case TypeKind.Pointer:
         return(false);
     }
     if (typeParameter.HasReferenceTypeConstraint)
     {
         if (typeArgument.IsReferenceType != true)
         {
             return(false);
         }
     }
     if (typeParameter.HasValueTypeConstraint)
     {
         if (!NullableType.IsNonNullableValueType(typeArgument))
         {
             return(false);
         }
     }
     if (typeParameter.HasDefaultConstructorConstraint)
     {
         ITypeDefinition def = typeArgument.GetDefinition();
         if (def != null && def.IsAbstract)
         {
             return(false);
         }
         var ctors = typeArgument.GetConstructors(
             m => m.Parameters.Count == 0 && m.Accessibility == Accessibility.Public,
             GetMemberOptions.IgnoreInheritedMembers | GetMemberOptions.ReturnMemberDefinitions
             );
         if (!ctors.Any())
         {
             return(false);
         }
     }
     foreach (IType constraintType in typeParameter.DirectBaseTypes)
     {
         IType c = constraintType;
         if (substitution != null)
         {
             c = c.AcceptVisitor(substitution);
         }
         if (!conversions.IsConstraintConvertible(typeArgument, c))
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 8
0
 public ConstraintValidatingSubstitution(IList <IType> classTypeArguments, IList <IType> methodTypeArguments, OverloadResolution overloadResolution)
     : base(classTypeArguments, methodTypeArguments)
 {
     this.conversions = overloadResolution.conversions;
 }