Example #1
0
        public static string ToJsName(IType type, IEmitter emitter, bool asDefinition = false, bool excludens = false, bool isAlias = false, bool skipMethodTypeParam = false, bool removeScope = true, bool nomodule = false, bool ignoreLiteralName = true, bool ignoreVirtual = false, bool excludeTypeOnly = false)
        {
            var        itypeDef   = type.GetDefinition();
            BridgeType bridgeType = emitter.BridgeTypes.Get(type, true);

            if (itypeDef != null)
            {
                string globalTarget = BridgeTypes.GetGlobalTarget(itypeDef, null, removeScope);

                if (globalTarget != null)
                {
                    if (bridgeType != null && !nomodule)
                    {
                        bool customName;
                        globalTarget = BridgeTypes.AddModule(globalTarget, bridgeType, excludens, false, out customName);
                    }
                    return(globalTarget);
                }
            }

            if (itypeDef != null && itypeDef.Attributes.Any(a => a.AttributeType.FullName == "Bridge.NonScriptableAttribute"))
            {
                throw new EmitterException(emitter.Translator.EmitNode, "Type " + type.FullName + " is marked as not usable from script");
            }

            if (type.Kind == TypeKind.Array)
            {
                var arrayType = type as ArrayType;

                if (arrayType != null && arrayType.ElementType != null)
                {
                    var elementAlias = BridgeTypes.ToJsName(arrayType.ElementType, emitter, asDefinition, excludens, isAlias, skipMethodTypeParam, excludeTypeOnly: excludeTypeOnly);

                    if (isAlias)
                    {
                        return($"{elementAlias}$Array{(arrayType.Dimensions > 1 ? "$" + arrayType.Dimensions : "")}");
                    }

                    if (arrayType.Dimensions > 1)
                    {
                        return(string.Format(JS.Types.System.Array.TYPE + "({0}, {1})", elementAlias, arrayType.Dimensions));
                    }
                    return(string.Format(JS.Types.System.Array.TYPE + "({0})", elementAlias));
                }

                return(JS.Types.ARRAY);
            }

            if (type.Kind == TypeKind.Delegate)
            {
                return(JS.Types.FUNCTION);
            }

            if (type.Kind == TypeKind.Dynamic)
            {
                return(JS.Types.System.Object.NAME);
            }

            if (type is ByReferenceType)
            {
                return(BridgeTypes.ToJsName(((ByReferenceType)type).ElementType, emitter, asDefinition, excludens, isAlias, skipMethodTypeParam, excludeTypeOnly: excludeTypeOnly));
            }

            if (ignoreLiteralName)
            {
                var isObjectLiteral = itypeDef != null && emitter.Validator.IsObjectLiteral(itypeDef);
                var isPlainMode     = isObjectLiteral && emitter.Validator.GetObjectCreateMode(emitter.GetTypeDefinition(type)) == 0;

                if (isPlainMode)
                {
                    return("System.Object");
                }
            }

            if (type.Kind == TypeKind.Anonymous)
            {
                var at = type as AnonymousType;
                if (at != null && emitter.AnonymousTypes.ContainsKey(at))
                {
                    return(emitter.AnonymousTypes[at].Name);
                }
                else
                {
                    return(JS.Types.System.Object.NAME);
                }
            }

            var typeParam = type as ITypeParameter;

            if (typeParam != null)
            {
                if ((skipMethodTypeParam || excludeTypeOnly) && (typeParam.OwnerType == SymbolKind.Method) || Helpers.IsIgnoreGeneric(typeParam.Owner, emitter))
                {
                    return(JS.Types.System.Object.NAME);
                }
            }

            var name = excludens ? "" : type.Namespace;

            var hasTypeDef = bridgeType != null && bridgeType.TypeDefinition != null;
            var isNested   = false;

            if (hasTypeDef)
            {
                var typeDef = bridgeType.TypeDefinition;

                if (typeDef.IsNested && !excludens)
                {
                    name     = BridgeTypes.ToJsName(typeDef.DeclaringType, emitter, true, ignoreVirtual: true, nomodule: nomodule);
                    isNested = true;
                }

                name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.ConvertName(emitter.GetTypeName(itypeDef, typeDef));
            }
            else
            {
                if (type.DeclaringType != null && !excludens)
                {
                    name     = BridgeTypes.ToJsName(type.DeclaringType, emitter, true, ignoreVirtual: true);
                    isNested = true;
                }

                name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.ConvertName(type.Name);
            }

            bool isCustomName = false;

            if (bridgeType != null)
            {
                if (nomodule)
                {
                    name = GetCustomName(name, bridgeType, excludens, isNested, ref isCustomName, null);
                }
                else
                {
                    name = BridgeTypes.AddModule(name, bridgeType, excludens, isNested, out isCustomName);
                }
            }

            var tDef       = type.GetDefinition();
            var skipSuffix = tDef != null && tDef.ParentAssembly.AssemblyName != CS.NS.BRIDGE && emitter.Validator.IsExternalType(tDef) && Helpers.IsIgnoreGeneric(tDef);

            if (!hasTypeDef && !isCustomName && type.TypeArguments.Count > 0 && !skipSuffix)
            {
                name += Helpers.PrefixDollar(type.TypeArguments.Count);
            }

            var genericSuffix = "$" + type.TypeArguments.Count;

            if (skipSuffix && !isCustomName && type.TypeArguments.Count > 0 && name.EndsWith(genericSuffix))
            {
                name = name.Substring(0, name.Length - genericSuffix.Length);
            }

            if (isAlias)
            {
                name = OverloadsCollection.NormalizeInterfaceName(name);
            }

            if (type.TypeArguments.Count > 0 && !Helpers.IsIgnoreGeneric(type, emitter) && !asDefinition && !skipMethodTypeParam)
            {
                if (isAlias)
                {
                    StringBuilder sb        = new StringBuilder(name);
                    bool          needComma = false;
                    sb.Append(JS.Vars.D);
                    bool isStr = false;
                    foreach (var typeArg in type.TypeArguments)
                    {
                        if (sb.ToString().EndsWith(")"))
                        {
                            sb.Append(" + \"");
                        }

                        if (needComma && !sb.ToString().EndsWith(JS.Vars.D.ToString()))
                        {
                            sb.Append(JS.Vars.D);
                        }

                        needComma = true;
                        var  isTypeParam = typeArg.Kind == TypeKind.TypeParameter;
                        bool needGet     = isTypeParam && !asDefinition && !excludeTypeOnly;
                        if (needGet)
                        {
                            if (!isStr)
                            {
                                sb.Insert(0, "\"");
                                isStr = true;
                            }
                            sb.Append("\" + " + JS.Types.Bridge.GET_TYPE_ALIAS + "(");
                        }

                        var typeArgName = BridgeTypes.ToJsName(typeArg, emitter, asDefinition, false, true, skipMethodTypeParam, ignoreVirtual: true, excludeTypeOnly: excludeTypeOnly);

                        if (!needGet && typeArgName.StartsWith("\""))
                        {
                            var tName = typeArgName.Substring(1);

                            if (tName.EndsWith("\""))
                            {
                                tName = tName.Remove(tName.Length - 1);
                            }

                            sb.Append(tName);

                            if (!isStr)
                            {
                                isStr = true;
                                sb.Insert(0, "\"");
                            }
                        }
                        else if (!isTypeParam || !excludeTypeOnly)
                        {
                            sb.Append(typeArgName);
                        }

                        if (needGet)
                        {
                            sb.Append(")");
                        }
                    }

                    if (isStr && sb.Length >= 1)
                    {
                        var sbEnd = sb.ToString(sb.Length - 1, 1);

                        if (!sbEnd.EndsWith(")") && !sbEnd.EndsWith("\""))
                        {
                            sb.Append("\"");
                        }
                    }

                    name = sb.ToString();
                }
                else
                {
                    StringBuilder sb        = new StringBuilder(name);
                    bool          needComma = false;
                    sb.Append("(");
                    foreach (var typeArg in type.TypeArguments)
                    {
                        if (needComma)
                        {
                            sb.Append(",");
                        }

                        needComma = true;

                        sb.Append(BridgeTypes.ToJsName(typeArg, emitter, skipMethodTypeParam: skipMethodTypeParam, excludeTypeOnly: excludeTypeOnly));
                    }
                    sb.Append(")");
                    name = sb.ToString();
                }
            }

            if (!ignoreVirtual && !isAlias)
            {
                var td = type.GetDefinition();
                if (td != null && emitter.Validator.IsVirtualType(td))
                {
                    string fnName = td.Kind == TypeKind.Interface ? JS.Types.Bridge.GET_INTERFACE : JS.Types.Bridge.GET_CLASS;
                    name = fnName + "(\"" + name + "\")";
                }
                else if (!isAlias && itypeDef != null && itypeDef.Kind == TypeKind.Interface)
                {
                    var externalInterface = emitter.Validator.IsExternalInterface(itypeDef);
                    if (externalInterface != null && externalInterface.IsVirtual)
                    {
                        name = JS.Types.Bridge.GET_INTERFACE + "(\"" + name + "\")";
                    }
                }
            }

            return(name);
        }
 public static bool ExcludeTypeParameterForDefinition(MemberResolveResult rr)
 {
     return(rr != null && OverloadsCollection.ExcludeTypeParameterForDefinition(rr.Member));
 }
        protected virtual string GetOverloadName(IMember definition, bool skipInterfaceName = false, string prefix = null, bool withoutTypeParams = false, bool isObjectLiteral = false)
        {
            IMember interfaceMember = null;

            if (definition.IsExplicitInterfaceImplementation)
            {
                interfaceMember = definition.ImplementedInterfaceMembers.First();
            }
            else if (definition.DeclaringTypeDefinition != null && definition.DeclaringTypeDefinition.Kind == TypeKind.Interface)
            {
                interfaceMember = definition;
            }

            if (interfaceMember != null && !skipInterfaceName && !this.Emitter.Validator.IsObjectLiteral(interfaceMember.DeclaringTypeDefinition))
            {
                return(OverloadsCollection.GetInterfaceMemberName(this.Emitter, interfaceMember, null, prefix, withoutTypeParams, this.IsSetter));
            }

            string name = isObjectLiteral ? this.Emitter.GetLiteralEntityName(definition) : this.Emitter.GetEntityName(definition);

            if (name.StartsWith("." + JS.Funcs.CONSTRUCTOR))
            {
                name = JS.Funcs.CONSTRUCTOR;
            }

            var attr = Helpers.GetInheritedAttribute(definition, "Bridge.NameAttribute");

            var iProperty = definition as IProperty;

            if (attr == null && iProperty != null && !IsField)
            {
                var acceessor = this.IsSetter ? iProperty.Setter : iProperty.Getter;

                if (acceessor != null)
                {
                    attr = Helpers.GetInheritedAttribute(acceessor, "Bridge.NameAttribute");

                    if (attr != null)
                    {
                        name = this.Emitter.GetEntityName(acceessor);
                    }
                }
            }

            if (attr != null)
            {
                if (!(iProperty != null || definition is IEvent))
                {
                    prefix = null;
                }
            }

            if (attr != null && definition.ImplementedInterfaceMembers.Count > 0)
            {
                if (this.Members.Where(member => member.ImplementedInterfaceMembers.Count > 0)
                    .Any(member => definition.ImplementedInterfaceMembers.Any(implementedInterfaceMember => member.ImplementedInterfaceMembers.Any(m => m.DeclaringTypeDefinition == implementedInterfaceMember.DeclaringTypeDefinition))))
                {
                    attr = null;
                }
            }

            bool skipSuffix = false;

            if (definition.DeclaringTypeDefinition != null &&
                this.Emitter.Validator.IsExternalType(definition.DeclaringTypeDefinition))
            {
                if (definition.DeclaringTypeDefinition.Kind == TypeKind.Interface)
                {
                    skipSuffix = definition.DeclaringTypeDefinition.ParentAssembly.AssemblyName != CS.NS.ROOT;
                }
                else
                {
                    skipSuffix = true;
                }
            }

            if (attr != null || skipSuffix)
            {
                return(prefix != null ? prefix + name : name);
            }

            var iDefinition = definition as IMethod;
            var isCtor      = iDefinition != null && iDefinition.IsConstructor;

            if (isCtor)
            {
                name = JS.Funcs.CONSTRUCTOR;
            }

            var index = this.GetIndex(definition);

            if (index > 0)
            {
                if (isCtor)
                {
                    name = JS.Vars.D + name + index;
                }
                else
                {
                    name += Helpers.PrefixDollar(index);
                    name  = Helpers.ReplaceFirstDollar(name);
                }
            }

            return(prefix != null ? prefix + name : name);
        }
Example #4
0
 public void AddMember(IMember member, bool isSetter, bool includeInline, OverloadsCollection overloads)
 {
     this.Members[Tuple.Create(member, isSetter, includeInline)] = overloads;
 }
Example #5
0
 public bool TryGetMember(IMember member, bool isSetter, bool includeInline, out OverloadsCollection overloads)
 {
     return(this.Members.TryGetValue(Tuple.Create(member, isSetter, includeInline), out overloads));
 }
Example #6
0
 public bool TryGetNode(AstNode astNode, bool isSetter, out OverloadsCollection overloads)
 {
     return(this.Nodes.TryGetValue(Tuple.Create(astNode, isSetter), out overloads));
 }
Example #7
0
 public void AddNode(AstNode astNode, bool isSetter, OverloadsCollection overloads)
 {
     this.Nodes[Tuple.Create(astNode, isSetter)] = overloads;
 }
Example #8
0
        public static string ToJsName(IType type, IEmitter emitter, bool asDefinition = false, bool excludens = false, bool isAlias = false)
        {
            var itypeDef = type.GetDefinition();

            if (itypeDef != null)
            {
                string globalTarget = BridgeTypes.GetGlobalTarget(itypeDef, null);

                if (globalTarget != null)
                {
                    return(globalTarget);
                }
            }

            if (itypeDef != null && itypeDef.Attributes.Any(a => a.AttributeType.FullName == "Bridge.NonScriptableAttribute"))
            {
                throw new EmitterException(emitter.Translator.EmitNode, "Type " + type.FullName + " is marked as not usable from script");
            }

            if (type.Kind == TypeKind.Array)
            {
                return(JS.Types.ARRAY);
            }

            if (type.Kind == TypeKind.Delegate)
            {
                return(JS.Types.FUNCTION);
            }

            if (type.Kind == TypeKind.Dynamic)
            {
                return(JS.Types.OBJECT);
            }

            if (NullableType.IsNullable(type))
            {
                return(BridgeTypes.ToJsName(NullableType.GetUnderlyingType(type), emitter));
            }

            if (type.Kind == TypeKind.Anonymous)
            {
                var at = type as AnonymousType;
                if (at != null && emitter.AnonymousTypes.ContainsKey(at))
                {
                    return(emitter.AnonymousTypes[at].Name);
                }
                else
                {
                    return("Object");
                }
            }

            BridgeType bridgeType = emitter.BridgeTypes.Get(type, true);

            var name = excludens ? "" : type.Namespace;

            var hasTypeDef = bridgeType != null && bridgeType.TypeDefinition != null;

            if (hasTypeDef)
            {
                var typeDef = bridgeType.TypeDefinition;

                if (typeDef.IsNested && !excludens)
                {
                    name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.GetParentNames(typeDef);
                }

                name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.ConvertName(typeDef.Name);
            }
            else
            {
                if (type.DeclaringType != null && !excludens)
                {
                    name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.GetParentNames(type);

                    if (type.DeclaringType.TypeArguments.Count > 0)
                    {
                        name += Helpers.PrefixDollar(type.TypeArguments.Count);
                    }
                }

                name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.ConvertName(type.Name);
            }

            bool isCustomName = false;

            if (bridgeType != null)
            {
                name = BridgeTypes.AddModule(name, bridgeType, out isCustomName);
            }

            if (!hasTypeDef && !isCustomName && type.TypeArguments.Count > 0)
            {
                name += Helpers.PrefixDollar(type.TypeArguments.Count);
            }

            if (isAlias)
            {
                name = OverloadsCollection.NormalizeInterfaceName(name);
            }

            if (type.TypeArguments.Count > 0 && !Helpers.IsIgnoreGeneric(type, emitter))
            {
                if (isAlias)
                {
                    StringBuilder sb        = new StringBuilder(name);
                    bool          needComma = false;
                    sb.Append(JS.Vars.D);
                    bool isStr = false;
                    foreach (var typeArg in type.TypeArguments)
                    {
                        if (sb.ToString().EndsWith(")"))
                        {
                            sb.Append(" + \"");
                        }

                        if (needComma && !sb.ToString().EndsWith(JS.Vars.D.ToString()))
                        {
                            sb.Append(JS.Vars.D);
                        }

                        needComma = true;
                        bool needGet = typeArg.Kind == TypeKind.TypeParameter && !asDefinition;
                        if (needGet)
                        {
                            if (!isStr)
                            {
                                sb.Insert(0, "\"");
                                isStr = true;
                            }
                            sb.Append("\" + Bridge.getTypeAlias(");
                        }

                        var typeArgName = BridgeTypes.ToJsName(typeArg, emitter, false, false, true);

                        if (!needGet && typeArgName.StartsWith("\""))
                        {
                            sb.Append(typeArgName.Substring(1));

                            if (!isStr)
                            {
                                isStr = true;
                                sb.Insert(0, "\"");
                            }
                        }
                        else
                        {
                            sb.Append(typeArgName);
                        }

                        if (needGet)
                        {
                            sb.Append(")");
                        }
                    }

                    if (isStr && !sb.ToString().EndsWith(")"))
                    {
                        sb.Append("\"");
                    }

                    name = sb.ToString();
                }
                else if (!asDefinition)
                {
                    StringBuilder sb        = new StringBuilder(name);
                    bool          needComma = false;
                    sb.Append("(");
                    foreach (var typeArg in type.TypeArguments)
                    {
                        if (needComma)
                        {
                            sb.Append(",");
                        }

                        needComma = true;

                        sb.Append(BridgeTypes.ToJsName(typeArg, emitter));
                    }
                    sb.Append(")");
                    name = sb.ToString();
                }
            }

            return(name);
        }
Example #9
0
        protected virtual string GetOverloadName(IMember definition, bool skipInterfaceName = false, string prefix = null, bool withoutTypeParams = false)
        {
            IMember interfaceMember = null;

            if (definition.IsExplicitInterfaceImplementation)
            {
                interfaceMember = definition.ImplementedInterfaceMembers.First();
            }
            else if (definition.DeclaringTypeDefinition != null && definition.DeclaringTypeDefinition.Kind == TypeKind.Interface)
            {
                interfaceMember = definition;
            }

            if (interfaceMember != null && !skipInterfaceName)
            {
                return(OverloadsCollection.GetInterfaceMemberName(this.Emitter, interfaceMember, null, prefix, withoutTypeParams, this.IsSetter));
            }

            string name = this.Emitter.GetEntityName(definition, this.CancelChangeCase);

            if (name.StartsWith(".ctor"))
            {
                name = JS.Funcs.CONSTRUCTOR;
            }

            var attr = Helpers.GetInheritedAttribute(definition, "Bridge.NameAttribute");

            if (attr == null && definition is IProperty)
            {
                var prop      = (IProperty)definition;
                var acceessor = this.IsSetter ? prop.Setter : prop.Getter;

                if (acceessor != null)
                {
                    attr = Helpers.GetInheritedAttribute(acceessor, "Bridge.NameAttribute");
                }
            }

            if (attr != null)
            {
                var value = attr.PositionalArguments.First().ConstantValue;
                if (value is string)
                {
                    name = value.ToString();
                }

                prefix = null;
            }

            if (attr != null && definition.ImplementedInterfaceMembers.Count > 0)
            {
                if (this.Members.Where(member => member.ImplementedInterfaceMembers.Count > 0)
                    .Any(member => definition.ImplementedInterfaceMembers.Any(implementedInterfaceMember => member.ImplementedInterfaceMembers.Any(m => m.DeclaringTypeDefinition == implementedInterfaceMember.DeclaringTypeDefinition))))
                {
                    attr = null;
                }
            }

            if (attr != null || (definition.DeclaringTypeDefinition != null && definition.DeclaringTypeDefinition.Kind != TypeKind.Interface && this.Emitter.Validator.IsIgnoreType(definition.DeclaringTypeDefinition)))
            {
                return(prefix != null ? prefix + name : name);
            }

            var isCtor = definition is IMethod && ((IMethod)definition).IsConstructor;

            if (isCtor)
            {
                name = JS.Funcs.CONSTRUCTOR;
            }

            var index = this.GetIndex(definition);

            if (index > 0)
            {
                if (isCtor)
                {
                    name = JS.Vars.D + name + index;
                }
                else
                {
                    name += Helpers.PrefixDollar(index);
                    name  = Helpers.ReplaceFirstDollar(name);
                }
            }

            return(prefix != null ? prefix + name : name);
        }
Example #10
0
        public static string ConvertTokens(IEmitter emitter, string template, IMember member)
        {
            string name = OverloadsCollection.Create(emitter, member).GetOverloadName(true);

            return(template.Replace("{@}", name).Replace("{$}", "{this}." + name));
        }