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