[vala/wip/attributes: 20/100] codegen: Add get_ccode_set_value_function



commit 6d9ccbf0db81cf0625e4ebb54a7bbd0f3e766c59
Author: Luca Bruno <lucabru src gnome org>
Date:   Tue Jun 28 14:27:52 2011 +0200

    codegen: Add get_ccode_set_value_function

 codegen/valaccodebasemodule.vala |   72 +++++++++++++++++++++++++++++++++++++-
 codegen/valagsignalmodule.vala   |    2 +-
 codegen/valagtypemodule.vala     |    4 +-
 3 files changed, 74 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index b6972e4..4248021 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -558,7 +558,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 	public CCodeIdentifier get_value_setter_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_set_value_function ());
+			return new CCodeIdentifier (get_ccode_set_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_set_boxed");
@@ -5665,6 +5665,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		return get_ccode_attribute(sym).get_value_function;
 	}
 
+	public static string get_ccode_set_value_function (CodeNode sym) {
+		return get_ccode_attribute(sym).set_value_function;
+	}
+
 	public override void visit_class (Class cl) {
 	}
 
@@ -6172,6 +6176,15 @@ public class Vala.CCodeAttribute : AttributeCache {
 		}
 	}
 
+	public string set_value_function {
+		get {
+			if (_set_value_function == null) {
+				_set_value_function = get_default_set_value_function ();
+			}
+			return _set_value_function;
+		}
+	}
+
 	private string _name;
 	private string _const_name;
 	private string _type_name;
@@ -6188,6 +6201,7 @@ public class Vala.CCodeAttribute : AttributeCache {
 	private string _type_id;
 	private string _marshaller_type_name;
 	private string _get_value_function;
+	private string _set_value_function;
 
 	public CCodeAttribute (CodeNode node) {
 		this.node = node;
@@ -6222,6 +6236,7 @@ public class Vala.CCodeAttribute : AttributeCache {
 			_type_id = attr.get_string ("type_id");
 			_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");
 		}
 	}
 
@@ -6664,4 +6679,59 @@ public class Vala.CCodeAttribute : AttributeCache {
 		}
 		return "";
 	}
+
+	private string get_default_set_value_function () {
+		if (sym is Class) {
+			var cl = (Class) sym;
+			if (cl.is_fundamental ()) {
+				return CCodeBaseModule.get_ccode_lower_case_cname (cl, "value_set_");
+			} else if (cl.base_class != null) {
+				return CCodeBaseModule.get_ccode_set_value_function (cl.base_class);
+			} else if (type_id == "G_TYPE_POINTER") {
+				return "g_value_set_pointer";
+			} else {
+				return "g_value_set_boxed";
+			}
+		} else if (sym is Enum) {
+			var en = (Enum) sym;
+			if (en.has_type_id) {
+				if (en.is_flags) {
+					return "g_value_set_flags";
+				} else {
+					return "g_value_set_enum";
+				}
+			} else {
+				if (en.is_flags) {
+					return "g_value_set_uint";
+				} else {
+					return "g_value_set_int";
+				}
+			}
+		} else if (sym is Interface) {
+			foreach (var prereq in ((Interface) sym).get_prerequisites ()) {
+				var type_name = CCodeBaseModule.get_ccode_set_value_function (prereq.data_type);
+				if (type_name != "") {
+					return type_name;
+				}
+			}
+			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_set_value_function (base_st);
+			}
+			if (st.is_simple_type ()) {
+				Report.error (st.source_reference, "The type `%s` doesn't declare a GValue set function".printf (st.get_full_name ()));
+			} else if (st.has_type_id) {
+				return "g_value_set_boxed";
+			} else {
+				return "g_value_set_pointer";
+			}
+		} else {
+			return "g_value_set_pointer";
+		}
+		return "";
+	}
+	
 }
diff --git a/codegen/valagsignalmodule.vala b/codegen/valagsignalmodule.vala
index 16cbb09..98ea9b4 100644
--- a/codegen/valagsignalmodule.vala
+++ b/codegen/valagsignalmodule.vala
@@ -288,7 +288,7 @@ public class Vala.GSignalModule : GObjectModule {
 			} else if (return_type.data_type is Class || return_type.data_type is Interface) {
 				set_fc = new CCodeFunctionCall (new CCodeIdentifier ("g_value_take_object"));
 			} else {
-				set_fc = new CCodeFunctionCall (new CCodeIdentifier (return_type.data_type.get_set_value_function ()));
+				set_fc = new CCodeFunctionCall (new CCodeIdentifier (get_ccode_set_value_function (return_type.data_type)));
 			}
 			set_fc.add_argument (new CCodeIdentifier ("return_value"));
 			set_fc.add_argument (new CCodeIdentifier ("v_return"));
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index 0c43f03..c917cc2 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -121,7 +121,7 @@ public class Vala.GTypeModule : GErrorModule {
 
 			decl_space.add_function_declaration (function);
 
-			function = new CCodeFunction (cl.get_set_value_function (), "void");
+			function = new CCodeFunction (get_ccode_set_value_function (cl), "void");
 			function.add_parameter (new CCodeParameter ("value", "GValue*"));
 			function.add_parameter (new CCodeParameter ("v_object", "gpointer"));
 
@@ -940,7 +940,7 @@ public class Vala.GTypeModule : GErrorModule {
 	}
 
 	private void add_g_value_set_function (Class cl) {
-		var function = new CCodeFunction (cl.get_set_value_function (), "void");
+		var function = new CCodeFunction (get_ccode_set_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]