glsl: Change type of is_array to bool.
[mesa.git] / src / glsl / glsl_symbol_table.cpp
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2010 Intel Corporation
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 #include "glsl_symbol_table.h"
26
27 class symbol_table_entry {
28 public:
29 /* Callers of this ralloc-based new need not call delete. It's
30 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
31 static void* operator new(size_t size, void *ctx)
32 {
33 void *entry = ralloc_size(ctx, size);
34 assert(entry != NULL);
35 return entry;
36 }
37
38 /* If the user *does* call delete, that's OK, we will just ralloc_free. */
39 static void operator delete(void *entry)
40 {
41 ralloc_free(entry);
42 }
43
44 bool add_interface(const glsl_type *i, enum ir_variable_mode mode)
45 {
46 const glsl_type **dest;
47
48 switch (mode) {
49 case ir_var_uniform:
50 dest = &ibu;
51 break;
52 case ir_var_shader_in:
53 dest = &ibi;
54 break;
55 case ir_var_shader_out:
56 dest = &ibo;
57 break;
58 default:
59 assert(!"Unsupported interface variable mode!");
60 return false;
61 }
62
63 if (*dest != NULL) {
64 return false;
65 } else {
66 *dest = i;
67 return true;
68 }
69 }
70
71 const glsl_type *get_interface(enum ir_variable_mode mode)
72 {
73 switch (mode) {
74 case ir_var_uniform:
75 return ibu;
76 case ir_var_shader_in:
77 return ibi;
78 case ir_var_shader_out:
79 return ibo;
80 default:
81 assert(!"Unsupported interface variable mode!");
82 return NULL;
83 }
84 }
85
86 symbol_table_entry(ir_variable *v) :
87 v(v), f(0), t(0), ibu(0), ibi(0), ibo(0) {}
88 symbol_table_entry(ir_function *f) :
89 v(0), f(f), t(0), ibu(0), ibi(0), ibo(0) {}
90 symbol_table_entry(const glsl_type *t) :
91 v(0), f(0), t(t), ibu(0), ibi(0), ibo(0) {}
92 symbol_table_entry(const glsl_type *t, enum ir_variable_mode mode) :
93 v(0), f(0), t(0), ibu(0), ibi(0), ibo(0)
94 {
95 assert(t->is_interface());
96 add_interface(t, mode);
97 }
98
99 ir_variable *v;
100 ir_function *f;
101 const glsl_type *t;
102 const glsl_type *ibu;
103 const glsl_type *ibi;
104 const glsl_type *ibo;
105 };
106
107 glsl_symbol_table::glsl_symbol_table()
108 {
109 this->separate_function_namespace = false;
110 this->table = _mesa_symbol_table_ctor();
111 this->mem_ctx = ralloc_context(NULL);
112 }
113
114 glsl_symbol_table::~glsl_symbol_table()
115 {
116 _mesa_symbol_table_dtor(table);
117 ralloc_free(mem_ctx);
118 }
119
120 void glsl_symbol_table::push_scope()
121 {
122 _mesa_symbol_table_push_scope(table);
123 }
124
125 void glsl_symbol_table::pop_scope()
126 {
127 _mesa_symbol_table_pop_scope(table);
128 }
129
130 bool glsl_symbol_table::name_declared_this_scope(const char *name)
131 {
132 return _mesa_symbol_table_symbol_scope(table, -1, name) == 0;
133 }
134
135 bool glsl_symbol_table::add_variable(ir_variable *v)
136 {
137 if (this->separate_function_namespace) {
138 /* In 1.10, functions and variables have separate namespaces. */
139 symbol_table_entry *existing = get_entry(v->name);
140 if (name_declared_this_scope(v->name)) {
141 /* If there's already an existing function (not a constructor!) in
142 * the current scope, just update the existing entry to include 'v'.
143 */
144 if (existing->v == NULL && existing->t == NULL) {
145 existing->v = v;
146 return true;
147 }
148 } else {
149 /* If not declared at this scope, add a new entry. But if an existing
150 * entry includes a function, propagate that to this block - otherwise
151 * the new variable declaration would shadow the function.
152 */
153 symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(v);
154 if (existing != NULL)
155 entry->f = existing->f;
156 int added = _mesa_symbol_table_add_symbol(table, -1, v->name, entry);
157 assert(added == 0);
158 (void)added;
159 return true;
160 }
161 return false;
162 }
163
164 /* 1.20+ rules: */
165 symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(v);
166 return _mesa_symbol_table_add_symbol(table, -1, v->name, entry) == 0;
167 }
168
169 bool glsl_symbol_table::add_type(const char *name, const glsl_type *t)
170 {
171 symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(t);
172 return _mesa_symbol_table_add_symbol(table, -1, name, entry) == 0;
173 }
174
175 bool glsl_symbol_table::add_interface(const char *name, const glsl_type *i,
176 enum ir_variable_mode mode)
177 {
178 assert(i->is_interface());
179 symbol_table_entry *entry = get_entry(name);
180 if (entry == NULL) {
181 symbol_table_entry *entry =
182 new(mem_ctx) symbol_table_entry(i, mode);
183 bool add_interface_symbol_result =
184 _mesa_symbol_table_add_symbol(table, -1, name, entry) == 0;
185 assert(add_interface_symbol_result);
186 return add_interface_symbol_result;
187 } else {
188 return entry->add_interface(i, mode);
189 }
190 }
191
192 bool glsl_symbol_table::add_function(ir_function *f)
193 {
194 if (this->separate_function_namespace && name_declared_this_scope(f->name)) {
195 /* In 1.10, functions and variables have separate namespaces. */
196 symbol_table_entry *existing = get_entry(f->name);
197 if ((existing->f == NULL) && (existing->t == NULL)) {
198 existing->f = f;
199 return true;
200 }
201 }
202 symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(f);
203 return _mesa_symbol_table_add_symbol(table, -1, f->name, entry) == 0;
204 }
205
206 void glsl_symbol_table::add_global_function(ir_function *f)
207 {
208 symbol_table_entry *entry = new(mem_ctx) symbol_table_entry(f);
209 int added = _mesa_symbol_table_add_global_symbol(table, -1, f->name, entry);
210 assert(added == 0);
211 (void)added;
212 }
213
214 ir_variable *glsl_symbol_table::get_variable(const char *name)
215 {
216 symbol_table_entry *entry = get_entry(name);
217 return entry != NULL ? entry->v : NULL;
218 }
219
220 const glsl_type *glsl_symbol_table::get_type(const char *name)
221 {
222 symbol_table_entry *entry = get_entry(name);
223 return entry != NULL ? entry->t : NULL;
224 }
225
226 const glsl_type *glsl_symbol_table::get_interface(const char *name,
227 enum ir_variable_mode mode)
228 {
229 symbol_table_entry *entry = get_entry(name);
230 return entry != NULL ? entry->get_interface(mode) : NULL;
231 }
232
233 ir_function *glsl_symbol_table::get_function(const char *name)
234 {
235 symbol_table_entry *entry = get_entry(name);
236 return entry != NULL ? entry->f : NULL;
237 }
238
239 symbol_table_entry *glsl_symbol_table::get_entry(const char *name)
240 {
241 return (symbol_table_entry *)
242 _mesa_symbol_table_find_symbol(table, -1, name);
243 }