2 // MakeMap.cs: Builds a C map of constants defined on C# land
5 // Miguel de Icaza (miguel@novell.com)
6 // Jonathan Pryor (jonpryor@vt.edu)
8 // (C) 2003 Novell, Inc.
9 // (C) 2004 Jonathan Pryor
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
35 using System.Reflection;
36 using System.Runtime.InteropServices;
38 delegate void CreateFileHandler (string assembly_name, string file_prefix);
39 delegate void AssemblyAttributesHandler (Assembly assembly);
40 delegate void TypeHandler (Type t, string ns, string fn);
41 delegate void CloseFileHandler (string file_prefix);
45 public static int Main (string [] args)
47 FileGenerator[] generators = new FileGenerator[]{
48 new HeaderFileGenerator (),
49 new SourceFileGenerator (),
50 new ConvertFileGenerator (),
51 new MphPrototypeFileGenerator (),
54 MakeMap composite = new MakeMap ();
55 foreach (FileGenerator g in generators) {
56 composite.FileCreators += new CreateFileHandler (g.CreateFile);
57 composite.AssemblyAttributesHandler +=
58 new AssemblyAttributesHandler (g.WriteAssemblyAttributes);
59 composite.TypeHandler += new TypeHandler (g.WriteType);
60 composite.FileClosers += new CloseFileHandler (g.CloseFile);
63 return composite.Run (args);
66 event CreateFileHandler FileCreators;
67 event AssemblyAttributesHandler AssemblyAttributesHandler;
68 event TypeHandler TypeHandler;
69 event CloseFileHandler FileClosers;
71 int Run (string[] args)
73 if (args.Length != 2){
74 Console.WriteLine ("Usage is: make-map assembly output");
78 string assembly_name = args[0];
79 string output = args[1];
81 FileCreators (assembly_name, output);
83 Assembly assembly = Assembly.LoadFrom (assembly_name);
84 AssemblyAttributesHandler (assembly);
86 Type [] exported_types = assembly.GetTypes ();
88 foreach (Type t in exported_types) {
89 string ns = t.Namespace;
90 if (ns == null || !ns.StartsWith ("Mono"))
92 string fn = GetNativeName (t.FullName);
93 ns = GetNativeName (ns);
95 TypeHandler (t, ns, fn);
102 internal static string GetNativeName (string fn)
104 return fn.Replace (".", "_").Replace ("Mono_Unix", "Mono_Posix");
108 abstract class FileGenerator {
109 public abstract void CreateFile (string assembly_name, string file_prefix);
111 public virtual void WriteAssemblyAttributes (Assembly assembly)
115 public abstract void WriteType (Type t, string ns, string fn);
116 public abstract void CloseFile (string file_prefix);
118 protected static void WriteHeader (StreamWriter s, string assembly)
122 " * This file was automatically generated by make-map from {0}.\n" +
124 " * DO NOT MODIFY.\n" +
126 "#include <config.h>\n",
130 protected static bool CanMapType (Type t, out bool bits)
132 object [] attributes = t.GetCustomAttributes (false);
136 foreach (object attr in attributes) {
137 if (attr.GetType ().Name == "MapAttribute")
139 if (attr.GetType ().Name == "FlagsAttribute")
145 protected static string GetNativeType (Type t)
149 ut = Enum.GetUnderlyingType (t).Name;
150 Type et = t.GetElementType ();
151 if (et != null && et.IsEnum)
152 ut = Enum.GetUnderlyingType (et).Name;
157 case "Boolean": type = "int"; break;
158 case "Byte": type = "unsigned char"; break;
159 case "SByte": type = "signed char"; break;
160 case "Int16": type = "short"; break;
161 case "UInt16": type = "unsigned short"; break;
162 case "Int32": type = "int"; break;
163 case "UInt32": type = "unsigned int"; break;
164 case "UInt32[]": type = "unsigned int*"; break;
165 case "Int64": type = "gint64"; break;
166 case "UInt64": type = "guint64"; break;
167 case "IntPtr": type = "void*"; break;
168 case "Byte[]": type = "void*"; break;
169 case "String": type = "const char*"; break;
170 case "StringBuilder": type = "char*"; break;
171 case "Void": type = "void"; break;
172 case "HandleRef": type = "void*"; break;
175 return string.Format ("{0}{1}", type,
176 t.IsByRef ? "*" : "");
177 return GetTypeName (t);
180 private static string GetTypeName (Type t)
182 if (t.Namespace.StartsWith ("System"))
183 return "int /* warning: unknown mapping for type: " + t.Name + " */";
184 string ts = "struct " +
185 MakeMap.GetNativeName (t.FullName).Replace ("+", "_").Replace ("&", "*");
190 class HeaderFileGenerator : FileGenerator {
193 public override void CreateFile (string assembly_name, string file_prefix)
195 sh = File.CreateText (file_prefix + ".h");
196 WriteHeader (sh, assembly_name);
197 sh.WriteLine ("#ifndef INC_Mono_Posix_" + file_prefix + "_H");
198 sh.WriteLine ("#define INC_Mono_Posix_" + file_prefix + "_H\n");
199 sh.WriteLine ("#include <glib/gtypes.h>\n");
200 sh.WriteLine ("G_BEGIN_DECLS\n");
203 public override void WriteType (Type t, string ns, string fn)
206 if (!CanMapType (t, out bits))
208 string etype = GetNativeType (t);
210 WriteLiteralValues (sh, t, fn);
211 sh.WriteLine ("int {1}_From{2} ({0} x, {0} *r);", etype, ns, t.Name);
212 sh.WriteLine ("int {1}_To{2} ({0} x, {0} *r);", etype, ns, t.Name);
216 static void WriteLiteralValues (StreamWriter sh, Type t, string n)
218 object inst = Activator.CreateInstance (t);
219 foreach (FieldInfo fi in t.GetFields ()){
222 sh.WriteLine ("#define {0}_{1} 0x{2:x}", n, fi.Name, fi.GetValue (inst));
226 public override void CloseFile (string file_prefix)
228 sh.WriteLine ("G_END_DECLS\n");
229 sh.WriteLine ("#endif /* ndef INC_Mono_Posix_" + file_prefix + "_H */\n");
234 class SourceFileGenerator : FileGenerator {
237 public override void CreateFile (string assembly_name, string file_prefix)
239 sc = File.CreateText (file_prefix + ".c");
240 WriteHeader (sc, assembly_name);
242 if (file_prefix.IndexOf ("/") != -1)
243 file_prefix = file_prefix.Substring (file_prefix.IndexOf ("/") + 1);
244 sc.WriteLine ("#include \"{0}.h\"", file_prefix);
248 public override void WriteAssemblyAttributes (Assembly assembly)
250 object [] x = assembly.GetCustomAttributes (false);
251 Console.WriteLine ("Got: " + x.Length);
252 foreach (object aattr in assembly.GetCustomAttributes (false)) {
253 Console.WriteLine ("Got: " + aattr.GetType ().Name);
254 if (aattr.GetType ().Name == "IncludeAttribute"){
255 WriteDefines (sc, aattr);
256 WriteIncludes (sc, aattr);
261 static void WriteDefines (TextWriter writer, object o)
263 PropertyInfo prop = o.GetType ().GetProperty ("Defines");
265 throw new Exception ("Cannot find 'Defines' property");
267 MethodInfo method = prop.GetGetMethod ();
268 string [] defines = (string []) method.Invoke (o, null);
269 foreach (string def in defines) {
270 writer.WriteLine ("#ifndef {0}", def);
271 writer.WriteLine ("#define {0}", def);
272 writer.WriteLine ("#endif /* ndef {0} */", def);
276 static void WriteIncludes (TextWriter writer, object o)
278 PropertyInfo prop = o.GetType ().GetProperty ("Includes");
280 throw new Exception ("Cannot find 'Includes' property");
282 MethodInfo method = prop.GetGetMethod ();
283 string [] includes = (string []) method.Invoke (o, null);
284 foreach (string inc in includes){
285 if (inc.Length > 3 &&
286 string.CompareOrdinal (inc, 0, "ah:", 0, 3) == 0) {
287 string i = inc.Substring (3);
288 writer.WriteLine ("#ifdef HAVE_" + (i.ToUpper ().Replace ("/", "_").Replace (".", "_")));
289 writer.WriteLine ("#include <{0}>", i);
290 writer.WriteLine ("#endif");
292 writer.WriteLine ("#include <{0}>", inc);
297 public override void WriteType (Type t, string ns, string fn)
300 if (!CanMapType (t, out bits))
302 string etype = GetNativeType (t);
304 WriteFromManagedType (t, ns, fn, etype, bits);
305 WriteToManagedType (t, ns, fn, etype, bits);
308 private void WriteFromManagedType (Type t, string ns, string fn, string etype, bool bits)
310 sc.WriteLine ("int {1}_From{2} ({0} x, {0} *r)", etype, ns, t.Name);
312 sc.WriteLine ("\t*r = 0;");
313 // For many values, 0 is a valid value, but doesn't have it's own symbol.
314 // Examples: Error (0 means "no error"), WaitOptions (0 means "no options").
315 // Make 0 valid for all conversions.
316 sc.WriteLine ("\tif (x == 0)\n\t\treturn 0;");
317 foreach (FieldInfo fi in t.GetFields ()) {
320 if (Attribute.GetCustomAttribute (fi,
321 typeof(ObsoleteAttribute), false) != null) {
322 sc.WriteLine ("\t/* {0}_{1} is obsolete; ignoring */", fn, fi.Name);
326 // properly handle case where [Flags] enumeration has helper
327 // synonyms. e.g. DEFFILEMODE and ACCESSPERMS for mode_t.
328 sc.WriteLine ("\tif ((x & {0}_{1}) == {0}_{1})", fn, fi.Name);
330 sc.WriteLine ("\tif (x == {0}_{1})", fn, fi.Name);
331 sc.WriteLine ("#ifdef {0}", fi.Name);
333 sc.WriteLine ("\t\t*r |= {1};", fn, fi.Name);
335 sc.WriteLine ("\t\t{{*r = {1}; return 0;}}", fn, fi.Name);
336 sc.WriteLine ("#else /* def {0} */\n\t\t{{errno = EINVAL; return -1;}}", fi.Name);
337 sc.WriteLine ("#endif /* ndef {0} */", fi.Name);
340 sc.WriteLine ("\treturn 0;");
342 sc.WriteLine ("\terrno = EINVAL; return -1;"); // return error if not matched
343 sc.WriteLine ("}\n");
346 private void WriteToManagedType (Type t, string ns, string fn, string etype, bool bits)
348 sc.WriteLine ("int {1}_To{2} ({0} x, {0} *r)", etype, ns, t.Name);
350 sc.WriteLine ("\t*r = 0;", etype);
351 // For many values, 0 is a valid value, but doesn't have it's own symbol.
352 // Examples: Error (0 means "no error"), WaitOptions (0 means "no options").
353 // Make 0 valid for all conversions.
354 sc.WriteLine ("\tif (x == 0)\n\t\treturn 0;");
355 foreach (FieldInfo fi in t.GetFields ()) {
358 sc.WriteLine ("#ifdef {0}", fi.Name);
360 // properly handle case where [Flags] enumeration has helper
361 // synonyms. e.g. DEFFILEMODE and ACCESSPERMS for mode_t.
362 sc.WriteLine ("\tif ((x & {1}) == {1})\n\t\t*r |= {0}_{1};", fn, fi.Name);
364 sc.WriteLine ("\tif (x == {1})\n\t\t{{*r = {0}_{1}; return 0;}}", fn, fi.Name);
365 sc.WriteLine ("#endif /* ndef {0} */", fi.Name);
368 sc.WriteLine ("\treturn 0;");
370 sc.WriteLine ("\terrno = EINVAL; return -1;");
371 sc.WriteLine ("}\n");
374 public override void CloseFile (string file_prefix)
380 class ConvertFileGenerator : FileGenerator {
383 public override void CreateFile (string assembly_name, string file_prefix)
385 scs = File.CreateText (file_prefix + ".cs");
386 WriteHeader (scs, assembly_name);
387 scs.WriteLine ("using System;");
388 scs.WriteLine ("using System.Runtime.InteropServices;");
389 scs.WriteLine ("using Mono.Unix;\n");
390 scs.WriteLine ("namespace Mono.Unix {\n");
391 scs.WriteLine ("\tpublic sealed /* static */ class UnixConvert");
392 scs.WriteLine ("\t{");
393 scs.WriteLine ("\t\tprivate UnixConvert () {}\n");
394 scs.WriteLine ("\t\tprivate const string LIB = \"MonoPosixHelper\";\n");
395 scs.WriteLine ("\t\tprivate static void ThrowArgumentException (object value)");
396 scs.WriteLine ("\t\t{");
397 scs.WriteLine ("\t\t\tthrow new ArgumentOutOfRangeException (\"value\", value,");
398 scs.WriteLine ("\t\t\t\tLocale.GetText (\"Current platform doesn't support this value.\"));");
399 scs.WriteLine ("\t\t}\n");
402 public override void WriteType (Type t, string ns, string fn)
405 if (!CanMapType (t, out bits))
408 string mtype = Enum.GetUnderlyingType(t).Name;
409 ObsoleteAttribute oa = (ObsoleteAttribute) Attribute.GetCustomAttribute (t,
410 typeof(ObsoleteAttribute), false);
411 string obsolete = "";
413 obsolete = "[Obsolete (\"" + oa.Message + "\")]\n\t\t";
415 scs.WriteLine ("\t\t[DllImport (LIB, " +
416 "EntryPoint=\"{0}_From{1}\")]\n" +
417 "\t\tprivate static extern int From{1} ({1} value, out {2} rval);\n",
419 scs.WriteLine ("\t\t{3}public static bool TryFrom{1} ({1} value, out {2} rval)\n" +
421 "\t\t\treturn From{1} (value, out rval) == 0;\n" +
422 "\t\t}}\n", ns, t.Name, mtype, obsolete);
423 scs.WriteLine ("\t\t{2}public static {0} From{1} ({1} value)", mtype, t.Name, obsolete);
424 scs.WriteLine ("\t\t{");
425 scs.WriteLine ("\t\t\t{0} rval;", mtype);
426 scs.WriteLine ("\t\t\tif (From{0} (value, out rval) == -1)\n" +
427 "\t\t\t\tThrowArgumentException (value);", t.Name);
428 scs.WriteLine ("\t\t\treturn rval;");
429 scs.WriteLine ("\t\t}\n");
430 scs.WriteLine ("\t\t[DllImport (LIB, " +
431 "EntryPoint=\"{0}_To{1}\")]\n" +
432 "\t\tprivate static extern int To{1} ({2} value, out {1} rval);\n",
434 scs.WriteLine ("\t\t{2}public static bool TryTo{1} ({0} value, out {1} rval)\n" +
436 "\t\t\treturn To{1} (value, out rval) == 0;\n" +
437 "\t\t}}\n", mtype, t.Name, obsolete);
438 scs.WriteLine ("\t\t{2}public static {1} To{1} ({0} value)", mtype, t.Name, obsolete);
439 scs.WriteLine ("\t\t{");
440 scs.WriteLine ("\t\t\t{0} rval;", t.Name);
441 scs.WriteLine ("\t\t\tif (To{0} (value, out rval) == -1)\n" +
442 "\t\t\t\tThrowArgumentException (value);", t.Name);
443 scs.WriteLine ("\t\t\treturn rval;");
444 scs.WriteLine ("\t\t}\n");
447 public override void CloseFile (string file_prefix)
449 scs.WriteLine ("\t}");
450 scs.WriteLine ("}\n");
455 class MphPrototypeFileGenerator : FileGenerator {
457 Hashtable methods = new Hashtable ();
458 Hashtable structs = new Hashtable ();
460 public override void CreateFile (string assembly_name, string file_prefix)
462 icall = File.CreateText (file_prefix + "-icalls.h");
463 WriteHeader (icall, assembly_name);
464 icall.WriteLine ("#ifndef INC_Mono_Posix_" + file_prefix + "_ICALLS_H");
465 icall.WriteLine ("#define INC_Mono_Posix_" + file_prefix + "_ICALLS_H\n");
466 icall.WriteLine ("#include <glib/gtypes.h>\n");
467 icall.WriteLine ("G_BEGIN_DECLS\n");
469 // Kill warning about unused method
473 public override void WriteType (Type t, string ns, string fn)
475 BindingFlags bf = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
476 foreach (MethodInfo m in t.GetMethods (bf)) {
477 if ((m.Attributes & MethodAttributes.PinvokeImpl) == 0)
479 DllImportAttribute dia = GetDllImportInfo (m);
481 Console.WriteLine ("Unable to emit native prototype for P/Invoke " +
485 // we shouldn't declare prototypes for POSIX, etc. functions.
486 if (dia.Value != "MonoPosixHelper" || IsOnExcludeList (dia.EntryPoint))
488 methods [dia.EntryPoint] = m;
493 private static DllImportAttribute GetDllImportInfo (MethodInfo method)
495 // .NET 2.0 synthesizes pseudo-attributes such as DllImport
496 DllImportAttribute dia = (DllImportAttribute) Attribute.GetCustomAttribute (method,
497 typeof(DllImportAttribute), false);
501 // We're not on .NET 2.0; assume we're on Mono and use some internal
503 Type MonoMethod = Type.GetType ("System.Reflection.MonoMethod", false);
504 if (MonoMethod == null) {
505 Console.WriteLine ("cannot find MonoMethod");
508 MethodInfo GetDllImportAttribute =
509 MonoMethod.GetMethod ("GetDllImportAttribute",
510 BindingFlags.Static | BindingFlags.NonPublic);
511 if (GetDllImportAttribute == null) {
512 Console.WriteLine ("cannot find GetDllImportAttribute");
515 IntPtr mhandle = method.MethodHandle.Value;
516 return (DllImportAttribute) GetDllImportAttribute.Invoke (null,
517 new object[]{mhandle});
520 private static string[] ExcludeList = new string[]{
521 "Mono_Posix_Stdlib_snprintf",
524 private bool IsOnExcludeList (string method)
526 int idx = Array.BinarySearch (ExcludeList, method);
527 return (idx >= 0 && idx < ExcludeList.Length) ? true : false;
530 private void RecordStructs (MethodInfo method)
532 ParameterInfo[] parameters = method.GetParameters ();
533 foreach (ParameterInfo pi in parameters) {
534 string s = GetNativeType (pi.ParameterType);
535 if (s.StartsWith ("struct"))
540 public override void CloseFile (string file_prefix)
542 icall.WriteLine ("/*\n * Structure Declarations\n */");
543 foreach (string s in Sort (structs.Keys))
544 icall.WriteLine ("{0};", s.Replace ("*", ""));
548 icall.WriteLine ("/*\n * Function Declarations\n */");
549 foreach (string method in Sort (methods.Keys)) {
550 WriteMethodDeclaration ((MethodInfo) methods [method], method);
553 icall.WriteLine ("\nG_END_DECLS\n");
554 icall.WriteLine ("#endif /* ndef INC_Mono_Posix_" + file_prefix + "_ICALLS_H */\n");
558 private static IEnumerable Sort (ICollection c)
560 ArrayList al = new ArrayList (c);
565 private void WriteMethodDeclaration (MethodInfo method, string entryPoint)
567 icall.Write ("{0} ", GetNativeType (method.ReturnType));
568 icall.Write ("{0} ", entryPoint);
569 ParameterInfo[] parameters = method.GetParameters();
570 if (parameters.Length == 0) {
571 icall.WriteLine ("(void);");
574 if (parameters.Length > 0) {
576 WriteParameterDeclaration (parameters [0]);
578 for (int i = 1; i < parameters.Length; ++i) {
580 WriteParameterDeclaration (parameters [i]);
582 icall.WriteLine (");");
585 private void DumpTypeInfo (Type t)
590 icall.WriteLine ("\t\t/* Type Info for " + t.FullName + ":");
591 foreach (MemberInfo mi in typeof(Type).GetMembers()) {
592 icall.WriteLine ("\t\t\t{0}={1}", mi.Name, GetMemberValue (mi, t));
594 icall.WriteLine ("\t\t */");
597 private static string GetMemberValue (MemberInfo mi, Type t)
600 switch (mi.MemberType) {
601 case MemberTypes.Constructor:
602 case MemberTypes.Method: {
603 MethodBase b = (MethodBase) mi;
604 if (b.GetParameters().Length == 0)
605 return b.Invoke (t, new object[]{}).ToString();
606 return "<<cannot invoke>>";
608 case MemberTypes.Field:
609 return ((FieldInfo) mi).GetValue (t).ToString ();
610 case MemberTypes.Property: {
611 PropertyInfo pi = (PropertyInfo) mi;
613 return "<<cannot read>>";
614 return pi.GetValue (t, null).ToString ();
617 return "<<unknown value>>";
620 catch (Exception e) {
621 return "<<exception reading member: " + e.Message + ">>";
625 private void WriteParameterDeclaration (ParameterInfo pi)
627 // DumpTypeInfo (pi.ParameterType);
628 icall.Write ("{0} {1}", GetNativeType (pi.ParameterType), pi.Name);