]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Add --target-glib command-line option, default to 2.12, based on patch by
authorJürg Billeter <j@bitron.ch>
Sun, 10 Aug 2008 14:39:24 +0000 (14:39 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Sun, 10 Aug 2008 14:39:24 +0000 (14:39 +0000)
2008-08-10  Jürg Billeter  <j@bitron.ch>

* vala/valacodecontext.vala:
* gobject/valaccodeclassbinding.vala:
* gobject/valaccodeinterfacebinding.vala:
* gobject/valaclassregisterfunction.vala:
* gobject/valainterfaceregisterfunction.vala:
* gobject/valatyperegisterfunction.vala:
* compiler/valacompiler.vala:

Add --target-glib command-line option, default to 2.12,
based on patch by Jared Moore, fixes bug 544990

svn path=/trunk/; revision=1746

ChangeLog
compiler/valacompiler.vala
gobject/valaccodeclassbinding.vala
gobject/valaccodeinterfacebinding.vala
gobject/valaclassregisterfunction.vala
gobject/valainterfaceregisterfunction.vala
gobject/valatyperegisterfunction.vala
vala/valacodecontext.vala

index a9a9c5c28cd42daeb54c0c1363c60f7130473558..fd5b6ec792fd0897a83cd59ad7535a450ca8cb12 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2008-08-10  Jürg Billeter  <j@bitron.ch>
+
+       * vala/valacodecontext.vala:
+       * gobject/valaccodeclassbinding.vala:
+       * gobject/valaccodeinterfacebinding.vala:
+       * gobject/valaclassregisterfunction.vala:
+       * gobject/valainterfaceregisterfunction.vala:
+       * gobject/valatyperegisterfunction.vala:
+       * compiler/valacompiler.vala:
+
+       Add --target-glib command-line option, default to 2.12,
+       based on patch by Jared Moore, fixes bug 544990
+
 2008-08-10  Jürg Billeter  <j@bitron.ch>
 
        * vala/valainterfacewriter.vala:
index b806244e6777cedf1e46c250fdc6a7443c4288c5..69da0b35519f813e165d25e55879109f39f9d72a 100644 (file)
@@ -34,6 +34,7 @@ class Vala.Compiler : Object {
        static string library;
        [NoArrayLength ()]
        static string[] packages;
+       static string target_glib;
 
        static bool ccode_only;
        static bool compile_only;
@@ -75,6 +76,7 @@ class Vala.Compiler : Object {
                { "Xcc", 'X', 0, OptionArg.STRING_ARRAY, ref cc_options, "Pass OPTION to the C compiler", "OPTION..." },
                { "save-temps", 0, 0, OptionArg.NONE, ref save_temps, "Keep temporary files", null },
                { "quiet", 'q', 0, OptionArg.NONE, ref quiet_mode, "Do not print messages to the console", null },
+               { "target-glib", 0, 0, OptionArg.STRING, ref target_glib, "Target version of glib for code generation", "MAJOR.MINOR" },
                { "", 0, 0, OptionArg.FILENAME_ARRAY, ref sources, null, "FILE..." },
                { null }
        };
@@ -168,6 +170,18 @@ class Vala.Compiler : Object {
                context.thread = thread;
                context.save_temps = save_temps;
 
+               int glib_major = 2;
+               int glib_minor = 12;
+               if (target_glib != null && target_glib.scanf ("%d.%d", out glib_major, out glib_minor) != 2) {
+                       Report.error (null, "Invalid format for --target-glib");
+               }
+
+               context.target_glib_major = glib_major;
+               context.target_glib_minor = glib_minor;
+               if (context.target_glib_major != 2) {
+                       Report.error (null, "This version of valac only supports GLib 2");
+               }
+
                if (defines != null) {
                        foreach (string define in defines) {
                                context.add_define (define);
index 63ec3e8c8decab6b59ceb18ed013f4c7e86e21b6..07248fbf2387da30cf16c763a6c1aeb015465964 100644 (file)
@@ -179,7 +179,7 @@ public class Vala.CCodeClassBinding : CCodeObjectTypeSymbolBinding {
                                }
                        }
 
-                       var type_fun = new ClassRegisterFunction (cl);
+                       var type_fun = new ClassRegisterFunction (cl, codegen);
                        type_fun.init_from_type (codegen.in_plugin);
                        if (cl.access != SymbolAccessibility.PRIVATE) {
                                codegen.header_type_member_declaration.append (type_fun.get_declaration ());
index 89f3359111e3f69ea1eb50ce48374a9edd3b3124..1945cfcb5a44b6ab44c19b9a0da7c15a3d4b285a 100644 (file)
@@ -87,7 +87,7 @@ public class Vala.CCodeInterfaceBinding : CCodeObjectTypeSymbolBinding {
                if (!iface.is_static) {
                        add_interface_base_init_function (iface);
 
-                       var type_fun = new InterfaceRegisterFunction (iface);
+                       var type_fun = new InterfaceRegisterFunction (iface, codegen);
                        type_fun.init_from_type ();
                        if (iface.access != SymbolAccessibility.PRIVATE) {
                                codegen.header_type_member_declaration.append (type_fun.get_declaration ());
index 5e97b8044b2ff84a0b0c7b50e573ea4cc4a4fad6..7c6d84daf846fceb04dea00fbe01cb3024dcb0b5 100644 (file)
@@ -37,8 +37,9 @@ public class Vala.ClassRegisterFunction : TypeRegisterFunction {
         * @param cl a class
         * @return   newly created class register function
         */
-       public ClassRegisterFunction (Class cl) {
+       public ClassRegisterFunction (Class cl, CCodeGenerator codegen) {
                class_reference = cl;
+               this.codegen = codegen;
        }
        
        public override TypeSymbol get_type_declaration () {
@@ -119,7 +120,7 @@ public class Vala.ClassRegisterFunction : TypeRegisterFunction {
                        var iface_info_name = "%s_info".printf (iface.get_lower_case_cname (null));
                        
                        var reg_call = new CCodeFunctionCall (new CCodeIdentifier ("g_type_add_interface_static"));
-                       reg_call.add_argument (new CCodeIdentifier ("%s_type_id_temp".printf (class_reference.get_lower_case_cname (null))));
+                       reg_call.add_argument (new CCodeIdentifier ("%s_type_id".printf (class_reference.get_lower_case_cname (null))));
                        reg_call.add_argument (new CCodeIdentifier (iface.get_type_id ()));
                        reg_call.add_argument (new CCodeIdentifier ("&%s".printf (iface_info_name)));
                        frag.append (new CCodeExpressionStatement (reg_call));
index b56d141471307d13f77df8c71e31e6ab4dd95600..9b66fa1ae786fcebeb6edbf5e1acb9f24d54b039 100644 (file)
@@ -32,8 +32,9 @@ public class Vala.InterfaceRegisterFunction : TypeRegisterFunction {
         */
        public weak Interface interface_reference { get; set; }
        
-       public InterfaceRegisterFunction (Interface iface) {
+       public InterfaceRegisterFunction (Interface iface, CCodeGenerator codegen) {
                interface_reference = iface;
+               this.codegen = codegen;
        }
        
        public override TypeSymbol get_type_declaration () {
@@ -76,7 +77,7 @@ public class Vala.InterfaceRegisterFunction : TypeRegisterFunction {
                        var prereq = prereq_ref.data_type;
                        
                        var func = new CCodeFunctionCall (new CCodeIdentifier ("g_type_interface_add_prerequisite"));
-                       func.add_argument (new CCodeIdentifier ("%s_type_id_temp".printf (interface_reference.get_lower_case_cname (null))));
+                       func.add_argument (new CCodeIdentifier ("%s_type_id".printf (interface_reference.get_lower_case_cname (null))));
                        func.add_argument (new CCodeIdentifier (prereq.get_type_id()));
                        
                        frag.append (new CCodeExpressionStatement (func));
index 7e627dd87ffdc057520f3fa7ec8db50c6682c76e..f2337d0d18b0c7a258fc8adbe952e6ee19e2c359 100644 (file)
@@ -30,10 +30,14 @@ public abstract class Vala.TypeRegisterFunction : Object {
 
        private CCodeFragment definition_fragment = new CCodeFragment ();
 
+       public CCodeGenerator codegen { get; set; }
+
        /**
         * Constructs the C function from the specified type.
         */
        public void init_from_type (bool plugin = false) {
+               bool use_thread_safe = codegen.context.require_glib_version (2, 14);
+
                bool fundamental = false;
                Class cl = get_type_declaration () as Class;
                if (cl != null && !cl.is_compact && cl.base_class == null) {
@@ -43,9 +47,18 @@ public abstract class Vala.TypeRegisterFunction : Object {
                string type_id_name = "%s_type_id".printf (get_type_declaration ().get_lower_case_cname (null));
 
                var type_block = new CCodeBlock ();
-               var cdecl = new CCodeDeclaration ("gsize");
-               cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer (type_id_name, new CCodeConstant ("0")));
-               cdecl.modifiers = CCodeModifiers.STATIC | CCodeModifiers.VOLATILE;
+               CCodeDeclaration cdecl;
+               if (use_thread_safe) {
+                       cdecl = new CCodeDeclaration ("gsize");
+                       cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer (type_id_name + "__volatile", new CCodeConstant ("0")));
+               } else {
+                       cdecl = new CCodeDeclaration ("GType");
+                       cdecl.add_declarator (new CCodeVariableDeclarator.with_initializer (type_id_name, new CCodeConstant ("0")));
+               }
+               cdecl.modifiers = CCodeModifiers.STATIC;
+               if (use_thread_safe) {
+                       cdecl.modifiers |= CCodeModifiers.VOLATILE;
+               }
                if (!plugin) {
                        type_block.add_statement (cdecl);
                } else {
@@ -111,10 +124,9 @@ public abstract class Vala.TypeRegisterFunction : Object {
                }
                reg_call.add_argument (new CCodeConstant (get_type_flags ()));
 
-               string temp_type_id_name = "%s_type_id_temp".printf (get_type_declaration ().get_lower_case_cname (null));
-               if (!plugin) {
+               if (use_thread_safe && !plugin) {
                        var temp_decl = new CCodeDeclaration ("GType");
-                       temp_decl.add_declarator (new CCodeVariableDeclarator.with_initializer (temp_type_id_name, reg_call));
+                       temp_decl.add_declarator (new CCodeVariableDeclarator.with_initializer (type_id_name, reg_call));
                        type_init.add_statement (temp_decl);
                } else {
                        type_init.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeIdentifier (type_id_name), reg_call)));
@@ -123,19 +135,33 @@ public abstract class Vala.TypeRegisterFunction : Object {
                type_init.add_statement (get_type_interface_init_statements ());
 
                if (!plugin) {
-                       var enter = new CCodeFunctionCall (new CCodeIdentifier ("g_once_init_enter"));
-                       enter.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (type_id_name)));
-                       var leave = new CCodeFunctionCall (new CCodeIdentifier ("g_once_init_leave"));
-                       leave.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (type_id_name)));
-                       leave.add_argument (new CCodeIdentifier (temp_type_id_name));
-                       type_init.add_statement (new CCodeExpressionStatement (leave));
-                       var cif = new CCodeIfStatement (enter, type_init);
+                       CCodeExpression condition; // the condition that guards the type initialisation
+                       if (use_thread_safe) {
+                               var enter = new CCodeFunctionCall (new CCodeIdentifier ("g_once_init_enter"));
+                               enter.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (type_id_name + "__volatile")));
+                               condition = enter;
+
+                               var leave = new CCodeFunctionCall (new CCodeIdentifier ("g_once_init_leave"));
+                               leave.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (type_id_name + "__volatile")));
+                               leave.add_argument (new CCodeIdentifier (type_id_name));
+                               type_init.add_statement (new CCodeExpressionStatement (leave));
+                       } else {
+                               var id = new CCodeIdentifier (type_id_name);
+                               var zero = new CCodeConstant ("0");
+                               condition = new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, id, zero);
+                       }
+
+                       var cif = new CCodeIfStatement (condition, type_init);
                        type_block.add_statement (cif);
                } else {
                        type_block = type_init;
                }
 
-               type_block.add_statement (new CCodeReturnStatement (new CCodeIdentifier (type_id_name)));
+               if (use_thread_safe) {
+                       type_block.add_statement (new CCodeReturnStatement (new CCodeIdentifier (type_id_name + "__volatile")));
+               } else {
+                       type_block.add_statement (new CCodeReturnStatement (new CCodeIdentifier (type_id_name)));
+               }
 
                declaration_fragment.append (fun.copy ());
 
index f3f563f36b0bba90699324c3f09e638107d13003..c8c3fad75fb25abb38a4a2880851387facfcccf4 100644 (file)
@@ -110,6 +110,24 @@ public class Vala.CodeContext : Object {
         */
        public bool save_temps { get; set; }
 
+       /**
+        * Target major version number of glib for code generation.
+        */
+       public int target_glib_major { get; set; }
+
+       /**
+        * Target minor version number of glib for code generation.
+        */
+       public int target_glib_minor { get; set; }
+
+       /**
+        * Returns true if the target version of glib is greater than or 
+        * equal to the specified version.
+        */
+       public bool require_glib_version (int major, int minor) {
+               return (target_glib_major > major) || (target_glib_major == major && target_glib_minor >= minor);
+       }
+
        public bool save_csources {
                get { return save_temps; }
        }