Пример #1
0
        public override void VisitEventDeclaration(EventDeclarationSyntax node)
        {
            var          ci      = m_ClassInfoStack.Peek();
            IEventSymbol declSym = m_Model.GetDeclaredSymbol(node);

            if (null != declSym)
            {
                string require = ClassInfo.GetAttributeArgument <string>(declSym, "Cs2Lua.RequireAttribute", 0);
                if (!string.IsNullOrEmpty(require))
                {
                    m_SymbolTable.AddRequire(ci.Key, require);
                }
                if (ClassInfo.HasAttribute(declSym, "Cs2Lua.IgnoreAttribute"))
                {
                    return;
                }
                if (declSym.IsAbstract)
                {
                    return;
                }
            }

            StringBuilder curBuilder = ci.CurrentCodeBuilder;

            if (declSym.IsStatic)
            {
                ci.CurrentCodeBuilder = ci.StaticFunctionCodeBuilder;
            }
            else
            {
                ci.CurrentCodeBuilder = ci.InstanceFunctionCodeBuilder;
            }
            foreach (var accessor in node.AccessorList.Accessors)
            {
                var sym = m_Model.GetDeclaredSymbol(accessor);
                if (null != sym)
                {
                    var mi = new MethodInfo();
                    mi.Init(sym, accessor);
                    m_MethodInfoStack.Push(mi);

                    string manglingName = NameMangling(sym);
                    string keyword      = accessor.Keyword.Text;
                    string paramStr     = string.Join(", ", mi.ParamNames.ToArray());
                    if (!declSym.IsStatic)
                    {
                        if (string.IsNullOrEmpty(paramStr))
                        {
                            paramStr = "this";
                        }
                        else
                        {
                            paramStr = "this, " + paramStr;
                        }
                    }
                    CodeBuilder.AppendFormat("{0}{1} = function({2})", GetIndentString(), manglingName, paramStr);
                    CodeBuilder.AppendLine();
                    ++m_Indent;
                    bool   isStatic    = declSym.IsStatic;
                    string luaModule   = ClassInfo.GetAttributeArgument <string>(sym, "Cs2Lua.TranslateToAttribute", 0);
                    string luaFuncName = ClassInfo.GetAttributeArgument <string>(sym, "Cs2Lua.TranslateToAttribute", 1);
                    if (!string.IsNullOrEmpty(luaModule) || !string.IsNullOrEmpty(luaFuncName))
                    {
                        if (!string.IsNullOrEmpty(luaModule))
                        {
                            m_SymbolTable.AddRequire(ci.Key, luaModule);
                        }
                        if (sym.ReturnsVoid && mi.ReturnParamNames.Count <= 0)
                        {
                            CodeBuilder.AppendFormat("{0}{1}({2}", GetIndentString(), luaFuncName, isStatic ? string.Empty : "this");
                        }
                        else
                        {
                            CodeBuilder.AppendFormat("{0}return {1}({2}", GetIndentString(), luaFuncName, isStatic ? string.Empty : "this");
                        }
                        if (mi.ParamNames.Count > 0)
                        {
                            if (!isStatic)
                            {
                                CodeBuilder.Append(", ");
                            }
                            CodeBuilder.Append(string.Join(", ", mi.ParamNames.ToArray()));
                        }
                        CodeBuilder.AppendLine(");");
                    }
                    else if (null != accessor.Body)
                    {
                        if (mi.ValueParams.Count > 0)
                        {
                            OutputWrapValueParams(CodeBuilder, mi);
                        }
                        VisitBlock(accessor.Body);
                    }
                    --m_Indent;
                    CodeBuilder.AppendFormat("{0}end,", GetIndentString());
                    CodeBuilder.AppendLine();

                    m_MethodInfoStack.Pop();
                }
            }
            ci.CurrentCodeBuilder = curBuilder;

            CodeBuilder.AppendFormat("{0}{1} = {{", GetIndentString(), SymbolTable.GetEventName(declSym));
            CodeBuilder.AppendLine();
            ++m_Indent;
            foreach (var accessor in node.AccessorList.Accessors)
            {
                var sym = m_Model.GetDeclaredSymbol(accessor);
                if (null != sym)
                {
                    string manglingName = NameMangling(sym);
                    string keyword      = accessor.Keyword.Text;
                    CodeBuilder.AppendFormat("{0}{1} = {2}.{3},", GetIndentString(), keyword, declSym.IsStatic ? "static_methods" : "instance_methods", manglingName);
                    CodeBuilder.AppendLine();
                }
            }
            --m_Indent;
            CodeBuilder.AppendFormat("{0}", GetIndentString());
            CodeBuilder.AppendLine("},");
        }
Пример #2
0
        private void BuildInheritTypeTreeRecursively(INamedTypeSymbol typeSym)
        {
            string       key = ClassInfo.GetFullName(typeSym);
            TypeTreeNode typeTreeNode;

            if (!m_TypeTreeNodes.TryGetValue(key, out typeTreeNode))
            {
                typeTreeNode      = new TypeTreeNode();
                typeTreeNode.Type = typeSym;
                m_TypeTreeNodes.Add(key, typeTreeNode);
            }
            var baseType = typeSym.BaseType;

            if (null != baseType)
            {
                string baseClassKey = ClassInfo.GetFullName(baseType);
                if (baseClassKey == SymbolTable.PrefixExternClassName("System.Object") || baseClassKey == SymbolTable.PrefixExternClassName("System.ValueType"))
                {
                }
                else
                {
                    TypeTreeNode baseTypeTreeNode;
                    if (!m_TypeTreeNodes.TryGetValue(baseClassKey, out baseTypeTreeNode))
                    {
                        baseTypeTreeNode      = new TypeTreeNode();
                        baseTypeTreeNode.Type = baseType;
                        m_TypeTreeNodes.Add(baseClassKey, baseTypeTreeNode);
                    }
                    typeTreeNode.BaseTypeNode = baseTypeTreeNode;
                    baseTypeTreeNode.ChildTypeNodes.Add(typeTreeNode);
                }
            }
            foreach (var newSym in typeSym.GetTypeMembers())
            {
                BuildInheritTypeTreeRecursively(newSym);
            }
        }
Пример #3
0
        internal void Init(INamedTypeSymbol typeSym, CSharpCompilation compilation, SymbolTable symTable)
        {
            if (typeSym.TypeKind == TypeKind.Error)
            {
                Logger.Instance.ReportIllegalType(typeSym);
                return;
            }

            IsInterface = typeSym.TypeKind == TypeKind.Interface;

            foreach (var intf in typeSym.AllInterfaces)
            {
                string          key = ClassInfo.GetFullName(intf);
                ClassSymbolInfo isi;
                if (!symTable.ClassSymbols.TryGetValue(key, out isi))
                {
                    isi = new ClassSymbolInfo();
                    symTable.ClassSymbols.Add(key, isi);
                    isi.Init(intf, compilation, symTable);
                }
            }

            ClassKey     = ClassInfo.GetFullName(typeSym);
            BaseClassKey = ClassInfo.GetFullName(typeSym.BaseType);
            if (BaseClassKey == SymbolTable.PrefixExternClassName("System.Object") || BaseClassKey == SymbolTable.PrefixExternClassName("System.ValueType"))
            {
                BaseClassKey = string.Empty;
            }
            ExistConstructor       = false;
            ExistStaticConstructor = false;

            if (typeSym.GetAttributes().Length > 0)
            {
                ExistAttributes = true;
            }

            bool fieldUseExplicitTypeParams  = false;
            bool staticUseExplicitTypeParams = false;

            TypeSymbol = typeSym;
            foreach (var sym in TypeSymbol.GetMembers())
            {
                var fsym = sym as IFieldSymbol;
                if (null != fsym)
                {
                    FieldSymbols.Add(fsym);

                    if (fsym.GetAttributes().Length > 0)
                    {
                        ExistAttributes = true;
                    }
                    CheckFieldUseExplicitTypeParam(fsym, compilation, ref fieldUseExplicitTypeParams, ref staticUseExplicitTypeParams);
                }
            }
            foreach (var sym in TypeSymbol.GetMembers())
            {
                var msym = sym as IMethodSymbol;
                if (null != msym)
                {
                    if (msym.MethodKind == MethodKind.Constructor && !msym.IsImplicitlyDeclared)
                    {
                        ExistConstructor = true;
                    }
                    else if (msym.MethodKind == MethodKind.StaticConstructor && !msym.IsImplicitlyDeclared)
                    {
                        ExistStaticConstructor = true;
                    }
                    MethodSymbols.Add(msym);

                    if (msym.GetAttributes().Length > 0)
                    {
                        ExistAttributes = true;
                    }

                    string name = msym.Name;
                    if (name[0] == '.')
                    {
                        name = name.Substring(1);
                    }
                    if (!SymbolOverloadFlags.ContainsKey(name))
                    {
                        SymbolOverloadFlags.Add(name, false);
                    }
                    else
                    {
                        SymbolOverloadFlags[name] = true;
                    }
                    continue;
                }
                var psym = sym as IPropertySymbol;
                if (null != psym && !psym.IsIndexer)
                {
                    PropertySymbols.Add(psym);

                    if (psym.GetAttributes().Length > 0)
                    {
                        ExistAttributes = true;
                    }
                    continue;
                }
                var esym = sym as IEventSymbol;
                if (null != esym)
                {
                    EventSymbols.Add(esym);

                    if (esym.GetAttributes().Length > 0)
                    {
                        ExistAttributes = true;
                    }
                    continue;
                }
            }
            BuildInterfaceInfo(typeSym, compilation, symTable);
        }
Пример #4
0
        public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
        {
            var             ci      = m_ClassInfoStack.Peek();
            IPropertySymbol declSym = m_Model.GetDeclaredSymbol(node);

            if (null != declSym)
            {
                string require = ClassInfo.GetAttributeArgument <string>(declSym, "Cs2Lua.RequireAttribute", 0);
                if (!string.IsNullOrEmpty(require))
                {
                    m_SymbolTable.AddRequire(ci.Key, require);
                }
                if (ClassInfo.HasAttribute(declSym, "Cs2Lua.IgnoreAttribute"))
                {
                    return;
                }
                if (declSym.IsAbstract)
                {
                    return;
                }
            }

            bool noimpl = true;

            foreach (var accessor in node.AccessorList.Accessors)
            {
                var sym = m_Model.GetDeclaredSymbol(accessor);
                if (null != accessor.Body)
                {
                    noimpl = false;
                    break;
                }
            }

            if (noimpl)
            {
                //退化为field
                StringBuilder curBuilder = ci.CurrentCodeBuilder;

                if (declSym.IsStatic)
                {
                    ci.CurrentCodeBuilder = ci.StaticFieldCodeBuilder;
                }
                else
                {
                    ci.CurrentCodeBuilder = ci.InstanceFieldCodeBuilder;
                }

                ++m_Indent;
                CodeBuilder.AppendFormat("{0}{1} = ", GetIndentString(), SymbolTable.GetPropertyName(declSym));
                if (null != node.Initializer)
                {
                    IConversionExpression opd = null;
                    var oper = m_Model.GetOperation(node.Initializer) as ISymbolInitializer;
                    if (null != oper)
                    {
                        opd = oper.Value as IConversionExpression;
                    }
                    OutputExpressionSyntax(node.Initializer.Value, opd);
                    CodeBuilder.Append(",");
                }
                else
                {
                    CodeBuilder.Append("__cs2lua_nil_field_value,");
                }
                CodeBuilder.AppendLine();
                --m_Indent;

                ci.CurrentCodeBuilder = curBuilder;
            }
            else
            {
                StringBuilder curBuilder = ci.CurrentCodeBuilder;
                if (declSym.IsStatic)
                {
                    ci.CurrentCodeBuilder = ci.StaticFunctionCodeBuilder;
                }
                else
                {
                    ci.CurrentCodeBuilder = ci.InstanceFunctionCodeBuilder;
                }
                foreach (var accessor in node.AccessorList.Accessors)
                {
                    var sym = m_Model.GetDeclaredSymbol(accessor);
                    if (null != sym)
                    {
                        var mi = new MethodInfo();
                        mi.Init(sym, accessor);
                        m_MethodInfoStack.Push(mi);

                        string manglingName = NameMangling(sym);
                        string keyword      = accessor.Keyword.Text;
                        string paramStr     = string.Join(", ", mi.ParamNames.ToArray());
                        if (!declSym.IsStatic)
                        {
                            if (string.IsNullOrEmpty(paramStr))
                            {
                                paramStr = "this";
                            }
                            else
                            {
                                paramStr = "this, " + paramStr;
                            }
                        }
                        CodeBuilder.AppendFormat("{0}{1} = {2}function({3})", GetIndentString(), manglingName, mi.ExistYield ? "wrapenumerable(" : string.Empty, paramStr);
                        CodeBuilder.AppendLine();
                        ++m_Indent;
                        bool   isStatic    = declSym.IsStatic;
                        string luaModule   = ClassInfo.GetAttributeArgument <string>(sym, "Cs2Lua.TranslateToAttribute", 0);
                        string luaFuncName = ClassInfo.GetAttributeArgument <string>(sym, "Cs2Lua.TranslateToAttribute", 1);
                        if (!string.IsNullOrEmpty(luaModule) || !string.IsNullOrEmpty(luaFuncName))
                        {
                            if (!string.IsNullOrEmpty(luaModule))
                            {
                                m_SymbolTable.AddRequire(ci.Key, luaModule);
                            }
                            if (sym.ReturnsVoid && mi.ReturnParamNames.Count <= 0)
                            {
                                CodeBuilder.AppendFormat("{0}{1}({2}", GetIndentString(), luaFuncName, isStatic ? string.Empty : "this");
                            }
                            else
                            {
                                CodeBuilder.AppendFormat("{0}return {1}({2}", GetIndentString(), luaFuncName, isStatic ? string.Empty : "this");
                            }
                            if (mi.ParamNames.Count > 0)
                            {
                                if (!isStatic)
                                {
                                    CodeBuilder.Append(", ");
                                }
                                CodeBuilder.Append(string.Join(", ", mi.ParamNames.ToArray()));
                            }
                            CodeBuilder.AppendLine(");");
                        }
                        else if (null != accessor.Body)
                        {
                            if (mi.ValueParams.Count > 0)
                            {
                                OutputWrapValueParams(CodeBuilder, mi);
                            }
                            VisitBlock(accessor.Body);
                        }
                        --m_Indent;
                        CodeBuilder.AppendFormat("{0}end{1},", GetIndentString(), mi.ExistYield ? ")" : string.Empty);
                        CodeBuilder.AppendLine();

                        m_MethodInfoStack.Pop();
                    }
                }
                ci.CurrentCodeBuilder = curBuilder;

                CodeBuilder.AppendFormat("{0}{1} = {{", GetIndentString(), SymbolTable.GetPropertyName(declSym));
                CodeBuilder.AppendLine();
                ++m_Indent;
                foreach (var accessor in node.AccessorList.Accessors)
                {
                    var sym = m_Model.GetDeclaredSymbol(accessor);
                    if (null != sym)
                    {
                        string manglingName = NameMangling(sym);
                        string keyword      = accessor.Keyword.Text;
                        CodeBuilder.AppendFormat("{0}{1} = {2}.{3},", GetIndentString(), keyword, declSym.IsStatic ? "static_methods" : "instance_methods", manglingName);
                        CodeBuilder.AppendLine();
                    }
                }
                --m_Indent;
                CodeBuilder.AppendFormat("{0}", GetIndentString());
                CodeBuilder.AppendLine("},");
            }
        }
Пример #5
0
        private void ProcessEqualOrNotEqual(string op, ExpressionSyntax left, ExpressionSyntax right, IConversionExpression lopd, IConversionExpression ropd)
        {
            var leftOper  = m_Model.GetOperation(left);
            var rightOper = m_Model.GetOperation(right);

            if (null != leftOper.Type && leftOper.Type.TypeKind == TypeKind.Delegate && (!leftOper.ConstantValue.HasValue || null != leftOper.ConstantValue.Value) && rightOper.ConstantValue.HasValue && rightOper.ConstantValue.Value == null)
            {
                var leftAssembly = leftOper.Type.ContainingAssembly;
                var sym          = m_Model.GetSymbolInfo(left);
                OutputDelegationCompareWithNull(sym.Symbol, left, leftAssembly == m_SymbolTable.AssemblySymbol, op == "==", lopd);
            }
            else if (null != rightOper.Type && rightOper.Type.TypeKind == TypeKind.Delegate && (!rightOper.ConstantValue.HasValue || null != rightOper.ConstantValue.Value) && leftOper.ConstantValue.HasValue && leftOper.ConstantValue.Value == null)
            {
                var rightAssembly = rightOper.Type.ContainingAssembly;
                var sym           = m_Model.GetSymbolInfo(right);
                OutputDelegationCompareWithNull(sym.Symbol, right, rightAssembly == m_SymbolTable.AssemblySymbol, op == "==", ropd);
            }
            else if (null != leftOper && null != rightOper && (leftOper.ConstantValue.HasValue && null == leftOper.ConstantValue.Value || rightOper.ConstantValue.HasValue && null == rightOper.ConstantValue.Value || SymbolTable.IsBasicType(leftOper.Type) || SymbolTable.IsBasicType(rightOper.Type)))
            {
                CodeBuilder.Append("(");
                OutputExpressionSyntax(left, lopd);
                CodeBuilder.AppendFormat(" {0} ", op);
                OutputExpressionSyntax(right, ropd);
                CodeBuilder.Append(")");
            }
            else
            {
                if (op == "==")
                {
                    CodeBuilder.Append("isequal(");
                    OutputExpressionSyntax(left, lopd);
                    CodeBuilder.Append(", ");
                    OutputExpressionSyntax(right, ropd);
                    CodeBuilder.Append(")");
                }
                else
                {
                    CodeBuilder.Append("(not isequal(");
                    OutputExpressionSyntax(left, lopd);
                    CodeBuilder.Append(", ");
                    OutputExpressionSyntax(right, ropd);
                    CodeBuilder.Append("))");
                }
            }
        }
Пример #6
0
 private void BuildInterfaceInfo(INamedTypeSymbol typeSym, CSharpCompilation compilation, SymbolTable symTable)
 {
     foreach (var intf in typeSym.AllInterfaces)
     {
         if (!InterfaceSymbols.Contains(intf))
         {
             InterfaceSymbols.Add(intf);
         }
         foreach (var sym in intf.GetMembers())
         {
             var msym = sym as IMethodSymbol;
             if (null != msym)
             {
                 var implSym = typeSym.FindImplementationForInterfaceMember(sym) as IMethodSymbol;
                 if (null != implSym)
                 {
                     string name = symTable.NameMangling(msym);
                     name = ClassInfo.CalcNameWithFullTypeName(name, sym.ContainingType);
                     string implName = symTable.NameMangling(implSym);
                     if (!InterfaceMethodMap.ContainsKey(name))
                     {
                         InterfaceMethodMap.Add(name, implName);
                     }
                 }
             }
             var psym = sym as IPropertySymbol;
             if (null != psym && !psym.IsIndexer)
             {
                 var implSym = typeSym.FindImplementationForInterfaceMember(sym) as IPropertySymbol;
                 if (null != implSym && !psym.IsIndexer)
                 {
                     string name = SymbolTable.GetPropertyName(psym);
                     name = ClassInfo.CalcNameWithFullTypeName(name, sym.ContainingType);
                     string implName = SymbolTable.GetPropertyName(implSym);
                     if (!InterfaceMethodMap.ContainsKey(name))
                     {
                         InterfaceMethodMap.Add(name, implName);
                     }
                 }
             }
             var esym = sym as IEventSymbol;
             if (null != esym)
             {
                 var implSym = typeSym.FindImplementationForInterfaceMember(sym) as IEventSymbol;
                 if (null != implSym)
                 {
                     string name = SymbolTable.GetEventName(esym);
                     name = ClassInfo.CalcNameWithFullTypeName(name, sym.ContainingType);
                     string implName = SymbolTable.GetEventName(implSym);
                     if (!InterfaceMethodMap.ContainsKey(name))
                     {
                         InterfaceMethodMap.Add(name, implName);
                     }
                 }
             }
         }
     }
     if (typeSym.TypeKind != TypeKind.Interface)
     {
         foreach (var sym in typeSym.GetMembers())
         {
             var msym = sym as IMethodSymbol;
             if (null != msym && msym.ExplicitInterfaceImplementations.Length > 0)
             {
                 foreach (var implSym in msym.ExplicitInterfaceImplementations)
                 {
                     string          fn = ClassInfo.GetFullName(implSym.ContainingType);
                     ClassSymbolInfo csi;
                     if (symTable.ClassSymbols.TryGetValue(fn, out csi))
                     {
                         if (!csi.ExplicitInterfaceImplementationMethods.Contains(implSym))
                         {
                             csi.ExplicitInterfaceImplementationMethods.Add(implSym);
                         }
                     }
                 }
             }
             var psym = sym as IPropertySymbol;
             if (null != psym && !psym.IsIndexer && psym.ExplicitInterfaceImplementations.Length > 0)
             {
                 foreach (var implSym in psym.ExplicitInterfaceImplementations)
                 {
                     string          fn = ClassInfo.GetFullName(implSym.ContainingType);
                     ClassSymbolInfo csi;
                     if (symTable.ClassSymbols.TryGetValue(fn, out csi))
                     {
                         if (!csi.ExplicitInterfaceImplementationProperties.Contains(implSym))
                         {
                             csi.ExplicitInterfaceImplementationProperties.Add(implSym);
                         }
                     }
                 }
             }
             var esym = sym as IEventSymbol;
             if (null != esym && esym.ExplicitInterfaceImplementations.Length > 0)
             {
                 foreach (var implSym in esym.ExplicitInterfaceImplementations)
                 {
                     string          fn = ClassInfo.GetFullName(implSym.ContainingType);
                     ClassSymbolInfo csi;
                     if (symTable.ClassSymbols.TryGetValue(fn, out csi))
                     {
                         if (!csi.ExplicitInterfaceImplementationEvents.Contains(implSym))
                         {
                             csi.ExplicitInterfaceImplementationEvents.Add(implSym);
                         }
                     }
                 }
             }
         }
     }
 }
Пример #7
0
 internal void FillTypeParamsAndArgs(INamedTypeSymbol refType)
 {
     TypeParameters.AddRange(SymbolTable.Instance.TypeParameters);
     TypeArguments.AddRange(SymbolTable.Instance.TypeArguments);
     SymbolTable.MergeTypeParamsAndArgs(TypeParameters, TypeArguments, refType);
 }
Пример #8
0
        internal bool CheckExplicitInterfaceAccess(ISymbol sym, ref string nameOfIntf, ref string mname)
        {
            bool ret = false;

            if (sym.ContainingType.TypeKind == TypeKind.Interface)
            {
                string          fn = ClassInfo.GetFullName(sym.ContainingType);
                ClassSymbolInfo csi;
                if (SymbolTable.Instance.ClassSymbols.TryGetValue(fn, out csi))
                {
                    switch (sym.Kind)
                    {
                    case SymbolKind.Method:
                        IMethodSymbol msym = sym as IMethodSymbol;
                        if (csi.ExplicitInterfaceImplementationMethods.Contains(msym))
                        {
                            ret = true;
                            if (null != nameOfIntf)
                            {
                                nameOfIntf = string.Format("\"{0}\"", fn.Replace(".", "_"));
                            }
                            if (null != mname)
                            {
                                mname = string.Format("\"{0}\"", NameMangling(msym));
                            }
                        }
                        break;

                    case SymbolKind.Property:
                        IPropertySymbol psym = sym as IPropertySymbol;
                        if (csi.ExplicitInterfaceImplementationProperties.Contains(psym))
                        {
                            ret = true;
                            if (null != nameOfIntf)
                            {
                                nameOfIntf = string.Format("\"{0}\"", fn.Replace(".", "_"));
                            }
                            if (null != mname)
                            {
                                mname = string.Format("\"{0}\"", SymbolTable.GetPropertyName(psym));
                            }
                        }
                        break;

                    case SymbolKind.Event:
                        IEventSymbol esym = sym as IEventSymbol;
                        if (csi.ExplicitInterfaceImplementationEvents.Contains(esym))
                        {
                            ret = true;
                            if (null != nameOfIntf)
                            {
                                nameOfIntf = string.Format("\"{0}\"", fn.Replace(".", "_"));
                            }
                            if (null != mname)
                            {
                                mname = string.Format("\"{0}\"", SymbolTable.GetEventName(esym));
                            }
                        }
                        break;
                    }
                }
            }
            return(ret);
        }
Пример #9
0
        public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
        {
            SymbolInfo symInfo = m_Model.GetSymbolInfo(node);
            var        sym     = symInfo.Symbol;

            string className = string.Empty;

            if (null != sym && sym.IsStatic && null != sym.ContainingType)
            {
                className = ClassInfo.GetFullName(sym.ContainingType);
            }

            if (null != sym)
            {
                if (sym.IsStatic)
                {
                    var ci = m_ClassInfoStack.Peek();
                    AddReferenceAndTryDeriveGenericTypeInstance(ci, sym);
                }
            }
            else
            {
                ReportIllegalSymbol(node, symInfo);
            }
            if (node.Parent is InvocationExpressionSyntax)
            {
                var    msym         = sym as IMethodSymbol;
                string manglingName = NameMangling(msym);
                if (string.IsNullOrEmpty(className))
                {
                    VisitExpressionSyntax(node.Expression);
                    CodeBuilder.Append(":");
                }
                else
                {
                    CodeBuilder.Append(className);
                    CodeBuilder.Append(".");
                }
                CodeBuilder.Append(manglingName);
            }
            else
            {
                var msym = sym as IMethodSymbol;
                if (null != msym)
                {
                    string manglingName = NameMangling(msym);
                    var    mi           = new MethodInfo();
                    mi.Init(msym, node);

                    CodeBuilder.Append("(function(");
                    string paramsString = string.Join(", ", mi.ParamNames.ToArray());
                    CodeBuilder.Append(paramsString);
                    CodeBuilder.Append(") ");
                    if (!msym.ReturnsVoid)
                    {
                        CodeBuilder.Append("return ");
                    }
                    if (string.IsNullOrEmpty(className))
                    {
                        VisitExpressionSyntax(node.Expression);
                        CodeBuilder.Append(":");
                    }
                    else
                    {
                        CodeBuilder.Append(className);
                        CodeBuilder.Append(".");
                    }
                    CodeBuilder.Append(manglingName);
                    CodeBuilder.AppendFormat("({0}) end)", paramsString);
                }
                else
                {
                    var    psym     = sym as IPropertySymbol;
                    string fnOfIntf = string.Empty;
                    string mname    = string.Empty;
                    bool   propExplicitImplementInterface = false;
                    bool   propForBasicValueType          = false;
                    if (null != psym)
                    {
                        if (!psym.IsStatic)
                        {
                            propExplicitImplementInterface = CheckExplicitInterfaceAccess(psym, ref fnOfIntf, ref mname);
                            propForBasicValueType          = SymbolTable.IsBasicValueProperty(psym);
                        }
                    }
                    if (propExplicitImplementInterface)
                    {
                        CodeBuilder.AppendFormat("getwithinterface(");
                        VisitExpressionSyntax(node.Expression);
                        CodeBuilder.AppendFormat(", \"{0}\", \"{1}\")", fnOfIntf, mname);
                    }
                    else if (propForBasicValueType)
                    {
                        string pname = psym.Name;
                        CodeBuilder.AppendFormat("getforbasicvalue(");
                        VisitExpressionSyntax(node.Expression);
                        CodeBuilder.AppendFormat(", \"{0}\", \"{1}\")", className, pname);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(className))
                        {
                            VisitExpressionSyntax(node.Expression);
                        }
                        else
                        {
                            CodeBuilder.Append(className);
                        }
                        CodeBuilder.Append(node.OperatorToken.Text);
                        CodeBuilder.Append(node.Name.Identifier.Text);
                    }
                }
            }
        }
Пример #10
0
        private void Init(IMethodSymbol sym)
        {
            MethodSymbol = sym;

            Args.Clear();
            ArgConversions.Clear();
            ReturnArgs.Clear();
            GenericTypeArgs.Clear();

            ClassKey            = ClassInfo.GetFullName(sym.ContainingType);
            GenericClassKey     = ClassInfo.GetFullNameWithTypeParameters(sym.ContainingType);
            IsExtensionMethod   = sym.IsExtensionMethod && SymbolTable.Instance.IsCs2LuaSymbol(sym);
            IsBasicValueMethod  = SymbolTable.IsBasicValueMethod(sym);
            IsArrayStaticMethod = ClassKey == SymbolTable.PrefixExternClassName("System.Array") && sym.IsStatic;

            if ((ClassKey == SymbolTable.PrefixExternClassName("UnityEngine.GameObject") || ClassKey == SymbolTable.PrefixExternClassName("UnityEngine.Component")) && (sym.Name.StartsWith("GetComponent") || sym.Name.StartsWith("AddComponent")))
            {
                IsComponentGetOrAdd = true;
            }

            if (sym.IsGenericMethod)
            {
                foreach (var arg in sym.TypeArguments)
                {
                    GenericTypeArgs.Add(arg);
                }
            }
        }
Пример #11
0
        internal void OutputInvocation(StringBuilder codeBuilder, CsLuaTranslater cs2lua, ExpressionSyntax exp, bool isMemberAccess, SemanticModel model, SyntaxNode node)
        {
            IMethodSymbol sym    = MethodSymbol;
            string        mname  = cs2lua.NameMangling(IsExtensionMethod && null != sym.ReducedFrom ? sym.ReducedFrom : sym);
            string        prestr = string.Empty;

            if (isMemberAccess)
            {
                string fnOfIntf = "nil";
                bool   isExplicitInterfaceInvoke = cs2lua.CheckExplicitInterfaceAccess(sym, ref fnOfIntf);
                if (sym.MethodKind == MethodKind.DelegateInvoke)
                {
                    var memberAccess = node as MemberAccessExpressionSyntax;
                    if (null != memberAccess)
                    {
                        cs2lua.OutputExpressionSyntax(exp);
                        codeBuilder.Append(".");
                        codeBuilder.Append(memberAccess.Name);
                        codeBuilder.Append("(");
                    }
                    else
                    {
                        //error;
                    }
                }
                else if (isExplicitInterfaceInvoke)
                {
                    codeBuilder.Append("invokewithinterface(");
                    cs2lua.OutputExpressionSyntax(exp);
                    codeBuilder.Append(", ");
                    codeBuilder.AppendFormat("{0}, \"{1}\"", fnOfIntf, mname);
                    prestr = ", ";
                }
                else if (IsExtensionMethod)
                {
                    codeBuilder.Append(ClassKey);
                    codeBuilder.Append(".");
                    codeBuilder.Append(mname);
                    codeBuilder.Append("(");
                    cs2lua.OutputExpressionSyntax(exp);
                    prestr = ", ";
                }
                else if (IsBasicValueMethod)
                {
                    string ckey = CalcInvokeTarget(ClassKey, cs2lua, exp, model);
                    codeBuilder.Append("invokeforbasicvalue(");
                    cs2lua.OutputExpressionSyntax(exp);
                    codeBuilder.Append(", ");
                    codeBuilder.AppendFormat("{0}, {1}, \"{2}\"", ClassKey == SymbolTable.PrefixExternClassName("System.Enum") ? "true" : "false", ckey, mname);
                    prestr = ", ";
                }
                else if (IsArrayStaticMethod)
                {
                    codeBuilder.Append("invokearraystaticmethod(");
                    if (null == FirstRefArray)
                    {
                        codeBuilder.Append("nil, ");
                    }
                    else
                    {
                        cs2lua.OutputExpressionSyntax(FirstRefArray);
                        codeBuilder.Append(", ");
                    }
                    if (null == SecondRefArray)
                    {
                        codeBuilder.Append("nil, ");
                    }
                    else
                    {
                        cs2lua.OutputExpressionSyntax(SecondRefArray);
                        codeBuilder.Append(", ");
                    }
                    codeBuilder.AppendFormat("\"{0}\"", mname);
                    prestr = ", ";
                }
                else
                {
                    if (sym.IsStatic)
                    {
                        codeBuilder.Append(ClassKey);
                        codeBuilder.Append(".");
                    }
                    else
                    {
                        cs2lua.OutputExpressionSyntax(exp);
                        codeBuilder.Append(":");
                    }
                    codeBuilder.Append(mname);
                    codeBuilder.Append("(");
                }
            }
            else
            {
                if (sym.MethodKind == MethodKind.DelegateInvoke)
                {
                    cs2lua.OutputExpressionSyntax(exp);
                }
                else if (sym.IsStatic)
                {
                    codeBuilder.AppendFormat("{0}.", ClassKey);
                    codeBuilder.Append(mname);
                }
                else
                {
                    codeBuilder.Append("this:");
                    codeBuilder.Append(mname);
                }
                codeBuilder.Append("(");
            }
            if (Args.Count + DefaultValueArgs.Count + GenericTypeArgs.Count > 0)
            {
                codeBuilder.Append(prestr);
            }
            bool useTypeNameString = false;

            if (IsComponentGetOrAdd && SymbolTable.LuaComponentByString)
            {
                var tArgs = sym.TypeArguments;
                if (tArgs.Length > 0 && SymbolTable.Instance.IsCs2LuaSymbol(tArgs[0]))
                {
                    useTypeNameString = true;
                }
            }
            cs2lua.OutputArgumentList(Args, DefaultValueArgs, GenericTypeArgs, ArrayToParams, useTypeNameString, node, ArgConversions.ToArray());
            codeBuilder.Append(")");
        }