slang: Handle OOM condition in new_instruction().
[mesa.git] / src / mesa / shader / slang / slang_compile_struct.c
index bad58f4e5be0409d24e192595311372904beb86c..e6c38730d7d7e889c448d7c689af18ae458576af 100644 (file)
-/*\r
- * Mesa 3-D graphics library\r
- * Version:  6.5\r
- *\r
- * Copyright (C) 2005-2006  Brian Paul   All Rights Reserved.\r
- *\r
- * Permission is hereby granted, free of charge, to any person obtaining a\r
- * copy of this software and associated documentation files (the "Software"),\r
- * to deal in the Software without restriction, including without limitation\r
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
- * and/or sell copies of the Software, and to permit persons to whom the\r
- * Software is furnished to do so, subject to the following conditions:\r
- *\r
- * The above copyright notice and this permission notice shall be included\r
- * in all copies or substantial portions of the Software.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL\r
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN\r
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
- */\r
-\r
-/**\r
- * \file slang_compile_struct.c\r
- * slang front-end compiler\r
- * \author Michal Krol\r
- */\r
-\r
-#include "imports.h"\r
-#include "slang_utility.h"\r
-#include "slang_compile_variable.h"\r
-#include "slang_compile_struct.h"\r
-#include "slang_compile_operation.h"\r
-#include "slang_compile_function.h"\r
-\r
-/* slang_struct_scope */\r
-\r
-int slang_struct_scope_construct (slang_struct_scope *scope)\r
-{\r
-       scope->structs = NULL;\r
-       scope->num_structs = 0;\r
-       scope->outer_scope = NULL;\r
-       return 1;\r
-}\r
-\r
-void slang_struct_scope_destruct (slang_struct_scope *scope)\r
-{\r
-       unsigned int i;\r
-\r
-       for (i = 0; i < scope->num_structs; i++)\r
-               slang_struct_destruct (scope->structs + i);\r
-       slang_alloc_free (scope->structs);\r
-       /* do not free scope->outer_scope */\r
-}\r
-\r
-int slang_struct_scope_copy (slang_struct_scope *x, const slang_struct_scope *y)\r
-{\r
-       slang_struct_scope z;\r
-       unsigned int i;\r
-\r
-       if (!slang_struct_scope_construct (&z))\r
-               return 0;\r
-       z.structs = (slang_struct *) slang_alloc_malloc (y->num_structs * sizeof (slang_struct));\r
-       if (z.structs == NULL)\r
-       {\r
-               slang_struct_scope_destruct (&z);\r
-               return 0;\r
-       }\r
-       for (z.num_structs = 0; z.num_structs < y->num_structs; z.num_structs++)\r
-               if (!slang_struct_construct (&z.structs[z.num_structs]))\r
-               {\r
-                       slang_struct_scope_destruct (&z);\r
-                       return 0;\r
-               }\r
-       for (i = 0; i < z.num_structs; i++)\r
-               if (!slang_struct_copy (&z.structs[i], &y->structs[i]))\r
-               {\r
-                       slang_struct_scope_destruct (&z);\r
-                       return 0;\r
-               }\r
-       z.outer_scope = y->outer_scope;\r
-       slang_struct_scope_destruct (x);\r
-       *x = z;\r
-       return 1;\r
-}\r
-\r
-slang_struct *slang_struct_scope_find (slang_struct_scope *stru, const char *name, int all_scopes)\r
-{\r
-       unsigned int i;\r
-\r
-       for (i = 0; i < stru->num_structs; i++)\r
-               if (slang_string_compare (name, stru->structs[i].name) == 0)\r
-                       return stru->structs + i;\r
-       if (all_scopes && stru->outer_scope != NULL)\r
-               return slang_struct_scope_find (stru->outer_scope, name, 1);\r
-       return NULL;\r
-}\r
-\r
-/* slang_struct */\r
-\r
-int slang_struct_construct (slang_struct *stru)\r
-{\r
-       stru->name = NULL;\r
-       stru->fields = (slang_variable_scope *) slang_alloc_malloc (sizeof (slang_variable_scope));\r
-       if (stru->fields == NULL)\r
-               return 0;\r
-       if (!slang_variable_scope_construct (stru->fields))\r
-       {\r
-               slang_alloc_free (stru->fields);\r
-               return 0;\r
-       }\r
-       stru->structs = (slang_struct_scope *) slang_alloc_malloc (sizeof (slang_struct_scope));\r
-       if (stru->structs == NULL)\r
-       {\r
-               slang_variable_scope_destruct (stru->fields);\r
-               slang_alloc_free (stru->fields);\r
-               return 0;\r
-       }\r
-       if (!slang_struct_scope_construct (stru->structs))\r
-       {\r
-               slang_variable_scope_destruct (stru->fields);\r
-               slang_alloc_free (stru->fields);\r
-               slang_alloc_free (stru->structs);\r
-               return 0;\r
-       }\r
-       return 1;\r
-}\r
-\r
-void slang_struct_destruct (slang_struct *stru)\r
-{\r
-       slang_alloc_free (stru->name);\r
-       slang_variable_scope_destruct (stru->fields);\r
-       slang_alloc_free (stru->fields);\r
-       slang_struct_scope_destruct (stru->structs);\r
-       slang_alloc_free (stru->structs);\r
-}\r
-\r
-int slang_struct_copy (slang_struct *x, const slang_struct *y)\r
-{\r
-       slang_struct z;\r
-\r
-       if (!slang_struct_construct (&z))\r
-               return 0;\r
-       if (y->name != NULL)\r
-       {\r
-               z.name = slang_string_duplicate (y->name);\r
-               if (z.name == NULL)\r
-               {\r
-                       slang_struct_destruct (&z);\r
-                       return 0;\r
-               }\r
-       }\r
-       if (!slang_variable_scope_copy (z.fields, y->fields))\r
-       {\r
-               slang_struct_destruct (&z);\r
-               return 0;\r
-       }\r
-       if (!slang_struct_scope_copy (z.structs, y->structs))\r
-       {\r
-               slang_struct_destruct (&z);\r
-               return 0;\r
-       }\r
-       slang_struct_destruct (x);\r
-       *x = z;\r
-       return 1;\r
-}\r
-\r
-int slang_struct_equal (const slang_struct *x, const slang_struct *y)\r
-{\r
-       unsigned int i;\r
-\r
-       if (x->fields->num_variables != y->fields->num_variables)\r
-               return 0;\r
-       for (i = 0; i < x->fields->num_variables; i++)\r
-       {\r
-               slang_variable *varx = x->fields->variables + i;\r
-               slang_variable *vary = y->fields->variables + i;\r
-               if (slang_string_compare (varx->name, vary->name) != 0)\r
-                       return 0;\r
-               if (!slang_type_specifier_equal (&varx->type.specifier, &vary->type.specifier))\r
-                       return 0;\r
-               if (varx->type.specifier.type == slang_spec_array)\r
-               {\r
-                       /* TODO compare array sizes */\r
-               }\r
-       }\r
-       return 1;\r
-}\r
-\r
+/*
+ * Mesa 3-D graphics library
+ * Version:  6.5.3
+ *
+ * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
+ * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * \file slang_compile_struct.c
+ * slang front-end compiler
+ * \author Michal Krol
+ */
+
+#include "main/imports.h"
+#include "slang_mem.h"
+#include "slang_compile.h"
+
+
+GLvoid
+_slang_struct_scope_ctr(slang_struct_scope * self)
+{
+   self->structs = NULL;
+   self->num_structs = 0;
+   self->outer_scope = NULL;
+}
+
+void
+slang_struct_scope_destruct(slang_struct_scope * scope)
+{
+   GLuint i;
+
+   for (i = 0; i < scope->num_structs; i++)
+      slang_struct_destruct(scope->structs + i);
+   _slang_free(scope->structs);
+   /* do not free scope->outer_scope */
+}
+
+int
+slang_struct_scope_copy(slang_struct_scope * x, const slang_struct_scope * y)
+{
+   slang_struct_scope z;
+   GLuint i;
+
+   _slang_struct_scope_ctr(&z);
+   z.structs = (slang_struct *)
+      _slang_alloc(y->num_structs * sizeof(slang_struct));
+   if (z.structs == NULL) {
+      slang_struct_scope_destruct(&z);
+      return 0;
+   }
+   for (z.num_structs = 0; z.num_structs < y->num_structs; z.num_structs++)
+      if (!slang_struct_construct(&z.structs[z.num_structs])) {
+         slang_struct_scope_destruct(&z);
+         return 0;
+      }
+   for (i = 0; i < z.num_structs; i++)
+      if (!slang_struct_copy(&z.structs[i], &y->structs[i])) {
+         slang_struct_scope_destruct(&z);
+         return 0;
+      }
+   z.outer_scope = y->outer_scope;
+   slang_struct_scope_destruct(x);
+   *x = z;
+   return 1;
+}
+
+slang_struct *
+slang_struct_scope_find(slang_struct_scope * stru, slang_atom a_name,
+                        int all_scopes)
+{
+   GLuint i;
+
+   for (i = 0; i < stru->num_structs; i++)
+      if (a_name == stru->structs[i].a_name)
+         return &stru->structs[i];
+   if (all_scopes && stru->outer_scope != NULL)
+      return slang_struct_scope_find(stru->outer_scope, a_name, 1);
+   return NULL;
+}
+
+/* slang_struct */
+
+int
+slang_struct_construct(slang_struct * stru)
+{
+   stru->a_name = SLANG_ATOM_NULL;
+   stru->fields = (slang_variable_scope *)
+      _slang_alloc(sizeof(slang_variable_scope));
+   if (stru->fields == NULL)
+      return 0;
+   _slang_variable_scope_ctr(stru->fields);
+
+   stru->structs =
+      (slang_struct_scope *) _slang_alloc(sizeof(slang_struct_scope));
+   if (stru->structs == NULL) {
+      slang_variable_scope_destruct(stru->fields);
+      _slang_free(stru->fields);
+      return 0;
+   }
+   _slang_struct_scope_ctr(stru->structs);
+   stru->constructor = NULL;
+   return 1;
+}
+
+void
+slang_struct_destruct(slang_struct * stru)
+{
+   slang_variable_scope_destruct(stru->fields);
+   _slang_free(stru->fields);
+   slang_struct_scope_destruct(stru->structs);
+   _slang_free(stru->structs);
+}
+
+int
+slang_struct_copy(slang_struct * x, const slang_struct * y)
+{
+   slang_struct z;
+
+   if (!slang_struct_construct(&z))
+      return 0;
+   z.a_name = y->a_name;
+   if (!slang_variable_scope_copy(z.fields, y->fields)) {
+      slang_struct_destruct(&z);
+      return 0;
+   }
+   if (!slang_struct_scope_copy(z.structs, y->structs)) {
+      slang_struct_destruct(&z);
+      return 0;
+   }
+   slang_struct_destruct(x);
+   *x = z;
+   return 1;
+}
+
+int
+slang_struct_equal(const slang_struct * x, const slang_struct * y)
+{
+   GLuint i;
+
+   if (x->fields->num_variables != y->fields->num_variables)
+      return 0;
+
+   for (i = 0; i < x->fields->num_variables; i++) {
+      const slang_variable *varx = x->fields->variables[i];
+      const slang_variable *vary = y->fields->variables[i];
+
+      if (varx->a_name != vary->a_name)
+         return 0;
+      if (!slang_type_specifier_equal(&varx->type.specifier,
+                                      &vary->type.specifier))
+         return 0;
+      if (varx->type.specifier.type == SLANG_SPEC_ARRAY)
+         if (varx->array_len != vary->array_len)
+            return GL_FALSE;
+   }
+   return 1;
+}