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/core.h" /* for struct gl_extensions */
53 #include "glsl_symbol_table.h"
54 #include "glsl_parser_extras.h"
56 #include "glsl_types.h"
57 #include "program/hash_table.h"
61 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
62 exec_list
*instructions
);
65 _mesa_ast_to_hir(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
)
67 _mesa_glsl_initialize_variables(instructions
, state
);
69 state
->symbols
->separate_function_namespace
= state
->language_version
== 110;
71 state
->current_function
= NULL
;
73 state
->toplevel_ir
= instructions
;
75 state
->gs_input_prim_type_specified
= false;
77 /* Section 4.2 of the GLSL 1.20 specification states:
78 * "The built-in functions are scoped in a scope outside the global scope
79 * users declare global variables in. That is, a shader's global scope,
80 * available for user-defined functions and global variables, is nested
81 * inside the scope containing the built-in functions."
83 * Since built-in functions like ftransform() access built-in variables,
84 * it follows that those must be in the outer scope as well.
86 * We push scope here to create this nesting effect...but don't pop.
87 * This way, a shader's globals are still in the symbol table for use
90 state
->symbols
->push_scope();
92 foreach_list_typed (ast_node
, ast
, link
, & state
->translation_unit
)
93 ast
->hir(instructions
, state
);
95 detect_recursion_unlinked(state
, instructions
);
96 detect_conflicting_assignments(state
, instructions
);
98 state
->toplevel_ir
= NULL
;
100 /* Move all of the variable declarations to the front of the IR list, and
101 * reverse the order. This has the (intended!) side effect that vertex
102 * shader inputs and fragment shader outputs will appear in the IR in the
103 * same order that they appeared in the shader code. This results in the
104 * locations being assigned in the declared order. Many (arguably buggy)
105 * applications depend on this behavior, and it matches what nearly all
108 foreach_list_safe(node
, instructions
) {
109 ir_variable
*const var
= ((ir_instruction
*) node
)->as_variable();
115 instructions
->push_head(var
);
121 * If a conversion is available, convert one operand to a different type
123 * The \c from \c ir_rvalue is converted "in place".
125 * \param to Type that the operand it to be converted to
126 * \param from Operand that is being converted
127 * \param state GLSL compiler state
130 * If a conversion is possible (or unnecessary), \c true is returned.
131 * Otherwise \c false is returned.
134 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
135 struct _mesa_glsl_parse_state
*state
)
138 if (to
->base_type
== from
->type
->base_type
)
141 /* This conversion was added in GLSL 1.20. If the compilation mode is
142 * GLSL 1.10, the conversion is skipped.
144 if (!state
->is_version(120, 0))
147 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
149 * "There are no implicit array or structure conversions. For
150 * example, an array of int cannot be implicitly converted to an
151 * array of float. There are no implicit conversions between
152 * signed and unsigned integers."
154 /* FINISHME: The above comment is partially a lie. There is int/uint
155 * FINISHME: conversion for immediate constants.
157 if (!to
->is_float() || !from
->type
->is_numeric())
160 /* Convert to a floating point type with the same number of components
161 * as the original type - i.e. int to float, not int to vec4.
163 to
= glsl_type::get_instance(GLSL_TYPE_FLOAT
, from
->type
->vector_elements
,
164 from
->type
->matrix_columns
);
166 switch (from
->type
->base_type
) {
168 from
= new(ctx
) ir_expression(ir_unop_i2f
, to
, from
, NULL
);
171 from
= new(ctx
) ir_expression(ir_unop_u2f
, to
, from
, NULL
);
174 from
= new(ctx
) ir_expression(ir_unop_b2f
, to
, from
, NULL
);
184 static const struct glsl_type
*
185 arithmetic_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
187 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
189 const glsl_type
*type_a
= value_a
->type
;
190 const glsl_type
*type_b
= value_b
->type
;
192 /* From GLSL 1.50 spec, page 56:
194 * "The arithmetic binary operators add (+), subtract (-),
195 * multiply (*), and divide (/) operate on integer and
196 * floating-point scalars, vectors, and matrices."
198 if (!type_a
->is_numeric() || !type_b
->is_numeric()) {
199 _mesa_glsl_error(loc
, state
,
200 "operands to arithmetic operators must be numeric");
201 return glsl_type::error_type
;
205 /* "If one operand is floating-point based and the other is
206 * not, then the conversions from Section 4.1.10 "Implicit
207 * Conversions" are applied to the non-floating-point-based operand."
209 if (!apply_implicit_conversion(type_a
, value_b
, state
)
210 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
211 _mesa_glsl_error(loc
, state
,
212 "could not implicitly convert operands to "
213 "arithmetic operator");
214 return glsl_type::error_type
;
216 type_a
= value_a
->type
;
217 type_b
= value_b
->type
;
219 /* "If the operands are integer types, they must both be signed or
222 * From this rule and the preceeding conversion it can be inferred that
223 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
224 * The is_numeric check above already filtered out the case where either
225 * type is not one of these, so now the base types need only be tested for
228 if (type_a
->base_type
!= type_b
->base_type
) {
229 _mesa_glsl_error(loc
, state
,
230 "base type mismatch for arithmetic operator");
231 return glsl_type::error_type
;
234 /* "All arithmetic binary operators result in the same fundamental type
235 * (signed integer, unsigned integer, or floating-point) as the
236 * operands they operate on, after operand type conversion. After
237 * conversion, the following cases are valid
239 * * The two operands are scalars. In this case the operation is
240 * applied, resulting in a scalar."
242 if (type_a
->is_scalar() && type_b
->is_scalar())
245 /* "* One operand is a scalar, and the other is a vector or matrix.
246 * In this case, the scalar operation is applied independently to each
247 * component of the vector or matrix, resulting in the same size
250 if (type_a
->is_scalar()) {
251 if (!type_b
->is_scalar())
253 } else if (type_b
->is_scalar()) {
257 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
258 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
261 assert(!type_a
->is_scalar());
262 assert(!type_b
->is_scalar());
264 /* "* The two operands are vectors of the same size. In this case, the
265 * operation is done component-wise resulting in the same size
268 if (type_a
->is_vector() && type_b
->is_vector()) {
269 if (type_a
== type_b
) {
272 _mesa_glsl_error(loc
, state
,
273 "vector size mismatch for arithmetic operator");
274 return glsl_type::error_type
;
278 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
279 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
280 * <vector, vector> have been handled. At least one of the operands must
281 * be matrix. Further, since there are no integer matrix types, the base
282 * type of both operands must be float.
284 assert(type_a
->is_matrix() || type_b
->is_matrix());
285 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
286 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
288 /* "* The operator is add (+), subtract (-), or divide (/), and the
289 * operands are matrices with the same number of rows and the same
290 * number of columns. In this case, the operation is done component-
291 * wise resulting in the same size matrix."
292 * * The operator is multiply (*), where both operands are matrices or
293 * one operand is a vector and the other a matrix. A right vector
294 * operand is treated as a column vector and a left vector operand as a
295 * row vector. In all these cases, it is required that the number of
296 * columns of the left operand is equal to the number of rows of the
297 * right operand. Then, the multiply (*) operation does a linear
298 * algebraic multiply, yielding an object that has the same number of
299 * rows as the left operand and the same number of columns as the right
300 * operand. Section 5.10 "Vector and Matrix Operations" explains in
301 * more detail how vectors and matrices are operated on."
304 if (type_a
== type_b
)
307 if (type_a
->is_matrix() && type_b
->is_matrix()) {
308 /* Matrix multiply. The columns of A must match the rows of B. Given
309 * the other previously tested constraints, this means the vector type
310 * of a row from A must be the same as the vector type of a column from
313 if (type_a
->row_type() == type_b
->column_type()) {
314 /* The resulting matrix has the number of columns of matrix B and
315 * the number of rows of matrix A. We get the row count of A by
316 * looking at the size of a vector that makes up a column. The
317 * transpose (size of a row) is done for B.
319 const glsl_type
*const type
=
320 glsl_type::get_instance(type_a
->base_type
,
321 type_a
->column_type()->vector_elements
,
322 type_b
->row_type()->vector_elements
);
323 assert(type
!= glsl_type::error_type
);
327 } else if (type_a
->is_matrix()) {
328 /* A is a matrix and B is a column vector. Columns of A must match
329 * rows of B. Given the other previously tested constraints, this
330 * means the vector type of a row from A must be the same as the
331 * vector the type of B.
333 if (type_a
->row_type() == type_b
) {
334 /* The resulting vector has a number of elements equal to
335 * the number of rows of matrix A. */
336 const glsl_type
*const type
=
337 glsl_type::get_instance(type_a
->base_type
,
338 type_a
->column_type()->vector_elements
,
340 assert(type
!= glsl_type::error_type
);
345 assert(type_b
->is_matrix());
347 /* A is a row vector and B is a matrix. Columns of A must match rows
348 * of B. Given the other previously tested constraints, this means
349 * the type of A must be the same as the vector type of a column from
352 if (type_a
== type_b
->column_type()) {
353 /* The resulting vector has a number of elements equal to
354 * the number of columns of matrix B. */
355 const glsl_type
*const type
=
356 glsl_type::get_instance(type_a
->base_type
,
357 type_b
->row_type()->vector_elements
,
359 assert(type
!= glsl_type::error_type
);
365 _mesa_glsl_error(loc
, state
, "size mismatch for matrix multiplication");
366 return glsl_type::error_type
;
370 /* "All other cases are illegal."
372 _mesa_glsl_error(loc
, state
, "type mismatch");
373 return glsl_type::error_type
;
377 static const struct glsl_type
*
378 unary_arithmetic_result_type(const struct glsl_type
*type
,
379 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
381 /* From GLSL 1.50 spec, page 57:
383 * "The arithmetic unary operators negate (-), post- and pre-increment
384 * and decrement (-- and ++) operate on integer or floating-point
385 * values (including vectors and matrices). All unary operators work
386 * component-wise on their operands. These result with the same type
389 if (!type
->is_numeric()) {
390 _mesa_glsl_error(loc
, state
,
391 "operands to arithmetic operators must be numeric");
392 return glsl_type::error_type
;
399 * \brief Return the result type of a bit-logic operation.
401 * If the given types to the bit-logic operator are invalid, return
402 * glsl_type::error_type.
404 * \param type_a Type of LHS of bit-logic op
405 * \param type_b Type of RHS of bit-logic op
407 static const struct glsl_type
*
408 bit_logic_result_type(const struct glsl_type
*type_a
,
409 const struct glsl_type
*type_b
,
411 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
413 if (!state
->check_bitwise_operations_allowed(loc
)) {
414 return glsl_type::error_type
;
417 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
419 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
420 * (|). The operands must be of type signed or unsigned integers or
423 if (!type_a
->is_integer()) {
424 _mesa_glsl_error(loc
, state
, "LHS of `%s' must be an integer",
425 ast_expression::operator_string(op
));
426 return glsl_type::error_type
;
428 if (!type_b
->is_integer()) {
429 _mesa_glsl_error(loc
, state
, "RHS of `%s' must be an integer",
430 ast_expression::operator_string(op
));
431 return glsl_type::error_type
;
434 /* "The fundamental types of the operands (signed or unsigned) must
437 if (type_a
->base_type
!= type_b
->base_type
) {
438 _mesa_glsl_error(loc
, state
, "operands of `%s' must have the same "
439 "base type", ast_expression::operator_string(op
));
440 return glsl_type::error_type
;
443 /* "The operands cannot be vectors of differing size." */
444 if (type_a
->is_vector() &&
445 type_b
->is_vector() &&
446 type_a
->vector_elements
!= type_b
->vector_elements
) {
447 _mesa_glsl_error(loc
, state
, "operands of `%s' cannot be vectors of "
448 "different sizes", ast_expression::operator_string(op
));
449 return glsl_type::error_type
;
452 /* "If one operand is a scalar and the other a vector, the scalar is
453 * applied component-wise to the vector, resulting in the same type as
454 * the vector. The fundamental types of the operands [...] will be the
455 * resulting fundamental type."
457 if (type_a
->is_scalar())
463 static const struct glsl_type
*
464 modulus_result_type(const struct glsl_type
*type_a
,
465 const struct glsl_type
*type_b
,
466 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
468 if (!state
->check_version(130, 300, loc
, "operator '%%' is reserved")) {
469 return glsl_type::error_type
;
472 /* From GLSL 1.50 spec, page 56:
473 * "The operator modulus (%) operates on signed or unsigned integers or
474 * integer vectors. The operand types must both be signed or both be
477 if (!type_a
->is_integer()) {
478 _mesa_glsl_error(loc
, state
, "LHS of operator %% must be an integer");
479 return glsl_type::error_type
;
481 if (!type_b
->is_integer()) {
482 _mesa_glsl_error(loc
, state
, "RHS of operator %% must be an integer");
483 return glsl_type::error_type
;
485 if (type_a
->base_type
!= type_b
->base_type
) {
486 _mesa_glsl_error(loc
, state
,
487 "operands of %% must have the same base type");
488 return glsl_type::error_type
;
491 /* "The operands cannot be vectors of differing size. If one operand is
492 * a scalar and the other vector, then the scalar is applied component-
493 * wise to the vector, resulting in the same type as the vector. If both
494 * are vectors of the same size, the result is computed component-wise."
496 if (type_a
->is_vector()) {
497 if (!type_b
->is_vector()
498 || (type_a
->vector_elements
== type_b
->vector_elements
))
503 /* "The operator modulus (%) is not defined for any other data types
504 * (non-integer types)."
506 _mesa_glsl_error(loc
, state
, "type mismatch");
507 return glsl_type::error_type
;
511 static const struct glsl_type
*
512 relational_result_type(ir_rvalue
* &value_a
, ir_rvalue
* &value_b
,
513 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
515 const glsl_type
*type_a
= value_a
->type
;
516 const glsl_type
*type_b
= value_b
->type
;
518 /* From GLSL 1.50 spec, page 56:
519 * "The relational operators greater than (>), less than (<), greater
520 * than or equal (>=), and less than or equal (<=) operate only on
521 * scalar integer and scalar floating-point expressions."
523 if (!type_a
->is_numeric()
524 || !type_b
->is_numeric()
525 || !type_a
->is_scalar()
526 || !type_b
->is_scalar()) {
527 _mesa_glsl_error(loc
, state
,
528 "operands to relational operators must be scalar and "
530 return glsl_type::error_type
;
533 /* "Either the operands' types must match, or the conversions from
534 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
535 * operand, after which the types must match."
537 if (!apply_implicit_conversion(type_a
, value_b
, state
)
538 && !apply_implicit_conversion(type_b
, value_a
, state
)) {
539 _mesa_glsl_error(loc
, state
,
540 "could not implicitly convert operands to "
541 "relational operator");
542 return glsl_type::error_type
;
544 type_a
= value_a
->type
;
545 type_b
= value_b
->type
;
547 if (type_a
->base_type
!= type_b
->base_type
) {
548 _mesa_glsl_error(loc
, state
, "base type mismatch");
549 return glsl_type::error_type
;
552 /* "The result is scalar Boolean."
554 return glsl_type::bool_type
;
558 * \brief Return the result type of a bit-shift operation.
560 * If the given types to the bit-shift operator are invalid, return
561 * glsl_type::error_type.
563 * \param type_a Type of LHS of bit-shift op
564 * \param type_b Type of RHS of bit-shift op
566 static const struct glsl_type
*
567 shift_result_type(const struct glsl_type
*type_a
,
568 const struct glsl_type
*type_b
,
570 struct _mesa_glsl_parse_state
*state
, YYLTYPE
*loc
)
572 if (!state
->check_bitwise_operations_allowed(loc
)) {
573 return glsl_type::error_type
;
576 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
578 * "The shift operators (<<) and (>>). For both operators, the operands
579 * must be signed or unsigned integers or integer vectors. One operand
580 * can be signed while the other is unsigned."
582 if (!type_a
->is_integer()) {
583 _mesa_glsl_error(loc
, state
, "LHS of operator %s must be an integer or "
584 "integer vector", ast_expression::operator_string(op
));
585 return glsl_type::error_type
;
588 if (!type_b
->is_integer()) {
589 _mesa_glsl_error(loc
, state
, "RHS of operator %s must be an integer or "
590 "integer vector", ast_expression::operator_string(op
));
591 return glsl_type::error_type
;
594 /* "If the first operand is a scalar, the second operand has to be
597 if (type_a
->is_scalar() && !type_b
->is_scalar()) {
598 _mesa_glsl_error(loc
, state
, "if the first operand of %s is scalar, the "
599 "second must be scalar as well",
600 ast_expression::operator_string(op
));
601 return glsl_type::error_type
;
604 /* If both operands are vectors, check that they have same number of
607 if (type_a
->is_vector() &&
608 type_b
->is_vector() &&
609 type_a
->vector_elements
!= type_b
->vector_elements
) {
610 _mesa_glsl_error(loc
, state
, "vector operands to operator %s must "
611 "have same number of elements",
612 ast_expression::operator_string(op
));
613 return glsl_type::error_type
;
616 /* "In all cases, the resulting type will be the same type as the left
623 * Validates that a value can be assigned to a location with a specified type
625 * Validates that \c rhs can be assigned to some location. If the types are
626 * not an exact match but an automatic conversion is possible, \c rhs will be
630 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
631 * Otherwise the actual RHS to be assigned will be returned. This may be
632 * \c rhs, or it may be \c rhs after some type conversion.
635 * In addition to being used for assignments, this function is used to
636 * type-check return values.
639 validate_assignment(struct _mesa_glsl_parse_state
*state
,
640 const glsl_type
*lhs_type
, ir_rvalue
*rhs
,
643 /* If there is already some error in the RHS, just return it. Anything
644 * else will lead to an avalanche of error message back to the user.
646 if (rhs
->type
->is_error())
649 /* If the types are identical, the assignment can trivially proceed.
651 if (rhs
->type
== lhs_type
)
654 /* If the array element types are the same and the size of the LHS is zero,
655 * the assignment is okay for initializers embedded in variable
658 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
659 * is handled by ir_dereference::is_lvalue.
661 if (is_initializer
&& lhs_type
->is_array() && rhs
->type
->is_array()
662 && (lhs_type
->element_type() == rhs
->type
->element_type())
663 && (lhs_type
->array_size() == 0)) {
667 /* Check for implicit conversion in GLSL 1.20 */
668 if (apply_implicit_conversion(lhs_type
, rhs
, state
)) {
669 if (rhs
->type
== lhs_type
)
677 mark_whole_array_access(ir_rvalue
*access
)
679 ir_dereference_variable
*deref
= access
->as_dereference_variable();
681 if (deref
&& deref
->var
) {
682 deref
->var
->max_array_access
= deref
->type
->length
- 1;
687 do_assignment(exec_list
*instructions
, struct _mesa_glsl_parse_state
*state
,
688 const char *non_lvalue_description
,
689 ir_rvalue
*lhs
, ir_rvalue
*rhs
, bool is_initializer
,
693 bool error_emitted
= (lhs
->type
->is_error() || rhs
->type
->is_error());
695 /* If the assignment LHS comes back as an ir_binop_vector_extract
696 * expression, move it to the RHS as an ir_triop_vector_insert.
698 if (lhs
->ir_type
== ir_type_expression
) {
699 ir_expression
*const expr
= lhs
->as_expression();
701 if (unlikely(expr
->operation
== ir_binop_vector_extract
)) {
703 validate_assignment(state
, lhs
->type
, rhs
, is_initializer
);
705 if (new_rhs
== NULL
) {
706 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
709 rhs
= new(ctx
) ir_expression(ir_triop_vector_insert
,
710 expr
->operands
[0]->type
,
714 lhs
= expr
->operands
[0]->clone(ctx
, NULL
);
719 ir_variable
*lhs_var
= lhs
->variable_referenced();
721 lhs_var
->assigned
= true;
723 if (!error_emitted
) {
724 if (non_lvalue_description
!= NULL
) {
725 _mesa_glsl_error(&lhs_loc
, state
,
727 non_lvalue_description
);
728 error_emitted
= true;
729 } else if (lhs
->variable_referenced() != NULL
730 && lhs
->variable_referenced()->read_only
) {
731 _mesa_glsl_error(&lhs_loc
, state
,
732 "assignment to read-only variable '%s'",
733 lhs
->variable_referenced()->name
);
734 error_emitted
= true;
736 } else if (lhs
->type
->is_array() &&
737 !state
->check_version(120, 300, &lhs_loc
,
738 "whole array assignment forbidden")) {
739 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
741 * "Other binary or unary expressions, non-dereferenced
742 * arrays, function names, swizzles with repeated fields,
743 * and constants cannot be l-values."
745 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
747 error_emitted
= true;
748 } else if (!lhs
->is_lvalue()) {
749 _mesa_glsl_error(& lhs_loc
, state
, "non-lvalue in assignment");
750 error_emitted
= true;
755 validate_assignment(state
, lhs
->type
, rhs
, is_initializer
);
756 if (new_rhs
== NULL
) {
757 _mesa_glsl_error(& lhs_loc
, state
, "type mismatch");
761 /* If the LHS array was not declared with a size, it takes it size from
762 * the RHS. If the LHS is an l-value and a whole array, it must be a
763 * dereference of a variable. Any other case would require that the LHS
764 * is either not an l-value or not a whole array.
766 if (lhs
->type
->array_size() == 0) {
767 ir_dereference
*const d
= lhs
->as_dereference();
771 ir_variable
*const var
= d
->variable_referenced();
775 if (var
->max_array_access
>= unsigned(rhs
->type
->array_size())) {
776 /* FINISHME: This should actually log the location of the RHS. */
777 _mesa_glsl_error(& lhs_loc
, state
, "array size must be > %u due to "
779 var
->max_array_access
);
782 var
->type
= glsl_type::get_array_instance(lhs
->type
->element_type(),
783 rhs
->type
->array_size());
786 mark_whole_array_access(rhs
);
787 mark_whole_array_access(lhs
);
790 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
791 * but not post_inc) need the converted assigned value as an rvalue
792 * to handle things like:
796 * So we always just store the computed value being assigned to a
797 * temporary and return a deref of that temporary. If the rvalue
798 * ends up not being used, the temp will get copy-propagated out.
800 ir_variable
*var
= new(ctx
) ir_variable(rhs
->type
, "assignment_tmp",
802 ir_dereference_variable
*deref_var
= new(ctx
) ir_dereference_variable(var
);
803 instructions
->push_tail(var
);
804 instructions
->push_tail(new(ctx
) ir_assignment(deref_var
, rhs
));
805 deref_var
= new(ctx
) ir_dereference_variable(var
);
808 instructions
->push_tail(new(ctx
) ir_assignment(lhs
, deref_var
));
810 return new(ctx
) ir_dereference_variable(var
);
814 get_lvalue_copy(exec_list
*instructions
, ir_rvalue
*lvalue
)
816 void *ctx
= ralloc_parent(lvalue
);
819 var
= new(ctx
) ir_variable(lvalue
->type
, "_post_incdec_tmp",
821 instructions
->push_tail(var
);
822 var
->mode
= ir_var_auto
;
824 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
827 return new(ctx
) ir_dereference_variable(var
);
832 ast_node::hir(exec_list
*instructions
,
833 struct _mesa_glsl_parse_state
*state
)
842 do_comparison(void *mem_ctx
, int operation
, ir_rvalue
*op0
, ir_rvalue
*op1
)
845 ir_rvalue
*cmp
= NULL
;
847 if (operation
== ir_binop_all_equal
)
848 join_op
= ir_binop_logic_and
;
850 join_op
= ir_binop_logic_or
;
852 switch (op0
->type
->base_type
) {
853 case GLSL_TYPE_FLOAT
:
857 return new(mem_ctx
) ir_expression(operation
, op0
, op1
);
859 case GLSL_TYPE_ARRAY
: {
860 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
861 ir_rvalue
*e0
, *e1
, *result
;
863 e0
= new(mem_ctx
) ir_dereference_array(op0
->clone(mem_ctx
, NULL
),
864 new(mem_ctx
) ir_constant(i
));
865 e1
= new(mem_ctx
) ir_dereference_array(op1
->clone(mem_ctx
, NULL
),
866 new(mem_ctx
) ir_constant(i
));
867 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
870 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
876 mark_whole_array_access(op0
);
877 mark_whole_array_access(op1
);
881 case GLSL_TYPE_STRUCT
: {
882 for (unsigned int i
= 0; i
< op0
->type
->length
; i
++) {
883 ir_rvalue
*e0
, *e1
, *result
;
884 const char *field_name
= op0
->type
->fields
.structure
[i
].name
;
886 e0
= new(mem_ctx
) ir_dereference_record(op0
->clone(mem_ctx
, NULL
),
888 e1
= new(mem_ctx
) ir_dereference_record(op1
->clone(mem_ctx
, NULL
),
890 result
= do_comparison(mem_ctx
, operation
, e0
, e1
);
893 cmp
= new(mem_ctx
) ir_expression(join_op
, cmp
, result
);
901 case GLSL_TYPE_ERROR
:
903 case GLSL_TYPE_SAMPLER
:
904 case GLSL_TYPE_INTERFACE
:
905 /* I assume a comparison of a struct containing a sampler just
906 * ignores the sampler present in the type.
912 cmp
= new(mem_ctx
) ir_constant(true);
917 /* For logical operations, we want to ensure that the operands are
918 * scalar booleans. If it isn't, emit an error and return a constant
919 * boolean to avoid triggering cascading error messages.
922 get_scalar_boolean_operand(exec_list
*instructions
,
923 struct _mesa_glsl_parse_state
*state
,
924 ast_expression
*parent_expr
,
926 const char *operand_name
,
929 ast_expression
*expr
= parent_expr
->subexpressions
[operand
];
931 ir_rvalue
*val
= expr
->hir(instructions
, state
);
933 if (val
->type
->is_boolean() && val
->type
->is_scalar())
936 if (!*error_emitted
) {
937 YYLTYPE loc
= expr
->get_location();
938 _mesa_glsl_error(&loc
, state
, "%s of `%s' must be scalar boolean",
940 parent_expr
->operator_string(parent_expr
->oper
));
941 *error_emitted
= true;
944 return new(ctx
) ir_constant(true);
948 * If name refers to a builtin array whose maximum allowed size is less than
949 * size, report an error and return true. Otherwise return false.
952 check_builtin_array_max_size(const char *name
, unsigned size
,
953 YYLTYPE loc
, struct _mesa_glsl_parse_state
*state
)
955 if ((strcmp("gl_TexCoord", name
) == 0)
956 && (size
> state
->Const
.MaxTextureCoords
)) {
957 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
959 * "The size [of gl_TexCoord] can be at most
960 * gl_MaxTextureCoords."
962 _mesa_glsl_error(&loc
, state
, "`gl_TexCoord' array size cannot "
963 "be larger than gl_MaxTextureCoords (%u)",
964 state
->Const
.MaxTextureCoords
);
965 } else if (strcmp("gl_ClipDistance", name
) == 0
966 && size
> state
->Const
.MaxClipPlanes
) {
967 /* From section 7.1 (Vertex Shader Special Variables) of the
970 * "The gl_ClipDistance array is predeclared as unsized and
971 * must be sized by the shader either redeclaring it with a
972 * size or indexing it only with integral constant
973 * expressions. ... The size can be at most
974 * gl_MaxClipDistances."
976 _mesa_glsl_error(&loc
, state
, "`gl_ClipDistance' array size cannot "
977 "be larger than gl_MaxClipDistances (%u)",
978 state
->Const
.MaxClipPlanes
);
983 * Create the constant 1, of a which is appropriate for incrementing and
984 * decrementing values of the given GLSL type. For example, if type is vec4,
985 * this creates a constant value of 1.0 having type float.
987 * If the given type is invalid for increment and decrement operators, return
988 * a floating point 1--the error will be detected later.
991 constant_one_for_inc_dec(void *ctx
, const glsl_type
*type
)
993 switch (type
->base_type
) {
995 return new(ctx
) ir_constant((unsigned) 1);
997 return new(ctx
) ir_constant(1);
999 case GLSL_TYPE_FLOAT
:
1000 return new(ctx
) ir_constant(1.0f
);
1005 ast_expression::hir(exec_list
*instructions
,
1006 struct _mesa_glsl_parse_state
*state
)
1009 static const int operations
[AST_NUM_OPERATORS
] = {
1010 -1, /* ast_assign doesn't convert to ir_expression. */
1011 -1, /* ast_plus doesn't convert to ir_expression. */
1025 ir_binop_any_nequal
,
1035 /* Note: The following block of expression types actually convert
1036 * to multiple IR instructions.
1038 ir_binop_mul
, /* ast_mul_assign */
1039 ir_binop_div
, /* ast_div_assign */
1040 ir_binop_mod
, /* ast_mod_assign */
1041 ir_binop_add
, /* ast_add_assign */
1042 ir_binop_sub
, /* ast_sub_assign */
1043 ir_binop_lshift
, /* ast_ls_assign */
1044 ir_binop_rshift
, /* ast_rs_assign */
1045 ir_binop_bit_and
, /* ast_and_assign */
1046 ir_binop_bit_xor
, /* ast_xor_assign */
1047 ir_binop_bit_or
, /* ast_or_assign */
1049 -1, /* ast_conditional doesn't convert to ir_expression. */
1050 ir_binop_add
, /* ast_pre_inc. */
1051 ir_binop_sub
, /* ast_pre_dec. */
1052 ir_binop_add
, /* ast_post_inc. */
1053 ir_binop_sub
, /* ast_post_dec. */
1054 -1, /* ast_field_selection doesn't conv to ir_expression. */
1055 -1, /* ast_array_index doesn't convert to ir_expression. */
1056 -1, /* ast_function_call doesn't conv to ir_expression. */
1057 -1, /* ast_identifier doesn't convert to ir_expression. */
1058 -1, /* ast_int_constant doesn't convert to ir_expression. */
1059 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1060 -1, /* ast_float_constant doesn't conv to ir_expression. */
1061 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1062 -1, /* ast_sequence doesn't convert to ir_expression. */
1064 ir_rvalue
*result
= NULL
;
1066 const struct glsl_type
*type
; /* a temporary variable for switch cases */
1067 bool error_emitted
= false;
1070 loc
= this->get_location();
1072 switch (this->oper
) {
1074 assert(!"ast_aggregate: Should never get here.");
1078 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1079 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1081 result
= do_assignment(instructions
, state
,
1082 this->subexpressions
[0]->non_lvalue_description
,
1083 op
[0], op
[1], false,
1084 this->subexpressions
[0]->get_location());
1085 error_emitted
= result
->type
->is_error();
1090 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1092 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1094 error_emitted
= type
->is_error();
1100 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1102 type
= unary_arithmetic_result_type(op
[0]->type
, state
, & loc
);
1104 error_emitted
= type
->is_error();
1106 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1114 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1115 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1117 type
= arithmetic_result_type(op
[0], op
[1],
1118 (this->oper
== ast_mul
),
1120 error_emitted
= type
->is_error();
1122 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1127 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1128 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1130 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1132 assert(operations
[this->oper
] == ir_binop_mod
);
1134 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1136 error_emitted
= type
->is_error();
1141 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1142 error_emitted
= true;
1145 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1146 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1147 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1149 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1151 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1158 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1159 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1161 type
= relational_result_type(op
[0], op
[1], state
, & loc
);
1163 /* The relational operators must either generate an error or result
1164 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1166 assert(type
->is_error()
1167 || ((type
->base_type
== GLSL_TYPE_BOOL
)
1168 && type
->is_scalar()));
1170 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1172 error_emitted
= type
->is_error();
1177 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1178 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1180 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1182 * "The equality operators equal (==), and not equal (!=)
1183 * operate on all types. They result in a scalar Boolean. If
1184 * the operand types do not match, then there must be a
1185 * conversion from Section 4.1.10 "Implicit Conversions"
1186 * applied to one operand that can make them match, in which
1187 * case this conversion is done."
1189 if ((!apply_implicit_conversion(op
[0]->type
, op
[1], state
)
1190 && !apply_implicit_conversion(op
[1]->type
, op
[0], state
))
1191 || (op
[0]->type
!= op
[1]->type
)) {
1192 _mesa_glsl_error(& loc
, state
, "operands of `%s' must have the same "
1193 "type", (this->oper
== ast_equal
) ? "==" : "!=");
1194 error_emitted
= true;
1195 } else if ((op
[0]->type
->is_array() || op
[1]->type
->is_array()) &&
1196 !state
->check_version(120, 300, &loc
,
1197 "array comparisons forbidden")) {
1198 error_emitted
= true;
1201 if (error_emitted
) {
1202 result
= new(ctx
) ir_constant(false);
1204 result
= do_comparison(ctx
, operations
[this->oper
], op
[0], op
[1]);
1205 assert(result
->type
== glsl_type::bool_type
);
1212 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1213 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1214 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1216 result
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1218 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1222 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1224 if (!state
->check_bitwise_operations_allowed(&loc
)) {
1225 error_emitted
= true;
1228 if (!op
[0]->type
->is_integer()) {
1229 _mesa_glsl_error(&loc
, state
, "operand of `~' must be an integer");
1230 error_emitted
= true;
1233 type
= error_emitted
? glsl_type::error_type
: op
[0]->type
;
1234 result
= new(ctx
) ir_expression(ir_unop_bit_not
, type
, op
[0], NULL
);
1237 case ast_logic_and
: {
1238 exec_list rhs_instructions
;
1239 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1240 "LHS", &error_emitted
);
1241 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1242 "RHS", &error_emitted
);
1244 if (rhs_instructions
.is_empty()) {
1245 result
= new(ctx
) ir_expression(ir_binop_logic_and
, op
[0], op
[1]);
1246 type
= result
->type
;
1248 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1251 instructions
->push_tail(tmp
);
1253 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1254 instructions
->push_tail(stmt
);
1256 stmt
->then_instructions
.append_list(&rhs_instructions
);
1257 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1258 ir_assignment
*const then_assign
=
1259 new(ctx
) ir_assignment(then_deref
, op
[1]);
1260 stmt
->then_instructions
.push_tail(then_assign
);
1262 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1263 ir_assignment
*const else_assign
=
1264 new(ctx
) ir_assignment(else_deref
, new(ctx
) ir_constant(false));
1265 stmt
->else_instructions
.push_tail(else_assign
);
1267 result
= new(ctx
) ir_dereference_variable(tmp
);
1273 case ast_logic_or
: {
1274 exec_list rhs_instructions
;
1275 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1276 "LHS", &error_emitted
);
1277 op
[1] = get_scalar_boolean_operand(&rhs_instructions
, state
, this, 1,
1278 "RHS", &error_emitted
);
1280 if (rhs_instructions
.is_empty()) {
1281 result
= new(ctx
) ir_expression(ir_binop_logic_or
, op
[0], op
[1]);
1282 type
= result
->type
;
1284 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::bool_type
,
1287 instructions
->push_tail(tmp
);
1289 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1290 instructions
->push_tail(stmt
);
1292 ir_dereference
*const then_deref
= new(ctx
) ir_dereference_variable(tmp
);
1293 ir_assignment
*const then_assign
=
1294 new(ctx
) ir_assignment(then_deref
, new(ctx
) ir_constant(true));
1295 stmt
->then_instructions
.push_tail(then_assign
);
1297 stmt
->else_instructions
.append_list(&rhs_instructions
);
1298 ir_dereference
*const else_deref
= new(ctx
) ir_dereference_variable(tmp
);
1299 ir_assignment
*const else_assign
=
1300 new(ctx
) ir_assignment(else_deref
, op
[1]);
1301 stmt
->else_instructions
.push_tail(else_assign
);
1303 result
= new(ctx
) ir_dereference_variable(tmp
);
1310 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1312 * "The logical binary operators and (&&), or ( | | ), and
1313 * exclusive or (^^). They operate only on two Boolean
1314 * expressions and result in a Boolean expression."
1316 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0, "LHS",
1318 op
[1] = get_scalar_boolean_operand(instructions
, state
, this, 1, "RHS",
1321 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1326 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1327 "operand", &error_emitted
);
1329 result
= new(ctx
) ir_expression(operations
[this->oper
], glsl_type::bool_type
,
1333 case ast_mul_assign
:
1334 case ast_div_assign
:
1335 case ast_add_assign
:
1336 case ast_sub_assign
: {
1337 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1338 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1340 type
= arithmetic_result_type(op
[0], op
[1],
1341 (this->oper
== ast_mul_assign
),
1344 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1347 result
= do_assignment(instructions
, state
,
1348 this->subexpressions
[0]->non_lvalue_description
,
1349 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1350 this->subexpressions
[0]->get_location());
1351 error_emitted
= (op
[0]->type
->is_error());
1353 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1354 * explicitly test for this because none of the binary expression
1355 * operators allow array operands either.
1361 case ast_mod_assign
: {
1362 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1363 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1365 type
= modulus_result_type(op
[0]->type
, op
[1]->type
, state
, & loc
);
1367 assert(operations
[this->oper
] == ir_binop_mod
);
1369 ir_rvalue
*temp_rhs
;
1370 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1373 result
= do_assignment(instructions
, state
,
1374 this->subexpressions
[0]->non_lvalue_description
,
1375 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1376 this->subexpressions
[0]->get_location());
1377 error_emitted
= type
->is_error();
1382 case ast_rs_assign
: {
1383 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1384 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1385 type
= shift_result_type(op
[0]->type
, op
[1]->type
, this->oper
, state
,
1387 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1388 type
, op
[0], op
[1]);
1389 result
= do_assignment(instructions
, state
,
1390 this->subexpressions
[0]->non_lvalue_description
,
1391 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1392 this->subexpressions
[0]->get_location());
1393 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1397 case ast_and_assign
:
1398 case ast_xor_assign
:
1399 case ast_or_assign
: {
1400 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1401 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
1402 type
= bit_logic_result_type(op
[0]->type
, op
[1]->type
, this->oper
,
1404 ir_rvalue
*temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
],
1405 type
, op
[0], op
[1]);
1406 result
= do_assignment(instructions
, state
,
1407 this->subexpressions
[0]->non_lvalue_description
,
1408 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1409 this->subexpressions
[0]->get_location());
1410 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1414 case ast_conditional
: {
1415 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1417 * "The ternary selection operator (?:). It operates on three
1418 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1419 * first expression, which must result in a scalar Boolean."
1421 op
[0] = get_scalar_boolean_operand(instructions
, state
, this, 0,
1422 "condition", &error_emitted
);
1424 /* The :? operator is implemented by generating an anonymous temporary
1425 * followed by an if-statement. The last instruction in each branch of
1426 * the if-statement assigns a value to the anonymous temporary. This
1427 * temporary is the r-value of the expression.
1429 exec_list then_instructions
;
1430 exec_list else_instructions
;
1432 op
[1] = this->subexpressions
[1]->hir(&then_instructions
, state
);
1433 op
[2] = this->subexpressions
[2]->hir(&else_instructions
, state
);
1435 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1437 * "The second and third expressions can be any type, as
1438 * long their types match, or there is a conversion in
1439 * Section 4.1.10 "Implicit Conversions" that can be applied
1440 * to one of the expressions to make their types match. This
1441 * resulting matching type is the type of the entire
1444 if ((!apply_implicit_conversion(op
[1]->type
, op
[2], state
)
1445 && !apply_implicit_conversion(op
[2]->type
, op
[1], state
))
1446 || (op
[1]->type
!= op
[2]->type
)) {
1447 YYLTYPE loc
= this->subexpressions
[1]->get_location();
1449 _mesa_glsl_error(& loc
, state
, "second and third operands of ?: "
1450 "operator must have matching types");
1451 error_emitted
= true;
1452 type
= glsl_type::error_type
;
1457 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1459 * "The second and third expressions must be the same type, but can
1460 * be of any type other than an array."
1462 if (type
->is_array() &&
1463 !state
->check_version(120, 300, &loc
,
1464 "second and third operands of ?: operator "
1465 "cannot be arrays")) {
1466 error_emitted
= true;
1469 ir_constant
*cond_val
= op
[0]->constant_expression_value();
1470 ir_constant
*then_val
= op
[1]->constant_expression_value();
1471 ir_constant
*else_val
= op
[2]->constant_expression_value();
1473 if (then_instructions
.is_empty()
1474 && else_instructions
.is_empty()
1475 && (cond_val
!= NULL
) && (then_val
!= NULL
) && (else_val
!= NULL
)) {
1476 result
= (cond_val
->value
.b
[0]) ? then_val
: else_val
;
1478 ir_variable
*const tmp
=
1479 new(ctx
) ir_variable(type
, "conditional_tmp", ir_var_temporary
);
1480 instructions
->push_tail(tmp
);
1482 ir_if
*const stmt
= new(ctx
) ir_if(op
[0]);
1483 instructions
->push_tail(stmt
);
1485 then_instructions
.move_nodes_to(& stmt
->then_instructions
);
1486 ir_dereference
*const then_deref
=
1487 new(ctx
) ir_dereference_variable(tmp
);
1488 ir_assignment
*const then_assign
=
1489 new(ctx
) ir_assignment(then_deref
, op
[1]);
1490 stmt
->then_instructions
.push_tail(then_assign
);
1492 else_instructions
.move_nodes_to(& stmt
->else_instructions
);
1493 ir_dereference
*const else_deref
=
1494 new(ctx
) ir_dereference_variable(tmp
);
1495 ir_assignment
*const else_assign
=
1496 new(ctx
) ir_assignment(else_deref
, op
[2]);
1497 stmt
->else_instructions
.push_tail(else_assign
);
1499 result
= new(ctx
) ir_dereference_variable(tmp
);
1506 this->non_lvalue_description
= (this->oper
== ast_pre_inc
)
1507 ? "pre-increment operation" : "pre-decrement operation";
1509 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1510 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1512 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1514 ir_rvalue
*temp_rhs
;
1515 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1518 result
= do_assignment(instructions
, state
,
1519 this->subexpressions
[0]->non_lvalue_description
,
1520 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1521 this->subexpressions
[0]->get_location());
1522 error_emitted
= op
[0]->type
->is_error();
1527 case ast_post_dec
: {
1528 this->non_lvalue_description
= (this->oper
== ast_post_inc
)
1529 ? "post-increment operation" : "post-decrement operation";
1530 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
1531 op
[1] = constant_one_for_inc_dec(ctx
, op
[0]->type
);
1533 error_emitted
= op
[0]->type
->is_error() || op
[1]->type
->is_error();
1535 type
= arithmetic_result_type(op
[0], op
[1], false, state
, & loc
);
1537 ir_rvalue
*temp_rhs
;
1538 temp_rhs
= new(ctx
) ir_expression(operations
[this->oper
], type
,
1541 /* Get a temporary of a copy of the lvalue before it's modified.
1542 * This may get thrown away later.
1544 result
= get_lvalue_copy(instructions
, op
[0]->clone(ctx
, NULL
));
1546 (void)do_assignment(instructions
, state
,
1547 this->subexpressions
[0]->non_lvalue_description
,
1548 op
[0]->clone(ctx
, NULL
), temp_rhs
, false,
1549 this->subexpressions
[0]->get_location());
1551 error_emitted
= op
[0]->type
->is_error();
1555 case ast_field_selection
:
1556 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
1559 case ast_array_index
: {
1560 YYLTYPE index_loc
= subexpressions
[1]->get_location();
1562 op
[0] = subexpressions
[0]->hir(instructions
, state
);
1563 op
[1] = subexpressions
[1]->hir(instructions
, state
);
1565 result
= _mesa_ast_array_index_to_hir(ctx
, state
, op
[0], op
[1],
1568 if (result
->type
->is_error())
1569 error_emitted
= true;
1574 case ast_function_call
:
1575 /* Should *NEVER* get here. ast_function_call should always be handled
1576 * by ast_function_expression::hir.
1581 case ast_identifier
: {
1582 /* ast_identifier can appear several places in a full abstract syntax
1583 * tree. This particular use must be at location specified in the grammar
1584 * as 'variable_identifier'.
1587 state
->symbols
->get_variable(this->primary_expression
.identifier
);
1591 result
= new(ctx
) ir_dereference_variable(var
);
1593 _mesa_glsl_error(& loc
, state
, "`%s' undeclared",
1594 this->primary_expression
.identifier
);
1596 result
= ir_rvalue::error_value(ctx
);
1597 error_emitted
= true;
1602 case ast_int_constant
:
1603 result
= new(ctx
) ir_constant(this->primary_expression
.int_constant
);
1606 case ast_uint_constant
:
1607 result
= new(ctx
) ir_constant(this->primary_expression
.uint_constant
);
1610 case ast_float_constant
:
1611 result
= new(ctx
) ir_constant(this->primary_expression
.float_constant
);
1614 case ast_bool_constant
:
1615 result
= new(ctx
) ir_constant(bool(this->primary_expression
.bool_constant
));
1618 case ast_sequence
: {
1619 /* It should not be possible to generate a sequence in the AST without
1620 * any expressions in it.
1622 assert(!this->expressions
.is_empty());
1624 /* The r-value of a sequence is the last expression in the sequence. If
1625 * the other expressions in the sequence do not have side-effects (and
1626 * therefore add instructions to the instruction list), they get dropped
1629 exec_node
*previous_tail_pred
= NULL
;
1630 YYLTYPE previous_operand_loc
= loc
;
1632 foreach_list_typed (ast_node
, ast
, link
, &this->expressions
) {
1633 /* If one of the operands of comma operator does not generate any
1634 * code, we want to emit a warning. At each pass through the loop
1635 * previous_tail_pred will point to the last instruction in the
1636 * stream *before* processing the previous operand. Naturally,
1637 * instructions->tail_pred will point to the last instruction in the
1638 * stream *after* processing the previous operand. If the two
1639 * pointers match, then the previous operand had no effect.
1641 * The warning behavior here differs slightly from GCC. GCC will
1642 * only emit a warning if none of the left-hand operands have an
1643 * effect. However, it will emit a warning for each. I believe that
1644 * there are some cases in C (especially with GCC extensions) where
1645 * it is useful to have an intermediate step in a sequence have no
1646 * effect, but I don't think these cases exist in GLSL. Either way,
1647 * it would be a giant hassle to replicate that behavior.
1649 if (previous_tail_pred
== instructions
->tail_pred
) {
1650 _mesa_glsl_warning(&previous_operand_loc
, state
,
1651 "left-hand operand of comma expression has "
1655 /* tail_pred is directly accessed instead of using the get_tail()
1656 * method for performance reasons. get_tail() has extra code to
1657 * return NULL when the list is empty. We don't care about that
1658 * here, so using tail_pred directly is fine.
1660 previous_tail_pred
= instructions
->tail_pred
;
1661 previous_operand_loc
= ast
->get_location();
1663 result
= ast
->hir(instructions
, state
);
1666 /* Any errors should have already been emitted in the loop above.
1668 error_emitted
= true;
1672 type
= NULL
; /* use result->type, not type. */
1673 assert(result
!= NULL
);
1675 if (result
->type
->is_error() && !error_emitted
)
1676 _mesa_glsl_error(& loc
, state
, "type mismatch");
1683 ast_expression_statement::hir(exec_list
*instructions
,
1684 struct _mesa_glsl_parse_state
*state
)
1686 /* It is possible to have expression statements that don't have an
1687 * expression. This is the solitary semicolon:
1689 * for (i = 0; i < 5; i++)
1692 * In this case the expression will be NULL. Test for NULL and don't do
1693 * anything in that case.
1695 if (expression
!= NULL
)
1696 expression
->hir(instructions
, state
);
1698 /* Statements do not have r-values.
1705 ast_compound_statement::hir(exec_list
*instructions
,
1706 struct _mesa_glsl_parse_state
*state
)
1709 state
->symbols
->push_scope();
1711 foreach_list_typed (ast_node
, ast
, link
, &this->statements
)
1712 ast
->hir(instructions
, state
);
1715 state
->symbols
->pop_scope();
1717 /* Compound statements do not have r-values.
1723 static const glsl_type
*
1724 process_array_type(YYLTYPE
*loc
, const glsl_type
*base
, ast_node
*array_size
,
1725 struct _mesa_glsl_parse_state
*state
)
1727 unsigned length
= 0;
1730 return glsl_type::error_type
;
1732 /* From page 19 (page 25) of the GLSL 1.20 spec:
1734 * "Only one-dimensional arrays may be declared."
1736 if (base
->is_array()) {
1737 _mesa_glsl_error(loc
, state
,
1738 "invalid array of `%s' (only one-dimensional arrays "
1741 return glsl_type::error_type
;
1744 if (array_size
!= NULL
) {
1745 exec_list dummy_instructions
;
1746 ir_rvalue
*const ir
= array_size
->hir(& dummy_instructions
, state
);
1747 YYLTYPE loc
= array_size
->get_location();
1750 if (!ir
->type
->is_integer()) {
1751 _mesa_glsl_error(& loc
, state
, "array size must be integer type");
1752 } else if (!ir
->type
->is_scalar()) {
1753 _mesa_glsl_error(& loc
, state
, "array size must be scalar type");
1755 ir_constant
*const size
= ir
->constant_expression_value();
1758 _mesa_glsl_error(& loc
, state
, "array size must be a "
1759 "constant valued expression");
1760 } else if (size
->value
.i
[0] <= 0) {
1761 _mesa_glsl_error(& loc
, state
, "array size must be > 0");
1763 assert(size
->type
== ir
->type
);
1764 length
= size
->value
.u
[0];
1766 /* If the array size is const (and we've verified that
1767 * it is) then no instructions should have been emitted
1768 * when we converted it to HIR. If they were emitted,
1769 * then either the array size isn't const after all, or
1770 * we are emitting unnecessary instructions.
1772 assert(dummy_instructions
.is_empty());
1778 const glsl_type
*array_type
= glsl_type::get_array_instance(base
, length
);
1779 return array_type
!= NULL
? array_type
: glsl_type::error_type
;
1784 ast_type_specifier::glsl_type(const char **name
,
1785 struct _mesa_glsl_parse_state
*state
) const
1787 const struct glsl_type
*type
;
1789 type
= state
->symbols
->get_type(this->type_name
);
1790 *name
= this->type_name
;
1792 if (this->is_array
) {
1793 YYLTYPE loc
= this->get_location();
1794 type
= process_array_type(&loc
, type
, this->array_size
, state
);
1801 ast_fully_specified_type::glsl_type(const char **name
,
1802 struct _mesa_glsl_parse_state
*state
) const
1804 const struct glsl_type
*type
= this->specifier
->glsl_type(name
, state
);
1809 if (type
->base_type
== GLSL_TYPE_FLOAT
1811 && state
->target
== fragment_shader
1812 && this->qualifier
.precision
== ast_precision_none
1813 && state
->symbols
->get_variable("#default precision") == NULL
) {
1814 YYLTYPE loc
= this->get_location();
1815 _mesa_glsl_error(&loc
, state
,
1816 "no precision specified this scope for type `%s'",
1824 * Determine whether a toplevel variable declaration declares a varying. This
1825 * function operates by examining the variable's mode and the shader target,
1826 * so it correctly identifies linkage variables regardless of whether they are
1827 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
1829 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
1830 * this function will produce undefined results.
1833 is_varying_var(ir_variable
*var
, _mesa_glsl_parser_targets target
)
1837 return var
->mode
== ir_var_shader_out
;
1838 case fragment_shader
:
1839 return var
->mode
== ir_var_shader_in
;
1841 return var
->mode
== ir_var_shader_out
|| var
->mode
== ir_var_shader_in
;
1847 * Matrix layout qualifiers are only allowed on certain types
1850 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state
*state
,
1852 const glsl_type
*type
)
1854 if (!type
->is_matrix() && !type
->is_record()) {
1855 _mesa_glsl_error(loc
, state
,
1856 "uniform block layout qualifiers row_major and "
1857 "column_major can only be applied to matrix and "
1859 } else if (type
->is_record()) {
1860 /* We allow 'layout(row_major)' on structure types because it's the only
1861 * way to get row-major layouts on matrices contained in structures.
1863 _mesa_glsl_warning(loc
, state
,
1864 "uniform block layout qualifiers row_major and "
1865 "column_major applied to structure types is not "
1866 "strictly conformant and my be rejected by other "
1872 validate_binding_qualifier(struct _mesa_glsl_parse_state
*state
,
1875 const ast_type_qualifier
*qual
)
1877 if (var
->mode
!= ir_var_uniform
) {
1878 _mesa_glsl_error(loc
, state
,
1879 "the \"binding\" qualifier only applies to uniforms");
1883 if (qual
->binding
< 0) {
1884 _mesa_glsl_error(loc
, state
, "binding values must be >= 0");
1888 const struct gl_context
*const ctx
= state
->ctx
;
1889 unsigned elements
= var
->type
->is_array() ? var
->type
->length
: 1;
1890 unsigned max_index
= qual
->binding
+ elements
- 1;
1892 if (var
->type
->is_interface()) {
1893 /* UBOs. From page 60 of the GLSL 4.20 specification:
1894 * "If the binding point for any uniform block instance is less than zero,
1895 * or greater than or equal to the implementation-dependent maximum
1896 * number of uniform buffer bindings, a compilation error will occur.
1897 * When the binding identifier is used with a uniform block instanced as
1898 * an array of size N, all elements of the array from binding through
1899 * binding + N – 1 must be within this range."
1901 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
1903 if (max_index
>= ctx
->Const
.MaxUniformBufferBindings
) {
1904 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d UBOs exceeds "
1905 "the maximum number of UBO binding points (%d)",
1906 qual
->binding
, elements
,
1907 ctx
->Const
.MaxUniformBufferBindings
);
1910 } else if (var
->type
->is_sampler() ||
1911 (var
->type
->is_array() && var
->type
->fields
.array
->is_sampler())) {
1912 /* Samplers. From page 63 of the GLSL 4.20 specification:
1913 * "If the binding is less than zero, or greater than or equal to the
1914 * implementation-dependent maximum supported number of units, a
1915 * compilation error will occur. When the binding identifier is used
1916 * with an array of size N, all elements of the array from binding
1917 * through binding + N - 1 must be within this range."
1920 switch (state
->target
) {
1922 limit
= ctx
->Const
.VertexProgram
.MaxTextureImageUnits
;
1924 case geometry_shader
:
1925 limit
= ctx
->Const
.GeometryProgram
.MaxTextureImageUnits
;
1927 case fragment_shader
:
1928 limit
= ctx
->Const
.FragmentProgram
.MaxTextureImageUnits
;
1932 if (max_index
>= limit
) {
1933 _mesa_glsl_error(loc
, state
, "layout(binding = %d) for %d samplers "
1934 "exceeds the maximum number of texture image units "
1935 "(%d)", qual
->binding
, elements
, limit
);
1940 _mesa_glsl_error(loc
, state
,
1941 "the \"binding\" qualifier only applies to uniform "
1942 "blocks, samplers, or arrays of samplers");
1950 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
1952 struct _mesa_glsl_parse_state
*state
,
1954 bool ubo_qualifiers_valid
,
1957 STATIC_ASSERT(sizeof(qual
->flags
.q
) <= sizeof(qual
->flags
.i
));
1959 if (qual
->flags
.q
.invariant
) {
1961 _mesa_glsl_error(loc
, state
,
1962 "variable `%s' may not be redeclared "
1963 "`invariant' after being used",
1970 if (qual
->flags
.q
.constant
|| qual
->flags
.q
.attribute
1971 || qual
->flags
.q
.uniform
1972 || (qual
->flags
.q
.varying
&& (state
->target
== fragment_shader
)))
1975 if (qual
->flags
.q
.centroid
)
1978 if (qual
->flags
.q
.attribute
&& state
->target
!= vertex_shader
) {
1979 var
->type
= glsl_type::error_type
;
1980 _mesa_glsl_error(loc
, state
,
1981 "`attribute' variables may not be declared in the "
1983 _mesa_glsl_shader_target_name(state
->target
));
1986 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
1988 * "However, the const qualifier cannot be used with out or inout."
1990 * The same section of the GLSL 4.40 spec further clarifies this saying:
1992 * "The const qualifier cannot be used with out or inout, or a
1993 * compile-time error results."
1995 if (is_parameter
&& qual
->flags
.q
.constant
&& qual
->flags
.q
.out
) {
1996 _mesa_glsl_error(loc
, state
,
1997 "`const' may not be applied to `out' or `inout' "
1998 "function parameters");
2001 /* If there is no qualifier that changes the mode of the variable, leave
2002 * the setting alone.
2004 if (qual
->flags
.q
.in
&& qual
->flags
.q
.out
)
2005 var
->mode
= ir_var_function_inout
;
2006 else if (qual
->flags
.q
.in
)
2007 var
->mode
= is_parameter
? ir_var_function_in
: ir_var_shader_in
;
2008 else if (qual
->flags
.q
.attribute
2009 || (qual
->flags
.q
.varying
&& (state
->target
== fragment_shader
)))
2010 var
->mode
= ir_var_shader_in
;
2011 else if (qual
->flags
.q
.out
)
2012 var
->mode
= is_parameter
? ir_var_function_out
: ir_var_shader_out
;
2013 else if (qual
->flags
.q
.varying
&& (state
->target
== vertex_shader
))
2014 var
->mode
= ir_var_shader_out
;
2015 else if (qual
->flags
.q
.uniform
)
2016 var
->mode
= ir_var_uniform
;
2018 if (!is_parameter
&& is_varying_var(var
, state
->target
)) {
2019 /* This variable is being used to link data between shader stages (in
2020 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2021 * that is allowed for such purposes.
2023 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2025 * "The varying qualifier can be used only with the data types
2026 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2029 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2030 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2032 * "Fragment inputs can only be signed and unsigned integers and
2033 * integer vectors, float, floating-point vectors, matrices, or
2034 * arrays of these. Structures cannot be input.
2036 * Similar text exists in the section on vertex shader outputs.
2038 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2039 * 3.00 spec allows structs as well. Varying structs are also allowed
2042 switch (var
->type
->get_scalar_type()->base_type
) {
2043 case GLSL_TYPE_FLOAT
:
2044 /* Ok in all GLSL versions */
2046 case GLSL_TYPE_UINT
:
2048 if (state
->is_version(130, 300))
2050 _mesa_glsl_error(loc
, state
,
2051 "varying variables must be of base type float in %s",
2052 state
->get_version_string());
2054 case GLSL_TYPE_STRUCT
:
2055 if (state
->is_version(150, 300))
2057 _mesa_glsl_error(loc
, state
,
2058 "varying variables may not be of type struct");
2061 _mesa_glsl_error(loc
, state
, "illegal type for a varying variable");
2066 if (state
->all_invariant
&& (state
->current_function
== NULL
)) {
2067 switch (state
->target
) {
2069 if (var
->mode
== ir_var_shader_out
)
2070 var
->invariant
= true;
2072 case geometry_shader
:
2073 if ((var
->mode
== ir_var_shader_in
)
2074 || (var
->mode
== ir_var_shader_out
))
2075 var
->invariant
= true;
2077 case fragment_shader
:
2078 if (var
->mode
== ir_var_shader_in
)
2079 var
->invariant
= true;
2084 if (qual
->flags
.q
.flat
)
2085 var
->interpolation
= INTERP_QUALIFIER_FLAT
;
2086 else if (qual
->flags
.q
.noperspective
)
2087 var
->interpolation
= INTERP_QUALIFIER_NOPERSPECTIVE
;
2088 else if (qual
->flags
.q
.smooth
)
2089 var
->interpolation
= INTERP_QUALIFIER_SMOOTH
;
2091 var
->interpolation
= INTERP_QUALIFIER_NONE
;
2093 if (var
->interpolation
!= INTERP_QUALIFIER_NONE
) {
2094 ir_variable_mode mode
= (ir_variable_mode
) var
->mode
;
2096 if (mode
!= ir_var_shader_in
&& mode
!= ir_var_shader_out
) {
2097 _mesa_glsl_error(loc
, state
,
2098 "interpolation qualifier `%s' can only be applied to "
2099 "shader inputs or outputs.",
2100 var
->interpolation_string());
2104 if ((state
->target
== vertex_shader
&& mode
== ir_var_shader_in
) ||
2105 (state
->target
== fragment_shader
&& mode
== ir_var_shader_out
)) {
2106 _mesa_glsl_error(loc
, state
,
2107 "interpolation qualifier `%s' cannot be applied to "
2108 "vertex shader inputs or fragment shader outputs",
2109 var
->interpolation_string());
2113 var
->pixel_center_integer
= qual
->flags
.q
.pixel_center_integer
;
2114 var
->origin_upper_left
= qual
->flags
.q
.origin_upper_left
;
2115 if ((qual
->flags
.q
.origin_upper_left
|| qual
->flags
.q
.pixel_center_integer
)
2116 && (strcmp(var
->name
, "gl_FragCoord") != 0)) {
2117 const char *const qual_string
= (qual
->flags
.q
.origin_upper_left
)
2118 ? "origin_upper_left" : "pixel_center_integer";
2120 _mesa_glsl_error(loc
, state
,
2121 "layout qualifier `%s' can only be applied to "
2122 "fragment shader input `gl_FragCoord'",
2126 if (qual
->flags
.q
.explicit_location
) {
2127 const bool global_scope
= (state
->current_function
== NULL
);
2129 const char *string
= "";
2131 /* In the vertex shader only shader inputs can be given explicit
2134 * In the fragment shader only shader outputs can be given explicit
2137 switch (state
->target
) {
2139 if (!global_scope
|| (var
->mode
!= ir_var_shader_in
)) {
2145 case geometry_shader
:
2146 _mesa_glsl_error(loc
, state
,
2147 "geometry shader variables cannot be given "
2148 "explicit locations");
2151 case fragment_shader
:
2152 if (!global_scope
|| (var
->mode
!= ir_var_shader_out
)) {
2160 _mesa_glsl_error(loc
, state
,
2161 "only %s shader %s variables can be given an "
2162 "explicit location",
2163 _mesa_glsl_shader_target_name(state
->target
),
2166 var
->explicit_location
= true;
2168 /* This bit of silliness is needed because invalid explicit locations
2169 * are supposed to be flagged during linking. Small negative values
2170 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2171 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2172 * The linker needs to be able to differentiate these cases. This
2173 * ensures that negative values stay negative.
2175 if (qual
->location
>= 0) {
2176 var
->location
= (state
->target
== vertex_shader
)
2177 ? (qual
->location
+ VERT_ATTRIB_GENERIC0
)
2178 : (qual
->location
+ FRAG_RESULT_DATA0
);
2180 var
->location
= qual
->location
;
2183 if (qual
->flags
.q
.explicit_index
) {
2184 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2185 * Layout Qualifiers):
2187 * "It is also a compile-time error if a fragment shader
2188 * sets a layout index to less than 0 or greater than 1."
2190 * Older specifications don't mandate a behavior; we take
2191 * this as a clarification and always generate the error.
2193 if (qual
->index
< 0 || qual
->index
> 1) {
2194 _mesa_glsl_error(loc
, state
,
2195 "explicit index may only be 0 or 1");
2197 var
->explicit_index
= true;
2198 var
->index
= qual
->index
;
2202 } else if (qual
->flags
.q
.explicit_index
) {
2203 _mesa_glsl_error(loc
, state
,
2204 "explicit index requires explicit location");
2207 if (qual
->flags
.q
.explicit_binding
&&
2208 validate_binding_qualifier(state
, loc
, var
, qual
)) {
2209 var
->explicit_binding
= true;
2210 var
->binding
= qual
->binding
;
2213 /* Does the declaration use the deprecated 'attribute' or 'varying'
2216 const bool uses_deprecated_qualifier
= qual
->flags
.q
.attribute
2217 || qual
->flags
.q
.varying
;
2219 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2220 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2221 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2222 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2223 * These extensions and all following extensions that add the 'layout'
2224 * keyword have been modified to require the use of 'in' or 'out'.
2226 * The following extension do not allow the deprecated keywords:
2228 * GL_AMD_conservative_depth
2229 * GL_ARB_conservative_depth
2230 * GL_ARB_gpu_shader5
2231 * GL_ARB_separate_shader_objects
2232 * GL_ARB_tesselation_shader
2233 * GL_ARB_transform_feedback3
2234 * GL_ARB_uniform_buffer_object
2236 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2237 * allow layout with the deprecated keywords.
2239 const bool relaxed_layout_qualifier_checking
=
2240 state
->ARB_fragment_coord_conventions_enable
;
2242 if (qual
->has_layout() && uses_deprecated_qualifier
) {
2243 if (relaxed_layout_qualifier_checking
) {
2244 _mesa_glsl_warning(loc
, state
,
2245 "`layout' qualifier may not be used with "
2246 "`attribute' or `varying'");
2248 _mesa_glsl_error(loc
, state
,
2249 "`layout' qualifier may not be used with "
2250 "`attribute' or `varying'");
2254 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2255 * AMD_conservative_depth.
2257 int depth_layout_count
= qual
->flags
.q
.depth_any
2258 + qual
->flags
.q
.depth_greater
2259 + qual
->flags
.q
.depth_less
2260 + qual
->flags
.q
.depth_unchanged
;
2261 if (depth_layout_count
> 0
2262 && !state
->AMD_conservative_depth_enable
2263 && !state
->ARB_conservative_depth_enable
) {
2264 _mesa_glsl_error(loc
, state
,
2265 "extension GL_AMD_conservative_depth or "
2266 "GL_ARB_conservative_depth must be enabled "
2267 "to use depth layout qualifiers");
2268 } else if (depth_layout_count
> 0
2269 && strcmp(var
->name
, "gl_FragDepth") != 0) {
2270 _mesa_glsl_error(loc
, state
,
2271 "depth layout qualifiers can be applied only to "
2273 } else if (depth_layout_count
> 1
2274 && strcmp(var
->name
, "gl_FragDepth") == 0) {
2275 _mesa_glsl_error(loc
, state
,
2276 "at most one depth layout qualifier can be applied to "
2279 if (qual
->flags
.q
.depth_any
)
2280 var
->depth_layout
= ir_depth_layout_any
;
2281 else if (qual
->flags
.q
.depth_greater
)
2282 var
->depth_layout
= ir_depth_layout_greater
;
2283 else if (qual
->flags
.q
.depth_less
)
2284 var
->depth_layout
= ir_depth_layout_less
;
2285 else if (qual
->flags
.q
.depth_unchanged
)
2286 var
->depth_layout
= ir_depth_layout_unchanged
;
2288 var
->depth_layout
= ir_depth_layout_none
;
2290 if (qual
->flags
.q
.std140
||
2291 qual
->flags
.q
.packed
||
2292 qual
->flags
.q
.shared
) {
2293 _mesa_glsl_error(loc
, state
,
2294 "uniform block layout qualifiers std140, packed, and "
2295 "shared can only be applied to uniform blocks, not "
2299 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
2300 if (!ubo_qualifiers_valid
) {
2301 _mesa_glsl_error(loc
, state
,
2302 "uniform block layout qualifiers row_major and "
2303 "column_major can only be applied to uniform block "
2306 validate_matrix_layout_for_type(state
, loc
, var
->type
);
2311 * Get the variable that is being redeclared by this declaration
2313 * Semantic checks to verify the validity of the redeclaration are also
2314 * performed. If semantic checks fail, compilation error will be emitted via
2315 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2318 * A pointer to an existing variable in the current scope if the declaration
2319 * is a redeclaration, \c NULL otherwise.
2322 get_variable_being_redeclared(ir_variable
*var
, ast_declaration
*decl
,
2323 struct _mesa_glsl_parse_state
*state
)
2325 /* Check if this declaration is actually a re-declaration, either to
2326 * resize an array or add qualifiers to an existing variable.
2328 * This is allowed for variables in the current scope, or when at
2329 * global scope (for built-ins in the implicit outer scope).
2331 ir_variable
*earlier
= state
->symbols
->get_variable(decl
->identifier
);
2332 if (earlier
== NULL
||
2333 (state
->current_function
!= NULL
&&
2334 !state
->symbols
->name_declared_this_scope(decl
->identifier
))) {
2339 YYLTYPE loc
= decl
->get_location();
2341 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2343 * "It is legal to declare an array without a size and then
2344 * later re-declare the same name as an array of the same
2345 * type and specify a size."
2347 if ((earlier
->type
->array_size() == 0)
2348 && var
->type
->is_array()
2349 && (var
->type
->element_type() == earlier
->type
->element_type())) {
2350 /* FINISHME: This doesn't match the qualifiers on the two
2351 * FINISHME: declarations. It's not 100% clear whether this is
2352 * FINISHME: required or not.
2355 const unsigned size
= unsigned(var
->type
->array_size());
2356 check_builtin_array_max_size(var
->name
, size
, loc
, state
);
2357 if ((size
> 0) && (size
<= earlier
->max_array_access
)) {
2358 _mesa_glsl_error(& loc
, state
, "array size must be > %u due to "
2360 earlier
->max_array_access
);
2363 earlier
->type
= var
->type
;
2366 } else if ((state
->ARB_fragment_coord_conventions_enable
||
2367 state
->is_version(150, 0))
2368 && strcmp(var
->name
, "gl_FragCoord") == 0
2369 && earlier
->type
== var
->type
2370 && earlier
->mode
== var
->mode
) {
2371 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2374 earlier
->origin_upper_left
= var
->origin_upper_left
;
2375 earlier
->pixel_center_integer
= var
->pixel_center_integer
;
2377 /* According to section 4.3.7 of the GLSL 1.30 spec,
2378 * the following built-in varaibles can be redeclared with an
2379 * interpolation qualifier:
2382 * * gl_FrontSecondaryColor
2383 * * gl_BackSecondaryColor
2385 * * gl_SecondaryColor
2387 } else if (state
->is_version(130, 0)
2388 && (strcmp(var
->name
, "gl_FrontColor") == 0
2389 || strcmp(var
->name
, "gl_BackColor") == 0
2390 || strcmp(var
->name
, "gl_FrontSecondaryColor") == 0
2391 || strcmp(var
->name
, "gl_BackSecondaryColor") == 0
2392 || strcmp(var
->name
, "gl_Color") == 0
2393 || strcmp(var
->name
, "gl_SecondaryColor") == 0)
2394 && earlier
->type
== var
->type
2395 && earlier
->mode
== var
->mode
) {
2396 earlier
->interpolation
= var
->interpolation
;
2398 /* Layout qualifiers for gl_FragDepth. */
2399 } else if ((state
->AMD_conservative_depth_enable
||
2400 state
->ARB_conservative_depth_enable
)
2401 && strcmp(var
->name
, "gl_FragDepth") == 0
2402 && earlier
->type
== var
->type
2403 && earlier
->mode
== var
->mode
) {
2405 /** From the AMD_conservative_depth spec:
2406 * Within any shader, the first redeclarations of gl_FragDepth
2407 * must appear before any use of gl_FragDepth.
2409 if (earlier
->used
) {
2410 _mesa_glsl_error(&loc
, state
,
2411 "the first redeclaration of gl_FragDepth "
2412 "must appear before any use of gl_FragDepth");
2415 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2416 if (earlier
->depth_layout
!= ir_depth_layout_none
2417 && earlier
->depth_layout
!= var
->depth_layout
) {
2418 _mesa_glsl_error(&loc
, state
,
2419 "gl_FragDepth: depth layout is declared here "
2420 "as '%s, but it was previously declared as "
2422 depth_layout_string(var
->depth_layout
),
2423 depth_layout_string(earlier
->depth_layout
));
2426 earlier
->depth_layout
= var
->depth_layout
;
2429 _mesa_glsl_error(&loc
, state
, "`%s' redeclared", decl
->identifier
);
2436 * Generate the IR for an initializer in a variable declaration
2439 process_initializer(ir_variable
*var
, ast_declaration
*decl
,
2440 ast_fully_specified_type
*type
,
2441 exec_list
*initializer_instructions
,
2442 struct _mesa_glsl_parse_state
*state
)
2444 ir_rvalue
*result
= NULL
;
2446 YYLTYPE initializer_loc
= decl
->initializer
->get_location();
2448 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2450 * "All uniform variables are read-only and are initialized either
2451 * directly by an application via API commands, or indirectly by
2454 if (var
->mode
== ir_var_uniform
) {
2455 state
->check_version(120, 0, &initializer_loc
,
2456 "cannot initialize uniforms");
2459 if (var
->type
->is_sampler()) {
2460 _mesa_glsl_error(& initializer_loc
, state
,
2461 "cannot initialize samplers");
2464 if ((var
->mode
== ir_var_shader_in
) && (state
->current_function
== NULL
)) {
2465 _mesa_glsl_error(& initializer_loc
, state
,
2466 "cannot initialize %s shader input / %s",
2467 _mesa_glsl_shader_target_name(state
->target
),
2468 (state
->target
== vertex_shader
)
2469 ? "attribute" : "varying");
2472 ir_dereference
*const lhs
= new(state
) ir_dereference_variable(var
);
2473 ir_rvalue
*rhs
= decl
->initializer
->hir(initializer_instructions
,
2476 /* Calculate the constant value if this is a const or uniform
2479 if (type
->qualifier
.flags
.q
.constant
2480 || type
->qualifier
.flags
.q
.uniform
) {
2481 ir_rvalue
*new_rhs
= validate_assignment(state
, var
->type
, rhs
, true);
2482 if (new_rhs
!= NULL
) {
2485 ir_constant
*constant_value
= rhs
->constant_expression_value();
2486 if (!constant_value
) {
2487 /* If ARB_shading_language_420pack is enabled, initializers of
2488 * const-qualified local variables do not have to be constant
2489 * expressions. Const-qualified global variables must still be
2490 * initialized with constant expressions.
2492 if (!state
->ARB_shading_language_420pack_enable
2493 || state
->current_function
== NULL
) {
2494 _mesa_glsl_error(& initializer_loc
, state
,
2495 "initializer of %s variable `%s' must be a "
2496 "constant expression",
2497 (type
->qualifier
.flags
.q
.constant
)
2498 ? "const" : "uniform",
2500 if (var
->type
->is_numeric()) {
2501 /* Reduce cascading errors. */
2502 var
->constant_value
= ir_constant::zero(state
, var
->type
);
2506 rhs
= constant_value
;
2507 var
->constant_value
= constant_value
;
2510 _mesa_glsl_error(&initializer_loc
, state
,
2511 "initializer of type %s cannot be assigned to "
2512 "variable of type %s",
2513 rhs
->type
->name
, var
->type
->name
);
2514 if (var
->type
->is_numeric()) {
2515 /* Reduce cascading errors. */
2516 var
->constant_value
= ir_constant::zero(state
, var
->type
);
2521 if (rhs
&& !rhs
->type
->is_error()) {
2522 bool temp
= var
->read_only
;
2523 if (type
->qualifier
.flags
.q
.constant
)
2524 var
->read_only
= false;
2526 /* Never emit code to initialize a uniform.
2528 const glsl_type
*initializer_type
;
2529 if (!type
->qualifier
.flags
.q
.uniform
) {
2530 result
= do_assignment(initializer_instructions
, state
,
2533 type
->get_location());
2534 initializer_type
= result
->type
;
2536 initializer_type
= rhs
->type
;
2538 var
->constant_initializer
= rhs
->constant_expression_value();
2539 var
->has_initializer
= true;
2541 /* If the declared variable is an unsized array, it must inherrit
2542 * its full type from the initializer. A declaration such as
2544 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
2548 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
2550 * The assignment generated in the if-statement (below) will also
2551 * automatically handle this case for non-uniforms.
2553 * If the declared variable is not an array, the types must
2554 * already match exactly. As a result, the type assignment
2555 * here can be done unconditionally. For non-uniforms the call
2556 * to do_assignment can change the type of the initializer (via
2557 * the implicit conversion rules). For uniforms the initializer
2558 * must be a constant expression, and the type of that expression
2559 * was validated above.
2561 var
->type
= initializer_type
;
2563 var
->read_only
= temp
;
2571 * Do additional processing necessary for geometry shader input declarations
2572 * (this covers both interface blocks arrays and bare input variables).
2575 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state
*state
,
2576 YYLTYPE loc
, ir_variable
*var
)
2578 unsigned num_vertices
= 0;
2579 if (state
->gs_input_prim_type_specified
) {
2580 num_vertices
= vertices_per_prim(state
->gs_input_prim_type
);
2583 /* Geometry shader input variables must be arrays. Caller should have
2584 * reported an error for this.
2586 if (!var
->type
->is_array()) {
2587 assert(state
->error
);
2589 /* To avoid cascading failures, short circuit the checks below. */
2593 if (var
->type
->length
== 0) {
2594 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
2596 * All geometry shader input unsized array declarations will be
2597 * sized by an earlier input layout qualifier, when present, as per
2598 * the following table.
2600 * Followed by a table mapping each allowed input layout qualifier to
2601 * the corresponding input length.
2603 if (num_vertices
!= 0)
2604 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
2607 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
2608 * includes the following examples of compile-time errors:
2610 * // code sequence within one shader...
2611 * in vec4 Color1[]; // size unknown
2612 * ...Color1.length()...// illegal, length() unknown
2613 * in vec4 Color2[2]; // size is 2
2614 * ...Color1.length()...// illegal, Color1 still has no size
2615 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
2616 * layout(lines) in; // legal, input size is 2, matching
2617 * in vec4 Color4[3]; // illegal, contradicts layout
2620 * To detect the case illustrated by Color3, we verify that the size of
2621 * an explicitly-sized array matches the size of any previously declared
2622 * explicitly-sized array. To detect the case illustrated by Color4, we
2623 * verify that the size of an explicitly-sized array is consistent with
2624 * any previously declared input layout.
2626 if (num_vertices
!= 0 && var
->type
->length
!= num_vertices
) {
2627 _mesa_glsl_error(&loc
, state
,
2628 "geometry shader input size contradicts previously"
2629 " declared layout (size is %u, but layout requires a"
2630 " size of %u)", var
->type
->length
, num_vertices
);
2631 } else if (state
->gs_input_size
!= 0 &&
2632 var
->type
->length
!= state
->gs_input_size
) {
2633 _mesa_glsl_error(&loc
, state
,
2634 "geometry shader input sizes are "
2635 "inconsistent (size is %u, but a previous "
2636 "declaration has size %u)",
2637 var
->type
->length
, state
->gs_input_size
);
2639 state
->gs_input_size
= var
->type
->length
;
2645 ast_declarator_list::hir(exec_list
*instructions
,
2646 struct _mesa_glsl_parse_state
*state
)
2649 const struct glsl_type
*decl_type
;
2650 const char *type_name
= NULL
;
2651 ir_rvalue
*result
= NULL
;
2652 YYLTYPE loc
= this->get_location();
2654 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
2656 * "To ensure that a particular output variable is invariant, it is
2657 * necessary to use the invariant qualifier. It can either be used to
2658 * qualify a previously declared variable as being invariant
2660 * invariant gl_Position; // make existing gl_Position be invariant"
2662 * In these cases the parser will set the 'invariant' flag in the declarator
2663 * list, and the type will be NULL.
2665 if (this->invariant
) {
2666 assert(this->type
== NULL
);
2668 if (state
->current_function
!= NULL
) {
2669 _mesa_glsl_error(& loc
, state
,
2670 "all uses of `invariant' keyword must be at global "
2674 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
2675 assert(!decl
->is_array
);
2676 assert(decl
->array_size
== NULL
);
2677 assert(decl
->initializer
== NULL
);
2679 ir_variable
*const earlier
=
2680 state
->symbols
->get_variable(decl
->identifier
);
2681 if (earlier
== NULL
) {
2682 _mesa_glsl_error(& loc
, state
,
2683 "undeclared variable `%s' cannot be marked "
2684 "invariant", decl
->identifier
);
2685 } else if ((state
->target
== vertex_shader
)
2686 && (earlier
->mode
!= ir_var_shader_out
)) {
2687 _mesa_glsl_error(& loc
, state
,
2688 "`%s' cannot be marked invariant, vertex shader "
2689 "outputs only", decl
->identifier
);
2690 } else if ((state
->target
== fragment_shader
)
2691 && (earlier
->mode
!= ir_var_shader_in
)) {
2692 _mesa_glsl_error(& loc
, state
,
2693 "`%s' cannot be marked invariant, fragment shader "
2694 "inputs only", decl
->identifier
);
2695 } else if (earlier
->used
) {
2696 _mesa_glsl_error(& loc
, state
,
2697 "variable `%s' may not be redeclared "
2698 "`invariant' after being used",
2701 earlier
->invariant
= true;
2705 /* Invariant redeclarations do not have r-values.
2710 assert(this->type
!= NULL
);
2711 assert(!this->invariant
);
2713 /* The type specifier may contain a structure definition. Process that
2714 * before any of the variable declarations.
2716 (void) this->type
->specifier
->hir(instructions
, state
);
2718 decl_type
= this->type
->glsl_type(& type_name
, state
);
2719 if (this->declarations
.is_empty()) {
2720 /* If there is no structure involved in the program text, there are two
2721 * possible scenarios:
2723 * - The program text contained something like 'vec4;'. This is an
2724 * empty declaration. It is valid but weird. Emit a warning.
2726 * - The program text contained something like 'S;' and 'S' is not the
2727 * name of a known structure type. This is both invalid and weird.
2730 * - The program text contained something like 'mediump float;'
2731 * when the programmer probably meant 'precision mediump
2732 * float;' Emit a warning with a description of what they
2733 * probably meant to do.
2735 * Note that if decl_type is NULL and there is a structure involved,
2736 * there must have been some sort of error with the structure. In this
2737 * case we assume that an error was already generated on this line of
2738 * code for the structure. There is no need to generate an additional,
2741 assert(this->type
->specifier
->structure
== NULL
|| decl_type
!= NULL
2744 if (decl_type
== NULL
) {
2745 _mesa_glsl_error(&loc
, state
,
2746 "invalid type `%s' in empty declaration",
2748 } else if (this->type
->qualifier
.precision
!= ast_precision_none
) {
2749 if (this->type
->specifier
->structure
!= NULL
) {
2750 _mesa_glsl_error(&loc
, state
,
2751 "precision qualifiers can't be applied "
2754 static const char *const precision_names
[] = {
2761 _mesa_glsl_warning(&loc
, state
,
2762 "empty declaration with precision qualifier, "
2763 "to set the default precision, use "
2764 "`precision %s %s;'",
2765 precision_names
[this->type
->qualifier
.precision
],
2769 _mesa_glsl_warning(&loc
, state
, "empty declaration");
2773 foreach_list_typed (ast_declaration
, decl
, link
, &this->declarations
) {
2774 const struct glsl_type
*var_type
;
2777 /* FINISHME: Emit a warning if a variable declaration shadows a
2778 * FINISHME: declaration at a higher scope.
2781 if ((decl_type
== NULL
) || decl_type
->is_void()) {
2782 if (type_name
!= NULL
) {
2783 _mesa_glsl_error(& loc
, state
,
2784 "invalid type `%s' in declaration of `%s'",
2785 type_name
, decl
->identifier
);
2787 _mesa_glsl_error(& loc
, state
,
2788 "invalid type in declaration of `%s'",
2794 if (decl
->is_array
) {
2795 var_type
= process_array_type(&loc
, decl_type
, decl
->array_size
,
2797 if (var_type
->is_error())
2800 var_type
= decl_type
;
2803 var
= new(ctx
) ir_variable(var_type
, decl
->identifier
, ir_var_auto
);
2805 /* The 'varying in' and 'varying out' qualifiers can only be used with
2806 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
2809 if (this->type
->qualifier
.flags
.q
.varying
) {
2810 if (this->type
->qualifier
.flags
.q
.in
) {
2811 _mesa_glsl_error(& loc
, state
,
2812 "`varying in' qualifier in declaration of "
2813 "`%s' only valid for geometry shaders using "
2814 "ARB_geometry_shader4 or EXT_geometry_shader4",
2816 } else if (this->type
->qualifier
.flags
.q
.out
) {
2817 _mesa_glsl_error(& loc
, state
,
2818 "`varying out' qualifier in declaration of "
2819 "`%s' only valid for geometry shaders using "
2820 "ARB_geometry_shader4 or EXT_geometry_shader4",
2825 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
2827 * "Global variables can only use the qualifiers const,
2828 * attribute, uni form, or varying. Only one may be
2831 * Local variables can only use the qualifier const."
2833 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
2834 * any extension that adds the 'layout' keyword.
2836 if (!state
->is_version(130, 300)
2837 && !state
->ARB_explicit_attrib_location_enable
2838 && !state
->ARB_fragment_coord_conventions_enable
) {
2839 if (this->type
->qualifier
.flags
.q
.out
) {
2840 _mesa_glsl_error(& loc
, state
,
2841 "`out' qualifier in declaration of `%s' "
2842 "only valid for function parameters in %s",
2843 decl
->identifier
, state
->get_version_string());
2845 if (this->type
->qualifier
.flags
.q
.in
) {
2846 _mesa_glsl_error(& loc
, state
,
2847 "`in' qualifier in declaration of `%s' "
2848 "only valid for function parameters in %s",
2849 decl
->identifier
, state
->get_version_string());
2851 /* FINISHME: Test for other invalid qualifiers. */
2854 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
,
2855 & loc
, this->ubo_qualifiers_valid
, false);
2857 if (this->type
->qualifier
.flags
.q
.invariant
) {
2858 if ((state
->target
== vertex_shader
) &&
2859 var
->mode
!= ir_var_shader_out
) {
2860 _mesa_glsl_error(& loc
, state
,
2861 "`%s' cannot be marked invariant, vertex shader "
2862 "outputs only", var
->name
);
2863 } else if ((state
->target
== fragment_shader
) &&
2864 var
->mode
!= ir_var_shader_in
) {
2865 /* FINISHME: Note that this doesn't work for invariant on
2866 * a function signature inval
2868 _mesa_glsl_error(& loc
, state
,
2869 "`%s' cannot be marked invariant, fragment shader "
2870 "inputs only", var
->name
);
2874 if (state
->current_function
!= NULL
) {
2875 const char *mode
= NULL
;
2876 const char *extra
= "";
2878 /* There is no need to check for 'inout' here because the parser will
2879 * only allow that in function parameter lists.
2881 if (this->type
->qualifier
.flags
.q
.attribute
) {
2883 } else if (this->type
->qualifier
.flags
.q
.uniform
) {
2885 } else if (this->type
->qualifier
.flags
.q
.varying
) {
2887 } else if (this->type
->qualifier
.flags
.q
.in
) {
2889 extra
= " or in function parameter list";
2890 } else if (this->type
->qualifier
.flags
.q
.out
) {
2892 extra
= " or in function parameter list";
2896 _mesa_glsl_error(& loc
, state
,
2897 "%s variable `%s' must be declared at "
2899 mode
, var
->name
, extra
);
2901 } else if (var
->mode
== ir_var_shader_in
) {
2902 var
->read_only
= true;
2904 if (state
->target
== vertex_shader
) {
2905 bool error_emitted
= false;
2907 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2909 * "Vertex shader inputs can only be float, floating-point
2910 * vectors, matrices, signed and unsigned integers and integer
2911 * vectors. Vertex shader inputs can also form arrays of these
2912 * types, but not structures."
2914 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
2916 * "Vertex shader inputs can only be float, floating-point
2917 * vectors, matrices, signed and unsigned integers and integer
2918 * vectors. They cannot be arrays or structures."
2920 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
2922 * "The attribute qualifier can be used only with float,
2923 * floating-point vectors, and matrices. Attribute variables
2924 * cannot be declared as arrays or structures."
2926 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
2928 * "Vertex shader inputs can only be float, floating-point
2929 * vectors, matrices, signed and unsigned integers and integer
2930 * vectors. Vertex shader inputs cannot be arrays or
2933 const glsl_type
*check_type
= var
->type
->is_array()
2934 ? var
->type
->fields
.array
: var
->type
;
2936 switch (check_type
->base_type
) {
2937 case GLSL_TYPE_FLOAT
:
2939 case GLSL_TYPE_UINT
:
2941 if (state
->is_version(120, 300))
2945 _mesa_glsl_error(& loc
, state
,
2946 "vertex shader input / attribute cannot have "
2948 var
->type
->is_array() ? "array of " : "",
2950 error_emitted
= true;
2953 if (!error_emitted
&& var
->type
->is_array() &&
2954 !state
->check_version(150, 0, &loc
,
2955 "vertex shader input / attribute "
2956 "cannot have array type")) {
2957 error_emitted
= true;
2959 } else if (state
->target
== geometry_shader
) {
2960 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
2962 * Geometry shader input variables get the per-vertex values
2963 * written out by vertex shader output variables of the same
2964 * names. Since a geometry shader operates on a set of
2965 * vertices, each input varying variable (or input block, see
2966 * interface blocks below) needs to be declared as an array.
2968 if (!var
->type
->is_array()) {
2969 _mesa_glsl_error(&loc
, state
,
2970 "geometry shader inputs must be arrays");
2973 handle_geometry_shader_input_decl(state
, loc
, var
);
2977 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
2978 * so must integer vertex outputs.
2980 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
2981 * "Fragment shader inputs that are signed or unsigned integers or
2982 * integer vectors must be qualified with the interpolation qualifier
2985 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
2986 * "Fragment shader inputs that are, or contain, signed or unsigned
2987 * integers or integer vectors must be qualified with the
2988 * interpolation qualifier flat."
2990 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
2991 * "Vertex shader outputs that are, or contain, signed or unsigned
2992 * integers or integer vectors must be qualified with the
2993 * interpolation qualifier flat."
2995 * Note that prior to GLSL 1.50, this requirement applied to vertex
2996 * outputs rather than fragment inputs. That creates problems in the
2997 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
2998 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
2999 * apply the restriction to both vertex outputs and fragment inputs.
3001 * Note also that the desktop GLSL specs are missing the text "or
3002 * contain"; this is presumably an oversight, since there is no
3003 * reasonable way to interpolate a fragment shader input that contains
3006 if (state
->is_version(130, 300) &&
3007 var
->type
->contains_integer() &&
3008 var
->interpolation
!= INTERP_QUALIFIER_FLAT
&&
3009 ((state
->target
== fragment_shader
&& var
->mode
== ir_var_shader_in
)
3010 || (state
->target
== vertex_shader
&& var
->mode
== ir_var_shader_out
3011 && state
->es_shader
))) {
3012 const char *var_type
= (state
->target
== vertex_shader
) ?
3013 "vertex output" : "fragment input";
3014 _mesa_glsl_error(&loc
, state
, "if a %s is (or contains) "
3015 "an integer, then it must be qualified with 'flat'",
3020 /* Interpolation qualifiers cannot be applied to 'centroid' and
3021 * 'centroid varying'.
3023 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3024 * "interpolation qualifiers may only precede the qualifiers in,
3025 * centroid in, out, or centroid out in a declaration. They do not apply
3026 * to the deprecated storage qualifiers varying or centroid varying."
3028 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3030 if (state
->is_version(130, 0)
3031 && this->type
->qualifier
.has_interpolation()
3032 && this->type
->qualifier
.flags
.q
.varying
) {
3034 const char *i
= this->type
->qualifier
.interpolation_string();
3037 if (this->type
->qualifier
.flags
.q
.centroid
)
3038 s
= "centroid varying";
3042 _mesa_glsl_error(&loc
, state
,
3043 "qualifier '%s' cannot be applied to the "
3044 "deprecated storage qualifier '%s'", i
, s
);
3048 /* Interpolation qualifiers can only apply to vertex shader outputs and
3049 * fragment shader inputs.
3051 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3052 * "Outputs from a vertex shader (out) and inputs to a fragment
3053 * shader (in) can be further qualified with one or more of these
3054 * interpolation qualifiers"
3056 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
3057 * "These interpolation qualifiers may only precede the qualifiers
3058 * in, centroid in, out, or centroid out in a declaration. They do
3059 * not apply to inputs into a vertex shader or outputs from a
3062 if (state
->is_version(130, 300)
3063 && this->type
->qualifier
.has_interpolation()) {
3065 const char *i
= this->type
->qualifier
.interpolation_string();
3068 switch (state
->target
) {
3070 if (this->type
->qualifier
.flags
.q
.in
) {
3071 _mesa_glsl_error(&loc
, state
,
3072 "qualifier '%s' cannot be applied to vertex "
3073 "shader inputs", i
);
3076 case fragment_shader
:
3077 if (this->type
->qualifier
.flags
.q
.out
) {
3078 _mesa_glsl_error(&loc
, state
,
3079 "qualifier '%s' cannot be applied to fragment "
3080 "shader outputs", i
);
3089 /* From section 4.3.4 of the GLSL 1.30 spec:
3090 * "It is an error to use centroid in in a vertex shader."
3092 * From section 4.3.4 of the GLSL ES 3.00 spec:
3093 * "It is an error to use centroid in or interpolation qualifiers in
3094 * a vertex shader input."
3096 if (state
->is_version(130, 300)
3097 && this->type
->qualifier
.flags
.q
.centroid
3098 && this->type
->qualifier
.flags
.q
.in
3099 && state
->target
== vertex_shader
) {
3101 _mesa_glsl_error(&loc
, state
,
3102 "'centroid in' cannot be used in a vertex shader");
3105 /* Section 4.3.6 of the GLSL 1.30 specification states:
3106 * "It is an error to use centroid out in a fragment shader."
3108 * The GL_ARB_shading_language_420pack extension specification states:
3109 * "It is an error to use auxiliary storage qualifiers or interpolation
3110 * qualifiers on an output in a fragment shader."
3112 if (state
->target
== fragment_shader
&&
3113 this->type
->qualifier
.flags
.q
.out
&&
3114 this->type
->qualifier
.has_auxiliary_storage()) {
3115 _mesa_glsl_error(&loc
, state
,
3116 "auxiliary storage qualifiers cannot be used on "
3117 "fragment shader outputs");
3120 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
3122 if (this->type
->qualifier
.precision
!= ast_precision_none
) {
3123 state
->check_precision_qualifiers_allowed(&loc
);
3127 /* Precision qualifiers only apply to floating point and integer types.
3129 * From section 4.5.2 of the GLSL 1.30 spec:
3130 * "Any floating point or any integer declaration can have the type
3131 * preceded by one of these precision qualifiers [...] Literal
3132 * constants do not have precision qualifiers. Neither do Boolean
3135 * In GLSL ES, sampler types are also allowed.
3137 * From page 87 of the GLSL ES spec:
3138 * "RESOLUTION: Allow sampler types to take a precision qualifier."
3140 if (this->type
->qualifier
.precision
!= ast_precision_none
3141 && !var
->type
->is_float()
3142 && !var
->type
->is_integer()
3143 && !var
->type
->is_record()
3144 && !(var
->type
->is_sampler() && state
->es_shader
)
3145 && !(var
->type
->is_array()
3146 && (var
->type
->fields
.array
->is_float()
3147 || var
->type
->fields
.array
->is_integer()))) {
3149 _mesa_glsl_error(&loc
, state
,
3150 "precision qualifiers apply only to floating point"
3151 "%s types", state
->es_shader
? ", integer, and sampler"
3155 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3157 * "[Sampler types] can only be declared as function
3158 * parameters or uniform variables (see Section 4.3.5
3161 if (var_type
->contains_sampler() &&
3162 !this->type
->qualifier
.flags
.q
.uniform
) {
3163 _mesa_glsl_error(&loc
, state
, "samplers must be declared uniform");
3166 /* Process the initializer and add its instructions to a temporary
3167 * list. This list will be added to the instruction stream (below) after
3168 * the declaration is added. This is done because in some cases (such as
3169 * redeclarations) the declaration may not actually be added to the
3170 * instruction stream.
3172 exec_list initializer_instructions
;
3173 ir_variable
*earlier
= get_variable_being_redeclared(var
, decl
, state
);
3175 if (decl
->initializer
!= NULL
) {
3176 result
= process_initializer((earlier
== NULL
) ? var
: earlier
,
3178 &initializer_instructions
, state
);
3181 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
3183 * "It is an error to write to a const variable outside of
3184 * its declaration, so they must be initialized when
3187 if (this->type
->qualifier
.flags
.q
.constant
&& decl
->initializer
== NULL
) {
3188 _mesa_glsl_error(& loc
, state
,
3189 "const declaration of `%s' must be initialized",
3193 if (state
->es_shader
) {
3194 const glsl_type
*const t
= (earlier
== NULL
)
3195 ? var
->type
: earlier
->type
;
3197 if (t
->is_array() && t
->length
== 0)
3198 /* Section 10.17 of the GLSL ES 1.00 specification states that
3199 * unsized array declarations have been removed from the language.
3200 * Arrays that are sized using an initializer are still explicitly
3201 * sized. However, GLSL ES 1.00 does not allow array
3202 * initializers. That is only allowed in GLSL ES 3.00.
3204 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
3206 * "An array type can also be formed without specifying a size
3207 * if the definition includes an initializer:
3209 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
3210 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
3215 _mesa_glsl_error(& loc
, state
,
3216 "unsized array declarations are not allowed in "
3220 /* If the declaration is not a redeclaration, there are a few additional
3221 * semantic checks that must be applied. In addition, variable that was
3222 * created for the declaration should be added to the IR stream.
3224 if (earlier
== NULL
) {
3225 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3227 * "Identifiers starting with "gl_" are reserved for use by
3228 * OpenGL, and may not be declared in a shader as either a
3229 * variable or a function."
3231 if (strncmp(decl
->identifier
, "gl_", 3) == 0)
3232 _mesa_glsl_error(& loc
, state
,
3233 "identifier `%s' uses reserved `gl_' prefix",
3235 else if (strstr(decl
->identifier
, "__")) {
3236 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
3239 * "In addition, all identifiers containing two
3240 * consecutive underscores (__) are reserved as
3241 * possible future keywords."
3243 _mesa_glsl_error(& loc
, state
,
3244 "identifier `%s' uses reserved `__' string",
3248 /* Add the variable to the symbol table. Note that the initializer's
3249 * IR was already processed earlier (though it hasn't been emitted
3250 * yet), without the variable in scope.
3252 * This differs from most C-like languages, but it follows the GLSL
3253 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
3256 * "Within a declaration, the scope of a name starts immediately
3257 * after the initializer if present or immediately after the name
3258 * being declared if not."
3260 if (!state
->symbols
->add_variable(var
)) {
3261 YYLTYPE loc
= this->get_location();
3262 _mesa_glsl_error(&loc
, state
, "name `%s' already taken in the "
3263 "current scope", decl
->identifier
);
3267 /* Push the variable declaration to the top. It means that all the
3268 * variable declarations will appear in a funny last-to-first order,
3269 * but otherwise we run into trouble if a function is prototyped, a
3270 * global var is decled, then the function is defined with usage of
3271 * the global var. See glslparsertest's CorrectModule.frag.
3273 instructions
->push_head(var
);
3276 instructions
->append_list(&initializer_instructions
);
3280 /* Generally, variable declarations do not have r-values. However,
3281 * one is used for the declaration in
3283 * while (bool b = some_condition()) {
3287 * so we return the rvalue from the last seen declaration here.
3294 ast_parameter_declarator::hir(exec_list
*instructions
,
3295 struct _mesa_glsl_parse_state
*state
)
3298 const struct glsl_type
*type
;
3299 const char *name
= NULL
;
3300 YYLTYPE loc
= this->get_location();
3302 type
= this->type
->glsl_type(& name
, state
);
3306 _mesa_glsl_error(& loc
, state
,
3307 "invalid type `%s' in declaration of `%s'",
3308 name
, this->identifier
);
3310 _mesa_glsl_error(& loc
, state
,
3311 "invalid type in declaration of `%s'",
3315 type
= glsl_type::error_type
;
3318 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
3320 * "Functions that accept no input arguments need not use void in the
3321 * argument list because prototypes (or definitions) are required and
3322 * therefore there is no ambiguity when an empty argument list "( )" is
3323 * declared. The idiom "(void)" as a parameter list is provided for
3326 * Placing this check here prevents a void parameter being set up
3327 * for a function, which avoids tripping up checks for main taking
3328 * parameters and lookups of an unnamed symbol.
3330 if (type
->is_void()) {
3331 if (this->identifier
!= NULL
)
3332 _mesa_glsl_error(& loc
, state
,
3333 "named parameter cannot have type `void'");
3339 if (formal_parameter
&& (this->identifier
== NULL
)) {
3340 _mesa_glsl_error(& loc
, state
, "formal parameter lacks a name");
3344 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
3345 * call already handled the "vec4[..] foo" case.
3347 if (this->is_array
) {
3348 type
= process_array_type(&loc
, type
, this->array_size
, state
);
3351 if (!type
->is_error() && type
->array_size() == 0) {
3352 _mesa_glsl_error(&loc
, state
, "arrays passed as parameters must have "
3354 type
= glsl_type::error_type
;
3358 ir_variable
*var
= new(ctx
)
3359 ir_variable(type
, this->identifier
, ir_var_function_in
);
3361 /* Apply any specified qualifiers to the parameter declaration. Note that
3362 * for function parameters the default mode is 'in'.
3364 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
, & loc
,
3367 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3369 * "Samplers cannot be treated as l-values; hence cannot be used
3370 * as out or inout function parameters, nor can they be assigned
3373 if ((var
->mode
== ir_var_function_inout
|| var
->mode
== ir_var_function_out
)
3374 && type
->contains_sampler()) {
3375 _mesa_glsl_error(&loc
, state
, "out and inout parameters cannot contain samplers");
3376 type
= glsl_type::error_type
;
3379 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
3381 * "When calling a function, expressions that do not evaluate to
3382 * l-values cannot be passed to parameters declared as out or inout."
3384 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
3386 * "Other binary or unary expressions, non-dereferenced arrays,
3387 * function names, swizzles with repeated fields, and constants
3388 * cannot be l-values."
3390 * So for GLSL 1.10, passing an array as an out or inout parameter is not
3391 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
3393 if ((var
->mode
== ir_var_function_inout
|| var
->mode
== ir_var_function_out
)
3395 && !state
->check_version(120, 100, &loc
,
3396 "arrays cannot be out or inout parameters")) {
3397 type
= glsl_type::error_type
;
3400 instructions
->push_tail(var
);
3402 /* Parameter declarations do not have r-values.
3409 ast_parameter_declarator::parameters_to_hir(exec_list
*ast_parameters
,
3411 exec_list
*ir_parameters
,
3412 _mesa_glsl_parse_state
*state
)
3414 ast_parameter_declarator
*void_param
= NULL
;
3417 foreach_list_typed (ast_parameter_declarator
, param
, link
, ast_parameters
) {
3418 param
->formal_parameter
= formal
;
3419 param
->hir(ir_parameters
, state
);
3427 if ((void_param
!= NULL
) && (count
> 1)) {
3428 YYLTYPE loc
= void_param
->get_location();
3430 _mesa_glsl_error(& loc
, state
,
3431 "`void' parameter must be only parameter");
3437 emit_function(_mesa_glsl_parse_state
*state
, ir_function
*f
)
3439 /* IR invariants disallow function declarations or definitions
3440 * nested within other function definitions. But there is no
3441 * requirement about the relative order of function declarations
3442 * and definitions with respect to one another. So simply insert
3443 * the new ir_function block at the end of the toplevel instruction
3446 state
->toplevel_ir
->push_tail(f
);
3451 ast_function::hir(exec_list
*instructions
,
3452 struct _mesa_glsl_parse_state
*state
)
3455 ir_function
*f
= NULL
;
3456 ir_function_signature
*sig
= NULL
;
3457 exec_list hir_parameters
;
3459 const char *const name
= identifier
;
3461 /* New functions are always added to the top-level IR instruction stream,
3462 * so this instruction list pointer is ignored. See also emit_function
3465 (void) instructions
;
3467 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
3469 * "Function declarations (prototypes) cannot occur inside of functions;
3470 * they must be at global scope, or for the built-in functions, outside
3471 * the global scope."
3473 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
3475 * "User defined functions may only be defined within the global scope."
3477 * Note that this language does not appear in GLSL 1.10.
3479 if ((state
->current_function
!= NULL
) &&
3480 state
->is_version(120, 100)) {
3481 YYLTYPE loc
= this->get_location();
3482 _mesa_glsl_error(&loc
, state
,
3483 "declaration of function `%s' not allowed within "
3484 "function body", name
);
3487 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3489 * "Identifiers starting with "gl_" are reserved for use by
3490 * OpenGL, and may not be declared in a shader as either a
3491 * variable or a function."
3493 if (strncmp(name
, "gl_", 3) == 0) {
3494 YYLTYPE loc
= this->get_location();
3495 _mesa_glsl_error(&loc
, state
,
3496 "identifier `%s' uses reserved `gl_' prefix", name
);
3499 /* Convert the list of function parameters to HIR now so that they can be
3500 * used below to compare this function's signature with previously seen
3501 * signatures for functions with the same name.
3503 ast_parameter_declarator::parameters_to_hir(& this->parameters
,
3505 & hir_parameters
, state
);
3507 const char *return_type_name
;
3508 const glsl_type
*return_type
=
3509 this->return_type
->glsl_type(& return_type_name
, state
);
3512 YYLTYPE loc
= this->get_location();
3513 _mesa_glsl_error(&loc
, state
,
3514 "function `%s' has undeclared return type `%s'",
3515 name
, return_type_name
);
3516 return_type
= glsl_type::error_type
;
3519 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3520 * "No qualifier is allowed on the return type of a function."
3522 if (this->return_type
->has_qualifiers()) {
3523 YYLTYPE loc
= this->get_location();
3524 _mesa_glsl_error(& loc
, state
,
3525 "function `%s' return type has qualifiers", name
);
3528 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
3530 * "Arrays are allowed as arguments and as the return type. In both
3531 * cases, the array must be explicitly sized."
3533 if (return_type
->is_array() && return_type
->length
== 0) {
3534 YYLTYPE loc
= this->get_location();
3535 _mesa_glsl_error(& loc
, state
,
3536 "function `%s' return type array must be explicitly "
3540 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3542 * "[Sampler types] can only be declared as function parameters
3543 * or uniform variables (see Section 4.3.5 "Uniform")".
3545 if (return_type
->contains_sampler()) {
3546 YYLTYPE loc
= this->get_location();
3547 _mesa_glsl_error(&loc
, state
,
3548 "function `%s' return type can't contain a sampler",
3552 /* Verify that this function's signature either doesn't match a previously
3553 * seen signature for a function with the same name, or, if a match is found,
3554 * that the previously seen signature does not have an associated definition.
3556 f
= state
->symbols
->get_function(name
);
3557 if (f
!= NULL
&& (state
->es_shader
|| f
->has_user_signature())) {
3558 sig
= f
->exact_matching_signature(&hir_parameters
);
3560 const char *badvar
= sig
->qualifiers_match(&hir_parameters
);
3561 if (badvar
!= NULL
) {
3562 YYLTYPE loc
= this->get_location();
3564 _mesa_glsl_error(&loc
, state
, "function `%s' parameter `%s' "
3565 "qualifiers don't match prototype", name
, badvar
);
3568 if (sig
->return_type
!= return_type
) {
3569 YYLTYPE loc
= this->get_location();
3571 _mesa_glsl_error(&loc
, state
, "function `%s' return type doesn't "
3572 "match prototype", name
);
3575 if (sig
->is_defined
) {
3576 if (is_definition
) {
3577 YYLTYPE loc
= this->get_location();
3578 _mesa_glsl_error(& loc
, state
, "function `%s' redefined", name
);
3580 /* We just encountered a prototype that exactly matches a
3581 * function that's already been defined. This is redundant,
3582 * and we should ignore it.
3589 f
= new(ctx
) ir_function(name
);
3590 if (!state
->symbols
->add_function(f
)) {
3591 /* This function name shadows a non-function use of the same name. */
3592 YYLTYPE loc
= this->get_location();
3594 _mesa_glsl_error(&loc
, state
, "function name `%s' conflicts with "
3595 "non-function", name
);
3599 emit_function(state
, f
);
3602 /* Verify the return type of main() */
3603 if (strcmp(name
, "main") == 0) {
3604 if (! return_type
->is_void()) {
3605 YYLTYPE loc
= this->get_location();
3607 _mesa_glsl_error(& loc
, state
, "main() must return void");
3610 if (!hir_parameters
.is_empty()) {
3611 YYLTYPE loc
= this->get_location();
3613 _mesa_glsl_error(& loc
, state
, "main() must not take any parameters");
3617 /* Finish storing the information about this new function in its signature.
3620 sig
= new(ctx
) ir_function_signature(return_type
);
3621 f
->add_signature(sig
);
3624 sig
->replace_parameters(&hir_parameters
);
3627 /* Function declarations (prototypes) do not have r-values.
3634 ast_function_definition::hir(exec_list
*instructions
,
3635 struct _mesa_glsl_parse_state
*state
)
3637 prototype
->is_definition
= true;
3638 prototype
->hir(instructions
, state
);
3640 ir_function_signature
*signature
= prototype
->signature
;
3641 if (signature
== NULL
)
3644 assert(state
->current_function
== NULL
);
3645 state
->current_function
= signature
;
3646 state
->found_return
= false;
3648 /* Duplicate parameters declared in the prototype as concrete variables.
3649 * Add these to the symbol table.
3651 state
->symbols
->push_scope();
3652 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
3653 ir_variable
*const var
= ((ir_instruction
*) iter
.get())->as_variable();
3655 assert(var
!= NULL
);
3657 /* The only way a parameter would "exist" is if two parameters have
3660 if (state
->symbols
->name_declared_this_scope(var
->name
)) {
3661 YYLTYPE loc
= this->get_location();
3663 _mesa_glsl_error(& loc
, state
, "parameter `%s' redeclared", var
->name
);
3665 state
->symbols
->add_variable(var
);
3669 /* Convert the body of the function to HIR. */
3670 this->body
->hir(&signature
->body
, state
);
3671 signature
->is_defined
= true;
3673 state
->symbols
->pop_scope();
3675 assert(state
->current_function
== signature
);
3676 state
->current_function
= NULL
;
3678 if (!signature
->return_type
->is_void() && !state
->found_return
) {
3679 YYLTYPE loc
= this->get_location();
3680 _mesa_glsl_error(& loc
, state
, "function `%s' has non-void return type "
3681 "%s, but no return statement",
3682 signature
->function_name(),
3683 signature
->return_type
->name
);
3686 /* Function definitions do not have r-values.
3693 ast_jump_statement::hir(exec_list
*instructions
,
3694 struct _mesa_glsl_parse_state
*state
)
3701 assert(state
->current_function
);
3703 if (opt_return_value
) {
3704 ir_rvalue
*ret
= opt_return_value
->hir(instructions
, state
);
3706 /* The value of the return type can be NULL if the shader says
3707 * 'return foo();' and foo() is a function that returns void.
3709 * NOTE: The GLSL spec doesn't say that this is an error. The type
3710 * of the return value is void. If the return type of the function is
3711 * also void, then this should compile without error. Seriously.
3713 const glsl_type
*const ret_type
=
3714 (ret
== NULL
) ? glsl_type::void_type
: ret
->type
;
3716 /* Implicit conversions are not allowed for return values prior to
3717 * ARB_shading_language_420pack.
3719 if (state
->current_function
->return_type
!= ret_type
) {
3720 YYLTYPE loc
= this->get_location();
3722 if (state
->ARB_shading_language_420pack_enable
) {
3723 if (!apply_implicit_conversion(state
->current_function
->return_type
,
3725 _mesa_glsl_error(& loc
, state
,
3726 "could not implicitly convert return value "
3727 "to %s, in function `%s'",
3728 state
->current_function
->return_type
->name
,
3729 state
->current_function
->function_name());
3732 _mesa_glsl_error(& loc
, state
,
3733 "`return' with wrong type %s, in function `%s' "
3736 state
->current_function
->function_name(),
3737 state
->current_function
->return_type
->name
);
3739 } else if (state
->current_function
->return_type
->base_type
==
3741 YYLTYPE loc
= this->get_location();
3743 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
3744 * specs add a clarification:
3746 * "A void function can only use return without a return argument, even if
3747 * the return argument has void type. Return statements only accept values:
3750 * void func2() { return func1(); } // illegal return statement"
3752 _mesa_glsl_error(& loc
, state
,
3753 "void functions can only use `return' without a "
3757 inst
= new(ctx
) ir_return(ret
);
3759 if (state
->current_function
->return_type
->base_type
!=
3761 YYLTYPE loc
= this->get_location();
3763 _mesa_glsl_error(& loc
, state
,
3764 "`return' with no value, in function %s returning "
3766 state
->current_function
->function_name());
3768 inst
= new(ctx
) ir_return
;
3771 state
->found_return
= true;
3772 instructions
->push_tail(inst
);
3777 if (state
->target
!= fragment_shader
) {
3778 YYLTYPE loc
= this->get_location();
3780 _mesa_glsl_error(& loc
, state
,
3781 "`discard' may only appear in a fragment shader");
3783 instructions
->push_tail(new(ctx
) ir_discard
);
3788 if (mode
== ast_continue
&&
3789 state
->loop_nesting_ast
== NULL
) {
3790 YYLTYPE loc
= this->get_location();
3792 _mesa_glsl_error(& loc
, state
,
3793 "continue may only appear in a loop");
3794 } else if (mode
== ast_break
&&
3795 state
->loop_nesting_ast
== NULL
&&
3796 state
->switch_state
.switch_nesting_ast
== NULL
) {
3797 YYLTYPE loc
= this->get_location();
3799 _mesa_glsl_error(& loc
, state
,
3800 "break may only appear in a loop or a switch");
3802 /* For a loop, inline the for loop expression again,
3803 * since we don't know where near the end of
3804 * the loop body the normal copy of it
3805 * is going to be placed.
3807 if (state
->loop_nesting_ast
!= NULL
&&
3808 mode
== ast_continue
&&
3809 state
->loop_nesting_ast
->rest_expression
) {
3810 state
->loop_nesting_ast
->rest_expression
->hir(instructions
,
3814 if (state
->switch_state
.is_switch_innermost
&&
3815 mode
== ast_break
) {
3816 /* Force break out of switch by setting is_break switch state.
3818 ir_variable
*const is_break_var
= state
->switch_state
.is_break_var
;
3819 ir_dereference_variable
*const deref_is_break_var
=
3820 new(ctx
) ir_dereference_variable(is_break_var
);
3821 ir_constant
*const true_val
= new(ctx
) ir_constant(true);
3822 ir_assignment
*const set_break_var
=
3823 new(ctx
) ir_assignment(deref_is_break_var
, true_val
);
3825 instructions
->push_tail(set_break_var
);
3828 ir_loop_jump
*const jump
=
3829 new(ctx
) ir_loop_jump((mode
== ast_break
)
3830 ? ir_loop_jump::jump_break
3831 : ir_loop_jump::jump_continue
);
3832 instructions
->push_tail(jump
);
3839 /* Jump instructions do not have r-values.
3846 ast_selection_statement::hir(exec_list
*instructions
,
3847 struct _mesa_glsl_parse_state
*state
)
3851 ir_rvalue
*const condition
= this->condition
->hir(instructions
, state
);
3853 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
3855 * "Any expression whose type evaluates to a Boolean can be used as the
3856 * conditional expression bool-expression. Vector types are not accepted
3857 * as the expression to if."
3859 * The checks are separated so that higher quality diagnostics can be
3860 * generated for cases where both rules are violated.
3862 if (!condition
->type
->is_boolean() || !condition
->type
->is_scalar()) {
3863 YYLTYPE loc
= this->condition
->get_location();
3865 _mesa_glsl_error(& loc
, state
, "if-statement condition must be scalar "
3869 ir_if
*const stmt
= new(ctx
) ir_if(condition
);
3871 if (then_statement
!= NULL
) {
3872 state
->symbols
->push_scope();
3873 then_statement
->hir(& stmt
->then_instructions
, state
);
3874 state
->symbols
->pop_scope();
3877 if (else_statement
!= NULL
) {
3878 state
->symbols
->push_scope();
3879 else_statement
->hir(& stmt
->else_instructions
, state
);
3880 state
->symbols
->pop_scope();
3883 instructions
->push_tail(stmt
);
3885 /* if-statements do not have r-values.
3892 ast_switch_statement::hir(exec_list
*instructions
,
3893 struct _mesa_glsl_parse_state
*state
)
3897 ir_rvalue
*const test_expression
=
3898 this->test_expression
->hir(instructions
, state
);
3900 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
3902 * "The type of init-expression in a switch statement must be a
3905 if (!test_expression
->type
->is_scalar() ||
3906 !test_expression
->type
->is_integer()) {
3907 YYLTYPE loc
= this->test_expression
->get_location();
3909 _mesa_glsl_error(& loc
,
3911 "switch-statement expression must be scalar "
3915 /* Track the switch-statement nesting in a stack-like manner.
3917 struct glsl_switch_state saved
= state
->switch_state
;
3919 state
->switch_state
.is_switch_innermost
= true;
3920 state
->switch_state
.switch_nesting_ast
= this;
3921 state
->switch_state
.labels_ht
= hash_table_ctor(0, hash_table_pointer_hash
,
3922 hash_table_pointer_compare
);
3923 state
->switch_state
.previous_default
= NULL
;
3925 /* Initalize is_fallthru state to false.
3927 ir_rvalue
*const is_fallthru_val
= new (ctx
) ir_constant(false);
3928 state
->switch_state
.is_fallthru_var
=
3929 new(ctx
) ir_variable(glsl_type::bool_type
,
3930 "switch_is_fallthru_tmp",
3932 instructions
->push_tail(state
->switch_state
.is_fallthru_var
);
3934 ir_dereference_variable
*deref_is_fallthru_var
=
3935 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
3936 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_fallthru_var
,
3939 /* Initalize is_break state to false.
3941 ir_rvalue
*const is_break_val
= new (ctx
) ir_constant(false);
3942 state
->switch_state
.is_break_var
= new(ctx
) ir_variable(glsl_type::bool_type
,
3943 "switch_is_break_tmp",
3945 instructions
->push_tail(state
->switch_state
.is_break_var
);
3947 ir_dereference_variable
*deref_is_break_var
=
3948 new(ctx
) ir_dereference_variable(state
->switch_state
.is_break_var
);
3949 instructions
->push_tail(new(ctx
) ir_assignment(deref_is_break_var
,
3952 /* Cache test expression.
3954 test_to_hir(instructions
, state
);
3956 /* Emit code for body of switch stmt.
3958 body
->hir(instructions
, state
);
3960 hash_table_dtor(state
->switch_state
.labels_ht
);
3962 state
->switch_state
= saved
;
3964 /* Switch statements do not have r-values. */
3970 ast_switch_statement::test_to_hir(exec_list
*instructions
,
3971 struct _mesa_glsl_parse_state
*state
)
3975 /* Cache value of test expression. */
3976 ir_rvalue
*const test_val
=
3977 test_expression
->hir(instructions
,
3980 state
->switch_state
.test_var
= new(ctx
) ir_variable(test_val
->type
,
3983 ir_dereference_variable
*deref_test_var
=
3984 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
3986 instructions
->push_tail(state
->switch_state
.test_var
);
3987 instructions
->push_tail(new(ctx
) ir_assignment(deref_test_var
, test_val
));
3992 ast_switch_body::hir(exec_list
*instructions
,
3993 struct _mesa_glsl_parse_state
*state
)
3996 stmts
->hir(instructions
, state
);
3998 /* Switch bodies do not have r-values. */
4003 ast_case_statement_list::hir(exec_list
*instructions
,
4004 struct _mesa_glsl_parse_state
*state
)
4006 foreach_list_typed (ast_case_statement
, case_stmt
, link
, & this->cases
)
4007 case_stmt
->hir(instructions
, state
);
4009 /* Case statements do not have r-values. */
4014 ast_case_statement::hir(exec_list
*instructions
,
4015 struct _mesa_glsl_parse_state
*state
)
4017 labels
->hir(instructions
, state
);
4019 /* Conditionally set fallthru state based on break state. */
4020 ir_constant
*const false_val
= new(state
) ir_constant(false);
4021 ir_dereference_variable
*const deref_is_fallthru_var
=
4022 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4023 ir_dereference_variable
*const deref_is_break_var
=
4024 new(state
) ir_dereference_variable(state
->switch_state
.is_break_var
);
4025 ir_assignment
*const reset_fallthru_on_break
=
4026 new(state
) ir_assignment(deref_is_fallthru_var
,
4028 deref_is_break_var
);
4029 instructions
->push_tail(reset_fallthru_on_break
);
4031 /* Guard case statements depending on fallthru state. */
4032 ir_dereference_variable
*const deref_fallthru_guard
=
4033 new(state
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4034 ir_if
*const test_fallthru
= new(state
) ir_if(deref_fallthru_guard
);
4036 foreach_list_typed (ast_node
, stmt
, link
, & this->stmts
)
4037 stmt
->hir(& test_fallthru
->then_instructions
, state
);
4039 instructions
->push_tail(test_fallthru
);
4041 /* Case statements do not have r-values. */
4047 ast_case_label_list::hir(exec_list
*instructions
,
4048 struct _mesa_glsl_parse_state
*state
)
4050 foreach_list_typed (ast_case_label
, label
, link
, & this->labels
)
4051 label
->hir(instructions
, state
);
4053 /* Case labels do not have r-values. */
4058 ast_case_label::hir(exec_list
*instructions
,
4059 struct _mesa_glsl_parse_state
*state
)
4063 ir_dereference_variable
*deref_fallthru_var
=
4064 new(ctx
) ir_dereference_variable(state
->switch_state
.is_fallthru_var
);
4066 ir_rvalue
*const true_val
= new(ctx
) ir_constant(true);
4068 /* If not default case, ... */
4069 if (this->test_value
!= NULL
) {
4070 /* Conditionally set fallthru state based on
4071 * comparison of cached test expression value to case label.
4073 ir_rvalue
*const label_rval
= this->test_value
->hir(instructions
, state
);
4074 ir_constant
*label_const
= label_rval
->constant_expression_value();
4077 YYLTYPE loc
= this->test_value
->get_location();
4079 _mesa_glsl_error(& loc
, state
,
4080 "switch statement case label must be a "
4081 "constant expression");
4083 /* Stuff a dummy value in to allow processing to continue. */
4084 label_const
= new(ctx
) ir_constant(0);
4086 ast_expression
*previous_label
= (ast_expression
*)
4087 hash_table_find(state
->switch_state
.labels_ht
,
4088 (void *)(uintptr_t)label_const
->value
.u
[0]);
4090 if (previous_label
) {
4091 YYLTYPE loc
= this->test_value
->get_location();
4092 _mesa_glsl_error(& loc
, state
,
4093 "duplicate case value");
4095 loc
= previous_label
->get_location();
4096 _mesa_glsl_error(& loc
, state
,
4097 "this is the previous case label");
4099 hash_table_insert(state
->switch_state
.labels_ht
,
4101 (void *)(uintptr_t)label_const
->value
.u
[0]);
4105 ir_dereference_variable
*deref_test_var
=
4106 new(ctx
) ir_dereference_variable(state
->switch_state
.test_var
);
4108 ir_rvalue
*const test_cond
= new(ctx
) ir_expression(ir_binop_all_equal
,
4112 ir_assignment
*set_fallthru_on_test
=
4113 new(ctx
) ir_assignment(deref_fallthru_var
,
4117 instructions
->push_tail(set_fallthru_on_test
);
4118 } else { /* default case */
4119 if (state
->switch_state
.previous_default
) {
4120 YYLTYPE loc
= this->get_location();
4121 _mesa_glsl_error(& loc
, state
,
4122 "multiple default labels in one switch");
4124 loc
= state
->switch_state
.previous_default
->get_location();
4125 _mesa_glsl_error(& loc
, state
,
4126 "this is the first default label");
4128 state
->switch_state
.previous_default
= this;
4130 /* Set falltrhu state. */
4131 ir_assignment
*set_fallthru
=
4132 new(ctx
) ir_assignment(deref_fallthru_var
, true_val
);
4134 instructions
->push_tail(set_fallthru
);
4137 /* Case statements do not have r-values. */
4142 ast_iteration_statement::condition_to_hir(ir_loop
*stmt
,
4143 struct _mesa_glsl_parse_state
*state
)
4147 if (condition
!= NULL
) {
4148 ir_rvalue
*const cond
=
4149 condition
->hir(& stmt
->body_instructions
, state
);
4152 || !cond
->type
->is_boolean() || !cond
->type
->is_scalar()) {
4153 YYLTYPE loc
= condition
->get_location();
4155 _mesa_glsl_error(& loc
, state
,
4156 "loop condition must be scalar boolean");
4158 /* As the first code in the loop body, generate a block that looks
4159 * like 'if (!condition) break;' as the loop termination condition.
4161 ir_rvalue
*const not_cond
=
4162 new(ctx
) ir_expression(ir_unop_logic_not
, cond
);
4164 ir_if
*const if_stmt
= new(ctx
) ir_if(not_cond
);
4166 ir_jump
*const break_stmt
=
4167 new(ctx
) ir_loop_jump(ir_loop_jump::jump_break
);
4169 if_stmt
->then_instructions
.push_tail(break_stmt
);
4170 stmt
->body_instructions
.push_tail(if_stmt
);
4177 ast_iteration_statement::hir(exec_list
*instructions
,
4178 struct _mesa_glsl_parse_state
*state
)
4182 /* For-loops and while-loops start a new scope, but do-while loops do not.
4184 if (mode
!= ast_do_while
)
4185 state
->symbols
->push_scope();
4187 if (init_statement
!= NULL
)
4188 init_statement
->hir(instructions
, state
);
4190 ir_loop
*const stmt
= new(ctx
) ir_loop();
4191 instructions
->push_tail(stmt
);
4193 /* Track the current loop nesting. */
4194 ast_iteration_statement
*nesting_ast
= state
->loop_nesting_ast
;
4196 state
->loop_nesting_ast
= this;
4198 /* Likewise, indicate that following code is closest to a loop,
4199 * NOT closest to a switch.
4201 bool saved_is_switch_innermost
= state
->switch_state
.is_switch_innermost
;
4202 state
->switch_state
.is_switch_innermost
= false;
4204 if (mode
!= ast_do_while
)
4205 condition_to_hir(stmt
, state
);
4208 body
->hir(& stmt
->body_instructions
, state
);
4210 if (rest_expression
!= NULL
)
4211 rest_expression
->hir(& stmt
->body_instructions
, state
);
4213 if (mode
== ast_do_while
)
4214 condition_to_hir(stmt
, state
);
4216 if (mode
!= ast_do_while
)
4217 state
->symbols
->pop_scope();
4219 /* Restore previous nesting before returning. */
4220 state
->loop_nesting_ast
= nesting_ast
;
4221 state
->switch_state
.is_switch_innermost
= saved_is_switch_innermost
;
4223 /* Loops do not have r-values.
4230 * Determine if the given type is valid for establishing a default precision
4233 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
4235 * "The precision statement
4237 * precision precision-qualifier type;
4239 * can be used to establish a default precision qualifier. The type field
4240 * can be either int or float or any of the sampler types, and the
4241 * precision-qualifier can be lowp, mediump, or highp."
4243 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
4244 * qualifiers on sampler types, but this seems like an oversight (since the
4245 * intention of including these in GLSL 1.30 is to allow compatibility with ES
4246 * shaders). So we allow int, float, and all sampler types regardless of GLSL
4250 is_valid_default_precision_type(const struct glsl_type
*const type
)
4255 switch (type
->base_type
) {
4257 case GLSL_TYPE_FLOAT
:
4258 /* "int" and "float" are valid, but vectors and matrices are not. */
4259 return type
->vector_elements
== 1 && type
->matrix_columns
== 1;
4260 case GLSL_TYPE_SAMPLER
:
4269 ast_type_specifier::hir(exec_list
*instructions
,
4270 struct _mesa_glsl_parse_state
*state
)
4272 if (this->default_precision
== ast_precision_none
&& this->structure
== NULL
)
4275 YYLTYPE loc
= this->get_location();
4277 /* If this is a precision statement, check that the type to which it is
4278 * applied is either float or int.
4280 * From section 4.5.3 of the GLSL 1.30 spec:
4281 * "The precision statement
4282 * precision precision-qualifier type;
4283 * can be used to establish a default precision qualifier. The type
4284 * field can be either int or float [...]. Any other types or
4285 * qualifiers will result in an error.
4287 if (this->default_precision
!= ast_precision_none
) {
4288 if (!state
->check_precision_qualifiers_allowed(&loc
))
4291 if (this->structure
!= NULL
) {
4292 _mesa_glsl_error(&loc
, state
,
4293 "precision qualifiers do not apply to structures");
4297 if (this->is_array
) {
4298 _mesa_glsl_error(&loc
, state
,
4299 "default precision statements do not apply to "
4304 const struct glsl_type
*const type
=
4305 state
->symbols
->get_type(this->type_name
);
4306 if (!is_valid_default_precision_type(type
)) {
4307 _mesa_glsl_error(&loc
, state
,
4308 "default precision statements apply only to "
4309 "float, int, and sampler types");
4313 if (type
->base_type
== GLSL_TYPE_FLOAT
4315 && state
->target
== fragment_shader
) {
4316 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
4319 * "The fragment language has no default precision qualifier for
4320 * floating point types."
4322 * As a result, we have to track whether or not default precision has
4323 * been specified for float in GLSL ES fragment shaders.
4325 * Earlier in that same section, the spec says:
4327 * "Non-precision qualified declarations will use the precision
4328 * qualifier specified in the most recent precision statement
4329 * that is still in scope. The precision statement has the same
4330 * scoping rules as variable declarations. If it is declared
4331 * inside a compound statement, its effect stops at the end of
4332 * the innermost statement it was declared in. Precision
4333 * statements in nested scopes override precision statements in
4334 * outer scopes. Multiple precision statements for the same basic
4335 * type can appear inside the same scope, with later statements
4336 * overriding earlier statements within that scope."
4338 * Default precision specifications follow the same scope rules as
4339 * variables. So, we can track the state of the default float
4340 * precision in the symbol table, and the rules will just work. This
4341 * is a slight abuse of the symbol table, but it has the semantics
4344 ir_variable
*const junk
=
4345 new(state
) ir_variable(type
, "#default precision",
4348 state
->symbols
->add_variable(junk
);
4351 /* FINISHME: Translate precision statements into IR. */
4355 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
4356 * process_record_constructor() can do type-checking on C-style initializer
4357 * expressions of structs, but ast_struct_specifier should only be translated
4358 * to HIR if it is declaring the type of a structure.
4360 * The ->is_declaration field is false for initializers of variables
4361 * declared separately from the struct's type definition.
4363 * struct S { ... }; (is_declaration = true)
4364 * struct T { ... } t = { ... }; (is_declaration = true)
4365 * S s = { ... }; (is_declaration = false)
4367 if (this->structure
!= NULL
&& this->structure
->is_declaration
)
4368 return this->structure
->hir(instructions
, state
);
4375 * Process a structure or interface block tree into an array of structure fields
4377 * After parsing, where there are some syntax differnces, structures and
4378 * interface blocks are almost identical. They are similar enough that the
4379 * AST for each can be processed the same way into a set of
4380 * \c glsl_struct_field to describe the members.
4383 * The number of fields processed. A pointer to the array structure fields is
4384 * stored in \c *fields_ret.
4387 ast_process_structure_or_interface_block(exec_list
*instructions
,
4388 struct _mesa_glsl_parse_state
*state
,
4389 exec_list
*declarations
,
4391 glsl_struct_field
**fields_ret
,
4393 bool block_row_major
)
4395 unsigned decl_count
= 0;
4397 /* Make an initial pass over the list of fields to determine how
4398 * many there are. Each element in this list is an ast_declarator_list.
4399 * This means that we actually need to count the number of elements in the
4400 * 'declarations' list in each of the elements.
4402 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
4403 foreach_list_const (decl_ptr
, & decl_list
->declarations
) {
4408 /* Allocate storage for the fields and process the field
4409 * declarations. As the declarations are processed, try to also convert
4410 * the types to HIR. This ensures that structure definitions embedded in
4411 * other structure definitions or in interface blocks are processed.
4413 glsl_struct_field
*const fields
= ralloc_array(state
, glsl_struct_field
,
4417 foreach_list_typed (ast_declarator_list
, decl_list
, link
, declarations
) {
4418 const char *type_name
;
4420 decl_list
->type
->specifier
->hir(instructions
, state
);
4422 /* Section 10.9 of the GLSL ES 1.00 specification states that
4423 * embedded structure definitions have been removed from the language.
4425 if (state
->es_shader
&& decl_list
->type
->specifier
->structure
!= NULL
) {
4426 _mesa_glsl_error(&loc
, state
, "embedded structure definitions are "
4427 "not allowed in GLSL ES 1.00");
4430 const glsl_type
*decl_type
=
4431 decl_list
->type
->glsl_type(& type_name
, state
);
4433 foreach_list_typed (ast_declaration
, decl
, link
,
4434 &decl_list
->declarations
) {
4435 /* From the GL_ARB_uniform_buffer_object spec:
4437 * "Sampler types are not allowed inside of uniform
4438 * blocks. All other types, arrays, and structures
4439 * allowed for uniforms are allowed within a uniform
4442 * It should be impossible for decl_type to be NULL here. Cases that
4443 * might naturally lead to decl_type being NULL, especially for the
4444 * is_interface case, will have resulted in compilation having
4445 * already halted due to a syntax error.
4447 const struct glsl_type
*field_type
=
4448 decl_type
!= NULL
? decl_type
: glsl_type::error_type
;
4450 if (is_interface
&& field_type
->contains_sampler()) {
4451 YYLTYPE loc
= decl_list
->get_location();
4452 _mesa_glsl_error(&loc
, state
,
4453 "uniform in non-default uniform block contains sampler");
4456 const struct ast_type_qualifier
*const qual
=
4457 & decl_list
->type
->qualifier
;
4458 if (qual
->flags
.q
.std140
||
4459 qual
->flags
.q
.packed
||
4460 qual
->flags
.q
.shared
) {
4461 _mesa_glsl_error(&loc
, state
,
4462 "uniform block layout qualifiers std140, packed, and "
4463 "shared can only be applied to uniform blocks, not "
4467 if (decl
->is_array
) {
4468 field_type
= process_array_type(&loc
, decl_type
, decl
->array_size
,
4471 fields
[i
].type
= field_type
;
4472 fields
[i
].name
= decl
->identifier
;
4474 if (qual
->flags
.q
.row_major
|| qual
->flags
.q
.column_major
) {
4475 if (!qual
->flags
.q
.uniform
) {
4476 _mesa_glsl_error(&loc
, state
,
4477 "row_major and column_major can only be "
4478 "applied to uniform interface blocks");
4479 } else if (!field_type
->is_matrix() && !field_type
->is_record()) {
4480 _mesa_glsl_error(&loc
, state
,
4481 "uniform block layout qualifiers row_major and "
4482 "column_major can only be applied to matrix and "
4485 validate_matrix_layout_for_type(state
, &loc
, field_type
);
4488 if (qual
->flags
.q
.uniform
&& qual
->has_interpolation()) {
4489 _mesa_glsl_error(&loc
, state
,
4490 "interpolation qualifiers cannot be used "
4491 "with uniform interface blocks");
4494 if (field_type
->is_matrix() ||
4495 (field_type
->is_array() && field_type
->fields
.array
->is_matrix())) {
4496 fields
[i
].row_major
= block_row_major
;
4497 if (qual
->flags
.q
.row_major
)
4498 fields
[i
].row_major
= true;
4499 else if (qual
->flags
.q
.column_major
)
4500 fields
[i
].row_major
= false;
4507 assert(i
== decl_count
);
4509 *fields_ret
= fields
;
4515 ast_struct_specifier::hir(exec_list
*instructions
,
4516 struct _mesa_glsl_parse_state
*state
)
4518 YYLTYPE loc
= this->get_location();
4520 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
4522 * "Anonymous structures are not supported; so embedded structures must
4523 * have a declarator. A name given to an embedded struct is scoped at
4524 * the same level as the struct it is embedded in."
4526 * The same section of the GLSL 1.20 spec says:
4528 * "Anonymous structures are not supported. Embedded structures are not
4531 * struct S { float f; };
4533 * S; // Error: anonymous structures disallowed
4534 * struct { ... }; // Error: embedded structures disallowed
4535 * S s; // Okay: nested structures with name are allowed
4538 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
4539 * we allow embedded structures in 1.10 only.
4541 if (state
->language_version
!= 110 && state
->struct_specifier_depth
!= 0)
4542 _mesa_glsl_error(&loc
, state
,
4543 "embedded structure declartions are not allowed");
4545 state
->struct_specifier_depth
++;
4547 glsl_struct_field
*fields
;
4548 unsigned decl_count
=
4549 ast_process_structure_or_interface_block(instructions
,
4551 &this->declarations
,
4557 const glsl_type
*t
=
4558 glsl_type::get_record_instance(fields
, decl_count
, this->name
);
4560 if (!state
->symbols
->add_type(name
, t
)) {
4561 _mesa_glsl_error(& loc
, state
, "struct `%s' previously defined", name
);
4563 const glsl_type
**s
= reralloc(state
, state
->user_structures
,
4565 state
->num_user_structures
+ 1);
4567 s
[state
->num_user_structures
] = t
;
4568 state
->user_structures
= s
;
4569 state
->num_user_structures
++;
4573 state
->struct_specifier_depth
--;
4575 /* Structure type definitions do not have r-values.
4581 ast_interface_block::hir(exec_list
*instructions
,
4582 struct _mesa_glsl_parse_state
*state
)
4584 YYLTYPE loc
= this->get_location();
4586 /* The ast_interface_block has a list of ast_declarator_lists. We
4587 * need to turn those into ir_variables with an association
4588 * with this uniform block.
4590 enum glsl_interface_packing packing
;
4591 if (this->layout
.flags
.q
.shared
) {
4592 packing
= GLSL_INTERFACE_PACKING_SHARED
;
4593 } else if (this->layout
.flags
.q
.packed
) {
4594 packing
= GLSL_INTERFACE_PACKING_PACKED
;
4596 /* The default layout is std140.
4598 packing
= GLSL_INTERFACE_PACKING_STD140
;
4601 bool block_row_major
= this->layout
.flags
.q
.row_major
;
4602 exec_list declared_variables
;
4603 glsl_struct_field
*fields
;
4604 unsigned int num_variables
=
4605 ast_process_structure_or_interface_block(&declared_variables
,
4607 &this->declarations
,
4613 ir_variable_mode var_mode
;
4614 const char *iface_type_name
;
4615 if (this->layout
.flags
.q
.in
) {
4616 var_mode
= ir_var_shader_in
;
4617 iface_type_name
= "in";
4618 } else if (this->layout
.flags
.q
.out
) {
4619 var_mode
= ir_var_shader_out
;
4620 iface_type_name
= "out";
4621 } else if (this->layout
.flags
.q
.uniform
) {
4622 var_mode
= ir_var_uniform
;
4623 iface_type_name
= "uniform";
4625 var_mode
= ir_var_auto
;
4626 iface_type_name
= "UNKNOWN";
4627 assert(!"interface block layout qualifier not found!");
4630 const glsl_type
*block_type
=
4631 glsl_type::get_interface_instance(fields
,
4636 if (!state
->symbols
->add_interface(block_type
->name
, block_type
, var_mode
)) {
4637 YYLTYPE loc
= this->get_location();
4638 _mesa_glsl_error(&loc
, state
, "interface block `%s' with type `%s' "
4639 "already taken in the current scope",
4640 this->block_name
, iface_type_name
);
4643 /* Since interface blocks cannot contain statements, it should be
4644 * impossible for the block to generate any instructions.
4646 assert(declared_variables
.is_empty());
4648 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
4650 * Geometry shader input variables get the per-vertex values written
4651 * out by vertex shader output variables of the same names. Since a
4652 * geometry shader operates on a set of vertices, each input varying
4653 * variable (or input block, see interface blocks below) needs to be
4654 * declared as an array.
4656 if (state
->target
== geometry_shader
&& !this->is_array
&&
4657 var_mode
== ir_var_shader_in
) {
4658 _mesa_glsl_error(&loc
, state
, "geometry shader inputs must be arrays");
4661 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
4664 * "If an instance name (instance-name) is used, then it puts all the
4665 * members inside a scope within its own name space, accessed with the
4666 * field selector ( . ) operator (analogously to structures)."
4668 if (this->instance_name
) {
4671 if (this->is_array
) {
4672 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
4674 * For uniform blocks declared an array, each individual array
4675 * element corresponds to a separate buffer object backing one
4676 * instance of the block. As the array size indicates the number
4677 * of buffer objects needed, uniform block array declarations
4678 * must specify an array size.
4680 * And a few paragraphs later:
4682 * Geometry shader input blocks must be declared as arrays and
4683 * follow the array declaration and linking rules for all
4684 * geometry shader inputs. All other input and output block
4685 * arrays must specify an array size.
4687 * The upshot of this is that the only circumstance where an
4688 * interface array size *doesn't* need to be specified is on a
4689 * geometry shader input.
4691 if (this->array_size
== NULL
&&
4692 (state
->target
!= geometry_shader
|| !this->layout
.flags
.q
.in
)) {
4693 _mesa_glsl_error(&loc
, state
,
4694 "only geometry shader inputs may be unsized "
4695 "instance block arrays");
4699 const glsl_type
*block_array_type
=
4700 process_array_type(&loc
, block_type
, this->array_size
, state
);
4702 var
= new(state
) ir_variable(block_array_type
,
4703 this->instance_name
,
4706 var
= new(state
) ir_variable(block_type
,
4707 this->instance_name
,
4711 var
->interface_type
= block_type
;
4712 if (state
->target
== geometry_shader
&& var_mode
== ir_var_shader_in
)
4713 handle_geometry_shader_input_decl(state
, loc
, var
);
4714 state
->symbols
->add_variable(var
);
4715 instructions
->push_tail(var
);
4717 /* In order to have an array size, the block must also be declared with
4720 assert(!this->is_array
);
4722 for (unsigned i
= 0; i
< num_variables
; i
++) {
4724 new(state
) ir_variable(fields
[i
].type
,
4725 ralloc_strdup(state
, fields
[i
].name
),
4727 var
->interface_type
= block_type
;
4729 /* Propagate the "binding" keyword into this UBO's fields;
4730 * the UBO declaration itself doesn't get an ir_variable unless it
4731 * has an instance name. This is ugly.
4733 var
->explicit_binding
= this->layout
.flags
.q
.explicit_binding
;
4734 var
->binding
= this->layout
.binding
;
4736 state
->symbols
->add_variable(var
);
4737 instructions
->push_tail(var
);
4746 ast_gs_input_layout::hir(exec_list
*instructions
,
4747 struct _mesa_glsl_parse_state
*state
)
4749 YYLTYPE loc
= this->get_location();
4751 /* If any geometry input layout declaration preceded this one, make sure it
4752 * was consistent with this one.
4754 if (state
->gs_input_prim_type_specified
&&
4755 state
->gs_input_prim_type
!= this->prim_type
) {
4756 _mesa_glsl_error(&loc
, state
,
4757 "geometry shader input layout does not match"
4758 " previous declaration");
4762 /* If any shader inputs occurred before this declaration and specified an
4763 * array size, make sure the size they specified is consistent with the
4766 unsigned num_vertices
= vertices_per_prim(this->prim_type
);
4767 if (state
->gs_input_size
!= 0 && state
->gs_input_size
!= num_vertices
) {
4768 _mesa_glsl_error(&loc
, state
,
4769 "this geometry shader input layout implies %u vertices"
4770 " per primitive, but a previous input is declared"
4771 " with size %u", num_vertices
, state
->gs_input_size
);
4775 state
->gs_input_prim_type_specified
= true;
4776 state
->gs_input_prim_type
= this->prim_type
;
4778 /* If any shader inputs occurred before this declaration and did not
4779 * specify an array size, their size is determined now.
4781 foreach_list (node
, instructions
) {
4782 ir_variable
*var
= ((ir_instruction
*) node
)->as_variable();
4783 if (var
== NULL
|| var
->mode
!= ir_var_shader_in
)
4786 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
4789 if (!var
->type
->is_array())
4792 if (var
->type
->length
== 0) {
4793 if (var
->max_array_access
>= num_vertices
) {
4794 _mesa_glsl_error(&loc
, state
,
4795 "this geometry shader input layout implies %u"
4796 " vertices, but an access to element %u of input"
4797 " `%s' already exists", num_vertices
,
4798 var
->max_array_access
, var
->name
);
4800 var
->type
= glsl_type::get_array_instance(var
->type
->fields
.array
,
4811 detect_conflicting_assignments(struct _mesa_glsl_parse_state
*state
,
4812 exec_list
*instructions
)
4814 bool gl_FragColor_assigned
= false;
4815 bool gl_FragData_assigned
= false;
4816 bool user_defined_fs_output_assigned
= false;
4817 ir_variable
*user_defined_fs_output
= NULL
;
4819 /* It would be nice to have proper location information. */
4821 memset(&loc
, 0, sizeof(loc
));
4823 foreach_list(node
, instructions
) {
4824 ir_variable
*var
= ((ir_instruction
*)node
)->as_variable();
4826 if (!var
|| !var
->assigned
)
4829 if (strcmp(var
->name
, "gl_FragColor") == 0)
4830 gl_FragColor_assigned
= true;
4831 else if (strcmp(var
->name
, "gl_FragData") == 0)
4832 gl_FragData_assigned
= true;
4833 else if (strncmp(var
->name
, "gl_", 3) != 0) {
4834 if (state
->target
== fragment_shader
&&
4835 var
->mode
== ir_var_shader_out
) {
4836 user_defined_fs_output_assigned
= true;
4837 user_defined_fs_output
= var
;
4842 /* From the GLSL 1.30 spec:
4844 * "If a shader statically assigns a value to gl_FragColor, it
4845 * may not assign a value to any element of gl_FragData. If a
4846 * shader statically writes a value to any element of
4847 * gl_FragData, it may not assign a value to
4848 * gl_FragColor. That is, a shader may assign values to either
4849 * gl_FragColor or gl_FragData, but not both. Multiple shaders
4850 * linked together must also consistently write just one of
4851 * these variables. Similarly, if user declared output
4852 * variables are in use (statically assigned to), then the
4853 * built-in variables gl_FragColor and gl_FragData may not be
4854 * assigned to. These incorrect usages all generate compile
4857 if (gl_FragColor_assigned
&& gl_FragData_assigned
) {
4858 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
4859 "`gl_FragColor' and `gl_FragData'");
4860 } else if (gl_FragColor_assigned
&& user_defined_fs_output_assigned
) {
4861 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
4862 "`gl_FragColor' and `%s'",
4863 user_defined_fs_output
->name
);
4864 } else if (gl_FragData_assigned
&& user_defined_fs_output_assigned
) {
4865 _mesa_glsl_error(&loc
, state
, "fragment shader writes to both "
4866 "`gl_FragData' and `%s'",
4867 user_defined_fs_output
->name
);