2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * \file slang_compile_variable.c
27 * slang front-end compiler
31 #include "main/imports.h"
32 #include "slang_compile.h"
33 #include "slang_mem.h"
36 static slang_variable
*
37 slang_variable_new(void)
39 slang_variable
*v
= (slang_variable
*) _slang_alloc(sizeof(slang_variable
));
41 if (!slang_variable_construct(v
)) {
51 slang_variable_delete(slang_variable
* var
)
53 slang_variable_destruct(var
);
59 * slang_variable_scope
62 slang_variable_scope
*
63 _slang_variable_scope_new(slang_variable_scope
*parent
)
65 slang_variable_scope
*s
;
66 s
= (slang_variable_scope
*) _slang_alloc(sizeof(slang_variable_scope
));
68 s
->outer_scope
= parent
;
74 _slang_variable_scope_ctr(slang_variable_scope
* self
)
76 self
->variables
= NULL
;
77 self
->num_variables
= 0;
78 self
->outer_scope
= NULL
;
82 slang_variable_scope_destruct(slang_variable_scope
* scope
)
88 for (i
= 0; i
< scope
->num_variables
; i
++) {
89 if (scope
->variables
[i
])
90 slang_variable_delete(scope
->variables
[i
]);
92 _slang_free(scope
->variables
);
93 /* do not free scope->outer_scope */
97 slang_variable_scope_copy(slang_variable_scope
* x
,
98 const slang_variable_scope
* y
)
100 slang_variable_scope z
;
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
);
110 for (z
.num_variables
= 0; z
.num_variables
< y
->num_variables
;
112 z
.variables
[z
.num_variables
] = slang_variable_new();
113 if (!z
.variables
[z
.num_variables
]) {
114 slang_variable_scope_destruct(&z
);
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
);
124 z
.outer_scope
= y
->outer_scope
;
125 slang_variable_scope_destruct(x
);
132 * Grow the variable list by one.
133 * \return pointer to space for the new variable (will be initialized)
136 slang_variable_scope_grow(slang_variable_scope
*scope
)
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
)
146 scope
->num_variables
++;
148 scope
->variables
[n
] = slang_variable_new();
149 if (!scope
->variables
[n
])
152 return scope
->variables
[n
];
160 slang_variable_construct(slang_variable
* var
)
162 if (!slang_fully_specified_type_construct(&var
->type
))
164 var
->a_name
= SLANG_ATOM_NULL
;
166 var
->initializer
= NULL
;
168 var
->isTemp
= GL_FALSE
;
176 slang_variable_destruct(slang_variable
* var
)
178 slang_fully_specified_type_destruct(&var
->type
);
179 if (var
->initializer
!= NULL
) {
180 slang_operation_destruct(var
->initializer
);
181 _slang_free(var
->initializer
);
192 slang_variable_copy(slang_variable
* x
, const slang_variable
* y
)
196 if (!slang_variable_construct(&z
))
198 if (!slang_fully_specified_type_copy(&z
.type
, &y
->type
)) {
199 slang_variable_destruct(&z
);
202 z
.a_name
= y
->a_name
;
203 z
.array_len
= y
->array_len
;
204 if (y
->initializer
!= NULL
) {
206 = (slang_operation
*) _slang_alloc(sizeof(slang_operation
));
207 if (z
.initializer
== NULL
) {
208 slang_variable_destruct(&z
);
211 if (!slang_operation_construct(z
.initializer
)) {
212 _slang_free(z
.initializer
);
213 slang_variable_destruct(&z
);
216 if (!slang_operation_copy(z
.initializer
, y
->initializer
)) {
217 slang_variable_destruct(&z
);
222 slang_variable_destruct(x
);
229 * Search for named variable in given scope.
230 * \param all if true, search parent scopes too.
233 _slang_variable_locate(const slang_variable_scope
* scope
,
234 const slang_atom a_name
, GLboolean all
)
238 for (i
= 0; i
< scope
->num_variables
; i
++)
239 if (a_name
== scope
->variables
[i
]->a_name
)
240 return scope
->variables
[i
];
242 scope
= scope
->outer_scope
;