[gtk+/wip/otte/shader: 240/269] gsksldeclaration: Correctly parse initializers



commit 14a64d8b2f2e038d1cc48a4d7a47e2f01c81ab5c
Author: Benjamin Otte <otte redhat com>
Date:   Tue Oct 17 00:55:06 2017 +0200

    gsksldeclaration: Correctly parse initializers
    
    And store them as initializers for later.

 gsk/gsksldeclaration.c                             |   18 +++++++++++++++---
 .../gsksl/errors/initialize-global-const.frag      |    6 ++++++
 testsuite/gsksl/errors/initialize-local-const.frag |    5 +++++
 3 files changed, 26 insertions(+), 3 deletions(-)
---
diff --git a/gsk/gsksldeclaration.c b/gsk/gsksldeclaration.c
index 9a85dc8..6894864 100644
--- a/gsk/gsksldeclaration.c
+++ b/gsk/gsksldeclaration.c
@@ -28,6 +28,7 @@
 #include "gsksltokenizerprivate.h"
 #include "gsksltypeprivate.h"
 #include "gskslqualifierprivate.h"
+#include "gskslvalueprivate.h"
 #include "gskslvariableprivate.h"
 #include "gskspvwriterprivate.h"
 
@@ -221,13 +222,13 @@ gsk_sl_declaration_parse_variable (GskSlScope           *scope,
                                    const char           *name)
 {
   GskSlDeclarationVariable *variable;
+  GskSlValue *initial_value = NULL;
+  GskSlExpression *initial = NULL;
   const GskSlToken *token;
 
   token = gsk_sl_preprocessor_get (preproc);
   if (gsk_sl_token_is (token, GSK_SL_TOKEN_EQUAL))
     {
-      GskSlExpression *initial;
-
       gsk_sl_preprocessor_consume (preproc, NULL);
 
       initial = gsk_sl_expression_parse_assignment (scope, preproc);
@@ -239,6 +240,16 @@ gsk_sl_declaration_parse_variable (GskSlScope           *scope,
                                      gsk_sl_type_get_name (gsk_sl_expression_get_return_type (initial)),
                                      gsk_sl_type_get_name (type));
           gsk_sl_expression_unref (initial);
+          initial = NULL;
+        }
+      else
+        {
+          GskSlValue *unconverted = gsk_sl_expression_get_constant (initial);
+          if (unconverted)
+            {
+              initial_value = gsk_sl_value_new_convert (unconverted, type);
+              gsk_sl_value_free (unconverted);
+            }
         }
 
       token = gsk_sl_preprocessor_get (preproc);
@@ -259,7 +270,8 @@ gsk_sl_declaration_parse_variable (GskSlScope           *scope,
     }
 
   variable = gsk_sl_declaration_new (&GSK_SL_DECLARATION_VARIABLE);
-  variable->variable = gsk_sl_variable_new (name, type, qualifier, NULL);
+  variable->variable = gsk_sl_variable_new (name, type, qualifier, initial_value);
+  variable->initial = initial;
   gsk_sl_scope_add_variable (scope, variable->variable);
 
   return &variable->parent;
diff --git a/testsuite/gsksl/errors/initialize-global-const.frag 
b/testsuite/gsksl/errors/initialize-global-const.frag
new file mode 100644
index 0000000..8a49747
--- /dev/null
+++ b/testsuite/gsksl/errors/initialize-global-const.frag
@@ -0,0 +1,6 @@
+const int x;
+
+void
+main ()
+{
+}
diff --git a/testsuite/gsksl/errors/initialize-local-const.frag 
b/testsuite/gsksl/errors/initialize-local-const.frag
new file mode 100644
index 0000000..d541795
--- /dev/null
+++ b/testsuite/gsksl/errors/initialize-local-const.frag
@@ -0,0 +1,5 @@
+void
+main ()
+{
+  const int x;
+}


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