[vala/wip/attributes: 24/100] codegen: Add get_ccode_default_value



commit 317d4dae1b1ba4c03251eeffb959ab766768120e
Author: Luca Bruno <lucabru src gnome org>
Date:   Tue Jun 28 15:07:54 2011 +0200

    codegen: Add get_ccode_default_value

 codegen/valaccodebasemodule.vala |   42 ++++++++++++++++++++++++++++++++++++-
 codegen/valagtypemodule.vala     |    2 +-
 2 files changed, 41 insertions(+), 3 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 7b69ca2..8ee55e7 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -5454,8 +5454,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		           || type is PointerType || type is DelegateType
 		           || (array_type != null && !array_type.fixed_length)) {
 			return new CCodeConstant ("NULL");
-		} else if (type.data_type != null && type.data_type.get_default_value () != null) {
-			return new CCodeConstant (type.data_type.get_default_value ());
+		} else if (type.data_type != null && get_ccode_default_value (type.data_type) != "") {
+			return new CCodeConstant (get_ccode_default_value (type.data_type));
 		} else if (type.type_parameter != null) {
 			return new CCodeConstant ("NULL");
 		} else if (type is ErrorType) {
@@ -5678,6 +5678,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		}
 	}
 
+	public static string get_ccode_default_value (TypeSymbol sym) {
+		return get_ccode_attribute(sym).default_value;
+	}
+
 	public override void visit_class (Class cl) {
 	}
 
@@ -6212,6 +6216,15 @@ public class Vala.CCodeAttribute : AttributeCache {
 		}
 	}
 
+	public string default_value {
+		get {
+			if (_default_value == null) {
+				_default_value = get_default_default_value ();
+			}
+			return _default_value;
+		}
+	}
+
 	private string _name;
 	private string _const_name;
 	private string _type_name;
@@ -6231,6 +6244,7 @@ public class Vala.CCodeAttribute : AttributeCache {
 	private string _set_value_function;
 	private string _take_value_function;
 	private string _param_spec_function;
+	private string _default_value;
 
 	public CCodeAttribute (CodeNode node) {
 		this.node = node;
@@ -6268,6 +6282,7 @@ public class Vala.CCodeAttribute : AttributeCache {
 			_set_value_function = attr.get_string ("set_value_function");
 			_take_value_function = attr.get_string ("take_value_function");
 			_param_spec_function = attr.get_string ("param_spec_function");
+			_default_value = attr.get_string ("default_value");
 		}
 	}
 
@@ -6842,4 +6857,27 @@ public class Vala.CCodeAttribute : AttributeCache {
 		}
 		return "";
 	}
+
+	private string get_default_default_value () {
+		if (sym is Enum) {
+			return "0";
+		}
+
+		var st = (Struct) sym;
+		var base_st = st.base_struct;
+
+		if (base_st != null) {
+			return CCodeBaseModule.get_ccode_default_value (base_st);
+		}
+
+		if (CodeContext.get ().profile == Profile.DOVA) {
+			if (st.is_boolean_type ()) {
+				return "false";
+			} else if (st.is_integer_type () || st.is_floating_type ()) {
+				return "0";
+			}
+		}
+
+		return "";
+	}
 }
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index 9c07e9f..fabc6ce 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -1695,7 +1695,7 @@ public class Vala.GTypeModule : GErrorModule {
 			if (prop.initializer != null) {
 				cspec.add_argument ((CCodeExpression) get_ccodenode (prop.initializer));
 			} else {
-				cspec.add_argument (new CCodeConstant (prop.property_type.data_type.get_default_value ()));
+				cspec.add_argument (new CCodeConstant (get_ccode_default_value (prop.property_type.data_type)));
 			}
 		} else if (prop.property_type.data_type is Struct) {
 			var st = (Struct) prop.property_type.data_type;



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