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