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