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