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