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
32 #include "slang_compile.h"
34 /* slang_type_specifier_type */
39 slang_type_specifier_type type
;
40 } type_specifier_type_name
;
42 static const type_specifier_type_name type_specifier_type_names
[] = {
43 {"void", SLANG_SPEC_VOID
},
44 {"bool", SLANG_SPEC_BOOL
},
45 {"bvec2", SLANG_SPEC_BVEC2
},
46 {"bvec3", SLANG_SPEC_BVEC3
},
47 {"bvec4", SLANG_SPEC_BVEC4
},
48 {"int", SLANG_SPEC_INT
},
49 {"ivec2", SLANG_SPEC_IVEC2
},
50 {"ivec3", SLANG_SPEC_IVEC3
},
51 {"ivec4", SLANG_SPEC_IVEC4
},
52 {"float", SLANG_SPEC_FLOAT
},
53 {"vec2", SLANG_SPEC_VEC2
},
54 {"vec3", SLANG_SPEC_VEC3
},
55 {"vec4", SLANG_SPEC_VEC4
},
56 {"mat2", SLANG_SPEC_MAT2
},
57 {"mat3", SLANG_SPEC_MAT3
},
58 {"mat4", SLANG_SPEC_MAT4
},
59 {"mat2x3", SLANG_SPEC_MAT23
},
60 {"mat3x2", SLANG_SPEC_MAT32
},
61 {"mat2x4", SLANG_SPEC_MAT24
},
62 {"mat4x2", SLANG_SPEC_MAT42
},
63 {"mat3x4", SLANG_SPEC_MAT34
},
64 {"mat4x3", SLANG_SPEC_MAT43
},
65 {"sampler1D", SLANG_SPEC_SAMPLER1D
},
66 {"sampler2D", SLANG_SPEC_SAMPLER2D
},
67 {"sampler3D", SLANG_SPEC_SAMPLER3D
},
68 {"samplerCube", SLANG_SPEC_SAMPLERCUBE
},
69 {"sampler1DShadow", SLANG_SPEC_SAMPLER1DSHADOW
},
70 {"sampler2DShadow", SLANG_SPEC_SAMPLER2DSHADOW
},
71 {"sampler2DRect", SLANG_SPEC_SAMPLER2DRECT
},
72 {"sampler2DRectShadow", SLANG_SPEC_SAMPLER2DRECTSHADOW
},
73 {NULL
, SLANG_SPEC_VOID
}
76 slang_type_specifier_type
77 slang_type_specifier_type_from_string(const char *name
)
79 const type_specifier_type_name
*p
= type_specifier_type_names
;
80 while (p
->name
!= NULL
) {
81 if (slang_string_compare(p
->name
, name
) == 0)
89 slang_type_specifier_type_to_string(slang_type_specifier_type type
)
91 const type_specifier_type_name
*p
= type_specifier_type_names
;
92 while (p
->name
!= NULL
) {
100 /* slang_fully_specified_type */
103 slang_fully_specified_type_construct(slang_fully_specified_type
* type
)
105 type
->qualifier
= SLANG_QUAL_NONE
;
106 slang_type_specifier_ctr(&type
->specifier
);
111 slang_fully_specified_type_destruct(slang_fully_specified_type
* type
)
113 slang_type_specifier_dtr(&type
->specifier
);
117 slang_fully_specified_type_copy(slang_fully_specified_type
* x
,
118 const slang_fully_specified_type
* y
)
120 slang_fully_specified_type z
;
122 if (!slang_fully_specified_type_construct(&z
))
124 z
.qualifier
= y
->qualifier
;
125 if (!slang_type_specifier_copy(&z
.specifier
, &y
->specifier
)) {
126 slang_fully_specified_type_destruct(&z
);
129 slang_fully_specified_type_destruct(x
);
135 static slang_variable
*
136 slang_variable_new(void)
138 slang_variable
*v
= (slang_variable
*) malloc(sizeof(slang_variable
));
140 if (!slang_variable_construct(v
)) {
150 slang_variable_delete(slang_variable
* var
)
152 slang_variable_destruct(var
);
158 * slang_variable_scope
161 slang_variable_scope
*
162 _slang_variable_scope_new(slang_variable_scope
*parent
)
164 slang_variable_scope
*s
;
165 s
= (slang_variable_scope
*) _mesa_calloc(sizeof(slang_variable_scope
));
166 s
->outer_scope
= parent
;
172 _slang_variable_scope_ctr(slang_variable_scope
* self
)
174 self
->variables
= NULL
;
175 self
->num_variables
= 0;
176 self
->outer_scope
= NULL
;
180 slang_variable_scope_destruct(slang_variable_scope
* scope
)
186 for (i
= 0; i
< scope
->num_variables
; i
++) {
187 if (scope
->variables
[i
])
188 slang_variable_delete(scope
->variables
[i
]);
190 slang_alloc_free(scope
->variables
);
191 /* do not free scope->outer_scope */
195 slang_variable_scope_copy(slang_variable_scope
* x
,
196 const slang_variable_scope
* y
)
198 slang_variable_scope z
;
201 _slang_variable_scope_ctr(&z
);
202 z
.variables
= (slang_variable
**)
203 _mesa_calloc(y
->num_variables
* sizeof(slang_variable
*));
204 if (z
.variables
== NULL
) {
205 slang_variable_scope_destruct(&z
);
208 for (z
.num_variables
= 0; z
.num_variables
< y
->num_variables
;
210 z
.variables
[z
.num_variables
] = slang_variable_new();
211 if (!z
.variables
[z
.num_variables
]) {
212 slang_variable_scope_destruct(&z
);
216 for (i
= 0; i
< z
.num_variables
; i
++) {
217 if (!slang_variable_copy(z
.variables
[i
], y
->variables
[i
])) {
218 slang_variable_scope_destruct(&z
);
222 z
.outer_scope
= y
->outer_scope
;
223 slang_variable_scope_destruct(x
);
230 * Grow the variable list by one.
231 * \return pointer to space for the new variable (will be initialized)
234 slang_variable_scope_grow(slang_variable_scope
*scope
)
236 const int n
= scope
->num_variables
;
237 scope
->variables
= (slang_variable
**)
238 slang_alloc_realloc(scope
->variables
,
239 n
* sizeof(slang_variable
*),
240 (n
+ 1) * sizeof(slang_variable
*));
241 if (!scope
->variables
)
244 scope
->num_variables
++;
246 scope
->variables
[n
] = slang_variable_new();
247 if (!scope
->variables
[n
])
250 return scope
->variables
[n
];
258 slang_variable_construct(slang_variable
* var
)
260 if (!slang_fully_specified_type_construct(&var
->type
))
262 var
->a_name
= SLANG_ATOM_NULL
;
264 var
->initializer
= NULL
;
267 var
->isTemp
= GL_FALSE
;
274 slang_variable_destruct(slang_variable
* var
)
276 slang_fully_specified_type_destruct(&var
->type
);
277 if (var
->initializer
!= NULL
) {
278 slang_operation_destruct(var
->initializer
);
279 slang_alloc_free(var
->initializer
);
283 _mesa_free(var
->aux
);
290 slang_variable_copy(slang_variable
* x
, const slang_variable
* y
)
294 if (!slang_variable_construct(&z
))
296 if (!slang_fully_specified_type_copy(&z
.type
, &y
->type
)) {
297 slang_variable_destruct(&z
);
300 z
.a_name
= y
->a_name
;
301 z
.array_len
= y
->array_len
;
302 if (y
->initializer
!= NULL
) {
304 = (slang_operation
*) slang_alloc_malloc(sizeof(slang_operation
));
305 if (z
.initializer
== NULL
) {
306 slang_variable_destruct(&z
);
309 if (!slang_operation_construct(z
.initializer
)) {
310 slang_alloc_free(z
.initializer
);
311 slang_variable_destruct(&z
);
314 if (!slang_operation_copy(z
.initializer
, y
->initializer
)) {
315 slang_variable_destruct(&z
);
319 z
.address
= y
->address
;
321 slang_variable_destruct(x
);
328 _slang_locate_variable(const slang_variable_scope
* scope
,
329 const slang_atom a_name
, GLboolean all
)
333 for (i
= 0; i
< scope
->num_variables
; i
++)
334 if (a_name
== scope
->variables
[i
]->a_name
)
335 return scope
->variables
[i
];
336 if (all
&& scope
->outer_scope
!= NULL
)
337 return _slang_locate_variable(scope
->outer_scope
, a_name
, 1);
343 gl_type_from_specifier(const slang_type_specifier
* type
)
345 switch (type
->type
) {
346 case SLANG_SPEC_BOOL
:
348 case SLANG_SPEC_BVEC2
:
349 return GL_BOOL_VEC2_ARB
;
350 case SLANG_SPEC_BVEC3
:
351 return GL_BOOL_VEC3_ARB
;
352 case SLANG_SPEC_BVEC4
:
353 return GL_BOOL_VEC4_ARB
;
356 case SLANG_SPEC_IVEC2
:
357 return GL_INT_VEC2_ARB
;
358 case SLANG_SPEC_IVEC3
:
359 return GL_INT_VEC3_ARB
;
360 case SLANG_SPEC_IVEC4
:
361 return GL_INT_VEC4_ARB
;
362 case SLANG_SPEC_FLOAT
:
364 case SLANG_SPEC_VEC2
:
365 return GL_FLOAT_VEC2_ARB
;
366 case SLANG_SPEC_VEC3
:
367 return GL_FLOAT_VEC3_ARB
;
368 case SLANG_SPEC_VEC4
:
369 return GL_FLOAT_VEC4_ARB
;
370 case SLANG_SPEC_MAT2
:
371 return GL_FLOAT_MAT2_ARB
;
372 case SLANG_SPEC_MAT3
:
373 return GL_FLOAT_MAT3_ARB
;
374 case SLANG_SPEC_MAT4
:
375 return GL_FLOAT_MAT4_ARB
;
376 case SLANG_SPEC_MAT23
:
377 return GL_FLOAT_MAT2x3_ARB
;
378 case SLANG_SPEC_MAT32
:
379 return GL_FLOAT_MAT3x2_ARB
;
380 case SLANG_SPEC_MAT24
:
381 return GL_FLOAT_MAT2x4_ARB
;
382 case SLANG_SPEC_MAT42
:
383 return GL_FLOAT_MAT4x2_ARB
;
384 case SLANG_SPEC_MAT34
:
385 return GL_FLOAT_MAT3x4_ARB
;
386 case SLANG_SPEC_MAT43
:
387 return GL_FLOAT_MAT4x3_ARB
;
388 case SLANG_SPEC_SAMPLER1D
:
389 return GL_SAMPLER_1D_ARB
;
390 case SLANG_SPEC_SAMPLER2D
:
391 return GL_SAMPLER_2D_ARB
;
392 case SLANG_SPEC_SAMPLER3D
:
393 return GL_SAMPLER_3D_ARB
;
394 case SLANG_SPEC_SAMPLERCUBE
:
395 return GL_SAMPLER_CUBE_ARB
;
396 case SLANG_SPEC_SAMPLER1DSHADOW
:
397 return GL_SAMPLER_1D_SHADOW_ARB
;
398 case SLANG_SPEC_SAMPLER2DSHADOW
:
399 return GL_SAMPLER_2D_SHADOW_ARB
;
400 case SLANG_SPEC_ARRAy
:
401 return gl_type_from_specifier(type
->_array
);