mesa: glsl compiler function renaming
[mesa.git] / src / mesa / shader / slang / slang_compile_variable.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file slang_compile_variable.c
27 * slang front-end compiler
28 * \author Michal Krol
29 */
30
31 #include "main/imports.h"
32 #include "slang_compile.h"
33 #include "slang_mem.h"
34
35
36 static slang_variable *
37 slang_variable_new(void)
38 {
39 slang_variable *v = (slang_variable *) _slang_alloc(sizeof(slang_variable));
40 if (v) {
41 if (!slang_variable_construct(v)) {
42 _slang_free(v);
43 v = NULL;
44 }
45 }
46 return v;
47 }
48
49
50 static void
51 slang_variable_delete(slang_variable * var)
52 {
53 slang_variable_destruct(var);
54 _slang_free(var);
55 }
56
57
58 /*
59 * slang_variable_scope
60 */
61
62 slang_variable_scope *
63 _slang_variable_scope_new(slang_variable_scope *parent)
64 {
65 slang_variable_scope *s;
66 s = (slang_variable_scope *) _slang_alloc(sizeof(slang_variable_scope));
67 if (s)
68 s->outer_scope = parent;
69 return s;
70 }
71
72
73 GLvoid
74 _slang_variable_scope_ctr(slang_variable_scope * self)
75 {
76 self->variables = NULL;
77 self->num_variables = 0;
78 self->outer_scope = NULL;
79 }
80
81 void
82 slang_variable_scope_destruct(slang_variable_scope * scope)
83 {
84 unsigned int i;
85
86 if (!scope)
87 return;
88 for (i = 0; i < scope->num_variables; i++) {
89 if (scope->variables[i])
90 slang_variable_delete(scope->variables[i]);
91 }
92 _slang_free(scope->variables);
93 /* do not free scope->outer_scope */
94 }
95
96 int
97 slang_variable_scope_copy(slang_variable_scope * x,
98 const slang_variable_scope * y)
99 {
100 slang_variable_scope z;
101 unsigned int i;
102
103 _slang_variable_scope_ctr(&z);
104 z.variables = (slang_variable **)
105 _slang_alloc(y->num_variables * sizeof(slang_variable *));
106 if (z.variables == NULL) {
107 slang_variable_scope_destruct(&z);
108 return 0;
109 }
110 for (z.num_variables = 0; z.num_variables < y->num_variables;
111 z.num_variables++) {
112 z.variables[z.num_variables] = slang_variable_new();
113 if (!z.variables[z.num_variables]) {
114 slang_variable_scope_destruct(&z);
115 return 0;
116 }
117 }
118 for (i = 0; i < z.num_variables; i++) {
119 if (!slang_variable_copy(z.variables[i], y->variables[i])) {
120 slang_variable_scope_destruct(&z);
121 return 0;
122 }
123 }
124 z.outer_scope = y->outer_scope;
125 slang_variable_scope_destruct(x);
126 *x = z;
127 return 1;
128 }
129
130
131 /**
132 * Grow the variable list by one.
133 * \return pointer to space for the new variable (will be initialized)
134 */
135 slang_variable *
136 slang_variable_scope_grow(slang_variable_scope *scope)
137 {
138 const int n = scope->num_variables;
139 scope->variables = (slang_variable **)
140 _slang_realloc(scope->variables,
141 n * sizeof(slang_variable *),
142 (n + 1) * sizeof(slang_variable *));
143 if (!scope->variables)
144 return NULL;
145
146 scope->num_variables++;
147
148 scope->variables[n] = slang_variable_new();
149 if (!scope->variables[n])
150 return NULL;
151
152 return scope->variables[n];
153 }
154
155
156
157 /* slang_variable */
158
159 int
160 slang_variable_construct(slang_variable * var)
161 {
162 if (!slang_fully_specified_type_construct(&var->type))
163 return 0;
164 var->a_name = SLANG_ATOM_NULL;
165 var->array_len = 0;
166 var->initializer = NULL;
167 var->address = ~0;
168 var->size = 0;
169 var->isTemp = GL_FALSE;
170 var->store = NULL;
171 var->declared = 0;
172 return 1;
173 }
174
175
176 void
177 slang_variable_destruct(slang_variable * var)
178 {
179 slang_fully_specified_type_destruct(&var->type);
180 if (var->initializer != NULL) {
181 slang_operation_destruct(var->initializer);
182 _slang_free(var->initializer);
183 }
184 #if 0
185 if (var->aux) {
186 _mesa_free(var->aux);
187 }
188 #endif
189 }
190
191
192 int
193 slang_variable_copy(slang_variable * x, const slang_variable * y)
194 {
195 slang_variable z;
196
197 if (!slang_variable_construct(&z))
198 return 0;
199 if (!slang_fully_specified_type_copy(&z.type, &y->type)) {
200 slang_variable_destruct(&z);
201 return 0;
202 }
203 z.a_name = y->a_name;
204 z.array_len = y->array_len;
205 if (y->initializer != NULL) {
206 z.initializer
207 = (slang_operation *) _slang_alloc(sizeof(slang_operation));
208 if (z.initializer == NULL) {
209 slang_variable_destruct(&z);
210 return 0;
211 }
212 if (!slang_operation_construct(z.initializer)) {
213 _slang_free(z.initializer);
214 slang_variable_destruct(&z);
215 return 0;
216 }
217 if (!slang_operation_copy(z.initializer, y->initializer)) {
218 slang_variable_destruct(&z);
219 return 0;
220 }
221 }
222 z.address = y->address;
223 z.size = y->size;
224 slang_variable_destruct(x);
225 *x = z;
226 return 1;
227 }
228
229
230 /**
231 * Search for named variable in given scope.
232 * \param all if true, search parent scopes too.
233 */
234 slang_variable *
235 _slang_variable_locate(const slang_variable_scope * scope,
236 const slang_atom a_name, GLboolean all)
237 {
238 while (scope) {
239 GLuint i;
240 for (i = 0; i < scope->num_variables; i++)
241 if (a_name == scope->variables[i]->a_name)
242 return scope->variables[i];
243 if (all)
244 scope = scope->outer_scope;
245 else
246 scope = NULL;
247 }
248 return NULL;
249 }