[vala/wip/attributes: 16/100] codegen: Add get_ccode_free_function



commit 32d3d225ad76505de5de15c1cca9ba2cba55410e
Author: Luca Bruno <lucabru src gnome org>
Date:   Tue Jun 28 09:20:48 2011 +0200

    codegen: Add get_ccode_free_function

 codegen/valaccodebasemodule.vala      |   46 +++++++++++++++++++++++++++-----
 codegen/valaccodestructmodule.vala    |    4 +-
 codegen/valatyperegisterfunction.vala |    2 +-
 3 files changed, 41 insertions(+), 11 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index e842d2e..744286c 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2663,7 +2663,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 			if (elements_require_free && element_destroy_func_expression is CCodeIdentifier) {
 				return new CCodeIdentifier (generate_collection_free_wrapper (type, (CCodeIdentifier) element_destroy_func_expression));
 			} else {
-				return new CCodeIdentifier (type.data_type.get_free_function ());
+				return new CCodeIdentifier (get_ccode_free_function (type.data_type));
 			}
 		} else if (type is ErrorType) {
 			return new CCodeIdentifier ("g_error_free");
@@ -2681,13 +2681,13 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 					if (cl != null && cl.is_gboxed) {
 						unref_function = generate_free_func_wrapper (type);
 					} else {
-						unref_function = type.data_type.get_free_function ();
+						unref_function = get_ccode_free_function (type.data_type);
 					}
 				}
 			} else {
 				if (type.nullable) {
-					unref_function = type.data_type.get_free_function ();
-					if (unref_function == null) {
+					unref_function = get_ccode_free_function (type.data_type);
+					if (unref_function == "") {
 						if (type.data_type is Struct && ((Struct) type.data_type).is_disposable ()) {
 							unref_function = generate_free_func_wrapper (type);
 						} else {
@@ -2702,7 +2702,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 					unref_function = st.get_destroy_function ();
 				}
 			}
-			if (unref_function == null) {
+			if (unref_function == "") {
 				return new CCodeConstant ("NULL");
 			}
 			return new CCodeIdentifier (unref_function);
@@ -2731,7 +2731,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 	}
 
 	private string generate_collection_free_wrapper (DataType collection_type, CCodeIdentifier element_destroy_func_expression) {
-		string destroy_func = "_%s_%s".printf (collection_type.data_type.get_free_function (), element_destroy_func_expression.name);
+		string destroy_func = "_%s_%s".printf (get_ccode_free_function (collection_type.data_type), element_destroy_func_expression.name);
 
 		if (!add_wrapper (destroy_func)) {
 			// wrapper already defined
@@ -2786,7 +2786,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
 		ccode.add_expression (element_free_call);
 
-		var cfreecall = new CCodeFunctionCall (new CCodeIdentifier (collection_type.data_type.get_free_function ()));
+		var cfreecall = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_free_function (collection_type.data_type)));
 		cfreecall.add_argument (new CCodeIdentifier ("self"));
 		ccode.add_expression (cfreecall);
 
@@ -2955,7 +2955,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		var cassign = new CCodeAssignment (cvar, ccomma);
 
 		// g_free (NULL) is allowed
-		bool uses_gfree = (type.data_type != null && !type.data_type.is_reference_counting () && type.data_type.get_free_function () == "g_free");
+		bool uses_gfree = (type.data_type != null && !type.data_type.is_reference_counting () && get_ccode_free_function (type.data_type) == "g_free");
 		uses_gfree = uses_gfree || type is ArrayType;
 		if (uses_gfree) {
 			return cassign;
@@ -5642,6 +5642,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		return get_ccode_attribute(sym).copy_function;
 	}
 
+	public static string get_ccode_free_function (TypeSymbol sym) {
+		return get_ccode_attribute(sym).free_function;
+	}
+
 	public override void visit_class (Class cl) {
 	}
 
@@ -6113,6 +6117,15 @@ public class Vala.CCodeAttribute : AttributeCache {
 		}
 	}
 
+	public string free_function {
+		get {
+			if (_free_function == null) {
+				_free_function = get_default_free_function ();
+			}
+			return _free_function;
+		}
+	}
+
 	private string _name;
 	private string _const_name;
 	private string _type_name;
@@ -6125,6 +6138,7 @@ public class Vala.CCodeAttribute : AttributeCache {
 	private string _unref_function;
 	private string _ref_sink_function;
 	private string _copy_function;
+	private string _free_function;
 
 	public CCodeAttribute (CodeNode node) {
 		this.node = node;
@@ -6155,6 +6169,7 @@ public class Vala.CCodeAttribute : AttributeCache {
 			_unref_function = attr.get_string ("unref_function");
 			_ref_sink_function = attr.get_string ("ref_sink_function");
 			_copy_function = attr.get_string ("copy_function");
+			_free_function = attr.get_string ("free_function");
 		}
 	}
 
@@ -6407,4 +6422,19 @@ public class Vala.CCodeAttribute : AttributeCache {
 		}
 		return "";
 	}
+
+	private string get_default_free_function () {
+		if (sym is Class) {
+			var cl = (Class) sym;
+			if (cl.base_class != null) {
+				return CCodeBaseModule.get_ccode_free_function (cl.base_class);
+			}
+			return lower_case_prefix + "free";
+		} else if (sym is Struct) {
+			if (!sym.external_package) {
+				return lower_case_prefix + "free";
+			}
+		}
+		return "";
+	}
 }
diff --git a/codegen/valaccodestructmodule.vala b/codegen/valaccodestructmodule.vala
index 9df2690..e3f0cf1 100644
--- a/codegen/valaccodestructmodule.vala
+++ b/codegen/valaccodestructmodule.vala
@@ -120,7 +120,7 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
 		function.add_parameter (new CCodeParameter ("self", "const " + get_ccode_name (st) + "*"));
 		decl_space.add_function_declaration (function);
 
-		function = new CCodeFunction (st.get_free_function (), "void");
+		function = new CCodeFunction (get_ccode_free_function (st), "void");
 		if (st.is_private_symbol ()) {
 			function.modifiers = CCodeModifiers.STATIC;
 		}
@@ -226,7 +226,7 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
 	}
 
 	void add_struct_free_function (Struct st) {
-		var function = new CCodeFunction (st.get_free_function (), "void");
+		var function = new CCodeFunction (get_ccode_free_function (st), "void");
 		if (st.access == SymbolAccessibility.PRIVATE) {
 			function.modifiers = CCodeModifiers.STATIC;
 		}
diff --git a/codegen/valatyperegisterfunction.vala b/codegen/valatyperegisterfunction.vala
index a4d449a..e79b995 100644
--- a/codegen/valatyperegisterfunction.vala
+++ b/codegen/valatyperegisterfunction.vala
@@ -163,7 +163,7 @@ public abstract class Vala.TypeRegisterFunction {
 		if (get_type_declaration () is Struct) {
 			var st = (Struct) get_type_declaration ();
 			reg_call.add_argument (new CCodeCastExpression (new CCodeIdentifier (st.get_dup_function ()), "GBoxedCopyFunc"));
-			reg_call.add_argument (new CCodeCastExpression (new CCodeIdentifier (st.get_free_function ()), "GBoxedFreeFunc"));
+			reg_call.add_argument (new CCodeCastExpression (new CCodeIdentifier (CCodeBaseModule.get_ccode_free_function (st)), "GBoxedFreeFunc"));
 		} else if (get_type_declaration () is Enum) {
 			var en = get_type_declaration () as Enum;
 			var clist = new CCodeInitializerList (); /* or during visit time? */



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