Browse Source

Extract the enum flag check code into its own pass.

pull/1/head
triton 13 years ago
parent
commit
1813c06ca4
  1. 54
      src/Generator/Passes/CheckFlagEnumsPass.cs
  2. 32
      src/Generator/Transforms/Transform.cs

54
src/Generator/Passes/CheckFlagEnumsPass.cs

@ -0,0 +1,54 @@ @@ -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);
}
}
}

32
src/Generator/Transforms/Transform.cs

@ -116,8 +116,6 @@ namespace Cxxi.Passes @@ -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 @@ -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;
}
}
}
}
Loading…
Cancel
Save