[vala/staging: 4/5] codegen: Factor out GValueModule



commit ce62f282d9b8da764119eee0ced74a273b2cff58
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Mon Mar 25 11:49:34 2019 +0100

    codegen: Factor out GValueModule

 codegen/Makefile.am              |   1 +
 codegen/valaccodebasemodule.vala |  99 +++++------------------------------
 codegen/valagvaluemodule.vala    | 110 +++++++++++++++++++++++++++++++++++++++
 codegen/valagvariantmodule.vala  |   2 +-
 4 files changed, 124 insertions(+), 88 deletions(-)
---
diff --git a/codegen/Makefile.am b/codegen/Makefile.am
index 63e75d9ba..87e0bae7a 100644
--- a/codegen/Makefile.am
+++ b/codegen/Makefile.am
@@ -51,6 +51,7 @@ libvalaccodegen_la_VALASOURCES = \
        valagtkmodule.vala \
        valagsignalmodule.vala \
        valagtypemodule.vala \
+       valagvaluemodule.vala \
        valagvariantmodule.vala \
        valainterfaceregisterfunction.vala \
        valastructregisterfunction.vala \
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 62b152222..33ab02657 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -696,42 +696,6 @@ 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.type_symbol != null) {
-                       return new CCodeIdentifier (get_ccode_set_value_function 
(type_reference.type_symbol));
-               } else if (array_type != null && array_type.element_type.type_symbol == 
string_type.type_symbol) {
-                       // G_TYPE_STRV
-                       return new CCodeIdentifier ("g_value_set_boxed");
-               } else {
-                       return new CCodeIdentifier ("g_value_set_pointer");
-               }
-       }
-
-       public CCodeIdentifier get_value_taker_function (DataType type_reference) {
-               var array_type = type_reference as ArrayType;
-               if (type_reference.type_symbol != null) {
-                       return new CCodeIdentifier (get_ccode_take_value_function 
(type_reference.type_symbol));
-               } else if (array_type != null && array_type.element_type.type_symbol == 
string_type.type_symbol) {
-                       // G_TYPE_STRV
-                       return new CCodeIdentifier ("g_value_take_boxed");
-               } else {
-                       return new CCodeIdentifier ("g_value_set_pointer");
-               }
-       }
-
-       CCodeIdentifier get_value_getter_function (DataType type_reference) {
-               var array_type = type_reference as ArrayType;
-               if (type_reference.type_symbol != null) {
-                       return new CCodeIdentifier (get_ccode_get_value_function 
(type_reference.type_symbol));
-               } else if (array_type != null && array_type.element_type.type_symbol == 
string_type.type_symbol) {
-                       // G_TYPE_STRV
-                       return new CCodeIdentifier ("g_value_get_boxed");
-               } else {
-                       return new CCodeIdentifier ("g_value_get_pointer");
-               }
-       }
-
        public virtual void append_vala_array_free () {
        }
 
@@ -5226,49 +5190,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                set_cvalue (expr, new CCodeUnaryExpression (op, get_cvalue (expr.inner)));
        }
 
-       public CCodeExpression? try_cast_value_to_type (CCodeExpression ccodeexpr, DataType from, DataType 
to, Expression? expr = null) {
-               if (from == null || gvalue_type == null || from.type_symbol != gvalue_type || to.type_symbol 
== gvalue_type || get_ccode_type_id (to) == "") {
-                       return null;
-               }
-
-               // explicit conversion from GValue
-               var ccall = new CCodeFunctionCall (get_value_getter_function (to));
-               CCodeExpression gvalue;
-               if (from.nullable) {
-                       gvalue = ccodeexpr;
-               } else {
-                       gvalue = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, ccodeexpr);
-               }
-               ccall.add_argument (gvalue);
-
-               CCodeExpression rv = ccall;
-
-               if (expr != null && to is ArrayType) {
-                       // null-terminated string array
-                       var len_call = new CCodeFunctionCall (new CCodeIdentifier ("g_strv_length"));
-                       len_call.add_argument (rv);
-                       append_array_length (expr, len_call);
-               } else if (to is StructValueType) {
-                       CodeNode node = expr != null ? (CodeNode) expr : to;
-                       var temp_value = create_temp_value (to, true, node, true);
-                       var ctemp = get_cvalue_ (temp_value);
-
-                       rv = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new 
CCodeCastExpression (rv, get_ccode_name (new PointerType (to))));
-                       var holds = new CCodeFunctionCall (new CCodeIdentifier ("G_VALUE_HOLDS"));
-                       holds.add_argument (gvalue);
-                       holds.add_argument (new CCodeIdentifier (get_ccode_type_id (to)));
-                       var cond = new CCodeBinaryExpression (CCodeBinaryOperator.AND, holds, ccall);
-                       var warn = new CCodeFunctionCall (new CCodeIdentifier ("g_warning"));
-                       warn.add_argument (new CCodeConstant ("\"Invalid GValue unboxing (wrong type or 
NULL)\""));
-                       var fail = new CCodeCommaExpression ();
-                       fail.append_expression (warn);
-                       fail.append_expression (ctemp);
-                       rv = new CCodeConditionalExpression (cond, rv,  fail);
-               }
-
-               return rv;
-       }
-
        public virtual CCodeExpression? deserialize_expression (DataType type, CCodeExpression variant_expr, 
CCodeExpression? expr, CCodeExpression? error_expr = null, out bool may_fail = null) {
                assert_not_reached ();
        }
@@ -5280,14 +5201,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        public override void visit_cast_expression (CastExpression expr) {
                generate_type_declaration (expr.type_reference, cfile);
 
-               if (!expr.is_non_null_cast) {
-                       var valuecast = try_cast_value_to_type (get_cvalue (expr.inner), 
expr.inner.value_type, expr.type_reference, expr);
-                       if (valuecast != null) {
-                               set_cvalue (expr, valuecast);
-                               return;
-                       }
-               }
-
                unowned Class? cl = expr.type_reference.type_symbol as Class;
                unowned Interface? iface = expr.type_reference.type_symbol as Interface;
                if (context.profile == Profile.GOBJECT && (iface != null || (cl != null && !cl.is_compact))) {
@@ -6525,6 +6438,18 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                return new CCodeFunctionCall (new CCodeIdentifier (""));
        }
 
+       public virtual CCodeExpression get_value_getter_function (DataType type_reference) {
+               return new CCodeInvalidExpression ();
+       }
+
+       public virtual CCodeExpression get_value_setter_function (DataType type_reference) {
+               return new CCodeInvalidExpression ();
+       }
+
+       public virtual CCodeExpression get_value_taker_function (DataType type_reference) {
+               return new CCodeInvalidExpression ();
+       }
+
        public virtual void register_dbus_info (CCodeBlock block, ObjectTypeSymbol bindable) {
        }
 
diff --git a/codegen/valagvaluemodule.vala b/codegen/valagvaluemodule.vala
new file mode 100644
index 000000000..a6109fd40
--- /dev/null
+++ b/codegen/valagvaluemodule.vala
@@ -0,0 +1,110 @@
+/* valagvaluemodule.vala
+ *
+ * Copyright (C) 2019  Rico Tzschichholz
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Rico Tzschichholz <ricotz ubuntu com>
+ */
+
+public class Vala.GValueModule : GAsyncModule {
+       public override void visit_cast_expression (CastExpression expr) {
+               unowned DataType? value_type = expr.inner.value_type;
+               unowned DataType? target_type = expr.type_reference;
+
+               if (expr.is_non_null_cast || value_type == null || gvalue_type == null
+                   || value_type.type_symbol != gvalue_type || target_type.type_symbol == gvalue_type
+                   || get_ccode_type_id (target_type) == "") {
+                       base.visit_cast_expression (expr);
+                       return;
+               }
+
+               generate_type_declaration (expr.type_reference, cfile);
+
+               // explicit conversion from GValue
+               var ccall = new CCodeFunctionCall (get_value_getter_function (target_type));
+               CCodeExpression gvalue;
+               if (value_type.nullable) {
+                       gvalue = get_cvalue (expr.inner);
+               } else {
+                       gvalue = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_cvalue 
(expr.inner));
+               }
+               ccall.add_argument (gvalue);
+
+               CCodeExpression rv = ccall;
+
+               if (expr != null && target_type is ArrayType) {
+                       // null-terminated string array
+                       var len_call = new CCodeFunctionCall (new CCodeIdentifier ("g_strv_length"));
+                       len_call.add_argument (rv);
+                       append_array_length (expr, len_call);
+               } else if (target_type is StructValueType) {
+                       CodeNode node = expr != null ? (CodeNode) expr : target_type;
+                       var temp_value = create_temp_value (target_type, true, node, true);
+                       var ctemp = get_cvalue_ (temp_value);
+
+                       rv = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new 
CCodeCastExpression (rv, get_ccode_name (new PointerType (target_type))));
+                       var holds = new CCodeFunctionCall (new CCodeIdentifier ("G_VALUE_HOLDS"));
+                       holds.add_argument (gvalue);
+                       holds.add_argument (new CCodeIdentifier (get_ccode_type_id (target_type)));
+                       var cond = new CCodeBinaryExpression (CCodeBinaryOperator.AND, holds, ccall);
+                       var warn = new CCodeFunctionCall (new CCodeIdentifier ("g_warning"));
+                       warn.add_argument (new CCodeConstant ("\"Invalid GValue unboxing (wrong type or 
NULL)\""));
+                       var fail = new CCodeCommaExpression ();
+                       fail.append_expression (warn);
+                       fail.append_expression (ctemp);
+                       rv = new CCodeConditionalExpression (cond, rv,  fail);
+               }
+
+               set_cvalue (expr, rv);
+       }
+
+       public override CCodeExpression get_value_getter_function (DataType type_reference) {
+               var array_type = type_reference as ArrayType;
+               if (type_reference.type_symbol != null) {
+                       return new CCodeIdentifier (get_ccode_get_value_function 
(type_reference.type_symbol));
+               } else if (array_type != null && array_type.element_type.type_symbol == 
string_type.type_symbol) {
+                       // G_TYPE_STRV
+                       return new CCodeIdentifier ("g_value_get_boxed");
+               } else {
+                       return new CCodeIdentifier ("g_value_get_pointer");
+               }
+       }
+
+       public override CCodeExpression get_value_setter_function (DataType type_reference) {
+               var array_type = type_reference as ArrayType;
+               if (type_reference.type_symbol != null) {
+                       return new CCodeIdentifier (get_ccode_set_value_function 
(type_reference.type_symbol));
+               } else if (array_type != null && array_type.element_type.type_symbol == 
string_type.type_symbol) {
+                       // G_TYPE_STRV
+                       return new CCodeIdentifier ("g_value_set_boxed");
+               } else {
+                       return new CCodeIdentifier ("g_value_set_pointer");
+               }
+       }
+
+       public override CCodeExpression get_value_taker_function (DataType type_reference) {
+               var array_type = type_reference as ArrayType;
+               if (type_reference.type_symbol != null) {
+                       return new CCodeIdentifier (get_ccode_take_value_function 
(type_reference.type_symbol));
+               } else if (array_type != null && array_type.element_type.type_symbol == 
string_type.type_symbol) {
+                       // G_TYPE_STRV
+                       return new CCodeIdentifier ("g_value_take_boxed");
+               } else {
+                       return new CCodeIdentifier ("g_value_set_pointer");
+               }
+       }
+}
diff --git a/codegen/valagvariantmodule.vala b/codegen/valagvariantmodule.vala
index 7e679f860..dc5952599 100644
--- a/codegen/valagvariantmodule.vala
+++ b/codegen/valagvariantmodule.vala
@@ -20,7 +20,7 @@
  *     Jürg Billeter <j bitron ch>
  */
 
-public class Vala.GVariantModule : GAsyncModule {
+public class Vala.GVariantModule : GValueModule {
        struct BasicTypeInfo {
                public unowned string signature;
                public unowned string type_name;


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