|
1 | 1 | using CppSharp.AST; |
| 2 | +using CppSharp.Extensions; |
2 | 3 |
|
3 | 4 | namespace CppSharp.Passes |
4 | 5 | { |
5 | 6 | /// <summary> |
6 | | - /// Checks for enumerations that should be treated as a collection |
7 | | - /// of flags (and annotated with the .NET [Flags] when generated). |
| 7 | + /// Validates enumerations and checks if any should be treated as a collection |
| 8 | + /// of flags (and annotate them with the .NET [Flags] when generated). |
8 | 9 | /// </summary> |
9 | 10 | public class CheckEnumsPass : TranslationUnitPass |
10 | 11 | { |
@@ -38,15 +39,39 @@ private static bool IsFlagEnum(Enumeration @enum) |
38 | 39 | return isFlags && hasBigRange; |
39 | 40 | } |
40 | 41 |
|
| 42 | + private bool IsValidEnumBaseType(Enumeration @enum) |
| 43 | + { |
| 44 | + if (Options.IsCSharpGenerator) |
| 45 | + return @enum.BuiltinType.Type.IsIntegerType(); |
| 46 | + |
| 47 | + return @enum.BuiltinType.Type.IsIntegerType() || @enum.BuiltinType.Type == PrimitiveType.Bool; |
| 48 | + } |
| 49 | + |
| 50 | + |
41 | 51 | public override bool VisitEnumDecl(Enumeration @enum) |
42 | 52 | { |
43 | | - if (IsFlagEnum(@enum)) |
| 53 | + if (!base.VisitEnumDecl(@enum)) |
| 54 | + return false; |
| 55 | + |
| 56 | + if (!IsValidEnumBaseType(@enum)) |
44 | 57 | { |
45 | | - @enum.Modifiers |= Enumeration.EnumModifiers.Flags; |
46 | | - return true; |
| 58 | + if (@enum.BuiltinType.Type == PrimitiveType.Bool) |
| 59 | + { |
| 60 | + @enum.BuiltinType = new BuiltinType(PrimitiveType.UChar); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + Diagnostics.Warning( |
| 65 | + "The enum `{0}` has a base type of `{1}`, which is currently not supported. The base type will be ignored.", |
| 66 | + @enum, @enum.BuiltinType); |
| 67 | + @enum.BuiltinType = new BuiltinType(PrimitiveType.Int); |
| 68 | + } |
47 | 69 | } |
48 | 70 |
|
49 | | - return base.VisitEnumDecl(@enum); |
| 71 | + if (IsFlagEnum(@enum)) |
| 72 | + @enum.Modifiers |= Enumeration.EnumModifiers.Flags; |
| 73 | + |
| 74 | + return true; |
50 | 75 | } |
51 | 76 | } |
52 | 77 | } |
0 commit comments