Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / glsl / glsl_symbol_table.cpp
index e9bf89b951df246cf312dbd9d75383f8290c0239..536f0a3a8c23e319e1d3ef3956cc7653b4f747e0 100644 (file)
 
 class symbol_table_entry {
 public:
-   /* Callers of this talloc-based new need not call delete. It's
-    * easier to just talloc_free 'ctx' (or any of its ancestors). */
-   static void* operator new(size_t size, void *ctx)
+   DECLARE_RALLOC_CXX_OPERATORS(symbol_table_entry);
+
+   bool add_interface(const glsl_type *i, enum ir_variable_mode mode)
    {
-      void *entry = talloc_size(ctx, size);
-      assert(entry != NULL);
-      return entry;
+      const glsl_type **dest;
+
+      switch (mode) {
+      case ir_var_uniform:
+         dest = &ibu;
+         break;
+      case ir_var_shader_storage:
+         dest = &iss;
+         break;
+      case ir_var_shader_in:
+         dest = &ibi;
+         break;
+      case ir_var_shader_out:
+         dest = &ibo;
+         break;
+      default:
+         assert(!"Unsupported interface variable mode!");
+         return false;
+      }
+
+      if (*dest != NULL) {
+         return false;
+      } else {
+         *dest = i;
+         return true;
+      }
    }
 
-   /* If the user *does* call delete, that's OK, we will just
-    * talloc_free in that case. Here, C++ will have already called the
-    * destructor so tell talloc not to do that again. */
-   static void operator delete(void *table)
+   const glsl_type *get_interface(enum ir_variable_mode mode)
    {
-      talloc_set_destructor(table, NULL);
-      talloc_free(table);
+      switch (mode) {
+      case ir_var_uniform:
+         return ibu;
+      case ir_var_shader_storage:
+         return iss;
+      case ir_var_shader_in:
+         return ibi;
+      case ir_var_shader_out:
+         return ibo;
+      default:
+         assert(!"Unsupported interface variable mode!");
+         return NULL;
+      }
    }
 
-   symbol_table_entry(ir_variable *v)                     : v(v), f(0), t(0) {}
-   symbol_table_entry(ir_function *f)                     : v(0), f(f), t(0) {}
-   symbol_table_entry(const glsl_type *t)                 : v(0), f(0), t(t) {}
+   symbol_table_entry(ir_variable *v)               :
+      v(v), f(0), t(0), ibu(0), iss(0), ibi(0), ibo(0), a(0) {}
+   symbol_table_entry(ir_function *f)               :
+      v(0), f(f), t(0), ibu(0), iss(0), ibi(0), ibo(0), a(0) {}
+   symbol_table_entry(const glsl_type *t)           :
+      v(0), f(0), t(t), ibu(0), iss(0), ibi(0), ibo(0), a(0) {}
+   symbol_table_entry(const glsl_type *t, enum ir_variable_mode mode) :
+      v(0), f(0), t(0), ibu(0), iss(0), ibi(0), ibo(0), a(0)
+   {
+      assert(t->is_interface());
+      add_interface(t, mode);
+   }
+   symbol_table_entry(const class ast_type_specifier *a):
+      v(0), f(0), t(0), ibu(0), iss(0), ibi(0), ibo(0), a(a) {}
 
    ir_variable *v;
    ir_function *f;
    const glsl_type *t;
+   const glsl_type *ibu;
+   const glsl_type *iss;
+   const glsl_type *ibi;
+   const glsl_type *ibo;
+   const class ast_type_specifier *a;
 };
 
 glsl_symbol_table::glsl_symbol_table()
 {
-   this->language_version = 120;
+   this->separate_function_namespace = false;
    this->table = _mesa_symbol_table_ctor();
-   this->mem_ctx = talloc_init("symbol table entries");
+   this->mem_ctx = ralloc_context(NULL);
 }
 
 glsl_symbol_table::~glsl_symbol_table()
 {
    _mesa_symbol_table_dtor(table);
-   talloc_free(mem_ctx);
+   ralloc_free(mem_ctx);
 }
 
 void glsl_symbol_table::push_scope()
@@ -81,12 +128,14 @@ bool glsl_symbol_table::name_declared_this_scope(const char *name)
    return _mesa_symbol_table_symbol_scope(table, -1, name) == 0;
 }
 
-bool glsl_symbol_table::add_variable(const char *name, ir_variable *v)
+bool glsl_symbol_table::add_variable(ir_variable *v)
 {
-   if (this->language_version == 110) {
+   assert(v->data.mode != ir_var_temporary);
+
+   if (this->separate_function_namespace) {
       /* In 1.10, functions and variables have separate namespaces. */
-      symbol_table_entry *existing = get_entry(name);
-      if (name_declared_this_scope(name)) {
+      symbol_table_entry *existing = get_entry(v->name);
+      if (name_declared_this_scope(v->name)) {
         /* If there's already an existing function (not a constructor!) in
          * the current scope, just update the existing entry to include 'v'.
          */
@@ -102,7 +151,7 @@ bool glsl_symbol_table::add_variable(const char *name, ir_variable *v)
         symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(v);
         if (existing != NULL)
            entry->f = existing->f;
-        int added = _mesa_symbol_table_add_symbol(table, -1, name, entry);
+        int added = _mesa_symbol_table_add_symbol(table, -1, v->name, entry);
         assert(added == 0);
         (void)added;
         return true;
@@ -112,7 +161,7 @@ bool glsl_symbol_table::add_variable(const char *name, ir_variable *v)
 
    /* 1.20+ rules: */
    symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(v);
-   return _mesa_symbol_table_add_symbol(table, -1, name, entry) == 0;
+   return _mesa_symbol_table_add_symbol(table, -1, v->name, entry) == 0;
 }
 
 bool glsl_symbol_table::add_type(const char *name, const glsl_type *t)
@@ -121,18 +170,43 @@ bool glsl_symbol_table::add_type(const char *name, const glsl_type *t)
    return _mesa_symbol_table_add_symbol(table, -1, name, entry) == 0;
 }
 
-bool glsl_symbol_table::add_function(const char *name, ir_function *f)
+bool glsl_symbol_table::add_interface(const char *name, const glsl_type *i,
+                                      enum ir_variable_mode mode)
+{
+   assert(i->is_interface());
+   symbol_table_entry *entry = get_entry(name);
+   if (entry == NULL) {
+      symbol_table_entry *entry =
+         new(mem_ctx) symbol_table_entry(i, mode);
+      bool add_interface_symbol_result =
+         _mesa_symbol_table_add_symbol(table, -1, name, entry) == 0;
+      assert(add_interface_symbol_result);
+      return add_interface_symbol_result;
+   } else {
+      return entry->add_interface(i, mode);
+   }
+}
+
+bool glsl_symbol_table::add_function(ir_function *f)
 {
-   if (this->language_version == 110 && name_declared_this_scope(name)) {
+   if (this->separate_function_namespace && name_declared_this_scope(f->name)) {
       /* In 1.10, functions and variables have separate namespaces. */
-      symbol_table_entry *existing = get_entry(name);
+      symbol_table_entry *existing = get_entry(f->name);
       if ((existing->f == NULL) && (existing->t == NULL)) {
         existing->f = f;
         return true;
       }
    }
    symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(f);
-   return _mesa_symbol_table_add_symbol(table, -1, name, entry) == 0;
+   return _mesa_symbol_table_add_symbol(table, -1, f->name, entry) == 0;
+}
+
+void glsl_symbol_table::add_global_function(ir_function *f)
+{
+   symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(f);
+   int added = _mesa_symbol_table_add_global_symbol(table, -1, f->name, entry);
+   assert(added == 0);
+   (void)added;
 }
 
 ir_variable *glsl_symbol_table::get_variable(const char *name)
@@ -147,6 +221,13 @@ const glsl_type *glsl_symbol_table::get_type(const char *name)
    return entry != NULL ? entry->t : NULL;
 }
 
+const glsl_type *glsl_symbol_table::get_interface(const char *name,
+                                                  enum ir_variable_mode mode)
+{
+   symbol_table_entry *entry = get_entry(name);
+   return entry != NULL ? entry->get_interface(mode) : NULL;
+}
+
 ir_function *glsl_symbol_table::get_function(const char *name)
 {
    symbol_table_entry *entry = get_entry(name);
@@ -158,3 +239,18 @@ symbol_table_entry *glsl_symbol_table::get_entry(const char *name)
    return (symbol_table_entry *)
       _mesa_symbol_table_find_symbol(table, -1, name);
 }
+
+void
+glsl_symbol_table::disable_variable(const char *name)
+{
+   /* Ideally we would remove the variable's entry from the symbol table, but
+    * that would be difficult.  Fortunately, since this is only used for
+    * built-in variables, it won't be possible for the shader to re-introduce
+    * the variable later, so all we really need to do is to make sure that
+    * further attempts to access it using get_variable() will return NULL.
+    */
+   symbol_table_entry *entry = get_entry(name);
+   if (entry != NULL) {
+      entry->v = NULL;
+   }
+}