3 * Copyright © 2010 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 #include "glsl_symbol_table.h"
27 class symbol_table_entry
{
29 DECLARE_RALLOC_CXX_OPERATORS(symbol_table_entry
);
31 bool add_interface(const glsl_type
*i
, enum ir_variable_mode mode
)
33 const glsl_type
**dest
;
39 case ir_var_shader_in
:
42 case ir_var_shader_out
:
46 assert(!"Unsupported interface variable mode!");
58 const glsl_type
*get_interface(enum ir_variable_mode mode
)
63 case ir_var_shader_in
:
65 case ir_var_shader_out
:
68 assert(!"Unsupported interface variable mode!");
73 symbol_table_entry(ir_variable
*v
) :
74 v(v
), f(0), t(0), ibu(0), ibi(0), ibo(0), a(0) {}
75 symbol_table_entry(ir_function
*f
) :
76 v(0), f(f
), t(0), ibu(0), ibi(0), ibo(0), a(0) {}
77 symbol_table_entry(const glsl_type
*t
) :
78 v(0), f(0), t(t
), ibu(0), ibi(0), ibo(0), a(0) {}
79 symbol_table_entry(const glsl_type
*t
, enum ir_variable_mode mode
) :
80 v(0), f(0), t(0), ibu(0), ibi(0), ibo(0), a(0)
82 assert(t
->is_interface());
83 add_interface(t
, mode
);
85 symbol_table_entry(const class ast_type_specifier
*a
):
86 v(0), f(0), t(0), ibu(0), ibi(0), ibo(0), a(a
) {}
94 const class ast_type_specifier
*a
;
97 glsl_symbol_table::glsl_symbol_table()
99 this->separate_function_namespace
= false;
100 this->table
= _mesa_symbol_table_ctor();
101 this->mem_ctx
= ralloc_context(NULL
);
104 glsl_symbol_table::~glsl_symbol_table()
106 _mesa_symbol_table_dtor(table
);
107 ralloc_free(mem_ctx
);
110 void glsl_symbol_table::push_scope()
112 _mesa_symbol_table_push_scope(table
);
115 void glsl_symbol_table::pop_scope()
117 _mesa_symbol_table_pop_scope(table
);
120 bool glsl_symbol_table::name_declared_this_scope(const char *name
)
122 return _mesa_symbol_table_symbol_scope(table
, -1, name
) == 0;
125 bool glsl_symbol_table::add_variable(ir_variable
*v
)
127 assert(v
->data
.mode
!= ir_var_temporary
);
129 if (this->separate_function_namespace
) {
130 /* In 1.10, functions and variables have separate namespaces. */
131 symbol_table_entry
*existing
= get_entry(v
->name
);
132 if (name_declared_this_scope(v
->name
)) {
133 /* If there's already an existing function (not a constructor!) in
134 * the current scope, just update the existing entry to include 'v'.
136 if (existing
->v
== NULL
&& existing
->t
== NULL
) {
141 /* If not declared at this scope, add a new entry. But if an existing
142 * entry includes a function, propagate that to this block - otherwise
143 * the new variable declaration would shadow the function.
145 symbol_table_entry
*entry
= new(mem_ctx
) symbol_table_entry(v
);
146 if (existing
!= NULL
)
147 entry
->f
= existing
->f
;
148 int added
= _mesa_symbol_table_add_symbol(table
, -1, v
->name
, entry
);
157 symbol_table_entry
*entry
= new(mem_ctx
) symbol_table_entry(v
);
158 return _mesa_symbol_table_add_symbol(table
, -1, v
->name
, entry
) == 0;
161 bool glsl_symbol_table::add_type(const char *name
, const glsl_type
*t
)
163 symbol_table_entry
*entry
= new(mem_ctx
) symbol_table_entry(t
);
164 return _mesa_symbol_table_add_symbol(table
, -1, name
, entry
) == 0;
167 bool glsl_symbol_table::add_interface(const char *name
, const glsl_type
*i
,
168 enum ir_variable_mode mode
)
170 assert(i
->is_interface());
171 symbol_table_entry
*entry
= get_entry(name
);
173 symbol_table_entry
*entry
=
174 new(mem_ctx
) symbol_table_entry(i
, mode
);
175 bool add_interface_symbol_result
=
176 _mesa_symbol_table_add_symbol(table
, -1, name
, entry
) == 0;
177 assert(add_interface_symbol_result
);
178 return add_interface_symbol_result
;
180 return entry
->add_interface(i
, mode
);
184 bool glsl_symbol_table::add_function(ir_function
*f
)
186 if (this->separate_function_namespace
&& name_declared_this_scope(f
->name
)) {
187 /* In 1.10, functions and variables have separate namespaces. */
188 symbol_table_entry
*existing
= get_entry(f
->name
);
189 if ((existing
->f
== NULL
) && (existing
->t
== NULL
)) {
194 symbol_table_entry
*entry
= new(mem_ctx
) symbol_table_entry(f
);
195 return _mesa_symbol_table_add_symbol(table
, -1, f
->name
, entry
) == 0;
198 void glsl_symbol_table::add_global_function(ir_function
*f
)
200 symbol_table_entry
*entry
= new(mem_ctx
) symbol_table_entry(f
);
201 int added
= _mesa_symbol_table_add_global_symbol(table
, -1, f
->name
, entry
);
206 ir_variable
*glsl_symbol_table::get_variable(const char *name
)
208 symbol_table_entry
*entry
= get_entry(name
);
209 return entry
!= NULL
? entry
->v
: NULL
;
212 const glsl_type
*glsl_symbol_table::get_type(const char *name
)
214 symbol_table_entry
*entry
= get_entry(name
);
215 return entry
!= NULL
? entry
->t
: NULL
;
218 const glsl_type
*glsl_symbol_table::get_interface(const char *name
,
219 enum ir_variable_mode mode
)
221 symbol_table_entry
*entry
= get_entry(name
);
222 return entry
!= NULL
? entry
->get_interface(mode
) : NULL
;
225 ir_function
*glsl_symbol_table::get_function(const char *name
)
227 symbol_table_entry
*entry
= get_entry(name
);
228 return entry
!= NULL
? entry
->f
: NULL
;
231 symbol_table_entry
*glsl_symbol_table::get_entry(const char *name
)
233 return (symbol_table_entry
*)
234 _mesa_symbol_table_find_symbol(table
, -1, name
);
238 glsl_symbol_table::disable_variable(const char *name
)
240 /* Ideally we would remove the variable's entry from the symbol table, but
241 * that would be difficult. Fortunately, since this is only used for
242 * built-in variables, it won't be possible for the shader to re-introduce
243 * the variable later, so all we really need to do is to make sure that
244 * further attempts to access it using get_variable() will return NULL.
246 symbol_table_entry
*entry
= get_entry(name
);