Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add Ignore property to MapAttribute, emit structure definitions in correct order #22

Closed
wants to merge 1 commit into from

2 participants

@steffen-kiess
  • [Map (Ignore=true)] can be used to ignore a field when generating the C wrapper structure. This is useful for structures with LayoutKind.Explicit.

  • When emitting the C wrapper structures make sure all needed types are emitted before emitting a structure.

This changes are needed for mono/mono#221

@steffen-kiess steffen-kiess Add Ignore property to MapAttribute, emit structure definitions in co…
…rrect order

* [Map (Ignore=true)] can be used to ignore a field when generating the C wrapper structure. This is useful for structures with LayoutKind.Explicit.

* When emitting the C wrapper structures make sure all needed types are emitted before emitting a structure.
20caea4
@jonpryor
Owner

Do the following:

$ cd path/to/mono-checkout
$ cd support
$ make refresh

Note: this will require that PKG_CONFIG_PATH prefer your mono-tools install over the system mono-tools install, as it will run pkg-config --variable=Programs create-native-map to get the updated create-native-map.exe before running it.

If nothing is changed in the mono/support directory, then the patch is ready for merging.

@steffen-kiess

Ok, I did that with the current mono HEAD (76da0ed16c2c86874e8c7503c881855878319fee) and nothing changes in mono/support

@jonpryor
Owner

As per this comment, I don't think that this patch is required.

@steffen-kiess

Yes, this patch isn't requirde anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 19, 2012
  1. @steffen-kiess

    Add Ignore property to MapAttribute, emit structure definitions in co…

    steffen-kiess authored
    …rrect order
    
    * [Map (Ignore=true)] can be used to ignore a field when generating the C wrapper structure. This is useful for structures with LayoutKind.Explicit.
    
    * When emitting the C wrapper structures make sure all needed types are emitted before emitting a structure.
This page is out of date. Refresh to see the latest.
View
6 create-native-map/src/MapAttribute.cs
@@ -38,6 +38,7 @@
internal class MapAttribute : Attribute {
private string nativeType;
private string suppressFlags;
+ private bool ignore;
public MapAttribute ()
{
@@ -56,5 +57,10 @@ public MapAttribute (string nativeType)
get {return suppressFlags;}
set {suppressFlags = value;}
}
+
+ public bool Ignore {
+ get {return ignore;}
+ set {ignore = value;}
+ }
}
View
46 create-native-map/src/create-native-map.cs
@@ -348,6 +348,7 @@ public static MapAttribute GetMapAttribute (ICustomAttributeProvider element)
? new MapAttribute ()
: new MapAttribute (nativeType);
map.SuppressFlags = GetPropertyValueAsString (o, "SuppressFlags");
+ map.Ignore = GetPropertyValueAsBool (o, "Ignore");
return map;
}
return null;
@@ -374,6 +375,12 @@ private static string GetPropertyValueAsString (object o, string property)
return null;
}
+ private static bool GetPropertyValueAsBool (object o, string property)
+ {
+ object v = GetPropertyValue (o, property);
+ return v == null ? false : (bool) v;
+ }
+
private static object GetPropertyValue (object o, string property)
{
PropertyInfo p = o.GetType().GetProperty (property);
@@ -484,6 +491,14 @@ public static string GetNativeType (FieldInfo field)
return null;
}
+ public static bool IsIgnored (FieldInfo field)
+ {
+ MapAttribute map = GetMapAttribute (field);
+ if (map != null)
+ return map.Ignore;
+ return false;
+ }
+
public static string GetFunctionDeclaration (string name, MethodInfo method)
{
StringBuilder sb = new StringBuilder ();
@@ -663,10 +678,11 @@ protected string GetNativeMemberName (FieldInfo field)
class HeaderFileGenerator : FileGenerator {
StreamWriter sh;
string assembly_file;
- Dictionary<string, MethodInfo> methods = new Dictionary <string, MethodInfo> ();
- Dictionary<string, Type> structs = new Dictionary <string, Type> ();
- Dictionary<string, MethodInfo> delegates = new Dictionary <string, MethodInfo> ();
- List<string> decls = new List <string> ();
+ Dictionary<string, MethodInfo> methods = new Dictionary <string, MethodInfo> ();
+ Dictionary<string, Type> structs = new Dictionary <string, Type> ();
+ Dictionary<string, MethodInfo> delegates = new Dictionary <string, MethodInfo> ();
+ List<string> decls = new List <string> ();
+ Dictionary<string, object> writtenStructs = new Dictionary <string, object> ();
public override void CreateFile (string assembly_name, string file_prefix)
{
@@ -894,17 +910,31 @@ private static IEnumerable<string> Sort (ICollection<string> c)
private void WriteStructDeclarations (string s)
{
+ if (writtenStructs.ContainsKey (s)) {
+ return;
+ }
+ writtenStructs.Add (s, null);
Type t = structs [s];
#if false
if (!t.Assembly.CodeBase.EndsWith (this.assembly_file)) {
return;
}
#endif
- sh.WriteLine ("struct {0} {{", MapUtils.GetManagedType (t));
FieldInfo[] fields = t.GetFields (BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic);
+ // make sure all structures used as fields are written before this structure
+ foreach (FieldInfo field in fields) {
+ if (MapUtils.IsIgnored (field))
+ continue;
+ string managed_type = field.FieldType.FullName;
+ if (structs.ContainsKey (managed_type))
+ WriteStructDeclarations (managed_type);
+ }
+ sh.WriteLine ("struct {0} {{", MapUtils.GetManagedType (t));
int max_type_len = 0, max_name_len = 0, max_native_len = 0;
Array.ForEach (fields, delegate (FieldInfo f) {
+ if (MapUtils.IsIgnored (f))
+ return;
max_type_len = Math.Max (max_type_len, HeaderFileGenerator.GetType (f.FieldType).Length);
max_name_len = Math.Max (max_name_len, GetNativeMemberName (f).Length);
string native_type = MapUtils.GetNativeType (f);
@@ -913,6 +943,8 @@ private void WriteStructDeclarations (string s)
});
SortFieldsInOffsetOrder (t, fields);
foreach (FieldInfo field in fields) {
+ if (MapUtils.IsIgnored (field))
+ continue;
string fname = GetNativeMemberName (field);
sh.Write ("\t{0,-" + max_type_len + "} {1};",
GetType (field.FieldType), fname);
@@ -1410,11 +1442,11 @@ private static FieldInfo[] GetFieldsToCopy (Type t)
BindingFlags.Public | BindingFlags.NonPublic);
int count = 0;
for (int i = 0; i < fields.Length; ++i)
- if (MapUtils.GetCustomAttribute <NonSerializedAttribute> (fields [i]) == null)
+ if (MapUtils.GetCustomAttribute <NonSerializedAttribute> (fields [i]) == null && !MapUtils.IsIgnored (fields [i]))
++count;
FieldInfo[] rf = new FieldInfo [count];
for (int i = 0, j = 0; i < fields.Length; ++i) {
- if (MapUtils.GetCustomAttribute <NonSerializedAttribute> (fields [i]) == null)
+ if (MapUtils.GetCustomAttribute <NonSerializedAttribute> (fields [i]) == null && !MapUtils.IsIgnored (fields [i]))
rf [j++] = fields [i];
}
return rf;
Something went wrong with that request. Please try again.