Example #1
0
 public MethodCodeGen(CodeGenerator cg, MethodBindingInfo bindingInfo)
     : base(cg)
 {
     this.bindingInfo = bindingInfo;
     WriteAllVariants(this.bindingInfo);
     // WriteTSAllVariants(this.bindingInfo);
 }
Example #2
0
        public void AddMethod(MethodInfo methodInfo, bool isIndexer, string renameRegName)
        {
            if (this.transform != null)
            {
                if (this.transform.IsBlocked(methodInfo))
                {
                    bindingManager.Info("skip blocked method: {0}", methodInfo.Name);
                    return;
                }
            }

            var isExtension = methodInfo.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute));
            var isStatic    = methodInfo.IsStatic && !isExtension;

            if (IsSupportedOperators(methodInfo))
            {
                var methodName    = TypeBindingInfo.GetNamingAttribute(methodInfo);
                var parameters    = methodInfo.GetParameters();
                var declaringType = methodInfo.DeclaringType;
                switch (methodName)
                {
                case "op_Addition":
                    if (parameters.Length == 2)
                    {
                        if (parameters[0].ParameterType == declaringType && parameters[1].ParameterType == declaringType)
                        {
                            var bindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, methodName, "+", "+", 2);
                            operators.Add(bindingInfo);
                        }
                    }
                    break;

                case "op_Subtraction":
                    if (parameters.Length == 2)
                    {
                        if (parameters[0].ParameterType == declaringType && parameters[1].ParameterType == declaringType)
                        {
                            var bindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, methodName, "-", "-", 2);
                            operators.Add(bindingInfo);
                        }
                    }
                    break;

                case "op_Equality":
                    if (parameters.Length == 2)
                    {
                        if (parameters[0].ParameterType == declaringType && parameters[1].ParameterType == declaringType)
                        {
                            var bindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, methodName, "==", "==", 2);
                            operators.Add(bindingInfo);
                        }
                    }
                    break;

                case "op_Multiply":
                    if (parameters.Length == 2)
                    {
                        var op0 = bindingManager.GetExportedType(parameters[0].ParameterType);
                        var op1 = bindingManager.GetExportedType(parameters[1].ParameterType);
                        if (op0 == null || op1 == null)
                        {
                            return;
                        }
                        var bindingName = methodName + "_" + op0.name + "_" + op1.name;
                        var bindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, bindingName, "*", "*", 2);
                        operators.Add(bindingInfo);
                    }
                    break;

                case "op_Division":
                    if (parameters.Length == 2)
                    {
                        var op0 = bindingManager.GetExportedType(parameters[0].ParameterType);
                        var op1 = bindingManager.GetExportedType(parameters[1].ParameterType);
                        if (op0 == null || op1 == null)
                        {
                            return;
                        }
                        var bindingName = methodName + "_" + op0.name + "_" + op1.name;
                        var bindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, bindingName, "/", "/", 2);
                        operators.Add(bindingInfo);
                    }
                    break;

                case "op_UnaryNegation":
                {
                    var bindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, methodName, "neg", "-", 1);
                    operators.Add(bindingInfo);
                }
                break;

                default:
                    bindingManager.Info("skip unsupported operator method: {0}", methodInfo.Name);
                    return;
                }
            }
            else
            {
                var group = isStatic ? staticMethods : methods;
                MethodBindingInfo overrides;
                var methodName = TypeBindingInfo.GetNamingAttribute(methodInfo);
                if (!group.TryGetValue(methodName, out overrides))
                {
                    overrides = new MethodBindingInfo(isIndexer, isStatic, methodName, renameRegName ?? methodName);
                    group.Add(methodName, overrides);
                }
                overrides.Add(methodInfo, isExtension);
            }

            CollectDelegate(methodInfo);
            bindingManager.Info("[AddMethod] {0}.{1}", type, methodInfo);
        }
Example #3
0
        public void AddMethod(MethodInfo methodInfo, bool isIndexer, string renameRegName)
        {
            if (this.transform != null)
            {
                if (this.transform.IsBlocked(methodInfo))
                {
                    bindingManager.Info("skip blocked method: {0}", methodInfo.Name);
                    return;
                }
            }

            var isExtension = BindingManager.IsExtensionMethod(methodInfo);
            var isStatic    = methodInfo.IsStatic && !isExtension;
            var methodName  = TypeBindingInfo.GetNamingAttribute(methodInfo);

            if (IsSupportedOperators(methodInfo))
            {
                var parameters    = methodInfo.GetParameters();
                var declaringType = methodInfo.DeclaringType;
                OperatorBindingInfo operatorBindingInfo = null;
                switch (methodName)
                {
                case "op_LessThan":
                    if (parameters.Length == 2)
                    {
                        if (parameters[0].ParameterType == declaringType && parameters[1].ParameterType == declaringType)
                        {
                            operatorBindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, methodName, "<", "<", 2);
                        }
                    }
                    break;

                case "op_Addition":
                    if (parameters.Length == 2)
                    {
                        if (parameters[0].ParameterType == declaringType && parameters[1].ParameterType == declaringType)
                        {
                            operatorBindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, methodName, "+", "+", 2);
                        }
                    }
                    break;

                case "op_Subtraction":
                    if (parameters.Length == 2)
                    {
                        if (parameters[0].ParameterType == declaringType && parameters[1].ParameterType == declaringType)
                        {
                            operatorBindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, methodName, "-", "-", 2);
                        }
                    }
                    break;

                case "op_Equality":
                    if (parameters.Length == 2)
                    {
                        if (parameters[0].ParameterType == declaringType && parameters[1].ParameterType == declaringType)
                        {
                            operatorBindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, methodName, "==", "==", 2);
                        }
                    }
                    break;

                case "op_Multiply":
                    if (parameters.Length == 2)
                    {
                        var op0 = bindingManager.GetExportedType(parameters[0].ParameterType);
                        var op1 = bindingManager.GetExportedType(parameters[1].ParameterType);
                        if (op0 != null && op1 != null)
                        {
                            var bindingName = methodName + "_" + op0.name + "_" + op1.name;
                            operatorBindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, bindingName, "*", "*", 2);
                        }
                    }
                    break;

                case "op_Division":
                    if (parameters.Length == 2)
                    {
                        var op0 = bindingManager.GetExportedType(parameters[0].ParameterType);
                        var op1 = bindingManager.GetExportedType(parameters[1].ParameterType);
                        if (op0 != null && op1 != null)
                        {
                            var bindingName = methodName + "_" + op0.name + "_" + op1.name;
                            operatorBindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, bindingName, "/", "/", 2);
                        }
                    }
                    break;

                case "op_UnaryNegation":
                {
                    operatorBindingInfo = new OperatorBindingInfo(methodInfo, isExtension, isStatic, methodName, "neg", "-", 1);
                }
                break;
                }

                if (operatorBindingInfo != null)
                {
                    operators.Add(operatorBindingInfo);
                    CollectDelegate(methodInfo);
                    bindingManager.Info("[AddOperator] {0}.{1}", type, methodInfo);
                    return;
                }

                // fallback to normal method binding
            }

            var group = isStatic ? staticMethods : methods;
            MethodBindingInfo methodBindingInfo;

            if (!group.TryGetValue(methodName, out methodBindingInfo))
            {
                methodBindingInfo = new MethodBindingInfo(isIndexer, isStatic, methodName, renameRegName ?? methodName);
                group.Add(methodName, methodBindingInfo);
            }
            if (!methodBindingInfo.Add(methodInfo, isExtension))
            {
                bindingManager.Info("fail to add method: {0}", methodInfo.Name);
                return;
            }

            CollectDelegate(methodInfo);
            bindingManager.Info("[AddMethod] {0}.{1}", type, methodInfo);
        }