Example #1
0
        public static bool NeedCreateAlias(MemberResolveResult rr)
        {
            if (rr == null || rr.Member.ImplementedInterfaceMembers.Count == 0)
            {
                return false;
            }

            if (rr.Member.ImplementedInterfaceMembers.Count > 0 &&
                rr.Member.ImplementedInterfaceMembers.Any(im => im.DeclaringTypeDefinition.TypeParameters.Any(tp => tp.Variance != VarianceModifier.Invariant)))
            {
                return true;
            }

            if (rr.Member.IsExplicitInterfaceImplementation)
            {
                var explicitInterfaceMember = rr.Member.ImplementedInterfaceMembers.First();
                var typeDef = explicitInterfaceMember.DeclaringTypeDefinition;
                var type = explicitInterfaceMember.DeclaringType;

                return typeDef != null && !Helpers.IsIgnoreGeneric(typeDef) && type != null && type.TypeArguments.Count > 0 && Helpers.IsTypeParameterType(type);
            }

            return true;
        }
Example #2
0
 public static bool IsAssignableFrom(TypeDefinition thisTypeDefinition, TypeDefinition typeDefinition, IEmitter emitter)
 {
     return (thisTypeDefinition == typeDefinition
             || (typeDefinition.IsClass && !typeDefinition.IsValueType && Helpers.IsSubclassOf(typeDefinition, thisTypeDefinition, emitter))
             || (typeDefinition.IsInterface && Helpers.IsImplementationOf(typeDefinition, thisTypeDefinition, emitter)));
 }
Example #3
0
        protected virtual List<IEvent> GetEventOverloads(List<IEvent> list = null, ITypeDefinition typeDef = null)
        {
            typeDef = typeDef ?? this.TypeDefinition;

            bool isTop = list == null;
            list = list ?? new List<IEvent>();

            if (typeDef != null)
            {
                var events = typeDef.Events.Where(e =>
                {
                    if (e.IsExplicitInterfaceImplementation)
                    {
                        return false;
                    }

                    var inline = e.AddAccessor != null ? this.Emitter.GetInline(e.AddAccessor) : null;
                    if (!string.IsNullOrWhiteSpace(inline))
                    {
                        return false;
                    }

                    inline = e.RemoveAccessor != null ? this.Emitter.GetInline(e.RemoveAccessor) : null;
                    if (!string.IsNullOrWhiteSpace(inline))
                    {
                        return false;
                    }

                    bool eq = false;
                    bool? equalsByAdd = null;
                    if (e.IsStatic == this.Static)
                    {
                        var addName = e.AddAccessor != null && e.CanAdd ? Helpers.GetEventRef(e, this.Emitter, false, true, true) : null;
                        var removeName = e.RemoveAccessor != null && e.CanRemove ? Helpers.GetEventRef(e, this.Emitter, true, true, true) : null;
                        var fieldName = Helpers.GetEventRef(e, this.Emitter, true, true, true, false, true);
                        if (addName != null && (addName == this.JsName || addName == this.AltJsName || addName == this.FieldJsName))
                        {
                            eq = true;
                            equalsByAdd = true;
                        }
                        else if (removeName != null && (removeName == this.JsName || removeName == this.AltJsName || removeName == this.FieldJsName))
                        {
                            eq = true;
                            equalsByAdd = false;
                        }
                        else if (fieldName != null && (fieldName == this.JsName || fieldName == this.AltJsName || fieldName == this.FieldJsName))
                        {
                            eq = true;
                        }
                    }

                    if (eq)
                    {
                        if (e.IsOverride && !this.IsTemplateOverride(e))
                        {
                            return false;
                        }

                        if (equalsByAdd.HasValue && this.Member is IMethod && this.AltJsName == null)
                        {
                            this.AltJsName = Helpers.GetEventRef(e, this.Emitter, equalsByAdd.Value, true, true);
                        }

                        return true;
                    }

                    return false;
                });

                list.AddRange(events);

                if (this.Inherit)
                {
                    var baseTypeDefinitions = typeDef.DirectBaseTypes.Where(t => t.Kind == typeDef.Kind || (typeDef.Kind == TypeKind.Struct && t.Kind == TypeKind.Class));

                    foreach (var baseTypeDef in baseTypeDefinitions)
                    {
                        list = this.GetEventOverloads(list, baseTypeDef.GetDefinition());
                    }
                }
            }

            var returnEvents = isTop ? list.Distinct().ToList() : list;
            return returnEvents;
        }
Example #4
0
        protected virtual List<IProperty> GetPropertyOverloads(List<IProperty> list = null, ITypeDefinition typeDef = null)
        {
            typeDef = typeDef ?? this.TypeDefinition;

            bool isTop = list == null;
            list = list ?? new List<IProperty>();

            if (this.Member != null && this.Member.IsOverride && !this.IsTemplateOverride(this.Member))
            {
                if (this.OriginalMember == null)
                {
                    this.OriginalMember = this.Member;
                }

                this.Member = InheritanceHelper.GetBaseMember(this.Member);
                typeDef = this.Member.DeclaringTypeDefinition;
            }

            if (typeDef != null)
            {
                bool isMember = this.Member is IMethod;
                var properties = typeDef.Properties.Where(p =>
                {
                    if (p.IsExplicitInterfaceImplementation)
                    {
                        return false;
                    }

                    var canGet = p.CanGet && p.Getter != null;
                    var canSet = p.CanSet && p.Setter != null;

                    if (!this.IncludeInline)
                    {
                        var inline = canGet ? this.Emitter.GetInline(p.Getter) : null;
                        if (!string.IsNullOrWhiteSpace(inline))
                        {
                            return false;
                        }

                        inline = canSet ? this.Emitter.GetInline(p.Setter) : null;
                        if (!string.IsNullOrWhiteSpace(inline))
                        {
                            return false;
                        }

                        if (p.IsIndexer && canGet && p.Getter.Attributes.Any(a => a.AttributeType.FullName == "Bridge.ExternalAttribute"))
                        {
                            return false;
                        }
                    }

                    bool eq = false;
                    bool? equalsByGetter = null;

                    if (p.IsStatic == this.Static)
                    {
                        var fieldName = this.Emitter.GetEntityName(p);

                        if (fieldName != null && (fieldName == this.JsName || fieldName == this.AltJsName || fieldName == this.FieldJsName))
                        {
                            eq = true;
                        }

                        if (!eq && p.IsIndexer)
                        {
                            var getterIgnore = canGet && this.Emitter.Validator.IsExternalType(p.Getter);
                            var setterIgnore = canSet && this.Emitter.Validator.IsExternalType(p.Setter);
                            var getterName = canGet ? Helpers.GetPropertyRef(p, this.Emitter, false, true, true) : null;
                            var setterName = canSet ? Helpers.GetPropertyRef(p, this.Emitter, true, true, true) : null;

                            if (!getterIgnore && getterName != null && (getterName == this.JsName || getterName == this.AltJsName || getterName == this.FieldJsName))
                            {
                                eq = true;
                                equalsByGetter = true;
                            }
                            else if (!setterIgnore && setterName != null && (setterName == this.JsName || setterName == this.AltJsName || setterName == this.FieldJsName))
                            {
                                eq = true;
                                equalsByGetter = false;
                            }
                        }
                    }

                    if (eq)
                    {
                        if (p.IsOverride && !this.IsTemplateOverride(p))
                        {
                            return false;
                        }

                        if (equalsByGetter.HasValue && isMember && this.AltJsName == null)
                        {
                            this.AltJsName = Helpers.GetPropertyRef(p, this.Emitter, equalsByGetter.Value, true, true);
                        }

                        return true;
                    }

                    return false;
                });

                list.AddRange(properties);

                if (this.Inherit)
                {
                    var baseTypeDefinitions = typeDef.DirectBaseTypes.Where(t => t.Kind == typeDef.Kind || (typeDef.Kind == TypeKind.Struct && t.Kind == TypeKind.Class));

                    foreach (var baseTypeDef in baseTypeDefinitions)
                    {
                        list = this.GetPropertyOverloads(list, baseTypeDef.GetDefinition());
                    }
                }
            }

            var returnProperties = isTop ? list.Distinct().ToList() : list;
            return returnProperties;
        }
Example #5
0
        protected virtual string GetOverloadName(IMember definition, bool skipInterfaceName = false, string prefix = null, bool withoutTypeParams = false, bool isObjectLiteral = false, bool excludeTypeOnly = 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, excludeTypeOnly);
            }

            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.BRIDGE;
                }
                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;
        }