Esempio n. 1
0
        internal void OutputInvocation(StringBuilder codeBuilder, CsDslTranslater cs2dsl, ExpressionSyntax exp, bool isMemberAccess, SemanticModel model, SyntaxNode node)
        {
            IMethodSymbol sym    = MethodSymbol;
            string        mname  = cs2dsl.NameMangling(IsExtensionMethod && null != sym.ReducedFrom ? sym.ReducedFrom : sym);
            string        prestr = string.Empty;

            if (isMemberAccess)
            {
                string fnOfIntf = "null";
                var    expOper  = model.GetOperation(exp);
                bool   isExplicitInterfaceInvoke = cs2dsl.CheckExplicitInterfaceAccess(sym, ref fnOfIntf);
                bool   expIsBasicType            = false;
                if (!sym.IsStatic && null != expOper && SymbolTable.IsBasicType(expOper.Type))
                {
                    expIsBasicType = true;
                }
                if (sym.MethodKind == MethodKind.DelegateInvoke)
                {
                    var memberAccess = node as MemberAccessExpressionSyntax;
                    if (null != memberAccess)
                    {
                        codeBuilder.Append("callinstance(");
                        cs2dsl.OutputExpressionSyntax(exp);
                        codeBuilder.AppendFormat(", \"{0}\"", memberAccess.Name);
                        prestr = ", ";
                    }
                    else
                    {
                        //error;
                    }
                }
                else if (isExplicitInterfaceInvoke)
                {
                    codeBuilder.Append("invokewithinterface(");
                    cs2dsl.OutputExpressionSyntax(exp);
                    codeBuilder.Append(", ");
                    codeBuilder.AppendFormat("{0}, \"{1}\"", fnOfIntf, mname);
                    prestr = ", ";
                }
                else if (IsExtensionMethod)
                {
                    codeBuilder.Append("callstatic(");
                    codeBuilder.AppendFormat("{0}, \"{1}\", ", ClassKey, mname);
                }
                else if (IsBasicValueMethod || expIsBasicType)
                {
                    string ckey = CalcInvokeTarget(IsEnumClass, ClassKey, cs2dsl, exp, model);
                    codeBuilder.Append("invokeforbasicvalue(");
                    cs2dsl.OutputExpressionSyntax(exp);
                    codeBuilder.Append(", ");
                    codeBuilder.AppendFormat("{0}, {1}, \"{2}\"", IsEnumClass ? "true" : "false", ckey, mname);
                    prestr = ", ";
                }
                else if (IsArrayStaticMethod)
                {
                    codeBuilder.Append("invokearraystaticmethod(");
                    if (null == FirstRefArray)
                    {
                        codeBuilder.Append("null, ");
                    }
                    else
                    {
                        cs2dsl.OutputExpressionSyntax(FirstRefArray);
                        codeBuilder.Append(", ");
                    }
                    if (null == SecondRefArray)
                    {
                        codeBuilder.Append("null, ");
                    }
                    else
                    {
                        cs2dsl.OutputExpressionSyntax(SecondRefArray);
                        codeBuilder.Append(", ");
                    }
                    codeBuilder.AppendFormat("\"{0}\"", mname);
                    prestr = ", ";
                }
                else
                {
                    if (sym.IsStatic)
                    {
                        codeBuilder.Append("callstatic(");
                        codeBuilder.Append(ClassKey);
                    }
                    else
                    {
                        codeBuilder.Append("callinstance(");
                        cs2dsl.OutputExpressionSyntax(exp);
                    }
                    codeBuilder.AppendFormat(", \"{0}\"", mname);
                    prestr = ", ";
                }
            }
            else
            {
                if (sym.MethodKind == MethodKind.DelegateInvoke)
                {
                    cs2dsl.OutputExpressionSyntax(exp);
                    codeBuilder.Append("(");
                }
                else if (sym.IsStatic)
                {
                    codeBuilder.Append("callstatic(");
                    codeBuilder.Append(ClassKey);
                    codeBuilder.AppendFormat(", \"{0}\"", mname);
                    prestr = ", ";
                }
                else
                {
                    codeBuilder.Append("callinstance(");
                    codeBuilder.Append("this");
                    codeBuilder.AppendFormat(", \"{0}\"", mname);
                    prestr = ", ";
                }
            }
            if (!string.IsNullOrEmpty(ExternOverloadedMethodSignature) || Args.Count + DefaultValueArgs.Count + GenericTypeArgs.Count > 0)
            {
                codeBuilder.Append(prestr);
            }
            bool useTypeNameString = false;

            if (IsComponentGetOrAdd && SymbolTable.DslComponentByString)
            {
                var tArgs = sym.TypeArguments;
                if (tArgs.Length > 0 && SymbolTable.Instance.IsCs2DslSymbol(tArgs[0]))
                {
                    useTypeNameString = true;
                }
            }
            if (IsExtensionMethod)
            {
                var args = new List <ExpressionSyntax>();
                args.Add(exp);
                args.AddRange(Args);
                cs2dsl.OutputArgumentList(args, DefaultValueArgs, GenericTypeArgs, ExternOverloadedMethodSignature, PostPositionGenericTypeArgs, ArrayToParams, useTypeNameString, node, ArgConversions.ToArray());
            }
            else
            {
                cs2dsl.OutputArgumentList(Args, DefaultValueArgs, GenericTypeArgs, ExternOverloadedMethodSignature, PostPositionGenericTypeArgs, ArrayToParams, useTypeNameString, node, ArgConversions.ToArray());
            }
            codeBuilder.Append(")");
        }
Esempio n. 2
0
        internal void OutputInvocation(StringBuilder codeBuilder, CsDslTranslater cs2dsl, ExpressionSyntax exp, bool isMemberAccess, SemanticModel model, SyntaxNode node)
        {
            IMethodSymbol sym                = MethodSymbol;
            string        mname              = GetMethodName();
            string        prestr             = string.Empty;
            bool          externReturnStruct = IsExternMethod && !sym.ReturnsVoid && sym.ReturnType.IsValueType && !SymbolTable.IsBasicType(sym.ReturnType);

            if (externReturnStruct)
            {
                cs2dsl.MarkNeedFuncInfo();
            }
            if (isMemberAccess)
            {
                string fnOfIntf = string.Empty;
                var    expOper  = model.GetOperationEx(exp);
                bool   isExplicitInterfaceInvoke = cs2dsl.CheckExplicitInterfaceAccess(sym, ref fnOfIntf);
                bool   expIsBasicType            = false;
                if (!sym.IsStatic && null != expOper && SymbolTable.IsBasicType(expOper.Type))
                {
                    expIsBasicType = true;
                }
                if (sym.MethodKind == MethodKind.DelegateInvoke)
                {
                    var memberAccess = node as MemberAccessExpressionSyntax;
                    if (null != memberAccess)
                    {
                        var symInfo = model.GetSymbolInfoEx(node);
                        var masym   = symInfo.Symbol as ISymbol;
                        if (null != masym)
                        {
                            bool   isCs2Lua = SymbolTable.Instance.IsCs2DslSymbol(masym);
                            string kind     = SymbolTable.Instance.GetSymbolKind(masym);
                            string fn       = ClassInfo.GetFullName(masym.ContainingType);
                            string dt       = ClassInfo.GetFullName(sym);
                            if (externReturnStruct)
                            {
                                codeBuilder.Append("callexterndelegationreturnstruct(getinstance(SymbolKind.");
                            }
                            else if (isCs2Lua)
                            {
                                codeBuilder.AppendFormat("call{0}delegation(getinstance(SymbolKind.", IsExternMethod ? "extern" : string.Empty);
                            }
                            else
                            {
                                codeBuilder.AppendFormat("call{0}delegation(getexterninstance(SymbolKind.", IsExternMethod ? "extern" : string.Empty);
                            }
                            codeBuilder.Append(kind);
                            codeBuilder.Append(", ");
                            cs2dsl.OutputExpressionSyntax(exp);
                            codeBuilder.AppendFormat(", {0}, \"{1}\"), \"{2}\"", fn, memberAccess.Name, dt);
                            prestr = ", ";
                        }
                        else
                        {
                            //error;
                        }
                    }
                    else
                    {
                        //error;
                    }
                }
                else if (isExplicitInterfaceInvoke)
                {
                    //这里不区分是否外部符号了,委托到动态语言的脚本库实现,可根据对象运行时信息判断
                    if (externReturnStruct)
                    {
                        codeBuilder.Append("callexterninstancereturnstruct(");
                    }
                    else
                    {
                        codeBuilder.Append("callinstance(");
                    }
                    cs2dsl.OutputExpressionSyntax(exp);
                    codeBuilder.Append(", ");
                    codeBuilder.Append(ClassKey);
                    codeBuilder.Append(", ");
                    codeBuilder.AppendFormat("\"{0}\"", fnOfIntf);
                    prestr = ", ";
                }
                else if (IsExtensionMethod)
                {
                    if (externReturnStruct)
                    {
                        codeBuilder.Append("callexternextensionreturnstruct(");
                    }
                    else if (IsExternMethod)
                    {
                        codeBuilder.Append("callexternextension(");
                    }
                    else
                    {
                        codeBuilder.Append("callextension(");
                    }
                    codeBuilder.AppendFormat("{0}, \"{1}\", ", ClassKey, mname);
                    cs2dsl.OutputExpressionSyntax(exp);
                    prestr = ", ";
                }
                else if (IsBasicValueMethod || expIsBasicType)
                {
                    //这里不区分是否外部符号了,委托到动态语言的脚本库实现,可根据对象运行时信息判断
                    string ckey = CalcInvokeTarget(IsEnumClass, ClassKey, cs2dsl, expOper);
                    codeBuilder.Append("invokeforbasicvalue(");
                    cs2dsl.OutputExpressionSyntax(exp);
                    codeBuilder.Append(", ");
                    codeBuilder.AppendFormat("{0}, {1}, \"{2}\"", IsEnumClass ? "true" : "false", ckey, mname);
                    prestr = ", ";
                }
                else if (IsArrayStaticMethod)
                {
                    //这里不区分是否外部符号了,委托到动态语言的脚本库实现,可根据对象运行时信息判断
                    codeBuilder.Append("invokearraystaticmethod(");
                    if (null == FirstRefArray)
                    {
                        codeBuilder.Append("null, ");
                    }
                    else
                    {
                        cs2dsl.OutputExpressionSyntax(FirstRefArray);
                        codeBuilder.Append(", ");
                    }
                    if (null == SecondRefArray)
                    {
                        codeBuilder.Append("null, ");
                    }
                    else
                    {
                        cs2dsl.OutputExpressionSyntax(SecondRefArray);
                        codeBuilder.Append(", ");
                    }
                    codeBuilder.AppendFormat("\"{0}\"", mname);
                    prestr = ", ";
                }
                else
                {
                    if (sym.IsStatic)
                    {
                        if (externReturnStruct)
                        {
                            codeBuilder.Append("callexternstaticreturnstruct(");
                        }
                        else if (IsExternMethod)
                        {
                            codeBuilder.Append("callexternstatic(");
                        }
                        else
                        {
                            codeBuilder.Append("callstatic(");
                        }
                        codeBuilder.Append(ClassKey);
                    }
                    else
                    {
                        if (externReturnStruct)
                        {
                            codeBuilder.Append("callexterninstancereturnstruct(");
                        }
                        else if (IsExternMethod)
                        {
                            codeBuilder.Append("callexterninstance(");
                        }
                        else
                        {
                            codeBuilder.Append("callinstance(");
                        }
                        cs2dsl.OutputExpressionSyntax(exp);
                        codeBuilder.Append(", ");
                        codeBuilder.Append(ClassKey);
                    }
                    codeBuilder.AppendFormat(", \"{0}\"", mname);
                    prestr = ", ";
                }
            }
            else
            {
                if (sym.MethodKind == MethodKind.DelegateInvoke)
                {
                    cs2dsl.OutputExpressionSyntax(exp);
                    codeBuilder.Append("(");
                }
                else if (sym.IsStatic)
                {
                    if (externReturnStruct)
                    {
                        codeBuilder.Append("callexternstaticreturnstruct(");
                    }
                    else if (IsExternMethod)
                    {
                        codeBuilder.Append("callexternstatic(");
                    }
                    else
                    {
                        codeBuilder.Append("callstatic(");
                    }
                    codeBuilder.Append(ClassKey);
                    codeBuilder.AppendFormat(", \"{0}\"", mname);
                    prestr = ", ";
                }
                else
                {
                    if (externReturnStruct)
                    {
                        codeBuilder.Append("callexterninstancereturnstruct(");
                    }
                    else if (IsExternMethod)
                    {
                        codeBuilder.Append("callexterninstance(");
                    }
                    else
                    {
                        codeBuilder.Append("callinstance(");
                    }
                    codeBuilder.Append("this");
                    codeBuilder.AppendFormat(", {0}, \"{1}\"", ClassKey, mname);
                    prestr = ", ";
                }
            }
            if (Args.Count + DefaultValueArgs.Count + GenericTypeArgs.Count > 0)
            {
                codeBuilder.Append(prestr);
            }
            bool useTypeNameString = false;

            if (IsComponentGetOrAdd && SymbolTable.DslComponentByString)
            {
                var tArgs = sym.TypeArguments;
                if (tArgs.Length > 0 && SymbolTable.Instance.IsCs2DslSymbol(tArgs[0]))
                {
                    useTypeNameString = true;
                }
            }
            TypeChecker.CheckInvocation(model, sym, Args, DefaultValueArgs, ArgConversions, CallerSyntaxNode, CallerMethodSymbol);
            cs2dsl.OutputArgumentList(this, useTypeNameString, node);
            codeBuilder.Append(")");
        }