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