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