* Syscall.cs: s/LOG_USRE/LOG_USER/. Fixes #75274.
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / make-map.cs
1 //
2 // MakeMap.cs: Builds a C map of constants defined on C# land
3 //
4 // Authors:
5 //  Miguel de Icaza (miguel@novell.com)
6 //  Jonathan Pryor (jonpryor@vt.edu)
7 //
8 // (C) 2003 Novell, Inc.
9 // (C) 2004 Jonathan Pryor
10 //
11
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32 using System;
33 using System.Collections;
34 using System.IO;
35 using System.Reflection;
36 using System.Runtime.InteropServices;
37
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);
42
43 class MakeMap {
44
45         public static int Main (string [] args)
46         {
47                 FileGenerator[] generators = new FileGenerator[]{
48                         new HeaderFileGenerator (),
49                         new SourceFileGenerator (),
50                         new ConvertFileGenerator (),
51                         new MphPrototypeFileGenerator (),
52                 };
53
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);
61                 }
62
63                 return composite.Run (args);
64         }
65
66         event CreateFileHandler FileCreators;
67         event AssemblyAttributesHandler AssemblyAttributesHandler;
68         event TypeHandler TypeHandler;
69         event CloseFileHandler FileClosers;
70
71         int Run (string[] args)
72         {
73                 if (args.Length != 2){
74                         Console.WriteLine ("Usage is: make-map assembly output");
75                         return 1;
76                 }
77                 
78                 string assembly_name = args[0];
79                 string output = args[1];
80
81                 FileCreators (assembly_name, output);
82
83                 Assembly assembly = Assembly.LoadFrom (assembly_name);
84                 AssemblyAttributesHandler (assembly);
85                 
86                 Type [] exported_types = assembly.GetTypes ();
87                         
88                 foreach (Type t in exported_types) {
89                         string ns = t.Namespace;
90                         if (ns == null || !ns.StartsWith ("Mono"))
91                                 continue;
92                         string fn = GetNativeName (t.FullName);
93                         ns = GetNativeName (ns);
94
95                         TypeHandler (t, ns, fn);
96                 }
97                 FileClosers (output);
98
99                 return 0;
100         }
101
102         internal static string GetNativeName (string fn)
103         {
104                 return fn.Replace (".", "_").Replace ("Mono_Unix", "Mono_Posix");
105         }
106 }
107
108 abstract class FileGenerator {
109         public abstract void CreateFile (string assembly_name, string file_prefix);
110
111         public virtual void WriteAssemblyAttributes (Assembly assembly)
112         {
113         }
114
115         public abstract void WriteType (Type t, string ns, string fn);
116         public abstract void CloseFile (string file_prefix);
117
118         protected static void WriteHeader (StreamWriter s, string assembly)
119         {
120                 s.WriteLine (
121                         "/*\n" +
122                         " * This file was automatically generated by make-map from {0}.\n" +
123                         " *\n" +
124                         " * DO NOT MODIFY.\n" +
125                         " */\n" +
126                         "#include <config.h>\n",
127                         assembly);
128         }
129
130         protected static bool CanMapType (Type t, out bool bits)
131         {
132                 object [] attributes = t.GetCustomAttributes (false);
133                 bool map = false;
134                 bits = false;
135                 
136                 foreach (object attr in attributes) {
137                         if (attr.GetType ().Name == "MapAttribute")
138                                 map = true;
139                         if (attr.GetType ().Name == "FlagsAttribute")
140                                 bits = true;
141                 }
142                 return map;
143         }
144
145         protected static string GetNativeType (Type t)
146         {
147                 string ut = t.Name;
148                 if (t.IsEnum)
149                         ut = Enum.GetUnderlyingType (t).Name;
150                 Type et = t.GetElementType ();
151                 if (et != null && et.IsEnum)
152                         ut = Enum.GetUnderlyingType (et).Name;
153
154                 string type = null;
155
156                 switch (ut) {
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;
173                 }
174                 if (type != null)
175                         return string.Format ("{0}{1}", type,
176                                         t.IsByRef ? "*" : "");
177                 return GetTypeName (t);
178         }
179
180         private static string GetTypeName (Type t)
181         {
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 ("&", "*");
186                 return ts;
187         }
188 }
189
190 class HeaderFileGenerator : FileGenerator {
191         StreamWriter sh;
192
193         public override void CreateFile (string assembly_name, string file_prefix)
194         {
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");
201         }
202
203         public override void WriteType (Type t, string ns, string fn)
204         {
205                 bool bits;
206                 if (!CanMapType (t, out bits))
207                         return;
208                 string etype = GetNativeType (t);
209
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);
213                 sh.WriteLine ();
214         }
215
216         static void WriteLiteralValues (StreamWriter sh, Type t, string n)
217         {
218                 object inst = Activator.CreateInstance (t);
219                 foreach (FieldInfo fi in t.GetFields ()){
220                         if (!fi.IsLiteral)
221                                 continue;
222                         sh.WriteLine ("#define {0}_{1} 0x{2:x}", n, fi.Name, fi.GetValue (inst));
223                 }
224         }
225
226         public override void CloseFile (string file_prefix)
227         {
228                 sh.WriteLine ("G_END_DECLS\n");
229                 sh.WriteLine ("#endif /* ndef INC_Mono_Posix_" + file_prefix + "_H */\n");
230                 sh.Close ();
231         }
232 }
233
234 class SourceFileGenerator : FileGenerator {
235         StreamWriter sc;
236
237         public override void CreateFile (string assembly_name, string file_prefix)
238         {
239                 sc = File.CreateText (file_prefix + ".c");
240                 WriteHeader (sc, assembly_name);
241
242                 if (file_prefix.IndexOf ("/") != -1)
243                         file_prefix = file_prefix.Substring (file_prefix.IndexOf ("/") + 1);
244                 sc.WriteLine ("#include \"{0}.h\"", file_prefix);
245                 sc.WriteLine ();
246         }
247
248         public override void WriteAssemblyAttributes (Assembly assembly)
249         {
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);
257                         }
258                 }
259         }
260
261         static void WriteDefines (TextWriter writer, object o)
262         {
263                 PropertyInfo prop = o.GetType ().GetProperty ("Defines");
264                 if (prop == null)
265                         throw new Exception ("Cannot find 'Defines' property");
266
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);
273                 }
274         }
275
276         static void WriteIncludes (TextWriter writer, object o)
277         {
278                 PropertyInfo prop = o.GetType ().GetProperty ("Includes");
279                 if (prop == null)
280                         throw new Exception ("Cannot find 'Includes' property");
281
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");
291                         } else 
292                                 writer.WriteLine ("#include <{0}>", inc);
293                 }
294                 writer.WriteLine ();
295         }
296
297         public override void WriteType (Type t, string ns, string fn)
298         {
299                 bool bits;
300                 if (!CanMapType (t, out bits))
301                         return;
302                 string etype = GetNativeType (t);
303
304                 WriteFromManagedType (t, ns, fn, etype, bits);
305                 WriteToManagedType (t, ns, fn, etype, bits);
306         }
307
308         private void WriteFromManagedType (Type t, string ns, string fn, string etype, bool bits)
309         {
310                 sc.WriteLine ("int {1}_From{2} ({0} x, {0} *r)", etype, ns, t.Name);
311                 sc.WriteLine ("{");
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 ()) {
318                         if (!fi.IsLiteral)
319                                 continue;
320                         if (bits)
321                                 // properly handle case where [Flags] enumeration has helper
322                                 // synonyms.  e.g. DEFFILEMODE and ACCESSPERMS for mode_t.
323                                 sc.WriteLine ("\tif ((x & {0}_{1}) == {0}_{1})", fn, fi.Name);
324                         else
325                                 sc.WriteLine ("\tif (x == {0}_{1})", fn, fi.Name);
326                         sc.WriteLine ("#ifdef {0}", fi.Name);
327                         if (bits)
328                                 sc.WriteLine ("\t\t*r |= {1};", fn, fi.Name);
329                         else
330                                 sc.WriteLine ("\t\t{{*r = {1}; return 0;}}", fn, fi.Name);
331                         sc.WriteLine ("#else /* def {0} */\n\t\t{{errno = EINVAL; return -1;}}", fi.Name);
332                         sc.WriteLine ("#endif /* ndef {0} */", fi.Name);
333                 }
334                 if (bits)
335                         sc.WriteLine ("\treturn 0;");
336                 else
337                         sc.WriteLine ("\terrno = EINVAL; return -1;"); // return error if not matched
338                 sc.WriteLine ("}\n");
339         }
340
341         private void WriteToManagedType (Type t, string ns, string fn, string etype, bool bits)
342         {
343                 sc.WriteLine ("int {1}_To{2} ({0} x, {0} *r)", etype, ns, t.Name);
344                 sc.WriteLine ("{");
345                 sc.WriteLine ("\t*r = 0;", etype);
346                 // For many values, 0 is a valid value, but doesn't have it's own symbol.
347                 // Examples: Error (0 means "no error"), WaitOptions (0 means "no options").
348                 // Make 0 valid for all conversions.
349                 sc.WriteLine ("\tif (x == 0)\n\t\treturn 0;");
350                 foreach (FieldInfo fi in t.GetFields ()) {
351                         if (!fi.IsLiteral)
352                                 continue;
353                         sc.WriteLine ("#ifdef {0}", fi.Name);
354                         if (bits)
355                                 // properly handle case where [Flags] enumeration has helper
356                                 // synonyms.  e.g. DEFFILEMODE and ACCESSPERMS for mode_t.
357                                 sc.WriteLine ("\tif ((x & {1}) == {1})\n\t\t*r |= {0}_{1};", fn, fi.Name);
358                         else
359                                 sc.WriteLine ("\tif (x == {1})\n\t\t{{*r = {0}_{1}; return 0;}}", fn, fi.Name);
360                         sc.WriteLine ("#endif /* ndef {0} */", fi.Name);
361                 }
362                 if (bits)
363                         sc.WriteLine ("\treturn 0;");
364                 else
365                         sc.WriteLine ("\terrno = EINVAL; return -1;");
366                 sc.WriteLine ("}\n");
367         }
368
369         public override void CloseFile (string file_prefix)
370         {
371                 sc.Close ();
372         }
373 }
374
375 class ConvertFileGenerator : FileGenerator {
376         StreamWriter scs;
377
378         public override void CreateFile (string assembly_name, string file_prefix)
379         {
380                 scs = File.CreateText (file_prefix + ".cs");
381                 WriteHeader (scs, assembly_name);
382                 scs.WriteLine ("using System;");
383                 scs.WriteLine ("using System.Runtime.InteropServices;");
384                 scs.WriteLine ("using Mono.Unix;\n");
385                 scs.WriteLine ("namespace Mono.Unix {\n");
386                 scs.WriteLine ("\tpublic sealed /* static */ class UnixConvert");
387                 scs.WriteLine ("\t{");
388                 scs.WriteLine ("\t\tprivate UnixConvert () {}\n");
389                 scs.WriteLine ("\t\tprivate const string LIB = \"MonoPosixHelper\";\n");
390                 scs.WriteLine ("\t\tprivate static void ThrowArgumentException (object value)");
391                 scs.WriteLine ("\t\t{");
392                 scs.WriteLine ("\t\t\tthrow new ArgumentOutOfRangeException (\"value\", value,");
393                 scs.WriteLine ("\t\t\t\tLocale.GetText (\"Current platform doesn't support this value.\"));");
394                 scs.WriteLine ("\t\t}\n");
395         }
396
397         public override void WriteType (Type t, string ns, string fn)
398         {
399                 bool bits;
400                 if (!CanMapType (t, out bits))
401                         return;
402
403                 string mtype = Enum.GetUnderlyingType(t).Name;
404                 ObsoleteAttribute oa = (ObsoleteAttribute) Attribute.GetCustomAttribute (t, 
405                                         typeof(ObsoleteAttribute), false);
406                 string obsolete = "";
407                 if (oa != null) {
408                         obsolete = "[Obsolete (\"" + oa.Message + "\")]\n\t\t";
409                 }
410                 scs.WriteLine ("\t\t[DllImport (LIB, " + 
411                         "EntryPoint=\"{0}_From{1}\")]\n" +
412                         "\t\tprivate static extern int From{1} ({1} value, out {2} rval);\n",
413                         ns, t.Name, mtype);
414                 scs.WriteLine ("\t\t{3}public static bool TryFrom{1} ({1} value, out {2} rval)\n" +
415                         "\t\t{{\n" +
416                         "\t\t\treturn From{1} (value, out rval) == 0;\n" +
417                         "\t\t}}\n", ns, t.Name, mtype, obsolete);
418                 scs.WriteLine ("\t\t{2}public static {0} From{1} ({1} value)", mtype, t.Name, obsolete);
419                 scs.WriteLine ("\t\t{");
420                 scs.WriteLine ("\t\t\t{0} rval;", mtype);
421                 scs.WriteLine ("\t\t\tif (From{0} (value, out rval) == -1)\n" + 
422                                 "\t\t\t\tThrowArgumentException (value);", t.Name);
423                 scs.WriteLine ("\t\t\treturn rval;");
424                 scs.WriteLine ("\t\t}\n");
425                 scs.WriteLine ("\t\t[DllImport (LIB, " + 
426                         "EntryPoint=\"{0}_To{1}\")]\n" +
427                         "\t\tprivate static extern int To{1} ({2} value, out {1} rval);\n",
428                         ns, t.Name, mtype);
429                 scs.WriteLine ("\t\t{2}public static bool TryTo{1} ({0} value, out {1} rval)\n" +
430                         "\t\t{{\n" +
431                         "\t\t\treturn To{1} (value, out rval) == 0;\n" +
432                         "\t\t}}\n", mtype, t.Name, obsolete);
433                 scs.WriteLine ("\t\t{2}public static {1} To{1} ({0} value)", mtype, t.Name, obsolete);
434                 scs.WriteLine ("\t\t{");
435                 scs.WriteLine ("\t\t\t{0} rval;", t.Name);
436                 scs.WriteLine ("\t\t\tif (To{0} (value, out rval) == -1)\n" + 
437                                 "\t\t\t\tThrowArgumentException (value);", t.Name);
438                 scs.WriteLine ("\t\t\treturn rval;");
439                 scs.WriteLine ("\t\t}\n");
440         }
441
442         public override void CloseFile (string file_prefix)
443         {
444                 scs.WriteLine ("\t}");
445                 scs.WriteLine ("}\n");
446                 scs.Close ();
447         }
448 }
449
450 class MphPrototypeFileGenerator : FileGenerator {
451         StreamWriter icall;
452         Hashtable methods = new Hashtable ();
453         Hashtable structs = new Hashtable ();
454
455         public override void CreateFile (string assembly_name, string file_prefix)
456         {
457                 icall = File.CreateText (file_prefix + "-icalls.h");
458                 WriteHeader (icall, assembly_name);
459                 icall.WriteLine ("#ifndef INC_Mono_Posix_" + file_prefix + "_ICALLS_H");
460                 icall.WriteLine ("#define INC_Mono_Posix_" + file_prefix + "_ICALLS_H\n");
461                 icall.WriteLine ("#include <glib/gtypes.h>\n");
462                 icall.WriteLine ("G_BEGIN_DECLS\n");
463
464                 // Kill warning about unused method
465                 DumpTypeInfo (null);
466         }
467
468         public override void WriteType (Type t, string ns, string fn)
469         {
470                 BindingFlags bf = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
471                 foreach (MethodInfo m in t.GetMethods (bf)) {
472                         if ((m.Attributes & MethodAttributes.PinvokeImpl) == 0)
473                                 continue;
474                         DllImportAttribute dia = GetDllImportInfo (m);
475                         if (dia == null) {
476                                 Console.WriteLine ("Unable to emit native prototype for P/Invoke " + 
477                                                 "method: {0}", m);
478                                 continue;
479                         }
480                         // we shouldn't declare prototypes for POSIX, etc. functions.
481                         if (dia.Value != "MonoPosixHelper" || IsOnExcludeList (dia.EntryPoint))
482                                 continue;
483                         methods [dia.EntryPoint] = m;
484                         RecordStructs (m);
485                 }
486         }
487
488         private static DllImportAttribute GetDllImportInfo (MethodInfo method)
489         {
490                 // .NET 2.0 synthesizes pseudo-attributes such as DllImport
491                 DllImportAttribute dia = (DllImportAttribute) Attribute.GetCustomAttribute (method, 
492                                         typeof(DllImportAttribute), false);
493                 if (dia != null)
494                         return dia;
495
496                 // We're not on .NET 2.0; assume we're on Mono and use some internal
497                 // methods...
498                 Type MonoMethod = Type.GetType ("System.Reflection.MonoMethod", false);
499                 if (MonoMethod == null) {
500                         Console.WriteLine ("cannot find MonoMethod");
501                         return null;
502                 }
503                 MethodInfo GetDllImportAttribute = 
504                         MonoMethod.GetMethod ("GetDllImportAttribute", 
505                                         BindingFlags.Static | BindingFlags.NonPublic);
506                 if (GetDllImportAttribute == null) {
507                         Console.WriteLine ("cannot find GetDllImportAttribute");
508                         return null;
509                 }
510                 IntPtr mhandle = method.MethodHandle.Value;
511                 return (DllImportAttribute) GetDllImportAttribute.Invoke (null, 
512                                 new object[]{mhandle});
513         }
514
515         private static string[] ExcludeList = new string[]{
516                 "Mono_Posix_Stdlib_snprintf",
517         };
518
519         private bool IsOnExcludeList (string method)
520         {
521                 int idx = Array.BinarySearch (ExcludeList, method);
522                 return (idx >= 0 && idx < ExcludeList.Length) ? true : false;
523         }
524
525         private void RecordStructs (MethodInfo method)
526         {
527                 ParameterInfo[] parameters = method.GetParameters ();
528                 foreach (ParameterInfo pi in parameters) {
529                         string s = GetNativeType (pi.ParameterType);
530                         if (s.StartsWith ("struct"))
531                                 structs [s] = s;
532                 }
533         }
534
535         public override void CloseFile (string file_prefix)
536         {
537                 icall.WriteLine ("/*\n * Structure Declarations\n */");
538                 foreach (string s in Sort (structs.Keys))
539                         icall.WriteLine ("{0};", s.Replace ("*", ""));
540
541                 icall.WriteLine ();
542
543                 icall.WriteLine ("/*\n * Function Declarations\n */");
544                 foreach (string method in Sort (methods.Keys)) {
545                         WriteMethodDeclaration ((MethodInfo) methods [method], method);
546                 }
547
548                 icall.WriteLine ("\nG_END_DECLS\n");
549                 icall.WriteLine ("#endif /* ndef INC_Mono_Posix_" + file_prefix + "_ICALLS_H */\n");
550                 icall.Close ();
551         }
552
553         private static IEnumerable Sort (ICollection c)
554         {
555                 ArrayList al = new ArrayList (c);
556                 al.Sort ();
557                 return al;
558         }
559
560         private void WriteMethodDeclaration (MethodInfo method, string entryPoint)
561         {
562                 icall.Write ("{0} ", GetNativeType (method.ReturnType));
563                 icall.Write ("{0} ", entryPoint);
564                 ParameterInfo[] parameters = method.GetParameters();
565                 if (parameters.Length == 0) {
566                         icall.WriteLine ("(void);");
567                         return;
568                 }
569                 if (parameters.Length > 0) {
570                         icall.Write ("(");
571                         WriteParameterDeclaration (parameters [0]);
572                 }
573                 for (int i = 1; i < parameters.Length; ++i) {
574                         icall.Write (", ");
575                         WriteParameterDeclaration (parameters [i]);
576                 }
577                 icall.WriteLine (");");
578         }
579
580         private void DumpTypeInfo (Type t)
581         {
582                 if (t == null)
583                         return;
584
585                 icall.WriteLine ("\t\t/* Type Info for " + t.FullName + ":");
586                 foreach (MemberInfo mi in typeof(Type).GetMembers()) {
587                         icall.WriteLine ("\t\t\t{0}={1}", mi.Name, GetMemberValue (mi, t));
588                 }
589                 icall.WriteLine ("\t\t */");
590         }
591
592         private static string GetMemberValue (MemberInfo mi, Type t)
593         {
594                 try {
595                 switch (mi.MemberType) {
596                         case MemberTypes.Constructor:
597                         case MemberTypes.Method: {
598                                 MethodBase b = (MethodBase) mi;
599                                 if (b.GetParameters().Length == 0)
600                                         return b.Invoke (t, new object[]{}).ToString();
601                                 return "<<cannot invoke>>";
602                         }
603                         case MemberTypes.Field:
604                                 return ((FieldInfo) mi).GetValue (t).ToString ();
605                         case MemberTypes.Property: {
606                                 PropertyInfo pi = (PropertyInfo) mi;
607                                 if (!pi.CanRead)
608                                         return "<<cannot read>>";
609                                 return pi.GetValue (t, null).ToString ();
610                         }
611                         default:
612                                 return "<<unknown value>>";
613                 }
614                 }
615                 catch (Exception e) {
616                         return "<<exception reading member: " + e.Message + ">>";
617                 }
618         }
619
620         private void WriteParameterDeclaration (ParameterInfo pi)
621         {
622                 // DumpTypeInfo (pi.ParameterType);
623                 icall.Write ("{0} {1}", GetNativeType (pi.ParameterType), pi.Name);
624         }
625 }
626
627 // vim: noexpandtab