int GetByRefParamCount(CallableSignature signature)
		{
			int count = 0;
			foreach (IParameter param in signature.Parameters)
			{
				if (param.IsByRef) ++count;
			}
			return count;
		}
Exemplo n.º 2
0
 public CallableSignature GetSignature()
 {
     if (null == _signature)
     {
         IMethod invoke = GetInvokeMethod();
         if (null == invoke) return null;
         _signature = invoke.CallableType.GetSignature();
     }
     return _signature;
 }
Exemplo n.º 3
0
        override public bool Equals(object other)
        {
            CallableSignature rhs = other as CallableSignature;

            if (null == rhs ||
                _returnType != rhs._returnType ||
                _acceptVarArgs != rhs._acceptVarArgs)
            {
                return(false);
            }
            return(AreSameParameters(_parameters, rhs._parameters));
        }
Exemplo n.º 4
0
 internal AnonymousCallableType(TypeSystemServices services, CallableSignature signature)
 {
     if (null == services)
     {
         throw new ArgumentNullException("services");
     }
     if (null == signature)
     {
         throw new ArgumentNullException("signature");
     }
     _typeSystemServices = services;
     _signature          = signature;
 }
Exemplo n.º 5
0
 internal AnonymousCallableType(TypeSystemServices services, CallableSignature signature)
 {
     if (null == services)
     {
         throw new ArgumentNullException("services");
     }
     if (null == signature)
     {
         throw new ArgumentNullException("signature");
     }
     _typeSystemServices = services;
     _signature = signature;
 }
Exemplo n.º 6
0
 public CallableSignature GetSignature()
 {
     if (null == _signature)
     {
         IMethod invoke = GetInvokeMethod();
         if (null == invoke)
         {
             return(null);
         }
         _signature = invoke.CallableType.GetSignature();
     }
     return(_signature);
 }
Exemplo n.º 7
0
        public CallableSignature GetSignature()
        {
            if (_signature == null)
            {
                CallableSignature definitionSignature = ((ICallableType)_definition).GetSignature();

                IParameter[] parameters = GenericMapping.Map(definitionSignature.Parameters);
                IType returnType = GenericMapping.Map(definitionSignature.ReturnType);

                _signature = new CallableSignature(parameters, returnType);
            }

            return _signature;
        }
Exemplo n.º 8
0
        public CallableSignature GetSignature()
        {
            if (_signature == null)
            {
                CallableSignature definitionSignature = ((ICallableType)_definition).GetSignature();

                IParameter[] parameters = GenericMapping.Map(definitionSignature.Parameters);
                IType        returnType = GenericMapping.Map(definitionSignature.ReturnType);

                _signature = new CallableSignature(parameters, returnType);
            }

            return(_signature);
        }
Exemplo n.º 9
0
        public CallableSignature GetSignature()
        {
            if (_signature == null)
            {
                CallableSignature definitionSignature = _definition.GetSignature();

                IParameter[] parameters = Array.ConvertAll <IParameter, IParameter>(
                    definitionSignature.Parameters,
                    delegate(IParameter p)
                {
                    return(new MappedParameter(_typeSystemServices, (ExternalParameter)p, this));
                });

                _signature = new CallableSignature(parameters, MapType(definitionSignature.ReturnType));
            }

            return(_signature);
        }
Exemplo n.º 10
0
        public bool IsCallableTypeAssignableFrom(ICallableType lhs, IType rhs)
        {
            if (lhs == rhs || Null.Default == rhs)
            {
                return(true);
            }

            ICallableType other = rhs as ICallableType;

            if (null != other)
            {
                CallableSignature lvalue = lhs.GetSignature();
                CallableSignature rvalue = other.GetSignature();
                if (lvalue == rvalue)
                {
                    return(true);
                }

                IParameter[] lparams = lvalue.Parameters;
                IParameter[] rparams = rvalue.Parameters;
                if (lparams.Length >= rparams.Length)
                {
                    for (int i = 0; i < rparams.Length; ++i)
                    {
                        IType lparamType = lparams[i].Type;
                        IType rparamType = rparams[i].Type;
                        if (!AreTypesRelated(lparamType, rparamType))
                        {
                            return(false);
                        }
                    }

                    if (VoidType != lvalue.ReturnType &&
                        VoidType != rvalue.ReturnType)
                    {
                        return(AreTypesRelated(lvalue.ReturnType, rvalue.ReturnType));
                    }

                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 11
0
        public bool IsCallableTypeAssignableFrom(ICallableType lhs, IType rhs)
        {
            if (lhs == rhs)
            {
                return(true);
            }
            if (rhs.IsNull())
            {
                return(true);
            }

            var other = rhs as ICallableType;

            if (null == other)
            {
                return(false);
            }

            CallableSignature lvalue = lhs.GetSignature();
            CallableSignature rvalue = other.GetSignature();

            if (lvalue == rvalue)
            {
                return(true);
            }

            IParameter[] lparams = lvalue.Parameters;
            IParameter[] rparams = rvalue.Parameters;
            if (lparams.Length < rparams.Length)
            {
                return(false);
            }

            for (int i = 0; i < rparams.Length; ++i)
            {
                if (!CanBeReachedFrom(lparams[i].Type, rparams[i].Type))
                {
                    return(false);
                }
            }

            return(CompatibleReturnTypes(lvalue, rvalue));
        }
Exemplo n.º 12
0
        public virtual void VisitCallableType(ICallableType callableType)
        {
            if (_inCallableTypes.Contains(callableType))
            {
                return;
            }

            _inCallableTypes.Push(callableType);
            try
            {
                CallableSignature sig = callableType.GetSignature();
                foreach (IParameter parameter in sig.Parameters)
                {
                    Visit(parameter.Type);
                }
                Visit(sig.ReturnType);
            }
            finally
            {
                _inCallableTypes.Pop();
            }
        }
Exemplo n.º 13
0
        public Method CreateEndInvokeMethod(ICallableType anonymousType)
        {
            CallableSignature signature = anonymousType.GetSignature();
            Method            method    = CodeBuilder.CreateRuntimeMethod("EndInvoke", signature.ReturnType);

            int delta = 1;

            foreach (IParameter p in signature.Parameters)
            {
                if (p.IsByRef)
                {
                    method.Parameters.Add(
                        CodeBuilder.CreateParameterDeclaration(++delta,
                                                               p.Name,
                                                               p.Type,
                                                               true));
                }
            }
            delta = method.Parameters.Count;
            method.Parameters.Add(
                CodeBuilder.CreateParameterDeclaration(delta + 1, "result", TypeSystemServices.Map(typeof(IAsyncResult))));
            return(method);
        }
Exemplo n.º 14
0
        private static int CalculateCallableScore(ICallableType parameterType, ICallableType argType)
        {
            // upcast
            // parameterType == ICallableType, "ThreadStart"
            // argumentType == ICallableType, "Anonymous Closure"
            // RULES:
            // Number of arguments for argumentType && parameterType == same
            // Either: all arguments "IsAssignableFrom"
            //			OR
            //			all arguments == exactly (best case scenario)
            // ExactMatch -- (best case)
            // UpCast -- "not exact match, but very close" (this is OK)
            // ImplicitConversion -- "assignable, but wrong number of parameters / whatever" (boo does the normal thing)

            CallableSignature siggyType = parameterType.GetSignature();
            CallableSignature siggyArg  = argType.GetSignature();

            // Ensuring that these callables have same number of arguments.
            // def foo(a, b,c) == { a, b, c| print foobar }
            if (siggyType.Parameters.Length != siggyArg.Parameters.Length)
            {
                return(UpCastScore);
            }
            for (int i = 0; i < siggyType.Parameters.Length; i++)
            {
                if (siggyType.Parameters[i].Type != siggyArg.Parameters[i].Type)
                {
                    // In the case that these parameters simply don't match
                    if (!siggyType.Parameters[i].Type.IsAssignableFrom(siggyArg.Parameters[i].Type))
                    {
                        return(ImplicitConversionScore);
                    }
                    return(UpCastScore);
                }
            }
            return(ExactMatchScore);
        }
Exemplo n.º 15
0
		public ICallableType GetCallableType(IMethodBase method)
		{
			var signature = new CallableSignature(method);
			return GetCallableType(signature);
		}
Exemplo n.º 16
0
 private IType[] GetParameterTypes(CallableSignature signature)
 {
     return Array.ConvertAll<IParameter, IType>(
         signature.Parameters,
         delegate(IParameter p) { return p.Type; });
 }
Exemplo n.º 17
0
        /// <summary>
        /// Maps a type involving generic parameters to the corresponding type after substituting concrete
        /// arguments for generic parameters.
        /// </summary>
        /// <remarks>
        /// If the source type is a generic parameter, it is mapped to the corresponding argument.
        /// If the source type is an open generic type using any of the specified generic parameters, it
        /// is mapped to a closed constructed type based on the specified arguments.
        /// TODO: complete this
        /// </remarks>
        public IType MapType(IType sourceType)
        {
            if (sourceType == null)
            {
                return(null);
            }

            // If sourceType is a reference type, map its element type
            if (sourceType.IsByRef)
            {
                return(MapType(sourceType.GetElementType()));
            }

            // Map generic parameter to corresponding argument
            IGenericParameter gp = sourceType as IGenericParameter;

            if (null != gp && _map.ContainsKey(gp))
            {
                return(_map[gp]);
            }

            // Map open constructed type using generic parameters to closed constructed type
            // using corresponding arguments
            if (null != sourceType.GenericTypeInfo)
            {
                IType[] mappedArguments = Array.ConvertAll <IType, IType>(
                    sourceType.GenericTypeInfo.GenericArguments,
                    MapType);

                IType mapped = sourceType.GenericTypeInfo.
                               GenericDefinition.GenericTypeDefinitionInfo.
                               MakeGenericType(mappedArguments);

                return(mapped);
            }

            // Map array types
            IArrayType array = (sourceType as IArrayType);

            if (array != null)
            {
                IType elementType       = array.GetElementType();
                IType mappedElementType = MapType(elementType);
                if (mappedElementType != elementType)
                {
                    return(_tss.GetArrayType(mappedElementType, array.GetArrayRank()));
                }
            }

            // Map callable types
            ICallableType callable = sourceType as ICallableType;

            if (callable != null)
            {
                CallableSignature signature = callable.GetSignature();

                IType        returnType = MapType(signature.ReturnType);
                IParameter[] parameters = Array.ConvertAll <IParameter, IParameter>(
                    signature.Parameters,
                    delegate(IParameter p) { return(new MappedParameter(_tss, (ExternalParameter)p, this)); });

                CallableSignature mappedSignature = new CallableSignature(
                    parameters, returnType, signature.AcceptVarArgs);

                return(_tss.GetCallableType(mappedSignature));
            }

            // If source type doesn't require mapping, return it as is
            return(sourceType);
        }
Exemplo n.º 18
0
 public virtual IType GetConcreteCallableType(Node sourceNode, CallableSignature signature)
 {
     return(_anonymousCallablesManager.GetConcreteCallableType(sourceNode, signature));
 }
Exemplo n.º 19
0
 public static bool CheckOverrideSignature(IParameter[] implParameters, IParameter[] baseParameters)
 {
     return(CallableSignature.AreSameParameters(implParameters, baseParameters));
 }
Exemplo n.º 20
0
		private bool CompatibleReturnTypes(CallableSignature lvalue, CallableSignature rvalue)
		{
			if (VoidType != lvalue.ReturnType && VoidType != rvalue.ReturnType)
				return CanBeReachedFrom(lvalue.ReturnType, rvalue.ReturnType);
			return true;
		}
Exemplo n.º 21
0
        public ICallableType GetCallableType(IMethodBase method)
        {
            var signature = new CallableSignature(method);

            return(GetCallableType(signature));
        }
Exemplo n.º 22
0
        public AnonymousCallableType GetCallableType(IMethod method)
        {
            CallableSignature signature = new CallableSignature(method);

            return(GetCallableType(signature));
        }
Exemplo n.º 23
0
        public AnonymousCallableType GetCallableType(CallableSignature signature)
        {
            AnonymousCallableType type = GetCachedCallableType(signature);

            if (type == null)
            {
                type = new AnonymousCallableType(TypeSystemServices, signature);
                _cache.Add(signature, type);
            }
            return type;
        }
Exemplo n.º 24
0
        public IType GetConcreteCallableType(Node sourceNode, CallableSignature signature)
        {
            AnonymousCallableType type = GetCallableType(signature);

            return(GetConcreteCallableType(sourceNode, type));
        }
Exemplo n.º 25
0
        /// <summary>
        /// Maps a type involving generic parameters to the corresponding type after substituting concrete
        /// arguments for generic parameters.
        /// </summary>
        /// <remarks>
        /// If the source type is a generic parameter, it is mapped to the corresponding argument.
        /// If the source type is an open generic type using any of the specified generic parameters, it
        /// is mapped to a closed constructed type based on the specified arguments.
        /// </remarks>
        protected virtual IType MapType(IType sourceType)
        {
            if (sourceType == null)
            {
                return(null);
            }

            // Strip reference types
            if (sourceType.IsByRef)
            {
                return(MapType(sourceType.GetElementType()));
            }

            // Map generic parameter to corresponding argument
            IGenericParameter gp = sourceType as IGenericParameter;

            if (null != gp && _map.ContainsKey(gp))
            {
                return(_map[gp]);
            }

            // Map open constructed type using generic parameters to closed constructed type
            // using corresponding arguments
            if (null != sourceType.ConstructedInfo)
            {
                IType[] mappedArguments = Array.ConvertAll <IType, IType>(
                    sourceType.ConstructedInfo.GenericArguments,
                    MapType);

                IType mapped = sourceType.ConstructedInfo.
                               GenericDefinition.GenericInfo.
                               ConstructType(mappedArguments);

                return(mapped);
            }

            // TODO: Map nested types
            // GenericType[of T].NestedType => GenericType[of int].NestedType

            // Map array types
            IArrayType array = (sourceType as IArrayType);

            if (array != null)
            {
                IType elementType       = array.GetElementType();
                IType mappedElementType = MapType(elementType);
                if (mappedElementType != elementType)
                {
                    return(_tss.GetArrayType(mappedElementType, array.GetArrayRank()));
                }
            }

            // Map callable types
            ICallableType callable = sourceType as ICallableType;

            if (callable != null)
            {
                CallableSignature signature = callable.GetSignature();

                IType        returnType = MapType(signature.ReturnType);
                IParameter[] parameters = Map(signature.Parameters);

                CallableSignature mappedSignature = new CallableSignature(
                    parameters, returnType, signature.AcceptVarArgs);

                return(_tss.GetCallableType(mappedSignature));
            }

            // If source type doesn't require mapping, return it as is
            return(sourceType);
        }
Exemplo n.º 26
0
 public AnonymousCallableType GetCallableType(IMethod method)
 {
     CallableSignature signature = new CallableSignature(method);
     return GetCallableType(signature);
 }
Exemplo n.º 27
0
        private bool CompatibleReturnTypes(CallableSignature lvalue, CallableSignature rvalue)
        {
            if (VoidType != lvalue.ReturnType && VoidType != rvalue.ReturnType)
            {
                return AreTypesRelated(lvalue.ReturnType, rvalue.ReturnType);
            }

            return true;
        }
Exemplo n.º 28
0
        private void InitializeDependencies(IGenericParameter[] genericParameters, CallableSignature signature)
        {
            IType[] parameterTypes = GetParameterTypes(signature);

            foreach (IType parameterType in parameterTypes)
            {
                ICallableType callableParameterType = parameterType as ICallableType;
                if (callableParameterType == null) continue;
                CalculateDependencies(callableParameterType.GetSignature());
            }
        }
Exemplo n.º 29
0
		public ICallableType GetCallableType(CallableSignature signature)
		{
			return _anonymousCallablesManager.GetCallableType(signature);
		}
        void ImplementRegularICallableCall(
            Method call,
            InternalCallableType type,
            ClassDefinition node,
            CallableSignature signature)
        {
            MethodInvocationExpression mie = CreateInvokeInvocation(type);
            IParameter[] parameters = signature.Parameters;
            int fixedParametersLength = signature.AcceptVarArgs ? parameters.Length - 1 : parameters.Length;
            for (int i=0; i<fixedParametersLength; ++i)
            {
                SlicingExpression slice = CodeBuilder.CreateSlicing(
                            CodeBuilder.CreateReference(call.Parameters[0]),
                            i);
                mie.Arguments.Add(slice);
            }

            if (signature.AcceptVarArgs)
            {
                if (parameters.Length == 1)
                {
                    mie.Arguments.Add(CodeBuilder.CreateReference(call.Parameters[0]));
                }
                else
                {
                    mie.Arguments.Add(
                        CodeBuilder.CreateMethodInvocation(
                            RuntimeServices_GetRange1,
                            CodeBuilder.CreateReference(call.Parameters[0]),
                            CodeBuilder.CreateIntegerLiteral(fixedParametersLength)));
                }
            }

            if (TypeSystemServices.VoidType == signature.ReturnType)
            {
                call.Body.Add(mie);
            }
            else
            {
                call.Body.Add(new ReturnStatement(mie));
            }
        }
Exemplo n.º 31
0
		public virtual IType GetConcreteCallableType(Node sourceNode, CallableSignature signature)
		{
			return _anonymousCallablesManager.GetConcreteCallableType(sourceNode, signature);
		}
        void ImplementByRefICallableCall(
            Method call,
            InternalCallableType type,
            ClassDefinition node,
            CallableSignature signature,
            int byRefCount)
        {
            MethodInvocationExpression mie = CreateInvokeInvocation(type);
            IParameter[] parameters = signature.Parameters;
            ReferenceExpression args = CodeBuilder.CreateReference(call.Parameters[0]);
            InternalLocal[] temporaries = new InternalLocal[byRefCount];

            int byRefIndex = 0;
            for (int i=0; i<parameters.Length; ++i)
            {
                SlicingExpression slice = CodeBuilder.CreateSlicing(args.CloneNode(), i);

                IParameter parameter = parameters[i];
                if (parameter.IsByRef)
                {
                    IType tempType = parameter.Type;
                    if (tempType.IsByRef)
                    {
                        tempType = tempType.ElementType;
                    }
                    temporaries[byRefIndex] = CodeBuilder.DeclareLocal(call,
                                "__temp_" + parameter.Name,
                                tempType);

                    call.Body.Add(
                        CodeBuilder.CreateAssignment(
                        CodeBuilder.CreateReference(temporaries[byRefIndex]),
                            CodeBuilder.CreateCast(
                                tempType,
                                slice)));

                    mie.Arguments.Add(
                        CodeBuilder.CreateReference(
                            temporaries[byRefIndex]));

                    ++byRefIndex;
                }
                else
                {
                    mie.Arguments.Add(slice);
                }
            }

            if (TypeSystemServices.VoidType == signature.ReturnType)
            {
                call.Body.Add(mie);
                PropagateByRefParameterChanges(call, parameters, temporaries);
            }
            else
            {
                InternalLocal invokeReturnValue = CodeBuilder.DeclareLocal(call,
                            "__returnValue", signature.ReturnType);
                call.Body.Add(
                    CodeBuilder.CreateAssignment(
                        CodeBuilder.CreateReference(invokeReturnValue),
                        mie));
                PropagateByRefParameterChanges(call, parameters, temporaries);
                call.Body.Add(
                    new ReturnStatement(
                        CodeBuilder.CreateReference(invokeReturnValue)));
            }
        }
Exemplo n.º 33
0
 private AnonymousCallableType GetCachedCallableType(CallableSignature signature)
 {
     AnonymousCallableType result = null;
     _cache.TryGetValue(signature, out result);
     return result;
 }
Exemplo n.º 34
0
        /// <summary>
        /// Maps a type involving generic parameters to the corresponding type after substituting concrete
        /// arguments for generic parameters.
        /// </summary>
        /// <remarks>
        /// If the source type is a generic parameter, it is mapped to the corresponding argument.
        /// If the source type is an open generic type using any of the specified generic parameters, it 
        /// is mapped to a closed constructed type based on the specified arguments.
        /// </remarks>
        protected virtual IType MapType(IType sourceType)
        {
            if (sourceType == null)
            {
                return null;
            }

            // Strip reference types
            if (sourceType.IsByRef)
            {
                return MapType(sourceType.GetElementType());
            }

            // Map generic parameter to corresponding argument
            IGenericParameter gp = sourceType as IGenericParameter;
            if (null != gp && _map.ContainsKey(gp))
            {
                return _map[gp];
            }

            // Map open constructed type using generic parameters to closed constructed type
            // using corresponding arguments
            if (null != sourceType.ConstructedInfo)
            {
                IType[] mappedArguments = Array.ConvertAll<IType, IType>(
                    sourceType.ConstructedInfo.GenericArguments,
                    MapType);

                IType mapped = sourceType.ConstructedInfo.
                    GenericDefinition.GenericInfo.
                    ConstructType(mappedArguments);

                return mapped;
            }

            // TODO: Map nested types
            // GenericType[of T].NestedType => GenericType[of int].NestedType

            // Map array types
            IArrayType array = (sourceType as IArrayType);
            if (array != null)
            {
                IType elementType = array.GetElementType();
                IType mappedElementType = MapType(elementType);
                if (mappedElementType != elementType)
                {
                    return _tss.GetArrayType(mappedElementType, array.GetArrayRank());
                }
            }

            // Map callable types
            ICallableType callable = sourceType as ICallableType;
            if (callable != null && EntityNeedsMapping(callable))
            {
                CallableSignature signature = callable.GetSignature();

                IType returnType = MapType(signature.ReturnType);
                IParameter[] parameters = Map(signature.Parameters);

                CallableSignature mappedSignature = new CallableSignature(
                    parameters, returnType, signature.AcceptVarArgs);

                return _tss.GetCallableType(mappedSignature);
            }

            // If source type doesn't require mapping, return it as is
            return sourceType;
        }
Exemplo n.º 35
0
 public ICallableType GetCallableType(CallableSignature signature)
 {
     return(_anonymousCallablesManager.GetCallableType(signature));
 }
Exemplo n.º 36
0
        Method CreateInvokeMethod(AnonymousCallableType anonymousType)
        {
            CallableSignature signature = anonymousType.GetSignature();

            return(CodeBuilder.CreateRuntimeMethod("Invoke", signature.ReturnType, signature.Parameters, signature.AcceptVarArgs));
        }
Exemplo n.º 37
0
 public IType GetConcreteCallableType(Node sourceNode, CallableSignature signature)
 {
     AnonymousCallableType type = GetCallableType(signature);
     return GetConcreteCallableType(sourceNode, type);
 }
Exemplo n.º 38
0
 public IType GetConcreteCallableType(Node sourceNode, CallableSignature signature)
 {
     var type = GetCallableType(signature);
     var anonymous = type as AnonymousCallableType;
     return anonymous != null ? GetConcreteCallableType(sourceNode, anonymous) : type;
 }
Exemplo n.º 39
0
 public static CompilerWarning InvalidEventUnsubscribe(Node node, string eventName, CallableSignature expected)
 {
     return new CompilerWarning("BCW0005", AstUtil.SafeLexicalInfo(node), eventName, expected);
 }
Exemplo n.º 40
0
 private void CalculateDependencies(CallableSignature signature)
 {
     foreach (IType inputType in GetParameterTypes(signature))
     {
         CalculateDependencies(inputType, signature.ReturnType);
     }
 }