glsl: Track explicit location in AST to IR translation
[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 "ir.h"
58
59 void
60 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
61 {
62 _mesa_glsl_initialize_variables(instructions, state);
63 _mesa_glsl_initialize_functions(instructions, state);
64
65 state->symbols->language_version = state->language_version;
66
67 state->current_function = NULL;
68
69 /* Section 4.2 of the GLSL 1.20 specification states:
70 * "The built-in functions are scoped in a scope outside the global scope
71 * users declare global variables in. That is, a shader's global scope,
72 * available for user-defined functions and global variables, is nested
73 * inside the scope containing the built-in functions."
74 *
75 * Since built-in functions like ftransform() access built-in variables,
76 * it follows that those must be in the outer scope as well.
77 *
78 * We push scope here to create this nesting effect...but don't pop.
79 * This way, a shader's globals are still in the symbol table for use
80 * by the linker.
81 */
82 state->symbols->push_scope();
83
84 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
85 ast->hir(instructions, state);
86 }
87
88
89 /**
90 * If a conversion is available, convert one operand to a different type
91 *
92 * The \c from \c ir_rvalue is converted "in place".
93 *
94 * \param to Type that the operand it to be converted to
95 * \param from Operand that is being converted
96 * \param state GLSL compiler state
97 *
98 * \return
99 * If a conversion is possible (or unnecessary), \c true is returned.
100 * Otherwise \c false is returned.
101 */
102 bool
103 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
104 struct _mesa_glsl_parse_state *state)
105 {
106 void *ctx = state;
107 if (to->base_type == from->type->base_type)
108 return true;
109
110 /* This conversion was added in GLSL 1.20. If the compilation mode is
111 * GLSL 1.10, the conversion is skipped.
112 */
113 if (state->language_version < 120)
114 return false;
115
116 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
117 *
118 * "There are no implicit array or structure conversions. For
119 * example, an array of int cannot be implicitly converted to an
120 * array of float. There are no implicit conversions between
121 * signed and unsigned integers."
122 */
123 /* FINISHME: The above comment is partially a lie. There is int/uint
124 * FINISHME: conversion for immediate constants.
125 */
126 if (!to->is_float() || !from->type->is_numeric())
127 return false;
128
129 /* Convert to a floating point type with the same number of components
130 * as the original type - i.e. int to float, not int to vec4.
131 */
132 to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
133 from->type->matrix_columns);
134
135 switch (from->type->base_type) {
136 case GLSL_TYPE_INT:
137 from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
138 break;
139 case GLSL_TYPE_UINT:
140 from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
141 break;
142 case GLSL_TYPE_BOOL:
143 from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
144 break;
145 default:
146 assert(0);
147 }
148
149 return true;
150 }
151
152
153 static const struct glsl_type *
154 arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
155 bool multiply,
156 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
157 {
158 const glsl_type *type_a = value_a->type;
159 const glsl_type *type_b = value_b->type;
160
161 /* From GLSL 1.50 spec, page 56:
162 *
163 * "The arithmetic binary operators add (+), subtract (-),
164 * multiply (*), and divide (/) operate on integer and
165 * floating-point scalars, vectors, and matrices."
166 */
167 if (!type_a->is_numeric() || !type_b->is_numeric()) {
168 _mesa_glsl_error(loc, state,
169 "Operands to arithmetic operators must be numeric");
170 return glsl_type::error_type;
171 }
172
173
174 /* "If one operand is floating-point based and the other is
175 * not, then the conversions from Section 4.1.10 "Implicit
176 * Conversions" are applied to the non-floating-point-based operand."
177 */
178 if (!apply_implicit_conversion(type_a, value_b, state)
179 && !apply_implicit_conversion(type_b, value_a, state)) {
180 _mesa_glsl_error(loc, state,
181 "Could not implicitly convert operands to "
182 "arithmetic operator");
183 return glsl_type::error_type;
184 }
185 type_a = value_a->type;
186 type_b = value_b->type;
187
188 /* "If the operands are integer types, they must both be signed or
189 * both be unsigned."
190 *
191 * From this rule and the preceeding conversion it can be inferred that
192 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
193 * The is_numeric check above already filtered out the case where either
194 * type is not one of these, so now the base types need only be tested for
195 * equality.
196 */
197 if (type_a->base_type != type_b->base_type) {
198 _mesa_glsl_error(loc, state,
199 "base type mismatch for arithmetic operator");
200 return glsl_type::error_type;
201 }
202
203 /* "All arithmetic binary operators result in the same fundamental type
204 * (signed integer, unsigned integer, or floating-point) as the
205 * operands they operate on, after operand type conversion. After
206 * conversion, the following cases are valid
207 *
208 * * The two operands are scalars. In this case the operation is
209 * applied, resulting in a scalar."
210 */
211 if (type_a->is_scalar() && type_b->is_scalar())
212 return type_a;
213
214 /* "* One operand is a scalar, and the other is a vector or matrix.
215 * In this case, the scalar operation is applied independently to each
216 * component of the vector or matrix, resulting in the same size
217 * vector or matrix."
218 */
219 if (type_a->is_scalar()) {
220 if (!type_b->is_scalar())
221 return type_b;
222 } else if (type_b->is_scalar()) {
223 return type_a;
224 }
225
226 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
227 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
228 * handled.
229 */
230 assert(!type_a->is_scalar());
231 assert(!type_b->is_scalar());
232
233 /* "* The two operands are vectors of the same size. In this case, the
234 * operation is done component-wise resulting in the same size
235 * vector."
236 */
237 if (type_a->is_vector() && type_b->is_vector()) {
238 if (type_a == type_b) {
239 return type_a;
240 } else {
241 _mesa_glsl_error(loc, state,
242 "vector size mismatch for arithmetic operator");
243 return glsl_type::error_type;
244 }
245 }
246
247 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
248 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
249 * <vector, vector> have been handled. At least one of the operands must
250 * be matrix. Further, since there are no integer matrix types, the base
251 * type of both operands must be float.
252 */
253 assert(type_a->is_matrix() || type_b->is_matrix());
254 assert(type_a->base_type == GLSL_TYPE_FLOAT);
255 assert(type_b->base_type == GLSL_TYPE_FLOAT);
256
257 /* "* The operator is add (+), subtract (-), or divide (/), and the
258 * operands are matrices with the same number of rows and the same
259 * number of columns. In this case, the operation is done component-
260 * wise resulting in the same size matrix."
261 * * The operator is multiply (*), where both operands are matrices or
262 * one operand is a vector and the other a matrix. A right vector
263 * operand is treated as a column vector and a left vector operand as a
264 * row vector. In all these cases, it is required that the number of
265 * columns of the left operand is equal to the number of rows of the
266 * right operand. Then, the multiply (*) operation does a linear
267 * algebraic multiply, yielding an object that has the same number of
268 * rows as the left operand and the same number of columns as the right
269 * operand. Section 5.10 "Vector and Matrix Operations" explains in
270 * more detail how vectors and matrices are operated on."
271 */
272 if (! multiply) {
273 if (type_a == type_b)
274 return type_a;
275 } else {
276 if (type_a->is_matrix() && type_b->is_matrix()) {
277 /* Matrix multiply. The columns of A must match the rows of B. Given
278 * the other previously tested constraints, this means the vector type
279 * of a row from A must be the same as the vector type of a column from
280 * B.
281 */
282 if (type_a->row_type() == type_b->column_type()) {
283 /* The resulting matrix has the number of columns of matrix B and
284 * the number of rows of matrix A. We get the row count of A by
285 * looking at the size of a vector that makes up a column. The
286 * transpose (size of a row) is done for B.
287 */
288 const glsl_type *const type =
289 glsl_type::get_instance(type_a->base_type,
290 type_a->column_type()->vector_elements,
291 type_b->row_type()->vector_elements);
292 assert(type != glsl_type::error_type);
293
294 return type;
295 }
296 } else if (type_a->is_matrix()) {
297 /* A is a matrix and B is a column vector. Columns of A must match
298 * rows of B. Given the other previously tested constraints, this
299 * means the vector type of a row from A must be the same as the
300 * vector the type of B.
301 */
302 if (type_a->row_type() == type_b) {
303 /* The resulting vector has a number of elements equal to
304 * the number of rows of matrix A. */
305 const glsl_type *const type =
306 glsl_type::get_instance(type_a->base_type,
307 type_a->column_type()->vector_elements,
308 1);
309 assert(type != glsl_type::error_type);
310
311 return type;
312 }
313 } else {
314 assert(type_b->is_matrix());
315
316 /* A is a row vector and B is a matrix. Columns of A must match rows
317 * of B. Given the other previously tested constraints, this means
318 * the type of A must be the same as the vector type of a column from
319 * B.
320 */
321 if (type_a == type_b->column_type()) {
322 /* The resulting vector has a number of elements equal to
323 * the number of columns of matrix B. */
324 const glsl_type *const type =
325 glsl_type::get_instance(type_a->base_type,
326 type_b->row_type()->vector_elements,
327 1);
328 assert(type != glsl_type::error_type);
329
330 return type;
331 }
332 }
333
334 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
335 return glsl_type::error_type;
336 }
337
338
339 /* "All other cases are illegal."
340 */
341 _mesa_glsl_error(loc, state, "type mismatch");
342 return glsl_type::error_type;
343 }
344
345
346 static const struct glsl_type *
347 unary_arithmetic_result_type(const struct glsl_type *type,
348 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
349 {
350 /* From GLSL 1.50 spec, page 57:
351 *
352 * "The arithmetic unary operators negate (-), post- and pre-increment
353 * and decrement (-- and ++) operate on integer or floating-point
354 * values (including vectors and matrices). All unary operators work
355 * component-wise on their operands. These result with the same type
356 * they operated on."
357 */
358 if (!type->is_numeric()) {
359 _mesa_glsl_error(loc, state,
360 "Operands to arithmetic operators must be numeric");
361 return glsl_type::error_type;
362 }
363
364 return type;
365 }
366
367
368 static const struct glsl_type *
369 modulus_result_type(const struct glsl_type *type_a,
370 const struct glsl_type *type_b,
371 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
372 {
373 /* From GLSL 1.50 spec, page 56:
374 * "The operator modulus (%) operates on signed or unsigned integers or
375 * integer vectors. The operand types must both be signed or both be
376 * unsigned."
377 */
378 if (!type_a->is_integer() || !type_b->is_integer()
379 || (type_a->base_type != type_b->base_type)) {
380 _mesa_glsl_error(loc, state, "type mismatch");
381 return glsl_type::error_type;
382 }
383
384 /* "The operands cannot be vectors of differing size. If one operand is
385 * a scalar and the other vector, then the scalar is applied component-
386 * wise to the vector, resulting in the same type as the vector. If both
387 * are vectors of the same size, the result is computed component-wise."
388 */
389 if (type_a->is_vector()) {
390 if (!type_b->is_vector()
391 || (type_a->vector_elements == type_b->vector_elements))
392 return type_a;
393 } else
394 return type_b;
395
396 /* "The operator modulus (%) is not defined for any other data types
397 * (non-integer types)."
398 */
399 _mesa_glsl_error(loc, state, "type mismatch");
400 return glsl_type::error_type;
401 }
402
403
404 static const struct glsl_type *
405 relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
406 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
407 {
408 const glsl_type *type_a = value_a->type;
409 const glsl_type *type_b = value_b->type;
410
411 /* From GLSL 1.50 spec, page 56:
412 * "The relational operators greater than (>), less than (<), greater
413 * than or equal (>=), and less than or equal (<=) operate only on
414 * scalar integer and scalar floating-point expressions."
415 */
416 if (!type_a->is_numeric()
417 || !type_b->is_numeric()
418 || !type_a->is_scalar()
419 || !type_b->is_scalar()) {
420 _mesa_glsl_error(loc, state,
421 "Operands to relational operators must be scalar and "
422 "numeric");
423 return glsl_type::error_type;
424 }
425
426 /* "Either the operands' types must match, or the conversions from
427 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
428 * operand, after which the types must match."
429 */
430 if (!apply_implicit_conversion(type_a, value_b, state)
431 && !apply_implicit_conversion(type_b, value_a, state)) {
432 _mesa_glsl_error(loc, state,
433 "Could not implicitly convert operands to "
434 "relational operator");
435 return glsl_type::error_type;
436 }
437 type_a = value_a->type;
438 type_b = value_b->type;
439
440 if (type_a->base_type != type_b->base_type) {
441 _mesa_glsl_error(loc, state, "base type mismatch");
442 return glsl_type::error_type;
443 }
444
445 /* "The result is scalar Boolean."
446 */
447 return glsl_type::bool_type;
448 }
449
450
451 /**
452 * Validates that a value can be assigned to a location with a specified type
453 *
454 * Validates that \c rhs can be assigned to some location. If the types are
455 * not an exact match but an automatic conversion is possible, \c rhs will be
456 * converted.
457 *
458 * \return
459 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
460 * Otherwise the actual RHS to be assigned will be returned. This may be
461 * \c rhs, or it may be \c rhs after some type conversion.
462 *
463 * \note
464 * In addition to being used for assignments, this function is used to
465 * type-check return values.
466 */
467 ir_rvalue *
468 validate_assignment(struct _mesa_glsl_parse_state *state,
469 const glsl_type *lhs_type, ir_rvalue *rhs)
470 {
471 const glsl_type *rhs_type = rhs->type;
472
473 /* If there is already some error in the RHS, just return it. Anything
474 * else will lead to an avalanche of error message back to the user.
475 */
476 if (rhs_type->is_error())
477 return rhs;
478
479 /* If the types are identical, the assignment can trivially proceed.
480 */
481 if (rhs_type == lhs_type)
482 return rhs;
483
484 /* If the array element types are the same and the size of the LHS is zero,
485 * the assignment is okay.
486 *
487 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
488 * is handled by ir_dereference::is_lvalue.
489 */
490 if (lhs_type->is_array() && rhs->type->is_array()
491 && (lhs_type->element_type() == rhs->type->element_type())
492 && (lhs_type->array_size() == 0)) {
493 return rhs;
494 }
495
496 /* Check for implicit conversion in GLSL 1.20 */
497 if (apply_implicit_conversion(lhs_type, rhs, state)) {
498 rhs_type = rhs->type;
499 if (rhs_type == lhs_type)
500 return rhs;
501 }
502
503 return NULL;
504 }
505
506 ir_rvalue *
507 do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
508 ir_rvalue *lhs, ir_rvalue *rhs,
509 YYLTYPE lhs_loc)
510 {
511 void *ctx = state;
512 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
513
514 if (!error_emitted) {
515 if (!lhs->is_lvalue()) {
516 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
517 error_emitted = true;
518 }
519
520 if (state->es_shader && lhs->type->is_array()) {
521 _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not "
522 "allowed in GLSL ES 1.00.");
523 error_emitted = true;
524 }
525 }
526
527 ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs);
528 if (new_rhs == NULL) {
529 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
530 } else {
531 rhs = new_rhs;
532
533 /* If the LHS array was not declared with a size, it takes it size from
534 * the RHS. If the LHS is an l-value and a whole array, it must be a
535 * dereference of a variable. Any other case would require that the LHS
536 * is either not an l-value or not a whole array.
537 */
538 if (lhs->type->array_size() == 0) {
539 ir_dereference *const d = lhs->as_dereference();
540
541 assert(d != NULL);
542
543 ir_variable *const var = d->variable_referenced();
544
545 assert(var != NULL);
546
547 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
548 /* FINISHME: This should actually log the location of the RHS. */
549 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
550 "previous access",
551 var->max_array_access);
552 }
553
554 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
555 rhs->type->array_size());
556 d->type = var->type;
557 }
558 }
559
560 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
561 * but not post_inc) need the converted assigned value as an rvalue
562 * to handle things like:
563 *
564 * i = j += 1;
565 *
566 * So we always just store the computed value being assigned to a
567 * temporary and return a deref of that temporary. If the rvalue
568 * ends up not being used, the temp will get copy-propagated out.
569 */
570 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
571 ir_var_temporary);
572 ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
573 instructions->push_tail(var);
574 instructions->push_tail(new(ctx) ir_assignment(deref_var,
575 rhs,
576 NULL));
577 deref_var = new(ctx) ir_dereference_variable(var);
578
579 if (!error_emitted)
580 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
581
582 return new(ctx) ir_dereference_variable(var);
583 }
584
585 static ir_rvalue *
586 get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
587 {
588 void *ctx = talloc_parent(lvalue);
589 ir_variable *var;
590
591 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
592 ir_var_temporary);
593 instructions->push_tail(var);
594 var->mode = ir_var_auto;
595
596 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
597 lvalue, NULL));
598
599 /* Once we've created this temporary, mark it read only so it's no
600 * longer considered an lvalue.
601 */
602 var->read_only = true;
603
604 return new(ctx) ir_dereference_variable(var);
605 }
606
607
608 ir_rvalue *
609 ast_node::hir(exec_list *instructions,
610 struct _mesa_glsl_parse_state *state)
611 {
612 (void) instructions;
613 (void) state;
614
615 return NULL;
616 }
617
618
619 ir_rvalue *
620 ast_expression::hir(exec_list *instructions,
621 struct _mesa_glsl_parse_state *state)
622 {
623 void *ctx = state;
624 static const int operations[AST_NUM_OPERATORS] = {
625 -1, /* ast_assign doesn't convert to ir_expression. */
626 -1, /* ast_plus doesn't convert to ir_expression. */
627 ir_unop_neg,
628 ir_binop_add,
629 ir_binop_sub,
630 ir_binop_mul,
631 ir_binop_div,
632 ir_binop_mod,
633 ir_binop_lshift,
634 ir_binop_rshift,
635 ir_binop_less,
636 ir_binop_greater,
637 ir_binop_lequal,
638 ir_binop_gequal,
639 ir_binop_all_equal,
640 ir_binop_any_nequal,
641 ir_binop_bit_and,
642 ir_binop_bit_xor,
643 ir_binop_bit_or,
644 ir_unop_bit_not,
645 ir_binop_logic_and,
646 ir_binop_logic_xor,
647 ir_binop_logic_or,
648 ir_unop_logic_not,
649
650 /* Note: The following block of expression types actually convert
651 * to multiple IR instructions.
652 */
653 ir_binop_mul, /* ast_mul_assign */
654 ir_binop_div, /* ast_div_assign */
655 ir_binop_mod, /* ast_mod_assign */
656 ir_binop_add, /* ast_add_assign */
657 ir_binop_sub, /* ast_sub_assign */
658 ir_binop_lshift, /* ast_ls_assign */
659 ir_binop_rshift, /* ast_rs_assign */
660 ir_binop_bit_and, /* ast_and_assign */
661 ir_binop_bit_xor, /* ast_xor_assign */
662 ir_binop_bit_or, /* ast_or_assign */
663
664 -1, /* ast_conditional doesn't convert to ir_expression. */
665 ir_binop_add, /* ast_pre_inc. */
666 ir_binop_sub, /* ast_pre_dec. */
667 ir_binop_add, /* ast_post_inc. */
668 ir_binop_sub, /* ast_post_dec. */
669 -1, /* ast_field_selection doesn't conv to ir_expression. */
670 -1, /* ast_array_index doesn't convert to ir_expression. */
671 -1, /* ast_function_call doesn't conv to ir_expression. */
672 -1, /* ast_identifier doesn't convert to ir_expression. */
673 -1, /* ast_int_constant doesn't convert to ir_expression. */
674 -1, /* ast_uint_constant doesn't conv to ir_expression. */
675 -1, /* ast_float_constant doesn't conv to ir_expression. */
676 -1, /* ast_bool_constant doesn't conv to ir_expression. */
677 -1, /* ast_sequence doesn't convert to ir_expression. */
678 };
679 ir_rvalue *result = NULL;
680 ir_rvalue *op[3];
681 const struct glsl_type *type = glsl_type::error_type;
682 bool error_emitted = false;
683 YYLTYPE loc;
684
685 loc = this->get_location();
686
687 switch (this->oper) {
688 case ast_assign: {
689 op[0] = this->subexpressions[0]->hir(instructions, state);
690 op[1] = this->subexpressions[1]->hir(instructions, state);
691
692 result = do_assignment(instructions, state, op[0], op[1],
693 this->subexpressions[0]->get_location());
694 error_emitted = result->type->is_error();
695 type = result->type;
696 break;
697 }
698
699 case ast_plus:
700 op[0] = this->subexpressions[0]->hir(instructions, state);
701
702 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
703
704 error_emitted = type->is_error();
705
706 result = op[0];
707 break;
708
709 case ast_neg:
710 op[0] = this->subexpressions[0]->hir(instructions, state);
711
712 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
713
714 error_emitted = type->is_error();
715
716 result = new(ctx) ir_expression(operations[this->oper], type,
717 op[0], NULL);
718 break;
719
720 case ast_add:
721 case ast_sub:
722 case ast_mul:
723 case ast_div:
724 op[0] = this->subexpressions[0]->hir(instructions, state);
725 op[1] = this->subexpressions[1]->hir(instructions, state);
726
727 type = arithmetic_result_type(op[0], op[1],
728 (this->oper == ast_mul),
729 state, & loc);
730 error_emitted = type->is_error();
731
732 result = new(ctx) ir_expression(operations[this->oper], type,
733 op[0], op[1]);
734 break;
735
736 case ast_mod:
737 op[0] = this->subexpressions[0]->hir(instructions, state);
738 op[1] = this->subexpressions[1]->hir(instructions, state);
739
740 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
741
742 assert(operations[this->oper] == ir_binop_mod);
743
744 result = new(ctx) ir_expression(operations[this->oper], type,
745 op[0], op[1]);
746 error_emitted = type->is_error();
747 break;
748
749 case ast_lshift:
750 case ast_rshift:
751 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
752 error_emitted = true;
753 break;
754
755 case ast_less:
756 case ast_greater:
757 case ast_lequal:
758 case ast_gequal:
759 op[0] = this->subexpressions[0]->hir(instructions, state);
760 op[1] = this->subexpressions[1]->hir(instructions, state);
761
762 type = relational_result_type(op[0], op[1], state, & loc);
763
764 /* The relational operators must either generate an error or result
765 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
766 */
767 assert(type->is_error()
768 || ((type->base_type == GLSL_TYPE_BOOL)
769 && type->is_scalar()));
770
771 result = new(ctx) ir_expression(operations[this->oper], type,
772 op[0], op[1]);
773 error_emitted = type->is_error();
774 break;
775
776 case ast_nequal:
777 case ast_equal:
778 op[0] = this->subexpressions[0]->hir(instructions, state);
779 op[1] = this->subexpressions[1]->hir(instructions, state);
780
781 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
782 *
783 * "The equality operators equal (==), and not equal (!=)
784 * operate on all types. They result in a scalar Boolean. If
785 * the operand types do not match, then there must be a
786 * conversion from Section 4.1.10 "Implicit Conversions"
787 * applied to one operand that can make them match, in which
788 * case this conversion is done."
789 */
790 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
791 && !apply_implicit_conversion(op[1]->type, op[0], state))
792 || (op[0]->type != op[1]->type)) {
793 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
794 "type", (this->oper == ast_equal) ? "==" : "!=");
795 error_emitted = true;
796 } else if ((state->language_version <= 110)
797 && (op[0]->type->is_array() || op[1]->type->is_array())) {
798 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
799 "GLSL 1.10");
800 error_emitted = true;
801 }
802
803 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
804 op[0], op[1]);
805 type = glsl_type::bool_type;
806
807 assert(result->type == glsl_type::bool_type);
808 break;
809
810 case ast_bit_and:
811 case ast_bit_xor:
812 case ast_bit_or:
813 op[0] = this->subexpressions[0]->hir(instructions, state);
814 op[1] = this->subexpressions[1]->hir(instructions, state);
815
816 if (state->language_version < 130) {
817 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
818 error_emitted = true;
819 }
820
821 if (!op[0]->type->is_integer()) {
822 _mesa_glsl_error(&loc, state, "LHS of `%s' must be an integer",
823 operator_string(this->oper));
824 error_emitted = true;
825 }
826
827 if (!op[1]->type->is_integer()) {
828 _mesa_glsl_error(&loc, state, "RHS of `%s' must be an integer",
829 operator_string(this->oper));
830 error_emitted = true;
831 }
832
833 if (op[0]->type->base_type != op[1]->type->base_type) {
834 _mesa_glsl_error(&loc, state, "operands of `%s' must have the same "
835 "base type", operator_string(this->oper));
836 error_emitted = true;
837 }
838
839 if (op[0]->type->is_vector() && op[1]->type->is_vector()
840 && op[0]->type->vector_elements != op[1]->type->vector_elements) {
841 _mesa_glsl_error(&loc, state, "operands of `%s' cannot be vectors of "
842 "different sizes", operator_string(this->oper));
843 error_emitted = true;
844 }
845
846 type = op[0]->type->is_scalar() ? op[1]->type : op[0]->type;
847 result = new(ctx) ir_expression(operations[this->oper], type,
848 op[0], op[1]);
849 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
850 break;
851
852 case ast_bit_not:
853 op[0] = this->subexpressions[0]->hir(instructions, state);
854
855 if (state->language_version < 130) {
856 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
857 error_emitted = true;
858 }
859
860 if (!op[0]->type->is_integer()) {
861 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
862 error_emitted = true;
863 }
864
865 type = op[0]->type;
866 result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
867 break;
868
869 case ast_logic_and: {
870 op[0] = this->subexpressions[0]->hir(instructions, state);
871
872 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
873 YYLTYPE loc = this->subexpressions[0]->get_location();
874
875 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
876 operator_string(this->oper));
877 error_emitted = true;
878 }
879
880 ir_constant *op0_const = op[0]->constant_expression_value();
881 if (op0_const) {
882 if (op0_const->value.b[0]) {
883 op[1] = this->subexpressions[1]->hir(instructions, state);
884
885 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
886 YYLTYPE loc = this->subexpressions[1]->get_location();
887
888 _mesa_glsl_error(& loc, state,
889 "RHS of `%s' must be scalar boolean",
890 operator_string(this->oper));
891 error_emitted = true;
892 }
893 result = op[1];
894 } else {
895 result = op0_const;
896 }
897 type = glsl_type::bool_type;
898 } else {
899 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
900 "and_tmp",
901 ir_var_temporary);
902 instructions->push_tail(tmp);
903
904 ir_if *const stmt = new(ctx) ir_if(op[0]);
905 instructions->push_tail(stmt);
906
907 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
908
909 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
910 YYLTYPE loc = this->subexpressions[1]->get_location();
911
912 _mesa_glsl_error(& loc, state,
913 "RHS of `%s' must be scalar boolean",
914 operator_string(this->oper));
915 error_emitted = true;
916 }
917
918 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
919 ir_assignment *const then_assign =
920 new(ctx) ir_assignment(then_deref, op[1], NULL);
921 stmt->then_instructions.push_tail(then_assign);
922
923 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
924 ir_assignment *const else_assign =
925 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
926 stmt->else_instructions.push_tail(else_assign);
927
928 result = new(ctx) ir_dereference_variable(tmp);
929 type = tmp->type;
930 }
931 break;
932 }
933
934 case ast_logic_or: {
935 op[0] = this->subexpressions[0]->hir(instructions, state);
936
937 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
938 YYLTYPE loc = this->subexpressions[0]->get_location();
939
940 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
941 operator_string(this->oper));
942 error_emitted = true;
943 }
944
945 ir_constant *op0_const = op[0]->constant_expression_value();
946 if (op0_const) {
947 if (op0_const->value.b[0]) {
948 result = op0_const;
949 } else {
950 op[1] = this->subexpressions[1]->hir(instructions, state);
951
952 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
953 YYLTYPE loc = this->subexpressions[1]->get_location();
954
955 _mesa_glsl_error(& loc, state,
956 "RHS of `%s' must be scalar boolean",
957 operator_string(this->oper));
958 error_emitted = true;
959 }
960 result = op[1];
961 }
962 type = glsl_type::bool_type;
963 } else {
964 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
965 "or_tmp",
966 ir_var_temporary);
967 instructions->push_tail(tmp);
968
969 ir_if *const stmt = new(ctx) ir_if(op[0]);
970 instructions->push_tail(stmt);
971
972 op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state);
973
974 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
975 YYLTYPE loc = this->subexpressions[1]->get_location();
976
977 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
978 operator_string(this->oper));
979 error_emitted = true;
980 }
981
982 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
983 ir_assignment *const then_assign =
984 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
985 stmt->then_instructions.push_tail(then_assign);
986
987 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
988 ir_assignment *const else_assign =
989 new(ctx) ir_assignment(else_deref, op[1], NULL);
990 stmt->else_instructions.push_tail(else_assign);
991
992 result = new(ctx) ir_dereference_variable(tmp);
993 type = tmp->type;
994 }
995 break;
996 }
997
998 case ast_logic_xor:
999 op[0] = this->subexpressions[0]->hir(instructions, state);
1000 op[1] = this->subexpressions[1]->hir(instructions, state);
1001
1002
1003 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1004 op[0], op[1]);
1005 type = glsl_type::bool_type;
1006 break;
1007
1008 case ast_logic_not:
1009 op[0] = this->subexpressions[0]->hir(instructions, state);
1010
1011 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1012 YYLTYPE loc = this->subexpressions[0]->get_location();
1013
1014 _mesa_glsl_error(& loc, state,
1015 "operand of `!' must be scalar boolean");
1016 error_emitted = true;
1017 }
1018
1019 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1020 op[0], NULL);
1021 type = glsl_type::bool_type;
1022 break;
1023
1024 case ast_mul_assign:
1025 case ast_div_assign:
1026 case ast_add_assign:
1027 case ast_sub_assign: {
1028 op[0] = this->subexpressions[0]->hir(instructions, state);
1029 op[1] = this->subexpressions[1]->hir(instructions, state);
1030
1031 type = arithmetic_result_type(op[0], op[1],
1032 (this->oper == ast_mul_assign),
1033 state, & loc);
1034
1035 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1036 op[0], op[1]);
1037
1038 result = do_assignment(instructions, state,
1039 op[0]->clone(ctx, NULL), temp_rhs,
1040 this->subexpressions[0]->get_location());
1041 type = result->type;
1042 error_emitted = (op[0]->type->is_error());
1043
1044 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1045 * explicitly test for this because none of the binary expression
1046 * operators allow array operands either.
1047 */
1048
1049 break;
1050 }
1051
1052 case ast_mod_assign: {
1053 op[0] = this->subexpressions[0]->hir(instructions, state);
1054 op[1] = this->subexpressions[1]->hir(instructions, state);
1055
1056 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
1057
1058 assert(operations[this->oper] == ir_binop_mod);
1059
1060 ir_rvalue *temp_rhs;
1061 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1062 op[0], op[1]);
1063
1064 result = do_assignment(instructions, state,
1065 op[0]->clone(ctx, NULL), temp_rhs,
1066 this->subexpressions[0]->get_location());
1067 type = result->type;
1068 error_emitted = type->is_error();
1069 break;
1070 }
1071
1072 case ast_ls_assign:
1073 case ast_rs_assign:
1074 _mesa_glsl_error(& loc, state,
1075 "FINISHME: implement bit-shift assignment operators");
1076 error_emitted = true;
1077 break;
1078
1079 case ast_and_assign:
1080 case ast_xor_assign:
1081 case ast_or_assign:
1082 _mesa_glsl_error(& loc, state,
1083 "FINISHME: implement logic assignment operators");
1084 error_emitted = true;
1085 break;
1086
1087 case ast_conditional: {
1088 op[0] = this->subexpressions[0]->hir(instructions, state);
1089
1090 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1091 *
1092 * "The ternary selection operator (?:). It operates on three
1093 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1094 * first expression, which must result in a scalar Boolean."
1095 */
1096 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1097 YYLTYPE loc = this->subexpressions[0]->get_location();
1098
1099 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
1100 error_emitted = true;
1101 }
1102
1103 /* The :? operator is implemented by generating an anonymous temporary
1104 * followed by an if-statement. The last instruction in each branch of
1105 * the if-statement assigns a value to the anonymous temporary. This
1106 * temporary is the r-value of the expression.
1107 */
1108 exec_list then_instructions;
1109 exec_list else_instructions;
1110
1111 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1112 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
1113
1114 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1115 *
1116 * "The second and third expressions can be any type, as
1117 * long their types match, or there is a conversion in
1118 * Section 4.1.10 "Implicit Conversions" that can be applied
1119 * to one of the expressions to make their types match. This
1120 * resulting matching type is the type of the entire
1121 * expression."
1122 */
1123 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1124 && !apply_implicit_conversion(op[2]->type, op[1], state))
1125 || (op[1]->type != op[2]->type)) {
1126 YYLTYPE loc = this->subexpressions[1]->get_location();
1127
1128 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1129 "operator must have matching types.");
1130 error_emitted = true;
1131 type = glsl_type::error_type;
1132 } else {
1133 type = op[1]->type;
1134 }
1135
1136 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1137 *
1138 * "The second and third expressions must be the same type, but can
1139 * be of any type other than an array."
1140 */
1141 if ((state->language_version <= 110) && type->is_array()) {
1142 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1143 "operator must not be arrays.");
1144 error_emitted = true;
1145 }
1146
1147 ir_constant *cond_val = op[0]->constant_expression_value();
1148 ir_constant *then_val = op[1]->constant_expression_value();
1149 ir_constant *else_val = op[2]->constant_expression_value();
1150
1151 if (then_instructions.is_empty()
1152 && else_instructions.is_empty()
1153 && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
1154 result = (cond_val->value.b[0]) ? then_val : else_val;
1155 } else {
1156 ir_variable *const tmp =
1157 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
1158 instructions->push_tail(tmp);
1159
1160 ir_if *const stmt = new(ctx) ir_if(op[0]);
1161 instructions->push_tail(stmt);
1162
1163 then_instructions.move_nodes_to(& stmt->then_instructions);
1164 ir_dereference *const then_deref =
1165 new(ctx) ir_dereference_variable(tmp);
1166 ir_assignment *const then_assign =
1167 new(ctx) ir_assignment(then_deref, op[1], NULL);
1168 stmt->then_instructions.push_tail(then_assign);
1169
1170 else_instructions.move_nodes_to(& stmt->else_instructions);
1171 ir_dereference *const else_deref =
1172 new(ctx) ir_dereference_variable(tmp);
1173 ir_assignment *const else_assign =
1174 new(ctx) ir_assignment(else_deref, op[2], NULL);
1175 stmt->else_instructions.push_tail(else_assign);
1176
1177 result = new(ctx) ir_dereference_variable(tmp);
1178 }
1179 break;
1180 }
1181
1182 case ast_pre_inc:
1183 case ast_pre_dec: {
1184 op[0] = this->subexpressions[0]->hir(instructions, state);
1185 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
1186 op[1] = new(ctx) ir_constant(1.0f);
1187 else
1188 op[1] = new(ctx) ir_constant(1);
1189
1190 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1191
1192 ir_rvalue *temp_rhs;
1193 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1194 op[0], op[1]);
1195
1196 result = do_assignment(instructions, state,
1197 op[0]->clone(ctx, NULL), temp_rhs,
1198 this->subexpressions[0]->get_location());
1199 type = result->type;
1200 error_emitted = op[0]->type->is_error();
1201 break;
1202 }
1203
1204 case ast_post_inc:
1205 case ast_post_dec: {
1206 op[0] = this->subexpressions[0]->hir(instructions, state);
1207 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
1208 op[1] = new(ctx) ir_constant(1.0f);
1209 else
1210 op[1] = new(ctx) ir_constant(1);
1211
1212 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1213
1214 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1215
1216 ir_rvalue *temp_rhs;
1217 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1218 op[0], op[1]);
1219
1220 /* Get a temporary of a copy of the lvalue before it's modified.
1221 * This may get thrown away later.
1222 */
1223 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1224
1225 (void)do_assignment(instructions, state,
1226 op[0]->clone(ctx, NULL), temp_rhs,
1227 this->subexpressions[0]->get_location());
1228
1229 type = result->type;
1230 error_emitted = op[0]->type->is_error();
1231 break;
1232 }
1233
1234 case ast_field_selection:
1235 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1236 type = result->type;
1237 break;
1238
1239 case ast_array_index: {
1240 YYLTYPE index_loc = subexpressions[1]->get_location();
1241
1242 op[0] = subexpressions[0]->hir(instructions, state);
1243 op[1] = subexpressions[1]->hir(instructions, state);
1244
1245 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1246
1247 ir_rvalue *const array = op[0];
1248
1249 result = new(ctx) ir_dereference_array(op[0], op[1]);
1250
1251 /* Do not use op[0] after this point. Use array.
1252 */
1253 op[0] = NULL;
1254
1255
1256 if (error_emitted)
1257 break;
1258
1259 if (!array->type->is_array()
1260 && !array->type->is_matrix()
1261 && !array->type->is_vector()) {
1262 _mesa_glsl_error(& index_loc, state,
1263 "cannot dereference non-array / non-matrix / "
1264 "non-vector");
1265 error_emitted = true;
1266 }
1267
1268 if (!op[1]->type->is_integer()) {
1269 _mesa_glsl_error(& index_loc, state,
1270 "array index must be integer type");
1271 error_emitted = true;
1272 } else if (!op[1]->type->is_scalar()) {
1273 _mesa_glsl_error(& index_loc, state,
1274 "array index must be scalar");
1275 error_emitted = true;
1276 }
1277
1278 /* If the array index is a constant expression and the array has a
1279 * declared size, ensure that the access is in-bounds. If the array
1280 * index is not a constant expression, ensure that the array has a
1281 * declared size.
1282 */
1283 ir_constant *const const_index = op[1]->constant_expression_value();
1284 if (const_index != NULL) {
1285 const int idx = const_index->value.i[0];
1286 const char *type_name;
1287 unsigned bound = 0;
1288
1289 if (array->type->is_matrix()) {
1290 type_name = "matrix";
1291 } else if (array->type->is_vector()) {
1292 type_name = "vector";
1293 } else {
1294 type_name = "array";
1295 }
1296
1297 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1298 *
1299 * "It is illegal to declare an array with a size, and then
1300 * later (in the same shader) index the same array with an
1301 * integral constant expression greater than or equal to the
1302 * declared size. It is also illegal to index an array with a
1303 * negative constant expression."
1304 */
1305 if (array->type->is_matrix()) {
1306 if (array->type->row_type()->vector_elements <= idx) {
1307 bound = array->type->row_type()->vector_elements;
1308 }
1309 } else if (array->type->is_vector()) {
1310 if (array->type->vector_elements <= idx) {
1311 bound = array->type->vector_elements;
1312 }
1313 } else {
1314 if ((array->type->array_size() > 0)
1315 && (array->type->array_size() <= idx)) {
1316 bound = array->type->array_size();
1317 }
1318 }
1319
1320 if (bound > 0) {
1321 _mesa_glsl_error(& loc, state, "%s index must be < %u",
1322 type_name, bound);
1323 error_emitted = true;
1324 } else if (idx < 0) {
1325 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
1326 type_name);
1327 error_emitted = true;
1328 }
1329
1330 if (array->type->is_array()) {
1331 /* If the array is a variable dereference, it dereferences the
1332 * whole array, by definition. Use this to get the variable.
1333 *
1334 * FINISHME: Should some methods for getting / setting / testing
1335 * FINISHME: array access limits be added to ir_dereference?
1336 */
1337 ir_variable *const v = array->whole_variable_referenced();
1338 if ((v != NULL) && (unsigned(idx) > v->max_array_access))
1339 v->max_array_access = idx;
1340 }
1341 } else if (array->type->array_size() == 0) {
1342 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
1343 } else {
1344 if (array->type->is_array()) {
1345 /* whole_variable_referenced can return NULL if the array is a
1346 * member of a structure. In this case it is safe to not update
1347 * the max_array_access field because it is never used for fields
1348 * of structures.
1349 */
1350 ir_variable *v = array->whole_variable_referenced();
1351 if (v != NULL)
1352 v->max_array_access = array->type->array_size();
1353 }
1354 }
1355
1356 if (error_emitted)
1357 result->type = glsl_type::error_type;
1358
1359 type = result->type;
1360 break;
1361 }
1362
1363 case ast_function_call:
1364 /* Should *NEVER* get here. ast_function_call should always be handled
1365 * by ast_function_expression::hir.
1366 */
1367 assert(0);
1368 break;
1369
1370 case ast_identifier: {
1371 /* ast_identifier can appear several places in a full abstract syntax
1372 * tree. This particular use must be at location specified in the grammar
1373 * as 'variable_identifier'.
1374 */
1375 ir_variable *var =
1376 state->symbols->get_variable(this->primary_expression.identifier);
1377
1378 result = new(ctx) ir_dereference_variable(var);
1379
1380 if (var != NULL) {
1381 type = result->type;
1382 } else {
1383 _mesa_glsl_error(& loc, state, "`%s' undeclared",
1384 this->primary_expression.identifier);
1385
1386 error_emitted = true;
1387 }
1388 break;
1389 }
1390
1391 case ast_int_constant:
1392 type = glsl_type::int_type;
1393 result = new(ctx) ir_constant(this->primary_expression.int_constant);
1394 break;
1395
1396 case ast_uint_constant:
1397 type = glsl_type::uint_type;
1398 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1399 break;
1400
1401 case ast_float_constant:
1402 type = glsl_type::float_type;
1403 result = new(ctx) ir_constant(this->primary_expression.float_constant);
1404 break;
1405
1406 case ast_bool_constant:
1407 type = glsl_type::bool_type;
1408 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1409 break;
1410
1411 case ast_sequence: {
1412 /* It should not be possible to generate a sequence in the AST without
1413 * any expressions in it.
1414 */
1415 assert(!this->expressions.is_empty());
1416
1417 /* The r-value of a sequence is the last expression in the sequence. If
1418 * the other expressions in the sequence do not have side-effects (and
1419 * therefore add instructions to the instruction list), they get dropped
1420 * on the floor.
1421 */
1422 foreach_list_typed (ast_node, ast, link, &this->expressions)
1423 result = ast->hir(instructions, state);
1424
1425 type = result->type;
1426
1427 /* Any errors should have already been emitted in the loop above.
1428 */
1429 error_emitted = true;
1430 break;
1431 }
1432 }
1433
1434 if (type->is_error() && !error_emitted)
1435 _mesa_glsl_error(& loc, state, "type mismatch");
1436
1437 return result;
1438 }
1439
1440
1441 ir_rvalue *
1442 ast_expression_statement::hir(exec_list *instructions,
1443 struct _mesa_glsl_parse_state *state)
1444 {
1445 /* It is possible to have expression statements that don't have an
1446 * expression. This is the solitary semicolon:
1447 *
1448 * for (i = 0; i < 5; i++)
1449 * ;
1450 *
1451 * In this case the expression will be NULL. Test for NULL and don't do
1452 * anything in that case.
1453 */
1454 if (expression != NULL)
1455 expression->hir(instructions, state);
1456
1457 /* Statements do not have r-values.
1458 */
1459 return NULL;
1460 }
1461
1462
1463 ir_rvalue *
1464 ast_compound_statement::hir(exec_list *instructions,
1465 struct _mesa_glsl_parse_state *state)
1466 {
1467 if (new_scope)
1468 state->symbols->push_scope();
1469
1470 foreach_list_typed (ast_node, ast, link, &this->statements)
1471 ast->hir(instructions, state);
1472
1473 if (new_scope)
1474 state->symbols->pop_scope();
1475
1476 /* Compound statements do not have r-values.
1477 */
1478 return NULL;
1479 }
1480
1481
1482 static const glsl_type *
1483 process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
1484 struct _mesa_glsl_parse_state *state)
1485 {
1486 unsigned length = 0;
1487
1488 /* FINISHME: Reject delcarations of multidimensional arrays. */
1489
1490 if (array_size != NULL) {
1491 exec_list dummy_instructions;
1492 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1493 YYLTYPE loc = array_size->get_location();
1494
1495 /* FINISHME: Verify that the grammar forbids side-effects in array
1496 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1497 */
1498 assert(dummy_instructions.is_empty());
1499
1500 if (ir != NULL) {
1501 if (!ir->type->is_integer()) {
1502 _mesa_glsl_error(& loc, state, "array size must be integer type");
1503 } else if (!ir->type->is_scalar()) {
1504 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1505 } else {
1506 ir_constant *const size = ir->constant_expression_value();
1507
1508 if (size == NULL) {
1509 _mesa_glsl_error(& loc, state, "array size must be a "
1510 "constant valued expression");
1511 } else if (size->value.i[0] <= 0) {
1512 _mesa_glsl_error(& loc, state, "array size must be > 0");
1513 } else {
1514 assert(size->type == ir->type);
1515 length = size->value.u[0];
1516 }
1517 }
1518 }
1519 } else if (state->es_shader) {
1520 /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
1521 * array declarations have been removed from the language.
1522 */
1523 _mesa_glsl_error(loc, state, "unsized array declarations are not "
1524 "allowed in GLSL ES 1.00.");
1525 }
1526
1527 return glsl_type::get_array_instance(base, length);
1528 }
1529
1530
1531 const glsl_type *
1532 ast_type_specifier::glsl_type(const char **name,
1533 struct _mesa_glsl_parse_state *state) const
1534 {
1535 const struct glsl_type *type;
1536
1537 type = state->symbols->get_type(this->type_name);
1538 *name = this->type_name;
1539
1540 if (this->is_array) {
1541 YYLTYPE loc = this->get_location();
1542 type = process_array_type(&loc, type, this->array_size, state);
1543 }
1544
1545 return type;
1546 }
1547
1548
1549 static void
1550 apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1551 ir_variable *var,
1552 struct _mesa_glsl_parse_state *state,
1553 YYLTYPE *loc)
1554 {
1555 if (qual->flags.q.invariant)
1556 var->invariant = 1;
1557
1558 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1559 if (qual->flags.q.constant || qual->flags.q.attribute
1560 || qual->flags.q.uniform
1561 || (qual->flags.q.varying && (state->target == fragment_shader)))
1562 var->read_only = 1;
1563
1564 if (qual->flags.q.centroid)
1565 var->centroid = 1;
1566
1567 if (qual->flags.q.attribute && state->target != vertex_shader) {
1568 var->type = glsl_type::error_type;
1569 _mesa_glsl_error(loc, state,
1570 "`attribute' variables may not be declared in the "
1571 "%s shader",
1572 _mesa_glsl_shader_target_name(state->target));
1573 }
1574
1575 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1576 *
1577 * "The varying qualifier can be used only with the data types
1578 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1579 * these."
1580 */
1581 if (qual->flags.q.varying) {
1582 const glsl_type *non_array_type;
1583
1584 if (var->type && var->type->is_array())
1585 non_array_type = var->type->fields.array;
1586 else
1587 non_array_type = var->type;
1588
1589 if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
1590 var->type = glsl_type::error_type;
1591 _mesa_glsl_error(loc, state,
1592 "varying variables must be of base type float");
1593 }
1594 }
1595
1596 /* If there is no qualifier that changes the mode of the variable, leave
1597 * the setting alone.
1598 */
1599 if (qual->flags.q.in && qual->flags.q.out)
1600 var->mode = ir_var_inout;
1601 else if (qual->flags.q.attribute || qual->flags.q.in
1602 || (qual->flags.q.varying && (state->target == fragment_shader)))
1603 var->mode = ir_var_in;
1604 else if (qual->flags.q.out
1605 || (qual->flags.q.varying && (state->target == vertex_shader)))
1606 var->mode = ir_var_out;
1607 else if (qual->flags.q.uniform)
1608 var->mode = ir_var_uniform;
1609
1610 if (qual->flags.q.flat)
1611 var->interpolation = ir_var_flat;
1612 else if (qual->flags.q.noperspective)
1613 var->interpolation = ir_var_noperspective;
1614 else
1615 var->interpolation = ir_var_smooth;
1616
1617 var->pixel_center_integer = qual->flags.q.pixel_center_integer;
1618 var->origin_upper_left = qual->flags.q.origin_upper_left;
1619 if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
1620 && (strcmp(var->name, "gl_FragCoord") != 0)) {
1621 const char *const qual_string = (qual->flags.q.origin_upper_left)
1622 ? "origin_upper_left" : "pixel_center_integer";
1623
1624 _mesa_glsl_error(loc, state,
1625 "layout qualifier `%s' can only be applied to "
1626 "fragment shader input `gl_FragCoord'",
1627 qual_string);
1628 }
1629
1630 if (qual->flags.q.explicit_location) {
1631 const bool global_scope = (state->current_function == NULL);
1632 bool fail = false;
1633 const char *string = "";
1634
1635 /* In the vertex shader only shader inputs can be given explicit
1636 * locations.
1637 *
1638 * In the fragment shader only shader outputs can be given explicit
1639 * locations.
1640 */
1641 switch (state->target) {
1642 case vertex_shader:
1643 if (!global_scope || (var->mode != ir_var_in)) {
1644 fail = true;
1645 string = "input";
1646 }
1647 break;
1648
1649 case geometry_shader:
1650 _mesa_glsl_error(loc, state,
1651 "geometry shader variables cannot be given "
1652 "explicit locations\n");
1653 break;
1654
1655 case fragment_shader:
1656 if (!global_scope || (var->mode != ir_var_in)) {
1657 fail = true;
1658 string = "output";
1659 }
1660 break;
1661 }
1662
1663 if (fail) {
1664 _mesa_glsl_error(loc, state,
1665 "only %s shader %s variables can be given an "
1666 "explicit location\n",
1667 _mesa_glsl_shader_target_name(state->target),
1668 string);
1669 } else {
1670 var->explicit_location = true;
1671 var->location = (state->target == vertex_shader)
1672 ? (qual->location + VERT_ATTRIB_GENERIC0)
1673 : (qual->location + FRAG_RESULT_DATA0);
1674 }
1675 }
1676
1677 if (var->type->is_array() && state->language_version != 110) {
1678 var->array_lvalue = true;
1679 }
1680 }
1681
1682
1683 ir_rvalue *
1684 ast_declarator_list::hir(exec_list *instructions,
1685 struct _mesa_glsl_parse_state *state)
1686 {
1687 void *ctx = state;
1688 const struct glsl_type *decl_type;
1689 const char *type_name = NULL;
1690 ir_rvalue *result = NULL;
1691 YYLTYPE loc = this->get_location();
1692
1693 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
1694 *
1695 * "To ensure that a particular output variable is invariant, it is
1696 * necessary to use the invariant qualifier. It can either be used to
1697 * qualify a previously declared variable as being invariant
1698 *
1699 * invariant gl_Position; // make existing gl_Position be invariant"
1700 *
1701 * In these cases the parser will set the 'invariant' flag in the declarator
1702 * list, and the type will be NULL.
1703 */
1704 if (this->invariant) {
1705 assert(this->type == NULL);
1706
1707 if (state->current_function != NULL) {
1708 _mesa_glsl_error(& loc, state,
1709 "All uses of `invariant' keyword must be at global "
1710 "scope\n");
1711 }
1712
1713 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1714 assert(!decl->is_array);
1715 assert(decl->array_size == NULL);
1716 assert(decl->initializer == NULL);
1717
1718 ir_variable *const earlier =
1719 state->symbols->get_variable(decl->identifier);
1720 if (earlier == NULL) {
1721 _mesa_glsl_error(& loc, state,
1722 "Undeclared variable `%s' cannot be marked "
1723 "invariant\n", decl->identifier);
1724 } else if ((state->target == vertex_shader)
1725 && (earlier->mode != ir_var_out)) {
1726 _mesa_glsl_error(& loc, state,
1727 "`%s' cannot be marked invariant, vertex shader "
1728 "outputs only\n", decl->identifier);
1729 } else if ((state->target == fragment_shader)
1730 && (earlier->mode != ir_var_in)) {
1731 _mesa_glsl_error(& loc, state,
1732 "`%s' cannot be marked invariant, fragment shader "
1733 "inputs only\n", decl->identifier);
1734 } else {
1735 earlier->invariant = true;
1736 }
1737 }
1738
1739 /* Invariant redeclarations do not have r-values.
1740 */
1741 return NULL;
1742 }
1743
1744 assert(this->type != NULL);
1745 assert(!this->invariant);
1746
1747 /* The type specifier may contain a structure definition. Process that
1748 * before any of the variable declarations.
1749 */
1750 (void) this->type->specifier->hir(instructions, state);
1751
1752 decl_type = this->type->specifier->glsl_type(& type_name, state);
1753 if (this->declarations.is_empty()) {
1754 /* The only valid case where the declaration list can be empty is when
1755 * the declaration is setting the default precision of a built-in type
1756 * (e.g., 'precision highp vec4;').
1757 */
1758
1759 if (decl_type != NULL) {
1760 } else {
1761 _mesa_glsl_error(& loc, state, "incomplete declaration");
1762 }
1763 }
1764
1765 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1766 const struct glsl_type *var_type;
1767 ir_variable *var;
1768
1769 /* FINISHME: Emit a warning if a variable declaration shadows a
1770 * FINISHME: declaration at a higher scope.
1771 */
1772
1773 if ((decl_type == NULL) || decl_type->is_void()) {
1774 if (type_name != NULL) {
1775 _mesa_glsl_error(& loc, state,
1776 "invalid type `%s' in declaration of `%s'",
1777 type_name, decl->identifier);
1778 } else {
1779 _mesa_glsl_error(& loc, state,
1780 "invalid type in declaration of `%s'",
1781 decl->identifier);
1782 }
1783 continue;
1784 }
1785
1786 if (decl->is_array) {
1787 var_type = process_array_type(&loc, decl_type, decl->array_size,
1788 state);
1789 } else {
1790 var_type = decl_type;
1791 }
1792
1793 var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
1794
1795 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1796 *
1797 * "Global variables can only use the qualifiers const,
1798 * attribute, uni form, or varying. Only one may be
1799 * specified.
1800 *
1801 * Local variables can only use the qualifier const."
1802 *
1803 * This is relaxed in GLSL 1.30.
1804 */
1805 if (state->language_version < 120) {
1806 if (this->type->qualifier.flags.q.out) {
1807 _mesa_glsl_error(& loc, state,
1808 "`out' qualifier in declaration of `%s' "
1809 "only valid for function parameters in GLSL 1.10.",
1810 decl->identifier);
1811 }
1812 if (this->type->qualifier.flags.q.in) {
1813 _mesa_glsl_error(& loc, state,
1814 "`in' qualifier in declaration of `%s' "
1815 "only valid for function parameters in GLSL 1.10.",
1816 decl->identifier);
1817 }
1818 /* FINISHME: Test for other invalid qualifiers. */
1819 }
1820
1821 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
1822 & loc);
1823
1824 if (this->type->qualifier.flags.q.invariant) {
1825 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
1826 var->mode == ir_var_inout)) {
1827 /* FINISHME: Note that this doesn't work for invariant on
1828 * a function signature outval
1829 */
1830 _mesa_glsl_error(& loc, state,
1831 "`%s' cannot be marked invariant, vertex shader "
1832 "outputs only\n", var->name);
1833 } else if ((state->target == fragment_shader) &&
1834 !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
1835 /* FINISHME: Note that this doesn't work for invariant on
1836 * a function signature inval
1837 */
1838 _mesa_glsl_error(& loc, state,
1839 "`%s' cannot be marked invariant, fragment shader "
1840 "inputs only\n", var->name);
1841 }
1842 }
1843
1844 if (state->current_function != NULL) {
1845 const char *mode = NULL;
1846 const char *extra = "";
1847
1848 /* There is no need to check for 'inout' here because the parser will
1849 * only allow that in function parameter lists.
1850 */
1851 if (this->type->qualifier.flags.q.attribute) {
1852 mode = "attribute";
1853 } else if (this->type->qualifier.flags.q.uniform) {
1854 mode = "uniform";
1855 } else if (this->type->qualifier.flags.q.varying) {
1856 mode = "varying";
1857 } else if (this->type->qualifier.flags.q.in) {
1858 mode = "in";
1859 extra = " or in function parameter list";
1860 } else if (this->type->qualifier.flags.q.out) {
1861 mode = "out";
1862 extra = " or in function parameter list";
1863 }
1864
1865 if (mode) {
1866 _mesa_glsl_error(& loc, state,
1867 "%s variable `%s' must be declared at "
1868 "global scope%s",
1869 mode, var->name, extra);
1870 }
1871 } else if (var->mode == ir_var_in) {
1872 if (state->target == vertex_shader) {
1873 bool error_emitted = false;
1874
1875 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1876 *
1877 * "Vertex shader inputs can only be float, floating-point
1878 * vectors, matrices, signed and unsigned integers and integer
1879 * vectors. Vertex shader inputs can also form arrays of these
1880 * types, but not structures."
1881 *
1882 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1883 *
1884 * "Vertex shader inputs can only be float, floating-point
1885 * vectors, matrices, signed and unsigned integers and integer
1886 * vectors. They cannot be arrays or structures."
1887 *
1888 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1889 *
1890 * "The attribute qualifier can be used only with float,
1891 * floating-point vectors, and matrices. Attribute variables
1892 * cannot be declared as arrays or structures."
1893 */
1894 const glsl_type *check_type = var->type->is_array()
1895 ? var->type->fields.array : var->type;
1896
1897 switch (check_type->base_type) {
1898 case GLSL_TYPE_FLOAT:
1899 break;
1900 case GLSL_TYPE_UINT:
1901 case GLSL_TYPE_INT:
1902 if (state->language_version > 120)
1903 break;
1904 /* FALLTHROUGH */
1905 default:
1906 _mesa_glsl_error(& loc, state,
1907 "vertex shader input / attribute cannot have "
1908 "type %s`%s'",
1909 var->type->is_array() ? "array of " : "",
1910 check_type->name);
1911 error_emitted = true;
1912 }
1913
1914 if (!error_emitted && (state->language_version <= 130)
1915 && var->type->is_array()) {
1916 _mesa_glsl_error(& loc, state,
1917 "vertex shader input / attribute cannot have "
1918 "array type");
1919 error_emitted = true;
1920 }
1921 }
1922 }
1923
1924 /* Process the initializer and add its instructions to a temporary
1925 * list. This list will be added to the instruction stream (below) after
1926 * the declaration is added. This is done because in some cases (such as
1927 * redeclarations) the declaration may not actually be added to the
1928 * instruction stream.
1929 */
1930 exec_list initializer_instructions;
1931 if (decl->initializer != NULL) {
1932 YYLTYPE initializer_loc = decl->initializer->get_location();
1933
1934 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1935 *
1936 * "All uniform variables are read-only and are initialized either
1937 * directly by an application via API commands, or indirectly by
1938 * OpenGL."
1939 */
1940 if ((state->language_version <= 110)
1941 && (var->mode == ir_var_uniform)) {
1942 _mesa_glsl_error(& initializer_loc, state,
1943 "cannot initialize uniforms in GLSL 1.10");
1944 }
1945
1946 if (var->type->is_sampler()) {
1947 _mesa_glsl_error(& initializer_loc, state,
1948 "cannot initialize samplers");
1949 }
1950
1951 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
1952 _mesa_glsl_error(& initializer_loc, state,
1953 "cannot initialize %s shader input / %s",
1954 _mesa_glsl_shader_target_name(state->target),
1955 (state->target == vertex_shader)
1956 ? "attribute" : "varying");
1957 }
1958
1959 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
1960 ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions,
1961 state);
1962
1963 /* Calculate the constant value if this is a const or uniform
1964 * declaration.
1965 */
1966 if (this->type->qualifier.flags.q.constant
1967 || this->type->qualifier.flags.q.uniform) {
1968 ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs);
1969 if (new_rhs != NULL) {
1970 rhs = new_rhs;
1971
1972 ir_constant *constant_value = rhs->constant_expression_value();
1973 if (!constant_value) {
1974 _mesa_glsl_error(& initializer_loc, state,
1975 "initializer of %s variable `%s' must be a "
1976 "constant expression",
1977 (this->type->qualifier.flags.q.constant)
1978 ? "const" : "uniform",
1979 decl->identifier);
1980 if (var->type->is_numeric()) {
1981 /* Reduce cascading errors. */
1982 var->constant_value = ir_constant::zero(ctx, var->type);
1983 }
1984 } else {
1985 rhs = constant_value;
1986 var->constant_value = constant_value;
1987 }
1988 } else {
1989 _mesa_glsl_error(&initializer_loc, state,
1990 "initializer of type %s cannot be assigned to "
1991 "variable of type %s",
1992 rhs->type->name, var->type->name);
1993 if (var->type->is_numeric()) {
1994 /* Reduce cascading errors. */
1995 var->constant_value = ir_constant::zero(ctx, var->type);
1996 }
1997 }
1998 }
1999
2000 if (rhs && !rhs->type->is_error()) {
2001 bool temp = var->read_only;
2002 if (this->type->qualifier.flags.q.constant)
2003 var->read_only = false;
2004
2005 /* Never emit code to initialize a uniform.
2006 */
2007 if (!this->type->qualifier.flags.q.uniform)
2008 result = do_assignment(&initializer_instructions, state,
2009 lhs, rhs,
2010 this->get_location());
2011 var->read_only = temp;
2012 }
2013 }
2014
2015 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
2016 *
2017 * "It is an error to write to a const variable outside of
2018 * its declaration, so they must be initialized when
2019 * declared."
2020 */
2021 if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
2022 _mesa_glsl_error(& loc, state,
2023 "const declaration of `%s' must be initialized");
2024 }
2025
2026 /* Check if this declaration is actually a re-declaration, either to
2027 * resize an array or add qualifiers to an existing variable.
2028 *
2029 * This is allowed for variables in the current scope, or when at
2030 * global scope (for built-ins in the implicit outer scope).
2031 */
2032 ir_variable *earlier = state->symbols->get_variable(decl->identifier);
2033 if (earlier != NULL && (state->current_function == NULL ||
2034 state->symbols->name_declared_this_scope(decl->identifier))) {
2035
2036 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2037 *
2038 * "It is legal to declare an array without a size and then
2039 * later re-declare the same name as an array of the same
2040 * type and specify a size."
2041 */
2042 if ((earlier->type->array_size() == 0)
2043 && var->type->is_array()
2044 && (var->type->element_type() == earlier->type->element_type())) {
2045 /* FINISHME: This doesn't match the qualifiers on the two
2046 * FINISHME: declarations. It's not 100% clear whether this is
2047 * FINISHME: required or not.
2048 */
2049
2050 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
2051 *
2052 * "The size [of gl_TexCoord] can be at most
2053 * gl_MaxTextureCoords."
2054 */
2055 const unsigned size = unsigned(var->type->array_size());
2056 if ((strcmp("gl_TexCoord", var->name) == 0)
2057 && (size > state->Const.MaxTextureCoords)) {
2058 YYLTYPE loc = this->get_location();
2059
2060 _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot "
2061 "be larger than gl_MaxTextureCoords (%u)\n",
2062 state->Const.MaxTextureCoords);
2063 } else if ((size > 0) && (size <= earlier->max_array_access)) {
2064 YYLTYPE loc = this->get_location();
2065
2066 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
2067 "previous access",
2068 earlier->max_array_access);
2069 }
2070
2071 earlier->type = var->type;
2072 delete var;
2073 var = NULL;
2074 } else if (state->extensions->ARB_fragment_coord_conventions
2075 && strcmp(var->name, "gl_FragCoord") == 0
2076 && earlier->type == var->type
2077 && earlier->mode == var->mode) {
2078 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2079 * qualifiers.
2080 */
2081 earlier->origin_upper_left = var->origin_upper_left;
2082 earlier->pixel_center_integer = var->pixel_center_integer;
2083 } else {
2084 YYLTYPE loc = this->get_location();
2085 _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
2086 }
2087
2088 continue;
2089 }
2090
2091 /* By now, we know it's a new variable declaration (we didn't hit the
2092 * above "continue").
2093 *
2094 * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2095 *
2096 * "Identifiers starting with "gl_" are reserved for use by
2097 * OpenGL, and may not be declared in a shader as either a
2098 * variable or a function."
2099 */
2100 if (strncmp(decl->identifier, "gl_", 3) == 0)
2101 _mesa_glsl_error(& loc, state,
2102 "identifier `%s' uses reserved `gl_' prefix",
2103 decl->identifier);
2104
2105 /* Add the variable to the symbol table. Note that the initializer's
2106 * IR was already processed earlier (though it hasn't been emitted yet),
2107 * without the variable in scope.
2108 *
2109 * This differs from most C-like languages, but it follows the GLSL
2110 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
2111 * spec:
2112 *
2113 * "Within a declaration, the scope of a name starts immediately
2114 * after the initializer if present or immediately after the name
2115 * being declared if not."
2116 */
2117 if (!state->symbols->add_variable(var->name, var)) {
2118 YYLTYPE loc = this->get_location();
2119 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
2120 "current scope", decl->identifier);
2121 continue;
2122 }
2123
2124 /* Push the variable declaration to the top. It means that all
2125 * the variable declarations will appear in a funny
2126 * last-to-first order, but otherwise we run into trouble if a
2127 * function is prototyped, a global var is decled, then the
2128 * function is defined with usage of the global var. See
2129 * glslparsertest's CorrectModule.frag.
2130 */
2131 instructions->push_head(var);
2132 instructions->append_list(&initializer_instructions);
2133 }
2134
2135
2136 /* Generally, variable declarations do not have r-values. However,
2137 * one is used for the declaration in
2138 *
2139 * while (bool b = some_condition()) {
2140 * ...
2141 * }
2142 *
2143 * so we return the rvalue from the last seen declaration here.
2144 */
2145 return result;
2146 }
2147
2148
2149 ir_rvalue *
2150 ast_parameter_declarator::hir(exec_list *instructions,
2151 struct _mesa_glsl_parse_state *state)
2152 {
2153 void *ctx = state;
2154 const struct glsl_type *type;
2155 const char *name = NULL;
2156 YYLTYPE loc = this->get_location();
2157
2158 type = this->type->specifier->glsl_type(& name, state);
2159
2160 if (type == NULL) {
2161 if (name != NULL) {
2162 _mesa_glsl_error(& loc, state,
2163 "invalid type `%s' in declaration of `%s'",
2164 name, this->identifier);
2165 } else {
2166 _mesa_glsl_error(& loc, state,
2167 "invalid type in declaration of `%s'",
2168 this->identifier);
2169 }
2170
2171 type = glsl_type::error_type;
2172 }
2173
2174 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2175 *
2176 * "Functions that accept no input arguments need not use void in the
2177 * argument list because prototypes (or definitions) are required and
2178 * therefore there is no ambiguity when an empty argument list "( )" is
2179 * declared. The idiom "(void)" as a parameter list is provided for
2180 * convenience."
2181 *
2182 * Placing this check here prevents a void parameter being set up
2183 * for a function, which avoids tripping up checks for main taking
2184 * parameters and lookups of an unnamed symbol.
2185 */
2186 if (type->is_void()) {
2187 if (this->identifier != NULL)
2188 _mesa_glsl_error(& loc, state,
2189 "named parameter cannot have type `void'");
2190
2191 is_void = true;
2192 return NULL;
2193 }
2194
2195 if (formal_parameter && (this->identifier == NULL)) {
2196 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
2197 return NULL;
2198 }
2199
2200 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
2201 * call already handled the "vec4[..] foo" case.
2202 */
2203 if (this->is_array) {
2204 type = process_array_type(&loc, type, this->array_size, state);
2205 }
2206
2207 if (type->array_size() == 0) {
2208 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
2209 "a declared size.");
2210 type = glsl_type::error_type;
2211 }
2212
2213 is_void = false;
2214 ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
2215
2216 /* Apply any specified qualifiers to the parameter declaration. Note that
2217 * for function parameters the default mode is 'in'.
2218 */
2219 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
2220
2221 instructions->push_tail(var);
2222
2223 /* Parameter declarations do not have r-values.
2224 */
2225 return NULL;
2226 }
2227
2228
2229 void
2230 ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
2231 bool formal,
2232 exec_list *ir_parameters,
2233 _mesa_glsl_parse_state *state)
2234 {
2235 ast_parameter_declarator *void_param = NULL;
2236 unsigned count = 0;
2237
2238 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
2239 param->formal_parameter = formal;
2240 param->hir(ir_parameters, state);
2241
2242 if (param->is_void)
2243 void_param = param;
2244
2245 count++;
2246 }
2247
2248 if ((void_param != NULL) && (count > 1)) {
2249 YYLTYPE loc = void_param->get_location();
2250
2251 _mesa_glsl_error(& loc, state,
2252 "`void' parameter must be only parameter");
2253 }
2254 }
2255
2256
2257 ir_rvalue *
2258 ast_function::hir(exec_list *instructions,
2259 struct _mesa_glsl_parse_state *state)
2260 {
2261 void *ctx = state;
2262 ir_function *f = NULL;
2263 ir_function_signature *sig = NULL;
2264 exec_list hir_parameters;
2265
2266 const char *const name = identifier;
2267
2268 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
2269 *
2270 * "Function declarations (prototypes) cannot occur inside of functions;
2271 * they must be at global scope, or for the built-in functions, outside
2272 * the global scope."
2273 *
2274 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
2275 *
2276 * "User defined functions may only be defined within the global scope."
2277 *
2278 * Note that this language does not appear in GLSL 1.10.
2279 */
2280 if ((state->current_function != NULL) && (state->language_version != 110)) {
2281 YYLTYPE loc = this->get_location();
2282 _mesa_glsl_error(&loc, state,
2283 "declaration of function `%s' not allowed within "
2284 "function body", name);
2285 }
2286
2287 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2288 *
2289 * "Identifiers starting with "gl_" are reserved for use by
2290 * OpenGL, and may not be declared in a shader as either a
2291 * variable or a function."
2292 */
2293 if (strncmp(name, "gl_", 3) == 0) {
2294 YYLTYPE loc = this->get_location();
2295 _mesa_glsl_error(&loc, state,
2296 "identifier `%s' uses reserved `gl_' prefix", name);
2297 }
2298
2299 /* Convert the list of function parameters to HIR now so that they can be
2300 * used below to compare this function's signature with previously seen
2301 * signatures for functions with the same name.
2302 */
2303 ast_parameter_declarator::parameters_to_hir(& this->parameters,
2304 is_definition,
2305 & hir_parameters, state);
2306
2307 const char *return_type_name;
2308 const glsl_type *return_type =
2309 this->return_type->specifier->glsl_type(& return_type_name, state);
2310
2311 if (!return_type) {
2312 YYLTYPE loc = this->get_location();
2313 _mesa_glsl_error(&loc, state,
2314 "function `%s' has undeclared return type `%s'",
2315 name, return_type_name);
2316 return_type = glsl_type::error_type;
2317 }
2318
2319 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2320 * "No qualifier is allowed on the return type of a function."
2321 */
2322 if (this->return_type->has_qualifiers()) {
2323 YYLTYPE loc = this->get_location();
2324 _mesa_glsl_error(& loc, state,
2325 "function `%s' return type has qualifiers", name);
2326 }
2327
2328 /* Verify that this function's signature either doesn't match a previously
2329 * seen signature for a function with the same name, or, if a match is found,
2330 * that the previously seen signature does not have an associated definition.
2331 */
2332 f = state->symbols->get_function(name);
2333 if (f != NULL && (state->es_shader || f->has_user_signature())) {
2334 sig = f->exact_matching_signature(&hir_parameters);
2335 if (sig != NULL) {
2336 const char *badvar = sig->qualifiers_match(&hir_parameters);
2337 if (badvar != NULL) {
2338 YYLTYPE loc = this->get_location();
2339
2340 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
2341 "qualifiers don't match prototype", name, badvar);
2342 }
2343
2344 if (sig->return_type != return_type) {
2345 YYLTYPE loc = this->get_location();
2346
2347 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
2348 "match prototype", name);
2349 }
2350
2351 if (is_definition && sig->is_defined) {
2352 YYLTYPE loc = this->get_location();
2353
2354 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
2355 }
2356 }
2357 } else {
2358 f = new(ctx) ir_function(name);
2359 if (!state->symbols->add_function(f->name, f)) {
2360 /* This function name shadows a non-function use of the same name. */
2361 YYLTYPE loc = this->get_location();
2362
2363 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
2364 "non-function", name);
2365 return NULL;
2366 }
2367
2368 /* Emit the new function header */
2369 if (state->current_function == NULL)
2370 instructions->push_tail(f);
2371 else {
2372 /* IR invariants disallow function declarations or definitions nested
2373 * within other function definitions. Insert the new ir_function
2374 * block in the instruction sequence before the ir_function block
2375 * containing the current ir_function_signature.
2376 *
2377 * This can only happen in a GLSL 1.10 shader. In all other GLSL
2378 * versions this nesting is disallowed. There is a check for this at
2379 * the top of this function.
2380 */
2381 ir_function *const curr =
2382 const_cast<ir_function *>(state->current_function->function());
2383
2384 curr->insert_before(f);
2385 }
2386 }
2387
2388 /* Verify the return type of main() */
2389 if (strcmp(name, "main") == 0) {
2390 if (! return_type->is_void()) {
2391 YYLTYPE loc = this->get_location();
2392
2393 _mesa_glsl_error(& loc, state, "main() must return void");
2394 }
2395
2396 if (!hir_parameters.is_empty()) {
2397 YYLTYPE loc = this->get_location();
2398
2399 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
2400 }
2401 }
2402
2403 /* Finish storing the information about this new function in its signature.
2404 */
2405 if (sig == NULL) {
2406 sig = new(ctx) ir_function_signature(return_type);
2407 f->add_signature(sig);
2408 }
2409
2410 sig->replace_parameters(&hir_parameters);
2411 signature = sig;
2412
2413 /* Function declarations (prototypes) do not have r-values.
2414 */
2415 return NULL;
2416 }
2417
2418
2419 ir_rvalue *
2420 ast_function_definition::hir(exec_list *instructions,
2421 struct _mesa_glsl_parse_state *state)
2422 {
2423 prototype->is_definition = true;
2424 prototype->hir(instructions, state);
2425
2426 ir_function_signature *signature = prototype->signature;
2427 if (signature == NULL)
2428 return NULL;
2429
2430 assert(state->current_function == NULL);
2431 state->current_function = signature;
2432 state->found_return = false;
2433
2434 /* Duplicate parameters declared in the prototype as concrete variables.
2435 * Add these to the symbol table.
2436 */
2437 state->symbols->push_scope();
2438 foreach_iter(exec_list_iterator, iter, signature->parameters) {
2439 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
2440
2441 assert(var != NULL);
2442
2443 /* The only way a parameter would "exist" is if two parameters have
2444 * the same name.
2445 */
2446 if (state->symbols->name_declared_this_scope(var->name)) {
2447 YYLTYPE loc = this->get_location();
2448
2449 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
2450 } else {
2451 state->symbols->add_variable(var->name, var);
2452 }
2453 }
2454
2455 /* Convert the body of the function to HIR. */
2456 this->body->hir(&signature->body, state);
2457 signature->is_defined = true;
2458
2459 state->symbols->pop_scope();
2460
2461 assert(state->current_function == signature);
2462 state->current_function = NULL;
2463
2464 if (!signature->return_type->is_void() && !state->found_return) {
2465 YYLTYPE loc = this->get_location();
2466 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
2467 "%s, but no return statement",
2468 signature->function_name(),
2469 signature->return_type->name);
2470 }
2471
2472 /* Function definitions do not have r-values.
2473 */
2474 return NULL;
2475 }
2476
2477
2478 ir_rvalue *
2479 ast_jump_statement::hir(exec_list *instructions,
2480 struct _mesa_glsl_parse_state *state)
2481 {
2482 void *ctx = state;
2483
2484 switch (mode) {
2485 case ast_return: {
2486 ir_return *inst;
2487 assert(state->current_function);
2488
2489 if (opt_return_value) {
2490 if (state->current_function->return_type->base_type ==
2491 GLSL_TYPE_VOID) {
2492 YYLTYPE loc = this->get_location();
2493
2494 _mesa_glsl_error(& loc, state,
2495 "`return` with a value, in function `%s' "
2496 "returning void",
2497 state->current_function->function_name());
2498 }
2499
2500 ir_expression *const ret = (ir_expression *)
2501 opt_return_value->hir(instructions, state);
2502 assert(ret != NULL);
2503
2504 /* Implicit conversions are not allowed for return values. */
2505 if (state->current_function->return_type != ret->type) {
2506 YYLTYPE loc = this->get_location();
2507
2508 _mesa_glsl_error(& loc, state,
2509 "`return' with wrong type %s, in function `%s' "
2510 "returning %s",
2511 ret->type->name,
2512 state->current_function->function_name(),
2513 state->current_function->return_type->name);
2514 }
2515
2516 inst = new(ctx) ir_return(ret);
2517 } else {
2518 if (state->current_function->return_type->base_type !=
2519 GLSL_TYPE_VOID) {
2520 YYLTYPE loc = this->get_location();
2521
2522 _mesa_glsl_error(& loc, state,
2523 "`return' with no value, in function %s returning "
2524 "non-void",
2525 state->current_function->function_name());
2526 }
2527 inst = new(ctx) ir_return;
2528 }
2529
2530 state->found_return = true;
2531 instructions->push_tail(inst);
2532 break;
2533 }
2534
2535 case ast_discard:
2536 if (state->target != fragment_shader) {
2537 YYLTYPE loc = this->get_location();
2538
2539 _mesa_glsl_error(& loc, state,
2540 "`discard' may only appear in a fragment shader");
2541 }
2542 instructions->push_tail(new(ctx) ir_discard);
2543 break;
2544
2545 case ast_break:
2546 case ast_continue:
2547 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2548 * FINISHME: and they use a different IR instruction for 'break'.
2549 */
2550 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2551 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2552 * FINISHME: loop.
2553 */
2554 if (state->loop_or_switch_nesting == NULL) {
2555 YYLTYPE loc = this->get_location();
2556
2557 _mesa_glsl_error(& loc, state,
2558 "`%s' may only appear in a loop",
2559 (mode == ast_break) ? "break" : "continue");
2560 } else {
2561 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
2562
2563 /* Inline the for loop expression again, since we don't know
2564 * where near the end of the loop body the normal copy of it
2565 * is going to be placed.
2566 */
2567 if (mode == ast_continue &&
2568 state->loop_or_switch_nesting_ast->rest_expression) {
2569 state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
2570 state);
2571 }
2572
2573 if (loop != NULL) {
2574 ir_loop_jump *const jump =
2575 new(ctx) ir_loop_jump((mode == ast_break)
2576 ? ir_loop_jump::jump_break
2577 : ir_loop_jump::jump_continue);
2578 instructions->push_tail(jump);
2579 }
2580 }
2581
2582 break;
2583 }
2584
2585 /* Jump instructions do not have r-values.
2586 */
2587 return NULL;
2588 }
2589
2590
2591 ir_rvalue *
2592 ast_selection_statement::hir(exec_list *instructions,
2593 struct _mesa_glsl_parse_state *state)
2594 {
2595 void *ctx = state;
2596
2597 ir_rvalue *const condition = this->condition->hir(instructions, state);
2598
2599 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2600 *
2601 * "Any expression whose type evaluates to a Boolean can be used as the
2602 * conditional expression bool-expression. Vector types are not accepted
2603 * as the expression to if."
2604 *
2605 * The checks are separated so that higher quality diagnostics can be
2606 * generated for cases where both rules are violated.
2607 */
2608 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
2609 YYLTYPE loc = this->condition->get_location();
2610
2611 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
2612 "boolean");
2613 }
2614
2615 ir_if *const stmt = new(ctx) ir_if(condition);
2616
2617 if (then_statement != NULL) {
2618 state->symbols->push_scope();
2619 then_statement->hir(& stmt->then_instructions, state);
2620 state->symbols->pop_scope();
2621 }
2622
2623 if (else_statement != NULL) {
2624 state->symbols->push_scope();
2625 else_statement->hir(& stmt->else_instructions, state);
2626 state->symbols->pop_scope();
2627 }
2628
2629 instructions->push_tail(stmt);
2630
2631 /* if-statements do not have r-values.
2632 */
2633 return NULL;
2634 }
2635
2636
2637 void
2638 ast_iteration_statement::condition_to_hir(ir_loop *stmt,
2639 struct _mesa_glsl_parse_state *state)
2640 {
2641 void *ctx = state;
2642
2643 if (condition != NULL) {
2644 ir_rvalue *const cond =
2645 condition->hir(& stmt->body_instructions, state);
2646
2647 if ((cond == NULL)
2648 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
2649 YYLTYPE loc = condition->get_location();
2650
2651 _mesa_glsl_error(& loc, state,
2652 "loop condition must be scalar boolean");
2653 } else {
2654 /* As the first code in the loop body, generate a block that looks
2655 * like 'if (!condition) break;' as the loop termination condition.
2656 */
2657 ir_rvalue *const not_cond =
2658 new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
2659 NULL);
2660
2661 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
2662
2663 ir_jump *const break_stmt =
2664 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
2665
2666 if_stmt->then_instructions.push_tail(break_stmt);
2667 stmt->body_instructions.push_tail(if_stmt);
2668 }
2669 }
2670 }
2671
2672
2673 ir_rvalue *
2674 ast_iteration_statement::hir(exec_list *instructions,
2675 struct _mesa_glsl_parse_state *state)
2676 {
2677 void *ctx = state;
2678
2679 /* For-loops and while-loops start a new scope, but do-while loops do not.
2680 */
2681 if (mode != ast_do_while)
2682 state->symbols->push_scope();
2683
2684 if (init_statement != NULL)
2685 init_statement->hir(instructions, state);
2686
2687 ir_loop *const stmt = new(ctx) ir_loop();
2688 instructions->push_tail(stmt);
2689
2690 /* Track the current loop and / or switch-statement nesting.
2691 */
2692 ir_instruction *const nesting = state->loop_or_switch_nesting;
2693 ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
2694
2695 state->loop_or_switch_nesting = stmt;
2696 state->loop_or_switch_nesting_ast = this;
2697
2698 if (mode != ast_do_while)
2699 condition_to_hir(stmt, state);
2700
2701 if (body != NULL)
2702 body->hir(& stmt->body_instructions, state);
2703
2704 if (rest_expression != NULL)
2705 rest_expression->hir(& stmt->body_instructions, state);
2706
2707 if (mode == ast_do_while)
2708 condition_to_hir(stmt, state);
2709
2710 if (mode != ast_do_while)
2711 state->symbols->pop_scope();
2712
2713 /* Restore previous nesting before returning.
2714 */
2715 state->loop_or_switch_nesting = nesting;
2716 state->loop_or_switch_nesting_ast = nesting_ast;
2717
2718 /* Loops do not have r-values.
2719 */
2720 return NULL;
2721 }
2722
2723
2724 ir_rvalue *
2725 ast_type_specifier::hir(exec_list *instructions,
2726 struct _mesa_glsl_parse_state *state)
2727 {
2728 if (this->structure != NULL)
2729 return this->structure->hir(instructions, state);
2730
2731 return NULL;
2732 }
2733
2734
2735 ir_rvalue *
2736 ast_struct_specifier::hir(exec_list *instructions,
2737 struct _mesa_glsl_parse_state *state)
2738 {
2739 unsigned decl_count = 0;
2740
2741 /* Make an initial pass over the list of structure fields to determine how
2742 * many there are. Each element in this list is an ast_declarator_list.
2743 * This means that we actually need to count the number of elements in the
2744 * 'declarations' list in each of the elements.
2745 */
2746 foreach_list_typed (ast_declarator_list, decl_list, link,
2747 &this->declarations) {
2748 foreach_list_const (decl_ptr, & decl_list->declarations) {
2749 decl_count++;
2750 }
2751 }
2752
2753 /* Allocate storage for the structure fields and process the field
2754 * declarations. As the declarations are processed, try to also convert
2755 * the types to HIR. This ensures that structure definitions embedded in
2756 * other structure definitions are processed.
2757 */
2758 glsl_struct_field *const fields = talloc_array(state, glsl_struct_field,
2759 decl_count);
2760
2761 unsigned i = 0;
2762 foreach_list_typed (ast_declarator_list, decl_list, link,
2763 &this->declarations) {
2764 const char *type_name;
2765
2766 decl_list->type->specifier->hir(instructions, state);
2767
2768 /* Section 10.9 of the GLSL ES 1.00 specification states that
2769 * embedded structure definitions have been removed from the language.
2770 */
2771 if (state->es_shader && decl_list->type->specifier->structure != NULL) {
2772 YYLTYPE loc = this->get_location();
2773 _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
2774 "not allowed in GLSL ES 1.00.");
2775 }
2776
2777 const glsl_type *decl_type =
2778 decl_list->type->specifier->glsl_type(& type_name, state);
2779
2780 foreach_list_typed (ast_declaration, decl, link,
2781 &decl_list->declarations) {
2782 const struct glsl_type *field_type = decl_type;
2783 if (decl->is_array) {
2784 YYLTYPE loc = decl->get_location();
2785 field_type = process_array_type(&loc, decl_type, decl->array_size,
2786 state);
2787 }
2788 fields[i].type = (field_type != NULL)
2789 ? field_type : glsl_type::error_type;
2790 fields[i].name = decl->identifier;
2791 i++;
2792 }
2793 }
2794
2795 assert(i == decl_count);
2796
2797 const glsl_type *t =
2798 glsl_type::get_record_instance(fields, decl_count, this->name);
2799
2800 YYLTYPE loc = this->get_location();
2801 if (!state->symbols->add_type(name, t)) {
2802 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
2803 } else {
2804
2805 const glsl_type **s = (const glsl_type **)
2806 realloc(state->user_structures,
2807 sizeof(state->user_structures[0]) *
2808 (state->num_user_structures + 1));
2809 if (s != NULL) {
2810 s[state->num_user_structures] = t;
2811 state->user_structures = s;
2812 state->num_user_structures++;
2813 }
2814 }
2815
2816 /* Structure type definitions do not have r-values.
2817 */
2818 return NULL;
2819 }