Beispiel #1
0
        private OverloadsCollection(IEmitter emitter, PropertyDeclaration propDeclaration, bool isSetter, bool isField)
        {
            this.Emitter     = emitter;
            this.IsField     = isField;
            this.Name        = propDeclaration.Name;
            this.JsName      = Helpers.GetPropertyRef(propDeclaration, emitter, isSetter, true, true);
            this.AltJsName   = Helpers.GetPropertyRef(propDeclaration, emitter, !isSetter, true, true);
            this.FieldJsName = propDeclaration.Getter != null && propDeclaration.Getter.Body.IsNull ? emitter.GetEntityName(propDeclaration) : null;
            this.Inherit     = !propDeclaration.HasModifier(Modifiers.Static);
            this.Static      = propDeclaration.HasModifier(Modifiers.Static);
            this.IsSetter    = isSetter;
            this.Member      = this.FindMember(propDeclaration);
            var p = (IProperty)this.Member;

            this.CancelChangeCase = this.Member.DeclaringTypeDefinition == null || !this.Emitter.Validator.IsObjectLiteral(this.Member.DeclaringTypeDefinition);
            this.FieldJsName      = this.Emitter.GetEntityName(p, true);
            this.TypeDefinition   = this.Member.DeclaringTypeDefinition;
            this.Type             = this.Member.DeclaringType;
            this.InitMembers();
            this.Emitter.OverloadsCacheNodes[new Tuple <AstNode, bool>(propDeclaration, isSetter)] = this;

            this.SetCaseFromNameAttr();
        }
Beispiel #2
0
        protected virtual List <IProperty> GetPropertyOverloads(List <IProperty> list = null, ITypeDefinition typeDef = null)
        {
            bool isTop = list == null;

            list    = list ?? new List <IProperty>();
            typeDef = typeDef ?? this.TypeDefinition;

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

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

                    bool eq = false;
                    if (p.IsStatic == this.Static)
                    {
                        var getterIgnore = p.Getter != null && this.Emitter.Validator.IsIgnoreType(p.Getter);
                        var setterIgnore = p.Setter != null && this.Emitter.Validator.IsIgnoreType(p.Setter);
                        var getterName   = p.Getter != null ? Helpers.GetPropertyRef(p, this.Emitter, false, true, true) : null;
                        var setterName   = p.Setter != null ? Helpers.GetPropertyRef(p, this.Emitter, true, true, true) : null;
                        if (!getterIgnore && getterName != null && (getterName == this.JsName || getterName == this.AltJsName))
                        {
                            eq = true;
                        }
                        else if (!setterIgnore && setterName != null && (setterName == this.JsName || setterName == this.AltJsName))
                        {
                            eq = true;
                        }
                    }

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

                        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)
                    {
                        var result = this.GetPropertyOverloads(list, baseTypeDef.GetDefinition());
                        list.AddRange(result);
                    }
                }
            }

            return(isTop ? list.Distinct().ToList() : list);
        }
Beispiel #3
0
        public static void EmitComment(IAbstractEmitterBlock block, AstNode node, bool?getter = null, VariableInitializer varInitializer = null)
        {
            if (block.Emitter.AssemblyInfo.GenerateDocumentation == Bridge.Contract.DocumentationMode.None || node.Parent == null)
            {
                return;
            }

            var visitor = new DocumentationCommentVisitor();

            node.AcceptChildren(visitor);

            if (block.Emitter.AssemblyInfo.GenerateDocumentation == Bridge.Contract.DocumentationMode.Basic && visitor.Comments.Count == 0)
            {
                return;
            }

            object value  = null;
            var    rr     = block.Emitter.Resolver.ResolveNode(varInitializer ?? node, block.Emitter);
            string source = BuildCommentString(visitor.Comments);

            if (node is FieldDeclaration)
            {
                var initializer = varInitializer.Initializer as PrimitiveExpression;

                if (initializer != null)
                {
                    value = initializer.Value;
                }
            }
            else if (node is EventDeclaration)
            {
                var eventDecl = (EventDeclaration)node;
                foreach (var evVar in eventDecl.Variables)
                {
                    var ev_rr = block.Emitter.Resolver.ResolveNode(evVar, block.Emitter);
                    var memberResolveResult = ev_rr as MemberResolveResult;
                    var ev = memberResolveResult.Member as IEvent;

                    if (!getter.HasValue || getter.Value)
                    {
                        var comment = new JsDocComment();
                        InitMember(comment, ev.AddAccessor, block.Emitter, null);
                        comment.Function = Helpers.GetEventRef(ev, block.Emitter, false);
                        block.Write(
                            block.WriteIndentToString(XmlToJsDoc.ReadComment(source, ev_rr, block.Emitter, comment)));
                        block.WriteNewLine();
                    }

                    if (!getter.HasValue || !getter.Value)
                    {
                        var comment = new JsDocComment();
                        InitMember(comment, ev.RemoveAccessor, block.Emitter, null);
                        comment.Function = Helpers.GetEventRef(ev, block.Emitter, true);
                        block.Write(
                            block.WriteIndentToString(XmlToJsDoc.ReadComment(source, ev_rr, block.Emitter, comment)));
                        block.WriteNewLine();
                    }
                }

                return;
            }

            var prop = node as PropertyDeclaration;

            if (prop != null)
            {
                var memberResolveResult = rr as MemberResolveResult;
                var rProp = memberResolveResult.Member as IProperty;

                var comment = new JsDocComment();
                InitMember(comment, rProp, block.Emitter, null);
                comment.Function = Helpers.GetPropertyRef(rProp, block.Emitter);
                block.Write(block.WriteIndentToString(XmlToJsDoc.ReadComment(source, rr, block.Emitter, comment)));
                block.WriteNewLine();
                return;
            }

            var indexer = node as IndexerDeclaration;

            if (indexer != null)
            {
                var memberResolveResult = rr as MemberResolveResult;
                var rProp = memberResolveResult.Member as IProperty;

                var comment = new JsDocComment();
                InitMember(comment, getter.HasValue && getter.Value ? rProp.Getter : rProp.Setter, block.Emitter, null);
                block.Write(block.WriteIndentToString(XmlToJsDoc.ReadComment(source, rr, block.Emitter, comment)));
                block.WriteNewLine();
                return;
            }

            block.Write(block.WriteIndentToString(XmlToJsDoc.Convert(source, rr, block.Emitter, value)));

            block.WriteNewLine();
        }
Beispiel #4
0
        public static void EmitComment(IAbstractEmitterBlock block, AstNode node)
        {
            if (block.Emitter.AssemblyInfo.GenerateDocumentation == Bridge.Contract.DocumentationMode.None || node.Parent == null)
            {
                return;
            }

            var visitor = new DocumentationCommentVisitor();

            node.AcceptChildren(visitor);

            if (block.Emitter.AssemblyInfo.GenerateDocumentation == Bridge.Contract.DocumentationMode.Basic && visitor.Comments.Count == 0)
            {
                return;
            }

            object value = null;

            if (node is FieldDeclaration)
            {
                var fieldDecl = (FieldDeclaration)node;
                node = fieldDecl.Variables.First();
                var initializer = fieldDecl.Variables.First().Initializer as PrimitiveExpression;

                if (initializer != null)
                {
                    value = initializer.Value;
                }
            }
            else if (node is EventDeclaration)
            {
                var eventDecl = (EventDeclaration)node;
                node = eventDecl.Variables.First();
                var initializer = eventDecl.Variables.First().Initializer as PrimitiveExpression;

                if (initializer != null)
                {
                    value = initializer.Value;
                }
            }

            var    rr     = block.Emitter.Resolver.ResolveNode(node, block.Emitter);
            string source = BuildCommentString(visitor.Comments);

            var prop = node as PropertyDeclaration;

            if (prop != null)
            {
                var memberResolveResult = rr as MemberResolveResult;
                var rProp = memberResolveResult.Member as DefaultResolvedProperty;

                var comment = new JsDocComment();
                InitMember(comment, rProp.Getter, block.Emitter, null);
                comment.Function = Helpers.GetPropertyRef(rProp, block.Emitter, false);
                block.Write(block.WriteIndentToString(XmlToJsDoc.ReadComment(source, rr, block.Emitter, comment)));
                block.WriteNewLine();

                comment = new JsDocComment();
                InitMember(comment, rProp.Setter, block.Emitter, null);
                comment.Function = Helpers.GetPropertyRef(rProp, block.Emitter, true);
                block.Write(block.WriteIndentToString(XmlToJsDoc.ReadComment(source, rr, block.Emitter, comment)));
                block.WriteNewLine();
                return;
            }

            block.Write(block.WriteIndentToString(XmlToJsDoc.Convert(source, rr, block.Emitter, value)));

            block.WriteNewLine();
        }
Beispiel #5
0
        protected virtual List <IProperty> GetPropertyOverloads(List <IProperty> list = null, ITypeDefinition typeDef = null)
        {
            bool isTop = list == null;

            list    = list ?? new List <IProperty>();
            typeDef = typeDef ?? this.TypeDefinition;

            if (typeDef != null)
            {
                var bridgeType  = this.Emitter.BridgeTypes.Get(typeDef);
                var monoTypeDef = bridgeType != null ? bridgeType.TypeDefinition : null;
                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 (monoTypeDef != null)
                    {
                        var monoProp = monoTypeDef.Properties.FirstOrDefault(mp => mp.Name == p.Name);

                        if (monoProp != null)
                        {
                            canGet = monoProp.GetMethod != null;
                            canSet = monoProp.SetMethod != 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 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;
                        var fieldName    = Helpers.IsAutoProperty(p) ? (Helpers.IsFieldProperty(p, this.Emitter) ? this.Emitter.GetEntityName(p) : Helpers.GetPropertyRef(p, this.Emitter, true, true, true, false, 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;
                        }
                        else if (fieldName != null && (fieldName == this.JsName || fieldName == this.AltJsName || fieldName == this.FieldJsName))
                        {
                            eq = true;
                        }
                    }

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

                        if (equalsByGetter.HasValue && this.Member is IMethod && 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)
                    {
                        var result = this.GetPropertyOverloads(list, baseTypeDef.GetDefinition());
                        list.AddRange(result);
                    }
                }
            }

            return(isTop ? list.Distinct().ToList() : list);
        }
Beispiel #6
0
        private OverloadsCollection(IEmitter emitter, IMember member, bool isSetter = false, bool includeInline = false, bool isField = false)
        {
            if (member is IMethod)
            {
                var method = (IMethod)member;
                this.Inherit     = !method.IsConstructor && !method.IsStatic;
                this.Static      = method.IsStatic;
                this.Constructor = method.IsConstructor;
            }
            else if (member is IEntity)
            {
                var entity = (IEntity)member;
                this.Inherit = !entity.IsStatic;
                this.Static  = entity.IsStatic;
            }

            this.Emitter = emitter;
            this.Name    = member.Name;
            this.IsField = isField;

            if (member is IProperty)
            {
                this.CancelChangeCase = !Helpers.IsFieldProperty(member, emitter);
                this.JsName           = Helpers.GetPropertyRef(member, emitter, isSetter, true, true);
                this.AltJsName        = Helpers.GetPropertyRef(member, emitter, !isSetter, true, true);
                var p = (IProperty)member;
                this.FieldJsName = Helpers.IsAutoProperty(p) ? (Helpers.IsFieldProperty(p, this.Emitter) ? this.Emitter.GetEntityName(p) : Helpers.GetPropertyRef(p, this.Emitter, true, true, true, false, true)) : null;
            }
            else if (member is IEvent)
            {
                this.CancelChangeCase = true;
                this.JsName           = Helpers.GetEventRef(member, emitter, isSetter, true, true);
                this.AltJsName        = Helpers.GetEventRef(member, emitter, !isSetter, true, true);
                this.FieldJsName      = Helpers.GetEventRef(member, emitter, true, true, true, false, true);
            }
            else
            {
                this.JsName = this.Emitter.GetEntityName(member, false, true);
            }

            this.IncludeInline  = includeInline;
            this.Member         = member;
            this.TypeDefinition = this.Member.DeclaringTypeDefinition;
            this.Type           = this.Member.DeclaringType;
            this.IsSetter       = isSetter;
            this.InitMembers();
            this.Emitter.OverloadsCacheMembers[new Tuple <IMember, bool, bool>(member, isSetter, includeInline)] = this;
        }
Beispiel #7
0
        private OverloadsCollection(IEmitter emitter, PropertyDeclaration propDeclaration, bool isSetter)
        {
            this.Emitter          = emitter;
            this.Name             = propDeclaration.Name;
            this.JsName           = Helpers.GetPropertyRef(propDeclaration, emitter, isSetter, true, true);
            this.AltJsName        = Helpers.GetPropertyRef(propDeclaration, emitter, !isSetter, true, true);
            this.FieldJsName      = propDeclaration.Getter != null && propDeclaration.Getter.Body.IsNull ? emitter.GetEntityName(propDeclaration) : null;
            this.Inherit          = !propDeclaration.HasModifier(Modifiers.Static);
            this.Static           = propDeclaration.HasModifier(Modifiers.Static);
            this.CancelChangeCase = !Helpers.IsFieldProperty(propDeclaration, emitter);
            this.IsSetter         = isSetter;
            this.Member           = this.FindMember(propDeclaration);
            var p = (IProperty)this.Member;

            this.FieldJsName    = Helpers.IsAutoProperty(p) ? (Helpers.IsFieldProperty(p, this.Emitter) ? this.Emitter.GetEntityName(p) : Helpers.GetPropertyRef(p, this.Emitter, true, true, true, false, true)) : null;
            this.TypeDefinition = this.Member.DeclaringTypeDefinition;
            this.Type           = this.Member.DeclaringType;
            this.InitMembers();
            this.Emitter.OverloadsCacheNodes[new Tuple <AstNode, bool>(propDeclaration, isSetter)] = this;
        }
Beispiel #8
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;
        }
Beispiel #9
0
        protected virtual List <IProperty> GetPropertyOverloads()
        {
            List <IProperty> list = new List <IProperty>();
            var typeDef           = this.TypeDefinition;

            while (typeDef != null)
            {
                var properties = typeDef.Properties.Where(p =>
                {
                    var inline = p.Getter != null ? this.Emitter.GetInline(p.Getter) : null;
                    if (!string.IsNullOrWhiteSpace(inline))
                    {
                        return(false);
                    }

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

                    bool eq = false;
                    if (p.IsStatic == this.Static)
                    {
                        var getterName = p.Getter != null ? Helpers.GetPropertyRef(p, this.Emitter, false, true, true) : null;
                        var setterName = p.Setter != null ? Helpers.GetPropertyRef(p, this.Emitter, true, true, true) : null;
                        if (getterName != null && (getterName == this.JsName || getterName == this.AltJsName))
                        {
                            eq = true;
                        }
                        else if (setterName != null && (setterName == this.JsName || setterName == this.AltJsName))
                        {
                            eq = true;
                        }
                    }

                    if (eq)
                    {
                        if (p.IsOverride)
                        {
                            return(false);
                        }

                        return(true);
                    }

                    return(false);
                });

                list.AddRange(properties);

                if (this.Inherit)
                {
                    var baseTypeDefinition = typeDef.DirectBaseTypes.FirstOrDefault();

                    if (baseTypeDefinition != null && baseTypeDefinition.Kind == typeDef.Kind)
                    {
                        typeDef = baseTypeDefinition.GetDefinition();
                    }
                    else
                    {
                        typeDef = null;
                    }
                }
                else
                {
                    typeDef = null;
                }
            }

            return(list);
        }