Esempio n. 1
0
        private static List <NameRule> GetSpecialRules(NameSemantic semantic)
        {
            var rules = new List <NameRule>();

            var nameAttr = Helpers.GetInheritedAttribute(semantic.Entity, "H5.NameAttribute");

            if (nameAttr != null)
            {
                var rule = new NameRule();
                rule.Level = NameRuleLevel.Member;
                var value = nameAttr.PositionalArguments.First().ConstantValue;
                if (value is bool)
                {
                    rule.Notation = (bool)value ? Notation.CamelCase : Notation.None;
                }
                else if (value is string)
                {
                    rule.CustomName = (string)value;
                }
                semantic.IsCustomName = true;
                rules.Add(rule);
            }
            else
            {
                if (semantic.Entity is IMethod method && method.IsConstructor)
                {
                    semantic.IsCustomName = true;
                    rules.Add(NameConvertor.ConstructorRule);
                }
            }

            return(rules);
        }
Esempio n. 2
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, "H5.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, "H5.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.H5;
                }
                else
                {
                    skipSuffix = true;
                }
            }

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

            var isCtor = definition is IMethod iDefinition && 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);
        }
Esempio n. 3
0
        private static List <NameRule> GetAttributeRules(NameSemantic semantic)
        {
            NameRule memberRule = null;

            NameRule[] classRules     = null;
            NameRule[] assemblyRules  = null;
            NameRule[] interfaceRules = null;

            if (semantic.Entity is IMember)
            {
                var attr = Helpers.GetInheritedAttribute(semantic.Entity, NameConvertor.ConventionAttrName);

                if (attr != null)
                {
                    memberRule = NameConvertor.ToRule(attr, NameRuleLevel.Member);
                }

                var typeDef = semantic.Entity.DeclaringTypeDefinition;

                if (typeDef != null)
                {
                    classRules = NameConvertor.GetClassRules(semantic, typeDef);
                }

                interfaceRules = NameConvertor.GetVirtualMemberRules(semantic);
            }
            else if (semantic.Entity is ITypeDefinition)
            {
                classRules = NameConvertor.GetClassRules(semantic, (ITypeDefinition)semantic.Entity);
            }

            var assembly = semantic.Entity.ParentAssembly;

            if (semantic.Emitter.AssemblyNameRuleCache.ContainsKey(assembly))
            {
                assemblyRules = semantic.Emitter.AssemblyNameRuleCache[assembly];
            }
            else
            {
                IAttribute[] assemblyAttrs = assembly.AssemblyAttributes.Where(a => a.AttributeType.FullName == NameConvertor.ConventionAttrName).ToArray();
                assemblyRules = new NameRule[assemblyAttrs.Length];
                for (int i = 0; i < assemblyAttrs.Length; i++)
                {
                    assemblyRules[i] = NameConvertor.ToRule(assemblyAttrs[i], NameRuleLevel.Assembly);
                }

                Array.Sort(assemblyRules, (item1, item2) => - item1.Priority.CompareTo(item2.Priority));

                semantic.Emitter.AssemblyNameRuleCache.Add(assembly, assemblyRules);
            }

            var rules = new List <NameRule>();

            if (memberRule != null)
            {
                rules.Add(memberRule);
            }

            if (classRules != null && classRules.Length > 0)
            {
                rules.AddRange(classRules);
            }

            if (interfaceRules != null && interfaceRules.Length > 0)
            {
                rules.AddRange(interfaceRules);
            }

            if (assemblyRules != null && assemblyRules.Length > 0)
            {
                rules.AddRange(assemblyRules);
            }

            return(rules);
        }
Esempio n. 4
0
        public static CompilerRule Get(IEmitter emitter, IEntity entity)
        {
            CompilerRule memberRule = null;

            CompilerRule[] classRules     = null;
            CompilerRule[] assemblyRules  = null;
            CompilerRule[] interfaceRules = null;

            if (entity is IMember)
            {
                var attr = Helpers.GetInheritedAttribute(entity, attributeName);

                if (attr != null)
                {
                    memberRule = Rules.ToRule(attr, CompilerRuleLevel.Member);
                }

                var typeDef = entity.DeclaringTypeDefinition;

                if (typeDef != null)
                {
                    classRules = Rules.GetClassRules(emitter, typeDef);
                }

                interfaceRules = Rules.GetVirtualMemberRules(emitter, entity);
            }
            else if (entity is ITypeDefinition)
            {
                classRules = Rules.GetClassRules(emitter, (ITypeDefinition)entity);
            }

            var assembly = entity.ParentAssembly;

            if (emitter != null && emitter.AssemblyCompilerRuleCache.ContainsKey(assembly))
            {
                assemblyRules = emitter.AssemblyCompilerRuleCache[assembly];
            }
            else
            {
                IAttribute[] assemblyAttrs = assembly.AssemblyAttributes.Where(a => a.AttributeType.FullName == Rules.attributeName).ToArray();
                assemblyRules = new CompilerRule[assemblyAttrs.Length];
                for (int i = 0; i < assemblyAttrs.Length; i++)
                {
                    assemblyRules[i] = Rules.ToRule(assemblyAttrs[i], CompilerRuleLevel.Assembly);
                }

                if (emitter != null)
                {
                    emitter.AssemblyCompilerRuleCache.Add(assembly, assemblyRules);
                }
            }

            var rules = new List <CompilerRule>();

            if (memberRule != null)
            {
                rules.Add(memberRule);
            }

            if (classRules != null && classRules.Length > 0)
            {
                rules.AddRange(classRules);
            }

            if (interfaceRules != null && interfaceRules.Length > 0)
            {
                rules.AddRange(interfaceRules);
            }

            if (emitter != null)
            {
                rules.Add(emitter.AssemblyInfo.Rules);
            }

            if (assemblyRules != null && assemblyRules.Length > 0)
            {
                rules.AddRange(assemblyRules);
            }

            //Must be last, as MergeRules works backwards
            if (!assembly.AssemblyName.StartsWith("H5"))
            {
                rules.Add(CompilerRule.DefaultIfNotH5());
            }

            return(MergeRules(rules));
        }