glsl: subroutine signatures must match exactly
[mesa.git] / src / compiler / 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 "compiler/glsl_types.h"
56 #include "util/hash_table.h"
57 #include "main/mtypes.h"
58 #include "main/macros.h"
59 #include "main/shaderobj.h"
60 #include "ir.h"
61 #include "ir_builder.h"
62 #include "builtin_functions.h"
63
64 using namespace ir_builder;
65
66 static void
67 detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
68 exec_list *instructions);
69 static void
70 verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state *state);
71
72 static void
73 remove_per_vertex_blocks(exec_list *instructions,
74 _mesa_glsl_parse_state *state, ir_variable_mode mode);
75
76 /**
77 * Visitor class that finds the first instance of any write-only variable that
78 * is ever read, if any
79 */
80 class read_from_write_only_variable_visitor : public ir_hierarchical_visitor
81 {
82 public:
83 read_from_write_only_variable_visitor() : found(NULL)
84 {
85 }
86
87 virtual ir_visitor_status visit(ir_dereference_variable *ir)
88 {
89 if (this->in_assignee)
90 return visit_continue;
91
92 ir_variable *var = ir->variable_referenced();
93 /* We can have memory_write_only set on both images and buffer variables,
94 * but in the former there is a distinction between reads from
95 * the variable itself (write_only) and from the memory they point to
96 * (memory_write_only), while in the case of buffer variables there is
97 * no such distinction, that is why this check here is limited to
98 * buffer variables alone.
99 */
100 if (!var || var->data.mode != ir_var_shader_storage)
101 return visit_continue;
102
103 if (var->data.memory_write_only) {
104 found = var;
105 return visit_stop;
106 }
107
108 return visit_continue;
109 }
110
111 ir_variable *get_variable() {
112 return found;
113 }
114
115 virtual ir_visitor_status visit_enter(ir_expression *ir)
116 {
117 /* .length() doesn't actually read anything */
118 if (ir->operation == ir_unop_ssbo_unsized_array_length)
119 return visit_continue_with_parent;
120
121 return visit_continue;
122 }
123
124 private:
125 ir_variable *found;
126 };
127
128 void
129 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
130 {
131 _mesa_glsl_initialize_variables(instructions, state);
132
133 state->symbols->separate_function_namespace = state->language_version == 110;
134
135 state->current_function = NULL;
136
137 state->toplevel_ir = instructions;
138
139 state->gs_input_prim_type_specified = false;
140 state->tcs_output_vertices_specified = false;
141 state->cs_input_local_size_specified = false;
142
143 /* Section 4.2 of the GLSL 1.20 specification states:
144 * "The built-in functions are scoped in a scope outside the global scope
145 * users declare global variables in. That is, a shader's global scope,
146 * available for user-defined functions and global variables, is nested
147 * inside the scope containing the built-in functions."
148 *
149 * Since built-in functions like ftransform() access built-in variables,
150 * it follows that those must be in the outer scope as well.
151 *
152 * We push scope here to create this nesting effect...but don't pop.
153 * This way, a shader's globals are still in the symbol table for use
154 * by the linker.
155 */
156 state->symbols->push_scope();
157
158 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
159 ast->hir(instructions, state);
160
161 verify_subroutine_associated_funcs(state);
162 detect_recursion_unlinked(state, instructions);
163 detect_conflicting_assignments(state, instructions);
164
165 state->toplevel_ir = NULL;
166
167 /* Move all of the variable declarations to the front of the IR list, and
168 * reverse the order. This has the (intended!) side effect that vertex
169 * shader inputs and fragment shader outputs will appear in the IR in the
170 * same order that they appeared in the shader code. This results in the
171 * locations being assigned in the declared order. Many (arguably buggy)
172 * applications depend on this behavior, and it matches what nearly all
173 * other drivers do.
174 */
175 foreach_in_list_safe(ir_instruction, node, instructions) {
176 ir_variable *const var = node->as_variable();
177
178 if (var == NULL)
179 continue;
180
181 var->remove();
182 instructions->push_head(var);
183 }
184
185 /* Figure out if gl_FragCoord is actually used in fragment shader */
186 ir_variable *const var = state->symbols->get_variable("gl_FragCoord");
187 if (var != NULL)
188 state->fs_uses_gl_fragcoord = var->data.used;
189
190 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
191 *
192 * If multiple shaders using members of a built-in block belonging to
193 * the same interface are linked together in the same program, they
194 * must all redeclare the built-in block in the same way, as described
195 * in section 4.3.7 "Interface Blocks" for interface block matching, or
196 * a link error will result.
197 *
198 * The phrase "using members of a built-in block" implies that if two
199 * shaders are linked together and one of them *does not use* any members
200 * of the built-in block, then that shader does not need to have a matching
201 * redeclaration of the built-in block.
202 *
203 * This appears to be a clarification to the behaviour established for
204 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
205 * version.
206 *
207 * The definition of "interface" in section 4.3.7 that applies here is as
208 * follows:
209 *
210 * The boundary between adjacent programmable pipeline stages: This
211 * spans all the outputs in all compilation units of the first stage
212 * and all the inputs in all compilation units of the second stage.
213 *
214 * Therefore this rule applies to both inter- and intra-stage linking.
215 *
216 * The easiest way to implement this is to check whether the shader uses
217 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
218 * remove all the relevant variable declaration from the IR, so that the
219 * linker won't see them and complain about mismatches.
220 */
221 remove_per_vertex_blocks(instructions, state, ir_var_shader_in);
222 remove_per_vertex_blocks(instructions, state, ir_var_shader_out);
223
224 /* Check that we don't have reads from write-only variables */
225 read_from_write_only_variable_visitor v;
226 v.run(instructions);
227 ir_variable *error_var = v.get_variable();
228 if (error_var) {
229 /* It would be nice to have proper location information, but for that
230 * we would need to check this as we process each kind of AST node
231 */
232 YYLTYPE loc;
233 memset(&loc, 0, sizeof(loc));
234 _mesa_glsl_error(&loc, state, "Read from write-only variable `%s'",
235 error_var->name);
236 }
237 }
238
239
240 static ir_expression_operation
241 get_implicit_conversion_operation(const glsl_type *to, const glsl_type *from,
242 struct _mesa_glsl_parse_state *state)
243 {
244 switch (to->base_type) {
245 case GLSL_TYPE_FLOAT:
246 switch (from->base_type) {
247 case GLSL_TYPE_INT: return ir_unop_i2f;
248 case GLSL_TYPE_UINT: return ir_unop_u2f;
249 default: return (ir_expression_operation)0;
250 }
251
252 case GLSL_TYPE_UINT:
253 if (!state->has_implicit_int_to_uint_conversion())
254 return (ir_expression_operation)0;
255 switch (from->base_type) {
256 case GLSL_TYPE_INT: return ir_unop_i2u;
257 default: return (ir_expression_operation)0;
258 }
259
260 case GLSL_TYPE_DOUBLE:
261 if (!state->has_double())
262 return (ir_expression_operation)0;
263 switch (from->base_type) {
264 case GLSL_TYPE_INT: return ir_unop_i2d;
265 case GLSL_TYPE_UINT: return ir_unop_u2d;
266 case GLSL_TYPE_FLOAT: return ir_unop_f2d;
267 case GLSL_TYPE_INT64: return ir_unop_i642d;
268 case GLSL_TYPE_UINT64: return ir_unop_u642d;
269 default: return (ir_expression_operation)0;
270 }
271
272 case GLSL_TYPE_UINT64:
273 if (!state->has_int64())
274 return (ir_expression_operation)0;
275 switch (from->base_type) {
276 case GLSL_TYPE_INT: return ir_unop_i2u64;
277 case GLSL_TYPE_UINT: return ir_unop_u2u64;
278 case GLSL_TYPE_INT64: return ir_unop_i642u64;
279 default: return (ir_expression_operation)0;
280 }
281
282 case GLSL_TYPE_INT64:
283 if (!state->has_int64())
284 return (ir_expression_operation)0;
285 switch (from->base_type) {
286 case GLSL_TYPE_INT: return ir_unop_i2i64;
287 default: return (ir_expression_operation)0;
288 }
289
290 default: return (ir_expression_operation)0;
291 }
292 }
293
294
295 /**
296 * If a conversion is available, convert one operand to a different type
297 *
298 * The \c from \c ir_rvalue is converted "in place".
299 *
300 * \param to Type that the operand it to be converted to
301 * \param from Operand that is being converted
302 * \param state GLSL compiler state
303 *
304 * \return
305 * If a conversion is possible (or unnecessary), \c true is returned.
306 * Otherwise \c false is returned.
307 */
308 static bool
309 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
310 struct _mesa_glsl_parse_state *state)
311 {
312 void *ctx = state;
313 if (to->base_type == from->type->base_type)
314 return true;
315
316 /* Prior to GLSL 1.20, there are no implicit conversions */
317 if (!state->has_implicit_conversions())
318 return false;
319
320 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
321 *
322 * "There are no implicit array or structure conversions. For
323 * example, an array of int cannot be implicitly converted to an
324 * array of float.
325 */
326 if (!to->is_numeric() || !from->type->is_numeric())
327 return false;
328
329 /* We don't actually want the specific type `to`, we want a type
330 * with the same base type as `to`, but the same vector width as
331 * `from`.
332 */
333 to = glsl_type::get_instance(to->base_type, from->type->vector_elements,
334 from->type->matrix_columns);
335
336 ir_expression_operation op = get_implicit_conversion_operation(to, from->type, state);
337 if (op) {
338 from = new(ctx) ir_expression(op, to, from, NULL);
339 return true;
340 } else {
341 return false;
342 }
343 }
344
345
346 static const struct glsl_type *
347 arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
348 bool multiply,
349 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
350 {
351 const glsl_type *type_a = value_a->type;
352 const glsl_type *type_b = value_b->type;
353
354 /* From GLSL 1.50 spec, page 56:
355 *
356 * "The arithmetic binary operators add (+), subtract (-),
357 * multiply (*), and divide (/) operate on integer and
358 * floating-point scalars, vectors, and matrices."
359 */
360 if (!type_a->is_numeric() || !type_b->is_numeric()) {
361 _mesa_glsl_error(loc, state,
362 "operands to arithmetic operators must be numeric");
363 return glsl_type::error_type;
364 }
365
366
367 /* "If one operand is floating-point based and the other is
368 * not, then the conversions from Section 4.1.10 "Implicit
369 * Conversions" are applied to the non-floating-point-based operand."
370 */
371 if (!apply_implicit_conversion(type_a, value_b, state)
372 && !apply_implicit_conversion(type_b, value_a, state)) {
373 _mesa_glsl_error(loc, state,
374 "could not implicitly convert operands to "
375 "arithmetic operator");
376 return glsl_type::error_type;
377 }
378 type_a = value_a->type;
379 type_b = value_b->type;
380
381 /* "If the operands are integer types, they must both be signed or
382 * both be unsigned."
383 *
384 * From this rule and the preceeding conversion it can be inferred that
385 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
386 * The is_numeric check above already filtered out the case where either
387 * type is not one of these, so now the base types need only be tested for
388 * equality.
389 */
390 if (type_a->base_type != type_b->base_type) {
391 _mesa_glsl_error(loc, state,
392 "base type mismatch for arithmetic operator");
393 return glsl_type::error_type;
394 }
395
396 /* "All arithmetic binary operators result in the same fundamental type
397 * (signed integer, unsigned integer, or floating-point) as the
398 * operands they operate on, after operand type conversion. After
399 * conversion, the following cases are valid
400 *
401 * * The two operands are scalars. In this case the operation is
402 * applied, resulting in a scalar."
403 */
404 if (type_a->is_scalar() && type_b->is_scalar())
405 return type_a;
406
407 /* "* One operand is a scalar, and the other is a vector or matrix.
408 * In this case, the scalar operation is applied independently to each
409 * component of the vector or matrix, resulting in the same size
410 * vector or matrix."
411 */
412 if (type_a->is_scalar()) {
413 if (!type_b->is_scalar())
414 return type_b;
415 } else if (type_b->is_scalar()) {
416 return type_a;
417 }
418
419 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
420 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
421 * handled.
422 */
423 assert(!type_a->is_scalar());
424 assert(!type_b->is_scalar());
425
426 /* "* The two operands are vectors of the same size. In this case, the
427 * operation is done component-wise resulting in the same size
428 * vector."
429 */
430 if (type_a->is_vector() && type_b->is_vector()) {
431 if (type_a == type_b) {
432 return type_a;
433 } else {
434 _mesa_glsl_error(loc, state,
435 "vector size mismatch for arithmetic operator");
436 return glsl_type::error_type;
437 }
438 }
439
440 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
441 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
442 * <vector, vector> have been handled. At least one of the operands must
443 * be matrix. Further, since there are no integer matrix types, the base
444 * type of both operands must be float.
445 */
446 assert(type_a->is_matrix() || type_b->is_matrix());
447 assert(type_a->is_float() || type_a->is_double());
448 assert(type_b->is_float() || type_b->is_double());
449
450 /* "* The operator is add (+), subtract (-), or divide (/), and the
451 * operands are matrices with the same number of rows and the same
452 * number of columns. In this case, the operation is done component-
453 * wise resulting in the same size matrix."
454 * * The operator is multiply (*), where both operands are matrices or
455 * one operand is a vector and the other a matrix. A right vector
456 * operand is treated as a column vector and a left vector operand as a
457 * row vector. In all these cases, it is required that the number of
458 * columns of the left operand is equal to the number of rows of the
459 * right operand. Then, the multiply (*) operation does a linear
460 * algebraic multiply, yielding an object that has the same number of
461 * rows as the left operand and the same number of columns as the right
462 * operand. Section 5.10 "Vector and Matrix Operations" explains in
463 * more detail how vectors and matrices are operated on."
464 */
465 if (! multiply) {
466 if (type_a == type_b)
467 return type_a;
468 } else {
469 const glsl_type *type = glsl_type::get_mul_type(type_a, type_b);
470
471 if (type == glsl_type::error_type) {
472 _mesa_glsl_error(loc, state,
473 "size mismatch for matrix multiplication");
474 }
475
476 return type;
477 }
478
479
480 /* "All other cases are illegal."
481 */
482 _mesa_glsl_error(loc, state, "type mismatch");
483 return glsl_type::error_type;
484 }
485
486
487 static const struct glsl_type *
488 unary_arithmetic_result_type(const struct glsl_type *type,
489 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
490 {
491 /* From GLSL 1.50 spec, page 57:
492 *
493 * "The arithmetic unary operators negate (-), post- and pre-increment
494 * and decrement (-- and ++) operate on integer or floating-point
495 * values (including vectors and matrices). All unary operators work
496 * component-wise on their operands. These result with the same type
497 * they operated on."
498 */
499 if (!type->is_numeric()) {
500 _mesa_glsl_error(loc, state,
501 "operands to arithmetic operators must be numeric");
502 return glsl_type::error_type;
503 }
504
505 return type;
506 }
507
508 /**
509 * \brief Return the result type of a bit-logic operation.
510 *
511 * If the given types to the bit-logic operator are invalid, return
512 * glsl_type::error_type.
513 *
514 * \param value_a LHS of bit-logic op
515 * \param value_b RHS of bit-logic op
516 */
517 static const struct glsl_type *
518 bit_logic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
519 ast_operators op,
520 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
521 {
522 const glsl_type *type_a = value_a->type;
523 const glsl_type *type_b = value_b->type;
524
525 if (!state->check_bitwise_operations_allowed(loc)) {
526 return glsl_type::error_type;
527 }
528
529 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
530 *
531 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
532 * (|). The operands must be of type signed or unsigned integers or
533 * integer vectors."
534 */
535 if (!type_a->is_integer_32_64()) {
536 _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
537 ast_expression::operator_string(op));
538 return glsl_type::error_type;
539 }
540 if (!type_b->is_integer_32_64()) {
541 _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
542 ast_expression::operator_string(op));
543 return glsl_type::error_type;
544 }
545
546 /* Prior to GLSL 4.0 / GL_ARB_gpu_shader5, implicit conversions didn't
547 * make sense for bitwise operations, as they don't operate on floats.
548 *
549 * GLSL 4.0 added implicit int -> uint conversions, which are relevant
550 * here. It wasn't clear whether or not we should apply them to bitwise
551 * operations. However, Khronos has decided that they should in future
552 * language revisions. Applications also rely on this behavior. We opt
553 * to apply them in general, but issue a portability warning.
554 *
555 * See https://www.khronos.org/bugzilla/show_bug.cgi?id=1405
556 */
557 if (type_a->base_type != type_b->base_type) {
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 "`%s` operator",
563 ast_expression::operator_string(op));
564 return glsl_type::error_type;
565 } else {
566 _mesa_glsl_warning(loc, state,
567 "some implementations may not support implicit "
568 "int -> uint conversions for `%s' operators; "
569 "consider casting explicitly for portability",
570 ast_expression::operator_string(op));
571 }
572 type_a = value_a->type;
573 type_b = value_b->type;
574 }
575
576 /* "The fundamental types of the operands (signed or unsigned) must
577 * match,"
578 */
579 if (type_a->base_type != type_b->base_type) {
580 _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
581 "base type", ast_expression::operator_string(op));
582 return glsl_type::error_type;
583 }
584
585 /* "The operands cannot be vectors of differing size." */
586 if (type_a->is_vector() &&
587 type_b->is_vector() &&
588 type_a->vector_elements != type_b->vector_elements) {
589 _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
590 "different sizes", ast_expression::operator_string(op));
591 return glsl_type::error_type;
592 }
593
594 /* "If one operand is a scalar and the other a vector, the scalar is
595 * applied component-wise to the vector, resulting in the same type as
596 * the vector. The fundamental types of the operands [...] will be the
597 * resulting fundamental type."
598 */
599 if (type_a->is_scalar())
600 return type_b;
601 else
602 return type_a;
603 }
604
605 static const struct glsl_type *
606 modulus_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
607 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
608 {
609 const glsl_type *type_a = value_a->type;
610 const glsl_type *type_b = value_b->type;
611
612 if (!state->EXT_gpu_shader4_enable &&
613 !state->check_version(130, 300, loc, "operator '%%' is reserved")) {
614 return glsl_type::error_type;
615 }
616
617 /* Section 5.9 (Expressions) of the GLSL 4.00 specification says:
618 *
619 * "The operator modulus (%) operates on signed or unsigned integers or
620 * integer vectors."
621 */
622 if (!type_a->is_integer_32_64()) {
623 _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer");
624 return glsl_type::error_type;
625 }
626 if (!type_b->is_integer_32_64()) {
627 _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer");
628 return glsl_type::error_type;
629 }
630
631 /* "If the fundamental types in the operands do not match, then the
632 * conversions from section 4.1.10 "Implicit Conversions" are applied
633 * to create matching types."
634 *
635 * Note that GLSL 4.00 (and GL_ARB_gpu_shader5) introduced implicit
636 * int -> uint conversion rules. Prior to that, there were no implicit
637 * conversions. So it's harmless to apply them universally - no implicit
638 * conversions will exist. If the types don't match, we'll receive false,
639 * and raise an error, satisfying the GLSL 1.50 spec, page 56:
640 *
641 * "The operand types must both be signed or unsigned."
642 */
643 if (!apply_implicit_conversion(type_a, value_b, state) &&
644 !apply_implicit_conversion(type_b, value_a, state)) {
645 _mesa_glsl_error(loc, state,
646 "could not implicitly convert operands to "
647 "modulus (%%) operator");
648 return glsl_type::error_type;
649 }
650 type_a = value_a->type;
651 type_b = value_b->type;
652
653 /* "The operands cannot be vectors of differing size. If one operand is
654 * a scalar and the other vector, then the scalar is applied component-
655 * wise to the vector, resulting in the same type as the vector. If both
656 * are vectors of the same size, the result is computed component-wise."
657 */
658 if (type_a->is_vector()) {
659 if (!type_b->is_vector()
660 || (type_a->vector_elements == type_b->vector_elements))
661 return type_a;
662 } else
663 return type_b;
664
665 /* "The operator modulus (%) is not defined for any other data types
666 * (non-integer types)."
667 */
668 _mesa_glsl_error(loc, state, "type mismatch");
669 return glsl_type::error_type;
670 }
671
672
673 static const struct glsl_type *
674 relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
675 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
676 {
677 const glsl_type *type_a = value_a->type;
678 const glsl_type *type_b = value_b->type;
679
680 /* From GLSL 1.50 spec, page 56:
681 * "The relational operators greater than (>), less than (<), greater
682 * than or equal (>=), and less than or equal (<=) operate only on
683 * scalar integer and scalar floating-point expressions."
684 */
685 if (!type_a->is_numeric()
686 || !type_b->is_numeric()
687 || !type_a->is_scalar()
688 || !type_b->is_scalar()) {
689 _mesa_glsl_error(loc, state,
690 "operands to relational operators must be scalar and "
691 "numeric");
692 return glsl_type::error_type;
693 }
694
695 /* "Either the operands' types must match, or the conversions from
696 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
697 * operand, after which the types must match."
698 */
699 if (!apply_implicit_conversion(type_a, value_b, state)
700 && !apply_implicit_conversion(type_b, value_a, state)) {
701 _mesa_glsl_error(loc, state,
702 "could not implicitly convert operands to "
703 "relational operator");
704 return glsl_type::error_type;
705 }
706 type_a = value_a->type;
707 type_b = value_b->type;
708
709 if (type_a->base_type != type_b->base_type) {
710 _mesa_glsl_error(loc, state, "base type mismatch");
711 return glsl_type::error_type;
712 }
713
714 /* "The result is scalar Boolean."
715 */
716 return glsl_type::bool_type;
717 }
718
719 /**
720 * \brief Return the result type of a bit-shift operation.
721 *
722 * If the given types to the bit-shift operator are invalid, return
723 * glsl_type::error_type.
724 *
725 * \param type_a Type of LHS of bit-shift op
726 * \param type_b Type of RHS of bit-shift op
727 */
728 static const struct glsl_type *
729 shift_result_type(const struct glsl_type *type_a,
730 const struct glsl_type *type_b,
731 ast_operators op,
732 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
733 {
734 if (!state->check_bitwise_operations_allowed(loc)) {
735 return glsl_type::error_type;
736 }
737
738 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
739 *
740 * "The shift operators (<<) and (>>). For both operators, the operands
741 * must be signed or unsigned integers or integer vectors. One operand
742 * can be signed while the other is unsigned."
743 */
744 if (!type_a->is_integer_32_64()) {
745 _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
746 "integer vector", ast_expression::operator_string(op));
747 return glsl_type::error_type;
748
749 }
750 if (!type_b->is_integer_32()) {
751 _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
752 "integer vector", ast_expression::operator_string(op));
753 return glsl_type::error_type;
754 }
755
756 /* "If the first operand is a scalar, the second operand has to be
757 * a scalar as well."
758 */
759 if (type_a->is_scalar() && !type_b->is_scalar()) {
760 _mesa_glsl_error(loc, state, "if the first operand of %s is scalar, the "
761 "second must be scalar as well",
762 ast_expression::operator_string(op));
763 return glsl_type::error_type;
764 }
765
766 /* If both operands are vectors, check that they have same number of
767 * elements.
768 */
769 if (type_a->is_vector() &&
770 type_b->is_vector() &&
771 type_a->vector_elements != type_b->vector_elements) {
772 _mesa_glsl_error(loc, state, "vector operands to operator %s must "
773 "have same number of elements",
774 ast_expression::operator_string(op));
775 return glsl_type::error_type;
776 }
777
778 /* "In all cases, the resulting type will be the same type as the left
779 * operand."
780 */
781 return type_a;
782 }
783
784 /**
785 * Returns the innermost array index expression in an rvalue tree.
786 * This is the largest indexing level -- if an array of blocks, then
787 * it is the block index rather than an indexing expression for an
788 * array-typed member of an array of blocks.
789 */
790 static ir_rvalue *
791 find_innermost_array_index(ir_rvalue *rv)
792 {
793 ir_dereference_array *last = NULL;
794 while (rv) {
795 if (rv->as_dereference_array()) {
796 last = rv->as_dereference_array();
797 rv = last->array;
798 } else if (rv->as_dereference_record())
799 rv = rv->as_dereference_record()->record;
800 else if (rv->as_swizzle())
801 rv = rv->as_swizzle()->val;
802 else
803 rv = NULL;
804 }
805
806 if (last)
807 return last->array_index;
808
809 return NULL;
810 }
811
812 /**
813 * Validates that a value can be assigned to a location with a specified type
814 *
815 * Validates that \c rhs can be assigned to some location. If the types are
816 * not an exact match but an automatic conversion is possible, \c rhs will be
817 * converted.
818 *
819 * \return
820 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
821 * Otherwise the actual RHS to be assigned will be returned. This may be
822 * \c rhs, or it may be \c rhs after some type conversion.
823 *
824 * \note
825 * In addition to being used for assignments, this function is used to
826 * type-check return values.
827 */
828 static ir_rvalue *
829 validate_assignment(struct _mesa_glsl_parse_state *state,
830 YYLTYPE loc, ir_rvalue *lhs,
831 ir_rvalue *rhs, bool is_initializer)
832 {
833 /* If there is already some error in the RHS, just return it. Anything
834 * else will lead to an avalanche of error message back to the user.
835 */
836 if (rhs->type->is_error())
837 return rhs;
838
839 /* In the Tessellation Control Shader:
840 * If a per-vertex output variable is used as an l-value, it is an error
841 * if the expression indicating the vertex number is not the identifier
842 * `gl_InvocationID`.
843 */
844 if (state->stage == MESA_SHADER_TESS_CTRL && !lhs->type->is_error()) {
845 ir_variable *var = lhs->variable_referenced();
846 if (var && var->data.mode == ir_var_shader_out && !var->data.patch) {
847 ir_rvalue *index = find_innermost_array_index(lhs);
848 ir_variable *index_var = index ? index->variable_referenced() : NULL;
849 if (!index_var || strcmp(index_var->name, "gl_InvocationID") != 0) {
850 _mesa_glsl_error(&loc, state,
851 "Tessellation control shader outputs can only "
852 "be indexed by gl_InvocationID");
853 return NULL;
854 }
855 }
856 }
857
858 /* If the types are identical, the assignment can trivially proceed.
859 */
860 if (rhs->type == lhs->type)
861 return rhs;
862
863 /* If the array element types are the same and the LHS is unsized,
864 * the assignment is okay for initializers embedded in variable
865 * declarations.
866 *
867 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
868 * is handled by ir_dereference::is_lvalue.
869 */
870 const glsl_type *lhs_t = lhs->type;
871 const glsl_type *rhs_t = rhs->type;
872 bool unsized_array = false;
873 while(lhs_t->is_array()) {
874 if (rhs_t == lhs_t)
875 break; /* the rest of the inner arrays match so break out early */
876 if (!rhs_t->is_array()) {
877 unsized_array = false;
878 break; /* number of dimensions mismatch */
879 }
880 if (lhs_t->length == rhs_t->length) {
881 lhs_t = lhs_t->fields.array;
882 rhs_t = rhs_t->fields.array;
883 continue;
884 } else if (lhs_t->is_unsized_array()) {
885 unsized_array = true;
886 } else {
887 unsized_array = false;
888 break; /* sized array mismatch */
889 }
890 lhs_t = lhs_t->fields.array;
891 rhs_t = rhs_t->fields.array;
892 }
893 if (unsized_array) {
894 if (is_initializer) {
895 if (rhs->type->get_scalar_type() == lhs->type->get_scalar_type())
896 return rhs;
897 } else {
898 _mesa_glsl_error(&loc, state,
899 "implicitly sized arrays cannot be assigned");
900 return NULL;
901 }
902 }
903
904 /* Check for implicit conversion in GLSL 1.20 */
905 if (apply_implicit_conversion(lhs->type, rhs, state)) {
906 if (rhs->type == lhs->type)
907 return rhs;
908 }
909
910 _mesa_glsl_error(&loc, state,
911 "%s of type %s cannot be assigned to "
912 "variable of type %s",
913 is_initializer ? "initializer" : "value",
914 rhs->type->name, lhs->type->name);
915
916 return NULL;
917 }
918
919 static void
920 mark_whole_array_access(ir_rvalue *access)
921 {
922 ir_dereference_variable *deref = access->as_dereference_variable();
923
924 if (deref && deref->var) {
925 deref->var->data.max_array_access = deref->type->length - 1;
926 }
927 }
928
929 static bool
930 do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
931 const char *non_lvalue_description,
932 ir_rvalue *lhs, ir_rvalue *rhs,
933 ir_rvalue **out_rvalue, bool needs_rvalue,
934 bool is_initializer,
935 YYLTYPE lhs_loc)
936 {
937 void *ctx = state;
938 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
939
940 ir_variable *lhs_var = lhs->variable_referenced();
941 if (lhs_var)
942 lhs_var->data.assigned = true;
943
944 if (!error_emitted) {
945 if (non_lvalue_description != NULL) {
946 _mesa_glsl_error(&lhs_loc, state,
947 "assignment to %s",
948 non_lvalue_description);
949 error_emitted = true;
950 } else if (lhs_var != NULL && (lhs_var->data.read_only ||
951 (lhs_var->data.mode == ir_var_shader_storage &&
952 lhs_var->data.memory_read_only))) {
953 /* We can have memory_read_only set on both images and buffer variables,
954 * but in the former there is a distinction between assignments to
955 * the variable itself (read_only) and to the memory they point to
956 * (memory_read_only), while in the case of buffer variables there is
957 * no such distinction, that is why this check here is limited to
958 * buffer variables alone.
959 */
960 _mesa_glsl_error(&lhs_loc, state,
961 "assignment to read-only variable '%s'",
962 lhs_var->name);
963 error_emitted = true;
964 } else if (lhs->type->is_array() &&
965 !state->check_version(120, 300, &lhs_loc,
966 "whole array assignment forbidden")) {
967 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
968 *
969 * "Other binary or unary expressions, non-dereferenced
970 * arrays, function names, swizzles with repeated fields,
971 * and constants cannot be l-values."
972 *
973 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
974 */
975 error_emitted = true;
976 } else if (!lhs->is_lvalue(state)) {
977 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
978 error_emitted = true;
979 }
980 }
981
982 ir_rvalue *new_rhs =
983 validate_assignment(state, lhs_loc, lhs, rhs, is_initializer);
984 if (new_rhs != NULL) {
985 rhs = new_rhs;
986
987 /* If the LHS array was not declared with a size, it takes it size from
988 * the RHS. If the LHS is an l-value and a whole array, it must be a
989 * dereference of a variable. Any other case would require that the LHS
990 * is either not an l-value or not a whole array.
991 */
992 if (lhs->type->is_unsized_array()) {
993 ir_dereference *const d = lhs->as_dereference();
994
995 assert(d != NULL);
996
997 ir_variable *const var = d->variable_referenced();
998
999 assert(var != NULL);
1000
1001 if (var->data.max_array_access >= rhs->type->array_size()) {
1002 /* FINISHME: This should actually log the location of the RHS. */
1003 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
1004 "previous access",
1005 var->data.max_array_access);
1006 }
1007
1008 var->type = glsl_type::get_array_instance(lhs->type->fields.array,
1009 rhs->type->array_size());
1010 d->type = var->type;
1011 }
1012 if (lhs->type->is_array()) {
1013 mark_whole_array_access(rhs);
1014 mark_whole_array_access(lhs);
1015 }
1016 } else {
1017 error_emitted = true;
1018 }
1019
1020 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
1021 * but not post_inc) need the converted assigned value as an rvalue
1022 * to handle things like:
1023 *
1024 * i = j += 1;
1025 */
1026 if (needs_rvalue) {
1027 ir_rvalue *rvalue;
1028 if (!error_emitted) {
1029 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
1030 ir_var_temporary);
1031 instructions->push_tail(var);
1032 instructions->push_tail(assign(var, rhs));
1033
1034 ir_dereference_variable *deref_var =
1035 new(ctx) ir_dereference_variable(var);
1036 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var));
1037 rvalue = new(ctx) ir_dereference_variable(var);
1038 } else {
1039 rvalue = ir_rvalue::error_value(ctx);
1040 }
1041 *out_rvalue = rvalue;
1042 } else {
1043 if (!error_emitted)
1044 instructions->push_tail(new(ctx) ir_assignment(lhs, rhs));
1045 *out_rvalue = NULL;
1046 }
1047
1048 return error_emitted;
1049 }
1050
1051 static ir_rvalue *
1052 get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
1053 {
1054 void *ctx = ralloc_parent(lvalue);
1055 ir_variable *var;
1056
1057 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
1058 ir_var_temporary);
1059 instructions->push_tail(var);
1060
1061 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
1062 lvalue));
1063
1064 return new(ctx) ir_dereference_variable(var);
1065 }
1066
1067
1068 ir_rvalue *
1069 ast_node::hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
1070 {
1071 (void) instructions;
1072 (void) state;
1073
1074 return NULL;
1075 }
1076
1077 bool
1078 ast_node::has_sequence_subexpression() const
1079 {
1080 return false;
1081 }
1082
1083 void
1084 ast_node::set_is_lhs(bool /* new_value */)
1085 {
1086 }
1087
1088 void
1089 ast_function_expression::hir_no_rvalue(exec_list *instructions,
1090 struct _mesa_glsl_parse_state *state)
1091 {
1092 (void)hir(instructions, state);
1093 }
1094
1095 void
1096 ast_aggregate_initializer::hir_no_rvalue(exec_list *instructions,
1097 struct _mesa_glsl_parse_state *state)
1098 {
1099 (void)hir(instructions, state);
1100 }
1101
1102 static ir_rvalue *
1103 do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
1104 {
1105 int join_op;
1106 ir_rvalue *cmp = NULL;
1107
1108 if (operation == ir_binop_all_equal)
1109 join_op = ir_binop_logic_and;
1110 else
1111 join_op = ir_binop_logic_or;
1112
1113 switch (op0->type->base_type) {
1114 case GLSL_TYPE_FLOAT:
1115 case GLSL_TYPE_FLOAT16:
1116 case GLSL_TYPE_UINT:
1117 case GLSL_TYPE_INT:
1118 case GLSL_TYPE_BOOL:
1119 case GLSL_TYPE_DOUBLE:
1120 case GLSL_TYPE_UINT64:
1121 case GLSL_TYPE_INT64:
1122 case GLSL_TYPE_UINT16:
1123 case GLSL_TYPE_INT16:
1124 case GLSL_TYPE_UINT8:
1125 case GLSL_TYPE_INT8:
1126 return new(mem_ctx) ir_expression(operation, op0, op1);
1127
1128 case GLSL_TYPE_ARRAY: {
1129 for (unsigned int i = 0; i < op0->type->length; i++) {
1130 ir_rvalue *e0, *e1, *result;
1131
1132 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
1133 new(mem_ctx) ir_constant(i));
1134 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
1135 new(mem_ctx) ir_constant(i));
1136 result = do_comparison(mem_ctx, operation, e0, e1);
1137
1138 if (cmp) {
1139 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
1140 } else {
1141 cmp = result;
1142 }
1143 }
1144
1145 mark_whole_array_access(op0);
1146 mark_whole_array_access(op1);
1147 break;
1148 }
1149
1150 case GLSL_TYPE_STRUCT: {
1151 for (unsigned int i = 0; i < op0->type->length; i++) {
1152 ir_rvalue *e0, *e1, *result;
1153 const char *field_name = op0->type->fields.structure[i].name;
1154
1155 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
1156 field_name);
1157 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
1158 field_name);
1159 result = do_comparison(mem_ctx, operation, e0, e1);
1160
1161 if (cmp) {
1162 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
1163 } else {
1164 cmp = result;
1165 }
1166 }
1167 break;
1168 }
1169
1170 case GLSL_TYPE_ERROR:
1171 case GLSL_TYPE_VOID:
1172 case GLSL_TYPE_SAMPLER:
1173 case GLSL_TYPE_IMAGE:
1174 case GLSL_TYPE_INTERFACE:
1175 case GLSL_TYPE_ATOMIC_UINT:
1176 case GLSL_TYPE_SUBROUTINE:
1177 case GLSL_TYPE_FUNCTION:
1178 /* I assume a comparison of a struct containing a sampler just
1179 * ignores the sampler present in the type.
1180 */
1181 break;
1182 }
1183
1184 if (cmp == NULL)
1185 cmp = new(mem_ctx) ir_constant(true);
1186
1187 return cmp;
1188 }
1189
1190 /* For logical operations, we want to ensure that the operands are
1191 * scalar booleans. If it isn't, emit an error and return a constant
1192 * boolean to avoid triggering cascading error messages.
1193 */
1194 static ir_rvalue *
1195 get_scalar_boolean_operand(exec_list *instructions,
1196 struct _mesa_glsl_parse_state *state,
1197 ast_expression *parent_expr,
1198 int operand,
1199 const char *operand_name,
1200 bool *error_emitted)
1201 {
1202 ast_expression *expr = parent_expr->subexpressions[operand];
1203 void *ctx = state;
1204 ir_rvalue *val = expr->hir(instructions, state);
1205
1206 if (val->type->is_boolean() && val->type->is_scalar())
1207 return val;
1208
1209 if (!*error_emitted) {
1210 YYLTYPE loc = expr->get_location();
1211 _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
1212 operand_name,
1213 parent_expr->operator_string(parent_expr->oper));
1214 *error_emitted = true;
1215 }
1216
1217 return new(ctx) ir_constant(true);
1218 }
1219
1220 /**
1221 * If name refers to a builtin array whose maximum allowed size is less than
1222 * size, report an error and return true. Otherwise return false.
1223 */
1224 void
1225 check_builtin_array_max_size(const char *name, unsigned size,
1226 YYLTYPE loc, struct _mesa_glsl_parse_state *state)
1227 {
1228 if ((strcmp("gl_TexCoord", name) == 0)
1229 && (size > state->Const.MaxTextureCoords)) {
1230 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1231 *
1232 * "The size [of gl_TexCoord] can be at most
1233 * gl_MaxTextureCoords."
1234 */
1235 _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
1236 "be larger than gl_MaxTextureCoords (%u)",
1237 state->Const.MaxTextureCoords);
1238 } else if (strcmp("gl_ClipDistance", name) == 0) {
1239 state->clip_dist_size = size;
1240 if (size + state->cull_dist_size > state->Const.MaxClipPlanes) {
1241 /* From section 7.1 (Vertex Shader Special Variables) of the
1242 * GLSL 1.30 spec:
1243 *
1244 * "The gl_ClipDistance array is predeclared as unsized and
1245 * must be sized by the shader either redeclaring it with a
1246 * size or indexing it only with integral constant
1247 * expressions. ... The size can be at most
1248 * gl_MaxClipDistances."
1249 */
1250 _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
1251 "be larger than gl_MaxClipDistances (%u)",
1252 state->Const.MaxClipPlanes);
1253 }
1254 } else if (strcmp("gl_CullDistance", name) == 0) {
1255 state->cull_dist_size = size;
1256 if (size + state->clip_dist_size > state->Const.MaxClipPlanes) {
1257 /* From the ARB_cull_distance spec:
1258 *
1259 * "The gl_CullDistance array is predeclared as unsized and
1260 * must be sized by the shader either redeclaring it with
1261 * a size or indexing it only with integral constant
1262 * expressions. The size determines the number and set of
1263 * enabled cull distances and can be at most
1264 * gl_MaxCullDistances."
1265 */
1266 _mesa_glsl_error(&loc, state, "`gl_CullDistance' array size cannot "
1267 "be larger than gl_MaxCullDistances (%u)",
1268 state->Const.MaxClipPlanes);
1269 }
1270 }
1271 }
1272
1273 /**
1274 * Create the constant 1, of a which is appropriate for incrementing and
1275 * decrementing values of the given GLSL type. For example, if type is vec4,
1276 * this creates a constant value of 1.0 having type float.
1277 *
1278 * If the given type is invalid for increment and decrement operators, return
1279 * a floating point 1--the error will be detected later.
1280 */
1281 static ir_rvalue *
1282 constant_one_for_inc_dec(void *ctx, const glsl_type *type)
1283 {
1284 switch (type->base_type) {
1285 case GLSL_TYPE_UINT:
1286 return new(ctx) ir_constant((unsigned) 1);
1287 case GLSL_TYPE_INT:
1288 return new(ctx) ir_constant(1);
1289 case GLSL_TYPE_UINT64:
1290 return new(ctx) ir_constant((uint64_t) 1);
1291 case GLSL_TYPE_INT64:
1292 return new(ctx) ir_constant((int64_t) 1);
1293 default:
1294 case GLSL_TYPE_FLOAT:
1295 return new(ctx) ir_constant(1.0f);
1296 }
1297 }
1298
1299 ir_rvalue *
1300 ast_expression::hir(exec_list *instructions,
1301 struct _mesa_glsl_parse_state *state)
1302 {
1303 return do_hir(instructions, state, true);
1304 }
1305
1306 void
1307 ast_expression::hir_no_rvalue(exec_list *instructions,
1308 struct _mesa_glsl_parse_state *state)
1309 {
1310 do_hir(instructions, state, false);
1311 }
1312
1313 void
1314 ast_expression::set_is_lhs(bool new_value)
1315 {
1316 /* is_lhs is tracked only to print "variable used uninitialized" warnings,
1317 * if we lack an identifier we can just skip it.
1318 */
1319 if (this->primary_expression.identifier == NULL)
1320 return;
1321
1322 this->is_lhs = new_value;
1323
1324 /* We need to go through the subexpressions tree to cover cases like
1325 * ast_field_selection
1326 */
1327 if (this->subexpressions[0] != NULL)
1328 this->subexpressions[0]->set_is_lhs(new_value);
1329 }
1330
1331 ir_rvalue *
1332 ast_expression::do_hir(exec_list *instructions,
1333 struct _mesa_glsl_parse_state *state,
1334 bool needs_rvalue)
1335 {
1336 void *ctx = state;
1337 static const int operations[AST_NUM_OPERATORS] = {
1338 -1, /* ast_assign doesn't convert to ir_expression. */
1339 -1, /* ast_plus doesn't convert to ir_expression. */
1340 ir_unop_neg,
1341 ir_binop_add,
1342 ir_binop_sub,
1343 ir_binop_mul,
1344 ir_binop_div,
1345 ir_binop_mod,
1346 ir_binop_lshift,
1347 ir_binop_rshift,
1348 ir_binop_less,
1349 ir_binop_less, /* This is correct. See the ast_greater case below. */
1350 ir_binop_gequal, /* This is correct. See the ast_lequal case below. */
1351 ir_binop_gequal,
1352 ir_binop_all_equal,
1353 ir_binop_any_nequal,
1354 ir_binop_bit_and,
1355 ir_binop_bit_xor,
1356 ir_binop_bit_or,
1357 ir_unop_bit_not,
1358 ir_binop_logic_and,
1359 ir_binop_logic_xor,
1360 ir_binop_logic_or,
1361 ir_unop_logic_not,
1362
1363 /* Note: The following block of expression types actually convert
1364 * to multiple IR instructions.
1365 */
1366 ir_binop_mul, /* ast_mul_assign */
1367 ir_binop_div, /* ast_div_assign */
1368 ir_binop_mod, /* ast_mod_assign */
1369 ir_binop_add, /* ast_add_assign */
1370 ir_binop_sub, /* ast_sub_assign */
1371 ir_binop_lshift, /* ast_ls_assign */
1372 ir_binop_rshift, /* ast_rs_assign */
1373 ir_binop_bit_and, /* ast_and_assign */
1374 ir_binop_bit_xor, /* ast_xor_assign */
1375 ir_binop_bit_or, /* ast_or_assign */
1376
1377 -1, /* ast_conditional doesn't convert to ir_expression. */
1378 ir_binop_add, /* ast_pre_inc. */
1379 ir_binop_sub, /* ast_pre_dec. */
1380 ir_binop_add, /* ast_post_inc. */
1381 ir_binop_sub, /* ast_post_dec. */
1382 -1, /* ast_field_selection doesn't conv to ir_expression. */
1383 -1, /* ast_array_index doesn't convert to ir_expression. */
1384 -1, /* ast_function_call doesn't conv to ir_expression. */
1385 -1, /* ast_identifier doesn't convert to ir_expression. */
1386 -1, /* ast_int_constant doesn't convert to ir_expression. */
1387 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1388 -1, /* ast_float_constant doesn't conv to ir_expression. */
1389 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1390 -1, /* ast_sequence doesn't convert to ir_expression. */
1391 -1, /* ast_aggregate shouldn't ever even get here. */
1392 };
1393 ir_rvalue *result = NULL;
1394 ir_rvalue *op[3];
1395 const struct glsl_type *type, *orig_type;
1396 bool error_emitted = false;
1397 YYLTYPE loc;
1398
1399 loc = this->get_location();
1400
1401 switch (this->oper) {
1402 case ast_aggregate:
1403 unreachable("ast_aggregate: Should never get here.");
1404
1405 case ast_assign: {
1406 this->subexpressions[0]->set_is_lhs(true);
1407 op[0] = this->subexpressions[0]->hir(instructions, state);
1408 op[1] = this->subexpressions[1]->hir(instructions, state);
1409
1410 error_emitted =
1411 do_assignment(instructions, state,
1412 this->subexpressions[0]->non_lvalue_description,
1413 op[0], op[1], &result, needs_rvalue, false,
1414 this->subexpressions[0]->get_location());
1415 break;
1416 }
1417
1418 case ast_plus:
1419 op[0] = this->subexpressions[0]->hir(instructions, state);
1420
1421 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1422
1423 error_emitted = type->is_error();
1424
1425 result = op[0];
1426 break;
1427
1428 case ast_neg:
1429 op[0] = this->subexpressions[0]->hir(instructions, state);
1430
1431 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1432
1433 error_emitted = type->is_error();
1434
1435 result = new(ctx) ir_expression(operations[this->oper], type,
1436 op[0], NULL);
1437 break;
1438
1439 case ast_add:
1440 case ast_sub:
1441 case ast_mul:
1442 case ast_div:
1443 op[0] = this->subexpressions[0]->hir(instructions, state);
1444 op[1] = this->subexpressions[1]->hir(instructions, state);
1445
1446 type = arithmetic_result_type(op[0], op[1],
1447 (this->oper == ast_mul),
1448 state, & loc);
1449 error_emitted = type->is_error();
1450
1451 result = new(ctx) ir_expression(operations[this->oper], type,
1452 op[0], op[1]);
1453 break;
1454
1455 case ast_mod:
1456 op[0] = this->subexpressions[0]->hir(instructions, state);
1457 op[1] = this->subexpressions[1]->hir(instructions, state);
1458
1459 type = modulus_result_type(op[0], op[1], state, &loc);
1460
1461 assert(operations[this->oper] == ir_binop_mod);
1462
1463 result = new(ctx) ir_expression(operations[this->oper], type,
1464 op[0], op[1]);
1465 error_emitted = type->is_error();
1466 break;
1467
1468 case ast_lshift:
1469 case ast_rshift:
1470 if (!state->check_bitwise_operations_allowed(&loc)) {
1471 error_emitted = true;
1472 }
1473
1474 op[0] = this->subexpressions[0]->hir(instructions, state);
1475 op[1] = this->subexpressions[1]->hir(instructions, state);
1476 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1477 &loc);
1478 result = new(ctx) ir_expression(operations[this->oper], type,
1479 op[0], op[1]);
1480 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1481 break;
1482
1483 case ast_less:
1484 case ast_greater:
1485 case ast_lequal:
1486 case ast_gequal:
1487 op[0] = this->subexpressions[0]->hir(instructions, state);
1488 op[1] = this->subexpressions[1]->hir(instructions, state);
1489
1490 type = relational_result_type(op[0], op[1], state, & loc);
1491
1492 /* The relational operators must either generate an error or result
1493 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1494 */
1495 assert(type->is_error()
1496 || (type->is_boolean() && type->is_scalar()));
1497
1498 /* Like NIR, GLSL IR does not have opcodes for > or <=. Instead, swap
1499 * the arguments and use < or >=.
1500 */
1501 if (this->oper == ast_greater || this->oper == ast_lequal) {
1502 ir_rvalue *const tmp = op[0];
1503 op[0] = op[1];
1504 op[1] = tmp;
1505 }
1506
1507 result = new(ctx) ir_expression(operations[this->oper], type,
1508 op[0], op[1]);
1509 error_emitted = type->is_error();
1510 break;
1511
1512 case ast_nequal:
1513 case ast_equal:
1514 op[0] = this->subexpressions[0]->hir(instructions, state);
1515 op[1] = this->subexpressions[1]->hir(instructions, state);
1516
1517 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1518 *
1519 * "The equality operators equal (==), and not equal (!=)
1520 * operate on all types. They result in a scalar Boolean. If
1521 * the operand types do not match, then there must be a
1522 * conversion from Section 4.1.10 "Implicit Conversions"
1523 * applied to one operand that can make them match, in which
1524 * case this conversion is done."
1525 */
1526
1527 if (op[0]->type == glsl_type::void_type || op[1]->type == glsl_type::void_type) {
1528 _mesa_glsl_error(& loc, state, "`%s': wrong operand types: "
1529 "no operation `%1$s' exists that takes a left-hand "
1530 "operand of type 'void' or a right operand of type "
1531 "'void'", (this->oper == ast_equal) ? "==" : "!=");
1532 error_emitted = true;
1533 } else if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1534 && !apply_implicit_conversion(op[1]->type, op[0], state))
1535 || (op[0]->type != op[1]->type)) {
1536 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
1537 "type", (this->oper == ast_equal) ? "==" : "!=");
1538 error_emitted = true;
1539 } else if ((op[0]->type->is_array() || op[1]->type->is_array()) &&
1540 !state->check_version(120, 300, &loc,
1541 "array comparisons forbidden")) {
1542 error_emitted = true;
1543 } else if ((op[0]->type->contains_subroutine() ||
1544 op[1]->type->contains_subroutine())) {
1545 _mesa_glsl_error(&loc, state, "subroutine comparisons forbidden");
1546 error_emitted = true;
1547 } else if ((op[0]->type->contains_opaque() ||
1548 op[1]->type->contains_opaque())) {
1549 _mesa_glsl_error(&loc, state, "opaque type comparisons forbidden");
1550 error_emitted = true;
1551 }
1552
1553 if (error_emitted) {
1554 result = new(ctx) ir_constant(false);
1555 } else {
1556 result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
1557 assert(result->type == glsl_type::bool_type);
1558 }
1559 break;
1560
1561 case ast_bit_and:
1562 case ast_bit_xor:
1563 case ast_bit_or:
1564 op[0] = this->subexpressions[0]->hir(instructions, state);
1565 op[1] = this->subexpressions[1]->hir(instructions, state);
1566 type = bit_logic_result_type(op[0], op[1], this->oper, state, &loc);
1567 result = new(ctx) ir_expression(operations[this->oper], type,
1568 op[0], op[1]);
1569 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1570 break;
1571
1572 case ast_bit_not:
1573 op[0] = this->subexpressions[0]->hir(instructions, state);
1574
1575 if (!state->check_bitwise_operations_allowed(&loc)) {
1576 error_emitted = true;
1577 }
1578
1579 if (!op[0]->type->is_integer_32_64()) {
1580 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
1581 error_emitted = true;
1582 }
1583
1584 type = error_emitted ? glsl_type::error_type : op[0]->type;
1585 result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
1586 break;
1587
1588 case ast_logic_and: {
1589 exec_list rhs_instructions;
1590 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1591 "LHS", &error_emitted);
1592 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1593 "RHS", &error_emitted);
1594
1595 if (rhs_instructions.is_empty()) {
1596 result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
1597 } else {
1598 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
1599 "and_tmp",
1600 ir_var_temporary);
1601 instructions->push_tail(tmp);
1602
1603 ir_if *const stmt = new(ctx) ir_if(op[0]);
1604 instructions->push_tail(stmt);
1605
1606 stmt->then_instructions.append_list(&rhs_instructions);
1607 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1608 ir_assignment *const then_assign =
1609 new(ctx) ir_assignment(then_deref, op[1]);
1610 stmt->then_instructions.push_tail(then_assign);
1611
1612 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1613 ir_assignment *const else_assign =
1614 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false));
1615 stmt->else_instructions.push_tail(else_assign);
1616
1617 result = new(ctx) ir_dereference_variable(tmp);
1618 }
1619 break;
1620 }
1621
1622 case ast_logic_or: {
1623 exec_list rhs_instructions;
1624 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1625 "LHS", &error_emitted);
1626 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1627 "RHS", &error_emitted);
1628
1629 if (rhs_instructions.is_empty()) {
1630 result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
1631 } else {
1632 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
1633 "or_tmp",
1634 ir_var_temporary);
1635 instructions->push_tail(tmp);
1636
1637 ir_if *const stmt = new(ctx) ir_if(op[0]);
1638 instructions->push_tail(stmt);
1639
1640 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1641 ir_assignment *const then_assign =
1642 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true));
1643 stmt->then_instructions.push_tail(then_assign);
1644
1645 stmt->else_instructions.append_list(&rhs_instructions);
1646 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1647 ir_assignment *const else_assign =
1648 new(ctx) ir_assignment(else_deref, op[1]);
1649 stmt->else_instructions.push_tail(else_assign);
1650
1651 result = new(ctx) ir_dereference_variable(tmp);
1652 }
1653 break;
1654 }
1655
1656 case ast_logic_xor:
1657 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1658 *
1659 * "The logical binary operators and (&&), or ( | | ), and
1660 * exclusive or (^^). They operate only on two Boolean
1661 * expressions and result in a Boolean expression."
1662 */
1663 op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
1664 &error_emitted);
1665 op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
1666 &error_emitted);
1667
1668 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1669 op[0], op[1]);
1670 break;
1671
1672 case ast_logic_not:
1673 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1674 "operand", &error_emitted);
1675
1676 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1677 op[0], NULL);
1678 break;
1679
1680 case ast_mul_assign:
1681 case ast_div_assign:
1682 case ast_add_assign:
1683 case ast_sub_assign: {
1684 this->subexpressions[0]->set_is_lhs(true);
1685 op[0] = this->subexpressions[0]->hir(instructions, state);
1686 op[1] = this->subexpressions[1]->hir(instructions, state);
1687
1688 orig_type = op[0]->type;
1689
1690 /* Break out if operand types were not parsed successfully. */
1691 if ((op[0]->type == glsl_type::error_type ||
1692 op[1]->type == glsl_type::error_type)) {
1693 error_emitted = true;
1694 break;
1695 }
1696
1697 type = arithmetic_result_type(op[0], op[1],
1698 (this->oper == ast_mul_assign),
1699 state, & loc);
1700
1701 if (type != orig_type) {
1702 _mesa_glsl_error(& loc, state,
1703 "could not implicitly convert "
1704 "%s to %s", type->name, orig_type->name);
1705 type = glsl_type::error_type;
1706 }
1707
1708 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1709 op[0], op[1]);
1710
1711 error_emitted =
1712 do_assignment(instructions, state,
1713 this->subexpressions[0]->non_lvalue_description,
1714 op[0]->clone(ctx, NULL), temp_rhs,
1715 &result, needs_rvalue, false,
1716 this->subexpressions[0]->get_location());
1717
1718 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1719 * explicitly test for this because none of the binary expression
1720 * operators allow array operands either.
1721 */
1722
1723 break;
1724 }
1725
1726 case ast_mod_assign: {
1727 this->subexpressions[0]->set_is_lhs(true);
1728 op[0] = this->subexpressions[0]->hir(instructions, state);
1729 op[1] = this->subexpressions[1]->hir(instructions, state);
1730
1731 orig_type = op[0]->type;
1732 type = modulus_result_type(op[0], op[1], state, &loc);
1733
1734 if (type != orig_type) {
1735 _mesa_glsl_error(& loc, state,
1736 "could not implicitly convert "
1737 "%s to %s", type->name, orig_type->name);
1738 type = glsl_type::error_type;
1739 }
1740
1741 assert(operations[this->oper] == ir_binop_mod);
1742
1743 ir_rvalue *temp_rhs;
1744 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1745 op[0], op[1]);
1746
1747 error_emitted =
1748 do_assignment(instructions, state,
1749 this->subexpressions[0]->non_lvalue_description,
1750 op[0]->clone(ctx, NULL), temp_rhs,
1751 &result, needs_rvalue, false,
1752 this->subexpressions[0]->get_location());
1753 break;
1754 }
1755
1756 case ast_ls_assign:
1757 case ast_rs_assign: {
1758 this->subexpressions[0]->set_is_lhs(true);
1759 op[0] = this->subexpressions[0]->hir(instructions, state);
1760 op[1] = this->subexpressions[1]->hir(instructions, state);
1761 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1762 &loc);
1763 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1764 type, op[0], op[1]);
1765 error_emitted =
1766 do_assignment(instructions, state,
1767 this->subexpressions[0]->non_lvalue_description,
1768 op[0]->clone(ctx, NULL), temp_rhs,
1769 &result, needs_rvalue, false,
1770 this->subexpressions[0]->get_location());
1771 break;
1772 }
1773
1774 case ast_and_assign:
1775 case ast_xor_assign:
1776 case ast_or_assign: {
1777 this->subexpressions[0]->set_is_lhs(true);
1778 op[0] = this->subexpressions[0]->hir(instructions, state);
1779 op[1] = this->subexpressions[1]->hir(instructions, state);
1780
1781 orig_type = op[0]->type;
1782 type = bit_logic_result_type(op[0], op[1], this->oper, state, &loc);
1783
1784 if (type != orig_type) {
1785 _mesa_glsl_error(& loc, state,
1786 "could not implicitly convert "
1787 "%s to %s", type->name, orig_type->name);
1788 type = glsl_type::error_type;
1789 }
1790
1791 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1792 type, op[0], op[1]);
1793 error_emitted =
1794 do_assignment(instructions, state,
1795 this->subexpressions[0]->non_lvalue_description,
1796 op[0]->clone(ctx, NULL), temp_rhs,
1797 &result, needs_rvalue, false,
1798 this->subexpressions[0]->get_location());
1799 break;
1800 }
1801
1802 case ast_conditional: {
1803 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1804 *
1805 * "The ternary selection operator (?:). It operates on three
1806 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1807 * first expression, which must result in a scalar Boolean."
1808 */
1809 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1810 "condition", &error_emitted);
1811
1812 /* The :? operator is implemented by generating an anonymous temporary
1813 * followed by an if-statement. The last instruction in each branch of
1814 * the if-statement assigns a value to the anonymous temporary. This
1815 * temporary is the r-value of the expression.
1816 */
1817 exec_list then_instructions;
1818 exec_list else_instructions;
1819
1820 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1821 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
1822
1823 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1824 *
1825 * "The second and third expressions can be any type, as
1826 * long their types match, or there is a conversion in
1827 * Section 4.1.10 "Implicit Conversions" that can be applied
1828 * to one of the expressions to make their types match. This
1829 * resulting matching type is the type of the entire
1830 * expression."
1831 */
1832 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1833 && !apply_implicit_conversion(op[2]->type, op[1], state))
1834 || (op[1]->type != op[2]->type)) {
1835 YYLTYPE loc = this->subexpressions[1]->get_location();
1836
1837 _mesa_glsl_error(& loc, state, "second and third operands of ?: "
1838 "operator must have matching types");
1839 error_emitted = true;
1840 type = glsl_type::error_type;
1841 } else {
1842 type = op[1]->type;
1843 }
1844
1845 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1846 *
1847 * "The second and third expressions must be the same type, but can
1848 * be of any type other than an array."
1849 */
1850 if (type->is_array() &&
1851 !state->check_version(120, 300, &loc,
1852 "second and third operands of ?: operator "
1853 "cannot be arrays")) {
1854 error_emitted = true;
1855 }
1856
1857 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1858 *
1859 * "Except for array indexing, structure member selection, and
1860 * parentheses, opaque variables are not allowed to be operands in
1861 * expressions; such use results in a compile-time error."
1862 */
1863 if (type->contains_opaque()) {
1864 if (!(state->has_bindless() && (type->is_image() || type->is_sampler()))) {
1865 _mesa_glsl_error(&loc, state, "variables of type %s cannot be "
1866 "operands of the ?: operator", type->name);
1867 error_emitted = true;
1868 }
1869 }
1870
1871 ir_constant *cond_val = op[0]->constant_expression_value(ctx);
1872
1873 if (then_instructions.is_empty()
1874 && else_instructions.is_empty()
1875 && cond_val != NULL) {
1876 result = cond_val->value.b[0] ? op[1] : op[2];
1877 } else {
1878 /* The copy to conditional_tmp reads the whole array. */
1879 if (type->is_array()) {
1880 mark_whole_array_access(op[1]);
1881 mark_whole_array_access(op[2]);
1882 }
1883
1884 ir_variable *const tmp =
1885 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
1886 instructions->push_tail(tmp);
1887
1888 ir_if *const stmt = new(ctx) ir_if(op[0]);
1889 instructions->push_tail(stmt);
1890
1891 then_instructions.move_nodes_to(& stmt->then_instructions);
1892 ir_dereference *const then_deref =
1893 new(ctx) ir_dereference_variable(tmp);
1894 ir_assignment *const then_assign =
1895 new(ctx) ir_assignment(then_deref, op[1]);
1896 stmt->then_instructions.push_tail(then_assign);
1897
1898 else_instructions.move_nodes_to(& stmt->else_instructions);
1899 ir_dereference *const else_deref =
1900 new(ctx) ir_dereference_variable(tmp);
1901 ir_assignment *const else_assign =
1902 new(ctx) ir_assignment(else_deref, op[2]);
1903 stmt->else_instructions.push_tail(else_assign);
1904
1905 result = new(ctx) ir_dereference_variable(tmp);
1906 }
1907 break;
1908 }
1909
1910 case ast_pre_inc:
1911 case ast_pre_dec: {
1912 this->non_lvalue_description = (this->oper == ast_pre_inc)
1913 ? "pre-increment operation" : "pre-decrement operation";
1914
1915 op[0] = this->subexpressions[0]->hir(instructions, state);
1916 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1917
1918 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1919
1920 ir_rvalue *temp_rhs;
1921 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1922 op[0], op[1]);
1923
1924 error_emitted =
1925 do_assignment(instructions, state,
1926 this->subexpressions[0]->non_lvalue_description,
1927 op[0]->clone(ctx, NULL), temp_rhs,
1928 &result, needs_rvalue, false,
1929 this->subexpressions[0]->get_location());
1930 break;
1931 }
1932
1933 case ast_post_inc:
1934 case ast_post_dec: {
1935 this->non_lvalue_description = (this->oper == ast_post_inc)
1936 ? "post-increment operation" : "post-decrement operation";
1937 op[0] = this->subexpressions[0]->hir(instructions, state);
1938 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1939
1940 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1941
1942 if (error_emitted) {
1943 result = ir_rvalue::error_value(ctx);
1944 break;
1945 }
1946
1947 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1948
1949 ir_rvalue *temp_rhs;
1950 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1951 op[0], op[1]);
1952
1953 /* Get a temporary of a copy of the lvalue before it's modified.
1954 * This may get thrown away later.
1955 */
1956 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1957
1958 ir_rvalue *junk_rvalue;
1959 error_emitted =
1960 do_assignment(instructions, state,
1961 this->subexpressions[0]->non_lvalue_description,
1962 op[0]->clone(ctx, NULL), temp_rhs,
1963 &junk_rvalue, false, false,
1964 this->subexpressions[0]->get_location());
1965
1966 break;
1967 }
1968
1969 case ast_field_selection:
1970 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1971 break;
1972
1973 case ast_array_index: {
1974 YYLTYPE index_loc = subexpressions[1]->get_location();
1975
1976 /* Getting if an array is being used uninitialized is beyond what we get
1977 * from ir_value.data.assigned. Setting is_lhs as true would force to
1978 * not raise a uninitialized warning when using an array
1979 */
1980 subexpressions[0]->set_is_lhs(true);
1981 op[0] = subexpressions[0]->hir(instructions, state);
1982 op[1] = subexpressions[1]->hir(instructions, state);
1983
1984 result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1],
1985 loc, index_loc);
1986
1987 if (result->type->is_error())
1988 error_emitted = true;
1989
1990 break;
1991 }
1992
1993 case ast_unsized_array_dim:
1994 unreachable("ast_unsized_array_dim: Should never get here.");
1995
1996 case ast_function_call:
1997 /* Should *NEVER* get here. ast_function_call should always be handled
1998 * by ast_function_expression::hir.
1999 */
2000 unreachable("ast_function_call: handled elsewhere ");
2001
2002 case ast_identifier: {
2003 /* ast_identifier can appear several places in a full abstract syntax
2004 * tree. This particular use must be at location specified in the grammar
2005 * as 'variable_identifier'.
2006 */
2007 ir_variable *var =
2008 state->symbols->get_variable(this->primary_expression.identifier);
2009
2010 if (var == NULL) {
2011 /* the identifier might be a subroutine name */
2012 char *sub_name;
2013 sub_name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), this->primary_expression.identifier);
2014 var = state->symbols->get_variable(sub_name);
2015 ralloc_free(sub_name);
2016 }
2017
2018 if (var != NULL) {
2019 var->data.used = true;
2020 result = new(ctx) ir_dereference_variable(var);
2021
2022 if ((var->data.mode == ir_var_auto || var->data.mode == ir_var_shader_out)
2023 && !this->is_lhs
2024 && result->variable_referenced()->data.assigned != true
2025 && !is_gl_identifier(var->name)) {
2026 _mesa_glsl_warning(&loc, state, "`%s' used uninitialized",
2027 this->primary_expression.identifier);
2028 }
2029
2030 /* From the EXT_shader_framebuffer_fetch spec:
2031 *
2032 * "Unless the GL_EXT_shader_framebuffer_fetch extension has been
2033 * enabled in addition, it's an error to use gl_LastFragData if it
2034 * hasn't been explicitly redeclared with layout(noncoherent)."
2035 */
2036 if (var->data.fb_fetch_output && var->data.memory_coherent &&
2037 !state->EXT_shader_framebuffer_fetch_enable) {
2038 _mesa_glsl_error(&loc, state,
2039 "invalid use of framebuffer fetch output not "
2040 "qualified with layout(noncoherent)");
2041 }
2042
2043 } else {
2044 _mesa_glsl_error(& loc, state, "`%s' undeclared",
2045 this->primary_expression.identifier);
2046
2047 result = ir_rvalue::error_value(ctx);
2048 error_emitted = true;
2049 }
2050 break;
2051 }
2052
2053 case ast_int_constant:
2054 result = new(ctx) ir_constant(this->primary_expression.int_constant);
2055 break;
2056
2057 case ast_uint_constant:
2058 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
2059 break;
2060
2061 case ast_float_constant:
2062 result = new(ctx) ir_constant(this->primary_expression.float_constant);
2063 break;
2064
2065 case ast_bool_constant:
2066 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
2067 break;
2068
2069 case ast_double_constant:
2070 result = new(ctx) ir_constant(this->primary_expression.double_constant);
2071 break;
2072
2073 case ast_uint64_constant:
2074 result = new(ctx) ir_constant(this->primary_expression.uint64_constant);
2075 break;
2076
2077 case ast_int64_constant:
2078 result = new(ctx) ir_constant(this->primary_expression.int64_constant);
2079 break;
2080
2081 case ast_sequence: {
2082 /* It should not be possible to generate a sequence in the AST without
2083 * any expressions in it.
2084 */
2085 assert(!this->expressions.is_empty());
2086
2087 /* The r-value of a sequence is the last expression in the sequence. If
2088 * the other expressions in the sequence do not have side-effects (and
2089 * therefore add instructions to the instruction list), they get dropped
2090 * on the floor.
2091 */
2092 exec_node *previous_tail = NULL;
2093 YYLTYPE previous_operand_loc = loc;
2094
2095 foreach_list_typed (ast_node, ast, link, &this->expressions) {
2096 /* If one of the operands of comma operator does not generate any
2097 * code, we want to emit a warning. At each pass through the loop
2098 * previous_tail will point to the last instruction in the stream
2099 * *before* processing the previous operand. Naturally,
2100 * instructions->get_tail_raw() will point to the last instruction in
2101 * the stream *after* processing the previous operand. If the two
2102 * pointers match, then the previous operand had no effect.
2103 *
2104 * The warning behavior here differs slightly from GCC. GCC will
2105 * only emit a warning if none of the left-hand operands have an
2106 * effect. However, it will emit a warning for each. I believe that
2107 * there are some cases in C (especially with GCC extensions) where
2108 * it is useful to have an intermediate step in a sequence have no
2109 * effect, but I don't think these cases exist in GLSL. Either way,
2110 * it would be a giant hassle to replicate that behavior.
2111 */
2112 if (previous_tail == instructions->get_tail_raw()) {
2113 _mesa_glsl_warning(&previous_operand_loc, state,
2114 "left-hand operand of comma expression has "
2115 "no effect");
2116 }
2117
2118 /* The tail is directly accessed instead of using the get_tail()
2119 * method for performance reasons. get_tail() has extra code to
2120 * return NULL when the list is empty. We don't care about that
2121 * here, so using get_tail_raw() is fine.
2122 */
2123 previous_tail = instructions->get_tail_raw();
2124 previous_operand_loc = ast->get_location();
2125
2126 result = ast->hir(instructions, state);
2127 }
2128
2129 /* Any errors should have already been emitted in the loop above.
2130 */
2131 error_emitted = true;
2132 break;
2133 }
2134 }
2135 type = NULL; /* use result->type, not type. */
2136 assert(error_emitted || (result != NULL || !needs_rvalue));
2137
2138 if (result && result->type->is_error() && !error_emitted)
2139 _mesa_glsl_error(& loc, state, "type mismatch");
2140
2141 return result;
2142 }
2143
2144 bool
2145 ast_expression::has_sequence_subexpression() const
2146 {
2147 switch (this->oper) {
2148 case ast_plus:
2149 case ast_neg:
2150 case ast_bit_not:
2151 case ast_logic_not:
2152 case ast_pre_inc:
2153 case ast_pre_dec:
2154 case ast_post_inc:
2155 case ast_post_dec:
2156 return this->subexpressions[0]->has_sequence_subexpression();
2157
2158 case ast_assign:
2159 case ast_add:
2160 case ast_sub:
2161 case ast_mul:
2162 case ast_div:
2163 case ast_mod:
2164 case ast_lshift:
2165 case ast_rshift:
2166 case ast_less:
2167 case ast_greater:
2168 case ast_lequal:
2169 case ast_gequal:
2170 case ast_nequal:
2171 case ast_equal:
2172 case ast_bit_and:
2173 case ast_bit_xor:
2174 case ast_bit_or:
2175 case ast_logic_and:
2176 case ast_logic_or:
2177 case ast_logic_xor:
2178 case ast_array_index:
2179 case ast_mul_assign:
2180 case ast_div_assign:
2181 case ast_add_assign:
2182 case ast_sub_assign:
2183 case ast_mod_assign:
2184 case ast_ls_assign:
2185 case ast_rs_assign:
2186 case ast_and_assign:
2187 case ast_xor_assign:
2188 case ast_or_assign:
2189 return this->subexpressions[0]->has_sequence_subexpression() ||
2190 this->subexpressions[1]->has_sequence_subexpression();
2191
2192 case ast_conditional:
2193 return this->subexpressions[0]->has_sequence_subexpression() ||
2194 this->subexpressions[1]->has_sequence_subexpression() ||
2195 this->subexpressions[2]->has_sequence_subexpression();
2196
2197 case ast_sequence:
2198 return true;
2199
2200 case ast_field_selection:
2201 case ast_identifier:
2202 case ast_int_constant:
2203 case ast_uint_constant:
2204 case ast_float_constant:
2205 case ast_bool_constant:
2206 case ast_double_constant:
2207 case ast_int64_constant:
2208 case ast_uint64_constant:
2209 return false;
2210
2211 case ast_aggregate:
2212 return false;
2213
2214 case ast_function_call:
2215 unreachable("should be handled by ast_function_expression::hir");
2216
2217 case ast_unsized_array_dim:
2218 unreachable("ast_unsized_array_dim: Should never get here.");
2219 }
2220
2221 return false;
2222 }
2223
2224 ir_rvalue *
2225 ast_expression_statement::hir(exec_list *instructions,
2226 struct _mesa_glsl_parse_state *state)
2227 {
2228 /* It is possible to have expression statements that don't have an
2229 * expression. This is the solitary semicolon:
2230 *
2231 * for (i = 0; i < 5; i++)
2232 * ;
2233 *
2234 * In this case the expression will be NULL. Test for NULL and don't do
2235 * anything in that case.
2236 */
2237 if (expression != NULL)
2238 expression->hir_no_rvalue(instructions, state);
2239
2240 /* Statements do not have r-values.
2241 */
2242 return NULL;
2243 }
2244
2245
2246 ir_rvalue *
2247 ast_compound_statement::hir(exec_list *instructions,
2248 struct _mesa_glsl_parse_state *state)
2249 {
2250 if (new_scope)
2251 state->symbols->push_scope();
2252
2253 foreach_list_typed (ast_node, ast, link, &this->statements)
2254 ast->hir(instructions, state);
2255
2256 if (new_scope)
2257 state->symbols->pop_scope();
2258
2259 /* Compound statements do not have r-values.
2260 */
2261 return NULL;
2262 }
2263
2264 /**
2265 * Evaluate the given exec_node (which should be an ast_node representing
2266 * a single array dimension) and return its integer value.
2267 */
2268 static unsigned
2269 process_array_size(exec_node *node,
2270 struct _mesa_glsl_parse_state *state)
2271 {
2272 void *mem_ctx = state;
2273
2274 exec_list dummy_instructions;
2275
2276 ast_node *array_size = exec_node_data(ast_node, node, link);
2277
2278 /**
2279 * Dimensions other than the outermost dimension can by unsized if they
2280 * are immediately sized by a constructor or initializer.
2281 */
2282 if (((ast_expression*)array_size)->oper == ast_unsized_array_dim)
2283 return 0;
2284
2285 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
2286 YYLTYPE loc = array_size->get_location();
2287
2288 if (ir == NULL) {
2289 _mesa_glsl_error(& loc, state,
2290 "array size could not be resolved");
2291 return 0;
2292 }
2293
2294 if (!ir->type->is_integer_32()) {
2295 _mesa_glsl_error(& loc, state,
2296 "array size must be integer type");
2297 return 0;
2298 }
2299
2300 if (!ir->type->is_scalar()) {
2301 _mesa_glsl_error(& loc, state,
2302 "array size must be scalar type");
2303 return 0;
2304 }
2305
2306 ir_constant *const size = ir->constant_expression_value(mem_ctx);
2307 if (size == NULL ||
2308 (state->is_version(120, 300) &&
2309 array_size->has_sequence_subexpression())) {
2310 _mesa_glsl_error(& loc, state, "array size must be a "
2311 "constant valued expression");
2312 return 0;
2313 }
2314
2315 if (size->value.i[0] <= 0) {
2316 _mesa_glsl_error(& loc, state, "array size must be > 0");
2317 return 0;
2318 }
2319
2320 assert(size->type == ir->type);
2321
2322 /* If the array size is const (and we've verified that
2323 * it is) then no instructions should have been emitted
2324 * when we converted it to HIR. If they were emitted,
2325 * then either the array size isn't const after all, or
2326 * we are emitting unnecessary instructions.
2327 */
2328 assert(dummy_instructions.is_empty());
2329
2330 return size->value.u[0];
2331 }
2332
2333 static const glsl_type *
2334 process_array_type(YYLTYPE *loc, const glsl_type *base,
2335 ast_array_specifier *array_specifier,
2336 struct _mesa_glsl_parse_state *state)
2337 {
2338 const glsl_type *array_type = base;
2339
2340 if (array_specifier != NULL) {
2341 if (base->is_array()) {
2342
2343 /* From page 19 (page 25) of the GLSL 1.20 spec:
2344 *
2345 * "Only one-dimensional arrays may be declared."
2346 */
2347 if (!state->check_arrays_of_arrays_allowed(loc)) {
2348 return glsl_type::error_type;
2349 }
2350 }
2351
2352 for (exec_node *node = array_specifier->array_dimensions.get_tail_raw();
2353 !node->is_head_sentinel(); node = node->prev) {
2354 unsigned array_size = process_array_size(node, state);
2355 array_type = glsl_type::get_array_instance(array_type, array_size);
2356 }
2357 }
2358
2359 return array_type;
2360 }
2361
2362 static bool
2363 precision_qualifier_allowed(const glsl_type *type)
2364 {
2365 /* Precision qualifiers apply to floating point, integer and opaque
2366 * types.
2367 *
2368 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
2369 * "Any floating point or any integer declaration can have the type
2370 * preceded by one of these precision qualifiers [...] Literal
2371 * constants do not have precision qualifiers. Neither do Boolean
2372 * variables.
2373 *
2374 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
2375 * spec also says:
2376 *
2377 * "Precision qualifiers are added for code portability with OpenGL
2378 * ES, not for functionality. They have the same syntax as in OpenGL
2379 * ES."
2380 *
2381 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
2382 *
2383 * "uniform lowp sampler2D sampler;
2384 * highp vec2 coord;
2385 * ...
2386 * lowp vec4 col = texture2D (sampler, coord);
2387 * // texture2D returns lowp"
2388 *
2389 * From this, we infer that GLSL 1.30 (and later) should allow precision
2390 * qualifiers on sampler types just like float and integer types.
2391 */
2392 const glsl_type *const t = type->without_array();
2393
2394 return (t->is_float() || t->is_integer_32() || t->contains_opaque()) &&
2395 !t->is_struct();
2396 }
2397
2398 const glsl_type *
2399 ast_type_specifier::glsl_type(const char **name,
2400 struct _mesa_glsl_parse_state *state) const
2401 {
2402 const struct glsl_type *type;
2403
2404 if (this->type != NULL)
2405 type = this->type;
2406 else if (structure)
2407 type = structure->type;
2408 else
2409 type = state->symbols->get_type(this->type_name);
2410 *name = this->type_name;
2411
2412 YYLTYPE loc = this->get_location();
2413 type = process_array_type(&loc, type, this->array_specifier, state);
2414
2415 return type;
2416 }
2417
2418 /**
2419 * From the OpenGL ES 3.0 spec, 4.5.4 Default Precision Qualifiers:
2420 *
2421 * "The precision statement
2422 *
2423 * precision precision-qualifier type;
2424 *
2425 * can be used to establish a default precision qualifier. The type field can
2426 * be either int or float or any of the sampler types, (...) If type is float,
2427 * the directive applies to non-precision-qualified floating point type
2428 * (scalar, vector, and matrix) declarations. If type is int, the directive
2429 * applies to all non-precision-qualified integer type (scalar, vector, signed,
2430 * and unsigned) declarations."
2431 *
2432 * We use the symbol table to keep the values of the default precisions for
2433 * each 'type' in each scope and we use the 'type' string from the precision
2434 * statement as key in the symbol table. When we want to retrieve the default
2435 * precision associated with a given glsl_type we need to know the type string
2436 * associated with it. This is what this function returns.
2437 */
2438 static const char *
2439 get_type_name_for_precision_qualifier(const glsl_type *type)
2440 {
2441 switch (type->base_type) {
2442 case GLSL_TYPE_FLOAT:
2443 return "float";
2444 case GLSL_TYPE_UINT:
2445 case GLSL_TYPE_INT:
2446 return "int";
2447 case GLSL_TYPE_ATOMIC_UINT:
2448 return "atomic_uint";
2449 case GLSL_TYPE_IMAGE:
2450 /* fallthrough */
2451 case GLSL_TYPE_SAMPLER: {
2452 const unsigned type_idx =
2453 type->sampler_array + 2 * type->sampler_shadow;
2454 const unsigned offset = type->is_sampler() ? 0 : 4;
2455 assert(type_idx < 4);
2456 switch (type->sampled_type) {
2457 case GLSL_TYPE_FLOAT:
2458 switch (type->sampler_dimensionality) {
2459 case GLSL_SAMPLER_DIM_1D: {
2460 assert(type->is_sampler());
2461 static const char *const names[4] = {
2462 "sampler1D", "sampler1DArray",
2463 "sampler1DShadow", "sampler1DArrayShadow"
2464 };
2465 return names[type_idx];
2466 }
2467 case GLSL_SAMPLER_DIM_2D: {
2468 static const char *const names[8] = {
2469 "sampler2D", "sampler2DArray",
2470 "sampler2DShadow", "sampler2DArrayShadow",
2471 "image2D", "image2DArray", NULL, NULL
2472 };
2473 return names[offset + type_idx];
2474 }
2475 case GLSL_SAMPLER_DIM_3D: {
2476 static const char *const names[8] = {
2477 "sampler3D", NULL, NULL, NULL,
2478 "image3D", NULL, NULL, NULL
2479 };
2480 return names[offset + type_idx];
2481 }
2482 case GLSL_SAMPLER_DIM_CUBE: {
2483 static const char *const names[8] = {
2484 "samplerCube", "samplerCubeArray",
2485 "samplerCubeShadow", "samplerCubeArrayShadow",
2486 "imageCube", NULL, NULL, NULL
2487 };
2488 return names[offset + type_idx];
2489 }
2490 case GLSL_SAMPLER_DIM_MS: {
2491 assert(type->is_sampler());
2492 static const char *const names[4] = {
2493 "sampler2DMS", "sampler2DMSArray", NULL, NULL
2494 };
2495 return names[type_idx];
2496 }
2497 case GLSL_SAMPLER_DIM_RECT: {
2498 assert(type->is_sampler());
2499 static const char *const names[4] = {
2500 "samplerRect", NULL, "samplerRectShadow", NULL
2501 };
2502 return names[type_idx];
2503 }
2504 case GLSL_SAMPLER_DIM_BUF: {
2505 static const char *const names[8] = {
2506 "samplerBuffer", NULL, NULL, NULL,
2507 "imageBuffer", NULL, NULL, NULL
2508 };
2509 return names[offset + type_idx];
2510 }
2511 case GLSL_SAMPLER_DIM_EXTERNAL: {
2512 assert(type->is_sampler());
2513 static const char *const names[4] = {
2514 "samplerExternalOES", NULL, NULL, NULL
2515 };
2516 return names[type_idx];
2517 }
2518 default:
2519 unreachable("Unsupported sampler/image dimensionality");
2520 } /* sampler/image float dimensionality */
2521 break;
2522 case GLSL_TYPE_INT:
2523 switch (type->sampler_dimensionality) {
2524 case GLSL_SAMPLER_DIM_1D: {
2525 assert(type->is_sampler());
2526 static const char *const names[4] = {
2527 "isampler1D", "isampler1DArray", NULL, NULL
2528 };
2529 return names[type_idx];
2530 }
2531 case GLSL_SAMPLER_DIM_2D: {
2532 static const char *const names[8] = {
2533 "isampler2D", "isampler2DArray", NULL, NULL,
2534 "iimage2D", "iimage2DArray", NULL, NULL
2535 };
2536 return names[offset + type_idx];
2537 }
2538 case GLSL_SAMPLER_DIM_3D: {
2539 static const char *const names[8] = {
2540 "isampler3D", NULL, NULL, NULL,
2541 "iimage3D", NULL, NULL, NULL
2542 };
2543 return names[offset + type_idx];
2544 }
2545 case GLSL_SAMPLER_DIM_CUBE: {
2546 static const char *const names[8] = {
2547 "isamplerCube", "isamplerCubeArray", NULL, NULL,
2548 "iimageCube", NULL, NULL, NULL
2549 };
2550 return names[offset + type_idx];
2551 }
2552 case GLSL_SAMPLER_DIM_MS: {
2553 assert(type->is_sampler());
2554 static const char *const names[4] = {
2555 "isampler2DMS", "isampler2DMSArray", NULL, NULL
2556 };
2557 return names[type_idx];
2558 }
2559 case GLSL_SAMPLER_DIM_RECT: {
2560 assert(type->is_sampler());
2561 static const char *const names[4] = {
2562 "isamplerRect", NULL, "isamplerRectShadow", NULL
2563 };
2564 return names[type_idx];
2565 }
2566 case GLSL_SAMPLER_DIM_BUF: {
2567 static const char *const names[8] = {
2568 "isamplerBuffer", NULL, NULL, NULL,
2569 "iimageBuffer", NULL, NULL, NULL
2570 };
2571 return names[offset + type_idx];
2572 }
2573 default:
2574 unreachable("Unsupported isampler/iimage dimensionality");
2575 } /* sampler/image int dimensionality */
2576 break;
2577 case GLSL_TYPE_UINT:
2578 switch (type->sampler_dimensionality) {
2579 case GLSL_SAMPLER_DIM_1D: {
2580 assert(type->is_sampler());
2581 static const char *const names[4] = {
2582 "usampler1D", "usampler1DArray", NULL, NULL
2583 };
2584 return names[type_idx];
2585 }
2586 case GLSL_SAMPLER_DIM_2D: {
2587 static const char *const names[8] = {
2588 "usampler2D", "usampler2DArray", NULL, NULL,
2589 "uimage2D", "uimage2DArray", NULL, NULL
2590 };
2591 return names[offset + type_idx];
2592 }
2593 case GLSL_SAMPLER_DIM_3D: {
2594 static const char *const names[8] = {
2595 "usampler3D", NULL, NULL, NULL,
2596 "uimage3D", NULL, NULL, NULL
2597 };
2598 return names[offset + type_idx];
2599 }
2600 case GLSL_SAMPLER_DIM_CUBE: {
2601 static const char *const names[8] = {
2602 "usamplerCube", "usamplerCubeArray", NULL, NULL,
2603 "uimageCube", NULL, NULL, NULL
2604 };
2605 return names[offset + type_idx];
2606 }
2607 case GLSL_SAMPLER_DIM_MS: {
2608 assert(type->is_sampler());
2609 static const char *const names[4] = {
2610 "usampler2DMS", "usampler2DMSArray", NULL, NULL
2611 };
2612 return names[type_idx];
2613 }
2614 case GLSL_SAMPLER_DIM_RECT: {
2615 assert(type->is_sampler());
2616 static const char *const names[4] = {
2617 "usamplerRect", NULL, "usamplerRectShadow", NULL
2618 };
2619 return names[type_idx];
2620 }
2621 case GLSL_SAMPLER_DIM_BUF: {
2622 static const char *const names[8] = {
2623 "usamplerBuffer", NULL, NULL, NULL,
2624 "uimageBuffer", NULL, NULL, NULL
2625 };
2626 return names[offset + type_idx];
2627 }
2628 default:
2629 unreachable("Unsupported usampler/uimage dimensionality");
2630 } /* sampler/image uint dimensionality */
2631 break;
2632 default:
2633 unreachable("Unsupported sampler/image type");
2634 } /* sampler/image type */
2635 break;
2636 } /* GLSL_TYPE_SAMPLER/GLSL_TYPE_IMAGE */
2637 break;
2638 default:
2639 unreachable("Unsupported type");
2640 } /* base type */
2641 }
2642
2643 static unsigned
2644 select_gles_precision(unsigned qual_precision,
2645 const glsl_type *type,
2646 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
2647 {
2648 /* Precision qualifiers do not have any meaning in Desktop GLSL.
2649 * In GLES we take the precision from the type qualifier if present,
2650 * otherwise, if the type of the variable allows precision qualifiers at
2651 * all, we look for the default precision qualifier for that type in the
2652 * current scope.
2653 */
2654 assert(state->es_shader);
2655
2656 unsigned precision = GLSL_PRECISION_NONE;
2657 if (qual_precision) {
2658 precision = qual_precision;
2659 } else if (precision_qualifier_allowed(type)) {
2660 const char *type_name =
2661 get_type_name_for_precision_qualifier(type->without_array());
2662 assert(type_name != NULL);
2663
2664 precision =
2665 state->symbols->get_default_precision_qualifier(type_name);
2666 if (precision == ast_precision_none) {
2667 _mesa_glsl_error(loc, state,
2668 "No precision specified in this scope for type `%s'",
2669 type->name);
2670 }
2671 }
2672
2673
2674 /* Section 4.1.7.3 (Atomic Counters) of the GLSL ES 3.10 spec says:
2675 *
2676 * "The default precision of all atomic types is highp. It is an error to
2677 * declare an atomic type with a different precision or to specify the
2678 * default precision for an atomic type to be lowp or mediump."
2679 */
2680 if (type->is_atomic_uint() && precision != ast_precision_high) {
2681 _mesa_glsl_error(loc, state,
2682 "atomic_uint can only have highp precision qualifier");
2683 }
2684
2685 return precision;
2686 }
2687
2688 const glsl_type *
2689 ast_fully_specified_type::glsl_type(const char **name,
2690 struct _mesa_glsl_parse_state *state) const
2691 {
2692 return this->specifier->glsl_type(name, state);
2693 }
2694
2695 /**
2696 * Determine whether a toplevel variable declaration declares a varying. This
2697 * function operates by examining the variable's mode and the shader target,
2698 * so it correctly identifies linkage variables regardless of whether they are
2699 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2700 *
2701 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2702 * this function will produce undefined results.
2703 */
2704 static bool
2705 is_varying_var(ir_variable *var, gl_shader_stage target)
2706 {
2707 switch (target) {
2708 case MESA_SHADER_VERTEX:
2709 return var->data.mode == ir_var_shader_out;
2710 case MESA_SHADER_FRAGMENT:
2711 return var->data.mode == ir_var_shader_in ||
2712 (var->data.mode == ir_var_system_value &&
2713 var->data.location == SYSTEM_VALUE_FRAG_COORD);
2714 default:
2715 return var->data.mode == ir_var_shader_out || var->data.mode == ir_var_shader_in;
2716 }
2717 }
2718
2719 static bool
2720 is_allowed_invariant(ir_variable *var, struct _mesa_glsl_parse_state *state)
2721 {
2722 if (is_varying_var(var, state->stage))
2723 return true;
2724
2725 /* From Section 4.6.1 ("The Invariant Qualifier") GLSL 1.20 spec:
2726 * "Only variables output from a vertex shader can be candidates
2727 * for invariance".
2728 */
2729 if (!state->is_version(130, 100))
2730 return false;
2731
2732 /*
2733 * Later specs remove this language - so allowed invariant
2734 * on fragment shader outputs as well.
2735 */
2736 if (state->stage == MESA_SHADER_FRAGMENT &&
2737 var->data.mode == ir_var_shader_out)
2738 return true;
2739 return false;
2740 }
2741
2742 /**
2743 * Matrix layout qualifiers are only allowed on certain types
2744 */
2745 static void
2746 validate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state,
2747 YYLTYPE *loc,
2748 const glsl_type *type,
2749 ir_variable *var)
2750 {
2751 if (var && !var->is_in_buffer_block()) {
2752 /* Layout qualifiers may only apply to interface blocks and fields in
2753 * them.
2754 */
2755 _mesa_glsl_error(loc, state,
2756 "uniform block layout qualifiers row_major and "
2757 "column_major may not be applied to variables "
2758 "outside of uniform blocks");
2759 } else if (!type->without_array()->is_matrix()) {
2760 /* The OpenGL ES 3.0 conformance tests did not originally allow
2761 * matrix layout qualifiers on non-matrices. However, the OpenGL
2762 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2763 * amended to specifically allow these layouts on all types. Emit
2764 * a warning so that people know their code may not be portable.
2765 */
2766 _mesa_glsl_warning(loc, state,
2767 "uniform block layout qualifiers row_major and "
2768 "column_major applied to non-matrix types may "
2769 "be rejected by older compilers");
2770 }
2771 }
2772
2773 static bool
2774 validate_xfb_buffer_qualifier(YYLTYPE *loc,
2775 struct _mesa_glsl_parse_state *state,
2776 unsigned xfb_buffer) {
2777 if (xfb_buffer >= state->Const.MaxTransformFeedbackBuffers) {
2778 _mesa_glsl_error(loc, state,
2779 "invalid xfb_buffer specified %d is larger than "
2780 "MAX_TRANSFORM_FEEDBACK_BUFFERS - 1 (%d).",
2781 xfb_buffer,
2782 state->Const.MaxTransformFeedbackBuffers - 1);
2783 return false;
2784 }
2785
2786 return true;
2787 }
2788
2789 /* From the ARB_enhanced_layouts spec:
2790 *
2791 * "Variables and block members qualified with *xfb_offset* can be
2792 * scalars, vectors, matrices, structures, and (sized) arrays of these.
2793 * The offset must be a multiple of the size of the first component of
2794 * the first qualified variable or block member, or a compile-time error
2795 * results. Further, if applied to an aggregate containing a double,
2796 * the offset must also be a multiple of 8, and the space taken in the
2797 * buffer will be a multiple of 8.
2798 */
2799 static bool
2800 validate_xfb_offset_qualifier(YYLTYPE *loc,
2801 struct _mesa_glsl_parse_state *state,
2802 int xfb_offset, const glsl_type *type,
2803 unsigned component_size) {
2804 const glsl_type *t_without_array = type->without_array();
2805
2806 if (xfb_offset != -1 && type->is_unsized_array()) {
2807 _mesa_glsl_error(loc, state,
2808 "xfb_offset can't be used with unsized arrays.");
2809 return false;
2810 }
2811
2812 /* Make sure nested structs don't contain unsized arrays, and validate
2813 * any xfb_offsets on interface members.
2814 */
2815 if (t_without_array->is_struct() || t_without_array->is_interface())
2816 for (unsigned int i = 0; i < t_without_array->length; i++) {
2817 const glsl_type *member_t = t_without_array->fields.structure[i].type;
2818
2819 /* When the interface block doesn't have an xfb_offset qualifier then
2820 * we apply the component size rules at the member level.
2821 */
2822 if (xfb_offset == -1)
2823 component_size = member_t->contains_double() ? 8 : 4;
2824
2825 int xfb_offset = t_without_array->fields.structure[i].offset;
2826 validate_xfb_offset_qualifier(loc, state, xfb_offset, member_t,
2827 component_size);
2828 }
2829
2830 /* Nested structs or interface block without offset may not have had an
2831 * offset applied yet so return.
2832 */
2833 if (xfb_offset == -1) {
2834 return true;
2835 }
2836
2837 if (xfb_offset % component_size) {
2838 _mesa_glsl_error(loc, state,
2839 "invalid qualifier xfb_offset=%d must be a multiple "
2840 "of the first component size of the first qualified "
2841 "variable or block member. Or double if an aggregate "
2842 "that contains a double (%d).",
2843 xfb_offset, component_size);
2844 return false;
2845 }
2846
2847 return true;
2848 }
2849
2850 static bool
2851 validate_stream_qualifier(YYLTYPE *loc, struct _mesa_glsl_parse_state *state,
2852 unsigned stream)
2853 {
2854 if (stream >= state->ctx->Const.MaxVertexStreams) {
2855 _mesa_glsl_error(loc, state,
2856 "invalid stream specified %d is larger than "
2857 "MAX_VERTEX_STREAMS - 1 (%d).",
2858 stream, state->ctx->Const.MaxVertexStreams - 1);
2859 return false;
2860 }
2861
2862 return true;
2863 }
2864
2865 static void
2866 apply_explicit_binding(struct _mesa_glsl_parse_state *state,
2867 YYLTYPE *loc,
2868 ir_variable *var,
2869 const glsl_type *type,
2870 const ast_type_qualifier *qual)
2871 {
2872 if (!qual->flags.q.uniform && !qual->flags.q.buffer) {
2873 _mesa_glsl_error(loc, state,
2874 "the \"binding\" qualifier only applies to uniforms and "
2875 "shader storage buffer objects");
2876 return;
2877 }
2878
2879 unsigned qual_binding;
2880 if (!process_qualifier_constant(state, loc, "binding", qual->binding,
2881 &qual_binding)) {
2882 return;
2883 }
2884
2885 const struct gl_context *const ctx = state->ctx;
2886 unsigned elements = type->is_array() ? type->arrays_of_arrays_size() : 1;
2887 unsigned max_index = qual_binding + elements - 1;
2888 const glsl_type *base_type = type->without_array();
2889
2890 if (base_type->is_interface()) {
2891 /* UBOs. From page 60 of the GLSL 4.20 specification:
2892 * "If the binding point for any uniform block instance is less than zero,
2893 * or greater than or equal to the implementation-dependent maximum
2894 * number of uniform buffer bindings, a compilation error will occur.
2895 * When the binding identifier is used with a uniform block instanced as
2896 * an array of size N, all elements of the array from binding through
2897 * binding + N – 1 must be within this range."
2898 *
2899 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2900 */
2901 if (qual->flags.q.uniform &&
2902 max_index >= ctx->Const.MaxUniformBufferBindings) {
2903 _mesa_glsl_error(loc, state, "layout(binding = %u) for %d UBOs exceeds "
2904 "the maximum number of UBO binding points (%d)",
2905 qual_binding, elements,
2906 ctx->Const.MaxUniformBufferBindings);
2907 return;
2908 }
2909
2910 /* SSBOs. From page 67 of the GLSL 4.30 specification:
2911 * "If the binding point for any uniform or shader storage block instance
2912 * is less than zero, or greater than or equal to the
2913 * implementation-dependent maximum number of uniform buffer bindings, a
2914 * compile-time error will occur. When the binding identifier is used
2915 * with a uniform or shader storage block instanced as an array of size
2916 * N, all elements of the array from binding through binding + N – 1 must
2917 * be within this range."
2918 */
2919 if (qual->flags.q.buffer &&
2920 max_index >= ctx->Const.MaxShaderStorageBufferBindings) {
2921 _mesa_glsl_error(loc, state, "layout(binding = %u) for %d SSBOs exceeds "
2922 "the maximum number of SSBO binding points (%d)",
2923 qual_binding, elements,
2924 ctx->Const.MaxShaderStorageBufferBindings);
2925 return;
2926 }
2927 } else if (base_type->is_sampler()) {
2928 /* Samplers. From page 63 of the GLSL 4.20 specification:
2929 * "If the binding is less than zero, or greater than or equal to the
2930 * implementation-dependent maximum supported number of units, a
2931 * compilation error will occur. When the binding identifier is used
2932 * with an array of size N, all elements of the array from binding
2933 * through binding + N - 1 must be within this range."
2934 */
2935 unsigned limit = ctx->Const.MaxCombinedTextureImageUnits;
2936
2937 if (max_index >= limit) {
2938 _mesa_glsl_error(loc, state, "layout(binding = %d) for %d samplers "
2939 "exceeds the maximum number of texture image units "
2940 "(%u)", qual_binding, elements, limit);
2941
2942 return;
2943 }
2944 } else if (base_type->contains_atomic()) {
2945 assert(ctx->Const.MaxAtomicBufferBindings <= MAX_COMBINED_ATOMIC_BUFFERS);
2946 if (qual_binding >= ctx->Const.MaxAtomicBufferBindings) {
2947 _mesa_glsl_error(loc, state, "layout(binding = %d) exceeds the "
2948 "maximum number of atomic counter buffer bindings "
2949 "(%u)", qual_binding,
2950 ctx->Const.MaxAtomicBufferBindings);
2951
2952 return;
2953 }
2954 } else if ((state->is_version(420, 310) ||
2955 state->ARB_shading_language_420pack_enable) &&
2956 base_type->is_image()) {
2957 assert(ctx->Const.MaxImageUnits <= MAX_IMAGE_UNITS);
2958 if (max_index >= ctx->Const.MaxImageUnits) {
2959 _mesa_glsl_error(loc, state, "Image binding %d exceeds the "
2960 "maximum number of image units (%d)", max_index,
2961 ctx->Const.MaxImageUnits);
2962 return;
2963 }
2964
2965 } else {
2966 _mesa_glsl_error(loc, state,
2967 "the \"binding\" qualifier only applies to uniform "
2968 "blocks, storage blocks, opaque variables, or arrays "
2969 "thereof");
2970 return;
2971 }
2972
2973 var->data.explicit_binding = true;
2974 var->data.binding = qual_binding;
2975
2976 return;
2977 }
2978
2979 static void
2980 validate_fragment_flat_interpolation_input(struct _mesa_glsl_parse_state *state,
2981 YYLTYPE *loc,
2982 const glsl_interp_mode interpolation,
2983 const struct glsl_type *var_type,
2984 ir_variable_mode mode)
2985 {
2986 if (state->stage != MESA_SHADER_FRAGMENT ||
2987 interpolation == INTERP_MODE_FLAT ||
2988 mode != ir_var_shader_in)
2989 return;
2990
2991 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
2992 * so must integer vertex outputs.
2993 *
2994 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
2995 * "Fragment shader inputs that are signed or unsigned integers or
2996 * integer vectors must be qualified with the interpolation qualifier
2997 * flat."
2998 *
2999 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3000 * "Fragment shader inputs that are, or contain, signed or unsigned
3001 * integers or integer vectors must be qualified with the
3002 * interpolation qualifier flat."
3003 *
3004 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3005 * "Vertex shader outputs that are, or contain, signed or unsigned
3006 * integers or integer vectors must be qualified with the
3007 * interpolation qualifier flat."
3008 *
3009 * Note that prior to GLSL 1.50, this requirement applied to vertex
3010 * outputs rather than fragment inputs. That creates problems in the
3011 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3012 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3013 * apply the restriction to both vertex outputs and fragment inputs.
3014 *
3015 * Note also that the desktop GLSL specs are missing the text "or
3016 * contain"; this is presumably an oversight, since there is no
3017 * reasonable way to interpolate a fragment shader input that contains
3018 * an integer. See Khronos bug #15671.
3019 */
3020 if ((state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
3021 && var_type->contains_integer()) {
3022 _mesa_glsl_error(loc, state, "if a fragment input is (or contains) "
3023 "an integer, then it must be qualified with 'flat'");
3024 }
3025
3026 /* Double fragment inputs must be qualified with 'flat'.
3027 *
3028 * From the "Overview" of the ARB_gpu_shader_fp64 extension spec:
3029 * "This extension does not support interpolation of double-precision
3030 * values; doubles used as fragment shader inputs must be qualified as
3031 * "flat"."
3032 *
3033 * From section 4.3.4 ("Inputs") of the GLSL 4.00 spec:
3034 * "Fragment shader inputs that are signed or unsigned integers, integer
3035 * vectors, or any double-precision floating-point type must be
3036 * qualified with the interpolation qualifier flat."
3037 *
3038 * Note that the GLSL specs are missing the text "or contain"; this is
3039 * presumably an oversight. See Khronos bug #15671.
3040 *
3041 * The 'double' type does not exist in GLSL ES so far.
3042 */
3043 if (state->has_double()
3044 && var_type->contains_double()) {
3045 _mesa_glsl_error(loc, state, "if a fragment input is (or contains) "
3046 "a double, then it must be qualified with 'flat'");
3047 }
3048
3049 /* Bindless sampler/image fragment inputs must be qualified with 'flat'.
3050 *
3051 * From section 4.3.4 of the ARB_bindless_texture spec:
3052 *
3053 * "(modify last paragraph, p. 35, allowing samplers and images as
3054 * fragment shader inputs) ... Fragment inputs can only be signed and
3055 * unsigned integers and integer vectors, floating point scalars,
3056 * floating-point vectors, matrices, sampler and image types, or arrays
3057 * or structures of these. Fragment shader inputs that are signed or
3058 * unsigned integers, integer vectors, or any double-precision floating-
3059 * point type, or any sampler or image type must be qualified with the
3060 * interpolation qualifier "flat"."
3061 */
3062 if (state->has_bindless()
3063 && (var_type->contains_sampler() || var_type->contains_image())) {
3064 _mesa_glsl_error(loc, state, "if a fragment input is (or contains) "
3065 "a bindless sampler (or image), then it must be "
3066 "qualified with 'flat'");
3067 }
3068 }
3069
3070 static void
3071 validate_interpolation_qualifier(struct _mesa_glsl_parse_state *state,
3072 YYLTYPE *loc,
3073 const glsl_interp_mode interpolation,
3074 const struct ast_type_qualifier *qual,
3075 const struct glsl_type *var_type,
3076 ir_variable_mode mode)
3077 {
3078 /* Interpolation qualifiers can only apply to shader inputs or outputs, but
3079 * not to vertex shader inputs nor fragment shader outputs.
3080 *
3081 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3082 * "Outputs from a vertex shader (out) and inputs to a fragment
3083 * shader (in) can be further qualified with one or more of these
3084 * interpolation qualifiers"
3085 * ...
3086 * "These interpolation qualifiers may only precede the qualifiers in,
3087 * centroid in, out, or centroid out in a declaration. They do not apply
3088 * to the deprecated storage qualifiers varying or centroid
3089 * varying. They also do not apply to inputs into a vertex shader or
3090 * outputs from a fragment shader."
3091 *
3092 * From section 4.3 ("Storage Qualifiers") of the GLSL ES 3.00 spec:
3093 * "Outputs from a shader (out) and inputs to a shader (in) can be
3094 * further qualified with one of these interpolation qualifiers."
3095 * ...
3096 * "These interpolation qualifiers may only precede the qualifiers
3097 * in, centroid in, out, or centroid out in a declaration. They do
3098 * not apply to inputs into a vertex shader or outputs from a
3099 * fragment shader."
3100 */
3101 if ((state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
3102 && interpolation != INTERP_MODE_NONE) {
3103 const char *i = interpolation_string(interpolation);
3104 if (mode != ir_var_shader_in && mode != ir_var_shader_out)
3105 _mesa_glsl_error(loc, state,
3106 "interpolation qualifier `%s' can only be applied to "
3107 "shader inputs or outputs.", i);
3108
3109 switch (state->stage) {
3110 case MESA_SHADER_VERTEX:
3111 if (mode == ir_var_shader_in) {
3112 _mesa_glsl_error(loc, state,
3113 "interpolation qualifier '%s' cannot be applied to "
3114 "vertex shader inputs", i);
3115 }
3116 break;
3117 case MESA_SHADER_FRAGMENT:
3118 if (mode == ir_var_shader_out) {
3119 _mesa_glsl_error(loc, state,
3120 "interpolation qualifier '%s' cannot be applied to "
3121 "fragment shader outputs", i);
3122 }
3123 break;
3124 default:
3125 break;
3126 }
3127 }
3128
3129 /* Interpolation qualifiers cannot be applied to 'centroid' and
3130 * 'centroid varying'.
3131 *
3132 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3133 * "interpolation qualifiers may only precede the qualifiers in,
3134 * centroid in, out, or centroid out in a declaration. They do not apply
3135 * to the deprecated storage qualifiers varying or centroid varying."
3136 *
3137 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3138 *
3139 * GL_EXT_gpu_shader4 allows this.
3140 */
3141 if (state->is_version(130, 0) && !state->EXT_gpu_shader4_enable
3142 && interpolation != INTERP_MODE_NONE
3143 && qual->flags.q.varying) {
3144
3145 const char *i = interpolation_string(interpolation);
3146 const char *s;
3147 if (qual->flags.q.centroid)
3148 s = "centroid varying";
3149 else
3150 s = "varying";
3151
3152 _mesa_glsl_error(loc, state,
3153 "qualifier '%s' cannot be applied to the "
3154 "deprecated storage qualifier '%s'", i, s);
3155 }
3156
3157 validate_fragment_flat_interpolation_input(state, loc, interpolation,
3158 var_type, mode);
3159 }
3160
3161 static glsl_interp_mode
3162 interpret_interpolation_qualifier(const struct ast_type_qualifier *qual,
3163 const struct glsl_type *var_type,
3164 ir_variable_mode mode,
3165 struct _mesa_glsl_parse_state *state,
3166 YYLTYPE *loc)
3167 {
3168 glsl_interp_mode interpolation;
3169 if (qual->flags.q.flat)
3170 interpolation = INTERP_MODE_FLAT;
3171 else if (qual->flags.q.noperspective)
3172 interpolation = INTERP_MODE_NOPERSPECTIVE;
3173 else if (qual->flags.q.smooth)
3174 interpolation = INTERP_MODE_SMOOTH;
3175 else
3176 interpolation = INTERP_MODE_NONE;
3177
3178 validate_interpolation_qualifier(state, loc,
3179 interpolation,
3180 qual, var_type, mode);
3181
3182 return interpolation;
3183 }
3184
3185
3186 static void
3187 apply_explicit_location(const struct ast_type_qualifier *qual,
3188 ir_variable *var,
3189 struct _mesa_glsl_parse_state *state,
3190 YYLTYPE *loc)
3191 {
3192 bool fail = false;
3193
3194 unsigned qual_location;
3195 if (!process_qualifier_constant(state, loc, "location", qual->location,
3196 &qual_location)) {
3197 return;
3198 }
3199
3200 /* Checks for GL_ARB_explicit_uniform_location. */
3201 if (qual->flags.q.uniform) {
3202 if (!state->check_explicit_uniform_location_allowed(loc, var))
3203 return;
3204
3205 const struct gl_context *const ctx = state->ctx;
3206 unsigned max_loc = qual_location + var->type->uniform_locations() - 1;
3207
3208 if (max_loc >= ctx->Const.MaxUserAssignableUniformLocations) {
3209 _mesa_glsl_error(loc, state, "location(s) consumed by uniform %s "
3210 ">= MAX_UNIFORM_LOCATIONS (%u)", var->name,
3211 ctx->Const.MaxUserAssignableUniformLocations);
3212 return;
3213 }
3214
3215 var->data.explicit_location = true;
3216 var->data.location = qual_location;
3217 return;
3218 }
3219
3220 /* Between GL_ARB_explicit_attrib_location an
3221 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
3222 * stage can be assigned explicit locations. The checking here associates
3223 * the correct extension with the correct stage's input / output:
3224 *
3225 * input output
3226 * ----- ------
3227 * vertex explicit_loc sso
3228 * tess control sso sso
3229 * tess eval sso sso
3230 * geometry sso sso
3231 * fragment sso explicit_loc
3232 */
3233 switch (state->stage) {
3234 case MESA_SHADER_VERTEX:
3235 if (var->data.mode == ir_var_shader_in) {
3236 if (!state->check_explicit_attrib_location_allowed(loc, var))
3237 return;
3238
3239 break;
3240 }
3241
3242 if (var->data.mode == ir_var_shader_out) {
3243 if (!state->check_separate_shader_objects_allowed(loc, var))
3244 return;
3245
3246 break;
3247 }
3248
3249 fail = true;
3250 break;
3251
3252 case MESA_SHADER_TESS_CTRL:
3253 case MESA_SHADER_TESS_EVAL:
3254 case MESA_SHADER_GEOMETRY:
3255 if (var->data.mode == ir_var_shader_in || var->data.mode == ir_var_shader_out) {
3256 if (!state->check_separate_shader_objects_allowed(loc, var))
3257 return;
3258
3259 break;
3260 }
3261
3262 fail = true;
3263 break;
3264
3265 case MESA_SHADER_FRAGMENT:
3266 if (var->data.mode == ir_var_shader_in) {
3267 if (!state->check_separate_shader_objects_allowed(loc, var))
3268 return;
3269
3270 break;
3271 }
3272
3273 if (var->data.mode == ir_var_shader_out) {
3274 if (!state->check_explicit_attrib_location_allowed(loc, var))
3275 return;
3276
3277 break;
3278 }
3279
3280 fail = true;
3281 break;
3282
3283 case MESA_SHADER_COMPUTE:
3284 _mesa_glsl_error(loc, state,
3285 "compute shader variables cannot be given "
3286 "explicit locations");
3287 return;
3288 default:
3289 fail = true;
3290 break;
3291 };
3292
3293 if (fail) {
3294 _mesa_glsl_error(loc, state,
3295 "%s cannot be given an explicit location in %s shader",
3296 mode_string(var),
3297 _mesa_shader_stage_to_string(state->stage));
3298 } else {
3299 var->data.explicit_location = true;
3300
3301 switch (state->stage) {
3302 case MESA_SHADER_VERTEX:
3303 var->data.location = (var->data.mode == ir_var_shader_in)
3304 ? (qual_location + VERT_ATTRIB_GENERIC0)
3305 : (qual_location + VARYING_SLOT_VAR0);
3306 break;
3307
3308 case MESA_SHADER_TESS_CTRL:
3309 case MESA_SHADER_TESS_EVAL:
3310 case MESA_SHADER_GEOMETRY:
3311 if (var->data.patch)
3312 var->data.location = qual_location + VARYING_SLOT_PATCH0;
3313 else
3314 var->data.location = qual_location + VARYING_SLOT_VAR0;
3315 break;
3316
3317 case MESA_SHADER_FRAGMENT:
3318 var->data.location = (var->data.mode == ir_var_shader_out)
3319 ? (qual_location + FRAG_RESULT_DATA0)
3320 : (qual_location + VARYING_SLOT_VAR0);
3321 break;
3322 default:
3323 assert(!"Unexpected shader type");
3324 break;
3325 }
3326
3327 /* Check if index was set for the uniform instead of the function */
3328 if (qual->flags.q.explicit_index && qual->is_subroutine_decl()) {
3329 _mesa_glsl_error(loc, state, "an index qualifier can only be "
3330 "used with subroutine functions");
3331 return;
3332 }
3333
3334 unsigned qual_index;
3335 if (qual->flags.q.explicit_index &&
3336 process_qualifier_constant(state, loc, "index", qual->index,
3337 &qual_index)) {
3338 /* From the GLSL 4.30 specification, section 4.4.2 (Output
3339 * Layout Qualifiers):
3340 *
3341 * "It is also a compile-time error if a fragment shader
3342 * sets a layout index to less than 0 or greater than 1."
3343 *
3344 * Older specifications don't mandate a behavior; we take
3345 * this as a clarification and always generate the error.
3346 */
3347 if (qual_index > 1) {
3348 _mesa_glsl_error(loc, state,
3349 "explicit index may only be 0 or 1");
3350 } else {
3351 var->data.explicit_index = true;
3352 var->data.index = qual_index;
3353 }
3354 }
3355 }
3356 }
3357
3358 static bool
3359 validate_storage_for_sampler_image_types(ir_variable *var,
3360 struct _mesa_glsl_parse_state *state,
3361 YYLTYPE *loc)
3362 {
3363 /* From section 4.1.7 of the GLSL 4.40 spec:
3364 *
3365 * "[Opaque types] can only be declared as function
3366 * parameters or uniform-qualified variables."
3367 *
3368 * From section 4.1.7 of the ARB_bindless_texture spec:
3369 *
3370 * "Samplers may be declared as shader inputs and outputs, as uniform
3371 * variables, as temporary variables, and as function parameters."
3372 *
3373 * From section 4.1.X of the ARB_bindless_texture spec:
3374 *
3375 * "Images may be declared as shader inputs and outputs, as uniform
3376 * variables, as temporary variables, and as function parameters."
3377 */
3378 if (state->has_bindless()) {
3379 if (var->data.mode != ir_var_auto &&
3380 var->data.mode != ir_var_uniform &&
3381 var->data.mode != ir_var_shader_in &&
3382 var->data.mode != ir_var_shader_out &&
3383 var->data.mode != ir_var_function_in &&
3384 var->data.mode != ir_var_function_out &&
3385 var->data.mode != ir_var_function_inout) {
3386 _mesa_glsl_error(loc, state, "bindless image/sampler variables may "
3387 "only be declared as shader inputs and outputs, as "
3388 "uniform variables, as temporary variables and as "
3389 "function parameters");
3390 return false;
3391 }
3392 } else {
3393 if (var->data.mode != ir_var_uniform &&
3394 var->data.mode != ir_var_function_in) {
3395 _mesa_glsl_error(loc, state, "image/sampler variables may only be "
3396 "declared as function parameters or "
3397 "uniform-qualified global variables");
3398 return false;
3399 }
3400 }
3401 return true;
3402 }
3403
3404 static bool
3405 validate_memory_qualifier_for_type(struct _mesa_glsl_parse_state *state,
3406 YYLTYPE *loc,
3407 const struct ast_type_qualifier *qual,
3408 const glsl_type *type)
3409 {
3410 /* From Section 4.10 (Memory Qualifiers) of the GLSL 4.50 spec:
3411 *
3412 * "Memory qualifiers are only supported in the declarations of image
3413 * variables, buffer variables, and shader storage blocks; it is an error
3414 * to use such qualifiers in any other declarations.
3415 */
3416 if (!type->is_image() && !qual->flags.q.buffer) {
3417 if (qual->flags.q.read_only ||
3418 qual->flags.q.write_only ||
3419 qual->flags.q.coherent ||
3420 qual->flags.q._volatile ||
3421 qual->flags.q.restrict_flag) {
3422 _mesa_glsl_error(loc, state, "memory qualifiers may only be applied "
3423 "in the declarations of image variables, buffer "
3424 "variables, and shader storage blocks");
3425 return false;
3426 }
3427 }
3428 return true;
3429 }
3430
3431 static bool
3432 validate_image_format_qualifier_for_type(struct _mesa_glsl_parse_state *state,
3433 YYLTYPE *loc,
3434 const struct ast_type_qualifier *qual,
3435 const glsl_type *type)
3436 {
3437 /* From section 4.4.6.2 (Format Layout Qualifiers) of the GLSL 4.50 spec:
3438 *
3439 * "Format layout qualifiers can be used on image variable declarations
3440 * (those declared with a basic type having “image ” in its keyword)."
3441 */
3442 if (!type->is_image() && qual->flags.q.explicit_image_format) {
3443 _mesa_glsl_error(loc, state, "format layout qualifiers may only be "
3444 "applied to images");
3445 return false;
3446 }
3447 return true;
3448 }
3449
3450 static void
3451 apply_image_qualifier_to_variable(const struct ast_type_qualifier *qual,
3452 ir_variable *var,
3453 struct _mesa_glsl_parse_state *state,
3454 YYLTYPE *loc)
3455 {
3456 const glsl_type *base_type = var->type->without_array();
3457
3458 if (!validate_image_format_qualifier_for_type(state, loc, qual, base_type) ||
3459 !validate_memory_qualifier_for_type(state, loc, qual, base_type))
3460 return;
3461
3462 if (!base_type->is_image())
3463 return;
3464
3465 if (!validate_storage_for_sampler_image_types(var, state, loc))
3466 return;
3467
3468 var->data.memory_read_only |= qual->flags.q.read_only;
3469 var->data.memory_write_only |= qual->flags.q.write_only;
3470 var->data.memory_coherent |= qual->flags.q.coherent;
3471 var->data.memory_volatile |= qual->flags.q._volatile;
3472 var->data.memory_restrict |= qual->flags.q.restrict_flag;
3473
3474 if (qual->flags.q.explicit_image_format) {
3475 if (var->data.mode == ir_var_function_in) {
3476 _mesa_glsl_error(loc, state, "format qualifiers cannot be used on "
3477 "image function parameters");
3478 }
3479
3480 if (qual->image_base_type != base_type->sampled_type) {
3481 _mesa_glsl_error(loc, state, "format qualifier doesn't match the base "
3482 "data type of the image");
3483 }
3484
3485 var->data.image_format = qual->image_format;
3486 } else if (state->has_image_load_formatted()) {
3487 if (var->data.mode == ir_var_uniform &&
3488 state->EXT_shader_image_load_formatted_warn) {
3489 _mesa_glsl_warning(loc, state, "GL_EXT_image_load_formatted used");
3490 }
3491 } else {
3492 if (var->data.mode == ir_var_uniform) {
3493 if (state->es_shader ||
3494 !(state->is_version(420, 310) || state->ARB_shader_image_load_store_enable)) {
3495 _mesa_glsl_error(loc, state, "all image uniforms must have a "
3496 "format layout qualifier");
3497 } else if (!qual->flags.q.write_only) {
3498 _mesa_glsl_error(loc, state, "image uniforms not qualified with "
3499 "`writeonly' must have a format layout qualifier");
3500 }
3501 }
3502 var->data.image_format = PIPE_FORMAT_NONE;
3503 }
3504
3505 /* From page 70 of the GLSL ES 3.1 specification:
3506 *
3507 * "Except for image variables qualified with the format qualifiers r32f,
3508 * r32i, and r32ui, image variables must specify either memory qualifier
3509 * readonly or the memory qualifier writeonly."
3510 */
3511 if (state->es_shader &&
3512 var->data.image_format != PIPE_FORMAT_R32_FLOAT &&
3513 var->data.image_format != PIPE_FORMAT_R32_SINT &&
3514 var->data.image_format != PIPE_FORMAT_R32_UINT &&
3515 !var->data.memory_read_only &&
3516 !var->data.memory_write_only) {
3517 _mesa_glsl_error(loc, state, "image variables of format other than r32f, "
3518 "r32i or r32ui must be qualified `readonly' or "
3519 "`writeonly'");
3520 }
3521 }
3522
3523 static inline const char*
3524 get_layout_qualifier_string(bool origin_upper_left, bool pixel_center_integer)
3525 {
3526 if (origin_upper_left && pixel_center_integer)
3527 return "origin_upper_left, pixel_center_integer";
3528 else if (origin_upper_left)
3529 return "origin_upper_left";
3530 else if (pixel_center_integer)
3531 return "pixel_center_integer";
3532 else
3533 return " ";
3534 }
3535
3536 static inline bool
3537 is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state *state,
3538 const struct ast_type_qualifier *qual)
3539 {
3540 /* If gl_FragCoord was previously declared, and the qualifiers were
3541 * different in any way, return true.
3542 */
3543 if (state->fs_redeclares_gl_fragcoord) {
3544 return (state->fs_pixel_center_integer != qual->flags.q.pixel_center_integer
3545 || state->fs_origin_upper_left != qual->flags.q.origin_upper_left);
3546 }
3547
3548 return false;
3549 }
3550
3551 static inline bool
3552 is_conflicting_layer_redeclaration(struct _mesa_glsl_parse_state *state,
3553 const struct ast_type_qualifier *qual)
3554 {
3555 if (state->redeclares_gl_layer) {
3556 return state->layer_viewport_relative != qual->flags.q.viewport_relative;
3557 }
3558 return false;
3559 }
3560
3561 static inline void
3562 validate_array_dimensions(const glsl_type *t,
3563 struct _mesa_glsl_parse_state *state,
3564 YYLTYPE *loc) {
3565 if (t->is_array()) {
3566 t = t->fields.array;
3567 while (t->is_array()) {
3568 if (t->is_unsized_array()) {
3569 _mesa_glsl_error(loc, state,
3570 "only the outermost array dimension can "
3571 "be unsized",
3572 t->name);
3573 break;
3574 }
3575 t = t->fields.array;
3576 }
3577 }
3578 }
3579
3580 static void
3581 apply_bindless_qualifier_to_variable(const struct ast_type_qualifier *qual,
3582 ir_variable *var,
3583 struct _mesa_glsl_parse_state *state,
3584 YYLTYPE *loc)
3585 {
3586 bool has_local_qualifiers = qual->flags.q.bindless_sampler ||
3587 qual->flags.q.bindless_image ||
3588 qual->flags.q.bound_sampler ||
3589 qual->flags.q.bound_image;
3590
3591 /* The ARB_bindless_texture spec says:
3592 *
3593 * "Modify Section 4.4.6 Opaque-Uniform Layout Qualifiers of the GLSL 4.30
3594 * spec"
3595 *
3596 * "If these layout qualifiers are applied to other types of default block
3597 * uniforms, or variables with non-uniform storage, a compile-time error
3598 * will be generated."
3599 */
3600 if (has_local_qualifiers && !qual->flags.q.uniform) {
3601 _mesa_glsl_error(loc, state, "ARB_bindless_texture layout qualifiers "
3602 "can only be applied to default block uniforms or "
3603 "variables with uniform storage");
3604 return;
3605 }
3606
3607 /* The ARB_bindless_texture spec doesn't state anything in this situation,
3608 * but it makes sense to only allow bindless_sampler/bound_sampler for
3609 * sampler types, and respectively bindless_image/bound_image for image
3610 * types.
3611 */
3612 if ((qual->flags.q.bindless_sampler || qual->flags.q.bound_sampler) &&
3613 !var->type->contains_sampler()) {
3614 _mesa_glsl_error(loc, state, "bindless_sampler or bound_sampler can only "
3615 "be applied to sampler types");
3616 return;
3617 }
3618
3619 if ((qual->flags.q.bindless_image || qual->flags.q.bound_image) &&
3620 !var->type->contains_image()) {
3621 _mesa_glsl_error(loc, state, "bindless_image or bound_image can only be "
3622 "applied to image types");
3623 return;
3624 }
3625
3626 /* The bindless_sampler/bindless_image (and respectively
3627 * bound_sampler/bound_image) layout qualifiers can be set at global and at
3628 * local scope.
3629 */
3630 if (var->type->contains_sampler() || var->type->contains_image()) {
3631 var->data.bindless = qual->flags.q.bindless_sampler ||
3632 qual->flags.q.bindless_image ||
3633 state->bindless_sampler_specified ||
3634 state->bindless_image_specified;
3635
3636 var->data.bound = qual->flags.q.bound_sampler ||
3637 qual->flags.q.bound_image ||
3638 state->bound_sampler_specified ||
3639 state->bound_image_specified;
3640 }
3641 }
3642
3643 static void
3644 apply_layout_qualifier_to_variable(const struct ast_type_qualifier *qual,
3645 ir_variable *var,
3646 struct _mesa_glsl_parse_state *state,
3647 YYLTYPE *loc)
3648 {
3649 if (var->name != NULL && strcmp(var->name, "gl_FragCoord") == 0) {
3650
3651 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
3652 *
3653 * "Within any shader, the first redeclarations of gl_FragCoord
3654 * must appear before any use of gl_FragCoord."
3655 *
3656 * Generate a compiler error if above condition is not met by the
3657 * fragment shader.
3658 */
3659 ir_variable *earlier = state->symbols->get_variable("gl_FragCoord");
3660 if (earlier != NULL &&
3661 earlier->data.used &&
3662 !state->fs_redeclares_gl_fragcoord) {
3663 _mesa_glsl_error(loc, state,
3664 "gl_FragCoord used before its first redeclaration "
3665 "in fragment shader");
3666 }
3667
3668 /* Make sure all gl_FragCoord redeclarations specify the same layout
3669 * qualifiers.
3670 */
3671 if (is_conflicting_fragcoord_redeclaration(state, qual)) {
3672 const char *const qual_string =
3673 get_layout_qualifier_string(qual->flags.q.origin_upper_left,
3674 qual->flags.q.pixel_center_integer);
3675
3676 const char *const state_string =
3677 get_layout_qualifier_string(state->fs_origin_upper_left,
3678 state->fs_pixel_center_integer);
3679
3680 _mesa_glsl_error(loc, state,
3681 "gl_FragCoord redeclared with different layout "
3682 "qualifiers (%s) and (%s) ",
3683 state_string,
3684 qual_string);
3685 }
3686 state->fs_origin_upper_left = qual->flags.q.origin_upper_left;
3687 state->fs_pixel_center_integer = qual->flags.q.pixel_center_integer;
3688 state->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers =
3689 !qual->flags.q.origin_upper_left && !qual->flags.q.pixel_center_integer;
3690 state->fs_redeclares_gl_fragcoord =
3691 state->fs_origin_upper_left ||
3692 state->fs_pixel_center_integer ||
3693 state->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers;
3694 }
3695
3696 if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
3697 && (strcmp(var->name, "gl_FragCoord") != 0)) {
3698 const char *const qual_string = (qual->flags.q.origin_upper_left)
3699 ? "origin_upper_left" : "pixel_center_integer";
3700
3701 _mesa_glsl_error(loc, state,
3702 "layout qualifier `%s' can only be applied to "
3703 "fragment shader input `gl_FragCoord'",
3704 qual_string);
3705 }
3706
3707 if (qual->flags.q.explicit_location) {
3708 apply_explicit_location(qual, var, state, loc);
3709
3710 if (qual->flags.q.explicit_component) {
3711 unsigned qual_component;
3712 if (process_qualifier_constant(state, loc, "component",
3713 qual->component, &qual_component)) {
3714 const glsl_type *type = var->type->without_array();
3715 unsigned components = type->component_slots();
3716
3717 if (type->is_matrix() || type->is_struct()) {
3718 _mesa_glsl_error(loc, state, "component layout qualifier "
3719 "cannot be applied to a matrix, a structure, "
3720 "a block, or an array containing any of "
3721 "these.");
3722 } else if (components > 4 && type->is_64bit()) {
3723 _mesa_glsl_error(loc, state, "component layout qualifier "
3724 "cannot be applied to dvec%u.",
3725 components / 2);
3726 } else if (qual_component != 0 &&
3727 (qual_component + components - 1) > 3) {
3728 _mesa_glsl_error(loc, state, "component overflow (%u > 3)",
3729 (qual_component + components - 1));
3730 } else if (qual_component == 1 && type->is_64bit()) {
3731 /* We don't bother checking for 3 as it should be caught by the
3732 * overflow check above.
3733 */
3734 _mesa_glsl_error(loc, state, "doubles cannot begin at "
3735 "component 1 or 3");
3736 } else {
3737 var->data.explicit_component = true;
3738 var->data.location_frac = qual_component;
3739 }
3740 }
3741 }
3742 } else if (qual->flags.q.explicit_index) {
3743 if (!qual->subroutine_list)
3744 _mesa_glsl_error(loc, state,
3745 "explicit index requires explicit location");
3746 } else if (qual->flags.q.explicit_component) {
3747 _mesa_glsl_error(loc, state,
3748 "explicit component requires explicit location");
3749 }
3750
3751 if (qual->flags.q.explicit_binding) {
3752 apply_explicit_binding(state, loc, var, var->type, qual);
3753 }
3754
3755 if (state->stage == MESA_SHADER_GEOMETRY &&
3756 qual->flags.q.out && qual->flags.q.stream) {
3757 unsigned qual_stream;
3758 if (process_qualifier_constant(state, loc, "stream", qual->stream,
3759 &qual_stream) &&
3760 validate_stream_qualifier(loc, state, qual_stream)) {
3761 var->data.stream = qual_stream;
3762 }
3763 }
3764
3765 if (qual->flags.q.out && qual->flags.q.xfb_buffer) {
3766 unsigned qual_xfb_buffer;
3767 if (process_qualifier_constant(state, loc, "xfb_buffer",
3768 qual->xfb_buffer, &qual_xfb_buffer) &&
3769 validate_xfb_buffer_qualifier(loc, state, qual_xfb_buffer)) {
3770 var->data.xfb_buffer = qual_xfb_buffer;
3771 if (qual->flags.q.explicit_xfb_buffer)
3772 var->data.explicit_xfb_buffer = true;
3773 }
3774 }
3775
3776 if (qual->flags.q.explicit_xfb_offset) {
3777 unsigned qual_xfb_offset;
3778 unsigned component_size = var->type->contains_double() ? 8 : 4;
3779
3780 if (process_qualifier_constant(state, loc, "xfb_offset",
3781 qual->offset, &qual_xfb_offset) &&
3782 validate_xfb_offset_qualifier(loc, state, (int) qual_xfb_offset,
3783 var->type, component_size)) {
3784 var->data.offset = qual_xfb_offset;
3785 var->data.explicit_xfb_offset = true;
3786 }
3787 }
3788
3789 if (qual->flags.q.explicit_xfb_stride) {
3790 unsigned qual_xfb_stride;
3791 if (process_qualifier_constant(state, loc, "xfb_stride",
3792 qual->xfb_stride, &qual_xfb_stride)) {
3793 var->data.xfb_stride = qual_xfb_stride;
3794 var->data.explicit_xfb_stride = true;
3795 }
3796 }
3797
3798 if (var->type->contains_atomic()) {
3799 if (var->data.mode == ir_var_uniform) {
3800 if (var->data.explicit_binding) {
3801 unsigned *offset =
3802 &state->atomic_counter_offsets[var->data.binding];
3803
3804 if (*offset % ATOMIC_COUNTER_SIZE)
3805 _mesa_glsl_error(loc, state,
3806 "misaligned atomic counter offset");
3807
3808 var->data.offset = *offset;
3809 *offset += var->type->atomic_size();
3810
3811 } else {
3812 _mesa_glsl_error(loc, state,
3813 "atomic counters require explicit binding point");
3814 }
3815 } else if (var->data.mode != ir_var_function_in) {
3816 _mesa_glsl_error(loc, state, "atomic counters may only be declared as "
3817 "function parameters or uniform-qualified "
3818 "global variables");
3819 }
3820 }
3821
3822 if (var->type->contains_sampler() &&
3823 !validate_storage_for_sampler_image_types(var, state, loc))
3824 return;
3825
3826 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
3827 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
3828 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
3829 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
3830 * These extensions and all following extensions that add the 'layout'
3831 * keyword have been modified to require the use of 'in' or 'out'.
3832 *
3833 * The following extension do not allow the deprecated keywords:
3834 *
3835 * GL_AMD_conservative_depth
3836 * GL_ARB_conservative_depth
3837 * GL_ARB_gpu_shader5
3838 * GL_ARB_separate_shader_objects
3839 * GL_ARB_tessellation_shader
3840 * GL_ARB_transform_feedback3
3841 * GL_ARB_uniform_buffer_object
3842 *
3843 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
3844 * allow layout with the deprecated keywords.
3845 */
3846 const bool relaxed_layout_qualifier_checking =
3847 state->ARB_fragment_coord_conventions_enable;
3848
3849 const bool uses_deprecated_qualifier = qual->flags.q.attribute
3850 || qual->flags.q.varying;
3851 if (qual->has_layout() && uses_deprecated_qualifier) {
3852 if (relaxed_layout_qualifier_checking) {
3853 _mesa_glsl_warning(loc, state,
3854 "`layout' qualifier may not be used with "
3855 "`attribute' or `varying'");
3856 } else {
3857 _mesa_glsl_error(loc, state,
3858 "`layout' qualifier may not be used with "
3859 "`attribute' or `varying'");
3860 }
3861 }
3862
3863 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
3864 * AMD_conservative_depth.
3865 */
3866 if (qual->flags.q.depth_type
3867 && !state->is_version(420, 0)
3868 && !state->AMD_conservative_depth_enable
3869 && !state->ARB_conservative_depth_enable) {
3870 _mesa_glsl_error(loc, state,
3871 "extension GL_AMD_conservative_depth or "
3872 "GL_ARB_conservative_depth must be enabled "
3873 "to use depth layout qualifiers");
3874 } else if (qual->flags.q.depth_type
3875 && strcmp(var->name, "gl_FragDepth") != 0) {
3876 _mesa_glsl_error(loc, state,
3877 "depth layout qualifiers can be applied only to "
3878 "gl_FragDepth");
3879 }
3880
3881 switch (qual->depth_type) {
3882 case ast_depth_any:
3883 var->data.depth_layout = ir_depth_layout_any;
3884 break;
3885 case ast_depth_greater:
3886 var->data.depth_layout = ir_depth_layout_greater;
3887 break;
3888 case ast_depth_less:
3889 var->data.depth_layout = ir_depth_layout_less;
3890 break;
3891 case ast_depth_unchanged:
3892 var->data.depth_layout = ir_depth_layout_unchanged;
3893 break;
3894 default:
3895 var->data.depth_layout = ir_depth_layout_none;
3896 break;
3897 }
3898
3899 if (qual->flags.q.std140 ||
3900 qual->flags.q.std430 ||
3901 qual->flags.q.packed ||
3902 qual->flags.q.shared) {
3903 _mesa_glsl_error(loc, state,
3904 "uniform and shader storage block layout qualifiers "
3905 "std140, std430, packed, and shared can only be "
3906 "applied to uniform or shader storage blocks, not "
3907 "members");
3908 }
3909
3910 if (qual->flags.q.row_major || qual->flags.q.column_major) {
3911 validate_matrix_layout_for_type(state, loc, var->type, var);
3912 }
3913
3914 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
3915 * Inputs):
3916 *
3917 * "Fragment shaders also allow the following layout qualifier on in only
3918 * (not with variable declarations)
3919 * layout-qualifier-id
3920 * early_fragment_tests
3921 * [...]"
3922 */
3923 if (qual->flags.q.early_fragment_tests) {
3924 _mesa_glsl_error(loc, state, "early_fragment_tests layout qualifier only "
3925 "valid in fragment shader input layout declaration.");
3926 }
3927
3928 if (qual->flags.q.inner_coverage) {
3929 _mesa_glsl_error(loc, state, "inner_coverage layout qualifier only "
3930 "valid in fragment shader input layout declaration.");
3931 }
3932
3933 if (qual->flags.q.post_depth_coverage) {
3934 _mesa_glsl_error(loc, state, "post_depth_coverage layout qualifier only "
3935 "valid in fragment shader input layout declaration.");
3936 }
3937
3938 if (state->has_bindless())
3939 apply_bindless_qualifier_to_variable(qual, var, state, loc);
3940
3941 if (qual->flags.q.pixel_interlock_ordered ||
3942 qual->flags.q.pixel_interlock_unordered ||
3943 qual->flags.q.sample_interlock_ordered ||
3944 qual->flags.q.sample_interlock_unordered) {
3945 _mesa_glsl_error(loc, state, "interlock layout qualifiers: "
3946 "pixel_interlock_ordered, pixel_interlock_unordered, "
3947 "sample_interlock_ordered and sample_interlock_unordered, "
3948 "only valid in fragment shader input layout declaration.");
3949 }
3950
3951 if (var->name != NULL && strcmp(var->name, "gl_Layer") == 0) {
3952 if (is_conflicting_layer_redeclaration(state, qual)) {
3953 _mesa_glsl_error(loc, state, "gl_Layer redeclaration with "
3954 "different viewport_relative setting than earlier");
3955 }
3956 state->redeclares_gl_layer = 1;
3957 if (qual->flags.q.viewport_relative) {
3958 state->layer_viewport_relative = 1;
3959 }
3960 } else if (qual->flags.q.viewport_relative) {
3961 _mesa_glsl_error(loc, state,
3962 "viewport_relative qualifier "
3963 "can only be applied to gl_Layer.");
3964 }
3965 }
3966
3967 static void
3968 apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
3969 ir_variable *var,
3970 struct _mesa_glsl_parse_state *state,
3971 YYLTYPE *loc,
3972 bool is_parameter)
3973 {
3974 STATIC_ASSERT(sizeof(qual->flags.q) <= sizeof(qual->flags.i));
3975
3976 if (qual->flags.q.invariant) {
3977 if (var->data.used) {
3978 _mesa_glsl_error(loc, state,
3979 "variable `%s' may not be redeclared "
3980 "`invariant' after being used",
3981 var->name);
3982 } else {
3983 var->data.explicit_invariant = true;
3984 var->data.invariant = true;
3985 }
3986 }
3987
3988 if (qual->flags.q.precise) {
3989 if (var->data.used) {
3990 _mesa_glsl_error(loc, state,
3991 "variable `%s' may not be redeclared "
3992 "`precise' after being used",
3993 var->name);
3994 } else {
3995 var->data.precise = 1;
3996 }
3997 }
3998
3999 if (qual->is_subroutine_decl() && !qual->flags.q.uniform) {
4000 _mesa_glsl_error(loc, state,
4001 "`subroutine' may only be applied to uniforms, "
4002 "subroutine type declarations, or function definitions");
4003 }
4004
4005 if (qual->flags.q.constant || qual->flags.q.attribute
4006 || qual->flags.q.uniform
4007 || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT)))
4008 var->data.read_only = 1;
4009
4010 if (qual->flags.q.centroid)
4011 var->data.centroid = 1;
4012
4013 if (qual->flags.q.sample)
4014 var->data.sample = 1;
4015
4016 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
4017 if (state->es_shader) {
4018 var->data.precision =
4019 select_gles_precision(qual->precision, var->type, state, loc);
4020 }
4021
4022 if (qual->flags.q.patch)
4023 var->data.patch = 1;
4024
4025 if (qual->flags.q.attribute && state->stage != MESA_SHADER_VERTEX) {
4026 var->type = glsl_type::error_type;
4027 _mesa_glsl_error(loc, state,
4028 "`attribute' variables may not be declared in the "
4029 "%s shader",
4030 _mesa_shader_stage_to_string(state->stage));
4031 }
4032
4033 /* Disallow layout qualifiers which may only appear on layout declarations. */
4034 if (qual->flags.q.prim_type) {
4035 _mesa_glsl_error(loc, state,
4036 "Primitive type may only be specified on GS input or output "
4037 "layout declaration, not on variables.");
4038 }
4039
4040 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
4041 *
4042 * "However, the const qualifier cannot be used with out or inout."
4043 *
4044 * The same section of the GLSL 4.40 spec further clarifies this saying:
4045 *
4046 * "The const qualifier cannot be used with out or inout, or a
4047 * compile-time error results."
4048 */
4049 if (is_parameter && qual->flags.q.constant && qual->flags.q.out) {
4050 _mesa_glsl_error(loc, state,
4051 "`const' may not be applied to `out' or `inout' "
4052 "function parameters");
4053 }
4054
4055 /* If there is no qualifier that changes the mode of the variable, leave
4056 * the setting alone.
4057 */
4058 assert(var->data.mode != ir_var_temporary);
4059 if (qual->flags.q.in && qual->flags.q.out)
4060 var->data.mode = is_parameter ? ir_var_function_inout : ir_var_shader_out;
4061 else if (qual->flags.q.in)
4062 var->data.mode = is_parameter ? ir_var_function_in : ir_var_shader_in;
4063 else if (qual->flags.q.attribute
4064 || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT)))
4065 var->data.mode = ir_var_shader_in;
4066 else if (qual->flags.q.out)
4067 var->data.mode = is_parameter ? ir_var_function_out : ir_var_shader_out;
4068 else if (qual->flags.q.varying && (state->stage == MESA_SHADER_VERTEX))
4069 var->data.mode = ir_var_shader_out;
4070 else if (qual->flags.q.uniform)
4071 var->data.mode = ir_var_uniform;
4072 else if (qual->flags.q.buffer)
4073 var->data.mode = ir_var_shader_storage;
4074 else if (qual->flags.q.shared_storage)
4075 var->data.mode = ir_var_shader_shared;
4076
4077 if (!is_parameter && state->has_framebuffer_fetch() &&
4078 state->stage == MESA_SHADER_FRAGMENT) {
4079 if (state->is_version(130, 300))
4080 var->data.fb_fetch_output = qual->flags.q.in && qual->flags.q.out;
4081 else
4082 var->data.fb_fetch_output = (strcmp(var->name, "gl_LastFragData") == 0);
4083 }
4084
4085 if (var->data.fb_fetch_output) {
4086 var->data.assigned = true;
4087 var->data.memory_coherent = !qual->flags.q.non_coherent;
4088
4089 /* From the EXT_shader_framebuffer_fetch spec:
4090 *
4091 * "It is an error to declare an inout fragment output not qualified
4092 * with layout(noncoherent) if the GL_EXT_shader_framebuffer_fetch
4093 * extension hasn't been enabled."
4094 */
4095 if (var->data.memory_coherent &&
4096 !state->EXT_shader_framebuffer_fetch_enable)
4097 _mesa_glsl_error(loc, state,
4098 "invalid declaration of framebuffer fetch output not "
4099 "qualified with layout(noncoherent)");
4100
4101 } else {
4102 /* From the EXT_shader_framebuffer_fetch spec:
4103 *
4104 * "Fragment outputs declared inout may specify the following layout
4105 * qualifier: [...] noncoherent"
4106 */
4107 if (qual->flags.q.non_coherent)
4108 _mesa_glsl_error(loc, state,
4109 "invalid layout(noncoherent) qualifier not part of "
4110 "framebuffer fetch output declaration");
4111 }
4112
4113 if (!is_parameter && is_varying_var(var, state->stage)) {
4114 /* User-defined ins/outs are not permitted in compute shaders. */
4115 if (state->stage == MESA_SHADER_COMPUTE) {
4116 _mesa_glsl_error(loc, state,
4117 "user-defined input and output variables are not "
4118 "permitted in compute shaders");
4119 }
4120
4121 /* This variable is being used to link data between shader stages (in
4122 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
4123 * that is allowed for such purposes.
4124 *
4125 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
4126 *
4127 * "The varying qualifier can be used only with the data types
4128 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
4129 * these."
4130 *
4131 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
4132 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
4133 *
4134 * "Fragment inputs can only be signed and unsigned integers and
4135 * integer vectors, float, floating-point vectors, matrices, or
4136 * arrays of these. Structures cannot be input.
4137 *
4138 * Similar text exists in the section on vertex shader outputs.
4139 *
4140 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
4141 * 3.00 spec allows structs as well. Varying structs are also allowed
4142 * in GLSL 1.50.
4143 *
4144 * From section 4.3.4 of the ARB_bindless_texture spec:
4145 *
4146 * "(modify third paragraph of the section to allow sampler and image
4147 * types) ... Vertex shader inputs can only be float,
4148 * single-precision floating-point scalars, single-precision
4149 * floating-point vectors, matrices, signed and unsigned integers
4150 * and integer vectors, sampler and image types."
4151 *
4152 * From section 4.3.6 of the ARB_bindless_texture spec:
4153 *
4154 * "Output variables can only be floating-point scalars,
4155 * floating-point vectors, matrices, signed or unsigned integers or
4156 * integer vectors, sampler or image types, or arrays or structures
4157 * of any these."
4158 */
4159 switch (var->type->without_array()->base_type) {
4160 case GLSL_TYPE_FLOAT:
4161 /* Ok in all GLSL versions */
4162 break;
4163 case GLSL_TYPE_UINT:
4164 case GLSL_TYPE_INT:
4165 if (state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
4166 break;
4167 _mesa_glsl_error(loc, state,
4168 "varying variables must be of base type float in %s",
4169 state->get_version_string());
4170 break;
4171 case GLSL_TYPE_STRUCT:
4172 if (state->is_version(150, 300))
4173 break;
4174 _mesa_glsl_error(loc, state,
4175 "varying variables may not be of type struct");
4176 break;
4177 case GLSL_TYPE_DOUBLE:
4178 case GLSL_TYPE_UINT64:
4179 case GLSL_TYPE_INT64:
4180 break;
4181 case GLSL_TYPE_SAMPLER:
4182 case GLSL_TYPE_IMAGE:
4183 if (state->has_bindless())
4184 break;
4185 /* fallthrough */
4186 default:
4187 _mesa_glsl_error(loc, state, "illegal type for a varying variable");
4188 break;
4189 }
4190 }
4191
4192 if (state->all_invariant && var->data.mode == ir_var_shader_out) {
4193 var->data.explicit_invariant = true;
4194 var->data.invariant = true;
4195 }
4196
4197 var->data.interpolation =
4198 interpret_interpolation_qualifier(qual, var->type,
4199 (ir_variable_mode) var->data.mode,
4200 state, loc);
4201
4202 /* Does the declaration use the deprecated 'attribute' or 'varying'
4203 * keywords?
4204 */
4205 const bool uses_deprecated_qualifier = qual->flags.q.attribute
4206 || qual->flags.q.varying;
4207
4208
4209 /* Validate auxiliary storage qualifiers */
4210
4211 /* From section 4.3.4 of the GLSL 1.30 spec:
4212 * "It is an error to use centroid in in a vertex shader."
4213 *
4214 * From section 4.3.4 of the GLSL ES 3.00 spec:
4215 * "It is an error to use centroid in or interpolation qualifiers in
4216 * a vertex shader input."
4217 */
4218
4219 /* Section 4.3.6 of the GLSL 1.30 specification states:
4220 * "It is an error to use centroid out in a fragment shader."
4221 *
4222 * The GL_ARB_shading_language_420pack extension specification states:
4223 * "It is an error to use auxiliary storage qualifiers or interpolation
4224 * qualifiers on an output in a fragment shader."
4225 */
4226 if (qual->flags.q.sample && (!is_varying_var(var, state->stage) || uses_deprecated_qualifier)) {
4227 _mesa_glsl_error(loc, state,
4228 "sample qualifier may only be used on `in` or `out` "
4229 "variables between shader stages");
4230 }
4231 if (qual->flags.q.centroid && !is_varying_var(var, state->stage)) {
4232 _mesa_glsl_error(loc, state,
4233 "centroid qualifier may only be used with `in', "
4234 "`out' or `varying' variables between shader stages");
4235 }
4236
4237 if (qual->flags.q.shared_storage && state->stage != MESA_SHADER_COMPUTE) {
4238 _mesa_glsl_error(loc, state,
4239 "the shared storage qualifiers can only be used with "
4240 "compute shaders");
4241 }
4242
4243 apply_image_qualifier_to_variable(qual, var, state, loc);
4244 }
4245
4246 /**
4247 * Get the variable that is being redeclared by this declaration or if it
4248 * does not exist, the current declared variable.
4249 *
4250 * Semantic checks to verify the validity of the redeclaration are also
4251 * performed. If semantic checks fail, compilation error will be emitted via
4252 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
4253 *
4254 * \returns
4255 * A pointer to an existing variable in the current scope if the declaration
4256 * is a redeclaration, current variable otherwise. \c is_declared boolean
4257 * will return \c true if the declaration is a redeclaration, \c false
4258 * otherwise.
4259 */
4260 static ir_variable *
4261 get_variable_being_redeclared(ir_variable **var_ptr, YYLTYPE loc,
4262 struct _mesa_glsl_parse_state *state,
4263 bool allow_all_redeclarations,
4264 bool *is_redeclaration)
4265 {
4266 ir_variable *var = *var_ptr;
4267
4268 /* Check if this declaration is actually a re-declaration, either to
4269 * resize an array or add qualifiers to an existing variable.
4270 *
4271 * This is allowed for variables in the current scope, or when at
4272 * global scope (for built-ins in the implicit outer scope).
4273 */
4274 ir_variable *earlier = state->symbols->get_variable(var->name);
4275 if (earlier == NULL ||
4276 (state->current_function != NULL &&
4277 !state->symbols->name_declared_this_scope(var->name))) {
4278 *is_redeclaration = false;
4279 return var;
4280 }
4281
4282 *is_redeclaration = true;
4283
4284 if (earlier->data.how_declared == ir_var_declared_implicitly) {
4285 /* Verify that the redeclaration of a built-in does not change the
4286 * storage qualifier. There are a couple special cases.
4287 *
4288 * 1. Some built-in variables that are defined as 'in' in the
4289 * specification are implemented as system values. Allow
4290 * ir_var_system_value -> ir_var_shader_in.
4291 *
4292 * 2. gl_LastFragData is implemented as a ir_var_shader_out, but the
4293 * specification requires that redeclarations omit any qualifier.
4294 * Allow ir_var_shader_out -> ir_var_auto for this one variable.
4295 */
4296 if (earlier->data.mode != var->data.mode &&
4297 !(earlier->data.mode == ir_var_system_value &&
4298 var->data.mode == ir_var_shader_in) &&
4299 !(strcmp(var->name, "gl_LastFragData") == 0 &&
4300 var->data.mode == ir_var_auto)) {
4301 _mesa_glsl_error(&loc, state,
4302 "redeclaration cannot change qualification of `%s'",
4303 var->name);
4304 }
4305 }
4306
4307 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
4308 *
4309 * "It is legal to declare an array without a size and then
4310 * later re-declare the same name as an array of the same
4311 * type and specify a size."
4312 */
4313 if (earlier->type->is_unsized_array() && var->type->is_array()
4314 && (var->type->fields.array == earlier->type->fields.array)) {
4315 const int size = var->type->array_size();
4316 check_builtin_array_max_size(var->name, size, loc, state);
4317 if ((size > 0) && (size <= earlier->data.max_array_access)) {
4318 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
4319 "previous access",
4320 earlier->data.max_array_access);
4321 }
4322
4323 earlier->type = var->type;
4324 delete var;
4325 var = NULL;
4326 *var_ptr = NULL;
4327 } else if (earlier->type != var->type) {
4328 _mesa_glsl_error(&loc, state,
4329 "redeclaration of `%s' has incorrect type",
4330 var->name);
4331 } else if ((state->ARB_fragment_coord_conventions_enable ||
4332 state->is_version(150, 0))
4333 && strcmp(var->name, "gl_FragCoord") == 0) {
4334 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
4335 * qualifiers.
4336 *
4337 * We don't really need to do anything here, just allow the
4338 * redeclaration. Any error on the gl_FragCoord is handled on the ast
4339 * level at apply_layout_qualifier_to_variable using the
4340 * ast_type_qualifier and _mesa_glsl_parse_state, or later at
4341 * linker.cpp.
4342 */
4343 /* According to section 4.3.7 of the GLSL 1.30 spec,
4344 * the following built-in varaibles can be redeclared with an
4345 * interpolation qualifier:
4346 * * gl_FrontColor
4347 * * gl_BackColor
4348 * * gl_FrontSecondaryColor
4349 * * gl_BackSecondaryColor
4350 * * gl_Color
4351 * * gl_SecondaryColor
4352 */
4353 } else if (state->is_version(130, 0)
4354 && (strcmp(var->name, "gl_FrontColor") == 0
4355 || strcmp(var->name, "gl_BackColor") == 0
4356 || strcmp(var->name, "gl_FrontSecondaryColor") == 0
4357 || strcmp(var->name, "gl_BackSecondaryColor") == 0
4358 || strcmp(var->name, "gl_Color") == 0
4359 || strcmp(var->name, "gl_SecondaryColor") == 0)) {
4360 earlier->data.interpolation = var->data.interpolation;
4361
4362 /* Layout qualifiers for gl_FragDepth. */
4363 } else if ((state->is_version(420, 0) ||
4364 state->AMD_conservative_depth_enable ||
4365 state->ARB_conservative_depth_enable)
4366 && strcmp(var->name, "gl_FragDepth") == 0) {
4367
4368 /** From the AMD_conservative_depth spec:
4369 * Within any shader, the first redeclarations of gl_FragDepth
4370 * must appear before any use of gl_FragDepth.
4371 */
4372 if (earlier->data.used) {
4373 _mesa_glsl_error(&loc, state,
4374 "the first redeclaration of gl_FragDepth "
4375 "must appear before any use of gl_FragDepth");
4376 }
4377
4378 /* Prevent inconsistent redeclaration of depth layout qualifier. */
4379 if (earlier->data.depth_layout != ir_depth_layout_none
4380 && earlier->data.depth_layout != var->data.depth_layout) {
4381 _mesa_glsl_error(&loc, state,
4382 "gl_FragDepth: depth layout is declared here "
4383 "as '%s, but it was previously declared as "
4384 "'%s'",
4385 depth_layout_string(var->data.depth_layout),
4386 depth_layout_string(earlier->data.depth_layout));
4387 }
4388
4389 earlier->data.depth_layout = var->data.depth_layout;
4390
4391 } else if (state->has_framebuffer_fetch() &&
4392 strcmp(var->name, "gl_LastFragData") == 0 &&
4393 var->data.mode == ir_var_auto) {
4394 /* According to the EXT_shader_framebuffer_fetch spec:
4395 *
4396 * "By default, gl_LastFragData is declared with the mediump precision
4397 * qualifier. This can be changed by redeclaring the corresponding
4398 * variables with the desired precision qualifier."
4399 *
4400 * "Fragment shaders may specify the following layout qualifier only for
4401 * redeclaring the built-in gl_LastFragData array [...]: noncoherent"
4402 */
4403 earlier->data.precision = var->data.precision;
4404 earlier->data.memory_coherent = var->data.memory_coherent;
4405
4406 } else if (state->NV_viewport_array2_enable &&
4407 strcmp(var->name, "gl_Layer") == 0 &&
4408 earlier->data.how_declared == ir_var_declared_implicitly) {
4409 /* No need to do anything, just allow it. Qualifier is stored in state */
4410
4411 } else if ((earlier->data.how_declared == ir_var_declared_implicitly &&
4412 state->allow_builtin_variable_redeclaration) ||
4413 allow_all_redeclarations) {
4414 /* Allow verbatim redeclarations of built-in variables. Not explicitly
4415 * valid, but some applications do it.
4416 */
4417 } else {
4418 _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
4419 }
4420
4421 return earlier;
4422 }
4423
4424 /**
4425 * Generate the IR for an initializer in a variable declaration
4426 */
4427 static ir_rvalue *
4428 process_initializer(ir_variable *var, ast_declaration *decl,
4429 ast_fully_specified_type *type,
4430 exec_list *initializer_instructions,
4431 struct _mesa_glsl_parse_state *state)
4432 {
4433 void *mem_ctx = state;
4434 ir_rvalue *result = NULL;
4435
4436 YYLTYPE initializer_loc = decl->initializer->get_location();
4437
4438 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
4439 *
4440 * "All uniform variables are read-only and are initialized either
4441 * directly by an application via API commands, or indirectly by
4442 * OpenGL."
4443 */
4444 if (var->data.mode == ir_var_uniform) {
4445 state->check_version(120, 0, &initializer_loc,
4446 "cannot initialize uniform %s",
4447 var->name);
4448 }
4449
4450 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4451 *
4452 * "Buffer variables cannot have initializers."
4453 */
4454 if (var->data.mode == ir_var_shader_storage) {
4455 _mesa_glsl_error(&initializer_loc, state,
4456 "cannot initialize buffer variable %s",
4457 var->name);
4458 }
4459
4460 /* From section 4.1.7 of the GLSL 4.40 spec:
4461 *
4462 * "Opaque variables [...] are initialized only through the
4463 * OpenGL API; they cannot be declared with an initializer in a
4464 * shader."
4465 *
4466 * From section 4.1.7 of the ARB_bindless_texture spec:
4467 *
4468 * "Samplers may be declared as shader inputs and outputs, as uniform
4469 * variables, as temporary variables, and as function parameters."
4470 *
4471 * From section 4.1.X of the ARB_bindless_texture spec:
4472 *
4473 * "Images may be declared as shader inputs and outputs, as uniform
4474 * variables, as temporary variables, and as function parameters."
4475 */
4476 if (var->type->contains_atomic() ||
4477 (!state->has_bindless() && var->type->contains_opaque())) {
4478 _mesa_glsl_error(&initializer_loc, state,
4479 "cannot initialize %s variable %s",
4480 var->name, state->has_bindless() ? "atomic" : "opaque");
4481 }
4482
4483 if ((var->data.mode == ir_var_shader_in) && (state->current_function == NULL)) {
4484 _mesa_glsl_error(&initializer_loc, state,
4485 "cannot initialize %s shader input / %s %s",
4486 _mesa_shader_stage_to_string(state->stage),
4487 (state->stage == MESA_SHADER_VERTEX)
4488 ? "attribute" : "varying",
4489 var->name);
4490 }
4491
4492 if (var->data.mode == ir_var_shader_out && state->current_function == NULL) {
4493 _mesa_glsl_error(&initializer_loc, state,
4494 "cannot initialize %s shader output %s",
4495 _mesa_shader_stage_to_string(state->stage),
4496 var->name);
4497 }
4498
4499 /* If the initializer is an ast_aggregate_initializer, recursively store
4500 * type information from the LHS into it, so that its hir() function can do
4501 * type checking.
4502 */
4503 if (decl->initializer->oper == ast_aggregate)
4504 _mesa_ast_set_aggregate_type(var->type, decl->initializer);
4505
4506 ir_dereference *const lhs = new(state) ir_dereference_variable(var);
4507 ir_rvalue *rhs = decl->initializer->hir(initializer_instructions, state);
4508
4509 /* Calculate the constant value if this is a const or uniform
4510 * declaration.
4511 *
4512 * Section 4.3 (Storage Qualifiers) of the GLSL ES 1.00.17 spec says:
4513 *
4514 * "Declarations of globals without a storage qualifier, or with
4515 * just the const qualifier, may include initializers, in which case
4516 * they will be initialized before the first line of main() is
4517 * executed. Such initializers must be a constant expression."
4518 *
4519 * The same section of the GLSL ES 3.00.4 spec has similar language.
4520 */
4521 if (type->qualifier.flags.q.constant
4522 || type->qualifier.flags.q.uniform
4523 || (state->es_shader && state->current_function == NULL)) {
4524 ir_rvalue *new_rhs = validate_assignment(state, initializer_loc,
4525 lhs, rhs, true);
4526 if (new_rhs != NULL) {
4527 rhs = new_rhs;
4528
4529 /* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec
4530 * says:
4531 *
4532 * "A constant expression is one of
4533 *
4534 * ...
4535 *
4536 * - an expression formed by an operator on operands that are
4537 * all constant expressions, including getting an element of
4538 * a constant array, or a field of a constant structure, or
4539 * components of a constant vector. However, the sequence
4540 * operator ( , ) and the assignment operators ( =, +=, ...)
4541 * are not included in the operators that can create a
4542 * constant expression."
4543 *
4544 * Section 12.43 (Sequence operator and constant expressions) says:
4545 *
4546 * "Should the following construct be allowed?
4547 *
4548 * float a[2,3];
4549 *
4550 * The expression within the brackets uses the sequence operator
4551 * (',') and returns the integer 3 so the construct is declaring
4552 * a single-dimensional array of size 3. In some languages, the
4553 * construct declares a two-dimensional array. It would be
4554 * preferable to make this construct illegal to avoid confusion.
4555 *
4556 * One possibility is to change the definition of the sequence
4557 * operator so that it does not return a constant-expression and
4558 * hence cannot be used to declare an array size.
4559 *
4560 * RESOLUTION: The result of a sequence operator is not a
4561 * constant-expression."
4562 *
4563 * Section 4.3.3 (Constant Expressions) of the GLSL 4.30.9 spec
4564 * contains language almost identical to the section 4.3.3 in the
4565 * GLSL ES 3.00.4 spec. This is a new limitation for these GLSL
4566 * versions.
4567 */
4568 ir_constant *constant_value =
4569 rhs->constant_expression_value(mem_ctx);
4570
4571 if (!constant_value ||
4572 (state->is_version(430, 300) &&
4573 decl->initializer->has_sequence_subexpression())) {
4574 const char *const variable_mode =
4575 (type->qualifier.flags.q.constant)
4576 ? "const"
4577 : ((type->qualifier.flags.q.uniform) ? "uniform" : "global");
4578
4579 /* If ARB_shading_language_420pack is enabled, initializers of
4580 * const-qualified local variables do not have to be constant
4581 * expressions. Const-qualified global variables must still be
4582 * initialized with constant expressions.
4583 */
4584 if (!state->has_420pack()
4585 || state->current_function == NULL) {
4586 _mesa_glsl_error(& initializer_loc, state,
4587 "initializer of %s variable `%s' must be a "
4588 "constant expression",
4589 variable_mode,
4590 decl->identifier);
4591 if (var->type->is_numeric()) {
4592 /* Reduce cascading errors. */
4593 var->constant_value = type->qualifier.flags.q.constant
4594 ? ir_constant::zero(state, var->type) : NULL;
4595 }
4596 }
4597 } else {
4598 rhs = constant_value;
4599 var->constant_value = type->qualifier.flags.q.constant
4600 ? constant_value : NULL;
4601 }
4602 } else {
4603 if (var->type->is_numeric()) {
4604 /* Reduce cascading errors. */
4605 rhs = var->constant_value = type->qualifier.flags.q.constant
4606 ? ir_constant::zero(state, var->type) : NULL;
4607 }
4608 }
4609 }
4610
4611 if (rhs && !rhs->type->is_error()) {
4612 bool temp = var->data.read_only;
4613 if (type->qualifier.flags.q.constant)
4614 var->data.read_only = false;
4615
4616 /* Never emit code to initialize a uniform.
4617 */
4618 const glsl_type *initializer_type;
4619 bool error_emitted = false;
4620 if (!type->qualifier.flags.q.uniform) {
4621 error_emitted =
4622 do_assignment(initializer_instructions, state,
4623 NULL, lhs, rhs,
4624 &result, true, true,
4625 type->get_location());
4626 initializer_type = result->type;
4627 } else
4628 initializer_type = rhs->type;
4629
4630 if (!error_emitted) {
4631 var->constant_initializer = rhs->constant_expression_value(mem_ctx);
4632 var->data.has_initializer = true;
4633 var->data.is_implicit_initializer = false;
4634
4635 /* If the declared variable is an unsized array, it must inherrit
4636 * its full type from the initializer. A declaration such as
4637 *
4638 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
4639 *
4640 * becomes
4641 *
4642 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
4643 *
4644 * The assignment generated in the if-statement (below) will also
4645 * automatically handle this case for non-uniforms.
4646 *
4647 * If the declared variable is not an array, the types must
4648 * already match exactly. As a result, the type assignment
4649 * here can be done unconditionally. For non-uniforms the call
4650 * to do_assignment can change the type of the initializer (via
4651 * the implicit conversion rules). For uniforms the initializer
4652 * must be a constant expression, and the type of that expression
4653 * was validated above.
4654 */
4655 var->type = initializer_type;
4656 }
4657
4658 var->data.read_only = temp;
4659 }
4660
4661 return result;
4662 }
4663
4664 static void
4665 validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state *state,
4666 YYLTYPE loc, ir_variable *var,
4667 unsigned num_vertices,
4668 unsigned *size,
4669 const char *var_category)
4670 {
4671 if (var->type->is_unsized_array()) {
4672 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
4673 *
4674 * All geometry shader input unsized array declarations will be
4675 * sized by an earlier input layout qualifier, when present, as per
4676 * the following table.
4677 *
4678 * Followed by a table mapping each allowed input layout qualifier to
4679 * the corresponding input length.
4680 *
4681 * Similarly for tessellation control shader outputs.
4682 */
4683 if (num_vertices != 0)
4684 var->type = glsl_type::get_array_instance(var->type->fields.array,
4685 num_vertices);
4686 } else {
4687 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
4688 * includes the following examples of compile-time errors:
4689 *
4690 * // code sequence within one shader...
4691 * in vec4 Color1[]; // size unknown
4692 * ...Color1.length()...// illegal, length() unknown
4693 * in vec4 Color2[2]; // size is 2
4694 * ...Color1.length()...// illegal, Color1 still has no size
4695 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
4696 * layout(lines) in; // legal, input size is 2, matching
4697 * in vec4 Color4[3]; // illegal, contradicts layout
4698 * ...
4699 *
4700 * To detect the case illustrated by Color3, we verify that the size of
4701 * an explicitly-sized array matches the size of any previously declared
4702 * explicitly-sized array. To detect the case illustrated by Color4, we
4703 * verify that the size of an explicitly-sized array is consistent with
4704 * any previously declared input layout.
4705 */
4706 if (num_vertices != 0 && var->type->length != num_vertices) {
4707 _mesa_glsl_error(&loc, state,
4708 "%s size contradicts previously declared layout "
4709 "(size is %u, but layout requires a size of %u)",
4710 var_category, var->type->length, num_vertices);
4711 } else if (*size != 0 && var->type->length != *size) {
4712 _mesa_glsl_error(&loc, state,
4713 "%s sizes are inconsistent (size is %u, but a "
4714 "previous declaration has size %u)",
4715 var_category, var->type->length, *size);
4716 } else {
4717 *size = var->type->length;
4718 }
4719 }
4720 }
4721
4722 static void
4723 handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state *state,
4724 YYLTYPE loc, ir_variable *var)
4725 {
4726 unsigned num_vertices = 0;
4727
4728 if (state->tcs_output_vertices_specified) {
4729 if (!state->out_qualifier->vertices->
4730 process_qualifier_constant(state, "vertices",
4731 &num_vertices, false)) {
4732 return;
4733 }
4734
4735 if (num_vertices > state->Const.MaxPatchVertices) {
4736 _mesa_glsl_error(&loc, state, "vertices (%d) exceeds "
4737 "GL_MAX_PATCH_VERTICES", num_vertices);
4738 return;
4739 }
4740 }
4741
4742 if (!var->type->is_array() && !var->data.patch) {
4743 _mesa_glsl_error(&loc, state,
4744 "tessellation control shader outputs must be arrays");
4745
4746 /* To avoid cascading failures, short circuit the checks below. */
4747 return;
4748 }
4749
4750 if (var->data.patch)
4751 return;
4752
4753 validate_layout_qualifier_vertex_count(state, loc, var, num_vertices,
4754 &state->tcs_output_size,
4755 "tessellation control shader output");
4756 }
4757
4758 /**
4759 * Do additional processing necessary for tessellation control/evaluation shader
4760 * input declarations. This covers both interface block arrays and bare input
4761 * variables.
4762 */
4763 static void
4764 handle_tess_shader_input_decl(struct _mesa_glsl_parse_state *state,
4765 YYLTYPE loc, ir_variable *var)
4766 {
4767 if (!var->type->is_array() && !var->data.patch) {
4768 _mesa_glsl_error(&loc, state,
4769 "per-vertex tessellation shader inputs must be arrays");
4770 /* Avoid cascading failures. */
4771 return;
4772 }
4773
4774 if (var->data.patch)
4775 return;
4776
4777 /* The ARB_tessellation_shader spec says:
4778 *
4779 * "Declaring an array size is optional. If no size is specified, it
4780 * will be taken from the implementation-dependent maximum patch size
4781 * (gl_MaxPatchVertices). If a size is specified, it must match the
4782 * maximum patch size; otherwise, a compile or link error will occur."
4783 *
4784 * This text appears twice, once for TCS inputs, and again for TES inputs.
4785 */
4786 if (var->type->is_unsized_array()) {
4787 var->type = glsl_type::get_array_instance(var->type->fields.array,
4788 state->Const.MaxPatchVertices);
4789 } else if (var->type->length != state->Const.MaxPatchVertices) {
4790 _mesa_glsl_error(&loc, state,
4791 "per-vertex tessellation shader input arrays must be "
4792 "sized to gl_MaxPatchVertices (%d).",
4793 state->Const.MaxPatchVertices);
4794 }
4795 }
4796
4797
4798 /**
4799 * Do additional processing necessary for geometry shader input declarations
4800 * (this covers both interface blocks arrays and bare input variables).
4801 */
4802 static void
4803 handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state *state,
4804 YYLTYPE loc, ir_variable *var)
4805 {
4806 unsigned num_vertices = 0;
4807
4808 if (state->gs_input_prim_type_specified) {
4809 num_vertices = vertices_per_prim(state->in_qualifier->prim_type);
4810 }
4811
4812 /* Geometry shader input variables must be arrays. Caller should have
4813 * reported an error for this.
4814 */
4815 if (!var->type->is_array()) {
4816 assert(state->error);
4817
4818 /* To avoid cascading failures, short circuit the checks below. */
4819 return;
4820 }
4821
4822 validate_layout_qualifier_vertex_count(state, loc, var, num_vertices,
4823 &state->gs_input_size,
4824 "geometry shader input");
4825 }
4826
4827 static void
4828 validate_identifier(const char *identifier, YYLTYPE loc,
4829 struct _mesa_glsl_parse_state *state)
4830 {
4831 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
4832 *
4833 * "Identifiers starting with "gl_" are reserved for use by
4834 * OpenGL, and may not be declared in a shader as either a
4835 * variable or a function."
4836 */
4837 if (is_gl_identifier(identifier)) {
4838 _mesa_glsl_error(&loc, state,
4839 "identifier `%s' uses reserved `gl_' prefix",
4840 identifier);
4841 } else if (strstr(identifier, "__")) {
4842 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
4843 * spec:
4844 *
4845 * "In addition, all identifiers containing two
4846 * consecutive underscores (__) are reserved as
4847 * possible future keywords."
4848 *
4849 * The intention is that names containing __ are reserved for internal
4850 * use by the implementation, and names prefixed with GL_ are reserved
4851 * for use by Khronos. Names simply containing __ are dangerous to use,
4852 * but should be allowed.
4853 *
4854 * A future version of the GLSL specification will clarify this.
4855 */
4856 _mesa_glsl_warning(&loc, state,
4857 "identifier `%s' uses reserved `__' string",
4858 identifier);
4859 }
4860 }
4861
4862 ir_rvalue *
4863 ast_declarator_list::hir(exec_list *instructions,
4864 struct _mesa_glsl_parse_state *state)
4865 {
4866 void *ctx = state;
4867 const struct glsl_type *decl_type;
4868 const char *type_name = NULL;
4869 ir_rvalue *result = NULL;
4870 YYLTYPE loc = this->get_location();
4871
4872 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
4873 *
4874 * "To ensure that a particular output variable is invariant, it is
4875 * necessary to use the invariant qualifier. It can either be used to
4876 * qualify a previously declared variable as being invariant
4877 *
4878 * invariant gl_Position; // make existing gl_Position be invariant"
4879 *
4880 * In these cases the parser will set the 'invariant' flag in the declarator
4881 * list, and the type will be NULL.
4882 */
4883 if (this->invariant) {
4884 assert(this->type == NULL);
4885
4886 if (state->current_function != NULL) {
4887 _mesa_glsl_error(& loc, state,
4888 "all uses of `invariant' keyword must be at global "
4889 "scope");
4890 }
4891
4892 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
4893 assert(decl->array_specifier == NULL);
4894 assert(decl->initializer == NULL);
4895
4896 ir_variable *const earlier =
4897 state->symbols->get_variable(decl->identifier);
4898 if (earlier == NULL) {
4899 _mesa_glsl_error(& loc, state,
4900 "undeclared variable `%s' cannot be marked "
4901 "invariant", decl->identifier);
4902 } else if (!is_allowed_invariant(earlier, state)) {
4903 _mesa_glsl_error(&loc, state,
4904 "`%s' cannot be marked invariant; interfaces between "
4905 "shader stages only.", decl->identifier);
4906 } else if (earlier->data.used) {
4907 _mesa_glsl_error(& loc, state,
4908 "variable `%s' may not be redeclared "
4909 "`invariant' after being used",
4910 earlier->name);
4911 } else {
4912 earlier->data.explicit_invariant = true;
4913 earlier->data.invariant = true;
4914 }
4915 }
4916
4917 /* Invariant redeclarations do not have r-values.
4918 */
4919 return NULL;
4920 }
4921
4922 if (this->precise) {
4923 assert(this->type == NULL);
4924
4925 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
4926 assert(decl->array_specifier == NULL);
4927 assert(decl->initializer == NULL);
4928
4929 ir_variable *const earlier =
4930 state->symbols->get_variable(decl->identifier);
4931 if (earlier == NULL) {
4932 _mesa_glsl_error(& loc, state,
4933 "undeclared variable `%s' cannot be marked "
4934 "precise", decl->identifier);
4935 } else if (state->current_function != NULL &&
4936 !state->symbols->name_declared_this_scope(decl->identifier)) {
4937 /* Note: we have to check if we're in a function, since
4938 * builtins are treated as having come from another scope.
4939 */
4940 _mesa_glsl_error(& loc, state,
4941 "variable `%s' from an outer scope may not be "
4942 "redeclared `precise' in this scope",
4943 earlier->name);
4944 } else if (earlier->data.used) {
4945 _mesa_glsl_error(& loc, state,
4946 "variable `%s' may not be redeclared "
4947 "`precise' after being used",
4948 earlier->name);
4949 } else {
4950 earlier->data.precise = true;
4951 }
4952 }
4953
4954 /* Precise redeclarations do not have r-values either. */
4955 return NULL;
4956 }
4957
4958 assert(this->type != NULL);
4959 assert(!this->invariant);
4960 assert(!this->precise);
4961
4962 /* GL_EXT_shader_image_load_store base type uses GLSL_TYPE_VOID as a special value to
4963 * indicate that it needs to be updated later (see glsl_parser.yy).
4964 * This is done here, based on the layout qualifier and the type of the image var
4965 */
4966 if (this->type->qualifier.flags.q.explicit_image_format &&
4967 this->type->specifier->type->is_image() &&
4968 this->type->qualifier.image_base_type == GLSL_TYPE_VOID) {
4969 /* "The ARB_shader_image_load_store says:
4970 * If both extensions are enabled in the shading language, the "size*" layout
4971 * qualifiers are treated as format qualifiers, and are mapped to equivalent
4972 * format qualifiers in the table below, according to the type of image
4973 * variable.
4974 * image* iimage* uimage*
4975 * -------- -------- --------
4976 * size1x8 n/a r8i r8ui
4977 * size1x16 r16f r16i r16ui
4978 * size1x32 r32f r32i r32ui
4979 * size2x32 rg32f rg32i rg32ui
4980 * size4x32 rgba32f rgba32i rgba32ui"
4981 */
4982 if (strncmp(this->type->specifier->type_name, "image", strlen("image")) == 0) {
4983 switch (this->type->qualifier.image_format) {
4984 case PIPE_FORMAT_R8_SINT:
4985 /* No valid qualifier in this case, driver will need to look at
4986 * the underlying image's format (just like no qualifier being
4987 * present).
4988 */
4989 this->type->qualifier.image_format = PIPE_FORMAT_NONE;
4990 break;
4991 case PIPE_FORMAT_R16_SINT:
4992 this->type->qualifier.image_format = PIPE_FORMAT_R16_FLOAT;
4993 break;
4994 case PIPE_FORMAT_R32_SINT:
4995 this->type->qualifier.image_format = PIPE_FORMAT_R32_FLOAT;
4996 break;
4997 case PIPE_FORMAT_R32G32_SINT:
4998 this->type->qualifier.image_format = PIPE_FORMAT_R32G32_FLOAT;
4999 break;
5000 case PIPE_FORMAT_R32G32B32A32_SINT:
5001 this->type->qualifier.image_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
5002 break;
5003 default:
5004 unreachable("Unknown image format");
5005 }
5006 this->type->qualifier.image_base_type = GLSL_TYPE_FLOAT;
5007 } else if (strncmp(this->type->specifier->type_name, "uimage", strlen("uimage")) == 0) {
5008 switch (this->type->qualifier.image_format) {
5009 case PIPE_FORMAT_R8_SINT:
5010 this->type->qualifier.image_format = PIPE_FORMAT_R8_UINT;
5011 break;
5012 case PIPE_FORMAT_R16_SINT:
5013 this->type->qualifier.image_format = PIPE_FORMAT_R16_UINT;
5014 break;
5015 case PIPE_FORMAT_R32_SINT:
5016 this->type->qualifier.image_format = PIPE_FORMAT_R32_UINT;
5017 break;
5018 case PIPE_FORMAT_R32G32_SINT:
5019 this->type->qualifier.image_format = PIPE_FORMAT_R32G32_UINT;
5020 break;
5021 case PIPE_FORMAT_R32G32B32A32_SINT:
5022 this->type->qualifier.image_format = PIPE_FORMAT_R32G32B32A32_UINT;
5023 break;
5024 default:
5025 unreachable("Unknown image format");
5026 }
5027 this->type->qualifier.image_base_type = GLSL_TYPE_UINT;
5028 } else if (strncmp(this->type->specifier->type_name, "iimage", strlen("iimage")) == 0) {
5029 this->type->qualifier.image_base_type = GLSL_TYPE_INT;
5030 } else {
5031 assert(false);
5032 }
5033 }
5034
5035 /* The type specifier may contain a structure definition. Process that
5036 * before any of the variable declarations.
5037 */
5038 (void) this->type->specifier->hir(instructions, state);
5039
5040 decl_type = this->type->glsl_type(& type_name, state);
5041
5042 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
5043 * "Buffer variables may only be declared inside interface blocks
5044 * (section 4.3.9 “Interface Blocks”), which are then referred to as
5045 * shader storage blocks. It is a compile-time error to declare buffer
5046 * variables at global scope (outside a block)."
5047 */
5048 if (type->qualifier.flags.q.buffer && !decl_type->is_interface()) {
5049 _mesa_glsl_error(&loc, state,
5050 "buffer variables cannot be declared outside "
5051 "interface blocks");
5052 }
5053
5054 /* An offset-qualified atomic counter declaration sets the default
5055 * offset for the next declaration within the same atomic counter
5056 * buffer.
5057 */
5058 if (decl_type && decl_type->contains_atomic()) {
5059 if (type->qualifier.flags.q.explicit_binding &&
5060 type->qualifier.flags.q.explicit_offset) {
5061 unsigned qual_binding;
5062 unsigned qual_offset;
5063 if (process_qualifier_constant(state, &loc, "binding",
5064 type->qualifier.binding,
5065 &qual_binding)
5066 && process_qualifier_constant(state, &loc, "offset",
5067 type->qualifier.offset,
5068 &qual_offset)) {
5069 if (qual_binding < ARRAY_SIZE(state->atomic_counter_offsets))
5070 state->atomic_counter_offsets[qual_binding] = qual_offset;
5071 }
5072 }
5073
5074 ast_type_qualifier allowed_atomic_qual_mask;
5075 allowed_atomic_qual_mask.flags.i = 0;
5076 allowed_atomic_qual_mask.flags.q.explicit_binding = 1;
5077 allowed_atomic_qual_mask.flags.q.explicit_offset = 1;
5078 allowed_atomic_qual_mask.flags.q.uniform = 1;
5079
5080 type->qualifier.validate_flags(&loc, state, allowed_atomic_qual_mask,
5081 "invalid layout qualifier for",
5082 "atomic_uint");
5083 }
5084
5085 if (this->declarations.is_empty()) {
5086 /* If there is no structure involved in the program text, there are two
5087 * possible scenarios:
5088 *
5089 * - The program text contained something like 'vec4;'. This is an
5090 * empty declaration. It is valid but weird. Emit a warning.
5091 *
5092 * - The program text contained something like 'S;' and 'S' is not the
5093 * name of a known structure type. This is both invalid and weird.
5094 * Emit an error.
5095 *
5096 * - The program text contained something like 'mediump float;'
5097 * when the programmer probably meant 'precision mediump
5098 * float;' Emit a warning with a description of what they
5099 * probably meant to do.
5100 *
5101 * Note that if decl_type is NULL and there is a structure involved,
5102 * there must have been some sort of error with the structure. In this
5103 * case we assume that an error was already generated on this line of
5104 * code for the structure. There is no need to generate an additional,
5105 * confusing error.
5106 */
5107 assert(this->type->specifier->structure == NULL || decl_type != NULL
5108 || state->error);
5109
5110 if (decl_type == NULL) {
5111 _mesa_glsl_error(&loc, state,
5112 "invalid type `%s' in empty declaration",
5113 type_name);
5114 } else {
5115 if (decl_type->is_array()) {
5116 /* From Section 13.22 (Array Declarations) of the GLSL ES 3.2
5117 * spec:
5118 *
5119 * "... any declaration that leaves the size undefined is
5120 * disallowed as this would add complexity and there are no
5121 * use-cases."
5122 */
5123 if (state->es_shader && decl_type->is_unsized_array()) {
5124 _mesa_glsl_error(&loc, state, "array size must be explicitly "
5125 "or implicitly defined");
5126 }
5127
5128 /* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec:
5129 *
5130 * "The combinations of types and qualifiers that cause
5131 * compile-time or link-time errors are the same whether or not
5132 * the declaration is empty."
5133 */
5134 validate_array_dimensions(decl_type, state, &loc);
5135 }
5136
5137 if (decl_type->is_atomic_uint()) {
5138 /* Empty atomic counter declarations are allowed and useful
5139 * to set the default offset qualifier.
5140 */
5141 return NULL;
5142 } else if (this->type->qualifier.precision != ast_precision_none) {
5143 if (this->type->specifier->structure != NULL) {
5144 _mesa_glsl_error(&loc, state,
5145 "precision qualifiers can't be applied "
5146 "to structures");
5147 } else {
5148 static const char *const precision_names[] = {
5149 "highp",
5150 "highp",
5151 "mediump",
5152 "lowp"
5153 };
5154
5155 _mesa_glsl_warning(&loc, state,
5156 "empty declaration with precision "
5157 "qualifier, to set the default precision, "
5158 "use `precision %s %s;'",
5159 precision_names[this->type->
5160 qualifier.precision],
5161 type_name);
5162 }
5163 } else if (this->type->specifier->structure == NULL) {
5164 _mesa_glsl_warning(&loc, state, "empty declaration");
5165 }
5166 }
5167 }
5168
5169 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
5170 const struct glsl_type *var_type;
5171 ir_variable *var;
5172 const char *identifier = decl->identifier;
5173 /* FINISHME: Emit a warning if a variable declaration shadows a
5174 * FINISHME: declaration at a higher scope.
5175 */
5176
5177 if ((decl_type == NULL) || decl_type->is_void()) {
5178 if (type_name != NULL) {
5179 _mesa_glsl_error(& loc, state,
5180 "invalid type `%s' in declaration of `%s'",
5181 type_name, decl->identifier);
5182 } else {
5183 _mesa_glsl_error(& loc, state,
5184 "invalid type in declaration of `%s'",
5185 decl->identifier);
5186 }
5187 continue;
5188 }
5189
5190 if (this->type->qualifier.is_subroutine_decl()) {
5191 const glsl_type *t;
5192 const char *name;
5193
5194 t = state->symbols->get_type(this->type->specifier->type_name);
5195 if (!t)
5196 _mesa_glsl_error(& loc, state,
5197 "invalid type in declaration of `%s'",
5198 decl->identifier);
5199 name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), decl->identifier);
5200
5201 identifier = name;
5202
5203 }
5204 var_type = process_array_type(&loc, decl_type, decl->array_specifier,
5205 state);
5206
5207 var = new(ctx) ir_variable(var_type, identifier, ir_var_auto);
5208
5209 /* The 'varying in' and 'varying out' qualifiers can only be used with
5210 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
5211 * yet.
5212 */
5213 if (this->type->qualifier.flags.q.varying) {
5214 if (this->type->qualifier.flags.q.in) {
5215 _mesa_glsl_error(& loc, state,
5216 "`varying in' qualifier in declaration of "
5217 "`%s' only valid for geometry shaders using "
5218 "ARB_geometry_shader4 or EXT_geometry_shader4",
5219 decl->identifier);
5220 } else if (this->type->qualifier.flags.q.out) {
5221 _mesa_glsl_error(& loc, state,
5222 "`varying out' qualifier in declaration of "
5223 "`%s' only valid for geometry shaders using "
5224 "ARB_geometry_shader4 or EXT_geometry_shader4",
5225 decl->identifier);
5226 }
5227 }
5228
5229 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
5230 *
5231 * "Global variables can only use the qualifiers const,
5232 * attribute, uniform, or varying. Only one may be
5233 * specified.
5234 *
5235 * Local variables can only use the qualifier const."
5236 *
5237 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
5238 * any extension that adds the 'layout' keyword.
5239 */
5240 if (!state->is_version(130, 300)
5241 && !state->has_explicit_attrib_location()
5242 && !state->has_separate_shader_objects()
5243 && !state->ARB_fragment_coord_conventions_enable) {
5244 /* GL_EXT_gpu_shader4 only allows "varying out" on fragment shader
5245 * outputs. (the varying flag is not set by the parser)
5246 */
5247 if (this->type->qualifier.flags.q.out &&
5248 (!state->EXT_gpu_shader4_enable ||
5249 state->stage != MESA_SHADER_FRAGMENT)) {
5250 _mesa_glsl_error(& loc, state,
5251 "`out' qualifier in declaration of `%s' "
5252 "only valid for function parameters in %s",
5253 decl->identifier, state->get_version_string());
5254 }
5255 if (this->type->qualifier.flags.q.in) {
5256 _mesa_glsl_error(& loc, state,
5257 "`in' qualifier in declaration of `%s' "
5258 "only valid for function parameters in %s",
5259 decl->identifier, state->get_version_string());
5260 }
5261 /* FINISHME: Test for other invalid qualifiers. */
5262 }
5263
5264 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
5265 & loc, false);
5266 apply_layout_qualifier_to_variable(&this->type->qualifier, var, state,
5267 &loc);
5268
5269 if ((state->zero_init & (1u << var->data.mode)) &&
5270 (var->type->is_numeric() || var->type->is_boolean())) {
5271 const ir_constant_data data = { { 0 } };
5272 var->data.has_initializer = true;
5273 var->data.is_implicit_initializer = true;
5274 var->constant_initializer = new(var) ir_constant(var->type, &data);
5275 }
5276
5277 if (this->type->qualifier.flags.q.invariant) {
5278 if (!is_allowed_invariant(var, state)) {
5279 _mesa_glsl_error(&loc, state,
5280 "`%s' cannot be marked invariant; interfaces between "
5281 "shader stages only", var->name);
5282 }
5283 }
5284
5285 if (state->current_function != NULL) {
5286 const char *mode = NULL;
5287 const char *extra = "";
5288
5289 /* There is no need to check for 'inout' here because the parser will
5290 * only allow that in function parameter lists.
5291 */
5292 if (this->type->qualifier.flags.q.attribute) {
5293 mode = "attribute";
5294 } else if (this->type->qualifier.is_subroutine_decl()) {
5295 mode = "subroutine uniform";
5296 } else if (this->type->qualifier.flags.q.uniform) {
5297 mode = "uniform";
5298 } else if (this->type->qualifier.flags.q.varying) {
5299 mode = "varying";
5300 } else if (this->type->qualifier.flags.q.in) {
5301 mode = "in";
5302 extra = " or in function parameter list";
5303 } else if (this->type->qualifier.flags.q.out) {
5304 mode = "out";
5305 extra = " or in function parameter list";
5306 }
5307
5308 if (mode) {
5309 _mesa_glsl_error(& loc, state,
5310 "%s variable `%s' must be declared at "
5311 "global scope%s",
5312 mode, var->name, extra);
5313 }
5314 } else if (var->data.mode == ir_var_shader_in) {
5315 var->data.read_only = true;
5316
5317 if (state->stage == MESA_SHADER_VERTEX) {
5318 bool error_emitted = false;
5319
5320 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
5321 *
5322 * "Vertex shader inputs can only be float, floating-point
5323 * vectors, matrices, signed and unsigned integers and integer
5324 * vectors. Vertex shader inputs can also form arrays of these
5325 * types, but not structures."
5326 *
5327 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
5328 *
5329 * "Vertex shader inputs can only be float, floating-point
5330 * vectors, matrices, signed and unsigned integers and integer
5331 * vectors. They cannot be arrays or structures."
5332 *
5333 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
5334 *
5335 * "The attribute qualifier can be used only with float,
5336 * floating-point vectors, and matrices. Attribute variables
5337 * cannot be declared as arrays or structures."
5338 *
5339 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
5340 *
5341 * "Vertex shader inputs can only be float, floating-point
5342 * vectors, matrices, signed and unsigned integers and integer
5343 * vectors. Vertex shader inputs cannot be arrays or
5344 * structures."
5345 *
5346 * From section 4.3.4 of the ARB_bindless_texture spec:
5347 *
5348 * "(modify third paragraph of the section to allow sampler and
5349 * image types) ... Vertex shader inputs can only be float,
5350 * single-precision floating-point scalars, single-precision
5351 * floating-point vectors, matrices, signed and unsigned
5352 * integers and integer vectors, sampler and image types."
5353 */
5354 const glsl_type *check_type = var->type->without_array();
5355
5356 switch (check_type->base_type) {
5357 case GLSL_TYPE_FLOAT:
5358 break;
5359 case GLSL_TYPE_UINT64:
5360 case GLSL_TYPE_INT64:
5361 break;
5362 case GLSL_TYPE_UINT:
5363 case GLSL_TYPE_INT:
5364 if (state->is_version(120, 300) || state->EXT_gpu_shader4_enable)
5365 break;
5366 case GLSL_TYPE_DOUBLE:
5367 if (check_type->is_double() && (state->is_version(410, 0) || state->ARB_vertex_attrib_64bit_enable))
5368 break;
5369 case GLSL_TYPE_SAMPLER:
5370 if (check_type->is_sampler() && state->has_bindless())
5371 break;
5372 case GLSL_TYPE_IMAGE:
5373 if (check_type->is_image() && state->has_bindless())
5374 break;
5375 /* FALLTHROUGH */
5376 default:
5377 _mesa_glsl_error(& loc, state,
5378 "vertex shader input / attribute cannot have "
5379 "type %s`%s'",
5380 var->type->is_array() ? "array of " : "",
5381 check_type->name);
5382 error_emitted = true;
5383 }
5384
5385 if (!error_emitted && var->type->is_array() &&
5386 !state->check_version(150, 0, &loc,
5387 "vertex shader input / attribute "
5388 "cannot have array type")) {
5389 error_emitted = true;
5390 }
5391 } else if (state->stage == MESA_SHADER_GEOMETRY) {
5392 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5393 *
5394 * Geometry shader input variables get the per-vertex values
5395 * written out by vertex shader output variables of the same
5396 * names. Since a geometry shader operates on a set of
5397 * vertices, each input varying variable (or input block, see
5398 * interface blocks below) needs to be declared as an array.
5399 */
5400 if (!var->type->is_array()) {
5401 _mesa_glsl_error(&loc, state,
5402 "geometry shader inputs must be arrays");
5403 }
5404
5405 handle_geometry_shader_input_decl(state, loc, var);
5406 } else if (state->stage == MESA_SHADER_FRAGMENT) {
5407 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
5408 *
5409 * It is a compile-time error to declare a fragment shader
5410 * input with, or that contains, any of the following types:
5411 *
5412 * * A boolean type
5413 * * An opaque type
5414 * * An array of arrays
5415 * * An array of structures
5416 * * A structure containing an array
5417 * * A structure containing a structure
5418 */
5419 if (state->es_shader) {
5420 const glsl_type *check_type = var->type->without_array();
5421 if (check_type->is_boolean() ||
5422 check_type->contains_opaque()) {
5423 _mesa_glsl_error(&loc, state,
5424 "fragment shader input cannot have type %s",
5425 check_type->name);
5426 }
5427 if (var->type->is_array() &&
5428 var->type->fields.array->is_array()) {
5429 _mesa_glsl_error(&loc, state,
5430 "%s shader output "
5431 "cannot have an array of arrays",
5432 _mesa_shader_stage_to_string(state->stage));
5433 }
5434 if (var->type->is_array() &&
5435 var->type->fields.array->is_struct()) {
5436 _mesa_glsl_error(&loc, state,
5437 "fragment shader input "
5438 "cannot have an array of structs");
5439 }
5440 if (var->type->is_struct()) {
5441 for (unsigned i = 0; i < var->type->length; i++) {
5442 if (var->type->fields.structure[i].type->is_array() ||
5443 var->type->fields.structure[i].type->is_struct())
5444 _mesa_glsl_error(&loc, state,
5445 "fragment shader input cannot have "
5446 "a struct that contains an "
5447 "array or struct");
5448 }
5449 }
5450 }
5451 } else if (state->stage == MESA_SHADER_TESS_CTRL ||
5452 state->stage == MESA_SHADER_TESS_EVAL) {
5453 handle_tess_shader_input_decl(state, loc, var);
5454 }
5455 } else if (var->data.mode == ir_var_shader_out) {
5456 const glsl_type *check_type = var->type->without_array();
5457
5458 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
5459 *
5460 * It is a compile-time error to declare a fragment shader output
5461 * that contains any of the following:
5462 *
5463 * * A Boolean type (bool, bvec2 ...)
5464 * * A double-precision scalar or vector (double, dvec2 ...)
5465 * * An opaque type
5466 * * Any matrix type
5467 * * A structure
5468 */
5469 if (state->stage == MESA_SHADER_FRAGMENT) {
5470 if (check_type->is_struct() || check_type->is_matrix())
5471 _mesa_glsl_error(&loc, state,
5472 "fragment shader output "
5473 "cannot have struct or matrix type");
5474 switch (check_type->base_type) {
5475 case GLSL_TYPE_UINT:
5476 case GLSL_TYPE_INT:
5477 case GLSL_TYPE_FLOAT:
5478 break;
5479 default:
5480 _mesa_glsl_error(&loc, state,
5481 "fragment shader output cannot have "
5482 "type %s", check_type->name);
5483 }
5484 }
5485
5486 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
5487 *
5488 * It is a compile-time error to declare a vertex shader output
5489 * with, or that contains, any of the following types:
5490 *
5491 * * A boolean type
5492 * * An opaque type
5493 * * An array of arrays
5494 * * An array of structures
5495 * * A structure containing an array
5496 * * A structure containing a structure
5497 *
5498 * It is a compile-time error to declare a fragment shader output
5499 * with, or that contains, any of the following types:
5500 *
5501 * * A boolean type
5502 * * An opaque type
5503 * * A matrix
5504 * * A structure
5505 * * An array of array
5506 *
5507 * ES 3.20 updates this to apply to tessellation and geometry shaders
5508 * as well. Because there are per-vertex arrays in the new stages,
5509 * it strikes the "array of..." rules and replaces them with these:
5510 *
5511 * * For per-vertex-arrayed variables (applies to tessellation
5512 * control, tessellation evaluation and geometry shaders):
5513 *
5514 * * Per-vertex-arrayed arrays of arrays
5515 * * Per-vertex-arrayed arrays of structures
5516 *
5517 * * For non-per-vertex-arrayed variables:
5518 *
5519 * * An array of arrays
5520 * * An array of structures
5521 *
5522 * which basically says to unwrap the per-vertex aspect and apply
5523 * the old rules.
5524 */
5525 if (state->es_shader) {
5526 if (var->type->is_array() &&
5527 var->type->fields.array->is_array()) {
5528 _mesa_glsl_error(&loc, state,
5529 "%s shader output "
5530 "cannot have an array of arrays",
5531 _mesa_shader_stage_to_string(state->stage));
5532 }
5533 if (state->stage <= MESA_SHADER_GEOMETRY) {
5534 const glsl_type *type = var->type;
5535
5536 if (state->stage == MESA_SHADER_TESS_CTRL &&
5537 !var->data.patch && var->type->is_array()) {
5538 type = var->type->fields.array;
5539 }
5540
5541 if (type->is_array() && type->fields.array->is_struct()) {
5542 _mesa_glsl_error(&loc, state,
5543 "%s shader output cannot have "
5544 "an array of structs",
5545 _mesa_shader_stage_to_string(state->stage));
5546 }
5547 if (type->is_struct()) {
5548 for (unsigned i = 0; i < type->length; i++) {
5549 if (type->fields.structure[i].type->is_array() ||
5550 type->fields.structure[i].type->is_struct())
5551 _mesa_glsl_error(&loc, state,
5552 "%s shader output cannot have a "
5553 "struct that contains an "
5554 "array or struct",
5555 _mesa_shader_stage_to_string(state->stage));
5556 }
5557 }
5558 }
5559 }
5560
5561 if (state->stage == MESA_SHADER_TESS_CTRL) {
5562 handle_tess_ctrl_shader_output_decl(state, loc, var);
5563 }
5564 } else if (var->type->contains_subroutine()) {
5565 /* declare subroutine uniforms as hidden */
5566 var->data.how_declared = ir_var_hidden;
5567 }
5568
5569 /* From section 4.3.4 of the GLSL 4.00 spec:
5570 * "Input variables may not be declared using the patch in qualifier
5571 * in tessellation control or geometry shaders."
5572 *
5573 * From section 4.3.6 of the GLSL 4.00 spec:
5574 * "It is an error to use patch out in a vertex, tessellation
5575 * evaluation, or geometry shader."
5576 *
5577 * This doesn't explicitly forbid using them in a fragment shader, but
5578 * that's probably just an oversight.
5579 */
5580 if (state->stage != MESA_SHADER_TESS_EVAL
5581 && this->type->qualifier.flags.q.patch
5582 && this->type->qualifier.flags.q.in) {
5583
5584 _mesa_glsl_error(&loc, state, "'patch in' can only be used in a "
5585 "tessellation evaluation shader");
5586 }
5587
5588 if (state->stage != MESA_SHADER_TESS_CTRL
5589 && this->type->qualifier.flags.q.patch
5590 && this->type->qualifier.flags.q.out) {
5591
5592 _mesa_glsl_error(&loc, state, "'patch out' can only be used in a "
5593 "tessellation control shader");
5594 }
5595
5596 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
5597 */
5598 if (this->type->qualifier.precision != ast_precision_none) {
5599 state->check_precision_qualifiers_allowed(&loc);
5600 }
5601
5602 if (this->type->qualifier.precision != ast_precision_none &&
5603 !precision_qualifier_allowed(var->type)) {
5604 _mesa_glsl_error(&loc, state,
5605 "precision qualifiers apply only to floating point"
5606 ", integer and opaque types");
5607 }
5608
5609 /* From section 4.1.7 of the GLSL 4.40 spec:
5610 *
5611 * "[Opaque types] can only be declared as function
5612 * parameters or uniform-qualified variables."
5613 *
5614 * From section 4.1.7 of the ARB_bindless_texture spec:
5615 *
5616 * "Samplers may be declared as shader inputs and outputs, as uniform
5617 * variables, as temporary variables, and as function parameters."
5618 *
5619 * From section 4.1.X of the ARB_bindless_texture spec:
5620 *
5621 * "Images may be declared as shader inputs and outputs, as uniform
5622 * variables, as temporary variables, and as function parameters."
5623 */
5624 if (!this->type->qualifier.flags.q.uniform &&
5625 (var_type->contains_atomic() ||
5626 (!state->has_bindless() && var_type->contains_opaque()))) {
5627 _mesa_glsl_error(&loc, state,
5628 "%s variables must be declared uniform",
5629 state->has_bindless() ? "atomic" : "opaque");
5630 }
5631
5632 /* Process the initializer and add its instructions to a temporary
5633 * list. This list will be added to the instruction stream (below) after
5634 * the declaration is added. This is done because in some cases (such as
5635 * redeclarations) the declaration may not actually be added to the
5636 * instruction stream.
5637 */
5638 exec_list initializer_instructions;
5639
5640 /* Examine var name here since var may get deleted in the next call */
5641 bool var_is_gl_id = is_gl_identifier(var->name);
5642
5643 bool is_redeclaration;
5644 var = get_variable_being_redeclared(&var, decl->get_location(), state,
5645 false /* allow_all_redeclarations */,
5646 &is_redeclaration);
5647 if (is_redeclaration) {
5648 if (var_is_gl_id &&
5649 var->data.how_declared == ir_var_declared_in_block) {
5650 _mesa_glsl_error(&loc, state,
5651 "`%s' has already been redeclared using "
5652 "gl_PerVertex", var->name);
5653 }
5654 var->data.how_declared = ir_var_declared_normally;
5655 }
5656
5657 if (decl->initializer != NULL) {
5658 result = process_initializer(var,
5659 decl, this->type,
5660 &initializer_instructions, state);
5661 } else {
5662 validate_array_dimensions(var_type, state, &loc);
5663 }
5664
5665 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
5666 *
5667 * "It is an error to write to a const variable outside of
5668 * its declaration, so they must be initialized when
5669 * declared."
5670 */
5671 if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
5672 _mesa_glsl_error(& loc, state,
5673 "const declaration of `%s' must be initialized",
5674 decl->identifier);
5675 }
5676
5677 if (state->es_shader) {
5678 const glsl_type *const t = var->type;
5679
5680 /* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs.
5681 *
5682 * The GL_OES_tessellation_shader spec says about inputs:
5683 *
5684 * "Declaring an array size is optional. If no size is specified,
5685 * it will be taken from the implementation-dependent maximum
5686 * patch size (gl_MaxPatchVertices)."
5687 *
5688 * and about TCS outputs:
5689 *
5690 * "If no size is specified, it will be taken from output patch
5691 * size declared in the shader."
5692 *
5693 * The GL_OES_geometry_shader spec says:
5694 *
5695 * "All geometry shader input unsized array declarations will be
5696 * sized by an earlier input primitive layout qualifier, when
5697 * present, as per the following table."
5698 */
5699 const bool implicitly_sized =
5700 (var->data.mode == ir_var_shader_in &&
5701 state->stage >= MESA_SHADER_TESS_CTRL &&
5702 state->stage <= MESA_SHADER_GEOMETRY) ||
5703 (var->data.mode == ir_var_shader_out &&
5704 state->stage == MESA_SHADER_TESS_CTRL);
5705
5706 if (t->is_unsized_array() && !implicitly_sized)
5707 /* Section 10.17 of the GLSL ES 1.00 specification states that
5708 * unsized array declarations have been removed from the language.
5709 * Arrays that are sized using an initializer are still explicitly
5710 * sized. However, GLSL ES 1.00 does not allow array
5711 * initializers. That is only allowed in GLSL ES 3.00.
5712 *
5713 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
5714 *
5715 * "An array type can also be formed without specifying a size
5716 * if the definition includes an initializer:
5717 *
5718 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
5719 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
5720 *
5721 * float a[5];
5722 * float b[] = a;"
5723 */
5724 _mesa_glsl_error(& loc, state,
5725 "unsized array declarations are not allowed in "
5726 "GLSL ES");
5727 }
5728
5729 /* Section 4.4.6.1 Atomic Counter Layout Qualifiers of the GLSL 4.60 spec:
5730 *
5731 * "It is a compile-time error to declare an unsized array of
5732 * atomic_uint"
5733 */
5734 if (var->type->is_unsized_array() &&
5735 var->type->without_array()->base_type == GLSL_TYPE_ATOMIC_UINT) {
5736 _mesa_glsl_error(& loc, state,
5737 "Unsized array of atomic_uint is not allowed");
5738 }
5739
5740 /* If the declaration is not a redeclaration, there are a few additional
5741 * semantic checks that must be applied. In addition, variable that was
5742 * created for the declaration should be added to the IR stream.
5743 */
5744 if (!is_redeclaration) {
5745 validate_identifier(decl->identifier, loc, state);
5746
5747 /* Add the variable to the symbol table. Note that the initializer's
5748 * IR was already processed earlier (though it hasn't been emitted
5749 * yet), without the variable in scope.
5750 *
5751 * This differs from most C-like languages, but it follows the GLSL
5752 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
5753 * spec:
5754 *
5755 * "Within a declaration, the scope of a name starts immediately
5756 * after the initializer if present or immediately after the name
5757 * being declared if not."
5758 */
5759 if (!state->symbols->add_variable(var)) {
5760 YYLTYPE loc = this->get_location();
5761 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
5762 "current scope", decl->identifier);
5763 continue;
5764 }
5765
5766 /* Push the variable declaration to the top. It means that all the
5767 * variable declarations will appear in a funny last-to-first order,
5768 * but otherwise we run into trouble if a function is prototyped, a
5769 * global var is decled, then the function is defined with usage of
5770 * the global var. See glslparsertest's CorrectModule.frag.
5771 */
5772 instructions->push_head(var);
5773 }
5774
5775 instructions->append_list(&initializer_instructions);
5776 }
5777
5778
5779 /* Generally, variable declarations do not have r-values. However,
5780 * one is used for the declaration in
5781 *
5782 * while (bool b = some_condition()) {
5783 * ...
5784 * }
5785 *
5786 * so we return the rvalue from the last seen declaration here.
5787 */
5788 return result;
5789 }
5790
5791
5792 ir_rvalue *
5793 ast_parameter_declarator::hir(exec_list *instructions,
5794 struct _mesa_glsl_parse_state *state)
5795 {
5796 void *ctx = state;
5797 const struct glsl_type *type;
5798 const char *name = NULL;
5799 YYLTYPE loc = this->get_location();
5800
5801 type = this->type->glsl_type(& name, state);
5802
5803 if (type == NULL) {
5804 if (name != NULL) {
5805 _mesa_glsl_error(& loc, state,
5806 "invalid type `%s' in declaration of `%s'",
5807 name, this->identifier);
5808 } else {
5809 _mesa_glsl_error(& loc, state,
5810 "invalid type in declaration of `%s'",
5811 this->identifier);
5812 }
5813
5814 type = glsl_type::error_type;
5815 }
5816
5817 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
5818 *
5819 * "Functions that accept no input arguments need not use void in the
5820 * argument list because prototypes (or definitions) are required and
5821 * therefore there is no ambiguity when an empty argument list "( )" is
5822 * declared. The idiom "(void)" as a parameter list is provided for
5823 * convenience."
5824 *
5825 * Placing this check here prevents a void parameter being set up
5826 * for a function, which avoids tripping up checks for main taking
5827 * parameters and lookups of an unnamed symbol.
5828 */
5829 if (type->is_void()) {
5830 if (this->identifier != NULL)
5831 _mesa_glsl_error(& loc, state,
5832 "named parameter cannot have type `void'");
5833
5834 is_void = true;
5835 return NULL;
5836 }
5837
5838 if (formal_parameter && (this->identifier == NULL)) {
5839 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
5840 return NULL;
5841 }
5842
5843 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
5844 * call already handled the "vec4[..] foo" case.
5845 */
5846 type = process_array_type(&loc, type, this->array_specifier, state);
5847
5848 if (!type->is_error() && type->is_unsized_array()) {
5849 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
5850 "a declared size");
5851 type = glsl_type::error_type;
5852 }
5853
5854 is_void = false;
5855 ir_variable *var = new(ctx)
5856 ir_variable(type, this->identifier, ir_var_function_in);
5857
5858 /* Apply any specified qualifiers to the parameter declaration. Note that
5859 * for function parameters the default mode is 'in'.
5860 */
5861 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc,
5862 true);
5863
5864 if (((1u << var->data.mode) & state->zero_init) &&
5865 (var->type->is_numeric() || var->type->is_boolean())) {
5866 const ir_constant_data data = { { 0 } };
5867 var->data.has_initializer = true;
5868 var->data.is_implicit_initializer = true;
5869 var->constant_initializer = new(var) ir_constant(var->type, &data);
5870 }
5871
5872 /* From section 4.1.7 of the GLSL 4.40 spec:
5873 *
5874 * "Opaque variables cannot be treated as l-values; hence cannot
5875 * be used as out or inout function parameters, nor can they be
5876 * assigned into."
5877 *
5878 * From section 4.1.7 of the ARB_bindless_texture spec:
5879 *
5880 * "Samplers can be used as l-values, so can be assigned into and used
5881 * as "out" and "inout" function parameters."
5882 *
5883 * From section 4.1.X of the ARB_bindless_texture spec:
5884 *
5885 * "Images can be used as l-values, so can be assigned into and used as
5886 * "out" and "inout" function parameters."
5887 */
5888 if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out)
5889 && (type->contains_atomic() ||
5890 (!state->has_bindless() && type->contains_opaque()))) {
5891 _mesa_glsl_error(&loc, state, "out and inout parameters cannot "
5892 "contain %s variables",
5893 state->has_bindless() ? "atomic" : "opaque");
5894 type = glsl_type::error_type;
5895 }
5896
5897 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
5898 *
5899 * "When calling a function, expressions that do not evaluate to
5900 * l-values cannot be passed to parameters declared as out or inout."
5901 *
5902 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
5903 *
5904 * "Other binary or unary expressions, non-dereferenced arrays,
5905 * function names, swizzles with repeated fields, and constants
5906 * cannot be l-values."
5907 *
5908 * So for GLSL 1.10, passing an array as an out or inout parameter is not
5909 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
5910 */
5911 if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out)
5912 && type->is_array()
5913 && !state->check_version(120, 100, &loc,
5914 "arrays cannot be out or inout parameters")) {
5915 type = glsl_type::error_type;
5916 }
5917
5918 instructions->push_tail(var);
5919
5920 /* Parameter declarations do not have r-values.
5921 */
5922 return NULL;
5923 }
5924
5925
5926 void
5927 ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
5928 bool formal,
5929 exec_list *ir_parameters,
5930 _mesa_glsl_parse_state *state)
5931 {
5932 ast_parameter_declarator *void_param = NULL;
5933 unsigned count = 0;
5934
5935 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
5936 param->formal_parameter = formal;
5937 param->hir(ir_parameters, state);
5938
5939 if (param->is_void)
5940 void_param = param;
5941
5942 count++;
5943 }
5944
5945 if ((void_param != NULL) && (count > 1)) {
5946 YYLTYPE loc = void_param->get_location();
5947
5948 _mesa_glsl_error(& loc, state,
5949 "`void' parameter must be only parameter");
5950 }
5951 }
5952
5953
5954 void
5955 emit_function(_mesa_glsl_parse_state *state, ir_function *f)
5956 {
5957 /* IR invariants disallow function declarations or definitions
5958 * nested within other function definitions. But there is no
5959 * requirement about the relative order of function declarations
5960 * and definitions with respect to one another. So simply insert
5961 * the new ir_function block at the end of the toplevel instruction
5962 * list.
5963 */
5964 state->toplevel_ir->push_tail(f);
5965 }
5966
5967
5968 ir_rvalue *
5969 ast_function::hir(exec_list *instructions,
5970 struct _mesa_glsl_parse_state *state)
5971 {
5972 void *ctx = state;
5973 ir_function *f = NULL;
5974 ir_function_signature *sig = NULL;
5975 exec_list hir_parameters;
5976 YYLTYPE loc = this->get_location();
5977
5978 const char *const name = identifier;
5979
5980 /* New functions are always added to the top-level IR instruction stream,
5981 * so this instruction list pointer is ignored. See also emit_function
5982 * (called below).
5983 */
5984 (void) instructions;
5985
5986 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
5987 *
5988 * "Function declarations (prototypes) cannot occur inside of functions;
5989 * they must be at global scope, or for the built-in functions, outside
5990 * the global scope."
5991 *
5992 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
5993 *
5994 * "User defined functions may only be defined within the global scope."
5995 *
5996 * Note that this language does not appear in GLSL 1.10.
5997 */
5998 if ((state->current_function != NULL) &&
5999 state->is_version(120, 100)) {
6000 YYLTYPE loc = this->get_location();
6001 _mesa_glsl_error(&loc, state,
6002 "declaration of function `%s' not allowed within "
6003 "function body", name);
6004 }
6005
6006 validate_identifier(name, this->get_location(), state);
6007
6008 /* Convert the list of function parameters to HIR now so that they can be
6009 * used below to compare this function's signature with previously seen
6010 * signatures for functions with the same name.
6011 */
6012 ast_parameter_declarator::parameters_to_hir(& this->parameters,
6013 is_definition,
6014 & hir_parameters, state);
6015
6016 const char *return_type_name;
6017 const glsl_type *return_type =
6018 this->return_type->glsl_type(& return_type_name, state);
6019
6020 if (!return_type) {
6021 YYLTYPE loc = this->get_location();
6022 _mesa_glsl_error(&loc, state,
6023 "function `%s' has undeclared return type `%s'",
6024 name, return_type_name);
6025 return_type = glsl_type::error_type;
6026 }
6027
6028 /* ARB_shader_subroutine states:
6029 * "Subroutine declarations cannot be prototyped. It is an error to prepend
6030 * subroutine(...) to a function declaration."
6031 */
6032 if (this->return_type->qualifier.subroutine_list && !is_definition) {
6033 YYLTYPE loc = this->get_location();
6034 _mesa_glsl_error(&loc, state,
6035 "function declaration `%s' cannot have subroutine prepended",
6036 name);
6037 }
6038
6039 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
6040 * "No qualifier is allowed on the return type of a function."
6041 */
6042 if (this->return_type->has_qualifiers(state)) {
6043 YYLTYPE loc = this->get_location();
6044 _mesa_glsl_error(& loc, state,
6045 "function `%s' return type has qualifiers", name);
6046 }
6047
6048 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
6049 *
6050 * "Arrays are allowed as arguments and as the return type. In both
6051 * cases, the array must be explicitly sized."
6052 */
6053 if (return_type->is_unsized_array()) {
6054 YYLTYPE loc = this->get_location();
6055 _mesa_glsl_error(& loc, state,
6056 "function `%s' return type array must be explicitly "
6057 "sized", name);
6058 }
6059
6060 /* From Section 6.1 (Function Definitions) of the GLSL 1.00 spec:
6061 *
6062 * "Arrays are allowed as arguments, but not as the return type. [...]
6063 * The return type can also be a structure if the structure does not
6064 * contain an array."
6065 */
6066 if (state->language_version == 100 && return_type->contains_array()) {
6067 YYLTYPE loc = this->get_location();
6068 _mesa_glsl_error(& loc, state,
6069 "function `%s' return type contains an array", name);
6070 }
6071
6072 /* From section 4.1.7 of the GLSL 4.40 spec:
6073 *
6074 * "[Opaque types] can only be declared as function parameters
6075 * or uniform-qualified variables."
6076 *
6077 * The ARB_bindless_texture spec doesn't clearly state this, but as it says
6078 * "Replace Section 4.1.7 (Samplers), p. 25" and, "Replace Section 4.1.X,
6079 * (Images)", this should be allowed.
6080 */
6081 if (return_type->contains_atomic() ||
6082 (!state->has_bindless() && return_type->contains_opaque())) {
6083 YYLTYPE loc = this->get_location();
6084 _mesa_glsl_error(&loc, state,
6085 "function `%s' return type can't contain an %s type",
6086 name, state->has_bindless() ? "atomic" : "opaque");
6087 }
6088
6089 /**/
6090 if (return_type->is_subroutine()) {
6091 YYLTYPE loc = this->get_location();
6092 _mesa_glsl_error(&loc, state,
6093 "function `%s' return type can't be a subroutine type",
6094 name);
6095 }
6096
6097 /* Get the precision for the return type */
6098 unsigned return_precision;
6099
6100 if (state->es_shader) {
6101 YYLTYPE loc = this->get_location();
6102 return_precision =
6103 select_gles_precision(this->return_type->qualifier.precision,
6104 return_type,
6105 state,
6106 &loc);
6107 } else {
6108 return_precision = GLSL_PRECISION_NONE;
6109 }
6110
6111 /* Create an ir_function if one doesn't already exist. */
6112 f = state->symbols->get_function(name);
6113 if (f == NULL) {
6114 f = new(ctx) ir_function(name);
6115 if (!this->return_type->qualifier.is_subroutine_decl()) {
6116 if (!state->symbols->add_function(f)) {
6117 /* This function name shadows a non-function use of the same name. */
6118 YYLTYPE loc = this->get_location();
6119 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
6120 "non-function", name);
6121 return NULL;
6122 }
6123 }
6124 emit_function(state, f);
6125 }
6126
6127 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
6128 *
6129 * "A shader cannot redefine or overload built-in functions."
6130 *
6131 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
6132 *
6133 * "User code can overload the built-in functions but cannot redefine
6134 * them."
6135 */
6136 if (state->es_shader) {
6137 /* Local shader has no exact candidates; check the built-ins. */
6138 if (state->language_version >= 300 &&
6139 _mesa_glsl_has_builtin_function(state, name)) {
6140 YYLTYPE loc = this->get_location();
6141 _mesa_glsl_error(& loc, state,
6142 "A shader cannot redefine or overload built-in "
6143 "function `%s' in GLSL ES 3.00", name);
6144 return NULL;
6145 }
6146
6147 if (state->language_version == 100) {
6148 ir_function_signature *sig =
6149 _mesa_glsl_find_builtin_function(state, name, &hir_parameters);
6150 if (sig && sig->is_builtin()) {
6151 _mesa_glsl_error(& loc, state,
6152 "A shader cannot redefine built-in "
6153 "function `%s' in GLSL ES 1.00", name);
6154 }
6155 }
6156 }
6157
6158 /* Verify that this function's signature either doesn't match a previously
6159 * seen signature for a function with the same name, or, if a match is found,
6160 * that the previously seen signature does not have an associated definition.
6161 */
6162 if (state->es_shader || f->has_user_signature()) {
6163 sig = f->exact_matching_signature(state, &hir_parameters);
6164 if (sig != NULL) {
6165 const char *badvar = sig->qualifiers_match(&hir_parameters);
6166 if (badvar != NULL) {
6167 YYLTYPE loc = this->get_location();
6168
6169 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
6170 "qualifiers don't match prototype", name, badvar);
6171 }
6172
6173 if (sig->return_type != return_type) {
6174 YYLTYPE loc = this->get_location();
6175
6176 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
6177 "match prototype", name);
6178 }
6179
6180 if (sig->return_precision != return_precision) {
6181 YYLTYPE loc = this->get_location();
6182
6183 _mesa_glsl_error(&loc, state, "function `%s' return type precision "
6184 "doesn't match prototype", name);
6185 }
6186
6187 if (sig->is_defined) {
6188 if (is_definition) {
6189 YYLTYPE loc = this->get_location();
6190 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
6191 } else {
6192 /* We just encountered a prototype that exactly matches a
6193 * function that's already been defined. This is redundant,
6194 * and we should ignore it.
6195 */
6196 return NULL;
6197 }
6198 } else if (state->language_version == 100 && !is_definition) {
6199 /* From the GLSL 1.00 spec, section 4.2.7:
6200 *
6201 * "A particular variable, structure or function declaration
6202 * may occur at most once within a scope with the exception
6203 * that a single function prototype plus the corresponding
6204 * function definition are allowed."
6205 */
6206 YYLTYPE loc = this->get_location();
6207 _mesa_glsl_error(&loc, state, "function `%s' redeclared", name);
6208 }
6209 }
6210 }
6211
6212 /* Verify the return type of main() */
6213 if (strcmp(name, "main") == 0) {
6214 if (! return_type->is_void()) {
6215 YYLTYPE loc = this->get_location();
6216
6217 _mesa_glsl_error(& loc, state, "main() must return void");
6218 }
6219
6220 if (!hir_parameters.is_empty()) {
6221 YYLTYPE loc = this->get_location();
6222
6223 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
6224 }
6225 }
6226
6227 /* Finish storing the information about this new function in its signature.
6228 */
6229 if (sig == NULL) {
6230 sig = new(ctx) ir_function_signature(return_type);
6231 sig->return_precision = return_precision;
6232 f->add_signature(sig);
6233 }
6234
6235 sig->replace_parameters(&hir_parameters);
6236 signature = sig;
6237
6238 if (this->return_type->qualifier.subroutine_list) {
6239 int idx;
6240
6241 if (this->return_type->qualifier.flags.q.explicit_index) {
6242 unsigned qual_index;
6243 if (process_qualifier_constant(state, &loc, "index",
6244 this->return_type->qualifier.index,
6245 &qual_index)) {
6246 if (!state->has_explicit_uniform_location()) {
6247 _mesa_glsl_error(&loc, state, "subroutine index requires "
6248 "GL_ARB_explicit_uniform_location or "
6249 "GLSL 4.30");
6250 } else if (qual_index >= MAX_SUBROUTINES) {
6251 _mesa_glsl_error(&loc, state,
6252 "invalid subroutine index (%d) index must "
6253 "be a number between 0 and "
6254 "GL_MAX_SUBROUTINES - 1 (%d)", qual_index,
6255 MAX_SUBROUTINES - 1);
6256 } else {
6257 f->subroutine_index = qual_index;
6258 }
6259 }
6260 }
6261
6262 f->num_subroutine_types = this->return_type->qualifier.subroutine_list->declarations.length();
6263 f->subroutine_types = ralloc_array(state, const struct glsl_type *,
6264 f->num_subroutine_types);
6265 idx = 0;
6266 foreach_list_typed(ast_declaration, decl, link, &this->return_type->qualifier.subroutine_list->declarations) {
6267 const struct glsl_type *type;
6268 /* the subroutine type must be already declared */
6269 type = state->symbols->get_type(decl->identifier);
6270 if (!type) {
6271 _mesa_glsl_error(& loc, state, "unknown type '%s' in subroutine function definition", decl->identifier);
6272 }
6273
6274 for (int i = 0; i < state->num_subroutine_types; i++) {
6275 ir_function *fn = state->subroutine_types[i];
6276 ir_function_signature *tsig = NULL;
6277
6278 if (strcmp(fn->name, decl->identifier))
6279 continue;
6280
6281 tsig = fn->matching_signature(state, &sig->parameters,
6282 false);
6283 if (!tsig) {
6284 _mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - signatures do not match\n", decl->identifier);
6285 } else {
6286 if (tsig->return_type != sig->return_type) {
6287 _mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - return types do not match\n", decl->identifier);
6288 }
6289 }
6290 }
6291 f->subroutine_types[idx++] = type;
6292 }
6293 state->subroutines = (ir_function **)reralloc(state, state->subroutines,
6294 ir_function *,
6295 state->num_subroutines + 1);
6296 state->subroutines[state->num_subroutines] = f;
6297 state->num_subroutines++;
6298
6299 }
6300
6301 if (this->return_type->qualifier.is_subroutine_decl()) {
6302 if (!state->symbols->add_type(this->identifier, glsl_type::get_subroutine_instance(this->identifier))) {
6303 _mesa_glsl_error(& loc, state, "type '%s' previously defined", this->identifier);
6304 return NULL;
6305 }
6306 state->subroutine_types = (ir_function **)reralloc(state, state->subroutine_types,
6307 ir_function *,
6308 state->num_subroutine_types + 1);
6309 state->subroutine_types[state->num_subroutine_types] = f;
6310 state->num_subroutine_types++;
6311
6312 f->is_subroutine = true;
6313 }
6314
6315 /* Function declarations (prototypes) do not have r-values.
6316 */
6317 return NULL;
6318 }
6319
6320
6321 ir_rvalue *
6322 ast_function_definition::hir(exec_list *instructions,
6323 struct _mesa_glsl_parse_state *state)
6324 {
6325 prototype->is_definition = true;
6326 prototype->hir(instructions, state);
6327
6328 ir_function_signature *signature = prototype->signature;
6329 if (signature == NULL)
6330 return NULL;
6331
6332 assert(state->current_function == NULL);
6333 state->current_function = signature;
6334 state->found_return = false;
6335 state->found_begin_interlock = false;
6336 state->found_end_interlock = false;
6337
6338 /* Duplicate parameters declared in the prototype as concrete variables.
6339 * Add these to the symbol table.
6340 */
6341 state->symbols->push_scope();
6342 foreach_in_list(ir_variable, var, &signature->parameters) {
6343 assert(var->as_variable() != NULL);
6344
6345 /* The only way a parameter would "exist" is if two parameters have
6346 * the same name.
6347 */
6348 if (state->symbols->name_declared_this_scope(var->name)) {
6349 YYLTYPE loc = this->get_location();
6350
6351 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
6352 } else {
6353 state->symbols->add_variable(var);
6354 }
6355 }
6356
6357 /* Convert the body of the function to HIR. */
6358 this->body->hir(&signature->body, state);
6359 signature->is_defined = true;
6360
6361 state->symbols->pop_scope();
6362
6363 assert(state->current_function == signature);
6364 state->current_function = NULL;
6365
6366 if (!signature->return_type->is_void() && !state->found_return) {
6367 YYLTYPE loc = this->get_location();
6368 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
6369 "%s, but no return statement",
6370 signature->function_name(),
6371 signature->return_type->name);
6372 }
6373
6374 /* Function definitions do not have r-values.
6375 */
6376 return NULL;
6377 }
6378
6379
6380 ir_rvalue *
6381 ast_jump_statement::hir(exec_list *instructions,
6382 struct _mesa_glsl_parse_state *state)
6383 {
6384 void *ctx = state;
6385
6386 switch (mode) {
6387 case ast_return: {
6388 ir_return *inst;
6389 assert(state->current_function);
6390
6391 if (opt_return_value) {
6392 ir_rvalue *ret = opt_return_value->hir(instructions, state);
6393
6394 /* The value of the return type can be NULL if the shader says
6395 * 'return foo();' and foo() is a function that returns void.
6396 *
6397 * NOTE: The GLSL spec doesn't say that this is an error. The type
6398 * of the return value is void. If the return type of the function is
6399 * also void, then this should compile without error. Seriously.
6400 */
6401 const glsl_type *const ret_type =
6402 (ret == NULL) ? glsl_type::void_type : ret->type;
6403
6404 /* Implicit conversions are not allowed for return values prior to
6405 * ARB_shading_language_420pack.
6406 */
6407 if (state->current_function->return_type != ret_type) {
6408 YYLTYPE loc = this->get_location();
6409
6410 if (state->has_420pack()) {
6411 if (!apply_implicit_conversion(state->current_function->return_type,
6412 ret, state)
6413 || (ret->type != state->current_function->return_type)) {
6414 _mesa_glsl_error(& loc, state,
6415 "could not implicitly convert return value "
6416 "to %s, in function `%s'",
6417 state->current_function->return_type->name,
6418 state->current_function->function_name());
6419 }
6420 } else {
6421 _mesa_glsl_error(& loc, state,
6422 "`return' with wrong type %s, in function `%s' "
6423 "returning %s",
6424 ret_type->name,
6425 state->current_function->function_name(),
6426 state->current_function->return_type->name);
6427 }
6428 } else if (state->current_function->return_type->base_type ==
6429 GLSL_TYPE_VOID) {
6430 YYLTYPE loc = this->get_location();
6431
6432 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
6433 * specs add a clarification:
6434 *
6435 * "A void function can only use return without a return argument, even if
6436 * the return argument has void type. Return statements only accept values:
6437 *
6438 * void func1() { }
6439 * void func2() { return func1(); } // illegal return statement"
6440 */
6441 _mesa_glsl_error(& loc, state,
6442 "void functions can only use `return' without a "
6443 "return argument");
6444 }
6445
6446 inst = new(ctx) ir_return(ret);
6447 } else {
6448 if (state->current_function->return_type->base_type !=
6449 GLSL_TYPE_VOID) {
6450 YYLTYPE loc = this->get_location();
6451
6452 _mesa_glsl_error(& loc, state,
6453 "`return' with no value, in function %s returning "
6454 "non-void",
6455 state->current_function->function_name());
6456 }
6457 inst = new(ctx) ir_return;
6458 }
6459
6460 state->found_return = true;
6461 instructions->push_tail(inst);
6462 break;
6463 }
6464
6465 case ast_discard:
6466 if (state->stage != MESA_SHADER_FRAGMENT) {
6467 YYLTYPE loc = this->get_location();
6468
6469 _mesa_glsl_error(& loc, state,
6470 "`discard' may only appear in a fragment shader");
6471 }
6472 instructions->push_tail(new(ctx) ir_discard);
6473 break;
6474
6475 case ast_break:
6476 case ast_continue:
6477 if (mode == ast_continue &&
6478 state->loop_nesting_ast == NULL) {
6479 YYLTYPE loc = this->get_location();
6480
6481 _mesa_glsl_error(& loc, state, "continue may only appear in a loop");
6482 } else if (mode == ast_break &&
6483 state->loop_nesting_ast == NULL &&
6484 state->switch_state.switch_nesting_ast == NULL) {
6485 YYLTYPE loc = this->get_location();
6486
6487 _mesa_glsl_error(& loc, state,
6488 "break may only appear in a loop or a switch");
6489 } else {
6490 /* For a loop, inline the for loop expression again, since we don't
6491 * know where near the end of the loop body the normal copy of it is
6492 * going to be placed. Same goes for the condition for a do-while
6493 * loop.
6494 */
6495 if (state->loop_nesting_ast != NULL &&
6496 mode == ast_continue && !state->switch_state.is_switch_innermost) {
6497 if (state->loop_nesting_ast->rest_expression) {
6498 state->loop_nesting_ast->rest_expression->hir(instructions,
6499 state);
6500 }
6501 if (state->loop_nesting_ast->mode ==
6502 ast_iteration_statement::ast_do_while) {
6503 state->loop_nesting_ast->condition_to_hir(instructions, state);
6504 }
6505 }
6506
6507 if (state->switch_state.is_switch_innermost &&
6508 mode == ast_continue) {
6509 /* Set 'continue_inside' to true. */
6510 ir_rvalue *const true_val = new (ctx) ir_constant(true);
6511 ir_dereference_variable *deref_continue_inside_var =
6512 new(ctx) ir_dereference_variable(state->switch_state.continue_inside);
6513 instructions->push_tail(new(ctx) ir_assignment(deref_continue_inside_var,
6514 true_val));
6515
6516 /* Break out from the switch, continue for the loop will
6517 * be called right after switch. */
6518 ir_loop_jump *const jump =
6519 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
6520 instructions->push_tail(jump);
6521
6522 } else if (state->switch_state.is_switch_innermost &&
6523 mode == ast_break) {
6524 /* Force break out of switch by inserting a break. */
6525 ir_loop_jump *const jump =
6526 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
6527 instructions->push_tail(jump);
6528 } else {
6529 ir_loop_jump *const jump =
6530 new(ctx) ir_loop_jump((mode == ast_break)
6531 ? ir_loop_jump::jump_break
6532 : ir_loop_jump::jump_continue);
6533 instructions->push_tail(jump);
6534 }
6535 }
6536
6537 break;
6538 }
6539
6540 /* Jump instructions do not have r-values.
6541 */
6542 return NULL;
6543 }
6544
6545
6546 ir_rvalue *
6547 ast_demote_statement::hir(exec_list *instructions,
6548 struct _mesa_glsl_parse_state *state)
6549 {
6550 void *ctx = state;
6551
6552 if (state->stage != MESA_SHADER_FRAGMENT) {
6553 YYLTYPE loc = this->get_location();
6554
6555 _mesa_glsl_error(& loc, state,
6556 "`demote' may only appear in a fragment shader");
6557 }
6558
6559 instructions->push_tail(new(ctx) ir_demote);
6560
6561 return NULL;
6562 }
6563
6564
6565 ir_rvalue *
6566 ast_selection_statement::hir(exec_list *instructions,
6567 struct _mesa_glsl_parse_state *state)
6568 {
6569 void *ctx = state;
6570
6571 ir_rvalue *const condition = this->condition->hir(instructions, state);
6572
6573 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
6574 *
6575 * "Any expression whose type evaluates to a Boolean can be used as the
6576 * conditional expression bool-expression. Vector types are not accepted
6577 * as the expression to if."
6578 *
6579 * The checks are separated so that higher quality diagnostics can be
6580 * generated for cases where both rules are violated.
6581 */
6582 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
6583 YYLTYPE loc = this->condition->get_location();
6584
6585 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
6586 "boolean");
6587 }
6588
6589 ir_if *const stmt = new(ctx) ir_if(condition);
6590
6591 if (then_statement != NULL) {
6592 state->symbols->push_scope();
6593 then_statement->hir(& stmt->then_instructions, state);
6594 state->symbols->pop_scope();
6595 }
6596
6597 if (else_statement != NULL) {
6598 state->symbols->push_scope();
6599 else_statement->hir(& stmt->else_instructions, state);
6600 state->symbols->pop_scope();
6601 }
6602
6603 instructions->push_tail(stmt);
6604
6605 /* if-statements do not have r-values.
6606 */
6607 return NULL;
6608 }
6609
6610
6611 struct case_label {
6612 /** Value of the case label. */
6613 unsigned value;
6614
6615 /** Does this label occur after the default? */
6616 bool after_default;
6617
6618 /**
6619 * AST for the case label.
6620 *
6621 * This is only used to generate error messages for duplicate labels.
6622 */
6623 ast_expression *ast;
6624 };
6625
6626 /* Used for detection of duplicate case values, compare
6627 * given contents directly.
6628 */
6629 static bool
6630 compare_case_value(const void *a, const void *b)
6631 {
6632 return ((struct case_label *) a)->value == ((struct case_label *) b)->value;
6633 }
6634
6635
6636 /* Used for detection of duplicate case values, just
6637 * returns key contents as is.
6638 */
6639 static unsigned
6640 key_contents(const void *key)
6641 {
6642 return ((struct case_label *) key)->value;
6643 }
6644
6645
6646 ir_rvalue *
6647 ast_switch_statement::hir(exec_list *instructions,
6648 struct _mesa_glsl_parse_state *state)
6649 {
6650 void *ctx = state;
6651
6652 ir_rvalue *const test_expression =
6653 this->test_expression->hir(instructions, state);
6654
6655 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
6656 *
6657 * "The type of init-expression in a switch statement must be a
6658 * scalar integer."
6659 */
6660 if (!test_expression->type->is_scalar() ||
6661 !test_expression->type->is_integer_32()) {
6662 YYLTYPE loc = this->test_expression->get_location();
6663
6664 _mesa_glsl_error(& loc,
6665 state,
6666 "switch-statement expression must be scalar "
6667 "integer");
6668 return NULL;
6669 }
6670
6671 /* Track the switch-statement nesting in a stack-like manner.
6672 */
6673 struct glsl_switch_state saved = state->switch_state;
6674
6675 state->switch_state.is_switch_innermost = true;
6676 state->switch_state.switch_nesting_ast = this;
6677 state->switch_state.labels_ht =
6678 _mesa_hash_table_create(NULL, key_contents,
6679 compare_case_value);
6680 state->switch_state.previous_default = NULL;
6681
6682 /* Initalize is_fallthru state to false.
6683 */
6684 ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
6685 state->switch_state.is_fallthru_var =
6686 new(ctx) ir_variable(glsl_type::bool_type,
6687 "switch_is_fallthru_tmp",
6688 ir_var_temporary);
6689 instructions->push_tail(state->switch_state.is_fallthru_var);
6690
6691 ir_dereference_variable *deref_is_fallthru_var =
6692 new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
6693 instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
6694 is_fallthru_val));
6695
6696 /* Initialize continue_inside state to false.
6697 */
6698 state->switch_state.continue_inside =
6699 new(ctx) ir_variable(glsl_type::bool_type,
6700 "continue_inside_tmp",
6701 ir_var_temporary);
6702 instructions->push_tail(state->switch_state.continue_inside);
6703
6704 ir_rvalue *const false_val = new (ctx) ir_constant(false);
6705 ir_dereference_variable *deref_continue_inside_var =
6706 new(ctx) ir_dereference_variable(state->switch_state.continue_inside);
6707 instructions->push_tail(new(ctx) ir_assignment(deref_continue_inside_var,
6708 false_val));
6709
6710 state->switch_state.run_default =
6711 new(ctx) ir_variable(glsl_type::bool_type,
6712 "run_default_tmp",
6713 ir_var_temporary);
6714 instructions->push_tail(state->switch_state.run_default);
6715
6716 /* Loop around the switch is used for flow control. */
6717 ir_loop * loop = new(ctx) ir_loop();
6718 instructions->push_tail(loop);
6719
6720 /* Cache test expression.
6721 */
6722 test_to_hir(&loop->body_instructions, state);
6723
6724 /* Emit code for body of switch stmt.
6725 */
6726 body->hir(&loop->body_instructions, state);
6727
6728 /* Insert a break at the end to exit loop. */
6729 ir_loop_jump *jump = new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
6730 loop->body_instructions.push_tail(jump);
6731
6732 /* If we are inside loop, check if continue got called inside switch. */
6733 if (state->loop_nesting_ast != NULL) {
6734 ir_dereference_variable *deref_continue_inside =
6735 new(ctx) ir_dereference_variable(state->switch_state.continue_inside);
6736 ir_if *irif = new(ctx) ir_if(deref_continue_inside);
6737 ir_loop_jump *jump = new(ctx) ir_loop_jump(ir_loop_jump::jump_continue);
6738
6739 if (state->loop_nesting_ast != NULL) {
6740 if (state->loop_nesting_ast->rest_expression) {
6741 state->loop_nesting_ast->rest_expression->hir(&irif->then_instructions,
6742 state);
6743 }
6744 if (state->loop_nesting_ast->mode ==
6745 ast_iteration_statement::ast_do_while) {
6746 state->loop_nesting_ast->condition_to_hir(&irif->then_instructions, state);
6747 }
6748 }
6749 irif->then_instructions.push_tail(jump);
6750 instructions->push_tail(irif);
6751 }
6752
6753 _mesa_hash_table_destroy(state->switch_state.labels_ht, NULL);
6754
6755 state->switch_state = saved;
6756
6757 /* Switch statements do not have r-values. */
6758 return NULL;
6759 }
6760
6761
6762 void
6763 ast_switch_statement::test_to_hir(exec_list *instructions,
6764 struct _mesa_glsl_parse_state *state)
6765 {
6766 void *ctx = state;
6767
6768 /* set to true to avoid a duplicate "use of uninitialized variable" warning
6769 * on the switch test case. The first one would be already raised when
6770 * getting the test_expression at ast_switch_statement::hir
6771 */
6772 test_expression->set_is_lhs(true);
6773 /* Cache value of test expression. */
6774 ir_rvalue *const test_val = test_expression->hir(instructions, state);
6775
6776 state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
6777 "switch_test_tmp",
6778 ir_var_temporary);
6779 ir_dereference_variable *deref_test_var =
6780 new(ctx) ir_dereference_variable(state->switch_state.test_var);
6781
6782 instructions->push_tail(state->switch_state.test_var);
6783 instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val));
6784 }
6785
6786
6787 ir_rvalue *
6788 ast_switch_body::hir(exec_list *instructions,
6789 struct _mesa_glsl_parse_state *state)
6790 {
6791 if (stmts != NULL)
6792 stmts->hir(instructions, state);
6793
6794 /* Switch bodies do not have r-values. */
6795 return NULL;
6796 }
6797
6798 ir_rvalue *
6799 ast_case_statement_list::hir(exec_list *instructions,
6800 struct _mesa_glsl_parse_state *state)
6801 {
6802 exec_list default_case, after_default, tmp;
6803
6804 foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases) {
6805 case_stmt->hir(&tmp, state);
6806
6807 /* Default case. */
6808 if (state->switch_state.previous_default && default_case.is_empty()) {
6809 default_case.append_list(&tmp);
6810 continue;
6811 }
6812
6813 /* If default case found, append 'after_default' list. */
6814 if (!default_case.is_empty())
6815 after_default.append_list(&tmp);
6816 else
6817 instructions->append_list(&tmp);
6818 }
6819
6820 /* Handle the default case. This is done here because default might not be
6821 * the last case. We need to add checks against following cases first to see
6822 * if default should be chosen or not.
6823 */
6824 if (!default_case.is_empty()) {
6825 ir_factory body(instructions, state);
6826
6827 ir_expression *cmp = NULL;
6828
6829 hash_table_foreach(state->switch_state.labels_ht, entry) {
6830 const struct case_label *const l = (struct case_label *) entry->data;
6831
6832 /* If the switch init-value is the value of one of the labels that
6833 * occurs after the default case, disable execution of the default
6834 * case.
6835 */
6836 if (l->after_default) {
6837 ir_constant *const cnst =
6838 state->switch_state.test_var->type->base_type == GLSL_TYPE_UINT
6839 ? body.constant(unsigned(l->value))
6840 : body.constant(int(l->value));
6841
6842 cmp = cmp == NULL
6843 ? equal(cnst, state->switch_state.test_var)
6844 : logic_or(cmp, equal(cnst, state->switch_state.test_var));
6845 }
6846 }
6847
6848 if (cmp != NULL)
6849 body.emit(assign(state->switch_state.run_default, logic_not(cmp)));
6850 else
6851 body.emit(assign(state->switch_state.run_default, body.constant(true)));
6852
6853 /* Append default case and all cases after it. */
6854 instructions->append_list(&default_case);
6855 instructions->append_list(&after_default);
6856 }
6857
6858 /* Case statements do not have r-values. */
6859 return NULL;
6860 }
6861
6862 ir_rvalue *
6863 ast_case_statement::hir(exec_list *instructions,
6864 struct _mesa_glsl_parse_state *state)
6865 {
6866 labels->hir(instructions, state);
6867
6868 /* Guard case statements depending on fallthru state. */
6869 ir_dereference_variable *const deref_fallthru_guard =
6870 new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
6871 ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
6872
6873 foreach_list_typed (ast_node, stmt, link, & this->stmts)
6874 stmt->hir(& test_fallthru->then_instructions, state);
6875
6876 instructions->push_tail(test_fallthru);
6877
6878 /* Case statements do not have r-values. */
6879 return NULL;
6880 }
6881
6882
6883 ir_rvalue *
6884 ast_case_label_list::hir(exec_list *instructions,
6885 struct _mesa_glsl_parse_state *state)
6886 {
6887 foreach_list_typed (ast_case_label, label, link, & this->labels)
6888 label->hir(instructions, state);
6889
6890 /* Case labels do not have r-values. */
6891 return NULL;
6892 }
6893
6894 ir_rvalue *
6895 ast_case_label::hir(exec_list *instructions,
6896 struct _mesa_glsl_parse_state *state)
6897 {
6898 ir_factory body(instructions, state);
6899
6900 ir_variable *const fallthru_var = state->switch_state.is_fallthru_var;
6901
6902 /* If not default case, ... */
6903 if (this->test_value != NULL) {
6904 /* Conditionally set fallthru state based on
6905 * comparison of cached test expression value to case label.
6906 */
6907 ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
6908 ir_constant *label_const =
6909 label_rval->constant_expression_value(body.mem_ctx);
6910
6911 if (!label_const) {
6912 YYLTYPE loc = this->test_value->get_location();
6913
6914 _mesa_glsl_error(& loc, state,
6915 "switch statement case label must be a "
6916 "constant expression");
6917
6918 /* Stuff a dummy value in to allow processing to continue. */
6919 label_const = body.constant(0);
6920 } else {
6921 hash_entry *entry =
6922 _mesa_hash_table_search(state->switch_state.labels_ht,
6923 &label_const->value.u[0]);
6924
6925 if (entry) {
6926 const struct case_label *const l =
6927 (struct case_label *) entry->data;
6928 const ast_expression *const previous_label = l->ast;
6929 YYLTYPE loc = this->test_value->get_location();
6930
6931 _mesa_glsl_error(& loc, state, "duplicate case value");
6932
6933 loc = previous_label->get_location();
6934 _mesa_glsl_error(& loc, state, "this is the previous case label");
6935 } else {
6936 struct case_label *l = ralloc(state->switch_state.labels_ht,
6937 struct case_label);
6938
6939 l->value = label_const->value.u[0];
6940 l->after_default = state->switch_state.previous_default != NULL;
6941 l->ast = this->test_value;
6942
6943 _mesa_hash_table_insert(state->switch_state.labels_ht,
6944 &label_const->value.u[0],
6945 l);
6946 }
6947 }
6948
6949 /* Create an r-value version of the ir_constant label here (after we may
6950 * have created a fake one in error cases) that can be passed to
6951 * apply_implicit_conversion below.
6952 */
6953 ir_rvalue *label = label_const;
6954
6955 ir_rvalue *deref_test_var =
6956 new(body.mem_ctx) ir_dereference_variable(state->switch_state.test_var);
6957
6958 /*
6959 * From GLSL 4.40 specification section 6.2 ("Selection"):
6960 *
6961 * "The type of the init-expression value in a switch statement must
6962 * be a scalar int or uint. The type of the constant-expression value
6963 * in a case label also must be a scalar int or uint. When any pair
6964 * of these values is tested for "equal value" and the types do not
6965 * match, an implicit conversion will be done to convert the int to a
6966 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
6967 * is done."
6968 */
6969 if (label->type != state->switch_state.test_var->type) {
6970 YYLTYPE loc = this->test_value->get_location();
6971
6972 const glsl_type *type_a = label->type;
6973 const glsl_type *type_b = state->switch_state.test_var->type;
6974
6975 /* Check if int->uint implicit conversion is supported. */
6976 bool integer_conversion_supported =
6977 glsl_type::int_type->can_implicitly_convert_to(glsl_type::uint_type,
6978 state);
6979
6980 if ((!type_a->is_integer_32() || !type_b->is_integer_32()) ||
6981 !integer_conversion_supported) {
6982 _mesa_glsl_error(&loc, state, "type mismatch with switch "
6983 "init-expression and case label (%s != %s)",
6984 type_a->name, type_b->name);
6985 } else {
6986 /* Conversion of the case label. */
6987 if (type_a->base_type == GLSL_TYPE_INT) {
6988 if (!apply_implicit_conversion(glsl_type::uint_type,
6989 label, state))
6990 _mesa_glsl_error(&loc, state, "implicit type conversion error");
6991 } else {
6992 /* Conversion of the init-expression value. */
6993 if (!apply_implicit_conversion(glsl_type::uint_type,
6994 deref_test_var, state))
6995 _mesa_glsl_error(&loc, state, "implicit type conversion error");
6996 }
6997 }
6998
6999 /* If the implicit conversion was allowed, the types will already be
7000 * the same. If the implicit conversion wasn't allowed, smash the
7001 * type of the label anyway. This will prevent the expression
7002 * constructor (below) from failing an assertion.
7003 */
7004 label->type = deref_test_var->type;
7005 }
7006
7007 body.emit(assign(fallthru_var,
7008 logic_or(fallthru_var, equal(label, deref_test_var))));
7009 } else { /* default case */
7010 if (state->switch_state.previous_default) {
7011 YYLTYPE loc = this->get_location();
7012 _mesa_glsl_error(& loc, state,
7013 "multiple default labels in one switch");
7014
7015 loc = state->switch_state.previous_default->get_location();
7016 _mesa_glsl_error(& loc, state, "this is the first default label");
7017 }
7018 state->switch_state.previous_default = this;
7019
7020 /* Set fallthru condition on 'run_default' bool. */
7021 body.emit(assign(fallthru_var,
7022 logic_or(fallthru_var,
7023 state->switch_state.run_default)));
7024 }
7025
7026 /* Case statements do not have r-values. */
7027 return NULL;
7028 }
7029
7030 void
7031 ast_iteration_statement::condition_to_hir(exec_list *instructions,
7032 struct _mesa_glsl_parse_state *state)
7033 {
7034 void *ctx = state;
7035
7036 if (condition != NULL) {
7037 ir_rvalue *const cond =
7038 condition->hir(instructions, state);
7039
7040 if ((cond == NULL)
7041 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
7042 YYLTYPE loc = condition->get_location();
7043
7044 _mesa_glsl_error(& loc, state,
7045 "loop condition must be scalar boolean");
7046 } else {
7047 /* As the first code in the loop body, generate a block that looks
7048 * like 'if (!condition) break;' as the loop termination condition.
7049 */
7050 ir_rvalue *const not_cond =
7051 new(ctx) ir_expression(ir_unop_logic_not, cond);
7052
7053 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
7054
7055 ir_jump *const break_stmt =
7056 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
7057
7058 if_stmt->then_instructions.push_tail(break_stmt);
7059 instructions->push_tail(if_stmt);
7060 }
7061 }
7062 }
7063
7064
7065 ir_rvalue *
7066 ast_iteration_statement::hir(exec_list *instructions,
7067 struct _mesa_glsl_parse_state *state)
7068 {
7069 void *ctx = state;
7070
7071 /* For-loops and while-loops start a new scope, but do-while loops do not.
7072 */
7073 if (mode != ast_do_while)
7074 state->symbols->push_scope();
7075
7076 if (init_statement != NULL)
7077 init_statement->hir(instructions, state);
7078
7079 ir_loop *const stmt = new(ctx) ir_loop();
7080 instructions->push_tail(stmt);
7081
7082 /* Track the current loop nesting. */
7083 ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
7084
7085 state->loop_nesting_ast = this;
7086
7087 /* Likewise, indicate that following code is closest to a loop,
7088 * NOT closest to a switch.
7089 */
7090 bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
7091 state->switch_state.is_switch_innermost = false;
7092
7093 if (mode != ast_do_while)
7094 condition_to_hir(&stmt->body_instructions, state);
7095
7096 if (body != NULL)
7097 body->hir(& stmt->body_instructions, state);
7098
7099 if (rest_expression != NULL)
7100 rest_expression->hir(& stmt->body_instructions, state);
7101
7102 if (mode == ast_do_while)
7103 condition_to_hir(&stmt->body_instructions, state);
7104
7105 if (mode != ast_do_while)
7106 state->symbols->pop_scope();
7107
7108 /* Restore previous nesting before returning. */
7109 state->loop_nesting_ast = nesting_ast;
7110 state->switch_state.is_switch_innermost = saved_is_switch_innermost;
7111
7112 /* Loops do not have r-values.
7113 */
7114 return NULL;
7115 }
7116
7117
7118 /**
7119 * Determine if the given type is valid for establishing a default precision
7120 * qualifier.
7121 *
7122 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
7123 *
7124 * "The precision statement
7125 *
7126 * precision precision-qualifier type;
7127 *
7128 * can be used to establish a default precision qualifier. The type field
7129 * can be either int or float or any of the sampler types, and the
7130 * precision-qualifier can be lowp, mediump, or highp."
7131 *
7132 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
7133 * qualifiers on sampler types, but this seems like an oversight (since the
7134 * intention of including these in GLSL 1.30 is to allow compatibility with ES
7135 * shaders). So we allow int, float, and all sampler types regardless of GLSL
7136 * version.
7137 */
7138 static bool
7139 is_valid_default_precision_type(const struct glsl_type *const type)
7140 {
7141 if (type == NULL)
7142 return false;
7143
7144 switch (type->base_type) {
7145 case GLSL_TYPE_INT:
7146 case GLSL_TYPE_FLOAT:
7147 /* "int" and "float" are valid, but vectors and matrices are not. */
7148 return type->vector_elements == 1 && type->matrix_columns == 1;
7149 case GLSL_TYPE_SAMPLER:
7150 case GLSL_TYPE_IMAGE:
7151 case GLSL_TYPE_ATOMIC_UINT:
7152 return true;
7153 default:
7154 return false;
7155 }
7156 }
7157
7158
7159 ir_rvalue *
7160 ast_type_specifier::hir(exec_list *instructions,
7161 struct _mesa_glsl_parse_state *state)
7162 {
7163 if (this->default_precision == ast_precision_none && this->structure == NULL)
7164 return NULL;
7165
7166 YYLTYPE loc = this->get_location();
7167
7168 /* If this is a precision statement, check that the type to which it is
7169 * applied is either float or int.
7170 *
7171 * From section 4.5.3 of the GLSL 1.30 spec:
7172 * "The precision statement
7173 * precision precision-qualifier type;
7174 * can be used to establish a default precision qualifier. The type
7175 * field can be either int or float [...]. Any other types or
7176 * qualifiers will result in an error.
7177 */
7178 if (this->default_precision != ast_precision_none) {
7179 if (!state->check_precision_qualifiers_allowed(&loc))
7180 return NULL;
7181
7182 if (this->structure != NULL) {
7183 _mesa_glsl_error(&loc, state,
7184 "precision qualifiers do not apply to structures");
7185 return NULL;
7186 }
7187
7188 if (this->array_specifier != NULL) {
7189 _mesa_glsl_error(&loc, state,
7190 "default precision statements do not apply to "
7191 "arrays");
7192 return NULL;
7193 }
7194
7195 const struct glsl_type *const type =
7196 state->symbols->get_type(this->type_name);
7197 if (!is_valid_default_precision_type(type)) {
7198 _mesa_glsl_error(&loc, state,
7199 "default precision statements apply only to "
7200 "float, int, and opaque types");
7201 return NULL;
7202 }
7203
7204 if (state->es_shader) {
7205 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
7206 * spec says:
7207 *
7208 * "Non-precision qualified declarations will use the precision
7209 * qualifier specified in the most recent precision statement
7210 * that is still in scope. The precision statement has the same
7211 * scoping rules as variable declarations. If it is declared
7212 * inside a compound statement, its effect stops at the end of
7213 * the innermost statement it was declared in. Precision
7214 * statements in nested scopes override precision statements in
7215 * outer scopes. Multiple precision statements for the same basic
7216 * type can appear inside the same scope, with later statements
7217 * overriding earlier statements within that scope."
7218 *
7219 * Default precision specifications follow the same scope rules as
7220 * variables. So, we can track the state of the default precision
7221 * qualifiers in the symbol table, and the rules will just work. This
7222 * is a slight abuse of the symbol table, but it has the semantics
7223 * that we want.
7224 */
7225 state->symbols->add_default_precision_qualifier(this->type_name,
7226 this->default_precision);
7227 }
7228
7229 /* FINISHME: Translate precision statements into IR. */
7230 return NULL;
7231 }
7232
7233 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
7234 * process_record_constructor() can do type-checking on C-style initializer
7235 * expressions of structs, but ast_struct_specifier should only be translated
7236 * to HIR if it is declaring the type of a structure.
7237 *
7238 * The ->is_declaration field is false for initializers of variables
7239 * declared separately from the struct's type definition.
7240 *
7241 * struct S { ... }; (is_declaration = true)
7242 * struct T { ... } t = { ... }; (is_declaration = true)
7243 * S s = { ... }; (is_declaration = false)
7244 */
7245 if (this->structure != NULL && this->structure->is_declaration)
7246 return this->structure->hir(instructions, state);
7247
7248 return NULL;
7249 }
7250
7251
7252 /**
7253 * Process a structure or interface block tree into an array of structure fields
7254 *
7255 * After parsing, where there are some syntax differnces, structures and
7256 * interface blocks are almost identical. They are similar enough that the
7257 * AST for each can be processed the same way into a set of
7258 * \c glsl_struct_field to describe the members.
7259 *
7260 * If we're processing an interface block, var_mode should be the type of the
7261 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
7262 * ir_var_shader_storage). If we're processing a structure, var_mode should be
7263 * ir_var_auto.
7264 *
7265 * \return
7266 * The number of fields processed. A pointer to the array structure fields is
7267 * stored in \c *fields_ret.
7268 */
7269 static unsigned
7270 ast_process_struct_or_iface_block_members(exec_list *instructions,
7271 struct _mesa_glsl_parse_state *state,
7272 exec_list *declarations,
7273 glsl_struct_field **fields_ret,
7274 bool is_interface,
7275 enum glsl_matrix_layout matrix_layout,
7276 bool allow_reserved_names,
7277 ir_variable_mode var_mode,
7278 ast_type_qualifier *layout,
7279 unsigned block_stream,
7280 unsigned block_xfb_buffer,
7281 unsigned block_xfb_offset,
7282 unsigned expl_location,
7283 unsigned expl_align)
7284 {
7285 unsigned decl_count = 0;
7286 unsigned next_offset = 0;
7287
7288 /* Make an initial pass over the list of fields to determine how
7289 * many there are. Each element in this list is an ast_declarator_list.
7290 * This means that we actually need to count the number of elements in the
7291 * 'declarations' list in each of the elements.
7292 */
7293 foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
7294 decl_count += decl_list->declarations.length();
7295 }
7296
7297 /* Allocate storage for the fields and process the field
7298 * declarations. As the declarations are processed, try to also convert
7299 * the types to HIR. This ensures that structure definitions embedded in
7300 * other structure definitions or in interface blocks are processed.
7301 */
7302 glsl_struct_field *const fields = rzalloc_array(state, glsl_struct_field,
7303 decl_count);
7304
7305 bool first_member = true;
7306 bool first_member_has_explicit_location = false;
7307
7308 unsigned i = 0;
7309 foreach_list_typed (ast_declarator_list, decl_list, link, declarations) {
7310 const char *type_name;
7311 YYLTYPE loc = decl_list->get_location();
7312
7313 decl_list->type->specifier->hir(instructions, state);
7314
7315 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
7316 *
7317 * "Anonymous structures are not supported; so embedded structures
7318 * must have a declarator. A name given to an embedded struct is
7319 * scoped at the same level as the struct it is embedded in."
7320 *
7321 * The same section of the GLSL 1.20 spec says:
7322 *
7323 * "Anonymous structures are not supported. Embedded structures are
7324 * not supported."
7325 *
7326 * The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow
7327 * embedded structures in 1.10 only.
7328 */
7329 if (state->language_version != 110 &&
7330 decl_list->type->specifier->structure != NULL)
7331 _mesa_glsl_error(&loc, state,
7332 "embedded structure declarations are not allowed");
7333
7334 const glsl_type *decl_type =
7335 decl_list->type->glsl_type(& type_name, state);
7336
7337 const struct ast_type_qualifier *const qual =
7338 &decl_list->type->qualifier;
7339
7340 /* From section 4.3.9 of the GLSL 4.40 spec:
7341 *
7342 * "[In interface blocks] opaque types are not allowed."
7343 *
7344 * It should be impossible for decl_type to be NULL here. Cases that
7345 * might naturally lead to decl_type being NULL, especially for the
7346 * is_interface case, will have resulted in compilation having
7347 * already halted due to a syntax error.
7348 */
7349 assert(decl_type);
7350
7351 if (is_interface) {
7352 /* From section 4.3.7 of the ARB_bindless_texture spec:
7353 *
7354 * "(remove the following bullet from the last list on p. 39,
7355 * thereby permitting sampler types in interface blocks; image
7356 * types are also permitted in blocks by this extension)"
7357 *
7358 * * sampler types are not allowed
7359 */
7360 if (decl_type->contains_atomic() ||
7361 (!state->has_bindless() && decl_type->contains_opaque())) {
7362 _mesa_glsl_error(&loc, state, "uniform/buffer in non-default "
7363 "interface block contains %s variable",
7364 state->has_bindless() ? "atomic" : "opaque");
7365 }
7366 } else {
7367 if (decl_type->contains_atomic()) {
7368 /* From section 4.1.7.3 of the GLSL 4.40 spec:
7369 *
7370 * "Members of structures cannot be declared as atomic counter
7371 * types."
7372 */
7373 _mesa_glsl_error(&loc, state, "atomic counter in structure");
7374 }
7375
7376 if (!state->has_bindless() && decl_type->contains_image()) {
7377 /* FINISHME: Same problem as with atomic counters.
7378 * FINISHME: Request clarification from Khronos and add
7379 * FINISHME: spec quotation here.
7380 */
7381 _mesa_glsl_error(&loc, state, "image in structure");
7382 }
7383 }
7384
7385 if (qual->flags.q.explicit_binding) {
7386 _mesa_glsl_error(&loc, state,
7387 "binding layout qualifier cannot be applied "
7388 "to struct or interface block members");
7389 }
7390
7391 if (is_interface) {
7392 if (!first_member) {
7393 if (!layout->flags.q.explicit_location &&
7394 ((first_member_has_explicit_location &&
7395 !qual->flags.q.explicit_location) ||
7396 (!first_member_has_explicit_location &&
7397 qual->flags.q.explicit_location))) {
7398 _mesa_glsl_error(&loc, state,
7399 "when block-level location layout qualifier "
7400 "is not supplied either all members must "
7401 "have a location layout qualifier or all "
7402 "members must not have a location layout "
7403 "qualifier");
7404 }
7405 } else {
7406 first_member = false;
7407 first_member_has_explicit_location =
7408 qual->flags.q.explicit_location;
7409 }
7410 }
7411
7412 if (qual->flags.q.std140 ||
7413 qual->flags.q.std430 ||
7414 qual->flags.q.packed ||
7415 qual->flags.q.shared) {
7416 _mesa_glsl_error(&loc, state,
7417 "uniform/shader storage block layout qualifiers "
7418 "std140, std430, packed, and shared can only be "
7419 "applied to uniform/shader storage blocks, not "
7420 "members");
7421 }
7422
7423 if (qual->flags.q.constant) {
7424 _mesa_glsl_error(&loc, state,
7425 "const storage qualifier cannot be applied "
7426 "to struct or interface block members");
7427 }
7428
7429 validate_memory_qualifier_for_type(state, &loc, qual, decl_type);
7430 validate_image_format_qualifier_for_type(state, &loc, qual, decl_type);
7431
7432 /* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec:
7433 *
7434 * "A block member may be declared with a stream identifier, but
7435 * the specified stream must match the stream associated with the
7436 * containing block."
7437 */
7438 if (qual->flags.q.explicit_stream) {
7439 unsigned qual_stream;
7440 if (process_qualifier_constant(state, &loc, "stream",
7441 qual->stream, &qual_stream) &&
7442 qual_stream != block_stream) {
7443 _mesa_glsl_error(&loc, state, "stream layout qualifier on "
7444 "interface block member does not match "
7445 "the interface block (%u vs %u)", qual_stream,
7446 block_stream);
7447 }
7448 }
7449
7450 int xfb_buffer;
7451 unsigned explicit_xfb_buffer = 0;
7452 if (qual->flags.q.explicit_xfb_buffer) {
7453 unsigned qual_xfb_buffer;
7454 if (process_qualifier_constant(state, &loc, "xfb_buffer",
7455 qual->xfb_buffer, &qual_xfb_buffer)) {
7456 explicit_xfb_buffer = 1;
7457 if (qual_xfb_buffer != block_xfb_buffer)
7458 _mesa_glsl_error(&loc, state, "xfb_buffer layout qualifier on "
7459 "interface block member does not match "
7460 "the interface block (%u vs %u)",
7461 qual_xfb_buffer, block_xfb_buffer);
7462 }
7463 xfb_buffer = (int) qual_xfb_buffer;
7464 } else {
7465 if (layout)
7466 explicit_xfb_buffer = layout->flags.q.explicit_xfb_buffer;
7467 xfb_buffer = (int) block_xfb_buffer;
7468 }
7469
7470 int xfb_stride = -1;
7471 if (qual->flags.q.explicit_xfb_stride) {
7472 unsigned qual_xfb_stride;
7473 if (process_qualifier_constant(state, &loc, "xfb_stride",
7474 qual->xfb_stride, &qual_xfb_stride)) {
7475 xfb_stride = (int) qual_xfb_stride;
7476 }
7477 }
7478
7479 if (qual->flags.q.uniform && qual->has_interpolation()) {
7480 _mesa_glsl_error(&loc, state,
7481 "interpolation qualifiers cannot be used "
7482 "with uniform interface blocks");
7483 }
7484
7485 if ((qual->flags.q.uniform || !is_interface) &&
7486 qual->has_auxiliary_storage()) {
7487 _mesa_glsl_error(&loc, state,
7488 "auxiliary storage qualifiers cannot be used "
7489 "in uniform blocks or structures.");
7490 }
7491
7492 if (qual->flags.q.row_major || qual->flags.q.column_major) {
7493 if (!qual->flags.q.uniform && !qual->flags.q.buffer) {
7494 _mesa_glsl_error(&loc, state,
7495 "row_major and column_major can only be "
7496 "applied to interface blocks");
7497 } else
7498 validate_matrix_layout_for_type(state, &loc, decl_type, NULL);
7499 }
7500
7501 foreach_list_typed (ast_declaration, decl, link,
7502 &decl_list->declarations) {
7503 YYLTYPE loc = decl->get_location();
7504
7505 if (!allow_reserved_names)
7506 validate_identifier(decl->identifier, loc, state);
7507
7508 const struct glsl_type *field_type =
7509 process_array_type(&loc, decl_type, decl->array_specifier, state);
7510 validate_array_dimensions(field_type, state, &loc);
7511 fields[i].type = field_type;
7512 fields[i].name = decl->identifier;
7513 fields[i].interpolation =
7514 interpret_interpolation_qualifier(qual, field_type,
7515 var_mode, state, &loc);
7516 fields[i].centroid = qual->flags.q.centroid ? 1 : 0;
7517 fields[i].sample = qual->flags.q.sample ? 1 : 0;
7518 fields[i].patch = qual->flags.q.patch ? 1 : 0;
7519 fields[i].offset = -1;
7520 fields[i].explicit_xfb_buffer = explicit_xfb_buffer;
7521 fields[i].xfb_buffer = xfb_buffer;
7522 fields[i].xfb_stride = xfb_stride;
7523
7524 if (qual->flags.q.explicit_location) {
7525 unsigned qual_location;
7526 if (process_qualifier_constant(state, &loc, "location",
7527 qual->location, &qual_location)) {
7528 fields[i].location = qual_location +
7529 (fields[i].patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0);
7530 expl_location = fields[i].location +
7531 fields[i].type->count_attribute_slots(false);
7532 }
7533 } else {
7534 if (layout && layout->flags.q.explicit_location) {
7535 fields[i].location = expl_location;
7536 expl_location += fields[i].type->count_attribute_slots(false);
7537 } else {
7538 fields[i].location = -1;
7539 }
7540 }
7541
7542 /* Offset can only be used with std430 and std140 layouts an initial
7543 * value of 0 is used for error detection.
7544 */
7545 unsigned align = 0;
7546 unsigned size = 0;
7547 if (layout) {
7548 bool row_major;
7549 if (qual->flags.q.row_major ||
7550 matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
7551 row_major = true;
7552 } else {
7553 row_major = false;
7554 }
7555
7556 if(layout->flags.q.std140) {
7557 align = field_type->std140_base_alignment(row_major);
7558 size = field_type->std140_size(row_major);
7559 } else if (layout->flags.q.std430) {
7560 align = field_type->std430_base_alignment(row_major);
7561 size = field_type->std430_size(row_major);
7562 }
7563 }
7564
7565 if (qual->flags.q.explicit_offset) {
7566 unsigned qual_offset;
7567 if (process_qualifier_constant(state, &loc, "offset",
7568 qual->offset, &qual_offset)) {
7569 if (align != 0 && size != 0) {
7570 if (next_offset > qual_offset)
7571 _mesa_glsl_error(&loc, state, "layout qualifier "
7572 "offset overlaps previous member");
7573
7574 if (qual_offset % align) {
7575 _mesa_glsl_error(&loc, state, "layout qualifier offset "
7576 "must be a multiple of the base "
7577 "alignment of %s", field_type->name);
7578 }
7579 fields[i].offset = qual_offset;
7580 next_offset = qual_offset + size;
7581 } else {
7582 _mesa_glsl_error(&loc, state, "offset can only be used "
7583 "with std430 and std140 layouts");
7584 }
7585 }
7586 }
7587
7588 if (qual->flags.q.explicit_align || expl_align != 0) {
7589 unsigned offset = fields[i].offset != -1 ? fields[i].offset :
7590 next_offset;
7591 if (align == 0 || size == 0) {
7592 _mesa_glsl_error(&loc, state, "align can only be used with "
7593 "std430 and std140 layouts");
7594 } else if (qual->flags.q.explicit_align) {
7595 unsigned member_align;
7596 if (process_qualifier_constant(state, &loc, "align",
7597 qual->align, &member_align)) {
7598 if (member_align == 0 ||
7599 member_align & (member_align - 1)) {
7600 _mesa_glsl_error(&loc, state, "align layout qualifier "
7601 "is not a power of 2");
7602 } else {
7603 fields[i].offset = glsl_align(offset, member_align);
7604 next_offset = fields[i].offset + size;
7605 }
7606 }
7607 } else {
7608 fields[i].offset = glsl_align(offset, expl_align);
7609 next_offset = fields[i].offset + size;
7610 }
7611 } else if (!qual->flags.q.explicit_offset) {
7612 if (align != 0 && size != 0)
7613 next_offset = glsl_align(next_offset, align) + size;
7614 }
7615
7616 /* From the ARB_enhanced_layouts spec:
7617 *
7618 * "The given offset applies to the first component of the first
7619 * member of the qualified entity. Then, within the qualified
7620 * entity, subsequent components are each assigned, in order, to
7621 * the next available offset aligned to a multiple of that
7622 * component's size. Aggregate types are flattened down to the
7623 * component level to get this sequence of components."
7624 */
7625 if (qual->flags.q.explicit_xfb_offset) {
7626 unsigned xfb_offset;
7627 if (process_qualifier_constant(state, &loc, "xfb_offset",
7628 qual->offset, &xfb_offset)) {
7629 fields[i].offset = xfb_offset;
7630 block_xfb_offset = fields[i].offset +
7631 4 * field_type->component_slots();
7632 }
7633 } else {
7634 if (layout && layout->flags.q.explicit_xfb_offset) {
7635 unsigned align = field_type->is_64bit() ? 8 : 4;
7636 fields[i].offset = glsl_align(block_xfb_offset, align);
7637 block_xfb_offset += 4 * field_type->component_slots();
7638 }
7639 }
7640
7641 /* Propogate row- / column-major information down the fields of the
7642 * structure or interface block. Structures need this data because
7643 * the structure may contain a structure that contains ... a matrix
7644 * that need the proper layout.
7645 */
7646 if (is_interface && layout &&
7647 (layout->flags.q.uniform || layout->flags.q.buffer) &&
7648 (field_type->without_array()->is_matrix()
7649 || field_type->without_array()->is_struct())) {
7650 /* If no layout is specified for the field, inherit the layout
7651 * from the block.
7652 */
7653 fields[i].matrix_layout = matrix_layout;
7654
7655 if (qual->flags.q.row_major)
7656 fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR;
7657 else if (qual->flags.q.column_major)
7658 fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR;
7659
7660 /* If we're processing an uniform or buffer block, the matrix
7661 * layout must be decided by this point.
7662 */
7663 assert(fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR
7664 || fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR);
7665 }
7666
7667 /* Memory qualifiers are allowed on buffer and image variables, while
7668 * the format qualifier is only accepted for images.
7669 */
7670 if (var_mode == ir_var_shader_storage ||
7671 field_type->without_array()->is_image()) {
7672 /* For readonly and writeonly qualifiers the field definition,
7673 * if set, overwrites the layout qualifier.
7674 */
7675 if (qual->flags.q.read_only || qual->flags.q.write_only) {
7676 fields[i].memory_read_only = qual->flags.q.read_only;
7677 fields[i].memory_write_only = qual->flags.q.write_only;
7678 } else {
7679 fields[i].memory_read_only =
7680 layout ? layout->flags.q.read_only : 0;
7681 fields[i].memory_write_only =
7682 layout ? layout->flags.q.write_only : 0;
7683 }
7684
7685 /* For other qualifiers, we set the flag if either the layout
7686 * qualifier or the field qualifier are set
7687 */
7688 fields[i].memory_coherent = qual->flags.q.coherent ||
7689 (layout && layout->flags.q.coherent);
7690 fields[i].memory_volatile = qual->flags.q._volatile ||
7691 (layout && layout->flags.q._volatile);
7692 fields[i].memory_restrict = qual->flags.q.restrict_flag ||
7693 (layout && layout->flags.q.restrict_flag);
7694
7695 if (field_type->without_array()->is_image()) {
7696 if (qual->flags.q.explicit_image_format) {
7697 if (qual->image_base_type !=
7698 field_type->without_array()->sampled_type) {
7699 _mesa_glsl_error(&loc, state, "format qualifier doesn't "
7700 "match the base data type of the image");
7701 }
7702
7703 fields[i].image_format = qual->image_format;
7704 } else {
7705 if (!qual->flags.q.write_only) {
7706 _mesa_glsl_error(&loc, state, "image not qualified with "
7707 "`writeonly' must have a format layout "
7708 "qualifier");
7709 }
7710
7711 fields[i].image_format = PIPE_FORMAT_NONE;
7712 }
7713 }
7714 }
7715
7716 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
7717 if (state->es_shader) {
7718 fields[i].precision = select_gles_precision(qual->precision,
7719 field_type,
7720 state,
7721 &loc);
7722 } else {
7723 fields[i].precision = qual->precision;
7724 }
7725
7726 i++;
7727 }
7728 }
7729
7730 assert(i == decl_count);
7731
7732 *fields_ret = fields;
7733 return decl_count;
7734 }
7735
7736
7737 ir_rvalue *
7738 ast_struct_specifier::hir(exec_list *instructions,
7739 struct _mesa_glsl_parse_state *state)
7740 {
7741 YYLTYPE loc = this->get_location();
7742
7743 unsigned expl_location = 0;
7744 if (layout && layout->flags.q.explicit_location) {
7745 if (!process_qualifier_constant(state, &loc, "location",
7746 layout->location, &expl_location)) {
7747 return NULL;
7748 } else {
7749 expl_location = VARYING_SLOT_VAR0 + expl_location;
7750 }
7751 }
7752
7753 glsl_struct_field *fields;
7754 unsigned decl_count =
7755 ast_process_struct_or_iface_block_members(instructions,
7756 state,
7757 &this->declarations,
7758 &fields,
7759 false,
7760 GLSL_MATRIX_LAYOUT_INHERITED,
7761 false /* allow_reserved_names */,
7762 ir_var_auto,
7763 layout,
7764 0, /* for interface only */
7765 0, /* for interface only */
7766 0, /* for interface only */
7767 expl_location,
7768 0 /* for interface only */);
7769
7770 validate_identifier(this->name, loc, state);
7771
7772 type = glsl_type::get_struct_instance(fields, decl_count, this->name);
7773
7774 if (!type->is_anonymous() && !state->symbols->add_type(name, type)) {
7775 const glsl_type *match = state->symbols->get_type(name);
7776 /* allow struct matching for desktop GL - older UE4 does this */
7777 if (match != NULL && state->is_version(130, 0) && match->record_compare(type, true, false))
7778 _mesa_glsl_warning(& loc, state, "struct `%s' previously defined", name);
7779 else
7780 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
7781 } else {
7782 const glsl_type **s = reralloc(state, state->user_structures,
7783 const glsl_type *,
7784 state->num_user_structures + 1);
7785 if (s != NULL) {
7786 s[state->num_user_structures] = type;
7787 state->user_structures = s;
7788 state->num_user_structures++;
7789 }
7790 }
7791
7792 /* Structure type definitions do not have r-values.
7793 */
7794 return NULL;
7795 }
7796
7797
7798 /**
7799 * Visitor class which detects whether a given interface block has been used.
7800 */
7801 class interface_block_usage_visitor : public ir_hierarchical_visitor
7802 {
7803 public:
7804 interface_block_usage_visitor(ir_variable_mode mode, const glsl_type *block)
7805 : mode(mode), block(block), found(false)
7806 {
7807 }
7808
7809 virtual ir_visitor_status visit(ir_dereference_variable *ir)
7810 {
7811 if (ir->var->data.mode == mode && ir->var->get_interface_type() == block) {
7812 found = true;
7813 return visit_stop;
7814 }
7815 return visit_continue;
7816 }
7817
7818 bool usage_found() const
7819 {
7820 return this->found;
7821 }
7822
7823 private:
7824 ir_variable_mode mode;
7825 const glsl_type *block;
7826 bool found;
7827 };
7828
7829 static bool
7830 is_unsized_array_last_element(ir_variable *v)
7831 {
7832 const glsl_type *interface_type = v->get_interface_type();
7833 int length = interface_type->length;
7834
7835 assert(v->type->is_unsized_array());
7836
7837 /* Check if it is the last element of the interface */
7838 if (strcmp(interface_type->fields.structure[length-1].name, v->name) == 0)
7839 return true;
7840 return false;
7841 }
7842
7843 static void
7844 apply_memory_qualifiers(ir_variable *var, glsl_struct_field field)
7845 {
7846 var->data.memory_read_only = field.memory_read_only;
7847 var->data.memory_write_only = field.memory_write_only;
7848 var->data.memory_coherent = field.memory_coherent;
7849 var->data.memory_volatile = field.memory_volatile;
7850 var->data.memory_restrict = field.memory_restrict;
7851 }
7852
7853 ir_rvalue *
7854 ast_interface_block::hir(exec_list *instructions,
7855 struct _mesa_glsl_parse_state *state)
7856 {
7857 YYLTYPE loc = this->get_location();
7858
7859 /* Interface blocks must be declared at global scope */
7860 if (state->current_function != NULL) {
7861 _mesa_glsl_error(&loc, state,
7862 "Interface block `%s' must be declared "
7863 "at global scope",
7864 this->block_name);
7865 }
7866
7867 /* Validate qualifiers:
7868 *
7869 * - Layout Qualifiers as per the table in Section 4.4
7870 * ("Layout Qualifiers") of the GLSL 4.50 spec.
7871 *
7872 * - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the
7873 * GLSL 4.50 spec:
7874 *
7875 * "Additionally, memory qualifiers may also be used in the declaration
7876 * of shader storage blocks"
7877 *
7878 * Note the table in Section 4.4 says std430 is allowed on both uniform and
7879 * buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block
7880 * Layout Qualifiers) of the GLSL 4.50 spec says:
7881 *
7882 * "The std430 qualifier is supported only for shader storage blocks;
7883 * using std430 on a uniform block will result in a compile-time error."
7884 */
7885 ast_type_qualifier allowed_blk_qualifiers;
7886 allowed_blk_qualifiers.flags.i = 0;
7887 if (this->layout.flags.q.buffer || this->layout.flags.q.uniform) {
7888 allowed_blk_qualifiers.flags.q.shared = 1;
7889 allowed_blk_qualifiers.flags.q.packed = 1;
7890 allowed_blk_qualifiers.flags.q.std140 = 1;
7891 allowed_blk_qualifiers.flags.q.row_major = 1;
7892 allowed_blk_qualifiers.flags.q.column_major = 1;
7893 allowed_blk_qualifiers.flags.q.explicit_align = 1;
7894 allowed_blk_qualifiers.flags.q.explicit_binding = 1;
7895 if (this->layout.flags.q.buffer) {
7896 allowed_blk_qualifiers.flags.q.buffer = 1;
7897 allowed_blk_qualifiers.flags.q.std430 = 1;
7898 allowed_blk_qualifiers.flags.q.coherent = 1;
7899 allowed_blk_qualifiers.flags.q._volatile = 1;
7900 allowed_blk_qualifiers.flags.q.restrict_flag = 1;
7901 allowed_blk_qualifiers.flags.q.read_only = 1;
7902 allowed_blk_qualifiers.flags.q.write_only = 1;
7903 } else {
7904 allowed_blk_qualifiers.flags.q.uniform = 1;
7905 }
7906 } else {
7907 /* Interface block */
7908 assert(this->layout.flags.q.in || this->layout.flags.q.out);
7909
7910 allowed_blk_qualifiers.flags.q.explicit_location = 1;
7911 if (this->layout.flags.q.out) {
7912 allowed_blk_qualifiers.flags.q.out = 1;
7913 if (state->stage == MESA_SHADER_GEOMETRY ||
7914 state->stage == MESA_SHADER_TESS_CTRL ||
7915 state->stage == MESA_SHADER_TESS_EVAL ||
7916 state->stage == MESA_SHADER_VERTEX ) {
7917 allowed_blk_qualifiers.flags.q.explicit_xfb_offset = 1;
7918 allowed_blk_qualifiers.flags.q.explicit_xfb_buffer = 1;
7919 allowed_blk_qualifiers.flags.q.xfb_buffer = 1;
7920 allowed_blk_qualifiers.flags.q.explicit_xfb_stride = 1;
7921 allowed_blk_qualifiers.flags.q.xfb_stride = 1;
7922 if (state->stage == MESA_SHADER_GEOMETRY) {
7923 allowed_blk_qualifiers.flags.q.stream = 1;
7924 allowed_blk_qualifiers.flags.q.explicit_stream = 1;
7925 }
7926 if (state->stage == MESA_SHADER_TESS_CTRL) {
7927 allowed_blk_qualifiers.flags.q.patch = 1;
7928 }
7929 }
7930 } else {
7931 allowed_blk_qualifiers.flags.q.in = 1;
7932 if (state->stage == MESA_SHADER_TESS_EVAL) {
7933 allowed_blk_qualifiers.flags.q.patch = 1;
7934 }
7935 }
7936 }
7937
7938 this->layout.validate_flags(&loc, state, allowed_blk_qualifiers,
7939 "invalid qualifier for block",
7940 this->block_name);
7941
7942 enum glsl_interface_packing packing;
7943 if (this->layout.flags.q.std140) {
7944 packing = GLSL_INTERFACE_PACKING_STD140;
7945 } else if (this->layout.flags.q.packed) {
7946 packing = GLSL_INTERFACE_PACKING_PACKED;
7947 } else if (this->layout.flags.q.std430) {
7948 packing = GLSL_INTERFACE_PACKING_STD430;
7949 } else {
7950 /* The default layout is shared.
7951 */
7952 packing = GLSL_INTERFACE_PACKING_SHARED;
7953 }
7954
7955 ir_variable_mode var_mode;
7956 const char *iface_type_name;
7957 if (this->layout.flags.q.in) {
7958 var_mode = ir_var_shader_in;
7959 iface_type_name = "in";
7960 } else if (this->layout.flags.q.out) {
7961 var_mode = ir_var_shader_out;
7962 iface_type_name = "out";
7963 } else if (this->layout.flags.q.uniform) {
7964 var_mode = ir_var_uniform;
7965 iface_type_name = "uniform";
7966 } else if (this->layout.flags.q.buffer) {
7967 var_mode = ir_var_shader_storage;
7968 iface_type_name = "buffer";
7969 } else {
7970 var_mode = ir_var_auto;
7971 iface_type_name = "UNKNOWN";
7972 assert(!"interface block layout qualifier not found!");
7973 }
7974
7975 enum glsl_matrix_layout matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
7976 if (this->layout.flags.q.row_major)
7977 matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR;
7978 else if (this->layout.flags.q.column_major)
7979 matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR;
7980
7981 bool redeclaring_per_vertex = strcmp(this->block_name, "gl_PerVertex") == 0;
7982 exec_list declared_variables;
7983 glsl_struct_field *fields;
7984
7985 /* For blocks that accept memory qualifiers (i.e. shader storage), verify
7986 * that we don't have incompatible qualifiers
7987 */
7988 if (this->layout.flags.q.read_only && this->layout.flags.q.write_only) {
7989 _mesa_glsl_error(&loc, state,
7990 "Interface block sets both readonly and writeonly");
7991 }
7992
7993 unsigned qual_stream;
7994 if (!process_qualifier_constant(state, &loc, "stream", this->layout.stream,
7995 &qual_stream) ||
7996 !validate_stream_qualifier(&loc, state, qual_stream)) {
7997 /* If the stream qualifier is invalid it doesn't make sense to continue
7998 * on and try to compare stream layouts on member variables against it
7999 * so just return early.
8000 */
8001 return NULL;
8002 }
8003
8004 unsigned qual_xfb_buffer;
8005 if (!process_qualifier_constant(state, &loc, "xfb_buffer",
8006 layout.xfb_buffer, &qual_xfb_buffer) ||
8007 !validate_xfb_buffer_qualifier(&loc, state, qual_xfb_buffer)) {
8008 return NULL;
8009 }
8010
8011 unsigned qual_xfb_offset;
8012 if (layout.flags.q.explicit_xfb_offset) {
8013 if (!process_qualifier_constant(state, &loc, "xfb_offset",
8014 layout.offset, &qual_xfb_offset)) {
8015 return NULL;
8016 }
8017 }
8018
8019 unsigned qual_xfb_stride;
8020 if (layout.flags.q.explicit_xfb_stride) {
8021 if (!process_qualifier_constant(state, &loc, "xfb_stride",
8022 layout.xfb_stride, &qual_xfb_stride)) {
8023 return NULL;
8024 }
8025 }
8026
8027 unsigned expl_location = 0;
8028 if (layout.flags.q.explicit_location) {
8029 if (!process_qualifier_constant(state, &loc, "location",
8030 layout.location, &expl_location)) {
8031 return NULL;
8032 } else {
8033 expl_location += this->layout.flags.q.patch ? VARYING_SLOT_PATCH0
8034 : VARYING_SLOT_VAR0;
8035 }
8036 }
8037
8038 unsigned expl_align = 0;
8039 if (layout.flags.q.explicit_align) {
8040 if (!process_qualifier_constant(state, &loc, "align",
8041 layout.align, &expl_align)) {
8042 return NULL;
8043 } else {
8044 if (expl_align == 0 || expl_align & (expl_align - 1)) {
8045 _mesa_glsl_error(&loc, state, "align layout qualifier is not a "
8046 "power of 2.");
8047 return NULL;
8048 }
8049 }
8050 }
8051
8052 unsigned int num_variables =
8053 ast_process_struct_or_iface_block_members(&declared_variables,
8054 state,
8055 &this->declarations,
8056 &fields,
8057 true,
8058 matrix_layout,
8059 redeclaring_per_vertex,
8060 var_mode,
8061 &this->layout,
8062 qual_stream,
8063 qual_xfb_buffer,
8064 qual_xfb_offset,
8065 expl_location,
8066 expl_align);
8067
8068 if (!redeclaring_per_vertex) {
8069 validate_identifier(this->block_name, loc, state);
8070
8071 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
8072 *
8073 * "Block names have no other use within a shader beyond interface
8074 * matching; it is a compile-time error to use a block name at global
8075 * scope for anything other than as a block name."
8076 */
8077 ir_variable *var = state->symbols->get_variable(this->block_name);
8078 if (var && !var->type->is_interface()) {
8079 _mesa_glsl_error(&loc, state, "Block name `%s' is "
8080 "already used in the scope.",
8081 this->block_name);
8082 }
8083 }
8084
8085 const glsl_type *earlier_per_vertex = NULL;
8086 if (redeclaring_per_vertex) {
8087 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
8088 * the named interface block gl_in, we can find it by looking at the
8089 * previous declaration of gl_in. Otherwise we can find it by looking
8090 * at the previous decalartion of any of the built-in outputs,
8091 * e.g. gl_Position.
8092 *
8093 * Also check that the instance name and array-ness of the redeclaration
8094 * are correct.
8095 */
8096 switch (var_mode) {
8097 case ir_var_shader_in:
8098 if (ir_variable *earlier_gl_in =
8099 state->symbols->get_variable("gl_in")) {
8100 earlier_per_vertex = earlier_gl_in->get_interface_type();
8101 } else {
8102 _mesa_glsl_error(&loc, state,
8103 "redeclaration of gl_PerVertex input not allowed "
8104 "in the %s shader",
8105 _mesa_shader_stage_to_string(state->stage));
8106 }
8107 if (this->instance_name == NULL ||
8108 strcmp(this->instance_name, "gl_in") != 0 || this->array_specifier == NULL ||
8109 !this->array_specifier->is_single_dimension()) {
8110 _mesa_glsl_error(&loc, state,
8111 "gl_PerVertex input must be redeclared as "
8112 "gl_in[]");
8113 }
8114 break;
8115 case ir_var_shader_out:
8116 if (ir_variable *earlier_gl_Position =
8117 state->symbols->get_variable("gl_Position")) {
8118 earlier_per_vertex = earlier_gl_Position->get_interface_type();
8119 } else if (ir_variable *earlier_gl_out =
8120 state->symbols->get_variable("gl_out")) {
8121 earlier_per_vertex = earlier_gl_out->get_interface_type();
8122 } else {
8123 _mesa_glsl_error(&loc, state,
8124 "redeclaration of gl_PerVertex output not "
8125 "allowed in the %s shader",
8126 _mesa_shader_stage_to_string(state->stage));
8127 }
8128 if (state->stage == MESA_SHADER_TESS_CTRL) {
8129 if (this->instance_name == NULL ||
8130 strcmp(this->instance_name, "gl_out") != 0 || this->array_specifier == NULL) {
8131 _mesa_glsl_error(&loc, state,
8132 "gl_PerVertex output must be redeclared as "
8133 "gl_out[]");
8134 }
8135 } else {
8136 if (this->instance_name != NULL) {
8137 _mesa_glsl_error(&loc, state,
8138 "gl_PerVertex output may not be redeclared with "
8139 "an instance name");
8140 }
8141 }
8142 break;
8143 default:
8144 _mesa_glsl_error(&loc, state,
8145 "gl_PerVertex must be declared as an input or an "
8146 "output");
8147 break;
8148 }
8149
8150 if (earlier_per_vertex == NULL) {
8151 /* An error has already been reported. Bail out to avoid null
8152 * dereferences later in this function.
8153 */
8154 return NULL;
8155 }
8156
8157 /* Copy locations from the old gl_PerVertex interface block. */
8158 for (unsigned i = 0; i < num_variables; i++) {
8159 int j = earlier_per_vertex->field_index(fields[i].name);
8160 if (j == -1) {
8161 _mesa_glsl_error(&loc, state,
8162 "redeclaration of gl_PerVertex must be a subset "
8163 "of the built-in members of gl_PerVertex");
8164 } else {
8165 fields[i].location =
8166 earlier_per_vertex->fields.structure[j].location;
8167 fields[i].offset =
8168 earlier_per_vertex->fields.structure[j].offset;
8169 fields[i].interpolation =
8170 earlier_per_vertex->fields.structure[j].interpolation;
8171 fields[i].centroid =
8172 earlier_per_vertex->fields.structure[j].centroid;
8173 fields[i].sample =
8174 earlier_per_vertex->fields.structure[j].sample;
8175 fields[i].patch =
8176 earlier_per_vertex->fields.structure[j].patch;
8177 fields[i].precision =
8178 earlier_per_vertex->fields.structure[j].precision;
8179 fields[i].explicit_xfb_buffer =
8180 earlier_per_vertex->fields.structure[j].explicit_xfb_buffer;
8181 fields[i].xfb_buffer =
8182 earlier_per_vertex->fields.structure[j].xfb_buffer;
8183 fields[i].xfb_stride =
8184 earlier_per_vertex->fields.structure[j].xfb_stride;
8185 }
8186 }
8187
8188 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
8189 * spec:
8190 *
8191 * If a built-in interface block is redeclared, it must appear in
8192 * the shader before any use of any member included in the built-in
8193 * declaration, or a compilation error will result.
8194 *
8195 * This appears to be a clarification to the behaviour established for
8196 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
8197 * regardless of GLSL version.
8198 */
8199 interface_block_usage_visitor v(var_mode, earlier_per_vertex);
8200 v.run(instructions);
8201 if (v.usage_found()) {
8202 _mesa_glsl_error(&loc, state,
8203 "redeclaration of a built-in interface block must "
8204 "appear before any use of any member of the "
8205 "interface block");
8206 }
8207 }
8208
8209 const glsl_type *block_type =
8210 glsl_type::get_interface_instance(fields,
8211 num_variables,
8212 packing,
8213 matrix_layout ==
8214 GLSL_MATRIX_LAYOUT_ROW_MAJOR,
8215 this->block_name);
8216
8217 unsigned component_size = block_type->contains_double() ? 8 : 4;
8218 int xfb_offset =
8219 layout.flags.q.explicit_xfb_offset ? (int) qual_xfb_offset : -1;
8220 validate_xfb_offset_qualifier(&loc, state, xfb_offset, block_type,
8221 component_size);
8222
8223 if (!state->symbols->add_interface(block_type->name, block_type, var_mode)) {
8224 YYLTYPE loc = this->get_location();
8225 _mesa_glsl_error(&loc, state, "interface block `%s' with type `%s' "
8226 "already taken in the current scope",
8227 this->block_name, iface_type_name);
8228 }
8229
8230 /* Since interface blocks cannot contain statements, it should be
8231 * impossible for the block to generate any instructions.
8232 */
8233 assert(declared_variables.is_empty());
8234
8235 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
8236 *
8237 * Geometry shader input variables get the per-vertex values written
8238 * out by vertex shader output variables of the same names. Since a
8239 * geometry shader operates on a set of vertices, each input varying
8240 * variable (or input block, see interface blocks below) needs to be
8241 * declared as an array.
8242 */
8243 if (state->stage == MESA_SHADER_GEOMETRY && this->array_specifier == NULL &&
8244 var_mode == ir_var_shader_in) {
8245 _mesa_glsl_error(&loc, state, "geometry shader inputs must be arrays");
8246 } else if ((state->stage == MESA_SHADER_TESS_CTRL ||
8247 state->stage == MESA_SHADER_TESS_EVAL) &&
8248 !this->layout.flags.q.patch &&
8249 this->array_specifier == NULL &&
8250 var_mode == ir_var_shader_in) {
8251 _mesa_glsl_error(&loc, state, "per-vertex tessellation shader inputs must be arrays");
8252 } else if (state->stage == MESA_SHADER_TESS_CTRL &&
8253 !this->layout.flags.q.patch &&
8254 this->array_specifier == NULL &&
8255 var_mode == ir_var_shader_out) {
8256 _mesa_glsl_error(&loc, state, "tessellation control shader outputs must be arrays");
8257 }
8258
8259
8260 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
8261 * says:
8262 *
8263 * "If an instance name (instance-name) is used, then it puts all the
8264 * members inside a scope within its own name space, accessed with the
8265 * field selector ( . ) operator (analogously to structures)."
8266 */
8267 if (this->instance_name) {
8268 if (redeclaring_per_vertex) {
8269 /* When a built-in in an unnamed interface block is redeclared,
8270 * get_variable_being_redeclared() calls
8271 * check_builtin_array_max_size() to make sure that built-in array
8272 * variables aren't redeclared to illegal sizes. But we're looking
8273 * at a redeclaration of a named built-in interface block. So we
8274 * have to manually call check_builtin_array_max_size() for all parts
8275 * of the interface that are arrays.
8276 */
8277 for (unsigned i = 0; i < num_variables; i++) {
8278 if (fields[i].type->is_array()) {
8279 const unsigned size = fields[i].type->array_size();
8280 check_builtin_array_max_size(fields[i].name, size, loc, state);
8281 }
8282 }
8283 } else {
8284 validate_identifier(this->instance_name, loc, state);
8285 }
8286
8287 ir_variable *var;
8288
8289 if (this->array_specifier != NULL) {
8290 const glsl_type *block_array_type =
8291 process_array_type(&loc, block_type, this->array_specifier, state);
8292
8293 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
8294 *
8295 * For uniform blocks declared an array, each individual array
8296 * element corresponds to a separate buffer object backing one
8297 * instance of the block. As the array size indicates the number
8298 * of buffer objects needed, uniform block array declarations
8299 * must specify an array size.
8300 *
8301 * And a few paragraphs later:
8302 *
8303 * Geometry shader input blocks must be declared as arrays and
8304 * follow the array declaration and linking rules for all
8305 * geometry shader inputs. All other input and output block
8306 * arrays must specify an array size.
8307 *
8308 * The same applies to tessellation shaders.
8309 *
8310 * The upshot of this is that the only circumstance where an
8311 * interface array size *doesn't* need to be specified is on a
8312 * geometry shader input, tessellation control shader input,
8313 * tessellation control shader output, and tessellation evaluation
8314 * shader input.
8315 */
8316 if (block_array_type->is_unsized_array()) {
8317 bool allow_inputs = state->stage == MESA_SHADER_GEOMETRY ||
8318 state->stage == MESA_SHADER_TESS_CTRL ||
8319 state->stage == MESA_SHADER_TESS_EVAL;
8320 bool allow_outputs = state->stage == MESA_SHADER_TESS_CTRL;
8321
8322 if (this->layout.flags.q.in) {
8323 if (!allow_inputs)
8324 _mesa_glsl_error(&loc, state,
8325 "unsized input block arrays not allowed in "
8326 "%s shader",
8327 _mesa_shader_stage_to_string(state->stage));
8328 } else if (this->layout.flags.q.out) {
8329 if (!allow_outputs)
8330 _mesa_glsl_error(&loc, state,
8331 "unsized output block arrays not allowed in "
8332 "%s shader",
8333 _mesa_shader_stage_to_string(state->stage));
8334 } else {
8335 /* by elimination, this is a uniform block array */
8336 _mesa_glsl_error(&loc, state,
8337 "unsized uniform block arrays not allowed in "
8338 "%s shader",
8339 _mesa_shader_stage_to_string(state->stage));
8340 }
8341 }
8342
8343 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
8344 *
8345 * * Arrays of arrays of blocks are not allowed
8346 */
8347 if (state->es_shader && block_array_type->is_array() &&
8348 block_array_type->fields.array->is_array()) {
8349 _mesa_glsl_error(&loc, state,
8350 "arrays of arrays interface blocks are "
8351 "not allowed");
8352 }
8353
8354 var = new(state) ir_variable(block_array_type,
8355 this->instance_name,
8356 var_mode);
8357 } else {
8358 var = new(state) ir_variable(block_type,
8359 this->instance_name,
8360 var_mode);
8361 }
8362
8363 var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED
8364 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout;
8365
8366 if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform)
8367 var->data.read_only = true;
8368
8369 var->data.patch = this->layout.flags.q.patch;
8370
8371 if (state->stage == MESA_SHADER_GEOMETRY && var_mode == ir_var_shader_in)
8372 handle_geometry_shader_input_decl(state, loc, var);
8373 else if ((state->stage == MESA_SHADER_TESS_CTRL ||
8374 state->stage == MESA_SHADER_TESS_EVAL) && var_mode == ir_var_shader_in)
8375 handle_tess_shader_input_decl(state, loc, var);
8376 else if (state->stage == MESA_SHADER_TESS_CTRL && var_mode == ir_var_shader_out)
8377 handle_tess_ctrl_shader_output_decl(state, loc, var);
8378
8379 for (unsigned i = 0; i < num_variables; i++) {
8380 if (var->data.mode == ir_var_shader_storage)
8381 apply_memory_qualifiers(var, fields[i]);
8382 }
8383
8384 if (ir_variable *earlier =
8385 state->symbols->get_variable(this->instance_name)) {
8386 if (!redeclaring_per_vertex) {
8387 _mesa_glsl_error(&loc, state, "`%s' redeclared",
8388 this->instance_name);
8389 }
8390 earlier->data.how_declared = ir_var_declared_normally;
8391 earlier->type = var->type;
8392 earlier->reinit_interface_type(block_type);
8393 delete var;
8394 } else {
8395 if (this->layout.flags.q.explicit_binding) {
8396 apply_explicit_binding(state, &loc, var, var->type,
8397 &this->layout);
8398 }
8399
8400 var->data.stream = qual_stream;
8401 if (layout.flags.q.explicit_location) {
8402 var->data.location = expl_location;
8403 var->data.explicit_location = true;
8404 }
8405
8406 state->symbols->add_variable(var);
8407 instructions->push_tail(var);
8408 }
8409 } else {
8410 /* In order to have an array size, the block must also be declared with
8411 * an instance name.
8412 */
8413 assert(this->array_specifier == NULL);
8414
8415 for (unsigned i = 0; i < num_variables; i++) {
8416 ir_variable *var =
8417 new(state) ir_variable(fields[i].type,
8418 ralloc_strdup(state, fields[i].name),
8419 var_mode);
8420 var->data.interpolation = fields[i].interpolation;
8421 var->data.centroid = fields[i].centroid;
8422 var->data.sample = fields[i].sample;
8423 var->data.patch = fields[i].patch;
8424 var->data.stream = qual_stream;
8425 var->data.location = fields[i].location;
8426
8427 if (fields[i].location != -1)
8428 var->data.explicit_location = true;
8429
8430 var->data.explicit_xfb_buffer = fields[i].explicit_xfb_buffer;
8431 var->data.xfb_buffer = fields[i].xfb_buffer;
8432
8433 if (fields[i].offset != -1)
8434 var->data.explicit_xfb_offset = true;
8435 var->data.offset = fields[i].offset;
8436
8437 var->init_interface_type(block_type);
8438
8439 if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform)
8440 var->data.read_only = true;
8441
8442 /* Precision qualifiers do not have any meaning in Desktop GLSL */
8443 if (state->es_shader) {
8444 var->data.precision =
8445 select_gles_precision(fields[i].precision, fields[i].type,
8446 state, &loc);
8447 }
8448
8449 if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED) {
8450 var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED
8451 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout;
8452 } else {
8453 var->data.matrix_layout = fields[i].matrix_layout;
8454 }
8455
8456 if (var->data.mode == ir_var_shader_storage)
8457 apply_memory_qualifiers(var, fields[i]);
8458
8459 /* Examine var name here since var may get deleted in the next call */
8460 bool var_is_gl_id = is_gl_identifier(var->name);
8461
8462 if (redeclaring_per_vertex) {
8463 bool is_redeclaration;
8464 var =
8465 get_variable_being_redeclared(&var, loc, state,
8466 true /* allow_all_redeclarations */,
8467 &is_redeclaration);
8468 if (!var_is_gl_id || !is_redeclaration) {
8469 _mesa_glsl_error(&loc, state,
8470 "redeclaration of gl_PerVertex can only "
8471 "include built-in variables");
8472 } else if (var->data.how_declared == ir_var_declared_normally) {
8473 _mesa_glsl_error(&loc, state,
8474 "`%s' has already been redeclared",
8475 var->name);
8476 } else {
8477 var->data.how_declared = ir_var_declared_in_block;
8478 var->reinit_interface_type(block_type);
8479 }
8480 continue;
8481 }
8482
8483 if (state->symbols->get_variable(var->name) != NULL)
8484 _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
8485
8486 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
8487 * The UBO declaration itself doesn't get an ir_variable unless it
8488 * has an instance name. This is ugly.
8489 */
8490 if (this->layout.flags.q.explicit_binding) {
8491 apply_explicit_binding(state, &loc, var,
8492 var->get_interface_type(), &this->layout);
8493 }
8494
8495 if (var->type->is_unsized_array()) {
8496 if (var->is_in_shader_storage_block() &&
8497 is_unsized_array_last_element(var)) {
8498 var->data.from_ssbo_unsized_array = true;
8499 } else {
8500 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
8501 *
8502 * "If an array is declared as the last member of a shader storage
8503 * block and the size is not specified at compile-time, it is
8504 * sized at run-time. In all other cases, arrays are sized only
8505 * at compile-time."
8506 *
8507 * In desktop GLSL it is allowed to have unsized-arrays that are
8508 * not last, as long as we can determine that they are implicitly
8509 * sized.
8510 */
8511 if (state->es_shader) {
8512 _mesa_glsl_error(&loc, state, "unsized array `%s' "
8513 "definition: only last member of a shader "
8514 "storage block can be defined as unsized "
8515 "array", fields[i].name);
8516 }
8517 }
8518 }
8519
8520 state->symbols->add_variable(var);
8521 instructions->push_tail(var);
8522 }
8523
8524 if (redeclaring_per_vertex && block_type != earlier_per_vertex) {
8525 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
8526 *
8527 * It is also a compilation error ... to redeclare a built-in
8528 * block and then use a member from that built-in block that was
8529 * not included in the redeclaration.
8530 *
8531 * This appears to be a clarification to the behaviour established
8532 * for gl_PerVertex by GLSL 1.50, therefore we implement this
8533 * behaviour regardless of GLSL version.
8534 *
8535 * To prevent the shader from using a member that was not included in
8536 * the redeclaration, we disable any ir_variables that are still
8537 * associated with the old declaration of gl_PerVertex (since we've
8538 * already updated all of the variables contained in the new
8539 * gl_PerVertex to point to it).
8540 *
8541 * As a side effect this will prevent
8542 * validate_intrastage_interface_blocks() from getting confused and
8543 * thinking there are conflicting definitions of gl_PerVertex in the
8544 * shader.
8545 */
8546 foreach_in_list_safe(ir_instruction, node, instructions) {
8547 ir_variable *const var = node->as_variable();
8548 if (var != NULL &&
8549 var->get_interface_type() == earlier_per_vertex &&
8550 var->data.mode == var_mode) {
8551 if (var->data.how_declared == ir_var_declared_normally) {
8552 _mesa_glsl_error(&loc, state,
8553 "redeclaration of gl_PerVertex cannot "
8554 "follow a redeclaration of `%s'",
8555 var->name);
8556 }
8557 state->symbols->disable_variable(var->name);
8558 var->remove();
8559 }
8560 }
8561 }
8562 }
8563
8564 return NULL;
8565 }
8566
8567
8568 ir_rvalue *
8569 ast_tcs_output_layout::hir(exec_list *instructions,
8570 struct _mesa_glsl_parse_state *state)
8571 {
8572 YYLTYPE loc = this->get_location();
8573
8574 unsigned num_vertices;
8575 if (!state->out_qualifier->vertices->
8576 process_qualifier_constant(state, "vertices", &num_vertices,
8577 false)) {
8578 /* return here to stop cascading incorrect error messages */
8579 return NULL;
8580 }
8581
8582 /* If any shader outputs occurred before this declaration and specified an
8583 * array size, make sure the size they specified is consistent with the
8584 * primitive type.
8585 */
8586 if (state->tcs_output_size != 0 && state->tcs_output_size != num_vertices) {
8587 _mesa_glsl_error(&loc, state,
8588 "this tessellation control shader output layout "
8589 "specifies %u vertices, but a previous output "
8590 "is declared with size %u",
8591 num_vertices, state->tcs_output_size);
8592 return NULL;
8593 }
8594
8595 state->tcs_output_vertices_specified = true;
8596
8597 /* If any shader outputs occurred before this declaration and did not
8598 * specify an array size, their size is determined now.
8599 */
8600 foreach_in_list (ir_instruction, node, instructions) {
8601 ir_variable *var = node->as_variable();
8602 if (var == NULL || var->data.mode != ir_var_shader_out)
8603 continue;
8604
8605 /* Note: Not all tessellation control shader output are arrays. */
8606 if (!var->type->is_unsized_array() || var->data.patch)
8607 continue;
8608
8609 if (var->data.max_array_access >= (int)num_vertices) {
8610 _mesa_glsl_error(&loc, state,
8611 "this tessellation control shader output layout "
8612 "specifies %u vertices, but an access to element "
8613 "%u of output `%s' already exists", num_vertices,
8614 var->data.max_array_access, var->name);
8615 } else {
8616 var->type = glsl_type::get_array_instance(var->type->fields.array,
8617 num_vertices);
8618 }
8619 }
8620
8621 return NULL;
8622 }
8623
8624
8625 ir_rvalue *
8626 ast_gs_input_layout::hir(exec_list *instructions,
8627 struct _mesa_glsl_parse_state *state)
8628 {
8629 YYLTYPE loc = this->get_location();
8630
8631 /* Should have been prevented by the parser. */
8632 assert(!state->gs_input_prim_type_specified
8633 || state->in_qualifier->prim_type == this->prim_type);
8634
8635 /* If any shader inputs occurred before this declaration and specified an
8636 * array size, make sure the size they specified is consistent with the
8637 * primitive type.
8638 */
8639 unsigned num_vertices = vertices_per_prim(this->prim_type);
8640 if (state->gs_input_size != 0 && state->gs_input_size != num_vertices) {
8641 _mesa_glsl_error(&loc, state,
8642 "this geometry shader input layout implies %u vertices"
8643 " per primitive, but a previous input is declared"
8644 " with size %u", num_vertices, state->gs_input_size);
8645 return NULL;
8646 }
8647
8648 state->gs_input_prim_type_specified = true;
8649
8650 /* If any shader inputs occurred before this declaration and did not
8651 * specify an array size, their size is determined now.
8652 */
8653 foreach_in_list(ir_instruction, node, instructions) {
8654 ir_variable *var = node->as_variable();
8655 if (var == NULL || var->data.mode != ir_var_shader_in)
8656 continue;
8657
8658 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
8659 * array; skip it.
8660 */
8661
8662 if (var->type->is_unsized_array()) {
8663 if (var->data.max_array_access >= (int)num_vertices) {
8664 _mesa_glsl_error(&loc, state,
8665 "this geometry shader input layout implies %u"
8666 " vertices, but an access to element %u of input"
8667 " `%s' already exists", num_vertices,
8668 var->data.max_array_access, var->name);
8669 } else {
8670 var->type = glsl_type::get_array_instance(var->type->fields.array,
8671 num_vertices);
8672 }
8673 }
8674 }
8675
8676 return NULL;
8677 }
8678
8679
8680 ir_rvalue *
8681 ast_cs_input_layout::hir(exec_list *instructions,
8682 struct _mesa_glsl_parse_state *state)
8683 {
8684 YYLTYPE loc = this->get_location();
8685
8686 /* From the ARB_compute_shader specification:
8687 *
8688 * If the local size of the shader in any dimension is greater
8689 * than the maximum size supported by the implementation for that
8690 * dimension, a compile-time error results.
8691 *
8692 * It is not clear from the spec how the error should be reported if
8693 * the total size of the work group exceeds
8694 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
8695 * report it at compile time as well.
8696 */
8697 GLuint64 total_invocations = 1;
8698 unsigned qual_local_size[3];
8699 for (int i = 0; i < 3; i++) {
8700
8701 char *local_size_str = ralloc_asprintf(NULL, "invalid local_size_%c",
8702 'x' + i);
8703 /* Infer a local_size of 1 for unspecified dimensions */
8704 if (this->local_size[i] == NULL) {
8705 qual_local_size[i] = 1;
8706 } else if (!this->local_size[i]->
8707 process_qualifier_constant(state, local_size_str,
8708 &qual_local_size[i], false)) {
8709 ralloc_free(local_size_str);
8710 return NULL;
8711 }
8712 ralloc_free(local_size_str);
8713
8714 if (qual_local_size[i] > state->ctx->Const.MaxComputeWorkGroupSize[i]) {
8715 _mesa_glsl_error(&loc, state,
8716 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
8717 " (%d)", 'x' + i,
8718 state->ctx->Const.MaxComputeWorkGroupSize[i]);
8719 break;
8720 }
8721 total_invocations *= qual_local_size[i];
8722 if (total_invocations >
8723 state->ctx->Const.MaxComputeWorkGroupInvocations) {
8724 _mesa_glsl_error(&loc, state,
8725 "product of local_sizes exceeds "
8726 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
8727 state->ctx->Const.MaxComputeWorkGroupInvocations);
8728 break;
8729 }
8730 }
8731
8732 /* If any compute input layout declaration preceded this one, make sure it
8733 * was consistent with this one.
8734 */
8735 if (state->cs_input_local_size_specified) {
8736 for (int i = 0; i < 3; i++) {
8737 if (state->cs_input_local_size[i] != qual_local_size[i]) {
8738 _mesa_glsl_error(&loc, state,
8739 "compute shader input layout does not match"
8740 " previous declaration");
8741 return NULL;
8742 }
8743 }
8744 }
8745
8746 /* The ARB_compute_variable_group_size spec says:
8747 *
8748 * If a compute shader including a *local_size_variable* qualifier also
8749 * declares a fixed local group size using the *local_size_x*,
8750 * *local_size_y*, or *local_size_z* qualifiers, a compile-time error
8751 * results
8752 */
8753 if (state->cs_input_local_size_variable_specified) {
8754 _mesa_glsl_error(&loc, state,
8755 "compute shader can't include both a variable and a "
8756 "fixed local group size");
8757 return NULL;
8758 }
8759
8760 state->cs_input_local_size_specified = true;
8761 for (int i = 0; i < 3; i++)
8762 state->cs_input_local_size[i] = qual_local_size[i];
8763
8764 /* We may now declare the built-in constant gl_WorkGroupSize (see
8765 * builtin_variable_generator::generate_constants() for why we didn't
8766 * declare it earlier).
8767 */
8768 ir_variable *var = new(state->symbols)
8769 ir_variable(glsl_type::uvec3_type, "gl_WorkGroupSize", ir_var_auto);
8770 var->data.how_declared = ir_var_declared_implicitly;
8771 var->data.read_only = true;
8772 instructions->push_tail(var);
8773 state->symbols->add_variable(var);
8774 ir_constant_data data;
8775 memset(&data, 0, sizeof(data));
8776 for (int i = 0; i < 3; i++)
8777 data.u[i] = qual_local_size[i];
8778 var->constant_value = new(var) ir_constant(glsl_type::uvec3_type, &data);
8779 var->constant_initializer =
8780 new(var) ir_constant(glsl_type::uvec3_type, &data);
8781 var->data.has_initializer = true;
8782 var->data.is_implicit_initializer = false;
8783
8784 return NULL;
8785 }
8786
8787
8788 static void
8789 detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
8790 exec_list *instructions)
8791 {
8792 bool gl_FragColor_assigned = false;
8793 bool gl_FragData_assigned = false;
8794 bool gl_FragSecondaryColor_assigned = false;
8795 bool gl_FragSecondaryData_assigned = false;
8796 bool user_defined_fs_output_assigned = false;
8797 ir_variable *user_defined_fs_output = NULL;
8798
8799 /* It would be nice to have proper location information. */
8800 YYLTYPE loc;
8801 memset(&loc, 0, sizeof(loc));
8802
8803 foreach_in_list(ir_instruction, node, instructions) {
8804 ir_variable *var = node->as_variable();
8805
8806 if (!var || !var->data.assigned)
8807 continue;
8808
8809 if (strcmp(var->name, "gl_FragColor") == 0) {
8810 gl_FragColor_assigned = true;
8811 if (!var->constant_initializer && state->zero_init) {
8812 const ir_constant_data data = { { 0 } };
8813 var->data.has_initializer = true;
8814 var->data.is_implicit_initializer = true;
8815 var->constant_initializer = new(var) ir_constant(var->type, &data);
8816 }
8817 }
8818 else if (strcmp(var->name, "gl_FragData") == 0)
8819 gl_FragData_assigned = true;
8820 else if (strcmp(var->name, "gl_SecondaryFragColorEXT") == 0)
8821 gl_FragSecondaryColor_assigned = true;
8822 else if (strcmp(var->name, "gl_SecondaryFragDataEXT") == 0)
8823 gl_FragSecondaryData_assigned = true;
8824 else if (!is_gl_identifier(var->name)) {
8825 if (state->stage == MESA_SHADER_FRAGMENT &&
8826 var->data.mode == ir_var_shader_out) {
8827 user_defined_fs_output_assigned = true;
8828 user_defined_fs_output = var;
8829 }
8830 }
8831 }
8832
8833 /* From the GLSL 1.30 spec:
8834 *
8835 * "If a shader statically assigns a value to gl_FragColor, it
8836 * may not assign a value to any element of gl_FragData. If a
8837 * shader statically writes a value to any element of
8838 * gl_FragData, it may not assign a value to
8839 * gl_FragColor. That is, a shader may assign values to either
8840 * gl_FragColor or gl_FragData, but not both. Multiple shaders
8841 * linked together must also consistently write just one of
8842 * these variables. Similarly, if user declared output
8843 * variables are in use (statically assigned to), then the
8844 * built-in variables gl_FragColor and gl_FragData may not be
8845 * assigned to. These incorrect usages all generate compile
8846 * time errors."
8847 */
8848 if (gl_FragColor_assigned && gl_FragData_assigned) {
8849 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
8850 "`gl_FragColor' and `gl_FragData'");
8851 } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
8852 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
8853 "`gl_FragColor' and `%s'",
8854 user_defined_fs_output->name);
8855 } else if (gl_FragSecondaryColor_assigned && gl_FragSecondaryData_assigned) {
8856 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
8857 "`gl_FragSecondaryColorEXT' and"
8858 " `gl_FragSecondaryDataEXT'");
8859 } else if (gl_FragColor_assigned && gl_FragSecondaryData_assigned) {
8860 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
8861 "`gl_FragColor' and"
8862 " `gl_FragSecondaryDataEXT'");
8863 } else if (gl_FragData_assigned && gl_FragSecondaryColor_assigned) {
8864 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
8865 "`gl_FragData' and"
8866 " `gl_FragSecondaryColorEXT'");
8867 } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
8868 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
8869 "`gl_FragData' and `%s'",
8870 user_defined_fs_output->name);
8871 }
8872
8873 if ((gl_FragSecondaryColor_assigned || gl_FragSecondaryData_assigned) &&
8874 !state->EXT_blend_func_extended_enable) {
8875 _mesa_glsl_error(&loc, state,
8876 "Dual source blending requires EXT_blend_func_extended");
8877 }
8878 }
8879
8880 static void
8881 verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state *state)
8882 {
8883 YYLTYPE loc;
8884 memset(&loc, 0, sizeof(loc));
8885
8886 /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
8887 *
8888 * "A program will fail to compile or link if any shader
8889 * or stage contains two or more functions with the same
8890 * name if the name is associated with a subroutine type."
8891 */
8892
8893 for (int i = 0; i < state->num_subroutines; i++) {
8894 unsigned definitions = 0;
8895 ir_function *fn = state->subroutines[i];
8896 /* Calculate number of function definitions with the same name */
8897 foreach_in_list(ir_function_signature, sig, &fn->signatures) {
8898 if (sig->is_defined) {
8899 if (++definitions > 1) {
8900 _mesa_glsl_error(&loc, state,
8901 "%s shader contains two or more function "
8902 "definitions with name `%s', which is "
8903 "associated with a subroutine type.\n",
8904 _mesa_shader_stage_to_string(state->stage),
8905 fn->name);
8906 return;
8907 }
8908 }
8909 }
8910 }
8911 }
8912
8913 static void
8914 remove_per_vertex_blocks(exec_list *instructions,
8915 _mesa_glsl_parse_state *state, ir_variable_mode mode)
8916 {
8917 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
8918 * if it exists in this shader type.
8919 */
8920 const glsl_type *per_vertex = NULL;
8921 switch (mode) {
8922 case ir_var_shader_in:
8923 if (ir_variable *gl_in = state->symbols->get_variable("gl_in"))
8924 per_vertex = gl_in->get_interface_type();
8925 break;
8926 case ir_var_shader_out:
8927 if (ir_variable *gl_Position =
8928 state->symbols->get_variable("gl_Position")) {
8929 per_vertex = gl_Position->get_interface_type();
8930 }
8931 break;
8932 default:
8933 assert(!"Unexpected mode");
8934 break;
8935 }
8936
8937 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
8938 * need to do anything.
8939 */
8940 if (per_vertex == NULL)
8941 return;
8942
8943 /* If the interface block is used by the shader, then we don't need to do
8944 * anything.
8945 */
8946 interface_block_usage_visitor v(mode, per_vertex);
8947 v.run(instructions);
8948 if (v.usage_found())
8949 return;
8950
8951 /* Remove any ir_variable declarations that refer to the interface block
8952 * we're removing.
8953 */
8954 foreach_in_list_safe(ir_instruction, node, instructions) {
8955 ir_variable *const var = node->as_variable();
8956 if (var != NULL && var->get_interface_type() == per_vertex &&
8957 var->data.mode == mode) {
8958 state->symbols->disable_variable(var->name);
8959 var->remove();
8960 }
8961 }
8962 }
8963
8964 ir_rvalue *
8965 ast_warnings_toggle::hir(exec_list *,
8966 struct _mesa_glsl_parse_state *state)
8967 {
8968 state->warnings_enabled = enable;
8969 return NULL;
8970 }