Ejemplo n.º 1
0
 public OperatorCodeGen(CodeGenerator cg, OperatorBindingInfo bindingInfo)
     : base(cg)
 {
     this.bindingInfo = bindingInfo;
     WriteCSAllVariants(this.bindingInfo);
     // WriteTSAllVariants(this.bindingInfo);
 }
Ejemplo n.º 2
0
 public TSOperatorCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo, OperatorBindingInfo bindingInfo)
     : base(cg)
 {
     this.bindingInfo = bindingInfo;
     WriteTSAllVariants(typeBindingInfo, this.bindingInfo);
 }
Ejemplo n.º 3
0
        public void AddMethod(MethodInfo methodInfo, bool asExtensionAnyway)
        {
            if (this.transform.IsBlocked(methodInfo))
            {
                bindingManager.Info("skip blocked method: {0}", methodInfo.Name);
                return;
            }

            // if (type.IsConstructedGenericType)
            // {
            //     var gTransform = bindingManager.GetTypeTransform(type.GetGenericTypeDefinition());
            //     if (gTransform != null && gTransform.IsBlocked(methodInfo.??))
            //     {
            //         bindingManager.Info("skip blocked method in generic definition: {0}", methodInfo.Name);
            //         return;
            //     }
            // }

            var isExtension = asExtensionAnyway || BindingManager.IsExtensionMethod(methodInfo);
            var isStatic    = methodInfo.IsStatic && !isExtension;

            if (isStatic && type.IsGenericTypeDefinition)
            {
                bindingManager.Info("skip static method in generic type definition: {0}", methodInfo.Name);
                return;
            }

            var methodCSName = methodInfo.Name;
            var methodJSName = this.bindingManager.GetNamingAttribute(this.transform, methodInfo);

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

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

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

                case "op_Equality":
                    if (parameters.Length == 2)
                    {
                        if (parameters[0].ParameterType == declaringType && parameters[1].ParameterType == declaringType)
                        {
                            operatorBindingInfo = new OperatorBindingInfo(bindingManager, methodInfo, isExtension, isStatic, methodCSName, "==", "==", 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 = methodCSName + "_" + op0.csBindingName + "_" + op1.csBindingName;
                            operatorBindingInfo = new OperatorBindingInfo(bindingManager, 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 = methodCSName + "_" + op0.csBindingName + "_" + op1.csBindingName;
                            operatorBindingInfo = new OperatorBindingInfo(bindingManager, methodInfo, isExtension, isStatic, bindingName, "/", "/", 2);
                        }
                    }
                    break;

                case "op_UnaryNegation":
                {
                    operatorBindingInfo = new OperatorBindingInfo(bindingManager, methodInfo, isExtension, isStatic, methodCSName, "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(methodCSName, out methodBindingInfo))
            {
                methodBindingInfo = new MethodBindingInfo(bindingManager, isStatic, methodCSName, methodJSName);
                group.Add(methodCSName, 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);
        }