diff --git a/src/AST/Enumeration.cs b/src/AST/Enumeration.cs index dc98b888..b76ccb51 100644 --- a/src/AST/Enumeration.cs +++ b/src/AST/Enumeration.cs @@ -22,10 +22,18 @@ namespace CppSharp.AST public class Item : INamedDecl { public string Name { get; set; } - public long Value; + public ulong Value; public string Expression; public string Comment; public bool ExplicitValue = true; + + public bool IsHexadecimal + { + get + { + return Expression.Contains("0x") || Expression.Contains("0X"); + } + } } public Enumeration() @@ -42,6 +50,14 @@ namespace CppSharp.AST return this; } + public string GetItemValueAsString(Item item) + { + var format = item.IsHexadecimal ? "x" : string.Empty; + var value = BuiltinType.IsUnsigned ? item.Value.ToString(format) : + ((long)item.Value).ToString(format); + return item.IsHexadecimal ? "0x" + value : value; + } + public Enumeration SetFlags() { Modifiers |= EnumModifiers.Flags; diff --git a/src/AST/Type.cs b/src/AST/Type.cs index 074bcfe5..13207ffe 100644 --- a/src/AST/Type.cs +++ b/src/AST/Type.cs @@ -534,6 +534,24 @@ namespace CppSharp.AST Type = type; } + public bool IsUnsigned + { + get + { + switch (Type) + { + case PrimitiveType.Bool: + case PrimitiveType.UInt8: + case PrimitiveType.UInt16: + case PrimitiveType.UInt32: + case PrimitiveType.UInt64: + return true; + } + + return false; + } + } + // Primitive type of built-in type. public PrimitiveType Type; diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index 7b30fd30..8279046c 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -608,7 +608,8 @@ namespace CppSharp.Generators.CLI var item = @enum.Items[i]; GenerateInlineSummary(item.Comment); if (item.ExplicitValue) - Write(String.Format("{0} = {1}", SafeIdentifier(item.Name), item.Value)); + Write(String.Format("{0} = {1}", SafeIdentifier(item.Name), + @enum.GetItemValueAsString(item))); else Write(String.Format("{0}", SafeIdentifier(item.Name))); diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index 46ae6de0..5f61d3ae 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -1601,8 +1601,9 @@ namespace CppSharp.Generators.CSharp var item = @enum.Items[i]; GenerateInlineSummary(item.Comment); + var value = @enum.GetItemValueAsString(item); Write(item.ExplicitValue - ? string.Format("{0} = {1}", SafeIdentifier(item.Name), item.Value) + ? string.Format("{0} = {1}", SafeIdentifier(item.Name), value) : string.Format("{0}", SafeIdentifier(item.Name))); if (i < @enum.Items.Count - 1) diff --git a/src/Generator/Library.cs b/src/Generator/Library.cs index e53c86e9..86e6a72b 100644 --- a/src/Generator/Library.cs +++ b/src/Generator/Library.cs @@ -114,12 +114,12 @@ namespace CppSharp return long.TryParse(num, out val); } - static long ParseMacroExpression(string expression) + static ulong ParseMacroExpression(string expression) { // TODO: Handle string expressions long val; - return ParseToNumber(expression, out val) ? val : 0; + return ParseToNumber(expression, out val) ? (ulong)val : 0; } public static Enumeration GenerateEnumFromMacros(this Library library, string name, diff --git a/src/Generator/Utils/Utils.cs b/src/Generator/Utils/Utils.cs index 6c21a470..86694451 100644 --- a/src/Generator/Utils/Utils.cs +++ b/src/Generator/Utils/Utils.cs @@ -9,7 +9,7 @@ namespace CppSharp { public static class IntHelpers { - public static bool IsPowerOfTwo(this long x) + public static bool IsPowerOfTwo(this ulong x) { return (x != 0) && ((x & (x - 1)) == 0); } diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index e3af86fd..b69d64c0 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -1218,9 +1218,10 @@ CppSharp::AST::Enumeration^ Parser::WalkEnum(clang::EnumDecl* ED) auto EnumItem = gcnew CppSharp::AST::Enumeration::Item(); EnumItem->Name = marshalString(ECD->getNameAsString()); - EnumItem->Value = (int) ECD->getInitVal().getLimitedValue(); + auto Value = ECD->getInitVal(); + EnumItem->Value = Value.isSigned() ? Value.getSExtValue() + : Value.getZExtValue(); EnumItem->Comment = marshalString(BriefText); - //EnumItem->ExplicitValue = ECD->getExplicitValue(); std::string Text; if (GetDeclText(ECD->getSourceRange(), Text))