[vala/wip/attributes: 22/27] codegen: Add get_ccode_take_value_function



commit ee1761c47a99ba0d27bdcb221b2092ae8d78129d
Author: Luca Bruno <lucabru src gnome org>
Date:   Tue Jun 28 14:41:35 2011 +0200

    codegen: Add get_ccode_take_value_function

 codegen/valaccodebasemodule.vala |   72 ++++++++++++++++++++++++++++++++++++-
 codegen/valagtypemodule.vala     |    4 +-
 2 files changed, 72 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 573359e..dd75995 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -570,7 +570,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 	public CCodeIdentifier get_value_taker_function (DataType type_reference) {
 		var array_type = type_reference as ArrayType;
 		if (type_reference.data_type != null) {
-			return new CCodeIdentifier (type_reference.data_type.get_take_value_function ());
+			return new CCodeIdentifier (get_ccode_take_value_function (type_reference.data_type));
 		} else if (array_type != null && array_type.element_type.data_type == string_type.data_type) {
 			// G_TYPE_STRV
 			return new CCodeIdentifier ("g_value_take_boxed");
@@ -5662,6 +5662,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		return get_ccode_attribute(sym).set_value_function;
 	}
 
+	public static string get_ccode_take_value_function (CodeNode sym) {
+		return get_ccode_attribute(sym).take_value_function;
+	}
+
 	public override void visit_class (Class cl) {
 	}
 
@@ -6178,6 +6182,15 @@ public class Vala.CCodeAttribute : AttributeCache {
 		}
 	}
 
+	public string take_value_function {
+		get {
+			if (_take_value_function == null) {
+				_take_value_function = get_default_take_value_function ();
+			}
+			return _take_value_function;
+		}
+	}
+
 	private string _name;
 	private string _const_name;
 	private string _type_name;
@@ -6195,6 +6208,7 @@ public class Vala.CCodeAttribute : AttributeCache {
 	private string _marshaller_type_name;
 	private string _get_value_function;
 	private string _set_value_function;
+	private string _take_value_function;
 
 	public CCodeAttribute (CodeNode node) {
 		this.node = node;
@@ -6230,6 +6244,7 @@ public class Vala.CCodeAttribute : AttributeCache {
 			_marshaller_type_name = attr.get_string ("marshaller_type_name");
 			_get_value_function = attr.get_string ("get_value_function");
 			_set_value_function = attr.get_string ("set_value_function");
+			_take_value_function = attr.get_string ("take_value_function");
 		}
 	}
 
@@ -6707,5 +6722,58 @@ public class Vala.CCodeAttribute : AttributeCache {
 		}
 		return "";
 	}
-	
+
+	private string get_default_take_value_function () {
+		if (sym is Class) {
+			var cl = (Class) sym;
+			if (cl.is_fundamental ()) {
+				return CCodeBaseModule.get_ccode_lower_case_cname (cl, "value_take_");
+			} else if (cl.base_class != null) {
+				return CCodeBaseModule.get_ccode_take_value_function (cl.base_class);
+			} else if (type_id == "G_TYPE_POINTER") {
+				return "g_value_set_pointer";
+			} else {
+				return "g_value_take_boxed";
+			}
+		} else if (sym is Enum) {
+			var en = (Enum) sym;
+			if (en.has_type_id) {
+				if (en.is_flags) {
+					return "g_value_take_flags";
+				} else {
+					return "g_value_take_enum";
+				}
+			} else {
+				if (en.is_flags) {
+					return "g_value_take_uint";
+				} else {
+					return "g_value_take_int";
+				}
+			}
+		} else if (sym is Interface) {
+			foreach (var prereq in ((Interface) sym).get_prerequisites ()) {
+				var func = CCodeBaseModule.get_ccode_take_value_function (prereq.data_type);
+				if (func != "") {
+					return func;
+				}
+			}
+			return "g_value_set_pointer";
+		} else if (sym is Struct) {
+			var st = (Struct) sym;
+			var base_st = st.base_struct;
+			if (base_st != null) {
+				return CCodeBaseModule.get_ccode_take_value_function (base_st);
+			}
+			if (st.is_simple_type ()) {
+				Report.error (st.source_reference, "The type `%s` doesn't declare a GValue take function".printf (st.get_full_name ()));
+			} else if (st.has_type_id) {
+				return "g_value_take_boxed";
+			} else {
+				return "g_value_set_pointer";
+			}
+		} else {
+			return "g_value_set_pointer";
+		}
+		return "";
+	}
 }
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index e51e377..cd29fb0 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -133,7 +133,7 @@ public class Vala.GTypeModule : GErrorModule {
 
 			decl_space.add_function_declaration (function);
 
-			function = new CCodeFunction (cl.get_take_value_function (), "void");
+			function = new CCodeFunction (get_ccode_take_value_function (cl), "void");
 			function.add_parameter (new CCodeParameter ("value", "GValue*"));
 			function.add_parameter (new CCodeParameter ("v_object", "gpointer"));
 
@@ -1008,7 +1008,7 @@ public class Vala.GTypeModule : GErrorModule {
 	}
 
 	private void add_g_value_take_function (Class cl) {
-		var function = new CCodeFunction (cl.get_take_value_function (), "void");
+		var function = new CCodeFunction (get_ccode_take_value_function (cl), "void");
 		function.add_parameter (new CCodeParameter ("value", "GValue*"));
 		function.add_parameter (new CCodeParameter ("v_object", "gpointer"));
 



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