Пример #1
0
        public override void VisitIndexerDeclaration(IndexerDeclaration indexerDeclaration)
        {
            if (indexerDeclaration.HasModifier(Modifiers.Abstract))
            {
                return;
            }

            IDictionary <string, List <EntityDeclaration> > dict = CurrentType.InstanceProperties;

            var key = indexerDeclaration.Name;

            if (dict.ContainsKey(key))
            {
                dict[key].Add(indexerDeclaration);
            }
            else
            {
                dict.Add(key, new List <EntityDeclaration>(new[] { indexerDeclaration }));
            }

            var rr = this.Resolver.ResolveNode(indexerDeclaration, null) as MemberResolveResult;

            if (OverloadsCollection.NeedCreateAlias(rr))
            {
                var config = rr.Member.IsStatic
                ? CurrentType.StaticConfig
                : CurrentType.InstanceConfig;
                config.Alias.Add(new TypeConfigItem {
                    Entity = indexerDeclaration
                });
            }
        }
Пример #2
0
        public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
        {
            if (propertyDeclaration.HasModifier(Modifiers.Abstract))
            {
                return;
            }

            bool isStatic = propertyDeclaration.HasModifier(Modifiers.Static);

            IDictionary <string, List <EntityDeclaration> > dict = isStatic
                ? CurrentType.StaticProperties
                : CurrentType.InstanceProperties;

            var key = propertyDeclaration.Name;

            if (dict.ContainsKey(key))
            {
                dict[key].Add(propertyDeclaration);
            }
            else
            {
                dict.Add(key, new List <EntityDeclaration>(new[] { propertyDeclaration }));
            }

            var rr = this.Resolver.ResolveNode(propertyDeclaration, null) as MemberResolveResult;

            if (OverloadsCollection.NeedCreateAlias(rr))
            {
                var config = rr.Member.IsStatic
                ? CurrentType.StaticConfig
                : CurrentType.InstanceConfig;
                config.Alias.Add(new TypeConfigItem {
                    Entity = propertyDeclaration
                });
            }

            if (!this.HasExternal(propertyDeclaration) &&
                !this.HasTemplate(propertyDeclaration.Getter))
            {
                Expression     initializer     = this.GetDefaultFieldInitializer(propertyDeclaration.ReturnType);
                TypeConfigInfo info            = isStatic ? this.CurrentType.StaticConfig : this.CurrentType.InstanceConfig;
                var            autoInitializer = info.AutoPropertyInitializers.FirstOrDefault(f => f.Name == key);

                if (autoInitializer != null)
                {
                    initializer = autoInitializer.Initializer;
                }

                info.Properties.Add(new TypeConfigItem
                {
                    Name                  = key,
                    Entity                = propertyDeclaration,
                    Initializer           = initializer,
                    IsPropertyInitializer = autoInitializer != null
                });
            }
        }
Пример #3
0
        public override void VisitEventDeclaration(EventDeclaration eventDeclaration)
        {
            bool isStatic = eventDeclaration.HasModifier(Modifiers.Static);

            foreach (var item in eventDeclaration.Variables)
            {
                Expression initializer = item.Initializer;
                this.CurrentType.EventsDeclarations.Add(item.Name, eventDeclaration);
                if (isStatic)
                {
                    this.CurrentType.StaticConfig.Events.Add(new TypeConfigItem
                    {
                        Name           = item.Name,
                        Entity         = eventDeclaration,
                        Initializer    = initializer,
                        VarInitializer = item
                    });
                }
                else
                {
                    this.CurrentType.InstanceConfig.Events.Add(new TypeConfigItem
                    {
                        Name           = item.Name,
                        Entity         = eventDeclaration,
                        Initializer    = initializer,
                        VarInitializer = item
                    });
                }

                var rr = this.Resolver.ResolveNode(item, null) as MemberResolveResult;
                if (OverloadsCollection.NeedCreateAlias(rr))
                {
                    var config = rr.Member.IsStatic
                    ? CurrentType.StaticConfig
                    : CurrentType.InstanceConfig;
                    config.Alias.Add(new TypeConfigItem {
                        Entity = eventDeclaration, VarInitializer = item
                    });
                }
            }
        }
Пример #4
0
        public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
        {
            if (methodDeclaration.HasModifier(Modifiers.Abstract) || this.HasInline(methodDeclaration))
            {
                return;
            }

            this.FixMethodParameters(methodDeclaration.Parameters, methodDeclaration.Body);

            bool isStatic = methodDeclaration.HasModifier(Modifiers.Static);

            Dictionary <string, List <MethodDeclaration> > dict = isStatic
                ? CurrentType.StaticMethods
                : CurrentType.InstanceMethods;

            var key = methodDeclaration.Name;

            if (dict.ContainsKey(key))
            {
                dict[key].Add(methodDeclaration);
            }
            else
            {
                dict.Add(key, new List <MethodDeclaration>(new[] { methodDeclaration }));
            }

            var memberrr = Resolver.ResolveNode(methodDeclaration, null) as MemberResolveResult;

            if (OverloadsCollection.NeedCreateAlias(memberrr))
            {
                var config = isStatic
                ? CurrentType.StaticConfig
                : CurrentType.InstanceConfig;
                config.Alias.Add(new TypeConfigItem {
                    Entity = methodDeclaration
                });
            }
        }
Пример #5
0
        public override void VisitCustomEventDeclaration(CustomEventDeclaration customEventDeclaration)
        {
            if (customEventDeclaration.HasModifier(Modifiers.Abstract))
            {
                return;
            }

            bool isStatic = customEventDeclaration.HasModifier(Modifiers.Static);

            IDictionary <string, List <EntityDeclaration> > dict = isStatic
                ? CurrentType.StaticProperties
                : CurrentType.InstanceProperties;

            var key = customEventDeclaration.Name;

            if (dict.ContainsKey(key))
            {
                dict[key].Add(customEventDeclaration);
            }
            else
            {
                dict.Add(key, new List <EntityDeclaration>(new[] { customEventDeclaration }));
            }

            var rr = Resolver.ResolveNode(customEventDeclaration) as MemberResolveResult;

            if (OverloadsCollection.NeedCreateAlias(rr))
            {
                var config = rr.Member.IsStatic
                ? CurrentType.StaticConfig
                : CurrentType.InstanceConfig;
                config.Alias.Add(new TypeConfigItem {
                    Entity = customEventDeclaration
                });
            }
        }
Пример #6
0
        public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
        {
            if (propertyDeclaration.HasModifier(Modifiers.Abstract))
            {
                return;
            }

            bool isStatic = propertyDeclaration.HasModifier(Modifiers.Static);

            IDictionary <string, List <EntityDeclaration> > dict = isStatic
                ? CurrentType.StaticProperties
                : CurrentType.InstanceProperties;

            var key = propertyDeclaration.Name;

            if (dict.ContainsKey(key))
            {
                dict[key].Add(propertyDeclaration);
            }
            else
            {
                dict.Add(key, new List <EntityDeclaration>(new[] { propertyDeclaration }));
            }

            var rr = this.Resolver.ResolveNode(propertyDeclaration, null) as MemberResolveResult;

            if (OverloadsCollection.NeedCreateAlias(rr))
            {
                var config = rr.Member.IsStatic
                ? CurrentType.StaticConfig
                : CurrentType.InstanceConfig;
                config.Alias.Add(new TypeConfigItem {
                    Entity = propertyDeclaration
                });
            }

            var isResolvedProperty = false;
            MemberResolveResult resolvedProperty = null;

            CheckFieldProperty(propertyDeclaration, ref isResolvedProperty, ref resolvedProperty);

            if (!propertyDeclaration.Getter.IsNull &&
                !this.HasIgnore(propertyDeclaration) &&
                !this.HasInline(propertyDeclaration.Getter) &&
                propertyDeclaration.Getter.Body.IsNull &&
                !this.HasScript(propertyDeclaration.Getter))
            {
                Expression     initializer = this.GetDefaultFieldInitializer(propertyDeclaration.ReturnType);
                TypeConfigInfo info        = isStatic ? this.CurrentType.StaticConfig : this.CurrentType.InstanceConfig;

                if (!isResolvedProperty)
                {
                    resolvedProperty   = Resolver.ResolveNode(propertyDeclaration, null) as MemberResolveResult;
                    isResolvedProperty = true;
                }

                bool autoPropertyToField = false;
                if (resolvedProperty != null && resolvedProperty.Member != null)
                {
                    autoPropertyToField = Helpers.IsFieldProperty(resolvedProperty.Member, AssemblyInfo);
                }
                else
                {
                    autoPropertyToField = AssemblyInfo.AutoPropertyToField;
                }

                var autoInitializer = info.AutoPropertyInitializers.FirstOrDefault(f => f.Name == key);

                if (autoInitializer != null)
                {
                    initializer = autoInitializer.Initializer;
                }

                if (!autoPropertyToField)
                {
                    if (resolvedProperty != null && resolvedProperty.Member.ImplementedInterfaceMembers.Count > 0 && resolvedProperty.Member.ImplementedInterfaceMembers.Any(m => Helpers.IsFieldProperty(m, this.AssemblyInfo)))
                    {
                        throw new EmitterException(propertyDeclaration, string.Format("The property {0} is not marked as FieldProperty but implemented interface member has such attribute", resolvedProperty.Member.ToString()));
                    }

                    info.Properties.Add(new TypeConfigItem
                    {
                        Name        = key,
                        Entity      = propertyDeclaration,
                        Initializer = initializer
                    });
                }
                else
                {
                    if (resolvedProperty != null && resolvedProperty.Member.ImplementedInterfaceMembers.Count > 0 && !resolvedProperty.Member.ImplementedInterfaceMembers.All(m => Helpers.IsFieldProperty(m, this.AssemblyInfo)))
                    {
                        throw new EmitterException(propertyDeclaration, string.Format("The property {0} is marked as FieldProperty but implemented interface member has no such attribute", resolvedProperty.Member.ToString()));
                    }

                    info.Fields.Add(new TypeConfigItem
                    {
                        Name        = key,
                        Entity      = propertyDeclaration,
                        Initializer = initializer
                    });
                }
            }
        }
Пример #7
0
        public override void VisitFieldDeclaration(FieldDeclaration fieldDeclaration)
        {
            bool isStatic = this.CurrentType.ClassType == ClassType.Enum ||
                            fieldDeclaration.HasModifier(Modifiers.Static) ||
                            fieldDeclaration.HasModifier(Modifiers.Const);

            foreach (var item in fieldDeclaration.Variables)
            {
                var rr = this.Resolver.ResolveNode(item, null) as MemberResolveResult;
                if (fieldDeclaration.HasModifier(Modifiers.Const) && rr != null && rr.Member.Attributes.Any(a => a.AttributeType.FullName == Bridge.Translator.Translator.Bridge_ASSEMBLY + ".InlineConstAttribute"))
                {
                    continue;
                }

                Expression initializer = item.Initializer;

                if (initializer.IsNull)
                {
                    if (this.CurrentType.ClassType == ClassType.Enum)
                    {
                        throw (EmitterException)this.CreateException(fieldDeclaration, "Enum items must be explicitly numbered");
                    }

                    initializer = this.GetDefaultFieldInitializer(fieldDeclaration.ReturnType);
                }

                this.CurrentType.FieldsDeclarations.Add(item.Name, fieldDeclaration);

                string prefix          = SharpSixRewriter.AutoInitFieldPrefix;
                bool   autoInitializer = item.Name.StartsWith(prefix);
                string name            = autoInitializer ? item.Name.Substring(prefix.Length) : item.Name;

                if (isStatic)
                {
                    var collection = this.CurrentType.StaticConfig.Fields;
                    if (autoInitializer)
                    {
                        collection = this.CurrentType.StaticConfig.AutoPropertyInitializers;
                        var prop = this.CurrentType.StaticConfig.Properties.FirstOrDefault(p => p.Name == name);

                        if (prop == null)
                        {
                            prop = this.CurrentType.StaticConfig.Fields.FirstOrDefault(p => p.Name == name);
                        }

                        if (prop != null)
                        {
                            prop.Initializer = initializer;
                        }
                    }

                    collection.Add(new TypeConfigItem
                    {
                        Name           = name,
                        Entity         = fieldDeclaration,
                        IsConst        = fieldDeclaration.HasModifier(Modifiers.Const),
                        VarInitializer = item,
                        Initializer    = initializer
                    });
                }
                else
                {
                    var collection = this.CurrentType.InstanceConfig.Fields;
                    if (autoInitializer)
                    {
                        collection = this.CurrentType.InstanceConfig.AutoPropertyInitializers;
                        var prop = this.CurrentType.InstanceConfig.Properties.FirstOrDefault(p => p.Name == name);

                        if (prop == null)
                        {
                            prop = this.CurrentType.InstanceConfig.Fields.FirstOrDefault(p => p.Name == name);
                        }

                        if (prop != null)
                        {
                            prop.Initializer = initializer;
                        }
                    }

                    collection.Add(new TypeConfigItem
                    {
                        Name           = name,
                        Entity         = fieldDeclaration,
                        VarInitializer = item,
                        Initializer    = initializer
                    });
                }

                if (OverloadsCollection.NeedCreateAlias(rr))
                {
                    var config = isStatic
                    ? CurrentType.StaticConfig
                    : CurrentType.InstanceConfig;
                    config.Alias.Add(new TypeConfigItem {
                        Entity = fieldDeclaration, VarInitializer = item
                    });
                }
            }
        }
Пример #8
0
        public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
        {
            if (propertyDeclaration.HasModifier(Modifiers.Abstract))
            {
                return;
            }

            bool isStatic = propertyDeclaration.HasModifier(Modifiers.Static);

            IDictionary <string, List <EntityDeclaration> > dict = isStatic
                ? CurrentType.StaticProperties
                : CurrentType.InstanceProperties;

            var key = propertyDeclaration.Name;

            if (dict.ContainsKey(key))
            {
                dict[key].Add(propertyDeclaration);
            }
            else
            {
                dict.Add(key, new List <EntityDeclaration>(new[] { propertyDeclaration }));
            }

            var rr = this.Resolver.ResolveNode(propertyDeclaration, null) as MemberResolveResult;

            if (OverloadsCollection.NeedCreateAlias(rr))
            {
                var config = rr.Member.IsStatic
                ? CurrentType.StaticConfig
                : CurrentType.InstanceConfig;
                config.Alias.Add(new TypeConfigItem {
                    Entity = propertyDeclaration
                });
            }

            if (!this.HasExternal(propertyDeclaration) &&
                !this.HasTemplate(propertyDeclaration.Getter))
            {
                Expression     initializer = this.GetDefaultFieldInitializer(propertyDeclaration.ReturnType);
                TypeConfigInfo info        = isStatic ? this.CurrentType.StaticConfig : this.CurrentType.InstanceConfig;

                bool autoPropertyToField = false;
                if (rr != null && rr.Member != null && Helpers.IsAutoProperty((IProperty)rr.Member))
                {
                    var rules = Rules.Get(this.Emitter, rr.Member);

                    if (rules.AutoProperty.HasValue)
                    {
                        autoPropertyToField = rules.AutoProperty.Value == AutoPropertyRule.Plain;
                    }
                    else
                    {
                        autoPropertyToField = this.HasFieldAttribute(rr.Member);

                        if (!autoPropertyToField && rr.Member.ImplementedInterfaceMembers.Count > 0)
                        {
                            foreach (var interfaceMember in rr.Member.ImplementedInterfaceMembers)
                            {
                                autoPropertyToField = this.HasFieldAttribute(interfaceMember, false);

                                if (autoPropertyToField)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }

                var autoInitializer = info.AutoPropertyInitializers.FirstOrDefault(f => f.Name == key);

                if (autoInitializer != null)
                {
                    initializer = autoInitializer.Initializer;
                }

                if (!autoPropertyToField)
                {
                    info.Properties.Add(new TypeConfigItem
                    {
                        Name                  = key,
                        Entity                = propertyDeclaration,
                        Initializer           = initializer,
                        IsPropertyInitializer = autoInitializer != null
                    });
                }
                else
                {
                    info.Fields.Add(new TypeConfigItem
                    {
                        Name        = key,
                        Entity      = propertyDeclaration,
                        Initializer = initializer
                    });
                }
            }
        }