diff --git a/src/Generator/Passes/CheckFlagEnumsPass.cs b/src/Generator/Passes/CheckFlagEnumsPass.cs new file mode 100644 index 00000000..f77a82b3 --- /dev/null +++ b/src/Generator/Passes/CheckFlagEnumsPass.cs @@ -0,0 +1,54 @@ +using System; + +namespace Cxxi.Passes +{ + class CheckFlagEnumsPass : TranslationUnitPass + { + static bool IsFlagEnum(Enumeration @enum) + { + // If the enumeration only has power of two values, assume it's + // a flags enum. + + bool isFlags = true; + bool hasBigRange = false; + + foreach (var item in @enum.Items) + { + if (item.Name.Length >= 1 && Char.IsDigit(item.Name[0])) + item.Name = String.Format("_{0}", item.Name); + + long value = item.Value; + if (value >= 4) + hasBigRange = true; + if (value <= 1 || value.IsPowerOfTwo()) + continue; + isFlags = false; + } + + // Only apply this heuristic if there are enough values to have a + // reasonable chance that it really is a bitfield. + + return isFlags && hasBigRange; + } + + public override bool ProcessEnum(Enumeration @enum) + { + if (IsFlagEnum(@enum)) + { + @enum.Modifiers |= Enumeration.EnumModifiers.Flags; + return true; + } + + return false; + } + } + + public static class CheckFlagEnumsExtensions + { + public static void CheckFlagEnums(this PassBuilder builder) + { + var pass = new CheckFlagEnumsPass(); + builder.AddPass(pass); + } + } +} diff --git a/src/Generator/Transforms/Transform.cs b/src/Generator/Transforms/Transform.cs index 1118f454..b8e3542b 100644 --- a/src/Generator/Transforms/Transform.cs +++ b/src/Generator/Transforms/Transform.cs @@ -116,8 +116,6 @@ namespace Cxxi.Passes pass.ProcessEnumItem(item); } - CheckIsFlagsEnum(@enum); - // If we still do not have a valid name, then try to guess one // based on the enum value names. @@ -138,35 +136,5 @@ namespace Cxxi.Passes @enum.Name = prefix; } } - - private static void CheckIsFlagsEnum(Enumeration @enum) - { - // If the enumeration only has power of two values, assume it's - // a flags enum. - - bool isFlags = true; - bool hasBigRange = false; - - foreach (var item in @enum.Items) - { - if (item.Name.Length >= 1 && Char.IsDigit(item.Name[0])) - item.Name = String.Format("_{0}", item.Name); - - long value = item.Value; - if (value >= 4) - hasBigRange = true; - if (value <= 1 || value.IsPowerOfTwo()) - continue; - isFlags = false; - } - - // Only apply this heuristic if there are enough values to have a - // reasonable chance that it really is a bitfield. - - if (isFlags && hasBigRange) - { - @enum.Modifiers |= Enumeration.EnumModifiers.Flags; - } - } } } \ No newline at end of file