public AstFunctionReferenceExpression(IAstElement target, IAstMethodReference reference)
        {
            this.Target = target;
            this.Reference = reference;

            SetExpressionType(() => AstUnknownType.WithNoName);
        }
 public AstFunctionReferenceExpression(IAstElement target, IAstMethodReference reference)
 {
     this.Target = target;
     this.Function = reference;
     this.ExpressionType = new AstInferredFunctionType(
         () => this.Function.ParameterTypes,
         () => this.Function.ReturnType
     );
 }
Example #3
0
 public AstFunctionReferenceExpression(IAstElement target, IAstMethodReference reference)
 {
     this.Target         = target;
     this.Function       = reference;
     this.ExpressionType = new AstInferredFunctionType(
         () => this.Function.ParameterTypes,
         () => this.Function.ReturnType
         );
 }
Example #4
0
        protected override IEnumerable <IAstElement> VisitOrTransformChildren(AstElementTransform transform)
        {
            if (this.Target != null)
            {
                yield return(this.Target = transform(this.Target));
            }

            yield return(this.Function = (IAstMethodReference)transform(this.Function));
        }
Example #5
0
        public AstMethodGroup(string name, IAstMethodReference[] methods)
        {
            Argument.RequireNotNullAndNotEmpty("name", name);
            Argument.RequireNotNullAndNotEmpty("methods", methods);

            this.Name = name;
            this.Methods = methods;
            this.ReturnType = AstUnknownType.WithNoName;

            var locationsCount = methods.Select(m => m.Location).Distinct().Count();
            this.Location = locationsCount == 1 ? methods[0].Location : MethodLocation.Unknown;
        }
        public AstGenericMethodWithTypeArguments(IAstMethodReference actual, IList<IAstTypeReference> typeArguments, GenericTypeHelper genericHelper)
        {
            Argument.RequireNotNull("actual", actual);
            Argument.RequireNotNull("typeArguments", typeArguments);

            this.Actual = actual;
            this.GenericArgumentTypes = typeArguments.AsReadOnly();

            var genericParameterTypes = actual.GetGenericParameterTypes().ToArray();
            this.ParameterTypes = actual.ParameterTypes.Select(t => ApplyArgumentTypes(genericHelper, t, genericParameterTypes)).ToArray().AsReadOnly();
            this.ReturnType = ApplyArgumentTypes(genericHelper, actual.ReturnType, genericParameterTypes);
        }
Example #7
0
        public AstGenericMethodWithTypeArguments(IAstMethodReference actual, IList <IAstTypeReference> typeArguments, GenericTypeHelper genericHelper)
        {
            Argument.RequireNotNull("actual", actual);
            Argument.RequireNotNull("typeArguments", typeArguments);

            this.Actual = actual;
            this.GenericArgumentTypes = typeArguments.AsReadOnly();

            var genericParameterTypes = actual.GetGenericParameterTypes().ToArray();

            this.ParameterTypes = actual.ParameterTypes.Select(t => ApplyArgumentTypes(genericHelper, t, genericParameterTypes)).ToArray().AsReadOnly();
            this.ReturnType     = ApplyArgumentTypes(genericHelper, actual.ReturnType, genericParameterTypes);
        }
Example #8
0
        public static void CompileTarget(ILProcessor processor, IAstExpression target, IAstMethodReference function, CilCompilationContext context)
        {
            context.Compile(target);
            if (function.Location == MethodLocation.Extension)
                return;

            var targetType = context.ConvertReference(target.ExpressionType);
            if (!targetType.IsValueType)
                return;

            var variable = context.DefineVariable("x", targetType);
            processor.Emit(OpCodes.Stloc, variable);
            processor.Emit(OpCodes.Ldloca_S, variable);
        }
        private IAstMethodReference CoerceToType(IAstMethodReference function, IAstFunctionTypeReference functionType)
        {
            var genericParameterTypes      = function.GetGenericParameterTypes().ToArray();
            var genericArgumentTypes       = new IAstTypeReference[genericParameterTypes.Length];
            var functionTypeParameterTypes = functionType.GetParameterTypes().ToArray();
            var functionParameterTypes     = (function.ParameterTypes as IList <IAstTypeReference>) ?? function.ParameterTypes.ToArray();

            for (var i = 0; i < genericArgumentTypes.Length; i++)
            {
                var parameterIndex = functionParameterTypes.IndexOf(genericParameterTypes[i]);
                genericArgumentTypes[i] = functionTypeParameterTypes[parameterIndex];
            }

            return(new AstGenericMethodWithTypeArguments(function, genericArgumentTypes, this.genericHelper));
        }
Example #10
0
 public BinaryExpression(IAstExpression left, IAstMethodReference @operator, IAstExpression right)
 {
     this.Left     = left;
     this.Operator = @operator;
     this.Right    = right;
 }
Example #11
0
        public IAstMethodReference Resolve(IAstMethodReference method, IAstElement target, IList <IAstExpression> arguments)
        {
            var group = method as AstMethodGroup;

            return(InnerResolve(group != null ? group.Methods : new[] { method }, target, arguments));
        }
Example #12
0
        private int GetDistanceForAll(IAstMethodReference reflected, IEnumerable<IAstTypeReference> parameterTypes, IEnumerable<IAstTypeReference> argumentTypes, Dictionary<IAstTypeReference, ISet<IAstTypeReference>> genericTheories)
        {
            var totalDistance = 0;

            using (var parameterTypeEnumerator = parameterTypes.GetEnumerator())
            using (var argumentTypeEnumerator = argumentTypes.GetEnumerator()) {
                while (parameterTypeEnumerator.MoveNext()) {
                    if (!argumentTypeEnumerator.MoveNext())
                        return -1;

                    var distance = GetDistance(parameterTypeEnumerator.Current, argumentTypeEnumerator.Current, reflected, genericTheories);
                    if (distance == -1)
                        return -1;

                    totalDistance += distance;
                }

                if (argumentTypeEnumerator.MoveNext())
                    return -1;
            }

            return totalDistance;
        }
Example #13
0
        private IAstTypeReference ReconcileGenericTheories(ISet<IAstTypeReference> theories, IAstMethodReference method)
        {
            if (theories.Count > 1)
                throw new NotImplementedException(string.Format("OverloadResolver: Generic theory aggregation is not yet supported. Method: {0}, theories: {1}.", method, string.Join(", ", theories)));

            return theories.First();
        }
Example #14
0
        private Tuple<IAstMethodReference, int> GetOrMakeMatch(IAstMethodReference method, IEnumerable<IAstTypeReference> parameterTypes, IEnumerable<IAstTypeReference> argumentTypes)
        {
            var genericTheories = new Dictionary<IAstTypeReference, ISet<IAstTypeReference>>();
            var distance = GetDistanceForAll(method, parameterTypes, argumentTypes, genericTheories);
            if (distance == -1)
                return null;

            if (method.IsGeneric) {
                var genericParameters = method.GetGenericParameterTypes().ToArray();
                var genericArguments = new IAstTypeReference[genericParameters.Length];
                for (var i = 0; i < genericParameters.Length; i++) {
                    var theories = genericTheories.GetValueOrDefault(genericParameters[i]);
                    if (theories == null)
                        return null;

                    var aggregate = ReconcileGenericTheories(theories, method);
                    if (aggregate == null)
                        return null;

                    genericArguments[i] = aggregate;
                }

                var generic = new AstGenericMethodWithTypeArguments(method, genericArguments, this.genericHelper);
                return Tuple.Create((IAstMethodReference)generic, distance);
            }

            return Tuple.Create(method, distance);
        }
Example #15
0
        public static void CompileTarget(ILProcessor processor, IAstExpression target, IAstMethodReference function, CilCompilationContext context)
        {
            context.Compile(target);
            if (function.Location == MethodLocation.Extension)
            {
                return;
            }

            var targetType = context.ConvertReference(target.ExpressionType);

            if (!targetType.IsValueType)
            {
                return;
            }

            var variable = context.DefineVariable("x", targetType);

            processor.Emit(OpCodes.Stloc, variable);
            processor.Emit(OpCodes.Ldloca_S, variable);
        }
Example #16
0
 public IAstMethodReference Resolve(IAstMethodReference method, IAstElement target, IList<IAstExpression> arguments)
 {
     var group = method as AstMethodGroup;
     return InnerResolve(group != null ? group.Methods : new[] { method }, target, arguments);
 }
Example #17
0
 private IAstTypeReference GetGenericArgumentType(IAstMethodReference methodReference, int index)
 {
     return(((AstGenericMethodWithTypeArguments)methodReference).GenericArgumentTypes[index]);
 }
Example #18
0
 public MethodReference ConvertReference(IAstMethodReference method, bool returnNullIfFailed = false)
 {
     return(this.referenceContext.ConvertReference(method, returnNullIfFailed));
 }
Example #19
0
        private int GetDistance(IAstTypeReference parameterType, IAstTypeReference argumentType, IAstMethodReference method, Dictionary<IAstTypeReference, ISet<IAstTypeReference>> genericTheories)
        {
            if (argumentType.IsImplicit())
                return 0;

            var argumentCompatibleTypes = GetCompatibleTypes(argumentType);
            var distance = argumentCompatibleTypes.GetValueOrDefault(parameterType, -1);
            if (distance != -1)
                return distance;

            if (!method.IsGeneric)
                return -1;

            if (CanMatchPotentialGeneric(parameterType, argumentType, () => argumentCompatibleTypes, genericTheories))
                return 0;

            return -1;
        }
Example #20
0
 public virtual MethodReference ConvertReference(IAstMethodReference method, bool returnNullIfFailed = false)
 {
     Argument.RequireNotNull("method", method);
     return (MethodReference)ConvertReference((IAstReference)method, returnNullIfFailed);
 }
Example #21
0
 public virtual MethodReference ConvertReference(IAstMethodReference method, bool returnNullIfFailed = false)
 {
     Argument.RequireNotNull("method", method);
     return((MethodReference)ConvertReference((IAstReference)method, returnNullIfFailed));
 }
Example #22
0
 public MethodReference ConvertReference(IAstMethodReference method, bool returnNullIfFailed = false)
 {
     return this.referenceContext.ConvertReference(method, returnNullIfFailed);
 }
Example #23
0
 public BinaryExpression(IAstExpression left, IAstMethodReference @operator, IAstExpression right)
 {
     this.Left = left;
     this.Operator = @operator;
     this.Right = right;
 }