2004-12-12 Alp Toker <alp@atoker.com>
authorAlp Toker <alp@mono-cvs.ximian.com>
Sun, 12 Dec 2004 03:04:21 +0000 (03:04 -0000)
committerAlp Toker <alp@mono-cvs.ximian.com>
Sun, 12 Dec 2004 03:04:21 +0000 (03:04 -0000)
  * cilc.cs:
  * Test.cs:
  * demo.c:
  First stab at return values, and associated updates to the demo.

svn path=/trunk/mcs/; revision=37649

mcs/tools/cilc/ChangeLog
mcs/tools/cilc/Test.cs
mcs/tools/cilc/cilc.cs
mcs/tools/cilc/demo.c

index 2c7f861fffdb7e48bc07e3ca09dc97617b590cbc..97e8f533b9eb2f7a0d8552346888a0065e2aebdf 100644 (file)
@@ -1,3 +1,10 @@
+2004-12-12  Alp Toker  <alp@atoker.com>
+
+  * cilc.cs:
+       * Test.cs:
+       * demo.c:
+       First stab at return values, and associated updates to the demo.
+
 2004-12-11  Alp Toker  <alp@atoker.com>
 
        * cilc.cs:
index 6ef6d1eedf0888b40824dd91ec3f69198f717f2a..ed3523cfface6003d4fbda01b67347a5bbd0c4f5 100644 (file)
@@ -49,6 +49,16 @@ namespace Demo
                        Console.WriteLine ("Instance method with an argument invoked: " + num + " added to value, making it " + counter);
                }
 
+               public int GetValue ()
+               {
+                       return counter;
+               }
+
+               public static Drink PickDrink ()
+               {
+                       return Drink.Water;
+               }
+
                public string Title
                {
                        get { return title; }
@@ -70,4 +80,11 @@ namespace Demo
                        Console.WriteLine ("c# method with an unusual name invoked");
                }
        }
+
+       public enum Drink
+       {
+               Water,
+               Juice,
+               Cola
+       }
 }
index eac84fc61ef9267edd0bb35cb5156c47530fe199..8920ecb0a795d264b756054195232815a194bf65 100644 (file)
@@ -30,6 +30,9 @@ public class cilc
 
                ns = "Unnamed";
 
+               RegisterByVal (typeof (uint));
+               RegisterByVal (typeof (int));
+
                if (args.Length == 1) {
                        SmartBind (args[0]);
                } else if (args.Length == 2) {
@@ -471,8 +474,8 @@ public class cilc
                Hdecls.WriteLine ();
                C.WriteLine ("{ 0, NULL, NULL }");
                C.WriteLine ("};");
+               C.WriteLine ("etype = g_enum_register_static (\"" + gname + "\", values);");
                C.WriteLine ("}");
-               C.WriteLine ("etype = g_enum_register_static (\"" + gname + "\", NULL);");
                C.WriteLine ("return etype;");
                C.WriteLine ("}");
        }
@@ -824,6 +827,9 @@ public class cilc
                if (IsRegisteredByVal (t))
                        return CsTypeToFlat (t) + " ";
 
+               if (t == typeof (void))
+                       return "void ";
+
                return CsTypeToG (t) + " *";
        }
 
@@ -876,42 +882,30 @@ public class cilc
        static void FunctionGen (ParameterInfo[] parameters, MethodBase m, Type t, Type ret_type, bool ctor)
        {
                string myargs = "";
+               bool has_return = !ctor && ret_type != null && ret_type != typeof (void);
+               bool stat = m.IsStatic;
 
-               bool has_return = false;
-               bool stat = false;
-               bool inst = false;
+               string mytype, rettype;
+               mytype = CurType + " *";
 
                if (ctor) {
                        has_return = true;
+                       rettype = mytype;
                        stat = true;
-               }
-               else {
-                       stat = m.IsStatic;
-               }
-
-               inst = !stat;
-
-               string mytype;
-               mytype = CurType + " *";
-
-               /*
-                        Console.WriteLine (ret_type);
-                        if (ret_type != null && ret_type != typeof (Void)) {
-                        has_return = true;
-               //TODO: return simple gint or gchar if possible
-               mytype = "MonoObject *";
-               }
-               */
+               } else
+                               rettype = CsTypeToC (ret_type);
 
                string params_arg = "NULL";
                if (parameters.Length != 0)
                        params_arg = "_mono_params";
 
                string instance = "thiz";
-               string instance_arg = instance + "->priv->mono_object";
+               string instance_arg = "NULL";
+        
+               if (ctor || !stat)
+                       instance_arg = instance + "->priv->mono_object";
 
-               //TODO: also check, !static
-               if (inst) {
+               if (!stat) {
                        myargs = mytype + instance;
                        if (parameters.Length > 0) myargs += ", ";
                }
@@ -963,12 +957,13 @@ public class cilc
                        }
                }
 
-               if (myargs == "") myargs = "void";
+               if (myargs == "")
+                       myargs = "void";
 
                C.WriteLine ();
 
                if (has_return)
-                       C.WriteLine (mytype + myname + " (" + myargs + ")", H, ";");
+                       C.WriteLine (rettype + myname + " (" + myargs + ")", H, ";");
                else
                        C.WriteLine ("void " + myname + " (" + myargs + ")", H, ";");
 
@@ -1002,23 +997,35 @@ public class cilc
                        ParameterInfo p = parameters[i];
                        C.WriteLine  (params_arg + "[" + i + "] = " + GetMonoVal (p.Name, p.ParameterType.ToString ()) + ";");
                }
-               if (parameters.Length != 0) C.WriteLine ();
 
-               if (ctor) C.WriteLine (instance_arg + " = (MonoObject*) mono_object_new ((MonoDomain*) " + NsToC (ns) + "_get_mono_domain ()" + ", " + cur_type + "_get_mono_class ());");
+               if (parameters.Length != 0)
+                       C.WriteLine ();
+
+               if (ctor)
+                       C.WriteLine (instance_arg + " = (MonoObject*) mono_object_new ((MonoDomain*) " + NsToC (ns) + "_get_mono_domain ()" + ", " + cur_type + "_get_mono_class ());");
 
                //delegates are a special case as we want their constructor to take a function pointer
                if (ctor && t.IsSubclassOf (typeof (MulticastDelegate))) {
                        C.WriteLine ("mono_delegate_ctor (" + instance_arg + ", object, method);");
                } else {
-                       //invoke the method
-
-                       if (ctor || inst)
-                               C.WriteLine ("mono_runtime_invoke (_mono_method, " + instance_arg + ", " + params_arg + ", NULL);");
+                       //code to invoke the method
+
+                       if (!ctor && has_return)
+                               if (IsRegisteredByVal (ret_type)) {
+                                       C.WriteLine ("{");
+                                       C.WriteLine (rettype + "* retval = (" + rettype + "*) mono_object_unbox (mono_runtime_invoke (_mono_method, " + instance_arg + ", " + params_arg + ", NULL));");
+                                       C.WriteLine ("return (" + rettype + ") *retval;");
+                                       C.WriteLine ("}");
+                               } else {
+                                       //TODO: this isn't right
+                                       C.WriteLine ("return (" + rettype + ") mono_runtime_invoke (_mono_method, " + instance_arg + ", " + params_arg + ", NULL);");
+                               }
                        else
-                               C.WriteLine ("mono_runtime_invoke (_mono_method, " + "NULL" + ", " + params_arg + ", NULL);");
+                               C.WriteLine ("mono_runtime_invoke (_mono_method, " + instance_arg + ", " + params_arg + ", NULL);");
                }
 
-               if (ctor) C.WriteLine ("return " + instance + ";");
+               if (ctor)
+                       C.WriteLine ("return " + instance + ";");
 
                C.WriteLine ("}");
        }
index 1b62b9e82d81c663808e5e668bcfb2a51796a018..879d26d473c71731171f3dc25b4fde9e40ec2ebb 100644 (file)
@@ -1,7 +1,13 @@
+#include <glib.h>
+#include <glib/gprintf.h>
 #include "demo.h"
 
 int main () {
   DemoTest *my_test;
+       //gchar *tmp;
+       int num;
+       DemoDrink drink;
+       //GEnumClass *enum_class;
   
   //run a static method
   demo_test_static_method ();
@@ -21,10 +27,19 @@ int main () {
   //run a property set accessor
   demo_test_set_title (my_test, "set property from c");
   
+       //run a property set accessor
+  //tmp = demo_test_get_title (my_test);
+       //g_print (tmp);
+  num = demo_test_get_value (my_test);
+       g_printf ("The counter's value is %d\n", num);
+  
+       drink = demo_test_pick_drink ();
+       //enum_class = g_type_class_peek (demo_drink_get_type ());
+       //g_enum_get_value (enum_class, drink);
+       //g_printf ("%d\n", drink);
+  
   //TODO: return value
   //g_printf ("returned string: %s\n", demo_test_get_title (my_test));
 
-  //TODO: gobject-style DEMO_IS_TEST etc. macros
-
-  return 1;
+  return 0;
 }