glsl: Implement parser support for atomic counters.
[mesa.git] / src / glsl / ast_to_hir.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file ast_to_hir.c
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
27 *
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
30 *
31 * * Symbol table management
32 * * Type checking
33 * * Function binding
34 *
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.
43 *
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.
47 *
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
50 */
51
52 #include "main/core.h" /* for struct gl_extensions */
53 #include "glsl_symbol_table.h"
54 #include "glsl_parser_extras.h"
55 #include "ast.h"
56 #include "glsl_types.h"
57 #include "program/hash_table.h"
58 #include "ir.h"
59
60 static void
61 detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
62 exec_list *instructions);
63 static void
64 remove_per_vertex_blocks(exec_list *instructions,
65 _mesa_glsl_parse_state *state, ir_variable_mode mode);
66
67
68 void
69 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
70 {
71 _mesa_glsl_initialize_variables(instructions, state);
72
73 state->symbols->separate_function_namespace = state->language_version == 110;
74
75 state->current_function = NULL;
76
77 state->toplevel_ir = instructions;
78
79 state->gs_input_prim_type_specified = false;
80
81 /* Section 4.2 of the GLSL 1.20 specification states:
82 * "The built-in functions are scoped in a scope outside the global scope
83 * users declare global variables in. That is, a shader's global scope,
84 * available for user-defined functions and global variables, is nested
85 * inside the scope containing the built-in functions."
86 *
87 * Since built-in functions like ftransform() access built-in variables,
88 * it follows that those must be in the outer scope as well.
89 *
90 * We push scope here to create this nesting effect...but don't pop.
91 * This way, a shader's globals are still in the symbol table for use
92 * by the linker.
93 */
94 state->symbols->push_scope();
95
96 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
97 ast->hir(instructions, state);
98
99 detect_recursion_unlinked(state, instructions);
100 detect_conflicting_assignments(state, instructions);
101
102 state->toplevel_ir = NULL;
103
104 /* Move all of the variable declarations to the front of the IR list, and
105 * reverse the order. This has the (intended!) side effect that vertex
106 * shader inputs and fragment shader outputs will appear in the IR in the
107 * same order that they appeared in the shader code. This results in the
108 * locations being assigned in the declared order. Many (arguably buggy)
109 * applications depend on this behavior, and it matches what nearly all
110 * other drivers do.
111 */
112 foreach_list_safe(node, instructions) {
113 ir_variable *const var = ((ir_instruction *) node)->as_variable();
114
115 if (var == NULL)
116 continue;
117
118 var->remove();
119 instructions->push_head(var);
120 }
121
122 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
123 *
124 * If multiple shaders using members of a built-in block belonging to
125 * the same interface are linked together in the same program, they
126 * must all redeclare the built-in block in the same way, as described
127 * in section 4.3.7 "Interface Blocks" for interface block matching, or
128 * a link error will result.
129 *
130 * The phrase "using members of a built-in block" implies that if two
131 * shaders are linked together and one of them *does not use* any members
132 * of the built-in block, then that shader does not need to have a matching
133 * redeclaration of the built-in block.
134 *
135 * This appears to be a clarification to the behaviour established for
136 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
137 * version.
138 *
139 * The definition of "interface" in section 4.3.7 that applies here is as
140 * follows:
141 *
142 * The boundary between adjacent programmable pipeline stages: This
143 * spans all the outputs in all compilation units of the first stage
144 * and all the inputs in all compilation units of the second stage.
145 *
146 * Therefore this rule applies to both inter- and intra-stage linking.
147 *
148 * The easiest way to implement this is to check whether the shader uses
149 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
150 * remove all the relevant variable declaration from the IR, so that the
151 * linker won't see them and complain about mismatches.
152 */
153 remove_per_vertex_blocks(instructions, state, ir_var_shader_in);
154 remove_per_vertex_blocks(instructions, state, ir_var_shader_out);
155 }
156
157
158 /**
159 * If a conversion is available, convert one operand to a different type
160 *
161 * The \c from \c ir_rvalue is converted "in place".
162 *
163 * \param to Type that the operand it to be converted to
164 * \param from Operand that is being converted
165 * \param state GLSL compiler state
166 *
167 * \return
168 * If a conversion is possible (or unnecessary), \c true is returned.
169 * Otherwise \c false is returned.
170 */
171 bool
172 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
173 struct _mesa_glsl_parse_state *state)
174 {
175 void *ctx = state;
176 if (to->base_type == from->type->base_type)
177 return true;
178
179 /* This conversion was added in GLSL 1.20. If the compilation mode is
180 * GLSL 1.10, the conversion is skipped.
181 */
182 if (!state->is_version(120, 0))
183 return false;
184
185 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
186 *
187 * "There are no implicit array or structure conversions. For
188 * example, an array of int cannot be implicitly converted to an
189 * array of float. There are no implicit conversions between
190 * signed and unsigned integers."
191 */
192 /* FINISHME: The above comment is partially a lie. There is int/uint
193 * FINISHME: conversion for immediate constants.
194 */
195 if (!to->is_float() || !from->type->is_numeric())
196 return false;
197
198 /* Convert to a floating point type with the same number of components
199 * as the original type - i.e. int to float, not int to vec4.
200 */
201 to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
202 from->type->matrix_columns);
203
204 switch (from->type->base_type) {
205 case GLSL_TYPE_INT:
206 from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
207 break;
208 case GLSL_TYPE_UINT:
209 from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
210 break;
211 case GLSL_TYPE_BOOL:
212 from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
213 break;
214 default:
215 assert(0);
216 }
217
218 return true;
219 }
220
221
222 static const struct glsl_type *
223 arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
224 bool multiply,
225 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
226 {
227 const glsl_type *type_a = value_a->type;
228 const glsl_type *type_b = value_b->type;
229
230 /* From GLSL 1.50 spec, page 56:
231 *
232 * "The arithmetic binary operators add (+), subtract (-),
233 * multiply (*), and divide (/) operate on integer and
234 * floating-point scalars, vectors, and matrices."
235 */
236 if (!type_a->is_numeric() || !type_b->is_numeric()) {
237 _mesa_glsl_error(loc, state,
238 "operands to arithmetic operators must be numeric");
239 return glsl_type::error_type;
240 }
241
242
243 /* "If one operand is floating-point based and the other is
244 * not, then the conversions from Section 4.1.10 "Implicit
245 * Conversions" are applied to the non-floating-point-based operand."
246 */
247 if (!apply_implicit_conversion(type_a, value_b, state)
248 && !apply_implicit_conversion(type_b, value_a, state)) {
249 _mesa_glsl_error(loc, state,
250 "could not implicitly convert operands to "
251 "arithmetic operator");
252 return glsl_type::error_type;
253 }
254 type_a = value_a->type;
255 type_b = value_b->type;
256
257 /* "If the operands are integer types, they must both be signed or
258 * both be unsigned."
259 *
260 * From this rule and the preceeding conversion it can be inferred that
261 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
262 * The is_numeric check above already filtered out the case where either
263 * type is not one of these, so now the base types need only be tested for
264 * equality.
265 */
266 if (type_a->base_type != type_b->base_type) {
267 _mesa_glsl_error(loc, state,
268 "base type mismatch for arithmetic operator");
269 return glsl_type::error_type;
270 }
271
272 /* "All arithmetic binary operators result in the same fundamental type
273 * (signed integer, unsigned integer, or floating-point) as the
274 * operands they operate on, after operand type conversion. After
275 * conversion, the following cases are valid
276 *
277 * * The two operands are scalars. In this case the operation is
278 * applied, resulting in a scalar."
279 */
280 if (type_a->is_scalar() && type_b->is_scalar())
281 return type_a;
282
283 /* "* One operand is a scalar, and the other is a vector or matrix.
284 * In this case, the scalar operation is applied independently to each
285 * component of the vector or matrix, resulting in the same size
286 * vector or matrix."
287 */
288 if (type_a->is_scalar()) {
289 if (!type_b->is_scalar())
290 return type_b;
291 } else if (type_b->is_scalar()) {
292 return type_a;
293 }
294
295 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
296 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
297 * handled.
298 */
299 assert(!type_a->is_scalar());
300 assert(!type_b->is_scalar());
301
302 /* "* The two operands are vectors of the same size. In this case, the
303 * operation is done component-wise resulting in the same size
304 * vector."
305 */
306 if (type_a->is_vector() && type_b->is_vector()) {
307 if (type_a == type_b) {
308 return type_a;
309 } else {
310 _mesa_glsl_error(loc, state,
311 "vector size mismatch for arithmetic operator");
312 return glsl_type::error_type;
313 }
314 }
315
316 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
317 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
318 * <vector, vector> have been handled. At least one of the operands must
319 * be matrix. Further, since there are no integer matrix types, the base
320 * type of both operands must be float.
321 */
322 assert(type_a->is_matrix() || type_b->is_matrix());
323 assert(type_a->base_type == GLSL_TYPE_FLOAT);
324 assert(type_b->base_type == GLSL_TYPE_FLOAT);
325
326 /* "* The operator is add (+), subtract (-), or divide (/), and the
327 * operands are matrices with the same number of rows and the same
328 * number of columns. In this case, the operation is done component-
329 * wise resulting in the same size matrix."
330 * * The operator is multiply (*), where both operands are matrices or
331 * one operand is a vector and the other a matrix. A right vector
332 * operand is treated as a column vector and a left vector operand as a
333 * row vector. In all these cases, it is required that the number of
334 * columns of the left operand is equal to the number of rows of the
335 * right operand. Then, the multiply (*) operation does a linear
336 * algebraic multiply, yielding an object that has the same number of
337 * rows as the left operand and the same number of columns as the right
338 * operand. Section 5.10 "Vector and Matrix Operations" explains in
339 * more detail how vectors and matrices are operated on."
340 */
341 if (! multiply) {
342 if (type_a == type_b)
343 return type_a;
344 } else {
345 if (type_a->is_matrix() && type_b->is_matrix()) {
346 /* Matrix multiply. The columns of A must match the rows of B. Given
347 * the other previously tested constraints, this means the vector type
348 * of a row from A must be the same as the vector type of a column from
349 * B.
350 */
351 if (type_a->row_type() == type_b->column_type()) {
352 /* The resulting matrix has the number of columns of matrix B and
353 * the number of rows of matrix A. We get the row count of A by
354 * looking at the size of a vector that makes up a column. The
355 * transpose (size of a row) is done for B.
356 */
357 const glsl_type *const type =
358 glsl_type::get_instance(type_a->base_type,
359 type_a->column_type()->vector_elements,
360 type_b->row_type()->vector_elements);
361 assert(type != glsl_type::error_type);
362
363 return type;
364 }
365 } else if (type_a->is_matrix()) {
366 /* A is a matrix and B is a column vector. Columns of A must match
367 * rows of B. Given the other previously tested constraints, this
368 * means the vector type of a row from A must be the same as the
369 * vector the type of B.
370 */
371 if (type_a->row_type() == type_b) {
372 /* The resulting vector has a number of elements equal to
373 * the number of rows of matrix A. */
374 const glsl_type *const type =
375 glsl_type::get_instance(type_a->base_type,
376 type_a->column_type()->vector_elements,
377 1);
378 assert(type != glsl_type::error_type);
379
380 return type;
381 }
382 } else {
383 assert(type_b->is_matrix());
384
385 /* A is a row vector and B is a matrix. Columns of A must match rows
386 * of B. Given the other previously tested constraints, this means
387 * the type of A must be the same as the vector type of a column from
388 * B.
389 */
390 if (type_a == type_b->column_type()) {
391 /* The resulting vector has a number of elements equal to
392 * the number of columns of matrix B. */
393 const glsl_type *const type =
394 glsl_type::get_instance(type_a->base_type,
395 type_b->row_type()->vector_elements,
396 1);
397 assert(type != glsl_type::error_type);
398
399 return type;
400 }
401 }
402
403 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
404 return glsl_type::error_type;
405 }
406
407
408 /* "All other cases are illegal."
409 */
410 _mesa_glsl_error(loc, state, "type mismatch");
411 return glsl_type::error_type;
412 }
413
414
415 static const struct glsl_type *
416 unary_arithmetic_result_type(const struct glsl_type *type,
417 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
418 {
419 /* From GLSL 1.50 spec, page 57:
420 *
421 * "The arithmetic unary operators negate (-), post- and pre-increment
422 * and decrement (-- and ++) operate on integer or floating-point
423 * values (including vectors and matrices). All unary operators work
424 * component-wise on their operands. These result with the same type
425 * they operated on."
426 */
427 if (!type->is_numeric()) {
428 _mesa_glsl_error(loc, state,
429 "operands to arithmetic operators must be numeric");
430 return glsl_type::error_type;
431 }
432
433 return type;
434 }
435
436 /**
437 * \brief Return the result type of a bit-logic operation.
438 *
439 * If the given types to the bit-logic operator are invalid, return
440 * glsl_type::error_type.
441 *
442 * \param type_a Type of LHS of bit-logic op
443 * \param type_b Type of RHS of bit-logic op
444 */
445 static const struct glsl_type *
446 bit_logic_result_type(const struct glsl_type *type_a,
447 const struct glsl_type *type_b,
448 ast_operators op,
449 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
450 {
451 if (!state->check_bitwise_operations_allowed(loc)) {
452 return glsl_type::error_type;
453 }
454
455 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
456 *
457 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
458 * (|). The operands must be of type signed or unsigned integers or
459 * integer vectors."
460 */
461 if (!type_a->is_integer()) {
462 _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
463 ast_expression::operator_string(op));
464 return glsl_type::error_type;
465 }
466 if (!type_b->is_integer()) {
467 _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
468 ast_expression::operator_string(op));
469 return glsl_type::error_type;
470 }
471
472 /* "The fundamental types of the operands (signed or unsigned) must
473 * match,"
474 */
475 if (type_a->base_type != type_b->base_type) {
476 _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
477 "base type", ast_expression::operator_string(op));
478 return glsl_type::error_type;
479 }
480
481 /* "The operands cannot be vectors of differing size." */
482 if (type_a->is_vector() &&
483 type_b->is_vector() &&
484 type_a->vector_elements != type_b->vector_elements) {
485 _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
486 "different sizes", ast_expression::operator_string(op));
487 return glsl_type::error_type;
488 }
489
490 /* "If one operand is a scalar and the other a vector, the scalar is
491 * applied component-wise to the vector, resulting in the same type as
492 * the vector. The fundamental types of the operands [...] will be the
493 * resulting fundamental type."
494 */
495 if (type_a->is_scalar())
496 return type_b;
497 else
498 return type_a;
499 }
500
501 static const struct glsl_type *
502 modulus_result_type(const struct glsl_type *type_a,
503 const struct glsl_type *type_b,
504 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
505 {
506 if (!state->check_version(130, 300, loc, "operator '%%' is reserved")) {
507 return glsl_type::error_type;
508 }
509
510 /* From GLSL 1.50 spec, page 56:
511 * "The operator modulus (%) operates on signed or unsigned integers or
512 * integer vectors. The operand types must both be signed or both be
513 * unsigned."
514 */
515 if (!type_a->is_integer()) {
516 _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer");
517 return glsl_type::error_type;
518 }
519 if (!type_b->is_integer()) {
520 _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer");
521 return glsl_type::error_type;
522 }
523 if (type_a->base_type != type_b->base_type) {
524 _mesa_glsl_error(loc, state,
525 "operands of %% must have the same base type");
526 return glsl_type::error_type;
527 }
528
529 /* "The operands cannot be vectors of differing size. If one operand is
530 * a scalar and the other vector, then the scalar is applied component-
531 * wise to the vector, resulting in the same type as the vector. If both
532 * are vectors of the same size, the result is computed component-wise."
533 */
534 if (type_a->is_vector()) {
535 if (!type_b->is_vector()
536 || (type_a->vector_elements == type_b->vector_elements))
537 return type_a;
538 } else
539 return type_b;
540
541 /* "The operator modulus (%) is not defined for any other data types
542 * (non-integer types)."
543 */
544 _mesa_glsl_error(loc, state, "type mismatch");
545 return glsl_type::error_type;
546 }
547
548
549 static const struct glsl_type *
550 relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
551 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
552 {
553 const glsl_type *type_a = value_a->type;
554 const glsl_type *type_b = value_b->type;
555
556 /* From GLSL 1.50 spec, page 56:
557 * "The relational operators greater than (>), less than (<), greater
558 * than or equal (>=), and less than or equal (<=) operate only on
559 * scalar integer and scalar floating-point expressions."
560 */
561 if (!type_a->is_numeric()
562 || !type_b->is_numeric()
563 || !type_a->is_scalar()
564 || !type_b->is_scalar()) {
565 _mesa_glsl_error(loc, state,
566 "operands to relational operators must be scalar and "
567 "numeric");
568 return glsl_type::error_type;
569 }
570
571 /* "Either the operands' types must match, or the conversions from
572 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
573 * operand, after which the types must match."
574 */
575 if (!apply_implicit_conversion(type_a, value_b, state)
576 && !apply_implicit_conversion(type_b, value_a, state)) {
577 _mesa_glsl_error(loc, state,
578 "could not implicitly convert operands to "
579 "relational operator");
580 return glsl_type::error_type;
581 }
582 type_a = value_a->type;
583 type_b = value_b->type;
584
585 if (type_a->base_type != type_b->base_type) {
586 _mesa_glsl_error(loc, state, "base type mismatch");
587 return glsl_type::error_type;
588 }
589
590 /* "The result is scalar Boolean."
591 */
592 return glsl_type::bool_type;
593 }
594
595 /**
596 * \brief Return the result type of a bit-shift operation.
597 *
598 * If the given types to the bit-shift operator are invalid, return
599 * glsl_type::error_type.
600 *
601 * \param type_a Type of LHS of bit-shift op
602 * \param type_b Type of RHS of bit-shift op
603 */
604 static const struct glsl_type *
605 shift_result_type(const struct glsl_type *type_a,
606 const struct glsl_type *type_b,
607 ast_operators op,
608 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
609 {
610 if (!state->check_bitwise_operations_allowed(loc)) {
611 return glsl_type::error_type;
612 }
613
614 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
615 *
616 * "The shift operators (<<) and (>>). For both operators, the operands
617 * must be signed or unsigned integers or integer vectors. One operand
618 * can be signed while the other is unsigned."
619 */
620 if (!type_a->is_integer()) {
621 _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
622 "integer vector", ast_expression::operator_string(op));
623 return glsl_type::error_type;
624
625 }
626 if (!type_b->is_integer()) {
627 _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
628 "integer vector", ast_expression::operator_string(op));
629 return glsl_type::error_type;
630 }
631
632 /* "If the first operand is a scalar, the second operand has to be
633 * a scalar as well."
634 */
635 if (type_a->is_scalar() && !type_b->is_scalar()) {
636 _mesa_glsl_error(loc, state, "if the first operand of %s is scalar, the "
637 "second must be scalar as well",
638 ast_expression::operator_string(op));
639 return glsl_type::error_type;
640 }
641
642 /* If both operands are vectors, check that they have same number of
643 * elements.
644 */
645 if (type_a->is_vector() &&
646 type_b->is_vector() &&
647 type_a->vector_elements != type_b->vector_elements) {
648 _mesa_glsl_error(loc, state, "vector operands to operator %s must "
649 "have same number of elements",
650 ast_expression::operator_string(op));
651 return glsl_type::error_type;
652 }
653
654 /* "In all cases, the resulting type will be the same type as the left
655 * operand."
656 */
657 return type_a;
658 }
659
660 /**
661 * Validates that a value can be assigned to a location with a specified type
662 *
663 * Validates that \c rhs can be assigned to some location. If the types are
664 * not an exact match but an automatic conversion is possible, \c rhs will be
665 * converted.
666 *
667 * \return
668 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
669 * Otherwise the actual RHS to be assigned will be returned. This may be
670 * \c rhs, or it may be \c rhs after some type conversion.
671 *
672 * \note
673 * In addition to being used for assignments, this function is used to
674 * type-check return values.
675 */
676 ir_rvalue *
677 validate_assignment(struct _mesa_glsl_parse_state *state,
678 YYLTYPE loc, const glsl_type *lhs_type,
679 ir_rvalue *rhs, bool is_initializer)
680 {
681 /* If there is already some error in the RHS, just return it. Anything
682 * else will lead to an avalanche of error message back to the user.
683 */
684 if (rhs->type->is_error())
685 return rhs;
686
687 /* If the types are identical, the assignment can trivially proceed.
688 */
689 if (rhs->type == lhs_type)
690 return rhs;
691
692 /* If the array element types are the same and the LHS is unsized,
693 * the assignment is okay for initializers embedded in variable
694 * declarations.
695 *
696 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
697 * is handled by ir_dereference::is_lvalue.
698 */
699 if (is_initializer && lhs_type->is_unsized_array() && rhs->type->is_array()
700 && (lhs_type->element_type() == rhs->type->element_type())) {
701 return rhs;
702 }
703
704 /* Check for implicit conversion in GLSL 1.20 */
705 if (apply_implicit_conversion(lhs_type, rhs, state)) {
706 if (rhs->type == lhs_type)
707 return rhs;
708 }
709
710 _mesa_glsl_error(&loc, state,
711 "%s of type %s cannot be assigned to "
712 "variable of type %s",
713 is_initializer ? "initializer" : "value",
714 rhs->type->name, lhs_type->name);
715
716 return NULL;
717 }
718
719 static void
720 mark_whole_array_access(ir_rvalue *access)
721 {
722 ir_dereference_variable *deref = access->as_dereference_variable();
723
724 if (deref && deref->var) {
725 deref->var->max_array_access = deref->type->length - 1;
726 }
727 }
728
729 ir_rvalue *
730 do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
731 const char *non_lvalue_description,
732 ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer,
733 YYLTYPE lhs_loc)
734 {
735 void *ctx = state;
736 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
737
738 /* If the assignment LHS comes back as an ir_binop_vector_extract
739 * expression, move it to the RHS as an ir_triop_vector_insert.
740 */
741 if (lhs->ir_type == ir_type_expression) {
742 ir_expression *const expr = lhs->as_expression();
743
744 if (unlikely(expr->operation == ir_binop_vector_extract)) {
745 ir_rvalue *new_rhs =
746 validate_assignment(state, lhs_loc, lhs->type,
747 rhs, is_initializer);
748
749 if (new_rhs == NULL) {
750 return lhs;
751 } else {
752 rhs = new(ctx) ir_expression(ir_triop_vector_insert,
753 expr->operands[0]->type,
754 expr->operands[0],
755 new_rhs,
756 expr->operands[1]);
757 lhs = expr->operands[0]->clone(ctx, NULL);
758 }
759 }
760 }
761
762 ir_variable *lhs_var = lhs->variable_referenced();
763 if (lhs_var)
764 lhs_var->assigned = true;
765
766 if (!error_emitted) {
767 if (non_lvalue_description != NULL) {
768 _mesa_glsl_error(&lhs_loc, state,
769 "assignment to %s",
770 non_lvalue_description);
771 error_emitted = true;
772 } else if (lhs->variable_referenced() != NULL
773 && lhs->variable_referenced()->read_only) {
774 _mesa_glsl_error(&lhs_loc, state,
775 "assignment to read-only variable '%s'",
776 lhs->variable_referenced()->name);
777 error_emitted = true;
778
779 } else if (lhs->type->is_array() &&
780 !state->check_version(120, 300, &lhs_loc,
781 "whole array assignment forbidden")) {
782 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
783 *
784 * "Other binary or unary expressions, non-dereferenced
785 * arrays, function names, swizzles with repeated fields,
786 * and constants cannot be l-values."
787 *
788 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
789 */
790 error_emitted = true;
791 } else if (!lhs->is_lvalue()) {
792 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
793 error_emitted = true;
794 }
795 }
796
797 ir_rvalue *new_rhs =
798 validate_assignment(state, lhs_loc, lhs->type, rhs, is_initializer);
799 if (new_rhs != NULL) {
800 rhs = new_rhs;
801
802 /* If the LHS array was not declared with a size, it takes it size from
803 * the RHS. If the LHS is an l-value and a whole array, it must be a
804 * dereference of a variable. Any other case would require that the LHS
805 * is either not an l-value or not a whole array.
806 */
807 if (lhs->type->is_unsized_array()) {
808 ir_dereference *const d = lhs->as_dereference();
809
810 assert(d != NULL);
811
812 ir_variable *const var = d->variable_referenced();
813
814 assert(var != NULL);
815
816 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
817 /* FINISHME: This should actually log the location of the RHS. */
818 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
819 "previous access",
820 var->max_array_access);
821 }
822
823 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
824 rhs->type->array_size());
825 d->type = var->type;
826 }
827 mark_whole_array_access(rhs);
828 mark_whole_array_access(lhs);
829 }
830
831 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
832 * but not post_inc) need the converted assigned value as an rvalue
833 * to handle things like:
834 *
835 * i = j += 1;
836 *
837 * So we always just store the computed value being assigned to a
838 * temporary and return a deref of that temporary. If the rvalue
839 * ends up not being used, the temp will get copy-propagated out.
840 */
841 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
842 ir_var_temporary);
843 ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
844 instructions->push_tail(var);
845 instructions->push_tail(new(ctx) ir_assignment(deref_var, rhs));
846 deref_var = new(ctx) ir_dereference_variable(var);
847
848 if (!error_emitted)
849 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var));
850
851 return new(ctx) ir_dereference_variable(var);
852 }
853
854 static ir_rvalue *
855 get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
856 {
857 void *ctx = ralloc_parent(lvalue);
858 ir_variable *var;
859
860 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
861 ir_var_temporary);
862 instructions->push_tail(var);
863 var->mode = ir_var_auto;
864
865 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
866 lvalue));
867
868 return new(ctx) ir_dereference_variable(var);
869 }
870
871
872 ir_rvalue *
873 ast_node::hir(exec_list *instructions,
874 struct _mesa_glsl_parse_state *state)
875 {
876 (void) instructions;
877 (void) state;
878
879 return NULL;
880 }
881
882 static ir_rvalue *
883 do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
884 {
885 int join_op;
886 ir_rvalue *cmp = NULL;
887
888 if (operation == ir_binop_all_equal)
889 join_op = ir_binop_logic_and;
890 else
891 join_op = ir_binop_logic_or;
892
893 switch (op0->type->base_type) {
894 case GLSL_TYPE_FLOAT:
895 case GLSL_TYPE_UINT:
896 case GLSL_TYPE_INT:
897 case GLSL_TYPE_BOOL:
898 return new(mem_ctx) ir_expression(operation, op0, op1);
899
900 case GLSL_TYPE_ARRAY: {
901 for (unsigned int i = 0; i < op0->type->length; i++) {
902 ir_rvalue *e0, *e1, *result;
903
904 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
905 new(mem_ctx) ir_constant(i));
906 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
907 new(mem_ctx) ir_constant(i));
908 result = do_comparison(mem_ctx, operation, e0, e1);
909
910 if (cmp) {
911 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
912 } else {
913 cmp = result;
914 }
915 }
916
917 mark_whole_array_access(op0);
918 mark_whole_array_access(op1);
919 break;
920 }
921
922 case GLSL_TYPE_STRUCT: {
923 for (unsigned int i = 0; i < op0->type->length; i++) {
924 ir_rvalue *e0, *e1, *result;
925 const char *field_name = op0->type->fields.structure[i].name;
926
927 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
928 field_name);
929 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
930 field_name);
931 result = do_comparison(mem_ctx, operation, e0, e1);
932
933 if (cmp) {
934 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
935 } else {
936 cmp = result;
937 }
938 }
939 break;
940 }
941
942 case GLSL_TYPE_ERROR:
943 case GLSL_TYPE_VOID:
944 case GLSL_TYPE_SAMPLER:
945 case GLSL_TYPE_INTERFACE:
946 case GLSL_TYPE_ATOMIC_UINT:
947 /* I assume a comparison of a struct containing a sampler just
948 * ignores the sampler present in the type.
949 */
950 break;
951 }
952
953 if (cmp == NULL)
954 cmp = new(mem_ctx) ir_constant(true);
955
956 return cmp;
957 }
958
959 /* For logical operations, we want to ensure that the operands are
960 * scalar booleans. If it isn't, emit an error and return a constant
961 * boolean to avoid triggering cascading error messages.
962 */
963 ir_rvalue *
964 get_scalar_boolean_operand(exec_list *instructions,
965 struct _mesa_glsl_parse_state *state,
966 ast_expression *parent_expr,
967 int operand,
968 const char *operand_name,
969 bool *error_emitted)
970 {
971 ast_expression *expr = parent_expr->subexpressions[operand];
972 void *ctx = state;
973 ir_rvalue *val = expr->hir(instructions, state);
974
975 if (val->type->is_boolean() && val->type->is_scalar())
976 return val;
977
978 if (!*error_emitted) {
979 YYLTYPE loc = expr->get_location();
980 _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
981 operand_name,
982 parent_expr->operator_string(parent_expr->oper));
983 *error_emitted = true;
984 }
985
986 return new(ctx) ir_constant(true);
987 }
988
989 /**
990 * If name refers to a builtin array whose maximum allowed size is less than
991 * size, report an error and return true. Otherwise return false.
992 */
993 void
994 check_builtin_array_max_size(const char *name, unsigned size,
995 YYLTYPE loc, struct _mesa_glsl_parse_state *state)
996 {
997 if ((strcmp("gl_TexCoord", name) == 0)
998 && (size > state->Const.MaxTextureCoords)) {
999 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1000 *
1001 * "The size [of gl_TexCoord] can be at most
1002 * gl_MaxTextureCoords."
1003 */
1004 _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
1005 "be larger than gl_MaxTextureCoords (%u)",
1006 state->Const.MaxTextureCoords);
1007 } else if (strcmp("gl_ClipDistance", name) == 0
1008 && size > state->Const.MaxClipPlanes) {
1009 /* From section 7.1 (Vertex Shader Special Variables) of the
1010 * GLSL 1.30 spec:
1011 *
1012 * "The gl_ClipDistance array is predeclared as unsized and
1013 * must be sized by the shader either redeclaring it with a
1014 * size or indexing it only with integral constant
1015 * expressions. ... The size can be at most
1016 * gl_MaxClipDistances."
1017 */
1018 _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
1019 "be larger than gl_MaxClipDistances (%u)",
1020 state->Const.MaxClipPlanes);
1021 }
1022 }
1023
1024 /**
1025 * Create the constant 1, of a which is appropriate for incrementing and
1026 * decrementing values of the given GLSL type. For example, if type is vec4,
1027 * this creates a constant value of 1.0 having type float.
1028 *
1029 * If the given type is invalid for increment and decrement operators, return
1030 * a floating point 1--the error will be detected later.
1031 */
1032 static ir_rvalue *
1033 constant_one_for_inc_dec(void *ctx, const glsl_type *type)
1034 {
1035 switch (type->base_type) {
1036 case GLSL_TYPE_UINT:
1037 return new(ctx) ir_constant((unsigned) 1);
1038 case GLSL_TYPE_INT:
1039 return new(ctx) ir_constant(1);
1040 default:
1041 case GLSL_TYPE_FLOAT:
1042 return new(ctx) ir_constant(1.0f);
1043 }
1044 }
1045
1046 ir_rvalue *
1047 ast_expression::hir(exec_list *instructions,
1048 struct _mesa_glsl_parse_state *state)
1049 {
1050 void *ctx = state;
1051 static const int operations[AST_NUM_OPERATORS] = {
1052 -1, /* ast_assign doesn't convert to ir_expression. */
1053 -1, /* ast_plus doesn't convert to ir_expression. */
1054 ir_unop_neg,
1055 ir_binop_add,
1056 ir_binop_sub,
1057 ir_binop_mul,
1058 ir_binop_div,
1059 ir_binop_mod,
1060 ir_binop_lshift,
1061 ir_binop_rshift,
1062 ir_binop_less,
1063 ir_binop_greater,
1064 ir_binop_lequal,
1065 ir_binop_gequal,
1066 ir_binop_all_equal,
1067 ir_binop_any_nequal,
1068 ir_binop_bit_and,
1069 ir_binop_bit_xor,
1070 ir_binop_bit_or,
1071 ir_unop_bit_not,
1072 ir_binop_logic_and,
1073 ir_binop_logic_xor,
1074 ir_binop_logic_or,
1075 ir_unop_logic_not,
1076
1077 /* Note: The following block of expression types actually convert
1078 * to multiple IR instructions.
1079 */
1080 ir_binop_mul, /* ast_mul_assign */
1081 ir_binop_div, /* ast_div_assign */
1082 ir_binop_mod, /* ast_mod_assign */
1083 ir_binop_add, /* ast_add_assign */
1084 ir_binop_sub, /* ast_sub_assign */
1085 ir_binop_lshift, /* ast_ls_assign */
1086 ir_binop_rshift, /* ast_rs_assign */
1087 ir_binop_bit_and, /* ast_and_assign */
1088 ir_binop_bit_xor, /* ast_xor_assign */
1089 ir_binop_bit_or, /* ast_or_assign */
1090
1091 -1, /* ast_conditional doesn't convert to ir_expression. */
1092 ir_binop_add, /* ast_pre_inc. */
1093 ir_binop_sub, /* ast_pre_dec. */
1094 ir_binop_add, /* ast_post_inc. */
1095 ir_binop_sub, /* ast_post_dec. */
1096 -1, /* ast_field_selection doesn't conv to ir_expression. */
1097 -1, /* ast_array_index doesn't convert to ir_expression. */
1098 -1, /* ast_function_call doesn't conv to ir_expression. */
1099 -1, /* ast_identifier doesn't convert to ir_expression. */
1100 -1, /* ast_int_constant doesn't convert to ir_expression. */
1101 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1102 -1, /* ast_float_constant doesn't conv to ir_expression. */
1103 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1104 -1, /* ast_sequence doesn't convert to ir_expression. */
1105 };
1106 ir_rvalue *result = NULL;
1107 ir_rvalue *op[3];
1108 const struct glsl_type *type; /* a temporary variable for switch cases */
1109 bool error_emitted = false;
1110 YYLTYPE loc;
1111
1112 loc = this->get_location();
1113
1114 switch (this->oper) {
1115 case ast_aggregate:
1116 assert(!"ast_aggregate: Should never get here.");
1117 break;
1118
1119 case ast_assign: {
1120 op[0] = this->subexpressions[0]->hir(instructions, state);
1121 op[1] = this->subexpressions[1]->hir(instructions, state);
1122
1123 result = do_assignment(instructions, state,
1124 this->subexpressions[0]->non_lvalue_description,
1125 op[0], op[1], false,
1126 this->subexpressions[0]->get_location());
1127 error_emitted = result->type->is_error();
1128 break;
1129 }
1130
1131 case ast_plus:
1132 op[0] = this->subexpressions[0]->hir(instructions, state);
1133
1134 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1135
1136 error_emitted = type->is_error();
1137
1138 result = op[0];
1139 break;
1140
1141 case ast_neg:
1142 op[0] = this->subexpressions[0]->hir(instructions, state);
1143
1144 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1145
1146 error_emitted = type->is_error();
1147
1148 result = new(ctx) ir_expression(operations[this->oper], type,
1149 op[0], NULL);
1150 break;
1151
1152 case ast_add:
1153 case ast_sub:
1154 case ast_mul:
1155 case ast_div:
1156 op[0] = this->subexpressions[0]->hir(instructions, state);
1157 op[1] = this->subexpressions[1]->hir(instructions, state);
1158
1159 type = arithmetic_result_type(op[0], op[1],
1160 (this->oper == ast_mul),
1161 state, & loc);
1162 error_emitted = type->is_error();
1163
1164 result = new(ctx) ir_expression(operations[this->oper], type,
1165 op[0], op[1]);
1166 break;
1167
1168 case ast_mod:
1169 op[0] = this->subexpressions[0]->hir(instructions, state);
1170 op[1] = this->subexpressions[1]->hir(instructions, state);
1171
1172 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
1173
1174 assert(operations[this->oper] == ir_binop_mod);
1175
1176 result = new(ctx) ir_expression(operations[this->oper], type,
1177 op[0], op[1]);
1178 error_emitted = type->is_error();
1179 break;
1180
1181 case ast_lshift:
1182 case ast_rshift:
1183 if (!state->check_bitwise_operations_allowed(&loc)) {
1184 error_emitted = true;
1185 }
1186
1187 op[0] = this->subexpressions[0]->hir(instructions, state);
1188 op[1] = this->subexpressions[1]->hir(instructions, state);
1189 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1190 &loc);
1191 result = new(ctx) ir_expression(operations[this->oper], type,
1192 op[0], op[1]);
1193 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1194 break;
1195
1196 case ast_less:
1197 case ast_greater:
1198 case ast_lequal:
1199 case ast_gequal:
1200 op[0] = this->subexpressions[0]->hir(instructions, state);
1201 op[1] = this->subexpressions[1]->hir(instructions, state);
1202
1203 type = relational_result_type(op[0], op[1], state, & loc);
1204
1205 /* The relational operators must either generate an error or result
1206 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1207 */
1208 assert(type->is_error()
1209 || ((type->base_type == GLSL_TYPE_BOOL)
1210 && type->is_scalar()));
1211
1212 result = new(ctx) ir_expression(operations[this->oper], type,
1213 op[0], op[1]);
1214 error_emitted = type->is_error();
1215 break;
1216
1217 case ast_nequal:
1218 case ast_equal:
1219 op[0] = this->subexpressions[0]->hir(instructions, state);
1220 op[1] = this->subexpressions[1]->hir(instructions, state);
1221
1222 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1223 *
1224 * "The equality operators equal (==), and not equal (!=)
1225 * operate on all types. They result in a scalar Boolean. If
1226 * the operand types do not match, then there must be a
1227 * conversion from Section 4.1.10 "Implicit Conversions"
1228 * applied to one operand that can make them match, in which
1229 * case this conversion is done."
1230 */
1231 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1232 && !apply_implicit_conversion(op[1]->type, op[0], state))
1233 || (op[0]->type != op[1]->type)) {
1234 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
1235 "type", (this->oper == ast_equal) ? "==" : "!=");
1236 error_emitted = true;
1237 } else if ((op[0]->type->is_array() || op[1]->type->is_array()) &&
1238 !state->check_version(120, 300, &loc,
1239 "array comparisons forbidden")) {
1240 error_emitted = true;
1241 } else if ((op[0]->type->contains_opaque() ||
1242 op[1]->type->contains_opaque())) {
1243 _mesa_glsl_error(&loc, state, "opaque type comparisons forbidden");
1244 error_emitted = true;
1245 }
1246
1247 if (error_emitted) {
1248 result = new(ctx) ir_constant(false);
1249 } else {
1250 result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
1251 assert(result->type == glsl_type::bool_type);
1252 }
1253 break;
1254
1255 case ast_bit_and:
1256 case ast_bit_xor:
1257 case ast_bit_or:
1258 op[0] = this->subexpressions[0]->hir(instructions, state);
1259 op[1] = this->subexpressions[1]->hir(instructions, state);
1260 type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1261 state, &loc);
1262 result = new(ctx) ir_expression(operations[this->oper], type,
1263 op[0], op[1]);
1264 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1265 break;
1266
1267 case ast_bit_not:
1268 op[0] = this->subexpressions[0]->hir(instructions, state);
1269
1270 if (!state->check_bitwise_operations_allowed(&loc)) {
1271 error_emitted = true;
1272 }
1273
1274 if (!op[0]->type->is_integer()) {
1275 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
1276 error_emitted = true;
1277 }
1278
1279 type = error_emitted ? glsl_type::error_type : op[0]->type;
1280 result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
1281 break;
1282
1283 case ast_logic_and: {
1284 exec_list rhs_instructions;
1285 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1286 "LHS", &error_emitted);
1287 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1288 "RHS", &error_emitted);
1289
1290 if (rhs_instructions.is_empty()) {
1291 result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
1292 type = result->type;
1293 } else {
1294 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
1295 "and_tmp",
1296 ir_var_temporary);
1297 instructions->push_tail(tmp);
1298
1299 ir_if *const stmt = new(ctx) ir_if(op[0]);
1300 instructions->push_tail(stmt);
1301
1302 stmt->then_instructions.append_list(&rhs_instructions);
1303 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1304 ir_assignment *const then_assign =
1305 new(ctx) ir_assignment(then_deref, op[1]);
1306 stmt->then_instructions.push_tail(then_assign);
1307
1308 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1309 ir_assignment *const else_assign =
1310 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false));
1311 stmt->else_instructions.push_tail(else_assign);
1312
1313 result = new(ctx) ir_dereference_variable(tmp);
1314 type = tmp->type;
1315 }
1316 break;
1317 }
1318
1319 case ast_logic_or: {
1320 exec_list rhs_instructions;
1321 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1322 "LHS", &error_emitted);
1323 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1324 "RHS", &error_emitted);
1325
1326 if (rhs_instructions.is_empty()) {
1327 result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
1328 type = result->type;
1329 } else {
1330 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
1331 "or_tmp",
1332 ir_var_temporary);
1333 instructions->push_tail(tmp);
1334
1335 ir_if *const stmt = new(ctx) ir_if(op[0]);
1336 instructions->push_tail(stmt);
1337
1338 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1339 ir_assignment *const then_assign =
1340 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true));
1341 stmt->then_instructions.push_tail(then_assign);
1342
1343 stmt->else_instructions.append_list(&rhs_instructions);
1344 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1345 ir_assignment *const else_assign =
1346 new(ctx) ir_assignment(else_deref, op[1]);
1347 stmt->else_instructions.push_tail(else_assign);
1348
1349 result = new(ctx) ir_dereference_variable(tmp);
1350 type = tmp->type;
1351 }
1352 break;
1353 }
1354
1355 case ast_logic_xor:
1356 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1357 *
1358 * "The logical binary operators and (&&), or ( | | ), and
1359 * exclusive or (^^). They operate only on two Boolean
1360 * expressions and result in a Boolean expression."
1361 */
1362 op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
1363 &error_emitted);
1364 op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
1365 &error_emitted);
1366
1367 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1368 op[0], op[1]);
1369 break;
1370
1371 case ast_logic_not:
1372 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1373 "operand", &error_emitted);
1374
1375 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1376 op[0], NULL);
1377 break;
1378
1379 case ast_mul_assign:
1380 case ast_div_assign:
1381 case ast_add_assign:
1382 case ast_sub_assign: {
1383 op[0] = this->subexpressions[0]->hir(instructions, state);
1384 op[1] = this->subexpressions[1]->hir(instructions, state);
1385
1386 type = arithmetic_result_type(op[0], op[1],
1387 (this->oper == ast_mul_assign),
1388 state, & loc);
1389
1390 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1391 op[0], op[1]);
1392
1393 result = do_assignment(instructions, state,
1394 this->subexpressions[0]->non_lvalue_description,
1395 op[0]->clone(ctx, NULL), temp_rhs, false,
1396 this->subexpressions[0]->get_location());
1397 error_emitted = (op[0]->type->is_error());
1398
1399 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1400 * explicitly test for this because none of the binary expression
1401 * operators allow array operands either.
1402 */
1403
1404 break;
1405 }
1406
1407 case ast_mod_assign: {
1408 op[0] = this->subexpressions[0]->hir(instructions, state);
1409 op[1] = this->subexpressions[1]->hir(instructions, state);
1410
1411 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
1412
1413 assert(operations[this->oper] == ir_binop_mod);
1414
1415 ir_rvalue *temp_rhs;
1416 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1417 op[0], op[1]);
1418
1419 result = do_assignment(instructions, state,
1420 this->subexpressions[0]->non_lvalue_description,
1421 op[0]->clone(ctx, NULL), temp_rhs, false,
1422 this->subexpressions[0]->get_location());
1423 error_emitted = type->is_error();
1424 break;
1425 }
1426
1427 case ast_ls_assign:
1428 case ast_rs_assign: {
1429 op[0] = this->subexpressions[0]->hir(instructions, state);
1430 op[1] = this->subexpressions[1]->hir(instructions, state);
1431 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1432 &loc);
1433 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1434 type, op[0], op[1]);
1435 result = do_assignment(instructions, state,
1436 this->subexpressions[0]->non_lvalue_description,
1437 op[0]->clone(ctx, NULL), temp_rhs, false,
1438 this->subexpressions[0]->get_location());
1439 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1440 break;
1441 }
1442
1443 case ast_and_assign:
1444 case ast_xor_assign:
1445 case ast_or_assign: {
1446 op[0] = this->subexpressions[0]->hir(instructions, state);
1447 op[1] = this->subexpressions[1]->hir(instructions, state);
1448 type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1449 state, &loc);
1450 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1451 type, op[0], op[1]);
1452 result = do_assignment(instructions, state,
1453 this->subexpressions[0]->non_lvalue_description,
1454 op[0]->clone(ctx, NULL), temp_rhs, false,
1455 this->subexpressions[0]->get_location());
1456 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1457 break;
1458 }
1459
1460 case ast_conditional: {
1461 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1462 *
1463 * "The ternary selection operator (?:). It operates on three
1464 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1465 * first expression, which must result in a scalar Boolean."
1466 */
1467 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1468 "condition", &error_emitted);
1469
1470 /* The :? operator is implemented by generating an anonymous temporary
1471 * followed by an if-statement. The last instruction in each branch of
1472 * the if-statement assigns a value to the anonymous temporary. This
1473 * temporary is the r-value of the expression.
1474 */
1475 exec_list then_instructions;
1476 exec_list else_instructions;
1477
1478 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1479 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
1480
1481 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1482 *
1483 * "The second and third expressions can be any type, as
1484 * long their types match, or there is a conversion in
1485 * Section 4.1.10 "Implicit Conversions" that can be applied
1486 * to one of the expressions to make their types match. This
1487 * resulting matching type is the type of the entire
1488 * expression."
1489 */
1490 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1491 && !apply_implicit_conversion(op[2]->type, op[1], state))
1492 || (op[1]->type != op[2]->type)) {
1493 YYLTYPE loc = this->subexpressions[1]->get_location();
1494
1495 _mesa_glsl_error(& loc, state, "second and third operands of ?: "
1496 "operator must have matching types");
1497 error_emitted = true;
1498 type = glsl_type::error_type;
1499 } else {
1500 type = op[1]->type;
1501 }
1502
1503 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1504 *
1505 * "The second and third expressions must be the same type, but can
1506 * be of any type other than an array."
1507 */
1508 if (type->is_array() &&
1509 !state->check_version(120, 300, &loc,
1510 "second and third operands of ?: operator "
1511 "cannot be arrays")) {
1512 error_emitted = true;
1513 }
1514
1515 ir_constant *cond_val = op[0]->constant_expression_value();
1516 ir_constant *then_val = op[1]->constant_expression_value();
1517 ir_constant *else_val = op[2]->constant_expression_value();
1518
1519 if (then_instructions.is_empty()
1520 && else_instructions.is_empty()
1521 && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
1522 result = (cond_val->value.b[0]) ? then_val : else_val;
1523 } else {
1524 ir_variable *const tmp =
1525 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
1526 instructions->push_tail(tmp);
1527
1528 ir_if *const stmt = new(ctx) ir_if(op[0]);
1529 instructions->push_tail(stmt);
1530
1531 then_instructions.move_nodes_to(& stmt->then_instructions);
1532 ir_dereference *const then_deref =
1533 new(ctx) ir_dereference_variable(tmp);
1534 ir_assignment *const then_assign =
1535 new(ctx) ir_assignment(then_deref, op[1]);
1536 stmt->then_instructions.push_tail(then_assign);
1537
1538 else_instructions.move_nodes_to(& stmt->else_instructions);
1539 ir_dereference *const else_deref =
1540 new(ctx) ir_dereference_variable(tmp);
1541 ir_assignment *const else_assign =
1542 new(ctx) ir_assignment(else_deref, op[2]);
1543 stmt->else_instructions.push_tail(else_assign);
1544
1545 result = new(ctx) ir_dereference_variable(tmp);
1546 }
1547 break;
1548 }
1549
1550 case ast_pre_inc:
1551 case ast_pre_dec: {
1552 this->non_lvalue_description = (this->oper == ast_pre_inc)
1553 ? "pre-increment operation" : "pre-decrement operation";
1554
1555 op[0] = this->subexpressions[0]->hir(instructions, state);
1556 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1557
1558 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1559
1560 ir_rvalue *temp_rhs;
1561 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1562 op[0], op[1]);
1563
1564 result = do_assignment(instructions, state,
1565 this->subexpressions[0]->non_lvalue_description,
1566 op[0]->clone(ctx, NULL), temp_rhs, false,
1567 this->subexpressions[0]->get_location());
1568 error_emitted = op[0]->type->is_error();
1569 break;
1570 }
1571
1572 case ast_post_inc:
1573 case ast_post_dec: {
1574 this->non_lvalue_description = (this->oper == ast_post_inc)
1575 ? "post-increment operation" : "post-decrement operation";
1576 op[0] = this->subexpressions[0]->hir(instructions, state);
1577 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1578
1579 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1580
1581 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1582
1583 ir_rvalue *temp_rhs;
1584 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1585 op[0], op[1]);
1586
1587 /* Get a temporary of a copy of the lvalue before it's modified.
1588 * This may get thrown away later.
1589 */
1590 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1591
1592 (void)do_assignment(instructions, state,
1593 this->subexpressions[0]->non_lvalue_description,
1594 op[0]->clone(ctx, NULL), temp_rhs, false,
1595 this->subexpressions[0]->get_location());
1596
1597 error_emitted = op[0]->type->is_error();
1598 break;
1599 }
1600
1601 case ast_field_selection:
1602 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1603 break;
1604
1605 case ast_array_index: {
1606 YYLTYPE index_loc = subexpressions[1]->get_location();
1607
1608 op[0] = subexpressions[0]->hir(instructions, state);
1609 op[1] = subexpressions[1]->hir(instructions, state);
1610
1611 result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1],
1612 loc, index_loc);
1613
1614 if (result->type->is_error())
1615 error_emitted = true;
1616
1617 break;
1618 }
1619
1620 case ast_function_call:
1621 /* Should *NEVER* get here. ast_function_call should always be handled
1622 * by ast_function_expression::hir.
1623 */
1624 assert(0);
1625 break;
1626
1627 case ast_identifier: {
1628 /* ast_identifier can appear several places in a full abstract syntax
1629 * tree. This particular use must be at location specified in the grammar
1630 * as 'variable_identifier'.
1631 */
1632 ir_variable *var =
1633 state->symbols->get_variable(this->primary_expression.identifier);
1634
1635 if (var != NULL) {
1636 var->used = true;
1637 result = new(ctx) ir_dereference_variable(var);
1638 } else {
1639 _mesa_glsl_error(& loc, state, "`%s' undeclared",
1640 this->primary_expression.identifier);
1641
1642 result = ir_rvalue::error_value(ctx);
1643 error_emitted = true;
1644 }
1645 break;
1646 }
1647
1648 case ast_int_constant:
1649 result = new(ctx) ir_constant(this->primary_expression.int_constant);
1650 break;
1651
1652 case ast_uint_constant:
1653 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1654 break;
1655
1656 case ast_float_constant:
1657 result = new(ctx) ir_constant(this->primary_expression.float_constant);
1658 break;
1659
1660 case ast_bool_constant:
1661 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1662 break;
1663
1664 case ast_sequence: {
1665 /* It should not be possible to generate a sequence in the AST without
1666 * any expressions in it.
1667 */
1668 assert(!this->expressions.is_empty());
1669
1670 /* The r-value of a sequence is the last expression in the sequence. If
1671 * the other expressions in the sequence do not have side-effects (and
1672 * therefore add instructions to the instruction list), they get dropped
1673 * on the floor.
1674 */
1675 exec_node *previous_tail_pred = NULL;
1676 YYLTYPE previous_operand_loc = loc;
1677
1678 foreach_list_typed (ast_node, ast, link, &this->expressions) {
1679 /* If one of the operands of comma operator does not generate any
1680 * code, we want to emit a warning. At each pass through the loop
1681 * previous_tail_pred will point to the last instruction in the
1682 * stream *before* processing the previous operand. Naturally,
1683 * instructions->tail_pred will point to the last instruction in the
1684 * stream *after* processing the previous operand. If the two
1685 * pointers match, then the previous operand had no effect.
1686 *
1687 * The warning behavior here differs slightly from GCC. GCC will
1688 * only emit a warning if none of the left-hand operands have an
1689 * effect. However, it will emit a warning for each. I believe that
1690 * there are some cases in C (especially with GCC extensions) where
1691 * it is useful to have an intermediate step in a sequence have no
1692 * effect, but I don't think these cases exist in GLSL. Either way,
1693 * it would be a giant hassle to replicate that behavior.
1694 */
1695 if (previous_tail_pred == instructions->tail_pred) {
1696 _mesa_glsl_warning(&previous_operand_loc, state,
1697 "left-hand operand of comma expression has "
1698 "no effect");
1699 }
1700
1701 /* tail_pred is directly accessed instead of using the get_tail()
1702 * method for performance reasons. get_tail() has extra code to
1703 * return NULL when the list is empty. We don't care about that
1704 * here, so using tail_pred directly is fine.
1705 */
1706 previous_tail_pred = instructions->tail_pred;
1707 previous_operand_loc = ast->get_location();
1708
1709 result = ast->hir(instructions, state);
1710 }
1711
1712 /* Any errors should have already been emitted in the loop above.
1713 */
1714 error_emitted = true;
1715 break;
1716 }
1717 }
1718 type = NULL; /* use result->type, not type. */
1719 assert(result != NULL);
1720
1721 if (result->type->is_error() && !error_emitted)
1722 _mesa_glsl_error(& loc, state, "type mismatch");
1723
1724 return result;
1725 }
1726
1727
1728 ir_rvalue *
1729 ast_expression_statement::hir(exec_list *instructions,
1730 struct _mesa_glsl_parse_state *state)
1731 {
1732 /* It is possible to have expression statements that don't have an
1733 * expression. This is the solitary semicolon:
1734 *
1735 * for (i = 0; i < 5; i++)
1736 * ;
1737 *
1738 * In this case the expression will be NULL. Test for NULL and don't do
1739 * anything in that case.
1740 */
1741 if (expression != NULL)
1742 expression->hir(instructions, state);
1743
1744 /* Statements do not have r-values.
1745 */
1746 return NULL;
1747 }
1748
1749
1750 ir_rvalue *
1751 ast_compound_statement::hir(exec_list *instructions,
1752 struct _mesa_glsl_parse_state *state)
1753 {
1754 if (new_scope)
1755 state->symbols->push_scope();
1756
1757 foreach_list_typed (ast_node, ast, link, &this->statements)
1758 ast->hir(instructions, state);
1759
1760 if (new_scope)
1761 state->symbols->pop_scope();
1762
1763 /* Compound statements do not have r-values.
1764 */
1765 return NULL;
1766 }
1767
1768
1769 static const glsl_type *
1770 process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
1771 struct _mesa_glsl_parse_state *state)
1772 {
1773 unsigned length = 0;
1774
1775 if (base == NULL)
1776 return glsl_type::error_type;
1777
1778 /* From page 19 (page 25) of the GLSL 1.20 spec:
1779 *
1780 * "Only one-dimensional arrays may be declared."
1781 */
1782 if (base->is_array()) {
1783 _mesa_glsl_error(loc, state,
1784 "invalid array of `%s' (only one-dimensional arrays "
1785 "may be declared)",
1786 base->name);
1787 return glsl_type::error_type;
1788 }
1789
1790 if (array_size != NULL) {
1791 exec_list dummy_instructions;
1792 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1793 YYLTYPE loc = array_size->get_location();
1794
1795 if (ir != NULL) {
1796 if (!ir->type->is_integer()) {
1797 _mesa_glsl_error(& loc, state, "array size must be integer type");
1798 } else if (!ir->type->is_scalar()) {
1799 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1800 } else {
1801 ir_constant *const size = ir->constant_expression_value();
1802
1803 if (size == NULL) {
1804 _mesa_glsl_error(& loc, state, "array size must be a "
1805 "constant valued expression");
1806 } else if (size->value.i[0] <= 0) {
1807 _mesa_glsl_error(& loc, state, "array size must be > 0");
1808 } else {
1809 assert(size->type == ir->type);
1810 length = size->value.u[0];
1811
1812 /* If the array size is const (and we've verified that
1813 * it is) then no instructions should have been emitted
1814 * when we converted it to HIR. If they were emitted,
1815 * then either the array size isn't const after all, or
1816 * we are emitting unnecessary instructions.
1817 */
1818 assert(dummy_instructions.is_empty());
1819 }
1820 }
1821 }
1822 }
1823
1824 const glsl_type *array_type = glsl_type::get_array_instance(base, length);
1825 return array_type != NULL ? array_type : glsl_type::error_type;
1826 }
1827
1828
1829 const glsl_type *
1830 ast_type_specifier::glsl_type(const char **name,
1831 struct _mesa_glsl_parse_state *state) const
1832 {
1833 const struct glsl_type *type;
1834
1835 type = state->symbols->get_type(this->type_name);
1836 *name = this->type_name;
1837
1838 if (this->is_array) {
1839 YYLTYPE loc = this->get_location();
1840 type = process_array_type(&loc, type, this->array_size, state);
1841 }
1842
1843 return type;
1844 }
1845
1846 const glsl_type *
1847 ast_fully_specified_type::glsl_type(const char **name,
1848 struct _mesa_glsl_parse_state *state) const
1849 {
1850 const struct glsl_type *type = this->specifier->glsl_type(name, state);
1851
1852 if (type == NULL)
1853 return NULL;
1854
1855 if (type->base_type == GLSL_TYPE_FLOAT
1856 && state->es_shader
1857 && state->target == fragment_shader
1858 && this->qualifier.precision == ast_precision_none
1859 && state->symbols->get_variable("#default precision") == NULL) {
1860 YYLTYPE loc = this->get_location();
1861 _mesa_glsl_error(&loc, state,
1862 "no precision specified this scope for type `%s'",
1863 type->name);
1864 }
1865
1866 return type;
1867 }
1868
1869 /**
1870 * Determine whether a toplevel variable declaration declares a varying. This
1871 * function operates by examining the variable's mode and the shader target,
1872 * so it correctly identifies linkage variables regardless of whether they are
1873 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
1874 *
1875 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
1876 * this function will produce undefined results.
1877 */
1878 static bool
1879 is_varying_var(ir_variable *var, _mesa_glsl_parser_targets target)
1880 {
1881 switch (target) {
1882 case vertex_shader:
1883 return var->mode == ir_var_shader_out;
1884 case fragment_shader:
1885 return var->mode == ir_var_shader_in;
1886 default:
1887 return var->mode == ir_var_shader_out || var->mode == ir_var_shader_in;
1888 }
1889 }
1890
1891
1892 /**
1893 * Matrix layout qualifiers are only allowed on certain types
1894 */
1895 static void
1896 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state,
1897 YYLTYPE *loc,
1898 const glsl_type *type,
1899 ir_variable *var)
1900 {
1901 if (var && !var->is_in_uniform_block()) {
1902 /* Layout qualifiers may only apply to interface blocks and fields in
1903 * them.
1904 */
1905 _mesa_glsl_error(loc, state,
1906 "uniform block layout qualifiers row_major and "
1907 "column_major may not be applied to variables "
1908 "outside of uniform blocks");
1909 } else if (!type->is_matrix()) {
1910 /* The OpenGL ES 3.0 conformance tests did not originally allow
1911 * matrix layout qualifiers on non-matrices. However, the OpenGL
1912 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
1913 * amended to specifically allow these layouts on all types. Emit
1914 * a warning so that people know their code may not be portable.
1915 */
1916 _mesa_glsl_warning(loc, state,
1917 "uniform block layout qualifiers row_major and "
1918 "column_major applied to non-matrix types may "
1919 "be rejected by older compilers");
1920 } else if (type->is_record()) {
1921 /* We allow 'layout(row_major)' on structure types because it's the only
1922 * way to get row-major layouts on matrices contained in structures.
1923 */
1924 _mesa_glsl_warning(loc, state,
1925 "uniform block layout qualifiers row_major and "
1926 "column_major applied to structure types is not "
1927 "strictly conformant and may be rejected by other "
1928 "compilers");
1929 }
1930 }
1931
1932 static bool
1933 validate_binding_qualifier(struct _mesa_glsl_parse_state *state,
1934 YYLTYPE *loc,
1935 ir_variable *var,
1936 const ast_type_qualifier *qual)
1937 {
1938 if (var->mode != ir_var_uniform) {
1939 _mesa_glsl_error(loc, state,
1940 "the \"binding\" qualifier only applies to uniforms");
1941 return false;
1942 }
1943
1944 if (qual->binding < 0) {
1945 _mesa_glsl_error(loc, state, "binding values must be >= 0");
1946 return false;
1947 }
1948
1949 const struct gl_context *const ctx = state->ctx;
1950 unsigned elements = var->type->is_array() ? var->type->length : 1;
1951 unsigned max_index = qual->binding + elements - 1;
1952
1953 if (var->type->is_interface()) {
1954 /* UBOs. From page 60 of the GLSL 4.20 specification:
1955 * "If the binding point for any uniform block instance is less than zero,
1956 * or greater than or equal to the implementation-dependent maximum
1957 * number of uniform buffer bindings, a compilation error will occur.
1958 * When the binding identifier is used with a uniform block instanced as
1959 * an array of size N, all elements of the array from binding through
1960 * binding + N – 1 must be within this range."
1961 *
1962 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
1963 */
1964 if (max_index >= ctx->Const.MaxUniformBufferBindings) {
1965 _mesa_glsl_error(loc, state, "layout(binding = %d) for %d UBOs exceeds "
1966 "the maximum number of UBO binding points (%d)",
1967 qual->binding, elements,
1968 ctx->Const.MaxUniformBufferBindings);
1969 return false;
1970 }
1971 } else if (var->type->is_sampler() ||
1972 (var->type->is_array() && var->type->fields.array->is_sampler())) {
1973 /* Samplers. From page 63 of the GLSL 4.20 specification:
1974 * "If the binding is less than zero, or greater than or equal to the
1975 * implementation-dependent maximum supported number of units, a
1976 * compilation error will occur. When the binding identifier is used
1977 * with an array of size N, all elements of the array from binding
1978 * through binding + N - 1 must be within this range."
1979 */
1980 unsigned limit = 0;
1981 switch (state->target) {
1982 case vertex_shader:
1983 limit = ctx->Const.VertexProgram.MaxTextureImageUnits;
1984 break;
1985 case geometry_shader:
1986 limit = ctx->Const.GeometryProgram.MaxTextureImageUnits;
1987 break;
1988 case fragment_shader:
1989 limit = ctx->Const.FragmentProgram.MaxTextureImageUnits;
1990 break;
1991 }
1992
1993 if (max_index >= limit) {
1994 _mesa_glsl_error(loc, state, "layout(binding = %d) for %d samplers "
1995 "exceeds the maximum number of texture image units "
1996 "(%d)", qual->binding, elements, limit);
1997
1998 return false;
1999 }
2000 } else if (var->type->contains_atomic()) {
2001 assert(ctx->Const.MaxAtomicBufferBindings <= MAX_COMBINED_ATOMIC_BUFFERS);
2002 if (unsigned(qual->binding) >= ctx->Const.MaxAtomicBufferBindings) {
2003 _mesa_glsl_error(loc, state, "layout(binding = %d) exceeds the "
2004 " maximum number of atomic counter buffer bindings"
2005 "(%d)", qual->binding,
2006 ctx->Const.MaxAtomicBufferBindings);
2007
2008 return false;
2009 }
2010 } else {
2011 _mesa_glsl_error(loc, state,
2012 "the \"binding\" qualifier only applies to uniform "
2013 "blocks, samplers, atomic counters, or arrays thereof");
2014 return false;
2015 }
2016
2017 return true;
2018 }
2019
2020
2021 static glsl_interp_qualifier
2022 interpret_interpolation_qualifier(const struct ast_type_qualifier *qual,
2023 ir_variable_mode mode,
2024 struct _mesa_glsl_parse_state *state,
2025 YYLTYPE *loc)
2026 {
2027 glsl_interp_qualifier interpolation;
2028 if (qual->flags.q.flat)
2029 interpolation = INTERP_QUALIFIER_FLAT;
2030 else if (qual->flags.q.noperspective)
2031 interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
2032 else if (qual->flags.q.smooth)
2033 interpolation = INTERP_QUALIFIER_SMOOTH;
2034 else
2035 interpolation = INTERP_QUALIFIER_NONE;
2036
2037 if (interpolation != INTERP_QUALIFIER_NONE) {
2038 if (mode != ir_var_shader_in && mode != ir_var_shader_out) {
2039 _mesa_glsl_error(loc, state,
2040 "interpolation qualifier `%s' can only be applied to "
2041 "shader inputs or outputs.",
2042 interpolation_string(interpolation));
2043
2044 }
2045
2046 if ((state->target == vertex_shader && mode == ir_var_shader_in) ||
2047 (state->target == fragment_shader && mode == ir_var_shader_out)) {
2048 _mesa_glsl_error(loc, state,
2049 "interpolation qualifier `%s' cannot be applied to "
2050 "vertex shader inputs or fragment shader outputs",
2051 interpolation_string(interpolation));
2052 }
2053 }
2054
2055 return interpolation;
2056 }
2057
2058
2059 static void
2060 validate_explicit_location(const struct ast_type_qualifier *qual,
2061 ir_variable *var,
2062 struct _mesa_glsl_parse_state *state,
2063 YYLTYPE *loc)
2064 {
2065 bool fail = false;
2066
2067 /* In the vertex shader only shader inputs can be given explicit
2068 * locations.
2069 *
2070 * In the fragment shader only shader outputs can be given explicit
2071 * locations.
2072 */
2073 switch (state->target) {
2074 case vertex_shader:
2075 if (var->mode == ir_var_shader_in) {
2076 if (!state->check_explicit_attrib_location_allowed(loc, var))
2077 return;
2078
2079 break;
2080 }
2081
2082 fail = true;
2083 break;
2084
2085 case geometry_shader:
2086 _mesa_glsl_error(loc, state,
2087 "geometry shader variables cannot be given "
2088 "explicit locations");
2089 return;
2090
2091 case fragment_shader:
2092 if (var->mode == ir_var_shader_out) {
2093 if (!state->check_explicit_attrib_location_allowed(loc, var))
2094 return;
2095
2096 break;
2097 }
2098
2099 fail = true;
2100 break;
2101 };
2102
2103 if (fail) {
2104 _mesa_glsl_error(loc, state,
2105 "%s cannot be given an explicit location in %s shader",
2106 mode_string(var),
2107 _mesa_glsl_shader_target_name(state->target));
2108 } else {
2109 var->explicit_location = true;
2110
2111 /* This bit of silliness is needed because invalid explicit locations
2112 * are supposed to be flagged during linking. Small negative values
2113 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2114 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2115 * The linker needs to be able to differentiate these cases. This
2116 * ensures that negative values stay negative.
2117 */
2118 if (qual->location >= 0) {
2119 var->location = (state->target == vertex_shader)
2120 ? (qual->location + VERT_ATTRIB_GENERIC0)
2121 : (qual->location + FRAG_RESULT_DATA0);
2122 } else {
2123 var->location = qual->location;
2124 }
2125
2126 if (qual->flags.q.explicit_index) {
2127 /* From the GLSL 4.30 specification, section 4.4.2 (Output
2128 * Layout Qualifiers):
2129 *
2130 * "It is also a compile-time error if a fragment shader
2131 * sets a layout index to less than 0 or greater than 1."
2132 *
2133 * Older specifications don't mandate a behavior; we take
2134 * this as a clarification and always generate the error.
2135 */
2136 if (qual->index < 0 || qual->index > 1) {
2137 _mesa_glsl_error(loc, state,
2138 "explicit index may only be 0 or 1");
2139 } else {
2140 var->explicit_index = true;
2141 var->index = qual->index;
2142 }
2143 }
2144 }
2145
2146 return;
2147 }
2148
2149 static void
2150 apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
2151 ir_variable *var,
2152 struct _mesa_glsl_parse_state *state,
2153 YYLTYPE *loc,
2154 bool is_parameter)
2155 {
2156 STATIC_ASSERT(sizeof(qual->flags.q) <= sizeof(qual->flags.i));
2157
2158 if (qual->flags.q.invariant) {
2159 if (var->used) {
2160 _mesa_glsl_error(loc, state,
2161 "variable `%s' may not be redeclared "
2162 "`invariant' after being used",
2163 var->name);
2164 } else {
2165 var->invariant = 1;
2166 }
2167 }
2168
2169 if (qual->flags.q.constant || qual->flags.q.attribute
2170 || qual->flags.q.uniform
2171 || (qual->flags.q.varying && (state->target == fragment_shader)))
2172 var->read_only = 1;
2173
2174 if (qual->flags.q.centroid)
2175 var->centroid = 1;
2176
2177 if (qual->flags.q.attribute && state->target != vertex_shader) {
2178 var->type = glsl_type::error_type;
2179 _mesa_glsl_error(loc, state,
2180 "`attribute' variables may not be declared in the "
2181 "%s shader",
2182 _mesa_glsl_shader_target_name(state->target));
2183 }
2184
2185 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
2186 *
2187 * "However, the const qualifier cannot be used with out or inout."
2188 *
2189 * The same section of the GLSL 4.40 spec further clarifies this saying:
2190 *
2191 * "The const qualifier cannot be used with out or inout, or a
2192 * compile-time error results."
2193 */
2194 if (is_parameter && qual->flags.q.constant && qual->flags.q.out) {
2195 _mesa_glsl_error(loc, state,
2196 "`const' may not be applied to `out' or `inout' "
2197 "function parameters");
2198 }
2199
2200 /* If there is no qualifier that changes the mode of the variable, leave
2201 * the setting alone.
2202 */
2203 if (qual->flags.q.in && qual->flags.q.out)
2204 var->mode = ir_var_function_inout;
2205 else if (qual->flags.q.in)
2206 var->mode = is_parameter ? ir_var_function_in : ir_var_shader_in;
2207 else if (qual->flags.q.attribute
2208 || (qual->flags.q.varying && (state->target == fragment_shader)))
2209 var->mode = ir_var_shader_in;
2210 else if (qual->flags.q.out)
2211 var->mode = is_parameter ? ir_var_function_out : ir_var_shader_out;
2212 else if (qual->flags.q.varying && (state->target == vertex_shader))
2213 var->mode = ir_var_shader_out;
2214 else if (qual->flags.q.uniform)
2215 var->mode = ir_var_uniform;
2216
2217 if (!is_parameter && is_varying_var(var, state->target)) {
2218 /* This variable is being used to link data between shader stages (in
2219 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
2220 * that is allowed for such purposes.
2221 *
2222 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
2223 *
2224 * "The varying qualifier can be used only with the data types
2225 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
2226 * these."
2227 *
2228 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
2229 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
2230 *
2231 * "Fragment inputs can only be signed and unsigned integers and
2232 * integer vectors, float, floating-point vectors, matrices, or
2233 * arrays of these. Structures cannot be input.
2234 *
2235 * Similar text exists in the section on vertex shader outputs.
2236 *
2237 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
2238 * 3.00 spec allows structs as well. Varying structs are also allowed
2239 * in GLSL 1.50.
2240 */
2241 switch (var->type->get_scalar_type()->base_type) {
2242 case GLSL_TYPE_FLOAT:
2243 /* Ok in all GLSL versions */
2244 break;
2245 case GLSL_TYPE_UINT:
2246 case GLSL_TYPE_INT:
2247 if (state->is_version(130, 300))
2248 break;
2249 _mesa_glsl_error(loc, state,
2250 "varying variables must be of base type float in %s",
2251 state->get_version_string());
2252 break;
2253 case GLSL_TYPE_STRUCT:
2254 if (state->is_version(150, 300))
2255 break;
2256 _mesa_glsl_error(loc, state,
2257 "varying variables may not be of type struct");
2258 break;
2259 default:
2260 _mesa_glsl_error(loc, state, "illegal type for a varying variable");
2261 break;
2262 }
2263 }
2264
2265 if (state->all_invariant && (state->current_function == NULL)) {
2266 switch (state->target) {
2267 case vertex_shader:
2268 if (var->mode == ir_var_shader_out)
2269 var->invariant = true;
2270 break;
2271 case geometry_shader:
2272 if ((var->mode == ir_var_shader_in)
2273 || (var->mode == ir_var_shader_out))
2274 var->invariant = true;
2275 break;
2276 case fragment_shader:
2277 if (var->mode == ir_var_shader_in)
2278 var->invariant = true;
2279 break;
2280 }
2281 }
2282
2283 var->interpolation =
2284 interpret_interpolation_qualifier(qual, (ir_variable_mode) var->mode,
2285 state, loc);
2286
2287 var->pixel_center_integer = qual->flags.q.pixel_center_integer;
2288 var->origin_upper_left = qual->flags.q.origin_upper_left;
2289 if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
2290 && (strcmp(var->name, "gl_FragCoord") != 0)) {
2291 const char *const qual_string = (qual->flags.q.origin_upper_left)
2292 ? "origin_upper_left" : "pixel_center_integer";
2293
2294 _mesa_glsl_error(loc, state,
2295 "layout qualifier `%s' can only be applied to "
2296 "fragment shader input `gl_FragCoord'",
2297 qual_string);
2298 }
2299
2300 if (qual->flags.q.explicit_location) {
2301 validate_explicit_location(qual, var, state, loc);
2302 } else if (qual->flags.q.explicit_index) {
2303 _mesa_glsl_error(loc, state,
2304 "explicit index requires explicit location");
2305 }
2306
2307 if (qual->flags.q.explicit_binding &&
2308 validate_binding_qualifier(state, loc, var, qual)) {
2309 var->explicit_binding = true;
2310 var->binding = qual->binding;
2311 }
2312
2313 if (var->type->contains_atomic()) {
2314 if (var->mode == ir_var_uniform) {
2315 if (var->explicit_binding) {
2316 unsigned *offset = &state->atomic_counter_offsets[var->binding];
2317
2318 if (*offset % ATOMIC_COUNTER_SIZE)
2319 _mesa_glsl_error(loc, state,
2320 "misaligned atomic counter offset");
2321
2322 var->atomic.offset = *offset;
2323 *offset += var->type->atomic_size();
2324
2325 } else {
2326 _mesa_glsl_error(loc, state,
2327 "atomic counters require explicit binding point");
2328 }
2329 } else if (var->mode != ir_var_function_in) {
2330 _mesa_glsl_error(loc, state, "atomic counters may only be declared as "
2331 "function parameters or uniform-qualified "
2332 "global variables");
2333 }
2334 }
2335
2336 /* Does the declaration use the deprecated 'attribute' or 'varying'
2337 * keywords?
2338 */
2339 const bool uses_deprecated_qualifier = qual->flags.q.attribute
2340 || qual->flags.q.varying;
2341
2342 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2343 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2344 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2345 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2346 * These extensions and all following extensions that add the 'layout'
2347 * keyword have been modified to require the use of 'in' or 'out'.
2348 *
2349 * The following extension do not allow the deprecated keywords:
2350 *
2351 * GL_AMD_conservative_depth
2352 * GL_ARB_conservative_depth
2353 * GL_ARB_gpu_shader5
2354 * GL_ARB_separate_shader_objects
2355 * GL_ARB_tesselation_shader
2356 * GL_ARB_transform_feedback3
2357 * GL_ARB_uniform_buffer_object
2358 *
2359 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2360 * allow layout with the deprecated keywords.
2361 */
2362 const bool relaxed_layout_qualifier_checking =
2363 state->ARB_fragment_coord_conventions_enable;
2364
2365 if (qual->has_layout() && uses_deprecated_qualifier) {
2366 if (relaxed_layout_qualifier_checking) {
2367 _mesa_glsl_warning(loc, state,
2368 "`layout' qualifier may not be used with "
2369 "`attribute' or `varying'");
2370 } else {
2371 _mesa_glsl_error(loc, state,
2372 "`layout' qualifier may not be used with "
2373 "`attribute' or `varying'");
2374 }
2375 }
2376
2377 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2378 * AMD_conservative_depth.
2379 */
2380 int depth_layout_count = qual->flags.q.depth_any
2381 + qual->flags.q.depth_greater
2382 + qual->flags.q.depth_less
2383 + qual->flags.q.depth_unchanged;
2384 if (depth_layout_count > 0
2385 && !state->AMD_conservative_depth_enable
2386 && !state->ARB_conservative_depth_enable) {
2387 _mesa_glsl_error(loc, state,
2388 "extension GL_AMD_conservative_depth or "
2389 "GL_ARB_conservative_depth must be enabled "
2390 "to use depth layout qualifiers");
2391 } else if (depth_layout_count > 0
2392 && strcmp(var->name, "gl_FragDepth") != 0) {
2393 _mesa_glsl_error(loc, state,
2394 "depth layout qualifiers can be applied only to "
2395 "gl_FragDepth");
2396 } else if (depth_layout_count > 1
2397 && strcmp(var->name, "gl_FragDepth") == 0) {
2398 _mesa_glsl_error(loc, state,
2399 "at most one depth layout qualifier can be applied to "
2400 "gl_FragDepth");
2401 }
2402 if (qual->flags.q.depth_any)
2403 var->depth_layout = ir_depth_layout_any;
2404 else if (qual->flags.q.depth_greater)
2405 var->depth_layout = ir_depth_layout_greater;
2406 else if (qual->flags.q.depth_less)
2407 var->depth_layout = ir_depth_layout_less;
2408 else if (qual->flags.q.depth_unchanged)
2409 var->depth_layout = ir_depth_layout_unchanged;
2410 else
2411 var->depth_layout = ir_depth_layout_none;
2412
2413 if (qual->flags.q.std140 ||
2414 qual->flags.q.packed ||
2415 qual->flags.q.shared) {
2416 _mesa_glsl_error(loc, state,
2417 "uniform block layout qualifiers std140, packed, and "
2418 "shared can only be applied to uniform blocks, not "
2419 "members");
2420 }
2421
2422 if (qual->flags.q.row_major || qual->flags.q.column_major) {
2423 validate_matrix_layout_for_type(state, loc, var->type, var);
2424 }
2425 }
2426
2427 /**
2428 * Get the variable that is being redeclared by this declaration
2429 *
2430 * Semantic checks to verify the validity of the redeclaration are also
2431 * performed. If semantic checks fail, compilation error will be emitted via
2432 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2433 *
2434 * \returns
2435 * A pointer to an existing variable in the current scope if the declaration
2436 * is a redeclaration, \c NULL otherwise.
2437 */
2438 static ir_variable *
2439 get_variable_being_redeclared(ir_variable *var, YYLTYPE loc,
2440 struct _mesa_glsl_parse_state *state,
2441 bool allow_all_redeclarations)
2442 {
2443 /* Check if this declaration is actually a re-declaration, either to
2444 * resize an array or add qualifiers to an existing variable.
2445 *
2446 * This is allowed for variables in the current scope, or when at
2447 * global scope (for built-ins in the implicit outer scope).
2448 */
2449 ir_variable *earlier = state->symbols->get_variable(var->name);
2450 if (earlier == NULL ||
2451 (state->current_function != NULL &&
2452 !state->symbols->name_declared_this_scope(var->name))) {
2453 return NULL;
2454 }
2455
2456
2457 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2458 *
2459 * "It is legal to declare an array without a size and then
2460 * later re-declare the same name as an array of the same
2461 * type and specify a size."
2462 */
2463 if (earlier->type->is_unsized_array() && var->type->is_array()
2464 && (var->type->element_type() == earlier->type->element_type())) {
2465 /* FINISHME: This doesn't match the qualifiers on the two
2466 * FINISHME: declarations. It's not 100% clear whether this is
2467 * FINISHME: required or not.
2468 */
2469
2470 const unsigned size = unsigned(var->type->array_size());
2471 check_builtin_array_max_size(var->name, size, loc, state);
2472 if ((size > 0) && (size <= earlier->max_array_access)) {
2473 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
2474 "previous access",
2475 earlier->max_array_access);
2476 }
2477
2478 earlier->type = var->type;
2479 delete var;
2480 var = NULL;
2481 } else if ((state->ARB_fragment_coord_conventions_enable ||
2482 state->is_version(150, 0))
2483 && strcmp(var->name, "gl_FragCoord") == 0
2484 && earlier->type == var->type
2485 && earlier->mode == var->mode) {
2486 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2487 * qualifiers.
2488 */
2489 earlier->origin_upper_left = var->origin_upper_left;
2490 earlier->pixel_center_integer = var->pixel_center_integer;
2491
2492 /* According to section 4.3.7 of the GLSL 1.30 spec,
2493 * the following built-in varaibles can be redeclared with an
2494 * interpolation qualifier:
2495 * * gl_FrontColor
2496 * * gl_BackColor
2497 * * gl_FrontSecondaryColor
2498 * * gl_BackSecondaryColor
2499 * * gl_Color
2500 * * gl_SecondaryColor
2501 */
2502 } else if (state->is_version(130, 0)
2503 && (strcmp(var->name, "gl_FrontColor") == 0
2504 || strcmp(var->name, "gl_BackColor") == 0
2505 || strcmp(var->name, "gl_FrontSecondaryColor") == 0
2506 || strcmp(var->name, "gl_BackSecondaryColor") == 0
2507 || strcmp(var->name, "gl_Color") == 0
2508 || strcmp(var->name, "gl_SecondaryColor") == 0)
2509 && earlier->type == var->type
2510 && earlier->mode == var->mode) {
2511 earlier->interpolation = var->interpolation;
2512
2513 /* Layout qualifiers for gl_FragDepth. */
2514 } else if ((state->AMD_conservative_depth_enable ||
2515 state->ARB_conservative_depth_enable)
2516 && strcmp(var->name, "gl_FragDepth") == 0
2517 && earlier->type == var->type
2518 && earlier->mode == var->mode) {
2519
2520 /** From the AMD_conservative_depth spec:
2521 * Within any shader, the first redeclarations of gl_FragDepth
2522 * must appear before any use of gl_FragDepth.
2523 */
2524 if (earlier->used) {
2525 _mesa_glsl_error(&loc, state,
2526 "the first redeclaration of gl_FragDepth "
2527 "must appear before any use of gl_FragDepth");
2528 }
2529
2530 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2531 if (earlier->depth_layout != ir_depth_layout_none
2532 && earlier->depth_layout != var->depth_layout) {
2533 _mesa_glsl_error(&loc, state,
2534 "gl_FragDepth: depth layout is declared here "
2535 "as '%s, but it was previously declared as "
2536 "'%s'",
2537 depth_layout_string(var->depth_layout),
2538 depth_layout_string(earlier->depth_layout));
2539 }
2540
2541 earlier->depth_layout = var->depth_layout;
2542
2543 } else if (allow_all_redeclarations) {
2544 if (earlier->mode != var->mode) {
2545 _mesa_glsl_error(&loc, state,
2546 "redeclaration of `%s' with incorrect qualifiers",
2547 var->name);
2548 } else if (earlier->type != var->type) {
2549 _mesa_glsl_error(&loc, state,
2550 "redeclaration of `%s' has incorrect type",
2551 var->name);
2552 }
2553 } else {
2554 _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
2555 }
2556
2557 return earlier;
2558 }
2559
2560 /**
2561 * Generate the IR for an initializer in a variable declaration
2562 */
2563 ir_rvalue *
2564 process_initializer(ir_variable *var, ast_declaration *decl,
2565 ast_fully_specified_type *type,
2566 exec_list *initializer_instructions,
2567 struct _mesa_glsl_parse_state *state)
2568 {
2569 ir_rvalue *result = NULL;
2570
2571 YYLTYPE initializer_loc = decl->initializer->get_location();
2572
2573 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2574 *
2575 * "All uniform variables are read-only and are initialized either
2576 * directly by an application via API commands, or indirectly by
2577 * OpenGL."
2578 */
2579 if (var->mode == ir_var_uniform) {
2580 state->check_version(120, 0, &initializer_loc,
2581 "cannot initialize uniforms");
2582 }
2583
2584 if (var->type->is_sampler()) {
2585 _mesa_glsl_error(& initializer_loc, state,
2586 "cannot initialize samplers");
2587 }
2588
2589 if ((var->mode == ir_var_shader_in) && (state->current_function == NULL)) {
2590 _mesa_glsl_error(& initializer_loc, state,
2591 "cannot initialize %s shader input / %s",
2592 _mesa_glsl_shader_target_name(state->target),
2593 (state->target == vertex_shader)
2594 ? "attribute" : "varying");
2595 }
2596
2597 ir_dereference *const lhs = new(state) ir_dereference_variable(var);
2598 ir_rvalue *rhs = decl->initializer->hir(initializer_instructions,
2599 state);
2600
2601 /* Calculate the constant value if this is a const or uniform
2602 * declaration.
2603 */
2604 if (type->qualifier.flags.q.constant
2605 || type->qualifier.flags.q.uniform) {
2606 ir_rvalue *new_rhs = validate_assignment(state, initializer_loc,
2607 var->type, rhs, true);
2608 if (new_rhs != NULL) {
2609 rhs = new_rhs;
2610
2611 ir_constant *constant_value = rhs->constant_expression_value();
2612 if (!constant_value) {
2613 /* If ARB_shading_language_420pack is enabled, initializers of
2614 * const-qualified local variables do not have to be constant
2615 * expressions. Const-qualified global variables must still be
2616 * initialized with constant expressions.
2617 */
2618 if (!state->ARB_shading_language_420pack_enable
2619 || state->current_function == NULL) {
2620 _mesa_glsl_error(& initializer_loc, state,
2621 "initializer of %s variable `%s' must be a "
2622 "constant expression",
2623 (type->qualifier.flags.q.constant)
2624 ? "const" : "uniform",
2625 decl->identifier);
2626 if (var->type->is_numeric()) {
2627 /* Reduce cascading errors. */
2628 var->constant_value = ir_constant::zero(state, var->type);
2629 }
2630 }
2631 } else {
2632 rhs = constant_value;
2633 var->constant_value = constant_value;
2634 }
2635 } else {
2636 if (var->type->is_numeric()) {
2637 /* Reduce cascading errors. */
2638 var->constant_value = ir_constant::zero(state, var->type);
2639 }
2640 }
2641 }
2642
2643 if (rhs && !rhs->type->is_error()) {
2644 bool temp = var->read_only;
2645 if (type->qualifier.flags.q.constant)
2646 var->read_only = false;
2647
2648 /* Never emit code to initialize a uniform.
2649 */
2650 const glsl_type *initializer_type;
2651 if (!type->qualifier.flags.q.uniform) {
2652 result = do_assignment(initializer_instructions, state,
2653 NULL,
2654 lhs, rhs, true,
2655 type->get_location());
2656 initializer_type = result->type;
2657 } else
2658 initializer_type = rhs->type;
2659
2660 var->constant_initializer = rhs->constant_expression_value();
2661 var->has_initializer = true;
2662
2663 /* If the declared variable is an unsized array, it must inherrit
2664 * its full type from the initializer. A declaration such as
2665 *
2666 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
2667 *
2668 * becomes
2669 *
2670 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
2671 *
2672 * The assignment generated in the if-statement (below) will also
2673 * automatically handle this case for non-uniforms.
2674 *
2675 * If the declared variable is not an array, the types must
2676 * already match exactly. As a result, the type assignment
2677 * here can be done unconditionally. For non-uniforms the call
2678 * to do_assignment can change the type of the initializer (via
2679 * the implicit conversion rules). For uniforms the initializer
2680 * must be a constant expression, and the type of that expression
2681 * was validated above.
2682 */
2683 var->type = initializer_type;
2684
2685 var->read_only = temp;
2686 }
2687
2688 return result;
2689 }
2690
2691
2692 /**
2693 * Do additional processing necessary for geometry shader input declarations
2694 * (this covers both interface blocks arrays and bare input variables).
2695 */
2696 static void
2697 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state *state,
2698 YYLTYPE loc, ir_variable *var)
2699 {
2700 unsigned num_vertices = 0;
2701 if (state->gs_input_prim_type_specified) {
2702 num_vertices = vertices_per_prim(state->gs_input_prim_type);
2703 }
2704
2705 /* Geometry shader input variables must be arrays. Caller should have
2706 * reported an error for this.
2707 */
2708 if (!var->type->is_array()) {
2709 assert(state->error);
2710
2711 /* To avoid cascading failures, short circuit the checks below. */
2712 return;
2713 }
2714
2715 if (var->type->is_unsized_array()) {
2716 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
2717 *
2718 * All geometry shader input unsized array declarations will be
2719 * sized by an earlier input layout qualifier, when present, as per
2720 * the following table.
2721 *
2722 * Followed by a table mapping each allowed input layout qualifier to
2723 * the corresponding input length.
2724 */
2725 if (num_vertices != 0)
2726 var->type = glsl_type::get_array_instance(var->type->fields.array,
2727 num_vertices);
2728 } else {
2729 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
2730 * includes the following examples of compile-time errors:
2731 *
2732 * // code sequence within one shader...
2733 * in vec4 Color1[]; // size unknown
2734 * ...Color1.length()...// illegal, length() unknown
2735 * in vec4 Color2[2]; // size is 2
2736 * ...Color1.length()...// illegal, Color1 still has no size
2737 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
2738 * layout(lines) in; // legal, input size is 2, matching
2739 * in vec4 Color4[3]; // illegal, contradicts layout
2740 * ...
2741 *
2742 * To detect the case illustrated by Color3, we verify that the size of
2743 * an explicitly-sized array matches the size of any previously declared
2744 * explicitly-sized array. To detect the case illustrated by Color4, we
2745 * verify that the size of an explicitly-sized array is consistent with
2746 * any previously declared input layout.
2747 */
2748 if (num_vertices != 0 && var->type->length != num_vertices) {
2749 _mesa_glsl_error(&loc, state,
2750 "geometry shader input size contradicts previously"
2751 " declared layout (size is %u, but layout requires a"
2752 " size of %u)", var->type->length, num_vertices);
2753 } else if (state->gs_input_size != 0 &&
2754 var->type->length != state->gs_input_size) {
2755 _mesa_glsl_error(&loc, state,
2756 "geometry shader input sizes are "
2757 "inconsistent (size is %u, but a previous "
2758 "declaration has size %u)",
2759 var->type->length, state->gs_input_size);
2760 } else {
2761 state->gs_input_size = var->type->length;
2762 }
2763 }
2764 }
2765
2766
2767 void
2768 validate_identifier(const char *identifier, YYLTYPE loc,
2769 struct _mesa_glsl_parse_state *state)
2770 {
2771 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2772 *
2773 * "Identifiers starting with "gl_" are reserved for use by
2774 * OpenGL, and may not be declared in a shader as either a
2775 * variable or a function."
2776 */
2777 if (strncmp(identifier, "gl_", 3) == 0) {
2778 _mesa_glsl_error(&loc, state,
2779 "identifier `%s' uses reserved `gl_' prefix",
2780 identifier);
2781 } else if (strstr(identifier, "__")) {
2782 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
2783 * spec:
2784 *
2785 * "In addition, all identifiers containing two
2786 * consecutive underscores (__) are reserved as
2787 * possible future keywords."
2788 */
2789 _mesa_glsl_error(&loc, state,
2790 "identifier `%s' uses reserved `__' string",
2791 identifier);
2792 }
2793 }
2794
2795
2796 ir_rvalue *
2797 ast_declarator_list::hir(exec_list *instructions,
2798 struct _mesa_glsl_parse_state *state)
2799 {
2800 void *ctx = state;
2801 const struct glsl_type *decl_type;
2802 const char *type_name = NULL;
2803 ir_rvalue *result = NULL;
2804 YYLTYPE loc = this->get_location();
2805
2806 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
2807 *
2808 * "To ensure that a particular output variable is invariant, it is
2809 * necessary to use the invariant qualifier. It can either be used to
2810 * qualify a previously declared variable as being invariant
2811 *
2812 * invariant gl_Position; // make existing gl_Position be invariant"
2813 *
2814 * In these cases the parser will set the 'invariant' flag in the declarator
2815 * list, and the type will be NULL.
2816 */
2817 if (this->invariant) {
2818 assert(this->type == NULL);
2819
2820 if (state->current_function != NULL) {
2821 _mesa_glsl_error(& loc, state,
2822 "all uses of `invariant' keyword must be at global "
2823 "scope");
2824 }
2825
2826 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
2827 assert(!decl->is_array);
2828 assert(decl->array_size == NULL);
2829 assert(decl->initializer == NULL);
2830
2831 ir_variable *const earlier =
2832 state->symbols->get_variable(decl->identifier);
2833 if (earlier == NULL) {
2834 _mesa_glsl_error(& loc, state,
2835 "undeclared variable `%s' cannot be marked "
2836 "invariant", decl->identifier);
2837 } else if ((state->target == vertex_shader)
2838 && (earlier->mode != ir_var_shader_out)) {
2839 _mesa_glsl_error(& loc, state,
2840 "`%s' cannot be marked invariant, vertex shader "
2841 "outputs only", decl->identifier);
2842 } else if ((state->target == fragment_shader)
2843 && (earlier->mode != ir_var_shader_in)) {
2844 _mesa_glsl_error(& loc, state,
2845 "`%s' cannot be marked invariant, fragment shader "
2846 "inputs only", decl->identifier);
2847 } else if (earlier->used) {
2848 _mesa_glsl_error(& loc, state,
2849 "variable `%s' may not be redeclared "
2850 "`invariant' after being used",
2851 earlier->name);
2852 } else {
2853 earlier->invariant = true;
2854 }
2855 }
2856
2857 /* Invariant redeclarations do not have r-values.
2858 */
2859 return NULL;
2860 }
2861
2862 assert(this->type != NULL);
2863 assert(!this->invariant);
2864
2865 /* The type specifier may contain a structure definition. Process that
2866 * before any of the variable declarations.
2867 */
2868 (void) this->type->specifier->hir(instructions, state);
2869
2870 decl_type = this->type->glsl_type(& type_name, state);
2871
2872 /* An offset-qualified atomic counter declaration sets the default
2873 * offset for the next declaration within the same atomic counter
2874 * buffer.
2875 */
2876 if (decl_type && decl_type->contains_atomic()) {
2877 if (type->qualifier.flags.q.explicit_binding &&
2878 type->qualifier.flags.q.explicit_offset)
2879 state->atomic_counter_offsets[type->qualifier.binding] =
2880 type->qualifier.offset;
2881 }
2882
2883 if (this->declarations.is_empty()) {
2884 /* If there is no structure involved in the program text, there are two
2885 * possible scenarios:
2886 *
2887 * - The program text contained something like 'vec4;'. This is an
2888 * empty declaration. It is valid but weird. Emit a warning.
2889 *
2890 * - The program text contained something like 'S;' and 'S' is not the
2891 * name of a known structure type. This is both invalid and weird.
2892 * Emit an error.
2893 *
2894 * - The program text contained something like 'mediump float;'
2895 * when the programmer probably meant 'precision mediump
2896 * float;' Emit a warning with a description of what they
2897 * probably meant to do.
2898 *
2899 * Note that if decl_type is NULL and there is a structure involved,
2900 * there must have been some sort of error with the structure. In this
2901 * case we assume that an error was already generated on this line of
2902 * code for the structure. There is no need to generate an additional,
2903 * confusing error.
2904 */
2905 assert(this->type->specifier->structure == NULL || decl_type != NULL
2906 || state->error);
2907
2908 if (decl_type == NULL) {
2909 _mesa_glsl_error(&loc, state,
2910 "invalid type `%s' in empty declaration",
2911 type_name);
2912 } else if (decl_type->base_type == GLSL_TYPE_ATOMIC_UINT) {
2913 /* Empty atomic counter declarations are allowed and useful
2914 * to set the default offset qualifier.
2915 */
2916 return NULL;
2917 } else if (this->type->qualifier.precision != ast_precision_none) {
2918 if (this->type->specifier->structure != NULL) {
2919 _mesa_glsl_error(&loc, state,
2920 "precision qualifiers can't be applied "
2921 "to structures");
2922 } else {
2923 static const char *const precision_names[] = {
2924 "highp",
2925 "highp",
2926 "mediump",
2927 "lowp"
2928 };
2929
2930 _mesa_glsl_warning(&loc, state,
2931 "empty declaration with precision qualifier, "
2932 "to set the default precision, use "
2933 "`precision %s %s;'",
2934 precision_names[this->type->qualifier.precision],
2935 type_name);
2936 }
2937 } else {
2938 _mesa_glsl_warning(&loc, state, "empty declaration");
2939 }
2940 }
2941
2942 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
2943 const struct glsl_type *var_type;
2944 ir_variable *var;
2945
2946 /* FINISHME: Emit a warning if a variable declaration shadows a
2947 * FINISHME: declaration at a higher scope.
2948 */
2949
2950 if ((decl_type == NULL) || decl_type->is_void()) {
2951 if (type_name != NULL) {
2952 _mesa_glsl_error(& loc, state,
2953 "invalid type `%s' in declaration of `%s'",
2954 type_name, decl->identifier);
2955 } else {
2956 _mesa_glsl_error(& loc, state,
2957 "invalid type in declaration of `%s'",
2958 decl->identifier);
2959 }
2960 continue;
2961 }
2962
2963 if (decl->is_array) {
2964 var_type = process_array_type(&loc, decl_type, decl->array_size,
2965 state);
2966 if (var_type->is_error())
2967 continue;
2968 } else {
2969 var_type = decl_type;
2970 }
2971
2972 var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
2973
2974 /* The 'varying in' and 'varying out' qualifiers can only be used with
2975 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
2976 * yet.
2977 */
2978 if (this->type->qualifier.flags.q.varying) {
2979 if (this->type->qualifier.flags.q.in) {
2980 _mesa_glsl_error(& loc, state,
2981 "`varying in' qualifier in declaration of "
2982 "`%s' only valid for geometry shaders using "
2983 "ARB_geometry_shader4 or EXT_geometry_shader4",
2984 decl->identifier);
2985 } else if (this->type->qualifier.flags.q.out) {
2986 _mesa_glsl_error(& loc, state,
2987 "`varying out' qualifier in declaration of "
2988 "`%s' only valid for geometry shaders using "
2989 "ARB_geometry_shader4 or EXT_geometry_shader4",
2990 decl->identifier);
2991 }
2992 }
2993
2994 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
2995 *
2996 * "Global variables can only use the qualifiers const,
2997 * attribute, uni form, or varying. Only one may be
2998 * specified.
2999 *
3000 * Local variables can only use the qualifier const."
3001 *
3002 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
3003 * any extension that adds the 'layout' keyword.
3004 */
3005 if (!state->is_version(130, 300)
3006 && !state->has_explicit_attrib_location()
3007 && !state->ARB_fragment_coord_conventions_enable) {
3008 if (this->type->qualifier.flags.q.out) {
3009 _mesa_glsl_error(& loc, state,
3010 "`out' qualifier in declaration of `%s' "
3011 "only valid for function parameters in %s",
3012 decl->identifier, state->get_version_string());
3013 }
3014 if (this->type->qualifier.flags.q.in) {
3015 _mesa_glsl_error(& loc, state,
3016 "`in' qualifier in declaration of `%s' "
3017 "only valid for function parameters in %s",
3018 decl->identifier, state->get_version_string());
3019 }
3020 /* FINISHME: Test for other invalid qualifiers. */
3021 }
3022
3023 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
3024 & loc, false);
3025
3026 if (this->type->qualifier.flags.q.invariant) {
3027 if ((state->target == vertex_shader) &&
3028 var->mode != ir_var_shader_out) {
3029 _mesa_glsl_error(& loc, state,
3030 "`%s' cannot be marked invariant, vertex shader "
3031 "outputs only", var->name);
3032 } else if ((state->target == fragment_shader) &&
3033 var->mode != ir_var_shader_in) {
3034 /* FINISHME: Note that this doesn't work for invariant on
3035 * a function signature inval
3036 */
3037 _mesa_glsl_error(& loc, state,
3038 "`%s' cannot be marked invariant, fragment shader "
3039 "inputs only", var->name);
3040 }
3041 }
3042
3043 if (state->current_function != NULL) {
3044 const char *mode = NULL;
3045 const char *extra = "";
3046
3047 /* There is no need to check for 'inout' here because the parser will
3048 * only allow that in function parameter lists.
3049 */
3050 if (this->type->qualifier.flags.q.attribute) {
3051 mode = "attribute";
3052 } else if (this->type->qualifier.flags.q.uniform) {
3053 mode = "uniform";
3054 } else if (this->type->qualifier.flags.q.varying) {
3055 mode = "varying";
3056 } else if (this->type->qualifier.flags.q.in) {
3057 mode = "in";
3058 extra = " or in function parameter list";
3059 } else if (this->type->qualifier.flags.q.out) {
3060 mode = "out";
3061 extra = " or in function parameter list";
3062 }
3063
3064 if (mode) {
3065 _mesa_glsl_error(& loc, state,
3066 "%s variable `%s' must be declared at "
3067 "global scope%s",
3068 mode, var->name, extra);
3069 }
3070 } else if (var->mode == ir_var_shader_in) {
3071 var->read_only = true;
3072
3073 if (state->target == vertex_shader) {
3074 bool error_emitted = false;
3075
3076 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
3077 *
3078 * "Vertex shader inputs can only be float, floating-point
3079 * vectors, matrices, signed and unsigned integers and integer
3080 * vectors. Vertex shader inputs can also form arrays of these
3081 * types, but not structures."
3082 *
3083 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
3084 *
3085 * "Vertex shader inputs can only be float, floating-point
3086 * vectors, matrices, signed and unsigned integers and integer
3087 * vectors. They cannot be arrays or structures."
3088 *
3089 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
3090 *
3091 * "The attribute qualifier can be used only with float,
3092 * floating-point vectors, and matrices. Attribute variables
3093 * cannot be declared as arrays or structures."
3094 *
3095 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
3096 *
3097 * "Vertex shader inputs can only be float, floating-point
3098 * vectors, matrices, signed and unsigned integers and integer
3099 * vectors. Vertex shader inputs cannot be arrays or
3100 * structures."
3101 */
3102 const glsl_type *check_type = var->type->is_array()
3103 ? var->type->fields.array : var->type;
3104
3105 switch (check_type->base_type) {
3106 case GLSL_TYPE_FLOAT:
3107 break;
3108 case GLSL_TYPE_UINT:
3109 case GLSL_TYPE_INT:
3110 if (state->is_version(120, 300))
3111 break;
3112 /* FALLTHROUGH */
3113 default:
3114 _mesa_glsl_error(& loc, state,
3115 "vertex shader input / attribute cannot have "
3116 "type %s`%s'",
3117 var->type->is_array() ? "array of " : "",
3118 check_type->name);
3119 error_emitted = true;
3120 }
3121
3122 if (!error_emitted && var->type->is_array() &&
3123 !state->check_version(150, 0, &loc,
3124 "vertex shader input / attribute "
3125 "cannot have array type")) {
3126 error_emitted = true;
3127 }
3128 } else if (state->target == geometry_shader) {
3129 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
3130 *
3131 * Geometry shader input variables get the per-vertex values
3132 * written out by vertex shader output variables of the same
3133 * names. Since a geometry shader operates on a set of
3134 * vertices, each input varying variable (or input block, see
3135 * interface blocks below) needs to be declared as an array.
3136 */
3137 if (!var->type->is_array()) {
3138 _mesa_glsl_error(&loc, state,
3139 "geometry shader inputs must be arrays");
3140 }
3141
3142 handle_geometry_shader_input_decl(state, loc, var);
3143 }
3144 }
3145
3146 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3147 * so must integer vertex outputs.
3148 *
3149 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3150 * "Fragment shader inputs that are signed or unsigned integers or
3151 * integer vectors must be qualified with the interpolation qualifier
3152 * flat."
3153 *
3154 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3155 * "Fragment shader inputs that are, or contain, signed or unsigned
3156 * integers or integer vectors must be qualified with the
3157 * interpolation qualifier flat."
3158 *
3159 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3160 * "Vertex shader outputs that are, or contain, signed or unsigned
3161 * integers or integer vectors must be qualified with the
3162 * interpolation qualifier flat."
3163 *
3164 * Note that prior to GLSL 1.50, this requirement applied to vertex
3165 * outputs rather than fragment inputs. That creates problems in the
3166 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3167 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3168 * apply the restriction to both vertex outputs and fragment inputs.
3169 *
3170 * Note also that the desktop GLSL specs are missing the text "or
3171 * contain"; this is presumably an oversight, since there is no
3172 * reasonable way to interpolate a fragment shader input that contains
3173 * an integer.
3174 */
3175 if (state->is_version(130, 300) &&
3176 var->type->contains_integer() &&
3177 var->interpolation != INTERP_QUALIFIER_FLAT &&
3178 ((state->target == fragment_shader && var->mode == ir_var_shader_in)
3179 || (state->target == vertex_shader && var->mode == ir_var_shader_out
3180 && state->es_shader))) {
3181 const char *var_type = (state->target == vertex_shader) ?
3182 "vertex output" : "fragment input";
3183 _mesa_glsl_error(&loc, state, "if a %s is (or contains) "
3184 "an integer, then it must be qualified with 'flat'",
3185 var_type);
3186 }
3187
3188
3189 /* Interpolation qualifiers cannot be applied to 'centroid' and
3190 * 'centroid varying'.
3191 *
3192 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3193 * "interpolation qualifiers may only precede the qualifiers in,
3194 * centroid in, out, or centroid out in a declaration. They do not apply
3195 * to the deprecated storage qualifiers varying or centroid varying."
3196 *
3197 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3198 */
3199 if (state->is_version(130, 0)
3200 && this->type->qualifier.has_interpolation()
3201 && this->type->qualifier.flags.q.varying) {
3202
3203 const char *i = this->type->qualifier.interpolation_string();
3204 assert(i != NULL);
3205 const char *s;
3206 if (this->type->qualifier.flags.q.centroid)
3207 s = "centroid varying";
3208 else
3209 s = "varying";
3210
3211 _mesa_glsl_error(&loc, state,
3212 "qualifier '%s' cannot be applied to the "
3213 "deprecated storage qualifier '%s'", i, s);
3214 }
3215
3216
3217 /* Interpolation qualifiers can only apply to vertex shader outputs and
3218 * fragment shader inputs.
3219 *
3220 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
3221 * "Outputs from a vertex shader (out) and inputs to a fragment
3222 * shader (in) can be further qualified with one or more of these
3223 * interpolation qualifiers"
3224 *
3225 * From page 31 (page 37 of the PDF) of the GLSL ES 3.00 spec:
3226 * "These interpolation qualifiers may only precede the qualifiers
3227 * in, centroid in, out, or centroid out in a declaration. They do
3228 * not apply to inputs into a vertex shader or outputs from a
3229 * fragment shader."
3230 */
3231 if (state->is_version(130, 300)
3232 && this->type->qualifier.has_interpolation()) {
3233
3234 const char *i = this->type->qualifier.interpolation_string();
3235 assert(i != NULL);
3236
3237 switch (state->target) {
3238 case vertex_shader:
3239 if (this->type->qualifier.flags.q.in) {
3240 _mesa_glsl_error(&loc, state,
3241 "qualifier '%s' cannot be applied to vertex "
3242 "shader inputs", i);
3243 }
3244 break;
3245 case fragment_shader:
3246 if (this->type->qualifier.flags.q.out) {
3247 _mesa_glsl_error(&loc, state,
3248 "qualifier '%s' cannot be applied to fragment "
3249 "shader outputs", i);
3250 }
3251 break;
3252 default:
3253 break;
3254 }
3255 }
3256
3257
3258 /* From section 4.3.4 of the GLSL 1.30 spec:
3259 * "It is an error to use centroid in in a vertex shader."
3260 *
3261 * From section 4.3.4 of the GLSL ES 3.00 spec:
3262 * "It is an error to use centroid in or interpolation qualifiers in
3263 * a vertex shader input."
3264 */
3265 if (state->is_version(130, 300)
3266 && this->type->qualifier.flags.q.centroid
3267 && this->type->qualifier.flags.q.in
3268 && state->target == vertex_shader) {
3269
3270 _mesa_glsl_error(&loc, state,
3271 "'centroid in' cannot be used in a vertex shader");
3272 }
3273
3274 /* Section 4.3.6 of the GLSL 1.30 specification states:
3275 * "It is an error to use centroid out in a fragment shader."
3276 *
3277 * The GL_ARB_shading_language_420pack extension specification states:
3278 * "It is an error to use auxiliary storage qualifiers or interpolation
3279 * qualifiers on an output in a fragment shader."
3280 */
3281 if (state->target == fragment_shader &&
3282 this->type->qualifier.flags.q.out &&
3283 this->type->qualifier.has_auxiliary_storage()) {
3284 _mesa_glsl_error(&loc, state,
3285 "auxiliary storage qualifiers cannot be used on "
3286 "fragment shader outputs");
3287 }
3288
3289 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
3290 */
3291 if (this->type->qualifier.precision != ast_precision_none) {
3292 state->check_precision_qualifiers_allowed(&loc);
3293 }
3294
3295
3296 /* Precision qualifiers apply to floating point, integer and sampler
3297 * types.
3298 *
3299 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
3300 * "Any floating point or any integer declaration can have the type
3301 * preceded by one of these precision qualifiers [...] Literal
3302 * constants do not have precision qualifiers. Neither do Boolean
3303 * variables.
3304 *
3305 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
3306 * spec also says:
3307 *
3308 * "Precision qualifiers are added for code portability with OpenGL
3309 * ES, not for functionality. They have the same syntax as in OpenGL
3310 * ES."
3311 *
3312 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
3313 *
3314 * "uniform lowp sampler2D sampler;
3315 * highp vec2 coord;
3316 * ...
3317 * lowp vec4 col = texture2D (sampler, coord);
3318 * // texture2D returns lowp"
3319 *
3320 * From this, we infer that GLSL 1.30 (and later) should allow precision
3321 * qualifiers on sampler types just like float and integer types.
3322 */
3323 if (this->type->qualifier.precision != ast_precision_none
3324 && !var->type->is_float()
3325 && !var->type->is_integer()
3326 && !var->type->is_record()
3327 && !var->type->is_sampler()
3328 && !(var->type->is_array()
3329 && (var->type->fields.array->is_float()
3330 || var->type->fields.array->is_integer()))) {
3331
3332 _mesa_glsl_error(&loc, state,
3333 "precision qualifiers apply only to floating point"
3334 ", integer and sampler types");
3335 }
3336
3337 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3338 *
3339 * "[Sampler types] can only be declared as function
3340 * parameters or uniform variables (see Section 4.3.5
3341 * "Uniform")".
3342 */
3343 if (var_type->contains_sampler() &&
3344 !this->type->qualifier.flags.q.uniform) {
3345 _mesa_glsl_error(&loc, state, "samplers must be declared uniform");
3346 }
3347
3348 /* Process the initializer and add its instructions to a temporary
3349 * list. This list will be added to the instruction stream (below) after
3350 * the declaration is added. This is done because in some cases (such as
3351 * redeclarations) the declaration may not actually be added to the
3352 * instruction stream.
3353 */
3354 exec_list initializer_instructions;
3355 ir_variable *earlier =
3356 get_variable_being_redeclared(var, decl->get_location(), state,
3357 false /* allow_all_redeclarations */);
3358
3359 if (decl->initializer != NULL) {
3360 result = process_initializer((earlier == NULL) ? var : earlier,
3361 decl, this->type,
3362 &initializer_instructions, state);
3363 }
3364
3365 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
3366 *
3367 * "It is an error to write to a const variable outside of
3368 * its declaration, so they must be initialized when
3369 * declared."
3370 */
3371 if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
3372 _mesa_glsl_error(& loc, state,
3373 "const declaration of `%s' must be initialized",
3374 decl->identifier);
3375 }
3376
3377 if (state->es_shader) {
3378 const glsl_type *const t = (earlier == NULL)
3379 ? var->type : earlier->type;
3380
3381 if (t->is_unsized_array())
3382 /* Section 10.17 of the GLSL ES 1.00 specification states that
3383 * unsized array declarations have been removed from the language.
3384 * Arrays that are sized using an initializer are still explicitly
3385 * sized. However, GLSL ES 1.00 does not allow array
3386 * initializers. That is only allowed in GLSL ES 3.00.
3387 *
3388 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
3389 *
3390 * "An array type can also be formed without specifying a size
3391 * if the definition includes an initializer:
3392 *
3393 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
3394 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
3395 *
3396 * float a[5];
3397 * float b[] = a;"
3398 */
3399 _mesa_glsl_error(& loc, state,
3400 "unsized array declarations are not allowed in "
3401 "GLSL ES");
3402 }
3403
3404 /* If the declaration is not a redeclaration, there are a few additional
3405 * semantic checks that must be applied. In addition, variable that was
3406 * created for the declaration should be added to the IR stream.
3407 */
3408 if (earlier == NULL) {
3409 validate_identifier(decl->identifier, loc, state);
3410
3411 /* Add the variable to the symbol table. Note that the initializer's
3412 * IR was already processed earlier (though it hasn't been emitted
3413 * yet), without the variable in scope.
3414 *
3415 * This differs from most C-like languages, but it follows the GLSL
3416 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
3417 * spec:
3418 *
3419 * "Within a declaration, the scope of a name starts immediately
3420 * after the initializer if present or immediately after the name
3421 * being declared if not."
3422 */
3423 if (!state->symbols->add_variable(var)) {
3424 YYLTYPE loc = this->get_location();
3425 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
3426 "current scope", decl->identifier);
3427 continue;
3428 }
3429
3430 /* Push the variable declaration to the top. It means that all the
3431 * variable declarations will appear in a funny last-to-first order,
3432 * but otherwise we run into trouble if a function is prototyped, a
3433 * global var is decled, then the function is defined with usage of
3434 * the global var. See glslparsertest's CorrectModule.frag.
3435 */
3436 instructions->push_head(var);
3437 }
3438
3439 instructions->append_list(&initializer_instructions);
3440 }
3441
3442
3443 /* Generally, variable declarations do not have r-values. However,
3444 * one is used for the declaration in
3445 *
3446 * while (bool b = some_condition()) {
3447 * ...
3448 * }
3449 *
3450 * so we return the rvalue from the last seen declaration here.
3451 */
3452 return result;
3453 }
3454
3455
3456 ir_rvalue *
3457 ast_parameter_declarator::hir(exec_list *instructions,
3458 struct _mesa_glsl_parse_state *state)
3459 {
3460 void *ctx = state;
3461 const struct glsl_type *type;
3462 const char *name = NULL;
3463 YYLTYPE loc = this->get_location();
3464
3465 type = this->type->glsl_type(& name, state);
3466
3467 if (type == NULL) {
3468 if (name != NULL) {
3469 _mesa_glsl_error(& loc, state,
3470 "invalid type `%s' in declaration of `%s'",
3471 name, this->identifier);
3472 } else {
3473 _mesa_glsl_error(& loc, state,
3474 "invalid type in declaration of `%s'",
3475 this->identifier);
3476 }
3477
3478 type = glsl_type::error_type;
3479 }
3480
3481 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
3482 *
3483 * "Functions that accept no input arguments need not use void in the
3484 * argument list because prototypes (or definitions) are required and
3485 * therefore there is no ambiguity when an empty argument list "( )" is
3486 * declared. The idiom "(void)" as a parameter list is provided for
3487 * convenience."
3488 *
3489 * Placing this check here prevents a void parameter being set up
3490 * for a function, which avoids tripping up checks for main taking
3491 * parameters and lookups of an unnamed symbol.
3492 */
3493 if (type->is_void()) {
3494 if (this->identifier != NULL)
3495 _mesa_glsl_error(& loc, state,
3496 "named parameter cannot have type `void'");
3497
3498 is_void = true;
3499 return NULL;
3500 }
3501
3502 if (formal_parameter && (this->identifier == NULL)) {
3503 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
3504 return NULL;
3505 }
3506
3507 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
3508 * call already handled the "vec4[..] foo" case.
3509 */
3510 if (this->is_array) {
3511 type = process_array_type(&loc, type, this->array_size, state);
3512 }
3513
3514 if (!type->is_error() && type->is_unsized_array()) {
3515 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
3516 "a declared size");
3517 type = glsl_type::error_type;
3518 }
3519
3520 is_void = false;
3521 ir_variable *var = new(ctx)
3522 ir_variable(type, this->identifier, ir_var_function_in);
3523
3524 /* Apply any specified qualifiers to the parameter declaration. Note that
3525 * for function parameters the default mode is 'in'.
3526 */
3527 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc,
3528 true);
3529
3530 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3531 *
3532 * "Samplers cannot be treated as l-values; hence cannot be used
3533 * as out or inout function parameters, nor can they be assigned
3534 * into."
3535 */
3536 if ((var->mode == ir_var_function_inout || var->mode == ir_var_function_out)
3537 && type->contains_sampler()) {
3538 _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers");
3539 type = glsl_type::error_type;
3540 }
3541
3542 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
3543 *
3544 * "When calling a function, expressions that do not evaluate to
3545 * l-values cannot be passed to parameters declared as out or inout."
3546 *
3547 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
3548 *
3549 * "Other binary or unary expressions, non-dereferenced arrays,
3550 * function names, swizzles with repeated fields, and constants
3551 * cannot be l-values."
3552 *
3553 * So for GLSL 1.10, passing an array as an out or inout parameter is not
3554 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
3555 */
3556 if ((var->mode == ir_var_function_inout || var->mode == ir_var_function_out)
3557 && type->is_array()
3558 && !state->check_version(120, 100, &loc,
3559 "arrays cannot be out or inout parameters")) {
3560 type = glsl_type::error_type;
3561 }
3562
3563 instructions->push_tail(var);
3564
3565 /* Parameter declarations do not have r-values.
3566 */
3567 return NULL;
3568 }
3569
3570
3571 void
3572 ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
3573 bool formal,
3574 exec_list *ir_parameters,
3575 _mesa_glsl_parse_state *state)
3576 {
3577 ast_parameter_declarator *void_param = NULL;
3578 unsigned count = 0;
3579
3580 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
3581 param->formal_parameter = formal;
3582 param->hir(ir_parameters, state);
3583
3584 if (param->is_void)
3585 void_param = param;
3586
3587 count++;
3588 }
3589
3590 if ((void_param != NULL) && (count > 1)) {
3591 YYLTYPE loc = void_param->get_location();
3592
3593 _mesa_glsl_error(& loc, state,
3594 "`void' parameter must be only parameter");
3595 }
3596 }
3597
3598
3599 void
3600 emit_function(_mesa_glsl_parse_state *state, ir_function *f)
3601 {
3602 /* IR invariants disallow function declarations or definitions
3603 * nested within other function definitions. But there is no
3604 * requirement about the relative order of function declarations
3605 * and definitions with respect to one another. So simply insert
3606 * the new ir_function block at the end of the toplevel instruction
3607 * list.
3608 */
3609 state->toplevel_ir->push_tail(f);
3610 }
3611
3612
3613 ir_rvalue *
3614 ast_function::hir(exec_list *instructions,
3615 struct _mesa_glsl_parse_state *state)
3616 {
3617 void *ctx = state;
3618 ir_function *f = NULL;
3619 ir_function_signature *sig = NULL;
3620 exec_list hir_parameters;
3621
3622 const char *const name = identifier;
3623
3624 /* New functions are always added to the top-level IR instruction stream,
3625 * so this instruction list pointer is ignored. See also emit_function
3626 * (called below).
3627 */
3628 (void) instructions;
3629
3630 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
3631 *
3632 * "Function declarations (prototypes) cannot occur inside of functions;
3633 * they must be at global scope, or for the built-in functions, outside
3634 * the global scope."
3635 *
3636 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
3637 *
3638 * "User defined functions may only be defined within the global scope."
3639 *
3640 * Note that this language does not appear in GLSL 1.10.
3641 */
3642 if ((state->current_function != NULL) &&
3643 state->is_version(120, 100)) {
3644 YYLTYPE loc = this->get_location();
3645 _mesa_glsl_error(&loc, state,
3646 "declaration of function `%s' not allowed within "
3647 "function body", name);
3648 }
3649
3650 validate_identifier(name, this->get_location(), state);
3651
3652 /* Convert the list of function parameters to HIR now so that they can be
3653 * used below to compare this function's signature with previously seen
3654 * signatures for functions with the same name.
3655 */
3656 ast_parameter_declarator::parameters_to_hir(& this->parameters,
3657 is_definition,
3658 & hir_parameters, state);
3659
3660 const char *return_type_name;
3661 const glsl_type *return_type =
3662 this->return_type->glsl_type(& return_type_name, state);
3663
3664 if (!return_type) {
3665 YYLTYPE loc = this->get_location();
3666 _mesa_glsl_error(&loc, state,
3667 "function `%s' has undeclared return type `%s'",
3668 name, return_type_name);
3669 return_type = glsl_type::error_type;
3670 }
3671
3672 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3673 * "No qualifier is allowed on the return type of a function."
3674 */
3675 if (this->return_type->has_qualifiers()) {
3676 YYLTYPE loc = this->get_location();
3677 _mesa_glsl_error(& loc, state,
3678 "function `%s' return type has qualifiers", name);
3679 }
3680
3681 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
3682 *
3683 * "Arrays are allowed as arguments and as the return type. In both
3684 * cases, the array must be explicitly sized."
3685 */
3686 if (return_type->is_unsized_array()) {
3687 YYLTYPE loc = this->get_location();
3688 _mesa_glsl_error(& loc, state,
3689 "function `%s' return type array must be explicitly "
3690 "sized", name);
3691 }
3692
3693 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3694 *
3695 * "[Sampler types] can only be declared as function parameters
3696 * or uniform variables (see Section 4.3.5 "Uniform")".
3697 */
3698 if (return_type->contains_sampler()) {
3699 YYLTYPE loc = this->get_location();
3700 _mesa_glsl_error(&loc, state,
3701 "function `%s' return type can't contain a sampler",
3702 name);
3703 }
3704
3705 /* Verify that this function's signature either doesn't match a previously
3706 * seen signature for a function with the same name, or, if a match is found,
3707 * that the previously seen signature does not have an associated definition.
3708 */
3709 f = state->symbols->get_function(name);
3710 if (f != NULL && (state->es_shader || f->has_user_signature())) {
3711 sig = f->exact_matching_signature(state, &hir_parameters);
3712 if (sig != NULL) {
3713 const char *badvar = sig->qualifiers_match(&hir_parameters);
3714 if (badvar != NULL) {
3715 YYLTYPE loc = this->get_location();
3716
3717 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
3718 "qualifiers don't match prototype", name, badvar);
3719 }
3720
3721 if (sig->return_type != return_type) {
3722 YYLTYPE loc = this->get_location();
3723
3724 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
3725 "match prototype", name);
3726 }
3727
3728 if (sig->is_defined) {
3729 if (is_definition) {
3730 YYLTYPE loc = this->get_location();
3731 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
3732 } else {
3733 /* We just encountered a prototype that exactly matches a
3734 * function that's already been defined. This is redundant,
3735 * and we should ignore it.
3736 */
3737 return NULL;
3738 }
3739 }
3740 }
3741 } else {
3742 f = new(ctx) ir_function(name);
3743 if (!state->symbols->add_function(f)) {
3744 /* This function name shadows a non-function use of the same name. */
3745 YYLTYPE loc = this->get_location();
3746
3747 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
3748 "non-function", name);
3749 return NULL;
3750 }
3751
3752 emit_function(state, f);
3753 }
3754
3755 /* Verify the return type of main() */
3756 if (strcmp(name, "main") == 0) {
3757 if (! return_type->is_void()) {
3758 YYLTYPE loc = this->get_location();
3759
3760 _mesa_glsl_error(& loc, state, "main() must return void");
3761 }
3762
3763 if (!hir_parameters.is_empty()) {
3764 YYLTYPE loc = this->get_location();
3765
3766 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
3767 }
3768 }
3769
3770 /* Finish storing the information about this new function in its signature.
3771 */
3772 if (sig == NULL) {
3773 sig = new(ctx) ir_function_signature(return_type);
3774 f->add_signature(sig);
3775 }
3776
3777 sig->replace_parameters(&hir_parameters);
3778 signature = sig;
3779
3780 /* Function declarations (prototypes) do not have r-values.
3781 */
3782 return NULL;
3783 }
3784
3785
3786 ir_rvalue *
3787 ast_function_definition::hir(exec_list *instructions,
3788 struct _mesa_glsl_parse_state *state)
3789 {
3790 prototype->is_definition = true;
3791 prototype->hir(instructions, state);
3792
3793 ir_function_signature *signature = prototype->signature;
3794 if (signature == NULL)
3795 return NULL;
3796
3797 assert(state->current_function == NULL);
3798 state->current_function = signature;
3799 state->found_return = false;
3800
3801 /* Duplicate parameters declared in the prototype as concrete variables.
3802 * Add these to the symbol table.
3803 */
3804 state->symbols->push_scope();
3805 foreach_iter(exec_list_iterator, iter, signature->parameters) {
3806 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
3807
3808 assert(var != NULL);
3809
3810 /* The only way a parameter would "exist" is if two parameters have
3811 * the same name.
3812 */
3813 if (state->symbols->name_declared_this_scope(var->name)) {
3814 YYLTYPE loc = this->get_location();
3815
3816 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
3817 } else {
3818 state->symbols->add_variable(var);
3819 }
3820 }
3821
3822 /* Convert the body of the function to HIR. */
3823 this->body->hir(&signature->body, state);
3824 signature->is_defined = true;
3825
3826 state->symbols->pop_scope();
3827
3828 assert(state->current_function == signature);
3829 state->current_function = NULL;
3830
3831 if (!signature->return_type->is_void() && !state->found_return) {
3832 YYLTYPE loc = this->get_location();
3833 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
3834 "%s, but no return statement",
3835 signature->function_name(),
3836 signature->return_type->name);
3837 }
3838
3839 /* Function definitions do not have r-values.
3840 */
3841 return NULL;
3842 }
3843
3844
3845 ir_rvalue *
3846 ast_jump_statement::hir(exec_list *instructions,
3847 struct _mesa_glsl_parse_state *state)
3848 {
3849 void *ctx = state;
3850
3851 switch (mode) {
3852 case ast_return: {
3853 ir_return *inst;
3854 assert(state->current_function);
3855
3856 if (opt_return_value) {
3857 ir_rvalue *ret = opt_return_value->hir(instructions, state);
3858
3859 /* The value of the return type can be NULL if the shader says
3860 * 'return foo();' and foo() is a function that returns void.
3861 *
3862 * NOTE: The GLSL spec doesn't say that this is an error. The type
3863 * of the return value is void. If the return type of the function is
3864 * also void, then this should compile without error. Seriously.
3865 */
3866 const glsl_type *const ret_type =
3867 (ret == NULL) ? glsl_type::void_type : ret->type;
3868
3869 /* Implicit conversions are not allowed for return values prior to
3870 * ARB_shading_language_420pack.
3871 */
3872 if (state->current_function->return_type != ret_type) {
3873 YYLTYPE loc = this->get_location();
3874
3875 if (state->ARB_shading_language_420pack_enable) {
3876 if (!apply_implicit_conversion(state->current_function->return_type,
3877 ret, state)) {
3878 _mesa_glsl_error(& loc, state,
3879 "could not implicitly convert return value "
3880 "to %s, in function `%s'",
3881 state->current_function->return_type->name,
3882 state->current_function->function_name());
3883 }
3884 } else {
3885 _mesa_glsl_error(& loc, state,
3886 "`return' with wrong type %s, in function `%s' "
3887 "returning %s",
3888 ret_type->name,
3889 state->current_function->function_name(),
3890 state->current_function->return_type->name);
3891 }
3892 } else if (state->current_function->return_type->base_type ==
3893 GLSL_TYPE_VOID) {
3894 YYLTYPE loc = this->get_location();
3895
3896 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
3897 * specs add a clarification:
3898 *
3899 * "A void function can only use return without a return argument, even if
3900 * the return argument has void type. Return statements only accept values:
3901 *
3902 * void func1() { }
3903 * void func2() { return func1(); } // illegal return statement"
3904 */
3905 _mesa_glsl_error(& loc, state,
3906 "void functions can only use `return' without a "
3907 "return argument");
3908 }
3909
3910 inst = new(ctx) ir_return(ret);
3911 } else {
3912 if (state->current_function->return_type->base_type !=
3913 GLSL_TYPE_VOID) {
3914 YYLTYPE loc = this->get_location();
3915
3916 _mesa_glsl_error(& loc, state,
3917 "`return' with no value, in function %s returning "
3918 "non-void",
3919 state->current_function->function_name());
3920 }
3921 inst = new(ctx) ir_return;
3922 }
3923
3924 state->found_return = true;
3925 instructions->push_tail(inst);
3926 break;
3927 }
3928
3929 case ast_discard:
3930 if (state->target != fragment_shader) {
3931 YYLTYPE loc = this->get_location();
3932
3933 _mesa_glsl_error(& loc, state,
3934 "`discard' may only appear in a fragment shader");
3935 }
3936 instructions->push_tail(new(ctx) ir_discard);
3937 break;
3938
3939 case ast_break:
3940 case ast_continue:
3941 if (mode == ast_continue &&
3942 state->loop_nesting_ast == NULL) {
3943 YYLTYPE loc = this->get_location();
3944
3945 _mesa_glsl_error(& loc, state,
3946 "continue may only appear in a loop");
3947 } else if (mode == ast_break &&
3948 state->loop_nesting_ast == NULL &&
3949 state->switch_state.switch_nesting_ast == NULL) {
3950 YYLTYPE loc = this->get_location();
3951
3952 _mesa_glsl_error(& loc, state,
3953 "break may only appear in a loop or a switch");
3954 } else {
3955 /* For a loop, inline the for loop expression again,
3956 * since we don't know where near the end of
3957 * the loop body the normal copy of it
3958 * is going to be placed.
3959 */
3960 if (state->loop_nesting_ast != NULL &&
3961 mode == ast_continue &&
3962 state->loop_nesting_ast->rest_expression) {
3963 state->loop_nesting_ast->rest_expression->hir(instructions,
3964 state);
3965 }
3966
3967 if (state->switch_state.is_switch_innermost &&
3968 mode == ast_break) {
3969 /* Force break out of switch by setting is_break switch state.
3970 */
3971 ir_variable *const is_break_var = state->switch_state.is_break_var;
3972 ir_dereference_variable *const deref_is_break_var =
3973 new(ctx) ir_dereference_variable(is_break_var);
3974 ir_constant *const true_val = new(ctx) ir_constant(true);
3975 ir_assignment *const set_break_var =
3976 new(ctx) ir_assignment(deref_is_break_var, true_val);
3977
3978 instructions->push_tail(set_break_var);
3979 }
3980 else {
3981 ir_loop_jump *const jump =
3982 new(ctx) ir_loop_jump((mode == ast_break)
3983 ? ir_loop_jump::jump_break
3984 : ir_loop_jump::jump_continue);
3985 instructions->push_tail(jump);
3986 }
3987 }
3988
3989 break;
3990 }
3991
3992 /* Jump instructions do not have r-values.
3993 */
3994 return NULL;
3995 }
3996
3997
3998 ir_rvalue *
3999 ast_selection_statement::hir(exec_list *instructions,
4000 struct _mesa_glsl_parse_state *state)
4001 {
4002 void *ctx = state;
4003
4004 ir_rvalue *const condition = this->condition->hir(instructions, state);
4005
4006 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
4007 *
4008 * "Any expression whose type evaluates to a Boolean can be used as the
4009 * conditional expression bool-expression. Vector types are not accepted
4010 * as the expression to if."
4011 *
4012 * The checks are separated so that higher quality diagnostics can be
4013 * generated for cases where both rules are violated.
4014 */
4015 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
4016 YYLTYPE loc = this->condition->get_location();
4017
4018 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
4019 "boolean");
4020 }
4021
4022 ir_if *const stmt = new(ctx) ir_if(condition);
4023
4024 if (then_statement != NULL) {
4025 state->symbols->push_scope();
4026 then_statement->hir(& stmt->then_instructions, state);
4027 state->symbols->pop_scope();
4028 }
4029
4030 if (else_statement != NULL) {
4031 state->symbols->push_scope();
4032 else_statement->hir(& stmt->else_instructions, state);
4033 state->symbols->pop_scope();
4034 }
4035
4036 instructions->push_tail(stmt);
4037
4038 /* if-statements do not have r-values.
4039 */
4040 return NULL;
4041 }
4042
4043
4044 ir_rvalue *
4045 ast_switch_statement::hir(exec_list *instructions,
4046 struct _mesa_glsl_parse_state *state)
4047 {
4048 void *ctx = state;
4049
4050 ir_rvalue *const test_expression =
4051 this->test_expression->hir(instructions, state);
4052
4053 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
4054 *
4055 * "The type of init-expression in a switch statement must be a
4056 * scalar integer."
4057 */
4058 if (!test_expression->type->is_scalar() ||
4059 !test_expression->type->is_integer()) {
4060 YYLTYPE loc = this->test_expression->get_location();
4061
4062 _mesa_glsl_error(& loc,
4063 state,
4064 "switch-statement expression must be scalar "
4065 "integer");
4066 }
4067
4068 /* Track the switch-statement nesting in a stack-like manner.
4069 */
4070 struct glsl_switch_state saved = state->switch_state;
4071
4072 state->switch_state.is_switch_innermost = true;
4073 state->switch_state.switch_nesting_ast = this;
4074 state->switch_state.labels_ht = hash_table_ctor(0, hash_table_pointer_hash,
4075 hash_table_pointer_compare);
4076 state->switch_state.previous_default = NULL;
4077
4078 /* Initalize is_fallthru state to false.
4079 */
4080 ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
4081 state->switch_state.is_fallthru_var =
4082 new(ctx) ir_variable(glsl_type::bool_type,
4083 "switch_is_fallthru_tmp",
4084 ir_var_temporary);
4085 instructions->push_tail(state->switch_state.is_fallthru_var);
4086
4087 ir_dereference_variable *deref_is_fallthru_var =
4088 new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
4089 instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
4090 is_fallthru_val));
4091
4092 /* Initalize is_break state to false.
4093 */
4094 ir_rvalue *const is_break_val = new (ctx) ir_constant(false);
4095 state->switch_state.is_break_var = new(ctx) ir_variable(glsl_type::bool_type,
4096 "switch_is_break_tmp",
4097 ir_var_temporary);
4098 instructions->push_tail(state->switch_state.is_break_var);
4099
4100 ir_dereference_variable *deref_is_break_var =
4101 new(ctx) ir_dereference_variable(state->switch_state.is_break_var);
4102 instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var,
4103 is_break_val));
4104
4105 /* Cache test expression.
4106 */
4107 test_to_hir(instructions, state);
4108
4109 /* Emit code for body of switch stmt.
4110 */
4111 body->hir(instructions, state);
4112
4113 hash_table_dtor(state->switch_state.labels_ht);
4114
4115 state->switch_state = saved;
4116
4117 /* Switch statements do not have r-values. */
4118 return NULL;
4119 }
4120
4121
4122 void
4123 ast_switch_statement::test_to_hir(exec_list *instructions,
4124 struct _mesa_glsl_parse_state *state)
4125 {
4126 void *ctx = state;
4127
4128 /* Cache value of test expression. */
4129 ir_rvalue *const test_val =
4130 test_expression->hir(instructions,
4131 state);
4132
4133 state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
4134 "switch_test_tmp",
4135 ir_var_temporary);
4136 ir_dereference_variable *deref_test_var =
4137 new(ctx) ir_dereference_variable(state->switch_state.test_var);
4138
4139 instructions->push_tail(state->switch_state.test_var);
4140 instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val));
4141 }
4142
4143
4144 ir_rvalue *
4145 ast_switch_body::hir(exec_list *instructions,
4146 struct _mesa_glsl_parse_state *state)
4147 {
4148 if (stmts != NULL)
4149 stmts->hir(instructions, state);
4150
4151 /* Switch bodies do not have r-values. */
4152 return NULL;
4153 }
4154
4155 ir_rvalue *
4156 ast_case_statement_list::hir(exec_list *instructions,
4157 struct _mesa_glsl_parse_state *state)
4158 {
4159 foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases)
4160 case_stmt->hir(instructions, state);
4161
4162 /* Case statements do not have r-values. */
4163 return NULL;
4164 }
4165
4166 ir_rvalue *
4167 ast_case_statement::hir(exec_list *instructions,
4168 struct _mesa_glsl_parse_state *state)
4169 {
4170 labels->hir(instructions, state);
4171
4172 /* Conditionally set fallthru state based on break state. */
4173 ir_constant *const false_val = new(state) ir_constant(false);
4174 ir_dereference_variable *const deref_is_fallthru_var =
4175 new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
4176 ir_dereference_variable *const deref_is_break_var =
4177 new(state) ir_dereference_variable(state->switch_state.is_break_var);
4178 ir_assignment *const reset_fallthru_on_break =
4179 new(state) ir_assignment(deref_is_fallthru_var,
4180 false_val,
4181 deref_is_break_var);
4182 instructions->push_tail(reset_fallthru_on_break);
4183
4184 /* Guard case statements depending on fallthru state. */
4185 ir_dereference_variable *const deref_fallthru_guard =
4186 new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
4187 ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
4188
4189 foreach_list_typed (ast_node, stmt, link, & this->stmts)
4190 stmt->hir(& test_fallthru->then_instructions, state);
4191
4192 instructions->push_tail(test_fallthru);
4193
4194 /* Case statements do not have r-values. */
4195 return NULL;
4196 }
4197
4198
4199 ir_rvalue *
4200 ast_case_label_list::hir(exec_list *instructions,
4201 struct _mesa_glsl_parse_state *state)
4202 {
4203 foreach_list_typed (ast_case_label, label, link, & this->labels)
4204 label->hir(instructions, state);
4205
4206 /* Case labels do not have r-values. */
4207 return NULL;
4208 }
4209
4210 ir_rvalue *
4211 ast_case_label::hir(exec_list *instructions,
4212 struct _mesa_glsl_parse_state *state)
4213 {
4214 void *ctx = state;
4215
4216 ir_dereference_variable *deref_fallthru_var =
4217 new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
4218
4219 ir_rvalue *const true_val = new(ctx) ir_constant(true);
4220
4221 /* If not default case, ... */
4222 if (this->test_value != NULL) {
4223 /* Conditionally set fallthru state based on
4224 * comparison of cached test expression value to case label.
4225 */
4226 ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
4227 ir_constant *label_const = label_rval->constant_expression_value();
4228
4229 if (!label_const) {
4230 YYLTYPE loc = this->test_value->get_location();
4231
4232 _mesa_glsl_error(& loc, state,
4233 "switch statement case label must be a "
4234 "constant expression");
4235
4236 /* Stuff a dummy value in to allow processing to continue. */
4237 label_const = new(ctx) ir_constant(0);
4238 } else {
4239 ast_expression *previous_label = (ast_expression *)
4240 hash_table_find(state->switch_state.labels_ht,
4241 (void *)(uintptr_t)label_const->value.u[0]);
4242
4243 if (previous_label) {
4244 YYLTYPE loc = this->test_value->get_location();
4245 _mesa_glsl_error(& loc, state,
4246 "duplicate case value");
4247
4248 loc = previous_label->get_location();
4249 _mesa_glsl_error(& loc, state,
4250 "this is the previous case label");
4251 } else {
4252 hash_table_insert(state->switch_state.labels_ht,
4253 this->test_value,
4254 (void *)(uintptr_t)label_const->value.u[0]);
4255 }
4256 }
4257
4258 ir_dereference_variable *deref_test_var =
4259 new(ctx) ir_dereference_variable(state->switch_state.test_var);
4260
4261 ir_rvalue *const test_cond = new(ctx) ir_expression(ir_binop_all_equal,
4262 label_const,
4263 deref_test_var);
4264
4265 ir_assignment *set_fallthru_on_test =
4266 new(ctx) ir_assignment(deref_fallthru_var,
4267 true_val,
4268 test_cond);
4269
4270 instructions->push_tail(set_fallthru_on_test);
4271 } else { /* default case */
4272 if (state->switch_state.previous_default) {
4273 YYLTYPE loc = this->get_location();
4274 _mesa_glsl_error(& loc, state,
4275 "multiple default labels in one switch");
4276
4277 loc = state->switch_state.previous_default->get_location();
4278 _mesa_glsl_error(& loc, state,
4279 "this is the first default label");
4280 }
4281 state->switch_state.previous_default = this;
4282
4283 /* Set falltrhu state. */
4284 ir_assignment *set_fallthru =
4285 new(ctx) ir_assignment(deref_fallthru_var, true_val);
4286
4287 instructions->push_tail(set_fallthru);
4288 }
4289
4290 /* Case statements do not have r-values. */
4291 return NULL;
4292 }
4293
4294 void
4295 ast_iteration_statement::condition_to_hir(ir_loop *stmt,
4296 struct _mesa_glsl_parse_state *state)
4297 {
4298 void *ctx = state;
4299
4300 if (condition != NULL) {
4301 ir_rvalue *const cond =
4302 condition->hir(& stmt->body_instructions, state);
4303
4304 if ((cond == NULL)
4305 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
4306 YYLTYPE loc = condition->get_location();
4307
4308 _mesa_glsl_error(& loc, state,
4309 "loop condition must be scalar boolean");
4310 } else {
4311 /* As the first code in the loop body, generate a block that looks
4312 * like 'if (!condition) break;' as the loop termination condition.
4313 */
4314 ir_rvalue *const not_cond =
4315 new(ctx) ir_expression(ir_unop_logic_not, cond);
4316
4317 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
4318
4319 ir_jump *const break_stmt =
4320 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
4321
4322 if_stmt->then_instructions.push_tail(break_stmt);
4323 stmt->body_instructions.push_tail(if_stmt);
4324 }
4325 }
4326 }
4327
4328
4329 ir_rvalue *
4330 ast_iteration_statement::hir(exec_list *instructions,
4331 struct _mesa_glsl_parse_state *state)
4332 {
4333 void *ctx = state;
4334
4335 /* For-loops and while-loops start a new scope, but do-while loops do not.
4336 */
4337 if (mode != ast_do_while)
4338 state->symbols->push_scope();
4339
4340 if (init_statement != NULL)
4341 init_statement->hir(instructions, state);
4342
4343 ir_loop *const stmt = new(ctx) ir_loop();
4344 instructions->push_tail(stmt);
4345
4346 /* Track the current loop nesting. */
4347 ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
4348
4349 state->loop_nesting_ast = this;
4350
4351 /* Likewise, indicate that following code is closest to a loop,
4352 * NOT closest to a switch.
4353 */
4354 bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
4355 state->switch_state.is_switch_innermost = false;
4356
4357 if (mode != ast_do_while)
4358 condition_to_hir(stmt, state);
4359
4360 if (body != NULL)
4361 body->hir(& stmt->body_instructions, state);
4362
4363 if (rest_expression != NULL)
4364 rest_expression->hir(& stmt->body_instructions, state);
4365
4366 if (mode == ast_do_while)
4367 condition_to_hir(stmt, state);
4368
4369 if (mode != ast_do_while)
4370 state->symbols->pop_scope();
4371
4372 /* Restore previous nesting before returning. */
4373 state->loop_nesting_ast = nesting_ast;
4374 state->switch_state.is_switch_innermost = saved_is_switch_innermost;
4375
4376 /* Loops do not have r-values.
4377 */
4378 return NULL;
4379 }
4380
4381
4382 /**
4383 * Determine if the given type is valid for establishing a default precision
4384 * qualifier.
4385 *
4386 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
4387 *
4388 * "The precision statement
4389 *
4390 * precision precision-qualifier type;
4391 *
4392 * can be used to establish a default precision qualifier. The type field
4393 * can be either int or float or any of the sampler types, and the
4394 * precision-qualifier can be lowp, mediump, or highp."
4395 *
4396 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
4397 * qualifiers on sampler types, but this seems like an oversight (since the
4398 * intention of including these in GLSL 1.30 is to allow compatibility with ES
4399 * shaders). So we allow int, float, and all sampler types regardless of GLSL
4400 * version.
4401 */
4402 static bool
4403 is_valid_default_precision_type(const struct glsl_type *const type)
4404 {
4405 if (type == NULL)
4406 return false;
4407
4408 switch (type->base_type) {
4409 case GLSL_TYPE_INT:
4410 case GLSL_TYPE_FLOAT:
4411 /* "int" and "float" are valid, but vectors and matrices are not. */
4412 return type->vector_elements == 1 && type->matrix_columns == 1;
4413 case GLSL_TYPE_SAMPLER:
4414 return true;
4415 default:
4416 return false;
4417 }
4418 }
4419
4420
4421 ir_rvalue *
4422 ast_type_specifier::hir(exec_list *instructions,
4423 struct _mesa_glsl_parse_state *state)
4424 {
4425 if (this->default_precision == ast_precision_none && this->structure == NULL)
4426 return NULL;
4427
4428 YYLTYPE loc = this->get_location();
4429
4430 /* If this is a precision statement, check that the type to which it is
4431 * applied is either float or int.
4432 *
4433 * From section 4.5.3 of the GLSL 1.30 spec:
4434 * "The precision statement
4435 * precision precision-qualifier type;
4436 * can be used to establish a default precision qualifier. The type
4437 * field can be either int or float [...]. Any other types or
4438 * qualifiers will result in an error.
4439 */
4440 if (this->default_precision != ast_precision_none) {
4441 if (!state->check_precision_qualifiers_allowed(&loc))
4442 return NULL;
4443
4444 if (this->structure != NULL) {
4445 _mesa_glsl_error(&loc, state,
4446 "precision qualifiers do not apply to structures");
4447 return NULL;
4448 }
4449
4450 if (this->is_array) {
4451 _mesa_glsl_error(&loc, state,
4452 "default precision statements do not apply to "
4453 "arrays");
4454 return NULL;
4455 }
4456
4457 const struct glsl_type *const type =
4458 state->symbols->get_type(this->type_name);
4459 if (!is_valid_default_precision_type(type)) {
4460 _mesa_glsl_error(&loc, state,
4461 "default precision statements apply only to "
4462 "float, int, and sampler types");
4463 return NULL;
4464 }
4465
4466 if (type->base_type == GLSL_TYPE_FLOAT
4467 && state->es_shader
4468 && state->target == fragment_shader) {
4469 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
4470 * spec says:
4471 *
4472 * "The fragment language has no default precision qualifier for
4473 * floating point types."
4474 *
4475 * As a result, we have to track whether or not default precision has
4476 * been specified for float in GLSL ES fragment shaders.
4477 *
4478 * Earlier in that same section, the spec says:
4479 *
4480 * "Non-precision qualified declarations will use the precision
4481 * qualifier specified in the most recent precision statement
4482 * that is still in scope. The precision statement has the same
4483 * scoping rules as variable declarations. If it is declared
4484 * inside a compound statement, its effect stops at the end of
4485 * the innermost statement it was declared in. Precision
4486 * statements in nested scopes override precision statements in
4487 * outer scopes. Multiple precision statements for the same basic
4488 * type can appear inside the same scope, with later statements
4489 * overriding earlier statements within that scope."
4490 *
4491 * Default precision specifications follow the same scope rules as
4492 * variables. So, we can track the state of the default float
4493 * precision in the symbol table, and the rules will just work. This
4494 * is a slight abuse of the symbol table, but it has the semantics
4495 * that we want.
4496 */
4497 ir_variable *const junk =
4498 new(state) ir_variable(type, "#default precision",
4499 ir_var_temporary);
4500
4501 state->symbols->add_variable(junk);
4502 }
4503
4504 /* FINISHME: Translate precision statements into IR. */
4505 return NULL;
4506 }
4507
4508 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
4509 * process_record_constructor() can do type-checking on C-style initializer
4510 * expressions of structs, but ast_struct_specifier should only be translated
4511 * to HIR if it is declaring the type of a structure.
4512 *
4513 * The ->is_declaration field is false for initializers of variables
4514 * declared separately from the struct's type definition.
4515 *
4516 * struct S { ... }; (is_declaration = true)
4517 * struct T { ... } t = { ... }; (is_declaration = true)
4518 * S s = { ... }; (is_declaration = false)
4519 */
4520 if (this->structure != NULL && this->structure->is_declaration)
4521 return this->structure->hir(instructions, state);
4522
4523 return NULL;
4524 }
4525
4526
4527 /**
4528 * Process a structure or interface block tree into an array of structure fields
4529 *
4530 * After parsing, where there are some syntax differnces, structures and
4531 * interface blocks are almost identical. They are similar enough that the
4532 * AST for each can be processed the same way into a set of
4533 * \c glsl_struct_field to describe the members.
4534 *
4535 * If we're processing an interface block, var_mode should be the type of the
4536 * interface block (ir_var_shader_in, ir_var_shader_out, or ir_var_uniform).
4537 * If we're processing a structure, var_mode should be ir_var_auto.
4538 *
4539 * \return
4540 * The number of fields processed. A pointer to the array structure fields is
4541 * stored in \c *fields_ret.
4542 */
4543 unsigned
4544 ast_process_structure_or_interface_block(exec_list *instructions,
4545 struct _mesa_glsl_parse_state *state,
4546 exec_list *declarations,
4547 YYLTYPE &loc,
4548 glsl_struct_field **fields_ret,
4549 bool is_interface,
4550 bool block_row_major,
4551 bool allow_reserved_names,
4552 ir_variable_mode var_mode)
4553 {
4554 unsigned decl_count = 0;
4555
4556 /* Make an initial pass over the list of fields to determine how
4557 * many there are. Each element in this list is an ast_declarator_list.
4558 * This means that we actually need to count the number of elements in the
4559 * 'declarations' list in each of the elements.
4560 */
4561 foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
4562 foreach_list_const (decl_ptr, & decl_list->declarations) {
4563 decl_count++;
4564 }
4565 }
4566
4567 /* Allocate storage for the fields and process the field
4568 * declarations. As the declarations are processed, try to also convert
4569 * the types to HIR. This ensures that structure definitions embedded in
4570 * other structure definitions or in interface blocks are processed.
4571 */
4572 glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field,
4573 decl_count);
4574
4575 unsigned i = 0;
4576 foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
4577 const char *type_name;
4578
4579 decl_list->type->specifier->hir(instructions, state);
4580
4581 /* Section 10.9 of the GLSL ES 1.00 specification states that
4582 * embedded structure definitions have been removed from the language.
4583 */
4584 if (state->es_shader && decl_list->type->specifier->structure != NULL) {
4585 _mesa_glsl_error(&loc, state, "embedded structure definitions are "
4586 "not allowed in GLSL ES 1.00");
4587 }
4588
4589 const glsl_type *decl_type =
4590 decl_list->type->glsl_type(& type_name, state);
4591
4592 foreach_list_typed (ast_declaration, decl, link,
4593 &decl_list->declarations) {
4594 if (!allow_reserved_names)
4595 validate_identifier(decl->identifier, loc, state);
4596
4597 /* From the GL_ARB_uniform_buffer_object spec:
4598 *
4599 * "Sampler types are not allowed inside of uniform
4600 * blocks. All other types, arrays, and structures
4601 * allowed for uniforms are allowed within a uniform
4602 * block."
4603 *
4604 * It should be impossible for decl_type to be NULL here. Cases that
4605 * might naturally lead to decl_type being NULL, especially for the
4606 * is_interface case, will have resulted in compilation having
4607 * already halted due to a syntax error.
4608 */
4609 const struct glsl_type *field_type =
4610 decl_type != NULL ? decl_type : glsl_type::error_type;
4611
4612 if (is_interface && field_type->contains_sampler()) {
4613 YYLTYPE loc = decl_list->get_location();
4614 _mesa_glsl_error(&loc, state,
4615 "uniform in non-default uniform block contains sampler");
4616 }
4617
4618 if (field_type->contains_atomic()) {
4619 /* FINISHME: Add a spec quotation here once updated spec
4620 * FINISHME: language is available. See Khronos bug #10903
4621 * FINISHME: on whether atomic counters are allowed in
4622 * FINISHME: structures.
4623 */
4624 YYLTYPE loc = decl_list->get_location();
4625 _mesa_glsl_error(&loc, state, "atomic counter in structure or "
4626 "uniform block");
4627 }
4628
4629 const struct ast_type_qualifier *const qual =
4630 & decl_list->type->qualifier;
4631 if (qual->flags.q.std140 ||
4632 qual->flags.q.packed ||
4633 qual->flags.q.shared) {
4634 _mesa_glsl_error(&loc, state,
4635 "uniform block layout qualifiers std140, packed, and "
4636 "shared can only be applied to uniform blocks, not "
4637 "members");
4638 }
4639
4640 if (decl->is_array) {
4641 field_type = process_array_type(&loc, decl_type, decl->array_size,
4642 state);
4643 }
4644 fields[i].type = field_type;
4645 fields[i].name = decl->identifier;
4646 fields[i].location = -1;
4647 fields[i].interpolation =
4648 interpret_interpolation_qualifier(qual, var_mode, state, &loc);
4649 fields[i].centroid = qual->flags.q.centroid ? 1 : 0;
4650
4651 if (qual->flags.q.row_major || qual->flags.q.column_major) {
4652 if (!qual->flags.q.uniform) {
4653 _mesa_glsl_error(&loc, state,
4654 "row_major and column_major can only be "
4655 "applied to uniform interface blocks");
4656 } else
4657 validate_matrix_layout_for_type(state, &loc, field_type, NULL);
4658 }
4659
4660 if (qual->flags.q.uniform && qual->has_interpolation()) {
4661 _mesa_glsl_error(&loc, state,
4662 "interpolation qualifiers cannot be used "
4663 "with uniform interface blocks");
4664 }
4665
4666 if (field_type->is_matrix() ||
4667 (field_type->is_array() && field_type->fields.array->is_matrix())) {
4668 fields[i].row_major = block_row_major;
4669 if (qual->flags.q.row_major)
4670 fields[i].row_major = true;
4671 else if (qual->flags.q.column_major)
4672 fields[i].row_major = false;
4673 }
4674
4675 i++;
4676 }
4677 }
4678
4679 assert(i == decl_count);
4680
4681 *fields_ret = fields;
4682 return decl_count;
4683 }
4684
4685
4686 ir_rvalue *
4687 ast_struct_specifier::hir(exec_list *instructions,
4688 struct _mesa_glsl_parse_state *state)
4689 {
4690 YYLTYPE loc = this->get_location();
4691
4692 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
4693 *
4694 * "Anonymous structures are not supported; so embedded structures must
4695 * have a declarator. A name given to an embedded struct is scoped at
4696 * the same level as the struct it is embedded in."
4697 *
4698 * The same section of the GLSL 1.20 spec says:
4699 *
4700 * "Anonymous structures are not supported. Embedded structures are not
4701 * supported.
4702 *
4703 * struct S { float f; };
4704 * struct T {
4705 * S; // Error: anonymous structures disallowed
4706 * struct { ... }; // Error: embedded structures disallowed
4707 * S s; // Okay: nested structures with name are allowed
4708 * };"
4709 *
4710 * The GLSL ES 1.00 and 3.00 specs have similar langauge and examples. So,
4711 * we allow embedded structures in 1.10 only.
4712 */
4713 if (state->language_version != 110 && state->struct_specifier_depth != 0)
4714 _mesa_glsl_error(&loc, state,
4715 "embedded structure declartions are not allowed");
4716
4717 state->struct_specifier_depth++;
4718
4719 glsl_struct_field *fields;
4720 unsigned decl_count =
4721 ast_process_structure_or_interface_block(instructions,
4722 state,
4723 &this->declarations,
4724 loc,
4725 &fields,
4726 false,
4727 false,
4728 false /* allow_reserved_names */,
4729 ir_var_auto);
4730
4731 validate_identifier(this->name, loc, state);
4732
4733 const glsl_type *t =
4734 glsl_type::get_record_instance(fields, decl_count, this->name);
4735
4736 if (!state->symbols->add_type(name, t)) {
4737 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
4738 } else {
4739 const glsl_type **s = reralloc(state, state->user_structures,
4740 const glsl_type *,
4741 state->num_user_structures + 1);
4742 if (s != NULL) {
4743 s[state->num_user_structures] = t;
4744 state->user_structures = s;
4745 state->num_user_structures++;
4746 }
4747 }
4748
4749 state->struct_specifier_depth--;
4750
4751 /* Structure type definitions do not have r-values.
4752 */
4753 return NULL;
4754 }
4755
4756
4757 /**
4758 * Visitor class which detects whether a given interface block has been used.
4759 */
4760 class interface_block_usage_visitor : public ir_hierarchical_visitor
4761 {
4762 public:
4763 interface_block_usage_visitor(ir_variable_mode mode, const glsl_type *block)
4764 : mode(mode), block(block), found(false)
4765 {
4766 }
4767
4768 virtual ir_visitor_status visit(ir_dereference_variable *ir)
4769 {
4770 if (ir->var->mode == mode && ir->var->get_interface_type() == block) {
4771 found = true;
4772 return visit_stop;
4773 }
4774 return visit_continue;
4775 }
4776
4777 bool usage_found() const
4778 {
4779 return this->found;
4780 }
4781
4782 private:
4783 ir_variable_mode mode;
4784 const glsl_type *block;
4785 bool found;
4786 };
4787
4788
4789 ir_rvalue *
4790 ast_interface_block::hir(exec_list *instructions,
4791 struct _mesa_glsl_parse_state *state)
4792 {
4793 YYLTYPE loc = this->get_location();
4794
4795 /* The ast_interface_block has a list of ast_declarator_lists. We
4796 * need to turn those into ir_variables with an association
4797 * with this uniform block.
4798 */
4799 enum glsl_interface_packing packing;
4800 if (this->layout.flags.q.shared) {
4801 packing = GLSL_INTERFACE_PACKING_SHARED;
4802 } else if (this->layout.flags.q.packed) {
4803 packing = GLSL_INTERFACE_PACKING_PACKED;
4804 } else {
4805 /* The default layout is std140.
4806 */
4807 packing = GLSL_INTERFACE_PACKING_STD140;
4808 }
4809
4810 ir_variable_mode var_mode;
4811 const char *iface_type_name;
4812 if (this->layout.flags.q.in) {
4813 var_mode = ir_var_shader_in;
4814 iface_type_name = "in";
4815 } else if (this->layout.flags.q.out) {
4816 var_mode = ir_var_shader_out;
4817 iface_type_name = "out";
4818 } else if (this->layout.flags.q.uniform) {
4819 var_mode = ir_var_uniform;
4820 iface_type_name = "uniform";
4821 } else {
4822 var_mode = ir_var_auto;
4823 iface_type_name = "UNKNOWN";
4824 assert(!"interface block layout qualifier not found!");
4825 }
4826
4827 bool redeclaring_per_vertex = strcmp(this->block_name, "gl_PerVertex") == 0;
4828 bool block_row_major = this->layout.flags.q.row_major;
4829 exec_list declared_variables;
4830 glsl_struct_field *fields;
4831 unsigned int num_variables =
4832 ast_process_structure_or_interface_block(&declared_variables,
4833 state,
4834 &this->declarations,
4835 loc,
4836 &fields,
4837 true,
4838 block_row_major,
4839 redeclaring_per_vertex,
4840 var_mode);
4841
4842 if (!redeclaring_per_vertex)
4843 validate_identifier(this->block_name, loc, state);
4844
4845 const glsl_type *earlier_per_vertex = NULL;
4846 if (redeclaring_per_vertex) {
4847 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
4848 * the named interface block gl_in, we can find it by looking at the
4849 * previous declaration of gl_in. Otherwise we can find it by looking
4850 * at the previous decalartion of any of the built-in outputs,
4851 * e.g. gl_Position.
4852 *
4853 * Also check that the instance name and array-ness of the redeclaration
4854 * are correct.
4855 */
4856 switch (var_mode) {
4857 case ir_var_shader_in:
4858 if (ir_variable *earlier_gl_in =
4859 state->symbols->get_variable("gl_in")) {
4860 earlier_per_vertex = earlier_gl_in->get_interface_type();
4861 } else {
4862 _mesa_glsl_error(&loc, state,
4863 "redeclaration of gl_PerVertex input not allowed "
4864 "in the %s shader",
4865 _mesa_glsl_shader_target_name(state->target));
4866 }
4867 if (this->instance_name == NULL ||
4868 strcmp(this->instance_name, "gl_in") != 0 || !this->is_array) {
4869 _mesa_glsl_error(&loc, state,
4870 "gl_PerVertex input must be redeclared as "
4871 "gl_in[]");
4872 }
4873 break;
4874 case ir_var_shader_out:
4875 if (ir_variable *earlier_gl_Position =
4876 state->symbols->get_variable("gl_Position")) {
4877 earlier_per_vertex = earlier_gl_Position->get_interface_type();
4878 } else {
4879 _mesa_glsl_error(&loc, state,
4880 "redeclaration of gl_PerVertex output not "
4881 "allowed in the %s shader",
4882 _mesa_glsl_shader_target_name(state->target));
4883 }
4884 if (this->instance_name != NULL) {
4885 _mesa_glsl_error(&loc, state,
4886 "gl_PerVertex input may not be redeclared with "
4887 "an instance name");
4888 }
4889 break;
4890 default:
4891 _mesa_glsl_error(&loc, state,
4892 "gl_PerVertex must be declared as an input or an "
4893 "output");
4894 break;
4895 }
4896
4897 if (earlier_per_vertex == NULL) {
4898 /* An error has already been reported. Bail out to avoid null
4899 * dereferences later in this function.
4900 */
4901 return NULL;
4902 }
4903
4904 /* Copy locations from the old gl_PerVertex interface block. */
4905 for (unsigned i = 0; i < num_variables; i++) {
4906 int j = earlier_per_vertex->field_index(fields[i].name);
4907 if (j == -1) {
4908 _mesa_glsl_error(&loc, state,
4909 "redeclaration of gl_PerVertex must be a subset "
4910 "of the built-in members of gl_PerVertex");
4911 } else {
4912 fields[i].location =
4913 earlier_per_vertex->fields.structure[j].location;
4914 fields[i].interpolation =
4915 earlier_per_vertex->fields.structure[j].interpolation;
4916 fields[i].centroid =
4917 earlier_per_vertex->fields.structure[j].centroid;
4918 }
4919 }
4920
4921 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
4922 * spec:
4923 *
4924 * If a built-in interface block is redeclared, it must appear in
4925 * the shader before any use of any member included in the built-in
4926 * declaration, or a compilation error will result.
4927 *
4928 * This appears to be a clarification to the behaviour established for
4929 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
4930 * regardless of GLSL version.
4931 */
4932 interface_block_usage_visitor v(var_mode, earlier_per_vertex);
4933 v.run(instructions);
4934 if (v.usage_found()) {
4935 _mesa_glsl_error(&loc, state,
4936 "redeclaration of a built-in interface block must "
4937 "appear before any use of any member of the "
4938 "interface block");
4939 }
4940 }
4941
4942 const glsl_type *block_type =
4943 glsl_type::get_interface_instance(fields,
4944 num_variables,
4945 packing,
4946 this->block_name);
4947
4948 if (!state->symbols->add_interface(block_type->name, block_type, var_mode)) {
4949 YYLTYPE loc = this->get_location();
4950 _mesa_glsl_error(&loc, state, "interface block `%s' with type `%s' "
4951 "already taken in the current scope",
4952 this->block_name, iface_type_name);
4953 }
4954
4955 /* Since interface blocks cannot contain statements, it should be
4956 * impossible for the block to generate any instructions.
4957 */
4958 assert(declared_variables.is_empty());
4959
4960 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
4961 *
4962 * Geometry shader input variables get the per-vertex values written
4963 * out by vertex shader output variables of the same names. Since a
4964 * geometry shader operates on a set of vertices, each input varying
4965 * variable (or input block, see interface blocks below) needs to be
4966 * declared as an array.
4967 */
4968 if (state->target == geometry_shader && !this->is_array &&
4969 var_mode == ir_var_shader_in) {
4970 _mesa_glsl_error(&loc, state, "geometry shader inputs must be arrays");
4971 }
4972
4973 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
4974 * says:
4975 *
4976 * "If an instance name (instance-name) is used, then it puts all the
4977 * members inside a scope within its own name space, accessed with the
4978 * field selector ( . ) operator (analogously to structures)."
4979 */
4980 if (this->instance_name) {
4981 if (redeclaring_per_vertex) {
4982 /* When a built-in in an unnamed interface block is redeclared,
4983 * get_variable_being_redeclared() calls
4984 * check_builtin_array_max_size() to make sure that built-in array
4985 * variables aren't redeclared to illegal sizes. But we're looking
4986 * at a redeclaration of a named built-in interface block. So we
4987 * have to manually call check_builtin_array_max_size() for all parts
4988 * of the interface that are arrays.
4989 */
4990 for (unsigned i = 0; i < num_variables; i++) {
4991 if (fields[i].type->is_array()) {
4992 const unsigned size = fields[i].type->array_size();
4993 check_builtin_array_max_size(fields[i].name, size, loc, state);
4994 }
4995 }
4996 } else {
4997 validate_identifier(this->instance_name, loc, state);
4998 }
4999
5000 ir_variable *var;
5001
5002 if (this->is_array) {
5003 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
5004 *
5005 * For uniform blocks declared an array, each individual array
5006 * element corresponds to a separate buffer object backing one
5007 * instance of the block. As the array size indicates the number
5008 * of buffer objects needed, uniform block array declarations
5009 * must specify an array size.
5010 *
5011 * And a few paragraphs later:
5012 *
5013 * Geometry shader input blocks must be declared as arrays and
5014 * follow the array declaration and linking rules for all
5015 * geometry shader inputs. All other input and output block
5016 * arrays must specify an array size.
5017 *
5018 * The upshot of this is that the only circumstance where an
5019 * interface array size *doesn't* need to be specified is on a
5020 * geometry shader input.
5021 */
5022 if (this->array_size == NULL &&
5023 (state->target != geometry_shader || !this->layout.flags.q.in)) {
5024 _mesa_glsl_error(&loc, state,
5025 "only geometry shader inputs may be unsized "
5026 "instance block arrays");
5027
5028 }
5029
5030 const glsl_type *block_array_type =
5031 process_array_type(&loc, block_type, this->array_size, state);
5032
5033 var = new(state) ir_variable(block_array_type,
5034 this->instance_name,
5035 var_mode);
5036 } else {
5037 var = new(state) ir_variable(block_type,
5038 this->instance_name,
5039 var_mode);
5040 }
5041
5042 if (state->target == geometry_shader && var_mode == ir_var_shader_in)
5043 handle_geometry_shader_input_decl(state, loc, var);
5044
5045 if (ir_variable *earlier =
5046 state->symbols->get_variable(this->instance_name)) {
5047 if (!redeclaring_per_vertex) {
5048 _mesa_glsl_error(&loc, state, "`%s' redeclared",
5049 this->instance_name);
5050 }
5051 earlier->type = var->type;
5052 earlier->reinit_interface_type(block_type);
5053 delete var;
5054 } else {
5055 state->symbols->add_variable(var);
5056 instructions->push_tail(var);
5057 }
5058 } else {
5059 /* In order to have an array size, the block must also be declared with
5060 * an instane name.
5061 */
5062 assert(!this->is_array);
5063
5064 for (unsigned i = 0; i < num_variables; i++) {
5065 ir_variable *var =
5066 new(state) ir_variable(fields[i].type,
5067 ralloc_strdup(state, fields[i].name),
5068 var_mode);
5069 var->interpolation = fields[i].interpolation;
5070 var->centroid = fields[i].centroid;
5071 var->init_interface_type(block_type);
5072
5073 if (redeclaring_per_vertex) {
5074 ir_variable *earlier =
5075 get_variable_being_redeclared(var, loc, state,
5076 true /* allow_all_redeclarations */);
5077 if (strncmp(var->name, "gl_", 3) != 0 || earlier == NULL) {
5078 _mesa_glsl_error(&loc, state,
5079 "redeclaration of gl_PerVertex can only "
5080 "include built-in variables");
5081 } else {
5082 earlier->reinit_interface_type(block_type);
5083 }
5084 continue;
5085 }
5086
5087 if (state->symbols->get_variable(var->name) != NULL)
5088 _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
5089
5090 /* Propagate the "binding" keyword into this UBO's fields;
5091 * the UBO declaration itself doesn't get an ir_variable unless it
5092 * has an instance name. This is ugly.
5093 */
5094 var->explicit_binding = this->layout.flags.q.explicit_binding;
5095 var->binding = this->layout.binding;
5096
5097 state->symbols->add_variable(var);
5098 instructions->push_tail(var);
5099 }
5100
5101 if (redeclaring_per_vertex && block_type != earlier_per_vertex) {
5102 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
5103 *
5104 * It is also a compilation error ... to redeclare a built-in
5105 * block and then use a member from that built-in block that was
5106 * not included in the redeclaration.
5107 *
5108 * This appears to be a clarification to the behaviour established
5109 * for gl_PerVertex by GLSL 1.50, therefore we implement this
5110 * behaviour regardless of GLSL version.
5111 *
5112 * To prevent the shader from using a member that was not included in
5113 * the redeclaration, we disable any ir_variables that are still
5114 * associated with the old declaration of gl_PerVertex (since we've
5115 * already updated all of the variables contained in the new
5116 * gl_PerVertex to point to it).
5117 *
5118 * As a side effect this will prevent
5119 * validate_intrastage_interface_blocks() from getting confused and
5120 * thinking there are conflicting definitions of gl_PerVertex in the
5121 * shader.
5122 */
5123 foreach_list_safe(node, instructions) {
5124 ir_variable *const var = ((ir_instruction *) node)->as_variable();
5125 if (var != NULL &&
5126 var->get_interface_type() == earlier_per_vertex &&
5127 var->mode == var_mode) {
5128 state->symbols->disable_variable(var->name);
5129 var->remove();
5130 }
5131 }
5132 }
5133 }
5134
5135 return NULL;
5136 }
5137
5138
5139 ir_rvalue *
5140 ast_gs_input_layout::hir(exec_list *instructions,
5141 struct _mesa_glsl_parse_state *state)
5142 {
5143 YYLTYPE loc = this->get_location();
5144
5145 /* If any geometry input layout declaration preceded this one, make sure it
5146 * was consistent with this one.
5147 */
5148 if (state->gs_input_prim_type_specified &&
5149 state->gs_input_prim_type != this->prim_type) {
5150 _mesa_glsl_error(&loc, state,
5151 "geometry shader input layout does not match"
5152 " previous declaration");
5153 return NULL;
5154 }
5155
5156 /* If any shader inputs occurred before this declaration and specified an
5157 * array size, make sure the size they specified is consistent with the
5158 * primitive type.
5159 */
5160 unsigned num_vertices = vertices_per_prim(this->prim_type);
5161 if (state->gs_input_size != 0 && state->gs_input_size != num_vertices) {
5162 _mesa_glsl_error(&loc, state,
5163 "this geometry shader input layout implies %u vertices"
5164 " per primitive, but a previous input is declared"
5165 " with size %u", num_vertices, state->gs_input_size);
5166 return NULL;
5167 }
5168
5169 state->gs_input_prim_type_specified = true;
5170 state->gs_input_prim_type = this->prim_type;
5171
5172 /* If any shader inputs occurred before this declaration and did not
5173 * specify an array size, their size is determined now.
5174 */
5175 foreach_list (node, instructions) {
5176 ir_variable *var = ((ir_instruction *) node)->as_variable();
5177 if (var == NULL || var->mode != ir_var_shader_in)
5178 continue;
5179
5180 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
5181 * array; skip it.
5182 */
5183
5184 if (var->type->is_unsized_array()) {
5185 if (var->max_array_access >= num_vertices) {
5186 _mesa_glsl_error(&loc, state,
5187 "this geometry shader input layout implies %u"
5188 " vertices, but an access to element %u of input"
5189 " `%s' already exists", num_vertices,
5190 var->max_array_access, var->name);
5191 } else {
5192 var->type = glsl_type::get_array_instance(var->type->fields.array,
5193 num_vertices);
5194 }
5195 }
5196 }
5197
5198 return NULL;
5199 }
5200
5201
5202 static void
5203 detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
5204 exec_list *instructions)
5205 {
5206 bool gl_FragColor_assigned = false;
5207 bool gl_FragData_assigned = false;
5208 bool user_defined_fs_output_assigned = false;
5209 ir_variable *user_defined_fs_output = NULL;
5210
5211 /* It would be nice to have proper location information. */
5212 YYLTYPE loc;
5213 memset(&loc, 0, sizeof(loc));
5214
5215 foreach_list(node, instructions) {
5216 ir_variable *var = ((ir_instruction *)node)->as_variable();
5217
5218 if (!var || !var->assigned)
5219 continue;
5220
5221 if (strcmp(var->name, "gl_FragColor") == 0)
5222 gl_FragColor_assigned = true;
5223 else if (strcmp(var->name, "gl_FragData") == 0)
5224 gl_FragData_assigned = true;
5225 else if (strncmp(var->name, "gl_", 3) != 0) {
5226 if (state->target == fragment_shader &&
5227 var->mode == ir_var_shader_out) {
5228 user_defined_fs_output_assigned = true;
5229 user_defined_fs_output = var;
5230 }
5231 }
5232 }
5233
5234 /* From the GLSL 1.30 spec:
5235 *
5236 * "If a shader statically assigns a value to gl_FragColor, it
5237 * may not assign a value to any element of gl_FragData. If a
5238 * shader statically writes a value to any element of
5239 * gl_FragData, it may not assign a value to
5240 * gl_FragColor. That is, a shader may assign values to either
5241 * gl_FragColor or gl_FragData, but not both. Multiple shaders
5242 * linked together must also consistently write just one of
5243 * these variables. Similarly, if user declared output
5244 * variables are in use (statically assigned to), then the
5245 * built-in variables gl_FragColor and gl_FragData may not be
5246 * assigned to. These incorrect usages all generate compile
5247 * time errors."
5248 */
5249 if (gl_FragColor_assigned && gl_FragData_assigned) {
5250 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
5251 "`gl_FragColor' and `gl_FragData'");
5252 } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
5253 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
5254 "`gl_FragColor' and `%s'",
5255 user_defined_fs_output->name);
5256 } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
5257 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
5258 "`gl_FragData' and `%s'",
5259 user_defined_fs_output->name);
5260 }
5261 }
5262
5263
5264 static void
5265 remove_per_vertex_blocks(exec_list *instructions,
5266 _mesa_glsl_parse_state *state, ir_variable_mode mode)
5267 {
5268 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
5269 * if it exists in this shader type.
5270 */
5271 const glsl_type *per_vertex = NULL;
5272 switch (mode) {
5273 case ir_var_shader_in:
5274 if (ir_variable *gl_in = state->symbols->get_variable("gl_in"))
5275 per_vertex = gl_in->get_interface_type();
5276 break;
5277 case ir_var_shader_out:
5278 if (ir_variable *gl_Position =
5279 state->symbols->get_variable("gl_Position")) {
5280 per_vertex = gl_Position->get_interface_type();
5281 }
5282 break;
5283 default:
5284 assert(!"Unexpected mode");
5285 break;
5286 }
5287
5288 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
5289 * need to do anything.
5290 */
5291 if (per_vertex == NULL)
5292 return;
5293
5294 /* If the interface block is used by the shader, then we don't need to do
5295 * anything.
5296 */
5297 interface_block_usage_visitor v(mode, per_vertex);
5298 v.run(instructions);
5299 if (v.usage_found())
5300 return;
5301
5302 /* Remove any ir_variable declarations that refer to the interface block
5303 * we're removing.
5304 */
5305 foreach_list_safe(node, instructions) {
5306 ir_variable *const var = ((ir_instruction *) node)->as_variable();
5307 if (var != NULL && var->get_interface_type() == per_vertex &&
5308 var->mode == mode) {
5309 state->symbols->disable_variable(var->name);
5310 var->remove();
5311 }
5312 }
5313 }