vala r1746 - in trunk: . compiler gobject vala



Author: juergbi
Date: Sun Aug 10 14:39:24 2008
New Revision: 1746
URL: http://svn.gnome.org/viewvc/vala?rev=1746&view=rev

Log:
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


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

Modified: trunk/compiler/valacompiler.vala
==============================================================================
--- trunk/compiler/valacompiler.vala	(original)
+++ trunk/compiler/valacompiler.vala	Sun Aug 10 14:39:24 2008
@@ -34,6 +34,7 @@
 	static string library;
 	[NoArrayLength ()]
 	static string[] packages;
+	static string target_glib;
 
 	static bool ccode_only;
 	static bool compile_only;
@@ -75,6 +76,7 @@
 		{ "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 @@
 		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);

Modified: trunk/gobject/valaccodeclassbinding.vala
==============================================================================
--- trunk/gobject/valaccodeclassbinding.vala	(original)
+++ trunk/gobject/valaccodeclassbinding.vala	Sun Aug 10 14:39:24 2008
@@ -179,7 +179,7 @@
 				}
 			}
 
-			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 ());

Modified: trunk/gobject/valaccodeinterfacebinding.vala
==============================================================================
--- trunk/gobject/valaccodeinterfacebinding.vala	(original)
+++ trunk/gobject/valaccodeinterfacebinding.vala	Sun Aug 10 14:39:24 2008
@@ -87,7 +87,7 @@
 		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 ());

Modified: trunk/gobject/valaclassregisterfunction.vala
==============================================================================
--- trunk/gobject/valaclassregisterfunction.vala	(original)
+++ trunk/gobject/valaclassregisterfunction.vala	Sun Aug 10 14:39:24 2008
@@ -37,8 +37,9 @@
 	 * @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 @@
 			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));

Modified: trunk/gobject/valainterfaceregisterfunction.vala
==============================================================================
--- trunk/gobject/valainterfaceregisterfunction.vala	(original)
+++ trunk/gobject/valainterfaceregisterfunction.vala	Sun Aug 10 14:39:24 2008
@@ -32,8 +32,9 @@
 	 */
 	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 @@
 			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));

Modified: trunk/gobject/valatyperegisterfunction.vala
==============================================================================
--- trunk/gobject/valatyperegisterfunction.vala	(original)
+++ trunk/gobject/valatyperegisterfunction.vala	Sun Aug 10 14:39:24 2008
@@ -30,10 +30,14 @@
 
 	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 @@
 		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 @@
 		}
 		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 @@
 		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 ());
 

Modified: trunk/vala/valacodecontext.vala
==============================================================================
--- trunk/vala/valacodecontext.vala	(original)
+++ trunk/vala/valacodecontext.vala	Sun Aug 10 14:39:24 2008
@@ -110,6 +110,24 @@
 	 */
 	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; }
 	}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]