[vala/0.40] codegen: Add default_value for CType to initialize variables if needed



commit 2cdc8c20311ac3a7ea312afa0dc63237a0efe8b8
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Dec 30 18:19:44 2018 +0100

    codegen: Add default_value for CType to initialize variables if needed
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/724

 codegen/valaccodebasemodule.vala       |  2 ++
 codegen/valaccodemethodcallmodule.vala |  4 ++--
 codegen/valactype.vala                 | 10 ++++++++--
 codegen/valagtypemodule.vala           |  2 +-
 4 files changed, 13 insertions(+), 5 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index afd007dae..750e7c317 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -6167,6 +6167,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 2b3f5ca68..8f039e067 100644
--- a/codegen/valaccodemethodcallmodule.vala
+++ b/codegen/valaccodemethodcallmodule.vala
@@ -431,7 +431,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
                                                        var array_type = (ArrayType) param.variable_type;
                                                        var array_length_type = int_type;
                                                        if (get_ccode_array_length_type (param) != null) {
-                                                               array_length_type = new CType 
(get_ccode_array_length_type (param));
+                                                               array_length_type = new CType 
(get_ccode_array_length_type (param), "0");
                                                        }
                                                        for (int dim = 1; dim <= array_type.rank; dim++) {
                                                                var temp_array_length = get_temp_variable 
(array_length_type);
@@ -525,7 +525,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
                                        if (get_ccode_array_length_type (m) == null) {
                                                temp_var = get_temp_variable (int_type, true, null, true);
                                        } else {
-                                               temp_var = get_temp_variable (new CType 
(get_ccode_array_length_type (m)), true, null, true);
+                                               temp_var = get_temp_variable (new CType 
(get_ccode_array_length_type (m), "0"), true, null, true);
                                        }
                                        var temp_ref = get_variable_cexpression (temp_var.name);
 
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 77725620e..88f46828a 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -2349,7 +2349,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"));


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