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