Example #1
0
            private static void MergeObjIntoOther(EnumObject src, EnumObject dest, Dictionary <string, List <EnumObject> > memberMap)
            {
                foreach (var m in src.members)
                {
                    if (StringComparer.OrdinalIgnoreCase.Equals(m.name, "None"))
                    {
                        continue;
                    }

                    dest.AddIfNotSet(m.name, m.value);
                    if (!memberMap.TryGetValue(m.name, out var oList))
                    {
                        oList = new List <EnumObject>();
                        oList.Add(dest);
                    }
                    else
                    {
                        if (oList.Contains(src))
                        {
                            oList.Remove(src);
                        }

                        if (!oList.Contains(dest))
                        {
                            oList.Add(dest);
                        }
                    }
                }

                foreach (var u in src.uses)
                {
                    dest.AddUse(u);
                }
            }
Example #2
0
            private bool TryMergingWithExistingObject(EnumObject enumObject)
            {
                if (enumObject.finished)
                {
                    return(false);
                }

                bool merged = false;

                foreach (var member in enumObject.members)
                {
                    if (this.enumMemberNameToEnumObj.TryGetValue(member.name, out var matchedObj))
                    {
                        merged = this.MergeObjIntoOther(enumObject, matchedObj);
                        if (merged)
                        {
                            break;
                        }
                    }
                }

                foreach (var use in enumObject.uses)
                {
                    if (this.enumMemberNameToEnumObj.TryGetValue(use.ToString(), out var matchedObj))
                    {
                        merged = this.MergeObjIntoOther(enumObject, matchedObj);
                        if (merged)
                        {
                            break;
                        }
                    }
                }

                return(merged);
            }
Example #3
0
                public override void VisitEnumDeclaration(EnumDeclarationSyntax node)
                {
                    base.VisitEnumDeclaration(node);

                    EnumObject enumObject = new EnumObject();

                    enumObject.@namespace = this.currentNamespace;
                    enumObject.name       = node.Identifier.ValueText;
                    enumObject.flags      = node.AttributeLists.Any(list => list.Attributes.Any(attr => attr.Name.ToString().StartsWith("Flags")));
                    enumObject.FromCSharp = true;

                    foreach (EnumMemberDeclarationSyntax member in node.Members)
                    {
                        EnumObject.Member m = new EnumObject.Member();
                        m.name = member.Identifier.ValueText;

                        if (member.EqualsValue != null)
                        {
                            m.value = member.EqualsValue.Value.ToString();
                        }

                        enumObject.members.Add(m);
                    }

                    this.enumObjects.Add(enumObject);
                }
Example #4
0
 private void LoadEnumsFromSourcesFiles(string sourcesDir)
 {
     foreach (var file in Directory.GetFiles(sourcesDir, "*.cs").Where(f => !f.EndsWith(".enums.cs") && !f.EndsWith(".constants.cs")))
     {
         foreach (var enumObj in EnumObject.LoadFromFile(file))
         {
             this.LoadObjMembersIntoMap(enumObj);
         }
     }
 }
Example #5
0
        public static EnumObject Normalize(EnumObject enumObject)
        {
            EnumObject fixedObject = Newtonsoft.Json.JsonConvert.DeserializeObject <EnumObject>(Newtonsoft.Json.JsonConvert.SerializeObject(enumObject));

            if (fixedObject.name != null && fixedObject.name.Contains('.'))
            {
                fixedObject.name = fixedObject.name.Replace('.', '_');
            }

            var firstUse = fixedObject.uses.FirstOrDefault();

            if (firstUse == null)
            {
                return(fixedObject);
            }

            if (fixedObject.name == null || !fixedObject.name.Contains('_'))
            {
                List <string> namesToMatch = new List <string>();
                const int     MaxToCheck   = 4;
                for (int i = 0; i < fixedObject.uses.Count; i++)
                {
                    var use = fixedObject.uses[i];
                    if (use.method != null)
                    {
                        namesToMatch.Add(use.method);
                        if (namesToMatch.Count == MaxToCheck)
                        {
                            break;
                        }
                    }
                }

                string varName     = firstUse.method != null ? firstUse.parameter : firstUse.field;
                string matchedName = GetCommonCamelCaseName(namesToMatch.ToArray());
                if (string.IsNullOrEmpty(matchedName))
                {
                    matchedName = firstUse.method != null ? firstUse.method : firstUse.@struct;
                }

                fixedObject.name = $"{matchedName}_{varName}";
            }

            foreach (var member in fixedObject.members)
            {
                if (int.TryParse(member.name, out var value))
                {
                    string name = firstUse.parameter != null ? firstUse.parameter : firstUse.field;
                    member.value = member.name;
                    member.name  = $"{name}{value}";
                }
            }

            return(fixedObject);
        }
Example #6
0
        public bool AddEnum(EnumObject enumObject)
        {
            if (enumObject.name == null || !enumObject.members.Any(m => m.value != null))
            {
                // Skip if we have no name or values to write
                return(false);
            }

            this.EnsureStarted();
            if (enumObject.flags)
            {
                this.writer.WriteLine(
                    $"    [Flags]");
            }

            string type          = enumObject.type ?? "uint";
            bool   forceUnsigned = enumObject.flags || (enumObject.type != null && (enumObject.type == "uint" || enumObject.type == "ulong" || enumObject.type == "ushort"));

            StringWriter enumBodyWriter = new StringWriter();

            foreach (var member in enumObject.members.Where(m => m.value != null))
            {
                var    currentType = type;
                string valueText   = ConstantWriter.FixIntValueText(forceUnsigned, ref currentType, member.value);

                enumBodyWriter.Write(
                    $@"        {member.name} = {valueText},
");
                // Make the type more specific if it's signed
                if (currentType != type && (type != "int" && type != "long"))
                {
                    type = currentType;
                }
            }

            if (enumObject.type != null)
            {
                type = enumObject.type;
            }

            string typePart = type != "int" ? $" : {type}" : string.Empty;

            this.writer.WriteLine(
                $@"    public enum {enumObject.name}{typePart}
    {{");

            this.writer.Write(enumBodyWriter.ToString());

            this.writer.WriteLine(
                $@"    }}
");

            return(true);
        }
Example #7
0
            private static List <EnumObject> LoadEnumsFromSourceFiles(string sourcesDir)
            {
                List <EnumObject> enumObjects = new List <EnumObject>();

                foreach (var file in Directory.GetFiles(sourcesDir, "*.cs"))
                {
                    enumObjects.AddRange(EnumObject.LoadFromFile(file));
                }

                return(enumObjects);
            }
Example #8
0
            private List <EnumObject> LoadEnumsFromJsonFiles(string[] enumJsonFiles)
            {
                List <EnumObject> enumObjects = new List <EnumObject>();

                if (enumJsonFiles != null)
                {
                    foreach (var enumJsonFile in enumJsonFiles)
                    {
                        enumObjects.AddRange(EnumObject.LoadFromFile(enumJsonFile));
                    }
                }

                return(enumObjects);
            }
Example #9
0
            private static List <EnumObject> LoadEnumsFromJsonFiles(string[] enumJsonFiles, Dictionary <string, string> renames)
            {
                List <EnumObject> enumObjects = new List <EnumObject>();

                if (enumJsonFiles != null)
                {
                    foreach (var enumJsonFile in enumJsonFiles)
                    {
                        enumObjects.AddRange(EnumObjectUtils.NormalizeEnumObjects(EnumObject.LoadFromFile(enumJsonFile), renames));
                    }
                }

                return(enumObjects);
            }
Example #10
0
            private bool MergeObjIntoOther(EnumObject src, EnumObject dest)
            {
                // If the dest is from C# code, make src point to dest. In effect,
                // it will give its uses to the C# version
                if (dest.FromCSharp)
                {
                    src.name = null;
                    src.members.Clear();
                    src.addUsesTo = dest.name;
                    return(true);
                }

                // Don't mess with an object that was declared to be finished in the json
                if (src.finished)
                {
                    return(false);
                }

                foreach (var m in src.members)
                {
                    if (StringComparer.OrdinalIgnoreCase.Equals(m.name, "None"))
                    {
                        continue;
                    }

                    dest.AddIfNotSet(m.name, m.value);
                    if (this.enumMemberNameToEnumObj.TryGetValue(m.name, out var memberOwner))
                    {
                        if (memberOwner != dest && memberOwner != src)
                        {
                            this.enumMemberNameToEnumObj[m.name] = dest;
                            this.MergeObjIntoOther(memberOwner, dest);
                        }
                    }

                    this.enumMemberNameToEnumObj[m.name] = dest;
                }

                foreach (var u in src.uses)
                {
                    dest.AddUse(u);
                }

                return(true);
            }
Example #11
0
            private void AddEnumWarningAndSuggestedMapping(string message, EnumObject enumObject)
            {
                this.output.Add(message);
                StringBuilder suggestedName = new StringBuilder();

                foreach (var match in NamePartsRegex.Matches(enumObject.name))
                {
                    if (suggestedName.Length != 0)
                    {
                        suggestedName.Append('_');
                    }

                    suggestedName.Append(match.ToString().ToUpperInvariant());
                }

                if (suggestedName.Length != 0)
                {
                    this.suggestedEnumRenames.Add($"{enumObject.name}={suggestedName}");
                }
            }
Example #12
0
            private void LoadObjMembersIntoMap(EnumObject obj)
            {
                foreach (var member in obj.members)
                {
                    if (StringComparer.OrdinalIgnoreCase.Equals(member.name, "None"))
                    {
                        continue;
                    }

                    if (!this.enumMemberNameToEnumObj.ContainsKey(member.name))
                    {
                        this.enumMemberNameToEnumObj[member.name] = obj;
                    }
                }

                foreach (var use in obj.uses)
                {
                    if (!this.enumMemberNameToEnumObj.ContainsKey(use.ToString()))
                    {
                        this.enumMemberNameToEnumObj[use.ToString()] = obj;
                    }
                }
            }
Example #13
0
            private List <EnumObject> LoadEnumsFromJsonFiles(string[] enumJsonFiles, Dictionary <string, string> renames)
            {
                List <EnumObject> enumObjects = new List <EnumObject>();

                if (enumJsonFiles != null)
                {
                    var namesToTypes = GetFullNamesToTypes($@"{this.repoRoot}\generation\emitter");

                    foreach (var enumJsonFile in enumJsonFiles)
                    {
                        enumObjects.AddRange(EnumObjectUtils.NormalizeEnumObjects(namesToTypes, EnumObject.LoadFromFile(enumJsonFile), renames));
                    }
                }

                return(enumObjects);
            }