summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2018-12-30 18:19:44 +0100
committerRico Tzschichholz <ricotz@ubuntu.com>2018-12-30 18:19:44 +0100
commit1746709f375c199c86809f4c725890387afdba62 (patch)
treec72a4234f6f48eca94cd7976fc22cd462fba50c4
parente7308eaba01ec6c478bb37e1c912a1b6d750bc2c (diff)
downloadvala-1746709f375c199c86809f4c725890387afdba62.tar.gz
codegen: Add default_value for CType to initialize variables if needed
Fixes https://gitlab.gnome.org/GNOME/vala/issues/724
-rw-r--r--codegen/valaccodebasemodule.vala2
-rw-r--r--codegen/valaccodemethodcallmodule.vala6
-rw-r--r--codegen/valactype.vala10
-rw-r--r--codegen/valagtypemodule.vala2
4 files changed, 14 insertions, 6 deletions
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 6662e1c45..d63c4ae52 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -6360,6 +6360,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
return new CCodeConstant ("NULL");
} else if (type is ErrorType) {
return new CCodeConstant ("NULL");
+ } else if (type is CType) {
+ return new CCodeConstant (((CType) type).cdefault_value);
}
return null;
}
diff --git a/codegen/valaccodemethodcallmodule.vala b/codegen/valaccodemethodcallmodule.vala
index d69df60cc..a2628a783 100644
--- a/codegen/valaccodemethodcallmodule.vala
+++ b/codegen/valaccodemethodcallmodule.vala
@@ -432,7 +432,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
var array_type = (ArrayType) param.variable_type;
var length_ctype = get_ccode_array_length_type (param) ?? get_ccode_array_length_type (array_type);
for (int dim = 1; dim <= array_type.rank; dim++) {
- var temp_array_length = get_temp_variable (new CType (length_ctype));
+ var temp_array_length = get_temp_variable (new CType (length_ctype, "0"), true, null, true);
emit_temp_var (temp_array_length);
append_array_length (arg, get_variable_cexpression (temp_array_length.name));
carg_map.set (get_param_pos (get_ccode_array_length_pos (param) + 0.01 * dim), new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_array_lengths (arg).get (dim - 1)));
@@ -519,7 +519,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
append_array_length (expr, len_call);
} else if (get_ccode_array_length (m)) {
var length_ctype = get_ccode_array_length_type (m) ?? get_ccode_array_length_type (array_type);
- var temp_var = get_temp_variable (new CType (length_ctype), true, null, true);
+ var temp_var = get_temp_variable (new CType (length_ctype, "0"), true, null, true);
var temp_ref = get_variable_cexpression (temp_var.name);
emit_temp_var (temp_var);
@@ -582,7 +582,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
append_array_length (expr, len_call);
} else if (get_ccode_array_length (deleg)) {
- var temp_var = get_temp_variable (new CType (get_ccode_array_length_type (array_type)), true, null, true);
+ var temp_var = get_temp_variable (array_type.length_type, true, null, true);
var temp_ref = get_variable_cexpression (temp_var.name);
emit_temp_var (temp_var);
diff --git a/codegen/valactype.vala b/codegen/valactype.vala
index 1190299cb..88894069e 100644
--- a/codegen/valactype.vala
+++ b/codegen/valactype.vala
@@ -29,11 +29,17 @@ public class Vala.CType : DataType {
*/
public string ctype_name { get; set; }
- public CType (string ctype_name) {
+ /**
+ * The default value of the C type.
+ */
+ public string cdefault_value { get; set; }
+
+ public CType (string ctype_name, string cdefault_value) {
this.ctype_name = ctype_name;
+ this.cdefault_value = cdefault_value;
}
public override DataType copy () {
- return new CType (ctype_name);
+ return new CType (ctype_name, cdefault_value);
}
}
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index 3dd0f2c48..017ac6a34 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -2380,7 +2380,7 @@ public class Vala.GTypeModule : GErrorModule {
expr.value_type.value_owned = true;
set_cvalue (expr, to_string);
} else {
- var temp_var = get_temp_variable (new CType (is_flags ? "GFlagsValue*" : "GEnumValue*"), false, expr, false);
+ var temp_var = get_temp_variable (new CType (is_flags ? "GFlagsValue*" : "GEnumValue*", "NULL"), false, expr, false);
emit_temp_var (temp_var);
var class_ref = new CCodeFunctionCall (new CCodeIdentifier ("g_type_class_ref"));