From 9de0fe06a99003462df3c77dda93718ab85da487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Kie=C3=9F?= Date: Thu, 10 Sep 2015 14:36:12 +0200 Subject: [PATCH] Changes to create-native-map - Use CompareOptions.OrdinalIgnoreCase instead of CompareOptions.Ordinal | CompareOptions.IgnoreCase (which is no longer supported by newer mono versions) - Make sure enum hex values are lowercase on newer mono versions - Do not create conversion calls for inherited fields - Allow [Map] for internal enums --- create-native-map/man/create-native-map.1 | 3 ++ create-native-map/src/TestMap.cs | 26 +++++++-- create-native-map/src/create-native-map.cs | 27 ++++++---- create-native-map/src/test.c.ref | 86 ++++++++++++++++++++++++++++++ create-native-map/src/test.cs.ref | 64 ++++++++++++++++++++++ create-native-map/src/test.h.ref | 32 +++++++++++ 6 files changed, 224 insertions(+), 14 deletions(-) diff --git a/create-native-map/man/create-native-map.1 b/create-native-map/man/create-native-map.1 index 20092a0..f0edf30 100644 --- a/create-native-map/man/create-native-map.1 +++ b/create-native-map/man/create-native-map.1 @@ -435,6 +435,9 @@ becomes } .fi +For classes, the conversion functions will only copy fields declared in the class itself. Fields declared in parent classes will not be copied. (This is because +.I create-native-map +does not know how the inheritance is implemented in C. Therefore copying fields from parent classes is left to the caller of the conversion functions.) .TP Fields If a field (1) has the diff --git a/create-native-map/src/TestMap.cs b/create-native-map/src/TestMap.cs index fc25795..7d489ff 100644 --- a/create-native-map/src/TestMap.cs +++ b/create-native-map/src/TestMap.cs @@ -16,7 +16,7 @@ struct ForDelegate {int i;} HandleRef h, ForDelegate fd); [Map] - enum TestEnum : long { + public enum TestEnum : long { Foo, Bar, Baz, @@ -24,7 +24,7 @@ enum TestEnum : long { } [Map, Flags] - enum SimpleFlagsEnum { + public enum SimpleFlagsEnum { None = 0, A = 1, B = 2, @@ -33,7 +33,7 @@ enum SimpleFlagsEnum { } [Map, Flags] - enum FlagsEnum { + public enum FlagsEnum { None = 0, A = 1, B = 2, @@ -110,6 +110,23 @@ class NativeMethods { [DllImport ("NativeLib")] private static extern void exclude_native_symbol (); } + + [Map] + enum InternalEnum : long { + Foo, + } + + [Map ("struct parent")] + [StructLayout (LayoutKind.Sequential)] + public class Parent { + public int i; + } + + [Map ("struct child")] + [StructLayout (LayoutKind.Sequential)] + public class Child : Parent { + public int j; + } } // Testing namespace renaming; this should be NSTo within test.h @@ -121,8 +138,7 @@ class Stat { } [Map] - enum Colors { + public enum Colors { Red, Blue, Green } } - diff --git a/create-native-map/src/create-native-map.cs b/create-native-map/src/create-native-map.cs index a237d46..26d8c6e 100644 --- a/create-native-map/src/create-native-map.cs +++ b/create-native-map/src/create-native-map.cs @@ -551,7 +551,7 @@ public int Compare (string s1, string s2) if (s2 == null) return -1; return CultureInfo.InvariantCulture.CompareInfo.Compare (s1, s2, - CompareOptions.Ordinal | CompareOptions.IgnoreCase); + CompareOptions.OrdinalIgnoreCase); } } @@ -742,8 +742,8 @@ static void WriteLiteralValues (StreamWriter sh, Type t, string n) if (!fi.IsLiteral) continue; string e = n + "_" + fi.Name; - sh.WriteLine ("\t{0,-" + max_field_length + "} = 0x{1:x},", - e, fi.GetValue (inst)); + sh.WriteLine ("\t{0,-" + max_field_length + "} = 0x{1},", + e, string.Format ("{0:x}", fi.GetValue (inst)).ToLower ()); sh.WriteLine ("\t#define {0,-" + max_field_length + "} {0}", e); } sh.WriteLine ("};"); @@ -1407,7 +1407,8 @@ private void WriteToManagedClass (Type t, string ns, string fn, string etype) private static FieldInfo[] GetFieldsToCopy (Type t) { FieldInfo[] fields = t.GetFields (BindingFlags.Instance | - BindingFlags.Public | BindingFlags.NonPublic); + BindingFlags.Public | BindingFlags.NonPublic | + BindingFlags.DeclaredOnly); int count = 0; for (int i = 0; i < fields.Length; ++i) if (MapUtils.GetCustomAttribute (fields [i]) == null) @@ -1472,6 +1473,7 @@ private void WriteEnum (Type t, string ns, string fn) { string mtype = Enum.GetUnderlyingType(t).Name; ObsoleteAttribute oa = MapUtils.GetCustomAttribute (t); + string visibility = (t.Attributes & TypeAttributes.Public) != 0 ? "public" : "internal"; string obsolete = ""; if (oa != null) { obsolete = string.Format ("[Obsolete (\"{0}\", {1})]\n\t\t", @@ -1481,12 +1483,12 @@ private void WriteEnum (Type t, string ns, string fn) "\t\t{0}[DllImport (LIB, EntryPoint=\"{1}_From{2}\")]\n" + "\t\tprivate static extern int From{2} ({2} value, out {3} rval);\n" + "\n" + - "\t\t{0}public static bool TryFrom{2} ({2} value, out {3} rval)\n" + + "\t\t{0}{4} static bool TryFrom{2} ({2} value, out {3} rval)\n" + "\t\t{{\n" + "\t\t\treturn From{2} (value, out rval) == 0;\n" + "\t\t}}\n" + "\n" + - "\t\t{0}public static {3} From{2} ({2} value)\n" + + "\t\t{0}{4} static {3} From{2} ({2} value)\n" + "\t\t{{\n" + "\t\t\t{3} rval;\n" + "\t\t\tif (From{2} (value, out rval) == -1)\n" + @@ -1497,19 +1499,19 @@ private void WriteEnum (Type t, string ns, string fn) "\t\t{0}[DllImport (LIB, EntryPoint=\"{1}_To{2}\")]\n" + "\t\tprivate static extern int To{2} ({3} value, out {2} rval);\n" + "\n" + - "\t\t{0}public static bool TryTo{2} ({3} value, out {2} rval)\n" + + "\t\t{0}{4} static bool TryTo{2} ({3} value, out {2} rval)\n" + "\t\t{{\n" + "\t\t\treturn To{2} (value, out rval) == 0;\n" + "\t\t}}\n" + "\n" + - "\t\t{0}public static {2} To{2} ({3} value)\n" + + "\t\t{0}{4} static {2} To{2} ({3} value)\n" + "\t\t{{\n" + "\t\t\t{2} rval;\n" + "\t\t\tif (To{2} (value, out rval) == -1)\n" + "\t\t\t\tThrowArgumentException (value);\n" + "\t\t\treturn rval;\n" + "\t\t}}\n", - obsolete, ns, t.Name, mtype + obsolete, ns, t.Name, mtype, visibility ); } @@ -1571,6 +1573,8 @@ public override void WriteType (Type t, string ns, string fn) { if (!CanMapType (t) || !t.IsEnum) return; + if ((t.Attributes & TypeAttributes.Public) == 0) + return; bool bits = IsFlagsEnum (t); @@ -1745,3 +1749,8 @@ public override void CloseFile (string file_prefix) } // vim: noexpandtab +// Local Variables: +// tab-width: 4 +// c-basic-offset: 4 +// indent-tabs-mode: t +// End: diff --git a/create-native-map/src/test.c.ref b/create-native-map/src/test.c.ref index a494340..412a3fb 100644 --- a/create-native-map/src/test.c.ref +++ b/create-native-map/src/test.c.ref @@ -194,6 +194,36 @@ } G_STMT_END #endif /* def DEBUG */ +#ifdef HAVE_STRUCT_CHILD +int +MakeMap_Test_FromChild (struct MakeMap_Test_Child *from, struct child *to) +{ + _cnm_return_val_if_overflow (int, from->j, -1); + + memset (to, 0, sizeof(*to)); + + to->j = from->j; + + return 0; +} +#endif /* ndef HAVE_STRUCT_CHILD */ + + +#ifdef HAVE_STRUCT_CHILD +int +MakeMap_Test_ToChild (struct child *from, struct MakeMap_Test_Child *to) +{ + _cnm_return_val_if_overflow (int, from->j, -1); + + memset (to, 0, sizeof(*to)); + + to->j = from->j; + + return 0; +} +#endif /* ndef HAVE_STRUCT_CHILD */ + + int MakeMap_Test_FromFlagsEnum (int x, int *r) { *r = 0; @@ -432,6 +462,62 @@ MakeMap_Test_ToFooHolder (struct foo_holder *from, struct MakeMap_Test_FooHolder #endif /* ndef HAVE_STRUCT_FOO_HOLDER */ +int MakeMap_Test_FromInternalEnum (gint64 x, gint64 *r) +{ + *r = 0; + if (x == MakeMap_Test_InternalEnum_Foo) +#ifdef Foo + {*r = Foo; return 0;} +#else /* def Foo */ + {errno = EINVAL; return -1;} +#endif /* ndef Foo */ + if (x == 0) + return 0; + errno = EINVAL; return -1; +} + +int MakeMap_Test_ToInternalEnum (gint64 x, gint64 *r) +{ + *r = 0; + if (x == 0) + return 0; +#ifdef Foo + if (x == Foo) + {*r = MakeMap_Test_InternalEnum_Foo; return 0;} +#endif /* ndef Foo */ + errno = EINVAL; return -1; +} + +#ifdef HAVE_STRUCT_PARENT +int +MakeMap_Test_FromParent (struct MakeMap_Test_Parent *from, struct parent *to) +{ + _cnm_return_val_if_overflow (int, from->i, -1); + + memset (to, 0, sizeof(*to)); + + to->i = from->i; + + return 0; +} +#endif /* ndef HAVE_STRUCT_PARENT */ + + +#ifdef HAVE_STRUCT_PARENT +int +MakeMap_Test_ToParent (struct parent *from, struct MakeMap_Test_Parent *to) +{ + _cnm_return_val_if_overflow (int, from->i, -1); + + memset (to, 0, sizeof(*to)); + + to->i = from->i; + + return 0; +} +#endif /* ndef HAVE_STRUCT_PARENT */ + + int MakeMap_Test_FromSimpleFlagsEnum (int x, int *r) { *r = 0; diff --git a/create-native-map/src/test.cs.ref b/create-native-map/src/test.cs.ref index 3723010..160fbe8 100644 --- a/create-native-map/src/test.cs.ref +++ b/create-native-map/src/test.cs.ref @@ -22,6 +22,22 @@ namespace Mono.Unix.Native { Locale.GetText ("Current platform doesn't support this value.")); } + [DllImport (LIB, EntryPoint="MakeMap_Test_FromChild")] + private static extern int FromChild (Child source, IntPtr destination); + + public static bool TryCopy (Child source, IntPtr destination) + { + return FromChild (source, destination) == 0; + } + + [DllImport (LIB, EntryPoint="MakeMap_Test_ToChild")] + private static extern int ToChild (IntPtr source, Child destination); + + public static bool TryCopy (IntPtr source, Child destination) + { + return ToChild (source, destination) == 0; + } + [DllImport (LIB, EntryPoint="MakeMap_Test_FromFlagsEnum")] private static extern int FromFlagsEnum (FlagsEnum value, out Int32 rval); @@ -86,6 +102,54 @@ namespace Mono.Unix.Native { return ToFooHolder (source, out destination) == 0; } + [DllImport (LIB, EntryPoint="MakeMap_Test_FromInternalEnum")] + private static extern int FromInternalEnum (InternalEnum value, out Int64 rval); + + internal static bool TryFromInternalEnum (InternalEnum value, out Int64 rval) + { + return FromInternalEnum (value, out rval) == 0; + } + + internal static Int64 FromInternalEnum (InternalEnum value) + { + Int64 rval; + if (FromInternalEnum (value, out rval) == -1) + ThrowArgumentException (value); + return rval; + } + + [DllImport (LIB, EntryPoint="MakeMap_Test_ToInternalEnum")] + private static extern int ToInternalEnum (Int64 value, out InternalEnum rval); + + internal static bool TryToInternalEnum (Int64 value, out InternalEnum rval) + { + return ToInternalEnum (value, out rval) == 0; + } + + internal static InternalEnum ToInternalEnum (Int64 value) + { + InternalEnum rval; + if (ToInternalEnum (value, out rval) == -1) + ThrowArgumentException (value); + return rval; + } + + [DllImport (LIB, EntryPoint="MakeMap_Test_FromParent")] + private static extern int FromParent (Parent source, IntPtr destination); + + public static bool TryCopy (Parent source, IntPtr destination) + { + return FromParent (source, destination) == 0; + } + + [DllImport (LIB, EntryPoint="MakeMap_Test_ToParent")] + private static extern int ToParent (IntPtr source, Parent destination); + + public static bool TryCopy (IntPtr source, Parent destination) + { + return ToParent (source, destination) == 0; + } + [DllImport (LIB, EntryPoint="MakeMap_Test_FromSimpleFlagsEnum")] private static extern int FromSimpleFlagsEnum (SimpleFlagsEnum value, out Int32 rval); diff --git a/create-native-map/src/test.h.ref b/create-native-map/src/test.h.ref index f45e838..34d2ef1 100644 --- a/create-native-map/src/test.h.ref +++ b/create-native-map/src/test.h.ref @@ -60,6 +60,13 @@ enum MakeMap_Test_FlagsEnum { int MakeMap_Test_FromFlagsEnum (int x, int *r); int MakeMap_Test_ToFlagsEnum (int x, int *r); +enum MakeMap_Test_InternalEnum { + MakeMap_Test_InternalEnum_Foo = 0x0000000000000000, + #define MakeMap_Test_InternalEnum_Foo MakeMap_Test_InternalEnum_Foo +}; +int MakeMap_Test_FromInternalEnum (gint64 x, gint64 *r); +int MakeMap_Test_ToInternalEnum (gint64 x, gint64 *r); + enum MakeMap_Test_SimpleFlagsEnum { MakeMap_Test_SimpleFlagsEnum_A = 0x00000001, #define MakeMap_Test_SimpleFlagsEnum_A MakeMap_Test_SimpleFlagsEnum_A @@ -105,9 +112,11 @@ int MakeMap_Rename_ToColors (int x, int *r); */ struct MakeMap_Test_Baz; +struct MakeMap_Test_Child; struct MakeMap_Test_Foo; struct MakeMap_Test_FooHolder; struct MakeMap_Test_ForDelegate; +struct MakeMap_Test_Parent; struct MakeMap_Test_Qux; struct MakeMap_Rename_Stat; @@ -115,8 +124,10 @@ struct MakeMap_Rename_Stat; * Inferred Structure Declarations */ +struct child; struct foo; struct foo_holder; +struct parent; /* * Delegate Declarations @@ -147,6 +158,17 @@ struct MakeMap_Test_Baz { DelRefArrayBaz b8; }; +struct MakeMap_Test_Child { + int i; + int j; +}; + +int +MakeMap_Test_FromChild (struct MakeMap_Test_Child* from, struct child *to); +int +MakeMap_Test_ToChild (struct child *from, struct MakeMap_Test_Child* to); + + struct MakeMap_Test_Foo { int foo; void* p; @@ -174,6 +196,16 @@ struct MakeMap_Test_ForDelegate { int i; }; +struct MakeMap_Test_Parent { + int i; +}; + +int +MakeMap_Test_FromParent (struct MakeMap_Test_Parent* from, struct parent *to); +int +MakeMap_Test_ToParent (struct parent *from, struct MakeMap_Test_Parent* to); + + struct MakeMap_Test_Qux { int i; struct MakeMap_Test_Baz* b;