[vala/wip/attributes: 11/13] codegen: Add get_ccode_unref_function



commit ad29ff0e5d9ed79990f605323e36fc02f455e1f1
Author: Luca Bruno <lucabru src gnome org>
Date:   Sun Jun 26 16:32:36 2011 +0200

    codegen: Add get_ccode_unref_function

 codegen/valaccodebasemodule.vala   |   40 +++++++++++++++++++++++++++++++++--
 codegen/valagdbusservermodule.vala |    2 +-
 codegen/valagtypemodule.vala       |    4 +-
 3 files changed, 40 insertions(+), 6 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 7c630d6..77aec32 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2676,8 +2676,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 			string unref_function;
 			if (type is ReferenceType) {
 				if (type.data_type.is_reference_counting ()) {
-					unref_function = type.data_type.get_unref_function ();
-					if (type.data_type is Interface && unref_function == null) {
+					unref_function = get_ccode_unref_function (type.data_type);
+					if (type.data_type is Interface && unref_function == "") {
 						Report.error (type.source_reference, "missing class prerequisite for interface `%s', add GLib.Object to interface declaration if unsure".printf (type.data_type.get_full_name ()));
 						return null;
 					}
@@ -3694,7 +3694,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
 		var cl = type.data_type as Class;
 		if (cl != null && cl.is_reference_counting ()
-		    && cl.get_unref_function () == "") {
+		    && get_ccode_unref_function (cl) == "") {
 			// empty unref_function => no unref necessary
 			return false;
 		}
@@ -5624,6 +5624,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		return get_ccode_attribute(sym).ref_function;
 	}
 
+	public static string get_ccode_unref_function (ObjectTypeSymbol sym) {
+		return get_ccode_attribute(sym).unref_function;
+	}
+
 	public override void visit_class (Class cl) {
 	}
 
@@ -6050,6 +6054,15 @@ public class Vala.CCodeAttribute : AttributeCache {
 		}
 	}
 
+	public string unref_function {
+		get {
+			if (_unref_function == null) {
+				_unref_function = get_default_unref_function ();
+			}
+			return _unref_function;
+		}
+	}
+
 	private string _name;
 	private string _const_name;
 	private string _type_name;
@@ -6058,6 +6071,7 @@ public class Vala.CCodeAttribute : AttributeCache {
 	private string _lower_case_prefix;
 	private string _lower_case_suffix;
 	private string _ref_function;
+	private string _unref_function;
 
 	public CCodeAttribute (CodeNode node) {
 		this.node = node;
@@ -6082,6 +6096,7 @@ public class Vala.CCodeAttribute : AttributeCache {
 			}
 			_lower_case_suffix = attr.get_string ("lower_case_csuffix");
 			_ref_function = attr.get_string ("ref_function");
+			_unref_function = attr.get_string ("unref_function");
 		}
 	}
 
@@ -6302,4 +6317,23 @@ public class Vala.CCodeAttribute : AttributeCache {
 		}
 		return "";
 	}
+
+	private string get_default_unref_function () {
+		if (sym is Class) {
+			var cl = (Class) sym;
+			if (cl.is_fundamental ()) {
+				return lower_case_prefix + "unref";
+			} else if (cl.base_class != null) {
+				return CCodeBaseModule.get_ccode_unref_function (cl.base_class);
+			}
+		} else if (sym is Interface) {
+			foreach (DataType prerequisite in prerequisites) {
+				string unref_func = CCodeBaseModule.get_ccode_unref_function (prerequisite.data_type);
+				if (unref_func != "") {
+					return unref_func;
+				}
+			}
+		}
+		return "";
+	}
 }
diff --git a/codegen/valagdbusservermodule.vala b/codegen/valagdbusservermodule.vala
index b21e97e..f875cae 100644
--- a/codegen/valagdbusservermodule.vala
+++ b/codegen/valagdbusservermodule.vala
@@ -1277,7 +1277,7 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 
 		ccode.add_declaration ("gpointer*", new CCodeVariableDeclarator ("data", new CCodeIdentifier ("user_data")));
 
-		var unref_object = new CCodeFunctionCall (new CCodeIdentifier (sym.get_unref_function ()));
+		var unref_object = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_unref_function (sym)));
 		unref_object.add_argument (new CCodeElementAccess (new CCodeIdentifier ("data"), new CCodeConstant ("0")));
 		ccode.add_expression (unref_object);
 
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index 732d845..8c74e31 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -998,7 +998,7 @@ public class Vala.GTypeModule : GErrorModule {
 		ccode.close ();
 
 		ccode.open_if (new CCodeIdentifier ("old"));
-		ccall = new CCodeFunctionCall (new CCodeIdentifier (cl.get_unref_function ()));
+		ccall = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_unref_function (cl)));
 		ccall.add_argument (new CCodeIdentifier ("old"));
 		ccode.add_expression (ccall);
 		ccode.close ();
@@ -1063,7 +1063,7 @@ public class Vala.GTypeModule : GErrorModule {
 		ccode.close ();
 
 		ccode.open_if (new CCodeIdentifier ("old"));
-		ccall = new CCodeFunctionCall (new CCodeIdentifier (cl.get_unref_function ()));
+		ccall = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_unref_function (cl)));
 		ccall.add_argument (new CCodeIdentifier ("old"));
 		ccode.add_expression (ccall);
 		ccode.close ();



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