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