2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
31 * * Symbol table management
35 * The majority of this work could be done during parsing, and the parser could
36 * probably generate HIR directly. However, this results in frequent changes
37 * to the parser code. Since we do not assume that every system this complier
38 * is built on will have Flex and Bison installed, we have to store the code
39 * generated by these tools in our version control system. In other parts of
40 * the system we've seen problems where a parser was changed but the generated
41 * code was not committed, merge conflicts where created because two developers
42 * had slightly different versions of Bison installed, etc.
44 * I have also noticed that running Bison generated parsers in GDB is very
45 * irritating. When you get a segfault on '$$ = $1->foo', you can't very
46 * well 'print $1' in GDB.
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
52 #include "main/imports.h"
53 #include "glsl_symbol_table.h"
54 #include "glsl_parser_extras.h"
56 #include "glsl_types.h"
60 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
62 _mesa_glsl_initialize_variables(instructions
, state
);
63 _mesa_glsl_initialize_constructors(instructions
, state
);
64 _mesa_glsl_initialize_functions(instructions
, state
);
66 state
->current_function
= NULL
;
68 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
69 ast
->hir(instructions
, state
);
74 * If a conversion is available, convert one operand to a different type
76 * The \c from \c ir_rvalue is converted "in place".
78 * \param to Type that the operand it to be converted to
79 * \param from Operand that is being converted
80 * \param state GLSL compiler state
83 * If a conversion is possible (or unnecessary), \c true is returned.
84 * Otherwise \c false is returned.
87 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
88 struct _mesa_glsl_parse_state
*state
)
90 if (to
->base_type
== from
->type
->base_type
)
93 /* This conversion was added in GLSL 1.20. If the compilation mode is
94 * GLSL 1.10, the conversion is skipped.
96 if (state
->language_version
< 120)
99 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
101 * "There are no implicit array or structure conversions. For
102 * example, an array of int cannot be implicitly converted to an
103 * array of float. There are no implicit conversions between
104 * signed and unsigned integers."
106 /* FINISHME: The above comment is partially a lie. There is int/uint
107 * FINISHME: conversion for immediate constants.
109 if (!to
->is_float() || !from
->type
->is_numeric())
112 switch (from
->type
->base_type
) {
114 from
= new ir_expression(ir_unop_i2f
, to
, from
, NULL
);
117 from
= new ir_expression(ir_unop_u2f
, to
, from
, NULL
);
120 from
= new ir_expression(ir_unop_b2f
, to
, from
, NULL
);
130 static const struct glsl_type
*
131 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
133 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
135 const glsl_type
*type_a
= value_a
->type
;
136 const glsl_type
*type_b
= value_b
->type
;
138 /* From GLSL 1.50 spec, page 56:
140 * "The arithmetic binary operators add (+), subtract (-),
141 * multiply (*), and divide (/) operate on integer and
142 * floating-point scalars, vectors, and matrices."
144 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
145 _mesa_glsl_error(loc
, state
,
146 "Operands to arithmetic operators must be numeric");
147 return glsl_type::error_type
;
151 /* "If one operand is floating-point based and the other is
152 * not, then the conversions from Section 4.1.10 "Implicit
153 * Conversions" are applied to the non-floating-point-based operand."
155 if (!apply_implicit_conversion(type_a
, value_b
, state
)
156 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
157 _mesa_glsl_error(loc
, state
,
158 "Could not implicitly convert operands to "
159 "arithmetic operator");
160 return glsl_type::error_type
;
162 type_a
= value_a
->type
;
163 type_b
= value_b
->type
;
165 /* "If the operands are integer types, they must both be signed or
168 * From this rule and the preceeding conversion it can be inferred that
169 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
170 * The is_numeric check above already filtered out the case where either
171 * type is not one of these, so now the base types need only be tested for
174 if (type_a
->base_type
!= type_b
->base_type
) {
175 _mesa_glsl_error(loc
, state
,
176 "base type mismatch for arithmetic operator");
177 return glsl_type::error_type
;
180 /* "All arithmetic binary operators result in the same fundamental type
181 * (signed integer, unsigned integer, or floating-point) as the
182 * operands they operate on, after operand type conversion. After
183 * conversion, the following cases are valid
185 * * The two operands are scalars. In this case the operation is
186 * applied, resulting in a scalar."
188 if (type_a
->is_scalar() && type_b
->is_scalar())
191 /* "* One operand is a scalar, and the other is a vector or matrix.
192 * In this case, the scalar operation is applied independently to each
193 * component of the vector or matrix, resulting in the same size
196 if (type_a
->is_scalar()) {
197 if (!type_b
->is_scalar())
199 } else if (type_b
->is_scalar()) {
203 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
204 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
207 assert(!type_a
->is_scalar());
208 assert(!type_b
->is_scalar());
210 /* "* The two operands are vectors of the same size. In this case, the
211 * operation is done component-wise resulting in the same size
214 if (type_a
->is_vector() && type_b
->is_vector()) {
215 if (type_a
== type_b
) {
218 _mesa_glsl_error(loc
, state
,
219 "vector size mismatch for arithmetic operator");
220 return glsl_type::error_type
;
224 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
225 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
226 * <vector, vector> have been handled. At least one of the operands must
227 * be matrix. Further, since there are no integer matrix types, the base
228 * type of both operands must be float.
230 assert(type_a
->is_matrix() || type_b
->is_matrix());
231 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
232 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
234 /* "* The operator is add (+), subtract (-), or divide (/), and the
235 * operands are matrices with the same number of rows and the same
236 * number of columns. In this case, the operation is done component-
237 * wise resulting in the same size matrix."
238 * * The operator is multiply (*), where both operands are matrices or
239 * one operand is a vector and the other a matrix. A right vector
240 * operand is treated as a column vector and a left vector operand as a
241 * row vector. In all these cases, it is required that the number of
242 * columns of the left operand is equal to the number of rows of the
243 * right operand. Then, the multiply (*) operation does a linear
244 * algebraic multiply, yielding an object that has the same number of
245 * rows as the left operand and the same number of columns as the right
246 * operand. Section 5.10 "Vector and Matrix Operations" explains in
247 * more detail how vectors and matrices are operated on."
250 if (type_a
== type_b
)
253 if (type_a
->is_matrix() && type_b
->is_matrix()) {
254 /* Matrix multiply. The columns of A must match the rows of B. Given
255 * the other previously tested constraints, this means the vector type
256 * of a row from A must be the same as the vector type of a column from
259 if (type_a
->row_type() == type_b
->column_type()) {
260 /* The resulting matrix has the number of columns of matrix B and
261 * the number of rows of matrix A. We get the row count of A by
262 * looking at the size of a vector that makes up a column. The
263 * transpose (size of a row) is done for B.
265 const glsl_type
*const type
=
266 glsl_type::get_instance(type_a
->base_type
,
267 type_a
->column_type()->vector_elements
,
268 type_b
->row_type()->vector_elements
);
269 assert(type
!= glsl_type::error_type
);
273 } else if (type_a
->is_matrix()) {
274 /* A is a matrix and B is a column vector. Columns of A must match
275 * rows of B. Given the other previously tested constraints, this
276 * means the vector type of a row from A must be the same as the
277 * vector the type of B.
279 if (type_a
->row_type() == type_b
)
282 assert(type_b
->is_matrix());
284 /* A is a row vector and B is a matrix. Columns of A must match rows
285 * of B. Given the other previously tested constraints, this means
286 * the type of A must be the same as the vector type of a column from
289 if (type_a
== type_b
->column_type())
293 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
294 return glsl_type::error_type
;
298 /* "All other cases are illegal."
300 _mesa_glsl_error(loc
, state
, "type mismatch");
301 return glsl_type::error_type
;
305 static const struct glsl_type
*
306 unary_arithmetic_result_type(const struct glsl_type
*type
,
307 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
309 /* From GLSL 1.50 spec, page 57:
311 * "The arithmetic unary operators negate (-), post- and pre-increment
312 * and decrement (-- and ++) operate on integer or floating-point
313 * values (including vectors and matrices). All unary operators work
314 * component-wise on their operands. These result with the same type
317 if (!type
->is_numeric()) {
318 _mesa_glsl_error(loc
, state
,
319 "Operands to arithmetic operators must be numeric");
320 return glsl_type::error_type
;
327 static const struct glsl_type
*
328 modulus_result_type(const struct glsl_type
*type_a
,
329 const struct glsl_type
*type_b
,
330 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
332 /* From GLSL 1.50 spec, page 56:
333 * "The operator modulus (%) operates on signed or unsigned integers or
334 * integer vectors. The operand types must both be signed or both be
337 if (!type_a
->is_integer() || !type_b
->is_integer()
338 || (type_a
->base_type
!= type_b
->base_type
)) {
339 _mesa_glsl_error(loc
, state
, "type mismatch");
340 return glsl_type::error_type
;
343 /* "The operands cannot be vectors of differing size. If one operand is
344 * a scalar and the other vector, then the scalar is applied component-
345 * wise to the vector, resulting in the same type as the vector. If both
346 * are vectors of the same size, the result is computed component-wise."
348 if (type_a
->is_vector()) {
349 if (!type_b
->is_vector()
350 || (type_a
->vector_elements
== type_b
->vector_elements
))
355 /* "The operator modulus (%) is not defined for any other data types
356 * (non-integer types)."
358 _mesa_glsl_error(loc
, state
, "type mismatch");
359 return glsl_type::error_type
;
363 static const struct glsl_type
*
364 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
365 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
367 const glsl_type
*type_a
= value_a
->type
;
368 const glsl_type
*type_b
= value_b
->type
;
370 /* From GLSL 1.50 spec, page 56:
371 * "The relational operators greater than (>), less than (<), greater
372 * than or equal (>=), and less than or equal (<=) operate only on
373 * scalar integer and scalar floating-point expressions."
375 if (!type_a
->is_numeric()
376 || !type_b
->is_numeric()
377 || !type_a
->is_scalar()
378 || !type_b
->is_scalar()) {
379 _mesa_glsl_error(loc
, state
,
380 "Operands to relational operators must be scalar and "
382 return glsl_type::error_type
;
385 /* "Either the operands' types must match, or the conversions from
386 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
387 * operand, after which the types must match."
389 if (!apply_implicit_conversion(type_a
, value_b
, state
)
390 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
391 _mesa_glsl_error(loc
, state
,
392 "Could not implicitly convert operands to "
393 "relational operator");
394 return glsl_type::error_type
;
396 type_a
= value_a
->type
;
397 type_b
= value_b
->type
;
399 if (type_a
->base_type
!= type_b
->base_type
) {
400 _mesa_glsl_error(loc
, state
, "base type mismatch");
401 return glsl_type::error_type
;
404 /* "The result is scalar Boolean."
406 return glsl_type::bool_type
;
411 * Validates that a value can be assigned to a location with a specified type
413 * Validates that \c rhs can be assigned to some location. If the types are
414 * not an exact match but an automatic conversion is possible, \c rhs will be
418 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
419 * Otherwise the actual RHS to be assigned will be returned. This may be
420 * \c rhs, or it may be \c rhs after some type conversion.
423 * In addition to being used for assignments, this function is used to
424 * type-check return values.
427 validate_assignment(struct _mesa_glsl_parse_state
*state
,
428 const glsl_type
*lhs_type
, ir_rvalue
*rhs
)
430 const glsl_type
*rhs_type
= rhs
->type
;
432 /* If there is already some error in the RHS, just return it. Anything
433 * else will lead to an avalanche of error message back to the user.
435 if (rhs_type
->is_error())
438 /* If the types are identical, the assignment can trivially proceed.
440 if (rhs_type
== lhs_type
)
443 /* If the array element types are the same and the size of the LHS is zero,
444 * the assignment is okay.
446 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
447 * is handled by ir_dereference::is_lvalue.
449 if (lhs_type
->is_array() && rhs
->type
->is_array()
450 && (lhs_type
->element_type() == rhs
->type
->element_type())
451 && (lhs_type
->array_size() == 0)) {
455 /* Check for implicit conversion in GLSL 1.20 */
456 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
457 rhs_type
= rhs
->type
;
458 if (rhs_type
== lhs_type
)
466 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
467 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
470 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
472 if (!error_emitted
) {
473 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
474 if (!lhs
->is_lvalue()) {
475 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
476 error_emitted
= true;
480 ir_rvalue
*new_rhs
= validate_assignment(state
, lhs
->type
, rhs
);
481 if (new_rhs
== NULL
) {
482 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
486 /* If the LHS array was not declared with a size, it takes it size from
487 * the RHS. If the LHS is an l-value and a whole array, it must be a
488 * dereference of a variable. Any other case would require that the LHS
489 * is either not an l-value or not a whole array.
491 if (lhs
->type
->array_size() == 0) {
492 ir_dereference
*const d
= lhs
->as_dereference();
496 ir_variable
*const var
= d
->variable_referenced();
500 if (var
->max_array_access
>= unsigned(rhs
->type
->array_size())) {
501 /* FINISHME: This should actually log the location of the RHS. */
502 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
504 var
->max_array_access
);
507 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
508 rhs
->type
->array_size());
512 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
513 * but not post_inc) need the converted assigned value as an rvalue
514 * to handle things like:
518 * So we always just store the computed value being assigned to a
519 * temporary and return a deref of that temporary. If the rvalue
520 * ends up not being used, the temp will get copy-propagated out.
522 ir_variable
*var
= new ir_variable(rhs
->type
, "assignment_tmp");
523 instructions
->push_tail(var
);
524 instructions
->push_tail(new ir_assignment(new ir_dereference_variable(var
),
528 instructions
->push_tail(new ir_assignment(lhs
,
529 new ir_dereference_variable(var
),
532 return new ir_dereference_variable(var
);
537 * Generate a new temporary and add its declaration to the instruction stream
540 generate_temporary(const glsl_type
*type
, exec_list
*instructions
,
541 struct _mesa_glsl_parse_state
*state
)
543 char *name
= (char *) malloc(sizeof(char) * 13);
545 snprintf(name
, 13, "tmp_%08X", state
->temp_index
);
548 ir_variable
*const var
= new ir_variable(type
, name
);
549 instructions
->push_tail(var
);
556 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
560 /* FINISHME: Give unique names to the temporaries. */
561 var
= new ir_variable(lvalue
->type
, "_post_incdec_tmp");
562 var
->mode
= ir_var_auto
;
564 instructions
->push_tail(new ir_assignment(new ir_dereference_variable(var
),
567 /* Once we've created this temporary, mark it read only so it's no
568 * longer considered an lvalue.
570 var
->read_only
= true;
572 return new ir_dereference_variable(var
);
577 ast_node::hir(exec_list
*instructions
,
578 struct _mesa_glsl_parse_state
*state
)
588 ast_expression::hir(exec_list
*instructions
,
589 struct _mesa_glsl_parse_state
*state
)
591 static const int operations
[AST_NUM_OPERATORS
] = {
592 -1, /* ast_assign doesn't convert to ir_expression. */
593 -1, /* ast_plus doesn't convert to ir_expression. */
617 /* Note: The following block of expression types actually convert
618 * to multiple IR instructions.
620 ir_binop_mul
, /* ast_mul_assign */
621 ir_binop_div
, /* ast_div_assign */
622 ir_binop_mod
, /* ast_mod_assign */
623 ir_binop_add
, /* ast_add_assign */
624 ir_binop_sub
, /* ast_sub_assign */
625 ir_binop_lshift
, /* ast_ls_assign */
626 ir_binop_rshift
, /* ast_rs_assign */
627 ir_binop_bit_and
, /* ast_and_assign */
628 ir_binop_bit_xor
, /* ast_xor_assign */
629 ir_binop_bit_or
, /* ast_or_assign */
631 -1, /* ast_conditional doesn't convert to ir_expression. */
632 ir_binop_add
, /* ast_pre_inc. */
633 ir_binop_sub
, /* ast_pre_dec. */
634 ir_binop_add
, /* ast_post_inc. */
635 ir_binop_sub
, /* ast_post_dec. */
636 -1, /* ast_field_selection doesn't conv to ir_expression. */
637 -1, /* ast_array_index doesn't convert to ir_expression. */
638 -1, /* ast_function_call doesn't conv to ir_expression. */
639 -1, /* ast_identifier doesn't convert to ir_expression. */
640 -1, /* ast_int_constant doesn't convert to ir_expression. */
641 -1, /* ast_uint_constant doesn't conv to ir_expression. */
642 -1, /* ast_float_constant doesn't conv to ir_expression. */
643 -1, /* ast_bool_constant doesn't conv to ir_expression. */
644 -1, /* ast_sequence doesn't convert to ir_expression. */
646 ir_rvalue
*result
= NULL
;
648 const struct glsl_type
*type
= glsl_type::error_type
;
649 bool error_emitted
= false;
652 loc
= this->get_location();
654 switch (this->oper
) {
656 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
657 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
659 result
= do_assignment(instructions
, state
, op
[0], op
[1],
660 this->subexpressions
[0]->get_location());
661 error_emitted
= result
->type
->is_error();
667 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
669 error_emitted
= op
[0]->type
->is_error();
670 if (type
->is_error())
677 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
679 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
681 error_emitted
= type
->is_error();
683 result
= new ir_expression(operations
[this->oper
], type
,
691 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
692 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
694 type
= arithmetic_result_type(op
[0], op
[1],
695 (this->oper
== ast_mul
),
697 error_emitted
= type
->is_error();
699 result
= new ir_expression(operations
[this->oper
], type
,
704 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
705 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
707 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
709 assert(operations
[this->oper
] == ir_binop_mod
);
711 result
= new ir_expression(operations
[this->oper
], type
,
713 error_emitted
= type
->is_error();
718 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-shift operators");
719 error_emitted
= true;
726 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
727 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
729 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
731 /* The relational operators must either generate an error or result
732 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
734 assert(type
->is_error()
735 || ((type
->base_type
== GLSL_TYPE_BOOL
)
736 && type
->is_scalar()));
738 result
= new ir_expression(operations
[this->oper
], type
,
740 error_emitted
= type
->is_error();
745 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
746 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
748 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
750 * "The equality operators equal (==), and not equal (!=)
751 * operate on all types. They result in a scalar Boolean. If
752 * the operand types do not match, then there must be a
753 * conversion from Section 4.1.10 "Implicit Conversions"
754 * applied to one operand that can make them match, in which
755 * case this conversion is done."
757 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
758 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
759 || (op
[0]->type
!= op
[1]->type
)) {
760 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
761 "type", (this->oper
== ast_equal
) ? "==" : "!=");
762 error_emitted
= true;
763 } else if ((state
->language_version
<= 110)
764 && (op
[0]->type
->is_array() || op
[1]->type
->is_array())) {
765 _mesa_glsl_error(& loc
, state
, "array comparisons forbidden in "
767 error_emitted
= true;
770 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
772 type
= glsl_type::bool_type
;
774 assert(result
->type
== glsl_type::bool_type
);
781 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-wise operators");
782 error_emitted
= true;
785 case ast_logic_and
: {
786 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
788 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
789 YYLTYPE loc
= this->subexpressions
[0]->get_location();
791 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
792 operator_string(this->oper
));
793 error_emitted
= true;
796 ir_constant
*op0_const
= op
[0]->constant_expression_value();
798 if (op0_const
->value
.b
[0]) {
799 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
801 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
802 YYLTYPE loc
= this->subexpressions
[1]->get_location();
804 _mesa_glsl_error(& loc
, state
,
805 "RHS of `%s' must be scalar boolean",
806 operator_string(this->oper
));
807 error_emitted
= true;
813 type
= glsl_type::bool_type
;
815 ir_if
*const stmt
= new ir_if(op
[0]);
816 instructions
->push_tail(stmt
);
818 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
820 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
821 YYLTYPE loc
= this->subexpressions
[1]->get_location();
823 _mesa_glsl_error(& loc
, state
,
824 "RHS of `%s' must be scalar boolean",
825 operator_string(this->oper
));
826 error_emitted
= true;
829 ir_variable
*const tmp
= generate_temporary(glsl_type::bool_type
,
830 instructions
, state
);
832 ir_dereference
*const then_deref
= new ir_dereference_variable(tmp
);
833 ir_assignment
*const then_assign
=
834 new ir_assignment(then_deref
, op
[1], NULL
);
835 stmt
->then_instructions
.push_tail(then_assign
);
837 ir_dereference
*const else_deref
= new ir_dereference_variable(tmp
);
838 ir_assignment
*const else_assign
=
839 new ir_assignment(else_deref
, new ir_constant(false), NULL
);
840 stmt
->else_instructions
.push_tail(else_assign
);
842 result
= new ir_dereference_variable(tmp
);
849 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
851 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
852 YYLTYPE loc
= this->subexpressions
[0]->get_location();
854 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
855 operator_string(this->oper
));
856 error_emitted
= true;
859 ir_constant
*op0_const
= op
[0]->constant_expression_value();
861 if (op0_const
->value
.b
[0]) {
864 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
866 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
867 YYLTYPE loc
= this->subexpressions
[1]->get_location();
869 _mesa_glsl_error(& loc
, state
,
870 "RHS of `%s' must be scalar boolean",
871 operator_string(this->oper
));
872 error_emitted
= true;
876 type
= glsl_type::bool_type
;
878 ir_if
*const stmt
= new ir_if(op
[0]);
879 instructions
->push_tail(stmt
);
881 ir_variable
*const tmp
= generate_temporary(glsl_type::bool_type
,
882 instructions
, state
);
884 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
886 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
887 YYLTYPE loc
= this->subexpressions
[1]->get_location();
889 _mesa_glsl_error(& loc
, state
, "RHS of `%s' must be scalar boolean",
890 operator_string(this->oper
));
891 error_emitted
= true;
894 ir_dereference
*const then_deref
= new ir_dereference_variable(tmp
);
895 ir_assignment
*const then_assign
=
896 new ir_assignment(then_deref
, new ir_constant(true), NULL
);
897 stmt
->then_instructions
.push_tail(then_assign
);
899 ir_dereference
*const else_deref
= new ir_dereference_variable(tmp
);
900 ir_assignment
*const else_assign
=
901 new ir_assignment(else_deref
, op
[1], NULL
);
902 stmt
->else_instructions
.push_tail(else_assign
);
904 result
= new ir_dereference_variable(tmp
);
911 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
912 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
915 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
917 type
= glsl_type::bool_type
;
921 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
923 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
924 YYLTYPE loc
= this->subexpressions
[0]->get_location();
926 _mesa_glsl_error(& loc
, state
,
927 "operand of `!' must be scalar boolean");
928 error_emitted
= true;
931 result
= new ir_expression(operations
[this->oper
], glsl_type::bool_type
,
933 type
= glsl_type::bool_type
;
939 case ast_sub_assign
: {
940 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
941 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
943 type
= arithmetic_result_type(op
[0], op
[1],
944 (this->oper
== ast_mul_assign
),
947 ir_rvalue
*temp_rhs
= new ir_expression(operations
[this->oper
], type
,
950 result
= do_assignment(instructions
, state
,
951 (ir_rvalue
*)op
[0]->clone(NULL
), temp_rhs
,
952 this->subexpressions
[0]->get_location());
954 error_emitted
= (op
[0]->type
->is_error());
956 /* GLSL 1.10 does not allow array assignment. However, we don't have to
957 * explicitly test for this because none of the binary expression
958 * operators allow array operands either.
964 case ast_mod_assign
: {
965 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
966 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
968 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
970 assert(operations
[this->oper
] == ir_binop_mod
);
972 struct ir_rvalue
*temp_rhs
;
973 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
976 result
= do_assignment(instructions
, state
,
977 (ir_rvalue
*)op
[0]->clone(NULL
), temp_rhs
,
978 this->subexpressions
[0]->get_location());
980 error_emitted
= type
->is_error();
986 _mesa_glsl_error(& loc
, state
,
987 "FINISHME: implement bit-shift assignment operators");
988 error_emitted
= true;
994 _mesa_glsl_error(& loc
, state
,
995 "FINISHME: implement logic assignment operators");
996 error_emitted
= true;
999 case ast_conditional
: {
1000 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1002 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1004 * "The ternary selection operator (?:). It operates on three
1005 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1006 * first expression, which must result in a scalar Boolean."
1008 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
1009 YYLTYPE loc
= this->subexpressions
[0]->get_location();
1011 _mesa_glsl_error(& loc
, state
, "?: condition must be scalar boolean");
1012 error_emitted
= true;
1015 /* The :? operator is implemented by generating an anonymous temporary
1016 * followed by an if-statement. The last instruction in each branch of
1017 * the if-statement assigns a value to the anonymous temporary. This
1018 * temporary is the r-value of the expression.
1020 exec_list then_instructions
;
1021 exec_list else_instructions
;
1023 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1024 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1026 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1028 * "The second and third expressions can be any type, as
1029 * long their types match, or there is a conversion in
1030 * Section 4.1.10 "Implicit Conversions" that can be applied
1031 * to one of the expressions to make their types match. This
1032 * resulting matching type is the type of the entire
1035 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1036 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1037 || (op
[1]->type
!= op
[2]->type
)) {
1038 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1040 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
1041 "operator must have matching types.");
1042 error_emitted
= true;
1043 type
= glsl_type::error_type
;
1048 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1049 ir_constant
*then_val
= op
[1]->constant_expression_value();
1050 ir_constant
*else_val
= op
[2]->constant_expression_value();
1052 if (then_instructions
.is_empty()
1053 && else_instructions
.is_empty()
1054 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1055 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1057 ir_variable
*const tmp
= generate_temporary(type
,
1058 instructions
, state
);
1060 ir_if
*const stmt
= new ir_if(op
[0]);
1061 instructions
->push_tail(stmt
);
1063 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1064 ir_dereference
*const then_deref
= new ir_dereference_variable(tmp
);
1065 ir_assignment
*const then_assign
=
1066 new ir_assignment(then_deref
, op
[1], NULL
);
1067 stmt
->then_instructions
.push_tail(then_assign
);
1069 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1070 ir_dereference
*const else_deref
= new ir_dereference_variable(tmp
);
1071 ir_assignment
*const else_assign
=
1072 new ir_assignment(else_deref
, op
[2], NULL
);
1073 stmt
->else_instructions
.push_tail(else_assign
);
1075 result
= new ir_dereference_variable(tmp
);
1082 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1083 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1084 op
[1] = new ir_constant(1.0f
);
1086 op
[1] = new ir_constant(1);
1088 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1090 struct ir_rvalue
*temp_rhs
;
1091 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
1094 result
= do_assignment(instructions
, state
,
1095 (ir_rvalue
*)op
[0]->clone(NULL
), temp_rhs
,
1096 this->subexpressions
[0]->get_location());
1097 type
= result
->type
;
1098 error_emitted
= op
[0]->type
->is_error();
1103 case ast_post_dec
: {
1104 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1105 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1106 op
[1] = new ir_constant(1.0f
);
1108 op
[1] = new ir_constant(1);
1110 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1112 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1114 struct ir_rvalue
*temp_rhs
;
1115 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
1118 /* Get a temporary of a copy of the lvalue before it's modified.
1119 * This may get thrown away later.
1121 result
= get_lvalue_copy(instructions
, (ir_rvalue
*)op
[0]->clone(NULL
));
1123 (void)do_assignment(instructions
, state
,
1124 (ir_rvalue
*)op
[0]->clone(NULL
), temp_rhs
,
1125 this->subexpressions
[0]->get_location());
1127 type
= result
->type
;
1128 error_emitted
= op
[0]->type
->is_error();
1132 case ast_field_selection
:
1133 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1134 type
= result
->type
;
1137 case ast_array_index
: {
1138 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1140 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1141 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1143 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1145 ir_rvalue
*const array
= op
[0];
1147 result
= new ir_dereference_array(op
[0], op
[1]);
1149 /* Do not use op[0] after this point. Use array.
1157 if (!array
->type
->is_array()
1158 && !array
->type
->is_matrix()
1159 && !array
->type
->is_vector()) {
1160 _mesa_glsl_error(& index_loc
, state
,
1161 "cannot dereference non-array / non-matrix / "
1163 error_emitted
= true;
1166 if (!op
[1]->type
->is_integer()) {
1167 _mesa_glsl_error(& index_loc
, state
,
1168 "array index must be integer type");
1169 error_emitted
= true;
1170 } else if (!op
[1]->type
->is_scalar()) {
1171 _mesa_glsl_error(& index_loc
, state
,
1172 "array index must be scalar");
1173 error_emitted
= true;
1176 /* If the array index is a constant expression and the array has a
1177 * declared size, ensure that the access is in-bounds. If the array
1178 * index is not a constant expression, ensure that the array has a
1181 ir_constant
*const const_index
= op
[1]->constant_expression_value();
1182 if (const_index
!= NULL
) {
1183 const int idx
= const_index
->value
.i
[0];
1184 const char *type_name
;
1187 if (array
->type
->is_matrix()) {
1188 type_name
= "matrix";
1189 } else if (array
->type
->is_vector()) {
1190 type_name
= "vector";
1192 type_name
= "array";
1195 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1197 * "It is illegal to declare an array with a size, and then
1198 * later (in the same shader) index the same array with an
1199 * integral constant expression greater than or equal to the
1200 * declared size. It is also illegal to index an array with a
1201 * negative constant expression."
1203 if (array
->type
->is_matrix()) {
1204 if (array
->type
->row_type()->vector_elements
<= idx
) {
1205 bound
= array
->type
->row_type()->vector_elements
;
1207 } else if (array
->type
->is_vector()) {
1208 if (array
->type
->vector_elements
<= idx
) {
1209 bound
= array
->type
->vector_elements
;
1212 if ((array
->type
->array_size() > 0)
1213 && (array
->type
->array_size() <= idx
)) {
1214 bound
= array
->type
->array_size();
1219 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
1221 error_emitted
= true;
1222 } else if (idx
< 0) {
1223 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
1225 error_emitted
= true;
1228 if (array
->type
->is_array()) {
1229 /* If the array is a variable dereference, it dereferences the
1230 * whole array, by definition. Use this to get the variable.
1232 * FINISHME: Should some methods for getting / setting / testing
1233 * FINISHME: array access limits be added to ir_dereference?
1235 ir_variable
*const v
= array
->whole_variable_referenced();
1236 if ((v
!= NULL
) && (unsigned(idx
) > v
->max_array_access
))
1237 v
->max_array_access
= idx
;
1242 result
->type
= glsl_type::error_type
;
1244 type
= result
->type
;
1248 case ast_function_call
:
1249 /* Should *NEVER* get here. ast_function_call should always be handled
1250 * by ast_function_expression::hir.
1255 case ast_identifier
: {
1256 /* ast_identifier can appear several places in a full abstract syntax
1257 * tree. This particular use must be at location specified in the grammar
1258 * as 'variable_identifier'.
1261 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1263 result
= new ir_dereference_variable(var
);
1266 type
= result
->type
;
1268 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1269 this->primary_expression
.identifier
);
1271 error_emitted
= true;
1276 case ast_int_constant
:
1277 type
= glsl_type::int_type
;
1278 result
= new ir_constant(this->primary_expression
.int_constant
);
1281 case ast_uint_constant
:
1282 type
= glsl_type::uint_type
;
1283 result
= new ir_constant(this->primary_expression
.uint_constant
);
1286 case ast_float_constant
:
1287 type
= glsl_type::float_type
;
1288 result
= new ir_constant(this->primary_expression
.float_constant
);
1291 case ast_bool_constant
:
1292 type
= glsl_type::bool_type
;
1293 result
= new ir_constant(bool(this->primary_expression
.bool_constant
));
1296 case ast_sequence
: {
1297 /* It should not be possible to generate a sequence in the AST without
1298 * any expressions in it.
1300 assert(!this->expressions
.is_empty());
1302 /* The r-value of a sequence is the last expression in the sequence. If
1303 * the other expressions in the sequence do not have side-effects (and
1304 * therefore add instructions to the instruction list), they get dropped
1307 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
)
1308 result
= ast
->hir(instructions
, state
);
1310 type
= result
->type
;
1312 /* Any errors should have already been emitted in the loop above.
1314 error_emitted
= true;
1319 if (type
->is_error() && !error_emitted
)
1320 _mesa_glsl_error(& loc
, state
, "type mismatch");
1327 ast_expression_statement::hir(exec_list
*instructions
,
1328 struct _mesa_glsl_parse_state
*state
)
1330 /* It is possible to have expression statements that don't have an
1331 * expression. This is the solitary semicolon:
1333 * for (i = 0; i < 5; i++)
1336 * In this case the expression will be NULL. Test for NULL and don't do
1337 * anything in that case.
1339 if (expression
!= NULL
)
1340 expression
->hir(instructions
, state
);
1342 /* Statements do not have r-values.
1349 ast_compound_statement::hir(exec_list
*instructions
,
1350 struct _mesa_glsl_parse_state
*state
)
1353 state
->symbols
->push_scope();
1355 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1356 ast
->hir(instructions
, state
);
1359 state
->symbols
->pop_scope();
1361 /* Compound statements do not have r-values.
1367 static const glsl_type
*
1368 process_array_type(const glsl_type
*base
, ast_node
*array_size
,
1369 struct _mesa_glsl_parse_state
*state
)
1371 unsigned length
= 0;
1373 /* FINISHME: Reject delcarations of multidimensional arrays. */
1375 if (array_size
!= NULL
) {
1376 exec_list dummy_instructions
;
1377 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1378 YYLTYPE loc
= array_size
->get_location();
1380 /* FINISHME: Verify that the grammar forbids side-effects in array
1381 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1383 assert(dummy_instructions
.is_empty());
1386 if (!ir
->type
->is_integer()) {
1387 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1388 } else if (!ir
->type
->is_scalar()) {
1389 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1391 ir_constant
*const size
= ir
->constant_expression_value();
1394 _mesa_glsl_error(& loc
, state
, "array size must be a "
1395 "constant valued expression");
1396 } else if (size
->value
.i
[0] <= 0) {
1397 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1399 assert(size
->type
== ir
->type
);
1400 length
= size
->value
.u
[0];
1406 return glsl_type::get_array_instance(base
, length
);
1411 ast_type_specifier::glsl_type(const char **name
,
1412 struct _mesa_glsl_parse_state
*state
) const
1414 const struct glsl_type
*type
;
1416 if ((this->type_specifier
== ast_struct
) && (this->type_name
== NULL
)) {
1417 /* FINISHME: Handle annonymous structures. */
1420 type
= state
->symbols
->get_type(this->type_name
);
1421 *name
= this->type_name
;
1423 if (this->is_array
) {
1424 type
= process_array_type(type
, this->array_size
, state
);
1433 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1434 struct ir_variable
*var
,
1435 struct _mesa_glsl_parse_state
*state
,
1438 if (qual
->invariant
)
1441 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1442 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
1443 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1449 if (qual
->attribute
&& state
->target
!= vertex_shader
) {
1450 var
->type
= glsl_type::error_type
;
1451 _mesa_glsl_error(loc
, state
,
1452 "`attribute' variables may not be declared in the "
1454 _mesa_glsl_shader_target_name(state
->target
));
1457 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1459 * "The varying qualifier can be used only with the data types
1460 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1463 if (qual
->varying
) {
1464 const glsl_type
*non_array_type
;
1466 if (var
->type
&& var
->type
->is_array())
1467 non_array_type
= var
->type
->fields
.array
;
1469 non_array_type
= var
->type
;
1471 if (non_array_type
&& non_array_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1472 var
->type
= glsl_type::error_type
;
1473 _mesa_glsl_error(loc
, state
,
1474 "varying variables must be of base type float");
1478 if (qual
->in
&& qual
->out
)
1479 var
->mode
= ir_var_inout
;
1480 else if (qual
->attribute
|| qual
->in
1481 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1482 var
->mode
= ir_var_in
;
1483 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
1484 var
->mode
= ir_var_out
;
1485 else if (qual
->uniform
)
1486 var
->mode
= ir_var_uniform
;
1488 var
->mode
= ir_var_auto
;
1491 var
->shader_in
= true;
1493 /* Any 'in' or 'inout' variables at global scope must be marked as being
1494 * shader inputs. Likewise, any 'out' or 'inout' variables at global scope
1495 * must be marked as being shader outputs.
1497 if (state
->current_function
== NULL
) {
1498 switch (var
->mode
) {
1500 case ir_var_uniform
:
1501 var
->shader_in
= true;
1504 var
->shader_out
= true;
1507 var
->shader_in
= true;
1508 var
->shader_out
= true;
1516 var
->interpolation
= ir_var_flat
;
1517 else if (qual
->noperspective
)
1518 var
->interpolation
= ir_var_noperspective
;
1520 var
->interpolation
= ir_var_smooth
;
1522 if (var
->type
->is_array() && (state
->language_version
>= 120)) {
1523 var
->array_lvalue
= true;
1529 ast_declarator_list::hir(exec_list
*instructions
,
1530 struct _mesa_glsl_parse_state
*state
)
1532 const struct glsl_type
*decl_type
;
1533 const char *type_name
= NULL
;
1534 ir_rvalue
*result
= NULL
;
1535 YYLTYPE loc
= this->get_location();
1537 /* The type specifier may contain a structure definition. Process that
1538 * before any of the variable declarations.
1540 (void) this->type
->specifier
->hir(instructions
, state
);
1542 /* FINISHME: Handle vertex shader "invariant" declarations that do not
1543 * FINISHME: include a type. These re-declare built-in variables to be
1544 * FINISHME: invariant.
1547 decl_type
= this->type
->specifier
->glsl_type(& type_name
, state
);
1548 if (this->declarations
.is_empty()) {
1549 /* There are only two valid cases where the declaration list can be
1552 * 1. The declaration is setting the default precision of a built-in
1553 * type (e.g., 'precision highp vec4;').
1555 * 2. Adding 'invariant' to an existing vertex shader output.
1558 if (this->type
->qualifier
.invariant
) {
1559 } else if (decl_type
!= NULL
) {
1561 _mesa_glsl_error(& loc
, state
, "incomplete declaration");
1565 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1566 const struct glsl_type
*var_type
;
1567 struct ir_variable
*var
;
1569 /* FINISHME: Emit a warning if a variable declaration shadows a
1570 * FINISHME: declaration at a higher scope.
1573 if ((decl_type
== NULL
) || decl_type
->is_void()) {
1574 if (type_name
!= NULL
) {
1575 _mesa_glsl_error(& loc
, state
,
1576 "invalid type `%s' in declaration of `%s'",
1577 type_name
, decl
->identifier
);
1579 _mesa_glsl_error(& loc
, state
,
1580 "invalid type in declaration of `%s'",
1586 if (decl
->is_array
) {
1587 var_type
= process_array_type(decl_type
, decl
->array_size
, state
);
1589 var_type
= decl_type
;
1592 var
= new ir_variable(var_type
, decl
->identifier
);
1594 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1596 * "Global variables can only use the qualifiers const,
1597 * attribute, uni form, or varying. Only one may be
1600 * Local variables can only use the qualifier const."
1602 * This is relaxed in GLSL 1.30.
1604 if (state
->language_version
< 120) {
1605 if (this->type
->qualifier
.out
) {
1606 _mesa_glsl_error(& loc
, state
,
1607 "`out' qualifier in declaration of `%s' "
1608 "only valid for function parameters in GLSL 1.10.",
1611 if (this->type
->qualifier
.in
) {
1612 _mesa_glsl_error(& loc
, state
,
1613 "`in' qualifier in declaration of `%s' "
1614 "only valid for function parameters in GLSL 1.10.",
1617 /* FINISHME: Test for other invalid qualifiers. */
1620 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
1623 /* Attempt to add the variable to the symbol table. If this fails, it
1624 * means the variable has already been declared at this scope. Arrays
1625 * fudge this rule a little bit.
1627 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1629 * "It is legal to declare an array without a size and then
1630 * later re-declare the same name as an array of the same
1631 * type and specify a size."
1633 if (state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
1634 ir_variable
*const earlier
=
1635 state
->symbols
->get_variable(decl
->identifier
);
1637 if ((earlier
!= NULL
)
1638 && (earlier
->type
->array_size() == 0)
1639 && var
->type
->is_array()
1640 && (var
->type
->element_type() == earlier
->type
->element_type())) {
1641 /* FINISHME: This doesn't match the qualifiers on the two
1642 * FINISHME: declarations. It's not 100% clear whether this is
1643 * FINISHME: required or not.
1646 if (var
->type
->array_size() <= (int)earlier
->max_array_access
) {
1647 YYLTYPE loc
= this->get_location();
1649 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
1651 earlier
->max_array_access
);
1654 earlier
->type
= var
->type
;
1658 YYLTYPE loc
= this->get_location();
1660 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
1667 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1669 * "Identifiers starting with "gl_" are reserved for use by
1670 * OpenGL, and may not be declared in a shader as either a
1671 * variable or a function."
1673 if (strncmp(decl
->identifier
, "gl_", 3) == 0) {
1674 /* FINISHME: This should only trigger if we're not redefining
1675 * FINISHME: a builtin (to add a qualifier, for example).
1677 _mesa_glsl_error(& loc
, state
,
1678 "identifier `%s' uses reserved `gl_' prefix",
1682 instructions
->push_tail(var
);
1684 if (state
->current_function
!= NULL
) {
1685 const char *mode
= NULL
;
1686 const char *extra
= "";
1688 /* There is no need to check for 'inout' here because the parser will
1689 * only allow that in function parameter lists.
1691 if (this->type
->qualifier
.attribute
) {
1693 } else if (this->type
->qualifier
.uniform
) {
1695 } else if (this->type
->qualifier
.varying
) {
1697 } else if (this->type
->qualifier
.in
) {
1699 extra
= " or in function parameter list";
1700 } else if (this->type
->qualifier
.out
) {
1702 extra
= " or in function parameter list";
1706 _mesa_glsl_error(& loc
, state
,
1707 "%s variable `%s' must be declared at "
1709 mode
, var
->name
, extra
);
1711 } else if (var
->mode
== ir_var_in
) {
1712 if (state
->target
== vertex_shader
) {
1713 bool error_emitted
= false;
1715 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1717 * "Vertex shader inputs can only be float, floating-point
1718 * vectors, matrices, signed and unsigned integers and integer
1719 * vectors. Vertex shader inputs can also form arrays of these
1720 * types, but not structures."
1722 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1724 * "Vertex shader inputs can only be float, floating-point
1725 * vectors, matrices, signed and unsigned integers and integer
1726 * vectors. They cannot be arrays or structures."
1728 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1730 * "The attribute qualifier can be used only with float,
1731 * floating-point vectors, and matrices. Attribute variables
1732 * cannot be declared as arrays or structures."
1734 const glsl_type
*check_type
= var
->type
->is_array()
1735 ? var
->type
->fields
.array
: var
->type
;
1737 switch (check_type
->base_type
) {
1738 case GLSL_TYPE_FLOAT
:
1740 case GLSL_TYPE_UINT
:
1742 if (state
->language_version
> 120)
1746 _mesa_glsl_error(& loc
, state
,
1747 "vertex shader input / attribute cannot have "
1749 var
->type
->is_array() ? "array of " : "",
1751 error_emitted
= true;
1754 if (!error_emitted
&& (state
->language_version
<= 130)
1755 && var
->type
->is_array()) {
1756 _mesa_glsl_error(& loc
, state
,
1757 "vertex shader input / attribute cannot have "
1759 error_emitted
= true;
1764 if (decl
->initializer
!= NULL
) {
1765 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
1767 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1769 * "All uniform variables are read-only and are initialized either
1770 * directly by an application via API commands, or indirectly by
1773 if ((state
->language_version
<= 110)
1774 && (var
->mode
== ir_var_uniform
)) {
1775 _mesa_glsl_error(& initializer_loc
, state
,
1776 "cannot initialize uniforms in GLSL 1.10");
1779 if (var
->type
->is_sampler()) {
1780 _mesa_glsl_error(& initializer_loc
, state
,
1781 "cannot initialize samplers");
1784 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
1785 _mesa_glsl_error(& initializer_loc
, state
,
1786 "cannot initialize %s shader input / %s",
1787 _mesa_glsl_shader_target_name(state
->target
),
1788 (state
->target
== vertex_shader
)
1789 ? "attribute" : "varying");
1792 ir_dereference
*const lhs
= new ir_dereference_variable(var
);
1793 ir_rvalue
*rhs
= decl
->initializer
->hir(instructions
, state
);
1795 /* Calculate the constant value if this is a const or uniform
1798 if (this->type
->qualifier
.constant
|| this->type
->qualifier
.uniform
) {
1799 ir_constant
*constant_value
= rhs
->constant_expression_value();
1800 if (!constant_value
) {
1801 _mesa_glsl_error(& initializer_loc
, state
,
1802 "initializer of %s variable `%s' must be a "
1803 "constant expression",
1804 (this->type
->qualifier
.constant
)
1805 ? "const" : "uniform",
1808 rhs
= constant_value
;
1809 var
->constant_value
= constant_value
;
1813 if (rhs
&& !rhs
->type
->is_error()) {
1814 bool temp
= var
->read_only
;
1815 if (this->type
->qualifier
.constant
)
1816 var
->read_only
= false;
1818 /* Never emit code to initialize a uniform.
1820 if (!this->type
->qualifier
.uniform
)
1821 result
= do_assignment(instructions
, state
, lhs
, rhs
,
1822 this->get_location());
1823 var
->read_only
= temp
;
1827 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1829 * "It is an error to write to a const variable outside of
1830 * its declaration, so they must be initialized when
1833 if (this->type
->qualifier
.constant
&& decl
->initializer
== NULL
) {
1834 _mesa_glsl_error(& loc
, state
,
1835 "const declaration of `%s' must be initialized");
1838 /* Add the vairable to the symbol table after processing the initializer.
1839 * This differs from most C-like languages, but it follows the GLSL
1840 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1843 * "Within a declaration, the scope of a name starts immediately
1844 * after the initializer if present or immediately after the name
1845 * being declared if not."
1847 const bool added_variable
=
1848 state
->symbols
->add_variable(decl
->identifier
, var
);
1849 assert(added_variable
);
1853 /* Generally, variable declarations do not have r-values. However,
1854 * one is used for the declaration in
1856 * while (bool b = some_condition()) {
1860 * so we return the rvalue from the last seen declaration here.
1867 ast_parameter_declarator::hir(exec_list
*instructions
,
1868 struct _mesa_glsl_parse_state
*state
)
1870 const struct glsl_type
*type
;
1871 const char *name
= NULL
;
1872 YYLTYPE loc
= this->get_location();
1874 type
= this->type
->specifier
->glsl_type(& name
, state
);
1878 _mesa_glsl_error(& loc
, state
,
1879 "invalid type `%s' in declaration of `%s'",
1880 name
, this->identifier
);
1882 _mesa_glsl_error(& loc
, state
,
1883 "invalid type in declaration of `%s'",
1887 type
= glsl_type::error_type
;
1890 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1892 * "Functions that accept no input arguments need not use void in the
1893 * argument list because prototypes (or definitions) are required and
1894 * therefore there is no ambiguity when an empty argument list "( )" is
1895 * declared. The idiom "(void)" as a parameter list is provided for
1898 * Placing this check here prevents a void parameter being set up
1899 * for a function, which avoids tripping up checks for main taking
1900 * parameters and lookups of an unnamed symbol.
1902 if (type
->is_void()) {
1903 if (this->identifier
!= NULL
)
1904 _mesa_glsl_error(& loc
, state
,
1905 "named parameter cannot have type `void'");
1911 if (formal_parameter
&& (this->identifier
== NULL
)) {
1912 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
1917 ir_variable
*var
= new ir_variable(type
, this->identifier
);
1919 /* FINISHME: Handle array declarations. Note that this requires
1920 * FINISHME: complete handling of constant expressions.
1923 /* Apply any specified qualifiers to the parameter declaration. Note that
1924 * for function parameters the default mode is 'in'.
1926 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
1927 if (var
->mode
== ir_var_auto
)
1928 var
->mode
= ir_var_in
;
1930 instructions
->push_tail(var
);
1932 /* Parameter declarations do not have r-values.
1939 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
1941 exec_list
*ir_parameters
,
1942 _mesa_glsl_parse_state
*state
)
1944 ast_parameter_declarator
*void_param
= NULL
;
1947 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
1948 param
->formal_parameter
= formal
;
1949 param
->hir(ir_parameters
, state
);
1957 if ((void_param
!= NULL
) && (count
> 1)) {
1958 YYLTYPE loc
= void_param
->get_location();
1960 _mesa_glsl_error(& loc
, state
,
1961 "`void' parameter must be only parameter");
1967 ast_function::hir(exec_list
*instructions
,
1968 struct _mesa_glsl_parse_state
*state
)
1970 ir_function
*f
= NULL
;
1971 ir_function_signature
*sig
= NULL
;
1972 exec_list hir_parameters
;
1975 /* Convert the list of function parameters to HIR now so that they can be
1976 * used below to compare this function's signature with previously seen
1977 * signatures for functions with the same name.
1979 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
1981 & hir_parameters
, state
);
1983 const char *return_type_name
;
1984 const glsl_type
*return_type
=
1985 this->return_type
->specifier
->glsl_type(& return_type_name
, state
);
1987 assert(return_type
!= NULL
);
1989 /* Verify that this function's signature either doesn't match a previously
1990 * seen signature for a function with the same name, or, if a match is found,
1991 * that the previously seen signature does not have an associated definition.
1993 const char *const name
= identifier
;
1994 f
= state
->symbols
->get_function(name
);
1996 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
1998 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
1999 if (badvar
!= NULL
) {
2000 YYLTYPE loc
= this->get_location();
2002 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
2003 "qualifiers don't match prototype", name
, badvar
);
2006 if (sig
->return_type
!= return_type
) {
2007 YYLTYPE loc
= this->get_location();
2009 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
2010 "match prototype", name
);
2013 if (is_definition
&& sig
->is_defined
) {
2014 YYLTYPE loc
= this->get_location();
2016 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
2020 } else if (state
->symbols
->name_declared_this_scope(name
)) {
2021 /* This function name shadows a non-function use of the same name.
2023 YYLTYPE loc
= this->get_location();
2025 _mesa_glsl_error(& loc
, state
, "function name `%s' conflicts with "
2026 "non-function", name
);
2029 f
= new ir_function(name
);
2030 state
->symbols
->add_function(f
->name
, f
);
2032 /* Emit the new function header */
2033 instructions
->push_tail(f
);
2036 /* Verify the return type of main() */
2037 if (strcmp(name
, "main") == 0) {
2038 if (! return_type
->is_void()) {
2039 YYLTYPE loc
= this->get_location();
2041 _mesa_glsl_error(& loc
, state
, "main() must return void");
2044 if (!hir_parameters
.is_empty()) {
2045 YYLTYPE loc
= this->get_location();
2047 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
2051 /* Finish storing the information about this new function in its signature.
2054 sig
= new ir_function_signature(return_type
);
2055 f
->add_signature(sig
);
2058 sig
->replace_parameters(&hir_parameters
);
2061 /* Function declarations (prototypes) do not have r-values.
2068 ast_function_definition::hir(exec_list
*instructions
,
2069 struct _mesa_glsl_parse_state
*state
)
2071 prototype
->is_definition
= true;
2072 prototype
->hir(instructions
, state
);
2074 ir_function_signature
*signature
= prototype
->signature
;
2076 assert(state
->current_function
== NULL
);
2077 state
->current_function
= signature
;
2079 /* Duplicate parameters declared in the prototype as concrete variables.
2080 * Add these to the symbol table.
2082 state
->symbols
->push_scope();
2083 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
2084 ir_variable
*const var
= ((ir_instruction
*) iter
.get())->as_variable();
2086 assert(var
!= NULL
);
2088 /* The only way a parameter would "exist" is if two parameters have
2091 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
2092 YYLTYPE loc
= this->get_location();
2094 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
2096 state
->symbols
->add_variable(var
->name
, var
);
2100 /* Convert the body of the function to HIR. */
2101 this->body
->hir(&signature
->body
, state
);
2102 signature
->is_defined
= true;
2104 state
->symbols
->pop_scope();
2106 assert(state
->current_function
== signature
);
2107 state
->current_function
= NULL
;
2109 /* Function definitions do not have r-values.
2116 ast_jump_statement::hir(exec_list
*instructions
,
2117 struct _mesa_glsl_parse_state
*state
)
2123 assert(state
->current_function
);
2125 if (opt_return_value
) {
2126 if (state
->current_function
->return_type
->base_type
==
2128 YYLTYPE loc
= this->get_location();
2130 _mesa_glsl_error(& loc
, state
,
2131 "`return` with a value, in function `%s' "
2133 state
->current_function
->function_name());
2136 ir_expression
*const ret
= (ir_expression
*)
2137 opt_return_value
->hir(instructions
, state
);
2138 assert(ret
!= NULL
);
2140 /* FINISHME: Make sure the type of the return value matches the return
2141 * FINISHME: type of the enclosing function.
2144 inst
= new ir_return(ret
);
2146 if (state
->current_function
->return_type
->base_type
!=
2148 YYLTYPE loc
= this->get_location();
2150 _mesa_glsl_error(& loc
, state
,
2151 "`return' with no value, in function %s returning "
2153 state
->current_function
->function_name());
2155 inst
= new ir_return
;
2158 instructions
->push_tail(inst
);
2163 /* FINISHME: discard support */
2164 if (state
->target
!= fragment_shader
) {
2165 YYLTYPE loc
= this->get_location();
2167 _mesa_glsl_error(& loc
, state
,
2168 "`discard' may only appear in a fragment shader");
2174 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2175 * FINISHME: and they use a different IR instruction for 'break'.
2177 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2178 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2181 if (state
->loop_or_switch_nesting
== NULL
) {
2182 YYLTYPE loc
= this->get_location();
2184 _mesa_glsl_error(& loc
, state
,
2185 "`%s' may only appear in a loop",
2186 (mode
== ast_break
) ? "break" : "continue");
2188 ir_loop
*const loop
= state
->loop_or_switch_nesting
->as_loop();
2191 ir_loop_jump
*const jump
=
2192 new ir_loop_jump((mode
== ast_break
)
2193 ? ir_loop_jump::jump_break
2194 : ir_loop_jump::jump_continue
);
2195 instructions
->push_tail(jump
);
2202 /* Jump instructions do not have r-values.
2209 ast_selection_statement::hir(exec_list
*instructions
,
2210 struct _mesa_glsl_parse_state
*state
)
2212 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
2214 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2216 * "Any expression whose type evaluates to a Boolean can be used as the
2217 * conditional expression bool-expression. Vector types are not accepted
2218 * as the expression to if."
2220 * The checks are separated so that higher quality diagnostics can be
2221 * generated for cases where both rules are violated.
2223 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
2224 YYLTYPE loc
= this->condition
->get_location();
2226 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
2230 ir_if
*const stmt
= new ir_if(condition
);
2232 if (then_statement
!= NULL
)
2233 then_statement
->hir(& stmt
->then_instructions
, state
);
2235 if (else_statement
!= NULL
)
2236 else_statement
->hir(& stmt
->else_instructions
, state
);
2238 instructions
->push_tail(stmt
);
2240 /* if-statements do not have r-values.
2247 ast_iteration_statement::condition_to_hir(ir_loop
*stmt
,
2248 struct _mesa_glsl_parse_state
*state
)
2250 if (condition
!= NULL
) {
2251 ir_rvalue
*const cond
=
2252 condition
->hir(& stmt
->body_instructions
, state
);
2255 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
2256 YYLTYPE loc
= condition
->get_location();
2258 _mesa_glsl_error(& loc
, state
,
2259 "loop condition must be scalar boolean");
2261 /* As the first code in the loop body, generate a block that looks
2262 * like 'if (!condition) break;' as the loop termination condition.
2264 ir_rvalue
*const not_cond
=
2265 new ir_expression(ir_unop_logic_not
, glsl_type::bool_type
, cond
,
2268 ir_if
*const if_stmt
= new ir_if(not_cond
);
2270 ir_jump
*const break_stmt
=
2271 new ir_loop_jump(ir_loop_jump::jump_break
);
2273 if_stmt
->then_instructions
.push_tail(break_stmt
);
2274 stmt
->body_instructions
.push_tail(if_stmt
);
2281 ast_iteration_statement::hir(exec_list
*instructions
,
2282 struct _mesa_glsl_parse_state
*state
)
2284 /* For-loops and while-loops start a new scope, but do-while loops do not.
2286 if (mode
!= ast_do_while
)
2287 state
->symbols
->push_scope();
2289 if (init_statement
!= NULL
)
2290 init_statement
->hir(instructions
, state
);
2292 ir_loop
*const stmt
= new ir_loop();
2293 instructions
->push_tail(stmt
);
2295 /* Track the current loop and / or switch-statement nesting.
2297 ir_instruction
*const nesting
= state
->loop_or_switch_nesting
;
2298 state
->loop_or_switch_nesting
= stmt
;
2300 if (mode
!= ast_do_while
)
2301 condition_to_hir(stmt
, state
);
2304 body
->hir(& stmt
->body_instructions
, state
);
2306 if (rest_expression
!= NULL
)
2307 rest_expression
->hir(& stmt
->body_instructions
, state
);
2309 if (mode
== ast_do_while
)
2310 condition_to_hir(stmt
, state
);
2312 if (mode
!= ast_do_while
)
2313 state
->symbols
->pop_scope();
2315 /* Restore previous nesting before returning.
2317 state
->loop_or_switch_nesting
= nesting
;
2319 /* Loops do not have r-values.
2326 ast_type_specifier::hir(exec_list
*instructions
,
2327 struct _mesa_glsl_parse_state
*state
)
2329 if (this->structure
!= NULL
)
2330 return this->structure
->hir(instructions
, state
);
2337 ast_struct_specifier::hir(exec_list
*instructions
,
2338 struct _mesa_glsl_parse_state
*state
)
2340 unsigned decl_count
= 0;
2342 /* Make an initial pass over the list of structure fields to determine how
2343 * many there are. Each element in this list is an ast_declarator_list.
2344 * This means that we actually need to count the number of elements in the
2345 * 'declarations' list in each of the elements.
2347 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2348 &this->declarations
) {
2349 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
2355 /* Allocate storage for the structure fields and process the field
2356 * declarations. As the declarations are processed, try to also convert
2357 * the types to HIR. This ensures that structure definitions embedded in
2358 * other structure definitions are processed.
2360 glsl_struct_field
*const fields
= (glsl_struct_field
*)
2361 malloc(sizeof(*fields
) * decl_count
);
2364 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2365 &this->declarations
) {
2366 const char *type_name
;
2368 decl_list
->type
->specifier
->hir(instructions
, state
);
2370 const glsl_type
*decl_type
=
2371 decl_list
->type
->specifier
->glsl_type(& type_name
, state
);
2373 foreach_list_typed (ast_declaration
, decl
, link
,
2374 &decl_list
->declarations
) {
2375 const struct glsl_type
*const field_type
=
2377 ? process_array_type(decl_type
, decl
->array_size
, state
)
2380 fields
[i
].type
= (field_type
!= NULL
)
2381 ? field_type
: glsl_type::error_type
;
2382 fields
[i
].name
= decl
->identifier
;
2387 assert(i
== decl_count
);
2390 if (this->name
== NULL
) {
2391 static unsigned anon_count
= 1;
2394 snprintf(buf
, sizeof(buf
), "#anon_struct_%04x", anon_count
);
2402 glsl_type
*t
= new glsl_type(fields
, decl_count
, name
);
2404 YYLTYPE loc
= this->get_location();
2405 if (!state
->symbols
->add_type(name
, t
)) {
2406 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
2408 /* This logic is a bit tricky. It is an error to declare a structure at
2409 * global scope if there is also a function with the same name.
2411 if ((state
->current_function
== NULL
)
2412 && (state
->symbols
->get_function(name
) != NULL
)) {
2413 _mesa_glsl_error(& loc
, state
, "name `%s' previously defined", name
);
2415 t
->generate_constructor(state
->symbols
);
2418 const glsl_type
**s
= (const glsl_type
**)
2419 realloc(state
->user_structures
,
2420 sizeof(state
->user_structures
[0]) *
2421 (state
->num_user_structures
+ 1));
2423 s
[state
->num_user_structures
] = t
;
2424 state
->user_structures
= s
;
2425 state
->num_user_structures
++;
2429 /* Structure type definitions do not have r-values.