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 void *ctx
= talloc_parent(state
);
91 if (to
->base_type
== from
->type
->base_type
)
94 /* This conversion was added in GLSL 1.20. If the compilation mode is
95 * GLSL 1.10, the conversion is skipped.
97 if (state
->language_version
< 120)
100 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
102 * "There are no implicit array or structure conversions. For
103 * example, an array of int cannot be implicitly converted to an
104 * array of float. There are no implicit conversions between
105 * signed and unsigned integers."
107 /* FINISHME: The above comment is partially a lie. There is int/uint
108 * FINISHME: conversion for immediate constants.
110 if (!to
->is_float() || !from
->type
->is_numeric())
113 switch (from
->type
->base_type
) {
115 from
= new(ctx
) ir_expression(ir_unop_i2f
, to
, from
, NULL
);
118 from
= new(ctx
) ir_expression(ir_unop_u2f
, to
, from
, NULL
);
121 from
= new(ctx
) ir_expression(ir_unop_b2f
, to
, from
, NULL
);
131 static const struct glsl_type
*
132 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
134 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
136 const glsl_type
*type_a
= value_a
->type
;
137 const glsl_type
*type_b
= value_b
->type
;
139 /* From GLSL 1.50 spec, page 56:
141 * "The arithmetic binary operators add (+), subtract (-),
142 * multiply (*), and divide (/) operate on integer and
143 * floating-point scalars, vectors, and matrices."
145 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
146 _mesa_glsl_error(loc
, state
,
147 "Operands to arithmetic operators must be numeric");
148 return glsl_type::error_type
;
152 /* "If one operand is floating-point based and the other is
153 * not, then the conversions from Section 4.1.10 "Implicit
154 * Conversions" are applied to the non-floating-point-based operand."
156 if (!apply_implicit_conversion(type_a
, value_b
, state
)
157 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
158 _mesa_glsl_error(loc
, state
,
159 "Could not implicitly convert operands to "
160 "arithmetic operator");
161 return glsl_type::error_type
;
163 type_a
= value_a
->type
;
164 type_b
= value_b
->type
;
166 /* "If the operands are integer types, they must both be signed or
169 * From this rule and the preceeding conversion it can be inferred that
170 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
171 * The is_numeric check above already filtered out the case where either
172 * type is not one of these, so now the base types need only be tested for
175 if (type_a
->base_type
!= type_b
->base_type
) {
176 _mesa_glsl_error(loc
, state
,
177 "base type mismatch for arithmetic operator");
178 return glsl_type::error_type
;
181 /* "All arithmetic binary operators result in the same fundamental type
182 * (signed integer, unsigned integer, or floating-point) as the
183 * operands they operate on, after operand type conversion. After
184 * conversion, the following cases are valid
186 * * The two operands are scalars. In this case the operation is
187 * applied, resulting in a scalar."
189 if (type_a
->is_scalar() && type_b
->is_scalar())
192 /* "* One operand is a scalar, and the other is a vector or matrix.
193 * In this case, the scalar operation is applied independently to each
194 * component of the vector or matrix, resulting in the same size
197 if (type_a
->is_scalar()) {
198 if (!type_b
->is_scalar())
200 } else if (type_b
->is_scalar()) {
204 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
205 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
208 assert(!type_a
->is_scalar());
209 assert(!type_b
->is_scalar());
211 /* "* The two operands are vectors of the same size. In this case, the
212 * operation is done component-wise resulting in the same size
215 if (type_a
->is_vector() && type_b
->is_vector()) {
216 if (type_a
== type_b
) {
219 _mesa_glsl_error(loc
, state
,
220 "vector size mismatch for arithmetic operator");
221 return glsl_type::error_type
;
225 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
226 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
227 * <vector, vector> have been handled. At least one of the operands must
228 * be matrix. Further, since there are no integer matrix types, the base
229 * type of both operands must be float.
231 assert(type_a
->is_matrix() || type_b
->is_matrix());
232 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
233 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
235 /* "* The operator is add (+), subtract (-), or divide (/), and the
236 * operands are matrices with the same number of rows and the same
237 * number of columns. In this case, the operation is done component-
238 * wise resulting in the same size matrix."
239 * * The operator is multiply (*), where both operands are matrices or
240 * one operand is a vector and the other a matrix. A right vector
241 * operand is treated as a column vector and a left vector operand as a
242 * row vector. In all these cases, it is required that the number of
243 * columns of the left operand is equal to the number of rows of the
244 * right operand. Then, the multiply (*) operation does a linear
245 * algebraic multiply, yielding an object that has the same number of
246 * rows as the left operand and the same number of columns as the right
247 * operand. Section 5.10 "Vector and Matrix Operations" explains in
248 * more detail how vectors and matrices are operated on."
251 if (type_a
== type_b
)
254 if (type_a
->is_matrix() && type_b
->is_matrix()) {
255 /* Matrix multiply. The columns of A must match the rows of B. Given
256 * the other previously tested constraints, this means the vector type
257 * of a row from A must be the same as the vector type of a column from
260 if (type_a
->row_type() == type_b
->column_type()) {
261 /* The resulting matrix has the number of columns of matrix B and
262 * the number of rows of matrix A. We get the row count of A by
263 * looking at the size of a vector that makes up a column. The
264 * transpose (size of a row) is done for B.
266 const glsl_type
*const type
=
267 glsl_type::get_instance(type_a
->base_type
,
268 type_a
->column_type()->vector_elements
,
269 type_b
->row_type()->vector_elements
);
270 assert(type
!= glsl_type::error_type
);
274 } else if (type_a
->is_matrix()) {
275 /* A is a matrix and B is a column vector. Columns of A must match
276 * rows of B. Given the other previously tested constraints, this
277 * means the vector type of a row from A must be the same as the
278 * vector the type of B.
280 if (type_a
->row_type() == type_b
)
283 assert(type_b
->is_matrix());
285 /* A is a row vector and B is a matrix. Columns of A must match rows
286 * of B. Given the other previously tested constraints, this means
287 * the type of A must be the same as the vector type of a column from
290 if (type_a
== type_b
->column_type())
294 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
295 return glsl_type::error_type
;
299 /* "All other cases are illegal."
301 _mesa_glsl_error(loc
, state
, "type mismatch");
302 return glsl_type::error_type
;
306 static const struct glsl_type
*
307 unary_arithmetic_result_type(const struct glsl_type
*type
,
308 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
310 /* From GLSL 1.50 spec, page 57:
312 * "The arithmetic unary operators negate (-), post- and pre-increment
313 * and decrement (-- and ++) operate on integer or floating-point
314 * values (including vectors and matrices). All unary operators work
315 * component-wise on their operands. These result with the same type
318 if (!type
->is_numeric()) {
319 _mesa_glsl_error(loc
, state
,
320 "Operands to arithmetic operators must be numeric");
321 return glsl_type::error_type
;
328 static const struct glsl_type
*
329 modulus_result_type(const struct glsl_type
*type_a
,
330 const struct glsl_type
*type_b
,
331 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
333 /* From GLSL 1.50 spec, page 56:
334 * "The operator modulus (%) operates on signed or unsigned integers or
335 * integer vectors. The operand types must both be signed or both be
338 if (!type_a
->is_integer() || !type_b
->is_integer()
339 || (type_a
->base_type
!= type_b
->base_type
)) {
340 _mesa_glsl_error(loc
, state
, "type mismatch");
341 return glsl_type::error_type
;
344 /* "The operands cannot be vectors of differing size. If one operand is
345 * a scalar and the other vector, then the scalar is applied component-
346 * wise to the vector, resulting in the same type as the vector. If both
347 * are vectors of the same size, the result is computed component-wise."
349 if (type_a
->is_vector()) {
350 if (!type_b
->is_vector()
351 || (type_a
->vector_elements
== type_b
->vector_elements
))
356 /* "The operator modulus (%) is not defined for any other data types
357 * (non-integer types)."
359 _mesa_glsl_error(loc
, state
, "type mismatch");
360 return glsl_type::error_type
;
364 static const struct glsl_type
*
365 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
366 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
368 const glsl_type
*type_a
= value_a
->type
;
369 const glsl_type
*type_b
= value_b
->type
;
371 /* From GLSL 1.50 spec, page 56:
372 * "The relational operators greater than (>), less than (<), greater
373 * than or equal (>=), and less than or equal (<=) operate only on
374 * scalar integer and scalar floating-point expressions."
376 if (!type_a
->is_numeric()
377 || !type_b
->is_numeric()
378 || !type_a
->is_scalar()
379 || !type_b
->is_scalar()) {
380 _mesa_glsl_error(loc
, state
,
381 "Operands to relational operators must be scalar and "
383 return glsl_type::error_type
;
386 /* "Either the operands' types must match, or the conversions from
387 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
388 * operand, after which the types must match."
390 if (!apply_implicit_conversion(type_a
, value_b
, state
)
391 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
392 _mesa_glsl_error(loc
, state
,
393 "Could not implicitly convert operands to "
394 "relational operator");
395 return glsl_type::error_type
;
397 type_a
= value_a
->type
;
398 type_b
= value_b
->type
;
400 if (type_a
->base_type
!= type_b
->base_type
) {
401 _mesa_glsl_error(loc
, state
, "base type mismatch");
402 return glsl_type::error_type
;
405 /* "The result is scalar Boolean."
407 return glsl_type::bool_type
;
412 * Validates that a value can be assigned to a location with a specified type
414 * Validates that \c rhs can be assigned to some location. If the types are
415 * not an exact match but an automatic conversion is possible, \c rhs will be
419 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
420 * Otherwise the actual RHS to be assigned will be returned. This may be
421 * \c rhs, or it may be \c rhs after some type conversion.
424 * In addition to being used for assignments, this function is used to
425 * type-check return values.
428 validate_assignment(struct _mesa_glsl_parse_state
*state
,
429 const glsl_type
*lhs_type
, ir_rvalue
*rhs
)
431 const glsl_type
*rhs_type
= rhs
->type
;
433 /* If there is already some error in the RHS, just return it. Anything
434 * else will lead to an avalanche of error message back to the user.
436 if (rhs_type
->is_error())
439 /* If the types are identical, the assignment can trivially proceed.
441 if (rhs_type
== lhs_type
)
444 /* If the array element types are the same and the size of the LHS is zero,
445 * the assignment is okay.
447 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
448 * is handled by ir_dereference::is_lvalue.
450 if (lhs_type
->is_array() && rhs
->type
->is_array()
451 && (lhs_type
->element_type() == rhs
->type
->element_type())
452 && (lhs_type
->array_size() == 0)) {
456 /* Check for implicit conversion in GLSL 1.20 */
457 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
458 rhs_type
= rhs
->type
;
459 if (rhs_type
== lhs_type
)
467 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
468 ir_rvalue
*lhs
, ir_rvalue
*rhs
,
471 void *ctx
= talloc_parent(state
);
472 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
474 if (!error_emitted
) {
475 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
476 if (!lhs
->is_lvalue()) {
477 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
478 error_emitted
= true;
482 ir_rvalue
*new_rhs
= validate_assignment(state
, lhs
->type
, rhs
);
483 if (new_rhs
== NULL
) {
484 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
488 /* If the LHS array was not declared with a size, it takes it size from
489 * the RHS. If the LHS is an l-value and a whole array, it must be a
490 * dereference of a variable. Any other case would require that the LHS
491 * is either not an l-value or not a whole array.
493 if (lhs
->type
->array_size() == 0) {
494 ir_dereference
*const d
= lhs
->as_dereference();
498 ir_variable
*const var
= d
->variable_referenced();
502 if (var
->max_array_access
>= unsigned(rhs
->type
->array_size())) {
503 /* FINISHME: This should actually log the location of the RHS. */
504 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
506 var
->max_array_access
);
509 var
->type
= glsl_type::get_array_instance(state
,
510 lhs
->type
->element_type(),
511 rhs
->type
->array_size());
515 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
516 * but not post_inc) need the converted assigned value as an rvalue
517 * to handle things like:
521 * So we always just store the computed value being assigned to a
522 * temporary and return a deref of that temporary. If the rvalue
523 * ends up not being used, the temp will get copy-propagated out.
525 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp");
526 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
527 instructions
->push_tail(var
);
528 instructions
->push_tail(new(ctx
) ir_assignment(deref_var
,
531 deref_var
= new(ctx
) ir_dereference_variable(var
);
533 instructions
->push_tail(new(ctx
) ir_assignment(lhs
,
537 return new(ctx
) ir_dereference_variable(var
);
542 * Generate a new temporary and add its declaration to the instruction stream
545 generate_temporary(const glsl_type
*type
, exec_list
*instructions
,
546 struct _mesa_glsl_parse_state
*state
)
548 void *ctx
= talloc_parent(state
);
549 char *name
= (char *) malloc(sizeof(char) * 13);
551 snprintf(name
, 13, "tmp_%08X", state
->temp_index
);
554 ir_variable
*const var
= new(ctx
) ir_variable(type
, name
);
555 instructions
->push_tail(var
);
562 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
564 void *ctx
= talloc_parent(lvalue
);
567 /* FINISHME: Give unique names to the temporaries. */
568 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp");
569 var
->mode
= ir_var_auto
;
571 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
574 /* Once we've created this temporary, mark it read only so it's no
575 * longer considered an lvalue.
577 var
->read_only
= true;
579 return new(ctx
) ir_dereference_variable(var
);
584 ast_node::hir(exec_list
*instructions
,
585 struct _mesa_glsl_parse_state
*state
)
595 ast_expression::hir(exec_list
*instructions
,
596 struct _mesa_glsl_parse_state
*state
)
598 void *ctx
= talloc_parent(state
);
599 static const int operations
[AST_NUM_OPERATORS
] = {
600 -1, /* ast_assign doesn't convert to ir_expression. */
601 -1, /* ast_plus doesn't convert to ir_expression. */
625 /* Note: The following block of expression types actually convert
626 * to multiple IR instructions.
628 ir_binop_mul
, /* ast_mul_assign */
629 ir_binop_div
, /* ast_div_assign */
630 ir_binop_mod
, /* ast_mod_assign */
631 ir_binop_add
, /* ast_add_assign */
632 ir_binop_sub
, /* ast_sub_assign */
633 ir_binop_lshift
, /* ast_ls_assign */
634 ir_binop_rshift
, /* ast_rs_assign */
635 ir_binop_bit_and
, /* ast_and_assign */
636 ir_binop_bit_xor
, /* ast_xor_assign */
637 ir_binop_bit_or
, /* ast_or_assign */
639 -1, /* ast_conditional doesn't convert to ir_expression. */
640 ir_binop_add
, /* ast_pre_inc. */
641 ir_binop_sub
, /* ast_pre_dec. */
642 ir_binop_add
, /* ast_post_inc. */
643 ir_binop_sub
, /* ast_post_dec. */
644 -1, /* ast_field_selection doesn't conv to ir_expression. */
645 -1, /* ast_array_index doesn't convert to ir_expression. */
646 -1, /* ast_function_call doesn't conv to ir_expression. */
647 -1, /* ast_identifier doesn't convert to ir_expression. */
648 -1, /* ast_int_constant doesn't convert to ir_expression. */
649 -1, /* ast_uint_constant doesn't conv to ir_expression. */
650 -1, /* ast_float_constant doesn't conv to ir_expression. */
651 -1, /* ast_bool_constant doesn't conv to ir_expression. */
652 -1, /* ast_sequence doesn't convert to ir_expression. */
654 ir_rvalue
*result
= NULL
;
656 const struct glsl_type
*type
= glsl_type::error_type
;
657 bool error_emitted
= false;
660 loc
= this->get_location();
662 switch (this->oper
) {
664 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
665 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
667 result
= do_assignment(instructions
, state
, op
[0], op
[1],
668 this->subexpressions
[0]->get_location());
669 error_emitted
= result
->type
->is_error();
675 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
677 error_emitted
= op
[0]->type
->is_error();
678 if (type
->is_error())
685 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
687 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
689 error_emitted
= type
->is_error();
691 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
699 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
700 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
702 type
= arithmetic_result_type(op
[0], op
[1],
703 (this->oper
== ast_mul
),
705 error_emitted
= type
->is_error();
707 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
712 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
713 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
715 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
717 assert(operations
[this->oper
] == ir_binop_mod
);
719 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
721 error_emitted
= type
->is_error();
726 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-shift operators");
727 error_emitted
= true;
734 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
735 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
737 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
739 /* The relational operators must either generate an error or result
740 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
742 assert(type
->is_error()
743 || ((type
->base_type
== GLSL_TYPE_BOOL
)
744 && type
->is_scalar()));
746 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
748 error_emitted
= type
->is_error();
753 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
754 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
756 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
758 * "The equality operators equal (==), and not equal (!=)
759 * operate on all types. They result in a scalar Boolean. If
760 * the operand types do not match, then there must be a
761 * conversion from Section 4.1.10 "Implicit Conversions"
762 * applied to one operand that can make them match, in which
763 * case this conversion is done."
765 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
766 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
767 || (op
[0]->type
!= op
[1]->type
)) {
768 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
769 "type", (this->oper
== ast_equal
) ? "==" : "!=");
770 error_emitted
= true;
771 } else if ((state
->language_version
<= 110)
772 && (op
[0]->type
->is_array() || op
[1]->type
->is_array())) {
773 _mesa_glsl_error(& loc
, state
, "array comparisons forbidden in "
775 error_emitted
= true;
778 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
780 type
= glsl_type::bool_type
;
782 assert(result
->type
== glsl_type::bool_type
);
789 _mesa_glsl_error(& loc
, state
, "FINISHME: implement bit-wise operators");
790 error_emitted
= true;
793 case ast_logic_and
: {
794 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
796 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
797 YYLTYPE loc
= this->subexpressions
[0]->get_location();
799 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
800 operator_string(this->oper
));
801 error_emitted
= true;
804 ir_constant
*op0_const
= op
[0]->constant_expression_value();
806 if (op0_const
->value
.b
[0]) {
807 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
809 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
810 YYLTYPE loc
= this->subexpressions
[1]->get_location();
812 _mesa_glsl_error(& loc
, state
,
813 "RHS of `%s' must be scalar boolean",
814 operator_string(this->oper
));
815 error_emitted
= true;
821 type
= glsl_type::bool_type
;
823 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
824 instructions
->push_tail(stmt
);
826 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
828 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
829 YYLTYPE loc
= this->subexpressions
[1]->get_location();
831 _mesa_glsl_error(& loc
, state
,
832 "RHS of `%s' must be scalar boolean",
833 operator_string(this->oper
));
834 error_emitted
= true;
837 ir_variable
*const tmp
= generate_temporary(glsl_type::bool_type
,
838 instructions
, state
);
840 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
841 ir_assignment
*const then_assign
=
842 new(ctx
) ir_assignment(then_deref
, op
[1], NULL
);
843 stmt
->then_instructions
.push_tail(then_assign
);
845 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
846 ir_assignment
*const else_assign
=
847 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false), NULL
);
848 stmt
->else_instructions
.push_tail(else_assign
);
850 result
= new(ctx
) ir_dereference_variable(tmp
);
857 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
859 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
860 YYLTYPE loc
= this->subexpressions
[0]->get_location();
862 _mesa_glsl_error(& loc
, state
, "LHS of `%s' must be scalar boolean",
863 operator_string(this->oper
));
864 error_emitted
= true;
867 ir_constant
*op0_const
= op
[0]->constant_expression_value();
869 if (op0_const
->value
.b
[0]) {
872 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
874 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
875 YYLTYPE loc
= this->subexpressions
[1]->get_location();
877 _mesa_glsl_error(& loc
, state
,
878 "RHS of `%s' must be scalar boolean",
879 operator_string(this->oper
));
880 error_emitted
= true;
884 type
= glsl_type::bool_type
;
886 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
887 instructions
->push_tail(stmt
);
889 ir_variable
*const tmp
= generate_temporary(glsl_type::bool_type
,
890 instructions
, state
);
892 op
[1] = this->subexpressions
[1]->hir(&stmt
->then_instructions
, state
);
894 if (!op
[1]->type
->is_boolean() || !op
[1]->type
->is_scalar()) {
895 YYLTYPE loc
= this->subexpressions
[1]->get_location();
897 _mesa_glsl_error(& loc
, state
, "RHS of `%s' must be scalar boolean",
898 operator_string(this->oper
));
899 error_emitted
= true;
902 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
903 ir_assignment
*const then_assign
=
904 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true), NULL
);
905 stmt
->then_instructions
.push_tail(then_assign
);
907 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
908 ir_assignment
*const else_assign
=
909 new(ctx
) ir_assignment(else_deref
, op
[1], NULL
);
910 stmt
->else_instructions
.push_tail(else_assign
);
912 result
= new(ctx
) ir_dereference_variable(tmp
);
919 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
920 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
923 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
925 type
= glsl_type::bool_type
;
929 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
931 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
932 YYLTYPE loc
= this->subexpressions
[0]->get_location();
934 _mesa_glsl_error(& loc
, state
,
935 "operand of `!' must be scalar boolean");
936 error_emitted
= true;
939 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
941 type
= glsl_type::bool_type
;
947 case ast_sub_assign
: {
948 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
949 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
951 type
= arithmetic_result_type(op
[0], op
[1],
952 (this->oper
== ast_mul_assign
),
955 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
958 result
= do_assignment(instructions
, state
,
959 (ir_rvalue
*)op
[0]->clone(NULL
), temp_rhs
,
960 this->subexpressions
[0]->get_location());
962 error_emitted
= (op
[0]->type
->is_error());
964 /* GLSL 1.10 does not allow array assignment. However, we don't have to
965 * explicitly test for this because none of the binary expression
966 * operators allow array operands either.
972 case ast_mod_assign
: {
973 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
974 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
976 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
978 assert(operations
[this->oper
] == ir_binop_mod
);
980 struct ir_rvalue
*temp_rhs
;
981 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
984 result
= do_assignment(instructions
, state
,
985 (ir_rvalue
*)op
[0]->clone(NULL
), temp_rhs
,
986 this->subexpressions
[0]->get_location());
988 error_emitted
= type
->is_error();
994 _mesa_glsl_error(& loc
, state
,
995 "FINISHME: implement bit-shift assignment operators");
996 error_emitted
= true;
1000 case ast_xor_assign
:
1002 _mesa_glsl_error(& loc
, state
,
1003 "FINISHME: implement logic assignment operators");
1004 error_emitted
= true;
1007 case ast_conditional
: {
1008 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1010 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1012 * "The ternary selection operator (?:). It operates on three
1013 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1014 * first expression, which must result in a scalar Boolean."
1016 if (!op
[0]->type
->is_boolean() || !op
[0]->type
->is_scalar()) {
1017 YYLTYPE loc
= this->subexpressions
[0]->get_location();
1019 _mesa_glsl_error(& loc
, state
, "?: condition must be scalar boolean");
1020 error_emitted
= true;
1023 /* The :? operator is implemented by generating an anonymous temporary
1024 * followed by an if-statement. The last instruction in each branch of
1025 * the if-statement assigns a value to the anonymous temporary. This
1026 * temporary is the r-value of the expression.
1028 exec_list then_instructions
;
1029 exec_list else_instructions
;
1031 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1032 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1034 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1036 * "The second and third expressions can be any type, as
1037 * long their types match, or there is a conversion in
1038 * Section 4.1.10 "Implicit Conversions" that can be applied
1039 * to one of the expressions to make their types match. This
1040 * resulting matching type is the type of the entire
1043 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1044 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1045 || (op
[1]->type
!= op
[2]->type
)) {
1046 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1048 _mesa_glsl_error(& loc
, state
, "Second and third operands of ?: "
1049 "operator must have matching types.");
1050 error_emitted
= true;
1051 type
= glsl_type::error_type
;
1056 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1057 ir_constant
*then_val
= op
[1]->constant_expression_value();
1058 ir_constant
*else_val
= op
[2]->constant_expression_value();
1060 if (then_instructions
.is_empty()
1061 && else_instructions
.is_empty()
1062 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1063 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1065 ir_variable
*const tmp
= generate_temporary(type
,
1066 instructions
, state
);
1068 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1069 instructions
->push_tail(stmt
);
1071 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1072 ir_dereference
*const then_deref
=
1073 new(ctx
) ir_dereference_variable(tmp
);
1074 ir_assignment
*const then_assign
=
1075 new(ctx
) ir_assignment(then_deref
, op
[1], NULL
);
1076 stmt
->then_instructions
.push_tail(then_assign
);
1078 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1079 ir_dereference
*const else_deref
=
1080 new(ctx
) ir_dereference_variable(tmp
);
1081 ir_assignment
*const else_assign
=
1082 new(ctx
) ir_assignment(else_deref
, op
[2], NULL
);
1083 stmt
->else_instructions
.push_tail(else_assign
);
1085 result
= new(ctx
) ir_dereference_variable(tmp
);
1092 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1093 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1094 op
[1] = new(ctx
) ir_constant(1.0f
);
1096 op
[1] = new(ctx
) ir_constant(1);
1098 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1100 struct ir_rvalue
*temp_rhs
;
1101 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1104 result
= do_assignment(instructions
, state
,
1105 (ir_rvalue
*)op
[0]->clone(NULL
), temp_rhs
,
1106 this->subexpressions
[0]->get_location());
1107 type
= result
->type
;
1108 error_emitted
= op
[0]->type
->is_error();
1113 case ast_post_dec
: {
1114 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1115 if (op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
)
1116 op
[1] = new(ctx
) ir_constant(1.0f
);
1118 op
[1] = new(ctx
) ir_constant(1);
1120 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1122 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1124 struct ir_rvalue
*temp_rhs
;
1125 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1128 /* Get a temporary of a copy of the lvalue before it's modified.
1129 * This may get thrown away later.
1131 result
= get_lvalue_copy(instructions
, (ir_rvalue
*)op
[0]->clone(NULL
));
1133 (void)do_assignment(instructions
, state
,
1134 (ir_rvalue
*)op
[0]->clone(NULL
), temp_rhs
,
1135 this->subexpressions
[0]->get_location());
1137 type
= result
->type
;
1138 error_emitted
= op
[0]->type
->is_error();
1142 case ast_field_selection
:
1143 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1144 type
= result
->type
;
1147 case ast_array_index
: {
1148 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1150 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1151 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1153 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1155 ir_rvalue
*const array
= op
[0];
1157 result
= new(ctx
) ir_dereference_array(op
[0], op
[1]);
1159 /* Do not use op[0] after this point. Use array.
1167 if (!array
->type
->is_array()
1168 && !array
->type
->is_matrix()
1169 && !array
->type
->is_vector()) {
1170 _mesa_glsl_error(& index_loc
, state
,
1171 "cannot dereference non-array / non-matrix / "
1173 error_emitted
= true;
1176 if (!op
[1]->type
->is_integer()) {
1177 _mesa_glsl_error(& index_loc
, state
,
1178 "array index must be integer type");
1179 error_emitted
= true;
1180 } else if (!op
[1]->type
->is_scalar()) {
1181 _mesa_glsl_error(& index_loc
, state
,
1182 "array index must be scalar");
1183 error_emitted
= true;
1186 /* If the array index is a constant expression and the array has a
1187 * declared size, ensure that the access is in-bounds. If the array
1188 * index is not a constant expression, ensure that the array has a
1191 ir_constant
*const const_index
= op
[1]->constant_expression_value();
1192 if (const_index
!= NULL
) {
1193 const int idx
= const_index
->value
.i
[0];
1194 const char *type_name
;
1197 if (array
->type
->is_matrix()) {
1198 type_name
= "matrix";
1199 } else if (array
->type
->is_vector()) {
1200 type_name
= "vector";
1202 type_name
= "array";
1205 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1207 * "It is illegal to declare an array with a size, and then
1208 * later (in the same shader) index the same array with an
1209 * integral constant expression greater than or equal to the
1210 * declared size. It is also illegal to index an array with a
1211 * negative constant expression."
1213 if (array
->type
->is_matrix()) {
1214 if (array
->type
->row_type()->vector_elements
<= idx
) {
1215 bound
= array
->type
->row_type()->vector_elements
;
1217 } else if (array
->type
->is_vector()) {
1218 if (array
->type
->vector_elements
<= idx
) {
1219 bound
= array
->type
->vector_elements
;
1222 if ((array
->type
->array_size() > 0)
1223 && (array
->type
->array_size() <= idx
)) {
1224 bound
= array
->type
->array_size();
1229 _mesa_glsl_error(& loc
, state
, "%s index must be < %u",
1231 error_emitted
= true;
1232 } else if (idx
< 0) {
1233 _mesa_glsl_error(& loc
, state
, "%s index must be >= 0",
1235 error_emitted
= true;
1238 if (array
->type
->is_array()) {
1239 /* If the array is a variable dereference, it dereferences the
1240 * whole array, by definition. Use this to get the variable.
1242 * FINISHME: Should some methods for getting / setting / testing
1243 * FINISHME: array access limits be added to ir_dereference?
1245 ir_variable
*const v
= array
->whole_variable_referenced();
1246 if ((v
!= NULL
) && (unsigned(idx
) > v
->max_array_access
))
1247 v
->max_array_access
= idx
;
1252 result
->type
= glsl_type::error_type
;
1254 type
= result
->type
;
1258 case ast_function_call
:
1259 /* Should *NEVER* get here. ast_function_call should always be handled
1260 * by ast_function_expression::hir.
1265 case ast_identifier
: {
1266 /* ast_identifier can appear several places in a full abstract syntax
1267 * tree. This particular use must be at location specified in the grammar
1268 * as 'variable_identifier'.
1271 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1273 result
= new(ctx
) ir_dereference_variable(var
);
1276 type
= result
->type
;
1278 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1279 this->primary_expression
.identifier
);
1281 error_emitted
= true;
1286 case ast_int_constant
:
1287 type
= glsl_type::int_type
;
1288 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1291 case ast_uint_constant
:
1292 type
= glsl_type::uint_type
;
1293 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1296 case ast_float_constant
:
1297 type
= glsl_type::float_type
;
1298 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1301 case ast_bool_constant
:
1302 type
= glsl_type::bool_type
;
1303 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1306 case ast_sequence
: {
1307 /* It should not be possible to generate a sequence in the AST without
1308 * any expressions in it.
1310 assert(!this->expressions
.is_empty());
1312 /* The r-value of a sequence is the last expression in the sequence. If
1313 * the other expressions in the sequence do not have side-effects (and
1314 * therefore add instructions to the instruction list), they get dropped
1317 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
)
1318 result
= ast
->hir(instructions
, state
);
1320 type
= result
->type
;
1322 /* Any errors should have already been emitted in the loop above.
1324 error_emitted
= true;
1329 if (type
->is_error() && !error_emitted
)
1330 _mesa_glsl_error(& loc
, state
, "type mismatch");
1337 ast_expression_statement::hir(exec_list
*instructions
,
1338 struct _mesa_glsl_parse_state
*state
)
1340 /* It is possible to have expression statements that don't have an
1341 * expression. This is the solitary semicolon:
1343 * for (i = 0; i < 5; i++)
1346 * In this case the expression will be NULL. Test for NULL and don't do
1347 * anything in that case.
1349 if (expression
!= NULL
)
1350 expression
->hir(instructions
, state
);
1352 /* Statements do not have r-values.
1359 ast_compound_statement::hir(exec_list
*instructions
,
1360 struct _mesa_glsl_parse_state
*state
)
1363 state
->symbols
->push_scope();
1365 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1366 ast
->hir(instructions
, state
);
1369 state
->symbols
->pop_scope();
1371 /* Compound statements do not have r-values.
1377 static const glsl_type
*
1378 process_array_type(const glsl_type
*base
, ast_node
*array_size
,
1379 struct _mesa_glsl_parse_state
*state
)
1381 unsigned length
= 0;
1383 /* FINISHME: Reject delcarations of multidimensional arrays. */
1385 if (array_size
!= NULL
) {
1386 exec_list dummy_instructions
;
1387 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1388 YYLTYPE loc
= array_size
->get_location();
1390 /* FINISHME: Verify that the grammar forbids side-effects in array
1391 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1393 assert(dummy_instructions
.is_empty());
1396 if (!ir
->type
->is_integer()) {
1397 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1398 } else if (!ir
->type
->is_scalar()) {
1399 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1401 ir_constant
*const size
= ir
->constant_expression_value();
1404 _mesa_glsl_error(& loc
, state
, "array size must be a "
1405 "constant valued expression");
1406 } else if (size
->value
.i
[0] <= 0) {
1407 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1409 assert(size
->type
== ir
->type
);
1410 length
= size
->value
.u
[0];
1416 return glsl_type::get_array_instance(state
, base
, length
);
1421 ast_type_specifier::glsl_type(const char **name
,
1422 struct _mesa_glsl_parse_state
*state
) const
1424 const struct glsl_type
*type
;
1426 if ((this->type_specifier
== ast_struct
) && (this->type_name
== NULL
)) {
1427 /* FINISHME: Handle annonymous structures. */
1430 type
= state
->symbols
->get_type(this->type_name
);
1431 *name
= this->type_name
;
1433 if (this->is_array
) {
1434 type
= process_array_type(type
, this->array_size
, state
);
1443 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1444 struct ir_variable
*var
,
1445 struct _mesa_glsl_parse_state
*state
,
1448 if (qual
->invariant
)
1451 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1452 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
1453 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1459 if (qual
->attribute
&& state
->target
!= vertex_shader
) {
1460 var
->type
= glsl_type::error_type
;
1461 _mesa_glsl_error(loc
, state
,
1462 "`attribute' variables may not be declared in the "
1464 _mesa_glsl_shader_target_name(state
->target
));
1467 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1469 * "The varying qualifier can be used only with the data types
1470 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1473 if (qual
->varying
) {
1474 const glsl_type
*non_array_type
;
1476 if (var
->type
&& var
->type
->is_array())
1477 non_array_type
= var
->type
->fields
.array
;
1479 non_array_type
= var
->type
;
1481 if (non_array_type
&& non_array_type
->base_type
!= GLSL_TYPE_FLOAT
) {
1482 var
->type
= glsl_type::error_type
;
1483 _mesa_glsl_error(loc
, state
,
1484 "varying variables must be of base type float");
1488 if (qual
->in
&& qual
->out
)
1489 var
->mode
= ir_var_inout
;
1490 else if (qual
->attribute
|| qual
->in
1491 || (qual
->varying
&& (state
->target
== fragment_shader
)))
1492 var
->mode
= ir_var_in
;
1493 else if (qual
->out
|| (qual
->varying
&& (state
->target
== vertex_shader
)))
1494 var
->mode
= ir_var_out
;
1495 else if (qual
->uniform
)
1496 var
->mode
= ir_var_uniform
;
1498 var
->mode
= ir_var_auto
;
1501 var
->shader_in
= true;
1503 /* Any 'in' or 'inout' variables at global scope must be marked as being
1504 * shader inputs. Likewise, any 'out' or 'inout' variables at global scope
1505 * must be marked as being shader outputs.
1507 if (state
->current_function
== NULL
) {
1508 switch (var
->mode
) {
1510 case ir_var_uniform
:
1511 var
->shader_in
= true;
1514 var
->shader_out
= true;
1517 var
->shader_in
= true;
1518 var
->shader_out
= true;
1526 var
->interpolation
= ir_var_flat
;
1527 else if (qual
->noperspective
)
1528 var
->interpolation
= ir_var_noperspective
;
1530 var
->interpolation
= ir_var_smooth
;
1532 if (var
->type
->is_array() && (state
->language_version
>= 120)) {
1533 var
->array_lvalue
= true;
1539 ast_declarator_list::hir(exec_list
*instructions
,
1540 struct _mesa_glsl_parse_state
*state
)
1542 void *ctx
= talloc_parent(state
);
1543 const struct glsl_type
*decl_type
;
1544 const char *type_name
= NULL
;
1545 ir_rvalue
*result
= NULL
;
1546 YYLTYPE loc
= this->get_location();
1548 /* The type specifier may contain a structure definition. Process that
1549 * before any of the variable declarations.
1551 (void) this->type
->specifier
->hir(instructions
, state
);
1553 /* FINISHME: Handle vertex shader "invariant" declarations that do not
1554 * FINISHME: include a type. These re-declare built-in variables to be
1555 * FINISHME: invariant.
1558 decl_type
= this->type
->specifier
->glsl_type(& type_name
, state
);
1559 if (this->declarations
.is_empty()) {
1560 /* There are only two valid cases where the declaration list can be
1563 * 1. The declaration is setting the default precision of a built-in
1564 * type (e.g., 'precision highp vec4;').
1566 * 2. Adding 'invariant' to an existing vertex shader output.
1569 if (this->type
->qualifier
.invariant
) {
1570 } else if (decl_type
!= NULL
) {
1572 _mesa_glsl_error(& loc
, state
, "incomplete declaration");
1576 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
1577 const struct glsl_type
*var_type
;
1578 struct ir_variable
*var
;
1580 /* FINISHME: Emit a warning if a variable declaration shadows a
1581 * FINISHME: declaration at a higher scope.
1584 if ((decl_type
== NULL
) || decl_type
->is_void()) {
1585 if (type_name
!= NULL
) {
1586 _mesa_glsl_error(& loc
, state
,
1587 "invalid type `%s' in declaration of `%s'",
1588 type_name
, decl
->identifier
);
1590 _mesa_glsl_error(& loc
, state
,
1591 "invalid type in declaration of `%s'",
1597 if (decl
->is_array
) {
1598 var_type
= process_array_type(decl_type
, decl
->array_size
, state
);
1600 var_type
= decl_type
;
1603 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
);
1605 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1607 * "Global variables can only use the qualifiers const,
1608 * attribute, uni form, or varying. Only one may be
1611 * Local variables can only use the qualifier const."
1613 * This is relaxed in GLSL 1.30.
1615 if (state
->language_version
< 120) {
1616 if (this->type
->qualifier
.out
) {
1617 _mesa_glsl_error(& loc
, state
,
1618 "`out' qualifier in declaration of `%s' "
1619 "only valid for function parameters in GLSL 1.10.",
1622 if (this->type
->qualifier
.in
) {
1623 _mesa_glsl_error(& loc
, state
,
1624 "`in' qualifier in declaration of `%s' "
1625 "only valid for function parameters in GLSL 1.10.",
1628 /* FINISHME: Test for other invalid qualifiers. */
1631 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
1634 /* Attempt to add the variable to the symbol table. If this fails, it
1635 * means the variable has already been declared at this scope. Arrays
1636 * fudge this rule a little bit.
1638 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1640 * "It is legal to declare an array without a size and then
1641 * later re-declare the same name as an array of the same
1642 * type and specify a size."
1644 if (state
->symbols
->name_declared_this_scope(decl
->identifier
)) {
1645 ir_variable
*const earlier
=
1646 state
->symbols
->get_variable(decl
->identifier
);
1648 if ((earlier
!= NULL
)
1649 && (earlier
->type
->array_size() == 0)
1650 && var
->type
->is_array()
1651 && (var
->type
->element_type() == earlier
->type
->element_type())) {
1652 /* FINISHME: This doesn't match the qualifiers on the two
1653 * FINISHME: declarations. It's not 100% clear whether this is
1654 * FINISHME: required or not.
1657 if (var
->type
->array_size() <= (int)earlier
->max_array_access
) {
1658 YYLTYPE loc
= this->get_location();
1660 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
1662 earlier
->max_array_access
);
1665 earlier
->type
= var
->type
;
1669 YYLTYPE loc
= this->get_location();
1671 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
1678 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1680 * "Identifiers starting with "gl_" are reserved for use by
1681 * OpenGL, and may not be declared in a shader as either a
1682 * variable or a function."
1684 if (strncmp(decl
->identifier
, "gl_", 3) == 0) {
1685 /* FINISHME: This should only trigger if we're not redefining
1686 * FINISHME: a builtin (to add a qualifier, for example).
1688 _mesa_glsl_error(& loc
, state
,
1689 "identifier `%s' uses reserved `gl_' prefix",
1693 instructions
->push_tail(var
);
1695 if (state
->current_function
!= NULL
) {
1696 const char *mode
= NULL
;
1697 const char *extra
= "";
1699 /* There is no need to check for 'inout' here because the parser will
1700 * only allow that in function parameter lists.
1702 if (this->type
->qualifier
.attribute
) {
1704 } else if (this->type
->qualifier
.uniform
) {
1706 } else if (this->type
->qualifier
.varying
) {
1708 } else if (this->type
->qualifier
.in
) {
1710 extra
= " or in function parameter list";
1711 } else if (this->type
->qualifier
.out
) {
1713 extra
= " or in function parameter list";
1717 _mesa_glsl_error(& loc
, state
,
1718 "%s variable `%s' must be declared at "
1720 mode
, var
->name
, extra
);
1722 } else if (var
->mode
== ir_var_in
) {
1723 if (state
->target
== vertex_shader
) {
1724 bool error_emitted
= false;
1726 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1728 * "Vertex shader inputs can only be float, floating-point
1729 * vectors, matrices, signed and unsigned integers and integer
1730 * vectors. Vertex shader inputs can also form arrays of these
1731 * types, but not structures."
1733 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1735 * "Vertex shader inputs can only be float, floating-point
1736 * vectors, matrices, signed and unsigned integers and integer
1737 * vectors. They cannot be arrays or structures."
1739 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1741 * "The attribute qualifier can be used only with float,
1742 * floating-point vectors, and matrices. Attribute variables
1743 * cannot be declared as arrays or structures."
1745 const glsl_type
*check_type
= var
->type
->is_array()
1746 ? var
->type
->fields
.array
: var
->type
;
1748 switch (check_type
->base_type
) {
1749 case GLSL_TYPE_FLOAT
:
1751 case GLSL_TYPE_UINT
:
1753 if (state
->language_version
> 120)
1757 _mesa_glsl_error(& loc
, state
,
1758 "vertex shader input / attribute cannot have "
1760 var
->type
->is_array() ? "array of " : "",
1762 error_emitted
= true;
1765 if (!error_emitted
&& (state
->language_version
<= 130)
1766 && var
->type
->is_array()) {
1767 _mesa_glsl_error(& loc
, state
,
1768 "vertex shader input / attribute cannot have "
1770 error_emitted
= true;
1775 if (decl
->initializer
!= NULL
) {
1776 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
1778 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1780 * "All uniform variables are read-only and are initialized either
1781 * directly by an application via API commands, or indirectly by
1784 if ((state
->language_version
<= 110)
1785 && (var
->mode
== ir_var_uniform
)) {
1786 _mesa_glsl_error(& initializer_loc
, state
,
1787 "cannot initialize uniforms in GLSL 1.10");
1790 if (var
->type
->is_sampler()) {
1791 _mesa_glsl_error(& initializer_loc
, state
,
1792 "cannot initialize samplers");
1795 if ((var
->mode
== ir_var_in
) && (state
->current_function
== NULL
)) {
1796 _mesa_glsl_error(& initializer_loc
, state
,
1797 "cannot initialize %s shader input / %s",
1798 _mesa_glsl_shader_target_name(state
->target
),
1799 (state
->target
== vertex_shader
)
1800 ? "attribute" : "varying");
1803 ir_dereference
*const lhs
= new(ctx
) ir_dereference_variable(var
);
1804 ir_rvalue
*rhs
= decl
->initializer
->hir(instructions
, state
);
1806 /* Calculate the constant value if this is a const or uniform
1809 if (this->type
->qualifier
.constant
|| this->type
->qualifier
.uniform
) {
1810 ir_constant
*constant_value
= rhs
->constant_expression_value();
1811 if (!constant_value
) {
1812 _mesa_glsl_error(& initializer_loc
, state
,
1813 "initializer of %s variable `%s' must be a "
1814 "constant expression",
1815 (this->type
->qualifier
.constant
)
1816 ? "const" : "uniform",
1819 rhs
= constant_value
;
1820 var
->constant_value
= constant_value
;
1824 if (rhs
&& !rhs
->type
->is_error()) {
1825 bool temp
= var
->read_only
;
1826 if (this->type
->qualifier
.constant
)
1827 var
->read_only
= false;
1829 /* Never emit code to initialize a uniform.
1831 if (!this->type
->qualifier
.uniform
)
1832 result
= do_assignment(instructions
, state
, lhs
, rhs
,
1833 this->get_location());
1834 var
->read_only
= temp
;
1838 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1840 * "It is an error to write to a const variable outside of
1841 * its declaration, so they must be initialized when
1844 if (this->type
->qualifier
.constant
&& decl
->initializer
== NULL
) {
1845 _mesa_glsl_error(& loc
, state
,
1846 "const declaration of `%s' must be initialized");
1849 /* Add the vairable to the symbol table after processing the initializer.
1850 * This differs from most C-like languages, but it follows the GLSL
1851 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1854 * "Within a declaration, the scope of a name starts immediately
1855 * after the initializer if present or immediately after the name
1856 * being declared if not."
1858 const bool added_variable
=
1859 state
->symbols
->add_variable(decl
->identifier
, var
);
1860 assert(added_variable
);
1864 /* Generally, variable declarations do not have r-values. However,
1865 * one is used for the declaration in
1867 * while (bool b = some_condition()) {
1871 * so we return the rvalue from the last seen declaration here.
1878 ast_parameter_declarator::hir(exec_list
*instructions
,
1879 struct _mesa_glsl_parse_state
*state
)
1881 void *ctx
= talloc_parent(state
);
1882 const struct glsl_type
*type
;
1883 const char *name
= NULL
;
1884 YYLTYPE loc
= this->get_location();
1886 type
= this->type
->specifier
->glsl_type(& name
, state
);
1890 _mesa_glsl_error(& loc
, state
,
1891 "invalid type `%s' in declaration of `%s'",
1892 name
, this->identifier
);
1894 _mesa_glsl_error(& loc
, state
,
1895 "invalid type in declaration of `%s'",
1899 type
= glsl_type::error_type
;
1902 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1904 * "Functions that accept no input arguments need not use void in the
1905 * argument list because prototypes (or definitions) are required and
1906 * therefore there is no ambiguity when an empty argument list "( )" is
1907 * declared. The idiom "(void)" as a parameter list is provided for
1910 * Placing this check here prevents a void parameter being set up
1911 * for a function, which avoids tripping up checks for main taking
1912 * parameters and lookups of an unnamed symbol.
1914 if (type
->is_void()) {
1915 if (this->identifier
!= NULL
)
1916 _mesa_glsl_error(& loc
, state
,
1917 "named parameter cannot have type `void'");
1923 if (formal_parameter
&& (this->identifier
== NULL
)) {
1924 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
1929 ir_variable
*var
= new(ctx
) ir_variable(type
, this->identifier
);
1931 /* FINISHME: Handle array declarations. Note that this requires
1932 * FINISHME: complete handling of constant expressions.
1935 /* Apply any specified qualifiers to the parameter declaration. Note that
1936 * for function parameters the default mode is 'in'.
1938 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
);
1939 if (var
->mode
== ir_var_auto
)
1940 var
->mode
= ir_var_in
;
1942 instructions
->push_tail(var
);
1944 /* Parameter declarations do not have r-values.
1951 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
1953 exec_list
*ir_parameters
,
1954 _mesa_glsl_parse_state
*state
)
1956 ast_parameter_declarator
*void_param
= NULL
;
1959 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
1960 param
->formal_parameter
= formal
;
1961 param
->hir(ir_parameters
, state
);
1969 if ((void_param
!= NULL
) && (count
> 1)) {
1970 YYLTYPE loc
= void_param
->get_location();
1972 _mesa_glsl_error(& loc
, state
,
1973 "`void' parameter must be only parameter");
1979 ast_function::hir(exec_list
*instructions
,
1980 struct _mesa_glsl_parse_state
*state
)
1982 void *ctx
= talloc_parent(state
);
1983 ir_function
*f
= NULL
;
1984 ir_function_signature
*sig
= NULL
;
1985 exec_list hir_parameters
;
1988 /* Convert the list of function parameters to HIR now so that they can be
1989 * used below to compare this function's signature with previously seen
1990 * signatures for functions with the same name.
1992 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
1994 & hir_parameters
, state
);
1996 const char *return_type_name
;
1997 const glsl_type
*return_type
=
1998 this->return_type
->specifier
->glsl_type(& return_type_name
, state
);
2000 assert(return_type
!= NULL
);
2002 /* Verify that this function's signature either doesn't match a previously
2003 * seen signature for a function with the same name, or, if a match is found,
2004 * that the previously seen signature does not have an associated definition.
2006 const char *const name
= identifier
;
2007 f
= state
->symbols
->get_function(name
);
2009 ir_function_signature
*sig
= f
->exact_matching_signature(&hir_parameters
);
2011 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
2012 if (badvar
!= NULL
) {
2013 YYLTYPE loc
= this->get_location();
2015 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
2016 "qualifiers don't match prototype", name
, badvar
);
2019 if (sig
->return_type
!= return_type
) {
2020 YYLTYPE loc
= this->get_location();
2022 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
2023 "match prototype", name
);
2026 if (is_definition
&& sig
->is_defined
) {
2027 YYLTYPE loc
= this->get_location();
2029 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
2033 } else if (state
->symbols
->name_declared_this_scope(name
)) {
2034 /* This function name shadows a non-function use of the same name.
2036 YYLTYPE loc
= this->get_location();
2038 _mesa_glsl_error(& loc
, state
, "function name `%s' conflicts with "
2039 "non-function", name
);
2042 f
= new(ctx
) ir_function(name
);
2043 state
->symbols
->add_function(f
->name
, f
);
2045 /* Emit the new function header */
2046 instructions
->push_tail(f
);
2049 /* Verify the return type of main() */
2050 if (strcmp(name
, "main") == 0) {
2051 if (! return_type
->is_void()) {
2052 YYLTYPE loc
= this->get_location();
2054 _mesa_glsl_error(& loc
, state
, "main() must return void");
2057 if (!hir_parameters
.is_empty()) {
2058 YYLTYPE loc
= this->get_location();
2060 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
2064 /* Finish storing the information about this new function in its signature.
2067 sig
= new(ctx
) ir_function_signature(return_type
);
2068 f
->add_signature(sig
);
2071 sig
->replace_parameters(&hir_parameters
);
2074 /* Function declarations (prototypes) do not have r-values.
2081 ast_function_definition::hir(exec_list
*instructions
,
2082 struct _mesa_glsl_parse_state
*state
)
2084 prototype
->is_definition
= true;
2085 prototype
->hir(instructions
, state
);
2087 ir_function_signature
*signature
= prototype
->signature
;
2089 assert(state
->current_function
== NULL
);
2090 state
->current_function
= signature
;
2092 /* Duplicate parameters declared in the prototype as concrete variables.
2093 * Add these to the symbol table.
2095 state
->symbols
->push_scope();
2096 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
2097 ir_variable
*const var
= ((ir_instruction
*) iter
.get())->as_variable();
2099 assert(var
!= NULL
);
2101 /* The only way a parameter would "exist" is if two parameters have
2104 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
2105 YYLTYPE loc
= this->get_location();
2107 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
2109 state
->symbols
->add_variable(var
->name
, var
);
2113 /* Convert the body of the function to HIR. */
2114 this->body
->hir(&signature
->body
, state
);
2115 signature
->is_defined
= true;
2117 state
->symbols
->pop_scope();
2119 assert(state
->current_function
== signature
);
2120 state
->current_function
= NULL
;
2122 /* Function definitions do not have r-values.
2129 ast_jump_statement::hir(exec_list
*instructions
,
2130 struct _mesa_glsl_parse_state
*state
)
2132 void *ctx
= talloc_parent(state
);
2137 assert(state
->current_function
);
2139 if (opt_return_value
) {
2140 if (state
->current_function
->return_type
->base_type
==
2142 YYLTYPE loc
= this->get_location();
2144 _mesa_glsl_error(& loc
, state
,
2145 "`return` with a value, in function `%s' "
2147 state
->current_function
->function_name());
2150 ir_expression
*const ret
= (ir_expression
*)
2151 opt_return_value
->hir(instructions
, state
);
2152 assert(ret
!= NULL
);
2154 /* FINISHME: Make sure the type of the return value matches the return
2155 * FINISHME: type of the enclosing function.
2158 inst
= new(ctx
) ir_return(ret
);
2160 if (state
->current_function
->return_type
->base_type
!=
2162 YYLTYPE loc
= this->get_location();
2164 _mesa_glsl_error(& loc
, state
,
2165 "`return' with no value, in function %s returning "
2167 state
->current_function
->function_name());
2169 inst
= new(ctx
) ir_return
;
2172 instructions
->push_tail(inst
);
2177 /* FINISHME: discard support */
2178 if (state
->target
!= fragment_shader
) {
2179 YYLTYPE loc
= this->get_location();
2181 _mesa_glsl_error(& loc
, state
,
2182 "`discard' may only appear in a fragment shader");
2188 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2189 * FINISHME: and they use a different IR instruction for 'break'.
2191 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2192 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2195 if (state
->loop_or_switch_nesting
== NULL
) {
2196 YYLTYPE loc
= this->get_location();
2198 _mesa_glsl_error(& loc
, state
,
2199 "`%s' may only appear in a loop",
2200 (mode
== ast_break
) ? "break" : "continue");
2202 ir_loop
*const loop
= state
->loop_or_switch_nesting
->as_loop();
2205 ir_loop_jump
*const jump
=
2206 new(ctx
) ir_loop_jump((mode
== ast_break
)
2207 ? ir_loop_jump::jump_break
2208 : ir_loop_jump::jump_continue
);
2209 instructions
->push_tail(jump
);
2216 /* Jump instructions do not have r-values.
2223 ast_selection_statement::hir(exec_list
*instructions
,
2224 struct _mesa_glsl_parse_state
*state
)
2226 void *ctx
= talloc_parent(state
);
2228 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
2230 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2232 * "Any expression whose type evaluates to a Boolean can be used as the
2233 * conditional expression bool-expression. Vector types are not accepted
2234 * as the expression to if."
2236 * The checks are separated so that higher quality diagnostics can be
2237 * generated for cases where both rules are violated.
2239 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
2240 YYLTYPE loc
= this->condition
->get_location();
2242 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
2246 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
2248 if (then_statement
!= NULL
)
2249 then_statement
->hir(& stmt
->then_instructions
, state
);
2251 if (else_statement
!= NULL
)
2252 else_statement
->hir(& stmt
->else_instructions
, state
);
2254 instructions
->push_tail(stmt
);
2256 /* if-statements do not have r-values.
2263 ast_iteration_statement::condition_to_hir(ir_loop
*stmt
,
2264 struct _mesa_glsl_parse_state
*state
)
2266 void *ctx
= talloc_parent(state
);
2268 if (condition
!= NULL
) {
2269 ir_rvalue
*const cond
=
2270 condition
->hir(& stmt
->body_instructions
, state
);
2273 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
2274 YYLTYPE loc
= condition
->get_location();
2276 _mesa_glsl_error(& loc
, state
,
2277 "loop condition must be scalar boolean");
2279 /* As the first code in the loop body, generate a block that looks
2280 * like 'if (!condition) break;' as the loop termination condition.
2282 ir_rvalue
*const not_cond
=
2283 new(ctx
) ir_expression(ir_unop_logic_not
, glsl_type::bool_type
, cond
,
2286 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
2288 ir_jump
*const break_stmt
=
2289 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
2291 if_stmt
->then_instructions
.push_tail(break_stmt
);
2292 stmt
->body_instructions
.push_tail(if_stmt
);
2299 ast_iteration_statement::hir(exec_list
*instructions
,
2300 struct _mesa_glsl_parse_state
*state
)
2302 void *ctx
= talloc_parent(state
);
2304 /* For-loops and while-loops start a new scope, but do-while loops do not.
2306 if (mode
!= ast_do_while
)
2307 state
->symbols
->push_scope();
2309 if (init_statement
!= NULL
)
2310 init_statement
->hir(instructions
, state
);
2312 ir_loop
*const stmt
= new(ctx
) ir_loop();
2313 instructions
->push_tail(stmt
);
2315 /* Track the current loop and / or switch-statement nesting.
2317 ir_instruction
*const nesting
= state
->loop_or_switch_nesting
;
2318 state
->loop_or_switch_nesting
= stmt
;
2320 if (mode
!= ast_do_while
)
2321 condition_to_hir(stmt
, state
);
2324 body
->hir(& stmt
->body_instructions
, state
);
2326 if (rest_expression
!= NULL
)
2327 rest_expression
->hir(& stmt
->body_instructions
, state
);
2329 if (mode
== ast_do_while
)
2330 condition_to_hir(stmt
, state
);
2332 if (mode
!= ast_do_while
)
2333 state
->symbols
->pop_scope();
2335 /* Restore previous nesting before returning.
2337 state
->loop_or_switch_nesting
= nesting
;
2339 /* Loops do not have r-values.
2346 ast_type_specifier::hir(exec_list
*instructions
,
2347 struct _mesa_glsl_parse_state
*state
)
2349 if (this->structure
!= NULL
)
2350 return this->structure
->hir(instructions
, state
);
2357 ast_struct_specifier::hir(exec_list
*instructions
,
2358 struct _mesa_glsl_parse_state
*state
)
2360 void *ctx
= talloc_parent(state
);
2361 unsigned decl_count
= 0;
2363 /* Make an initial pass over the list of structure fields to determine how
2364 * many there are. Each element in this list is an ast_declarator_list.
2365 * This means that we actually need to count the number of elements in the
2366 * 'declarations' list in each of the elements.
2368 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2369 &this->declarations
) {
2370 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
2376 /* Allocate storage for the structure fields and process the field
2377 * declarations. As the declarations are processed, try to also convert
2378 * the types to HIR. This ensures that structure definitions embedded in
2379 * other structure definitions are processed.
2381 glsl_struct_field
*const fields
= (glsl_struct_field
*)
2382 malloc(sizeof(*fields
) * decl_count
);
2385 foreach_list_typed (ast_declarator_list
, decl_list
, link
,
2386 &this->declarations
) {
2387 const char *type_name
;
2389 decl_list
->type
->specifier
->hir(instructions
, state
);
2391 const glsl_type
*decl_type
=
2392 decl_list
->type
->specifier
->glsl_type(& type_name
, state
);
2394 foreach_list_typed (ast_declaration
, decl
, link
,
2395 &decl_list
->declarations
) {
2396 const struct glsl_type
*const field_type
=
2398 ? process_array_type(decl_type
, decl
->array_size
, state
)
2401 fields
[i
].type
= (field_type
!= NULL
)
2402 ? field_type
: glsl_type::error_type
;
2403 fields
[i
].name
= decl
->identifier
;
2408 assert(i
== decl_count
);
2411 if (this->name
== NULL
) {
2412 static unsigned anon_count
= 1;
2415 snprintf(buf
, sizeof(buf
), "#anon_struct_%04x", anon_count
);
2423 glsl_type
*t
= new(ctx
) glsl_type(fields
, decl_count
, name
);
2425 YYLTYPE loc
= this->get_location();
2426 if (!state
->symbols
->add_type(name
, t
)) {
2427 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
2429 /* This logic is a bit tricky. It is an error to declare a structure at
2430 * global scope if there is also a function with the same name.
2432 if ((state
->current_function
== NULL
)
2433 && (state
->symbols
->get_function(name
) != NULL
)) {
2434 _mesa_glsl_error(& loc
, state
, "name `%s' previously defined", name
);
2436 t
->generate_constructor(state
->symbols
);
2439 const glsl_type
**s
= (const glsl_type
**)
2440 realloc(state
->user_structures
,
2441 sizeof(state
->user_structures
[0]) *
2442 (state
->num_user_structures
+ 1));
2444 s
[state
->num_user_structures
] = t
;
2445 state
->user_structures
= s
;
2446 state
->num_user_structures
++;
2450 /* Structure type definitions do not have r-values.