[gtk+/wip/otte/shader: 48/156] gskslfunction: Properly type-check calls to struct constructors



commit a787d4f4fac12596f83ea278811453023b1891af
Author: Benjamin Otte <otte redhat com>
Date:   Thu Sep 28 04:07:28 2017 +0200

    gskslfunction: Properly type-check calls to struct constructors

 gsk/gskslfunction.c |   66 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 62 insertions(+), 4 deletions(-)
---
diff --git a/gsk/gskslfunction.c b/gsk/gskslfunction.c
index 4f1ead1..f51aeac 100644
--- a/gsk/gskslfunction.c
+++ b/gsk/gskslfunction.c
@@ -98,10 +98,10 @@ gsk_sl_function_builtin_get_args_by_type (const GskSlType *type)
 }
 
 static gboolean
-gsk_sl_function_constructor_matches (const GskSlFunction  *function,
-                                     GskSlType           **arguments,
-                                     gsize                 n_arguments,
-                                     GError              **error)
+gsk_sl_function_constructor_matches_builtin (const GskSlFunction  *function,
+                                             GskSlType           **arguments,
+                                             gsize                 n_arguments,
+                                             GError              **error)
 {
   const GskSlFunctionConstructor *constructor = (const GskSlFunctionConstructor *) function;
   guint needed, provided;
@@ -136,6 +136,64 @@ gsk_sl_function_constructor_matches (const GskSlFunction  *function,
   return TRUE;
 }
 
+static gboolean
+gsk_sl_function_constructor_matches_struct (const GskSlFunction  *function,
+                                            GskSlType           **arguments,
+                                            gsize                 n_arguments,
+                                            GError              **error)
+{
+  const GskSlFunctionConstructor *constructor = (const GskSlFunctionConstructor *) function;
+  guint i;
+
+  if (n_arguments != gsk_sl_type_get_n_members (constructor->type))
+    {
+      g_set_error (error,
+                   GSK_SL_COMPILER_ERROR, GSK_SL_COMPILER_ERROR_TYPE_MISMATCH,
+                   "Constructor for %s needs %u arguments, but %"G_GSIZE_FORMAT" given.",
+                   gsk_sl_type_get_name (constructor->type),
+                   gsk_sl_type_get_n_members (constructor->type),
+                   n_arguments);
+      return FALSE;
+    }
+
+  for (i = 0; i < n_arguments; i++)
+    {
+      if (!gsk_sl_type_can_convert (gsk_sl_type_get_member_type (constructor->type, i), arguments[i]))
+        {
+          g_set_error (error,
+                       GSK_SL_COMPILER_ERROR, GSK_SL_COMPILER_ERROR_TYPE_MISMATCH,
+                       "Cannot convert argument %u from %s to %s.",
+                       i,
+                       gsk_sl_type_get_name (arguments[i]),
+                       gsk_sl_type_get_name (gsk_sl_type_get_member_type (constructor->type, i)));
+          return FALSE;
+        }
+    }
+
+  return TRUE;
+}
+
+static gboolean
+gsk_sl_function_constructor_matches (const GskSlFunction  *function,
+                                     GskSlType           **arguments,
+                                     gsize                 n_arguments,
+                                     GError              **error)
+{
+  const GskSlFunctionConstructor *constructor = (const GskSlFunctionConstructor *) function;
+
+  if (gsk_sl_type_is_scalar (constructor->type) ||
+      gsk_sl_type_is_vector (constructor->type) ||
+      gsk_sl_type_is_matrix (constructor->type))
+    return gsk_sl_function_constructor_matches_builtin (function, arguments, n_arguments, error);
+  else if (gsk_sl_type_is_struct (constructor->type))
+    return gsk_sl_function_constructor_matches_struct (function, arguments, n_arguments, error);
+  else
+    {
+      g_assert_not_reached ();
+    }
+
+}
+
 static guint32
 gsk_sl_function_constructor_write_spv (const GskSlFunction *function,
                                        GskSpvWriter        *writer)


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