2 * Copyright © 2010 Intel Corporation
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:
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
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.
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
31 * * Symbol table management
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.
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.
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
52 #include "main/imports.h"
53 #include "symbol_table.h"
54 #include "glsl_parser_extras.h"
56 #include "glsl_types.h"
59 static const struct glsl_type
*
60 arithmetic_result_type(const struct glsl_type
*type_a
,
61 const struct glsl_type
*type_b
,
63 struct _mesa_glsl_parse_state
*state
)
65 /* From GLSL 1.50 spec, page 56:
67 * "The arithmetic binary operators add (+), subtract (-),
68 * multiply (*), and divide (/) operate on integer and
69 * floating-point scalars, vectors, and matrices."
71 if (! is_numeric_base_type(type_a
->base_type
)
72 || ! is_numeric_base_type(type_b
->base_type
)) {
73 return glsl_error_type
;
77 /* "If one operand is floating-point based and the other is
78 * not, then the conversions from Section 4.1.10 "Implicit
79 * Conversions" are applied to the non-floating-point-based operand."
81 * This conversion was added in GLSL 1.20. If the compilation mode is
82 * GLSL 1.10, the conversion is skipped.
84 if (state
->language_version
>= 120) {
85 if ((type_a
->base_type
== GLSL_TYPE_FLOAT
)
86 && (type_b
->base_type
!= GLSL_TYPE_FLOAT
)) {
87 } else if ((type_a
->base_type
!= GLSL_TYPE_FLOAT
)
88 && (type_b
->base_type
== GLSL_TYPE_FLOAT
)) {
92 /* "If the operands are integer types, they must both be signed or
95 * From this rule and the preceeding conversion it can be inferred that
96 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
97 * The is_numeric_base_type check above already filtered out the case
98 * where either type is not one of these, so now the base types need only
99 * be tested for equality.
101 if (type_a
->base_type
!= type_b
->base_type
) {
102 return glsl_error_type
;
105 /* "All arithmetic binary operators result in the same fundamental type
106 * (signed integer, unsigned integer, or floating-point) as the
107 * operands they operate on, after operand type conversion. After
108 * conversion, the following cases are valid
110 * * The two operands are scalars. In this case the operation is
111 * applied, resulting in a scalar."
113 if (type_a
->is_scalar() && type_b
->is_scalar())
116 /* "* One operand is a scalar, and the other is a vector or matrix.
117 * In this case, the scalar operation is applied independently to each
118 * component of the vector or matrix, resulting in the same size
121 if (type_a
->is_scalar()) {
122 if (!type_b
->is_scalar())
124 } else if (type_b
->is_scalar()) {
128 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
129 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
132 assert(type_a
->vector_elements
> 1);
133 assert(type_b
->vector_elements
> 1);
135 /* "* The two operands are vectors of the same size. In this case, the
136 * operation is done component-wise resulting in the same size
139 if (is_glsl_type_vector(type_a
) && is_glsl_type_vector(type_b
)) {
140 if (type_a
->vector_elements
== type_b
->vector_elements
)
143 return glsl_error_type
;
146 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
147 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
148 * <vector, vector> have been handled. At least one of the operands must
149 * be matrix. Further, since there are no integer matrix types, the base
150 * type of both operands must be float.
152 assert((type_a
->matrix_rows
> 1) || (type_b
->matrix_rows
> 1));
153 assert(type_a
->base_type
== GLSL_TYPE_FLOAT
);
154 assert(type_b
->base_type
== GLSL_TYPE_FLOAT
);
156 /* "* The operator is add (+), subtract (-), or divide (/), and the
157 * operands are matrices with the same number of rows and the same
158 * number of columns. In this case, the operation is done component-
159 * wise resulting in the same size matrix."
160 * * The operator is multiply (*), where both operands are matrices or
161 * one operand is a vector and the other a matrix. A right vector
162 * operand is treated as a column vector and a left vector operand as a
163 * row vector. In all these cases, it is required that the number of
164 * columns of the left operand is equal to the number of rows of the
165 * right operand. Then, the multiply (*) operation does a linear
166 * algebraic multiply, yielding an object that has the same number of
167 * rows as the left operand and the same number of columns as the right
168 * operand. Section 5.10 "Vector and Matrix Operations" explains in
169 * more detail how vectors and matrices are operated on."
172 if (is_glsl_type_matrix(type_a
) && is_glsl_type_matrix(type_b
)
173 && (type_a
->vector_elements
== type_b
->vector_elements
)
174 && (type_a
->matrix_rows
== type_b
->matrix_rows
))
177 return glsl_error_type
;
179 if (is_glsl_type_matrix(type_a
) && is_glsl_type_matrix(type_b
)) {
180 if (type_a
->vector_elements
== type_b
->matrix_rows
) {
182 const struct glsl_type
*t
;
188 if (type_a
->matrix_rows
== type_b
->vector_elements
) {
189 type_name
[3] = '0' + type_a
->matrix_rows
;
192 type_name
[3] = '0' + type_a
->matrix_rows
;
194 type_name
[5] = '0' + type_b
->vector_elements
;
199 _mesa_symbol_table_find_symbol(state
->symbols
, 0, type_name
);
200 return (t
!= NULL
) ? t
: glsl_error_type
;
202 } else if (is_glsl_type_matrix(type_a
)) {
203 /* A is a matrix and B is a column vector. Columns of A must match
206 if (type_a
->vector_elements
== type_b
->vector_elements
)
209 assert(is_glsl_type_matrix(type_b
));
211 /* A is a row vector and B is a matrix. Columns of A must match
214 if (type_a
->vector_elements
== type_b
->matrix_rows
)
220 /* "All other cases are illegal."
222 return glsl_error_type
;
226 static const struct glsl_type
*
227 unary_arithmetic_result_type(const struct glsl_type
*type
)
229 /* From GLSL 1.50 spec, page 57:
231 * "The arithmetic unary operators negate (-), post- and pre-increment
232 * and decrement (-- and ++) operate on integer or floating-point
233 * values (including vectors and matrices). All unary operators work
234 * component-wise on their operands. These result with the same type
237 if (!is_numeric_base_type(type
->base_type
))
238 return glsl_error_type
;
244 static const struct glsl_type
*
245 modulus_result_type(const struct glsl_type
*type_a
,
246 const struct glsl_type
*type_b
)
248 /* From GLSL 1.50 spec, page 56:
249 * "The operator modulus (%) operates on signed or unsigned integers or
250 * integer vectors. The operand types must both be signed or both be
253 if (! is_integer_base_type(type_a
->base_type
)
254 || ! is_integer_base_type(type_b
->base_type
)
255 || (type_a
->base_type
!= type_b
->base_type
)) {
256 return glsl_error_type
;
259 /* "The operands cannot be vectors of differing size. If one operand is
260 * a scalar and the other vector, then the scalar is applied component-
261 * wise to the vector, resulting in the same type as the vector. If both
262 * are vectors of the same size, the result is computed component-wise."
264 if (is_glsl_type_vector(type_a
)) {
265 if (!is_glsl_type_vector(type_b
)
266 || (type_a
->vector_elements
== type_b
->vector_elements
))
271 /* "The operator modulus (%) is not defined for any other data types
272 * (non-integer types)."
274 return glsl_error_type
;
278 static const struct glsl_type
*
279 relational_result_type(const struct glsl_type
*type_a
,
280 const struct glsl_type
*type_b
,
281 struct _mesa_glsl_parse_state
*state
)
283 /* From GLSL 1.50 spec, page 56:
284 * "The relational operators greater than (>), less than (<), greater
285 * than or equal (>=), and less than or equal (<=) operate only on
286 * scalar integer and scalar floating-point expressions."
288 if (! is_numeric_base_type(type_a
->base_type
)
289 || ! is_numeric_base_type(type_b
->base_type
)
290 || !type_a
->is_scalar()
291 || !type_b
->is_scalar())
292 return glsl_error_type
;
294 /* "Either the operands' types must match, or the conversions from
295 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
296 * operand, after which the types must match."
298 * This conversion was added in GLSL 1.20. If the compilation mode is
299 * GLSL 1.10, the conversion is skipped.
301 if (state
->language_version
>= 120) {
302 if ((type_a
->base_type
== GLSL_TYPE_FLOAT
)
303 && (type_b
->base_type
!= GLSL_TYPE_FLOAT
)) {
304 /* FINISHME: Generate the implicit type conversion. */
305 } else if ((type_a
->base_type
!= GLSL_TYPE_FLOAT
)
306 && (type_b
->base_type
== GLSL_TYPE_FLOAT
)) {
307 /* FINISHME: Generate the implicit type conversion. */
311 if (type_a
->base_type
!= type_b
->base_type
)
312 return glsl_error_type
;
314 /* "The result is scalar Boolean."
316 return glsl_bool_type
;
321 ast_node::hir(exec_list
*instructions
,
322 struct _mesa_glsl_parse_state
*state
)
332 ast_expression::hir(exec_list
*instructions
,
333 struct _mesa_glsl_parse_state
*state
)
335 static const int operations
[AST_NUM_OPERATORS
] = {
336 -1, /* ast_assign doesn't convert to ir_expression. */
337 -1, /* ast_plus doesn't convert to ir_expression. */
361 /* Note: The following block of expression types actually convert
362 * to multiple IR instructions.
364 ir_binop_mul
, /* ast_mul_assign */
365 ir_binop_div
, /* ast_div_assign */
366 ir_binop_mod
, /* ast_mod_assign */
367 ir_binop_add
, /* ast_add_assign */
368 ir_binop_sub
, /* ast_sub_assign */
369 ir_binop_lshift
, /* ast_ls_assign */
370 ir_binop_rshift
, /* ast_rs_assign */
371 ir_binop_bit_and
, /* ast_and_assign */
372 ir_binop_bit_xor
, /* ast_xor_assign */
373 ir_binop_bit_or
, /* ast_or_assign */
375 -1, /* ast_conditional doesn't convert to ir_expression. */
376 -1, /* ast_pre_inc doesn't convert to ir_expression. */
377 -1, /* ast_pre_dec doesn't convert to ir_expression. */
378 -1, /* ast_post_inc doesn't convert to ir_expression. */
379 -1, /* ast_post_dec doesn't convert to ir_expression. */
380 -1, /* ast_field_selection doesn't conv to ir_expression. */
381 -1, /* ast_array_index doesn't convert to ir_expression. */
382 -1, /* ast_function_call doesn't conv to ir_expression. */
383 -1, /* ast_identifier doesn't convert to ir_expression. */
384 -1, /* ast_int_constant doesn't convert to ir_expression. */
385 -1, /* ast_uint_constant doesn't conv to ir_expression. */
386 -1, /* ast_float_constant doesn't conv to ir_expression. */
387 -1, /* ast_bool_constant doesn't conv to ir_expression. */
388 -1, /* ast_sequence doesn't convert to ir_expression. */
390 ir_instruction
*result
= NULL
;
391 ir_instruction
*op
[2];
392 struct simple_node op_list
;
393 const struct glsl_type
*type
= glsl_error_type
;
394 bool error_emitted
= false;
397 loc
= this->get_location();
398 make_empty_list(& op_list
);
400 switch (this->oper
) {
402 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
403 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
405 error_emitted
= ((op
[0]->type
== glsl_error_type
)
406 || (op
[1]->type
== glsl_error_type
));
409 if (!error_emitted
) {
412 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
413 loc
= this->subexpressions
[0]->get_location();
414 if (op
[0]->mode
!= ir_op_dereference
) {
415 _mesa_glsl_error(& loc
, state
, "invalid lvalue in assignment");
416 error_emitted
= true;
418 type
= glsl_error_type
;
420 const struct ir_dereference
*const ref
=
421 (struct ir_dereference
*) op
[0];
422 const struct ir_variable
*const var
=
423 (struct ir_variable
*) ref
->var
;
426 && (var
->mode
== ir_op_var_decl
)
427 && (var
->read_only
)) {
428 _mesa_glsl_error(& loc
, state
, "cannot assign to read-only "
429 "variable `%s'", var
->name
);
430 error_emitted
= true;
432 type
= glsl_error_type
;
437 /* FINISHME: Check that the LHS and RHS have matching types. */
438 /* FINISHME: For GLSL 1.10, check that the types are not arrays. */
440 result
= new ir_assignment(op
[0], op
[1], NULL
);
444 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
446 error_emitted
= (op
[0]->type
== glsl_error_type
);
447 if (type
== glsl_error_type
)
454 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
456 type
= unary_arithmetic_result_type(op
[0]->type
);
458 error_emitted
= (op
[0]->type
== glsl_error_type
);
460 result
= new ir_expression(operations
[this->oper
], type
,
468 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
469 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
471 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
472 (this->oper
== ast_mul
),
475 result
= new ir_expression(operations
[this->oper
], type
,
480 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
481 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
483 error_emitted
= ((op
[0]->type
== glsl_error_type
)
484 || (op
[1]->type
== glsl_error_type
));
486 type
= modulus_result_type(op
[0]->type
, op
[1]->type
);
488 assert(operations
[this->oper
] == ir_binop_mod
);
490 result
= new ir_expression(operations
[this->oper
], type
,
496 /* FINISHME: Implement bit-shift operators. */
503 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
504 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
506 error_emitted
= ((op
[0]->type
== glsl_error_type
)
507 || (op
[1]->type
== glsl_error_type
));
509 type
= relational_result_type(op
[0]->type
, op
[1]->type
, state
);
511 /* The relational operators must either generate an error or result
512 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
514 assert((type
== glsl_error_type
)
515 || ((type
->base_type
== GLSL_TYPE_BOOL
)
516 && type
->is_scalar()));
518 result
= new ir_expression(operations
[this->oper
], type
,
524 /* FINISHME: Implement equality operators. */
531 /* FINISHME: Implement bit-wise operators. */
538 /* FINISHME: Implement logical operators. */
544 case ast_sub_assign
: {
545 struct ir_instruction
*temp_rhs
;
547 op
[0] = this->subexpressions
[0]->hir(instructions
, state
);
548 op
[1] = this->subexpressions
[1]->hir(instructions
, state
);
550 error_emitted
= ((op
[0]->type
== glsl_error_type
)
551 || (op
[1]->type
== glsl_error_type
));
553 type
= arithmetic_result_type(op
[0]->type
, op
[1]->type
,
554 (this->oper
== ast_mul_assign
),
557 temp_rhs
= new ir_expression(operations
[this->oper
], type
,
560 /* FINISHME: Check that the LHS is assignable. */
562 /* We still have to test that the LHS and RHS have matching type. For
563 * example, the following GLSL code should generate a type error:
565 * mat4 m; vec4 v; m *= v;
567 * The type of (m*v) is a vec4, but the type of m is a mat4.
569 * FINISHME: Is multiplication between a matrix and a vector the only
570 * FINISHME: case that resuls in mismatched types?
572 /* FINISHME: Check that the LHS and RHS have matching types. */
574 /* GLSL 1.10 does not allow array assignment. However, we don't have to
575 * explicitly test for this because none of the binary expression
576 * operators allow array operands either.
579 /* FINISHME: This is wrong. The operation should assign to a new
580 * FINISHME: temporary. This assignment should then be added to the
581 * FINISHME: instruction list. Another assignment to the real
582 * FINISHME: destination should be generated. The temporary should then
583 * FINISHME: be returned as the r-value.
585 result
= new ir_assignment(op
[0], temp_rhs
, NULL
);
598 case ast_conditional
:
607 case ast_field_selection
:
608 result
= _mesa_ast_field_selection_to_hir(this, instructions
, state
);
612 case ast_array_index
:
615 case ast_function_call
:
616 /* There are three sorts of function calls.
618 * 1. contstructors - The first subexpression is an ast_type_specifier.
619 * 2. methods - Only the .length() method of array types.
620 * 3. functions - Calls to regular old functions.
622 * Method calls are actually detected when the ast_field_selection
623 * expression is handled.
626 result
= _mesa_ast_function_call_to_hir(this->subexpressions
[0],
627 this->subexpressions
[1],
633 case ast_identifier
: {
634 /* ast_identifier can appear several places in a full abstract syntax
635 * tree. This particular use must be at location specified in the grammar
636 * as 'variable_identifier'.
638 ir_variable
*var
= (ir_variable
*)
639 _mesa_symbol_table_find_symbol(state
->symbols
, 0,
640 this->primary_expression
.identifier
);
642 result
= new ir_dereference(var
);
647 _mesa_glsl_error(& loc
, NULL
, "`%s' undeclared",
648 this->primary_expression
.identifier
);
650 error_emitted
= true;
655 case ast_int_constant
:
656 type
= glsl_int_type
;
657 result
= new ir_constant(type
, & this->primary_expression
);
660 case ast_uint_constant
:
661 type
= glsl_uint_type
;
662 result
= new ir_constant(type
, & this->primary_expression
);
665 case ast_float_constant
:
666 type
= glsl_float_type
;
667 result
= new ir_constant(type
, & this->primary_expression
);
670 case ast_bool_constant
:
671 type
= glsl_bool_type
;
672 result
= new ir_constant(type
, & this->primary_expression
);
676 struct simple_node
*ptr
;
678 /* It should not be possible to generate a sequence in the AST without
679 * any expressions in it.
681 assert(!is_empty_list(&this->expressions
));
683 /* The r-value of a sequence is the last expression in the sequence. If
684 * the other expressions in the sequence do not have side-effects (and
685 * therefore add instructions to the instruction list), they get dropped
688 foreach (ptr
, &this->expressions
)
689 result
= ((ast_node
*)ptr
)->hir(instructions
, state
);
693 /* Any errors should have already been emitted in the loop above.
695 error_emitted
= true;
700 if (is_error_type(type
) && !error_emitted
)
701 _mesa_glsl_error(& loc
, NULL
, "type mismatch");
708 ast_expression_statement::hir(exec_list
*instructions
,
709 struct _mesa_glsl_parse_state
*state
)
711 /* It is possible to have expression statements that don't have an
712 * expression. This is the solitary semicolon:
714 * for (i = 0; i < 5; i++)
717 * In this case the expression will be NULL. Test for NULL and don't do
718 * anything in that case.
720 if (expression
!= NULL
)
721 expression
->hir(instructions
, state
);
723 /* Statements do not have r-values.
730 ast_compound_statement::hir(exec_list
*instructions
,
731 struct _mesa_glsl_parse_state
*state
)
733 struct simple_node
*ptr
;
737 _mesa_symbol_table_push_scope(state
->symbols
);
739 foreach (ptr
, &statements
)
740 ((ast_node
*)ptr
)->hir(instructions
, state
);
743 _mesa_symbol_table_pop_scope(state
->symbols
);
745 /* Compound statements do not have r-values.
751 static const struct glsl_type
*
752 type_specifier_to_glsl_type(const struct ast_type_specifier
*spec
,
754 struct _mesa_glsl_parse_state
*state
)
756 static const char *const type_names
[] = {
792 "sampler1DArrayShadow",
793 "sampler2DArrayShadow",
807 NULL
, /* ast_struct */
808 NULL
/* ast_type_name */
810 struct glsl_type
*type
;
811 const char *type_name
= NULL
;
813 if (spec
->type_specifier
== ast_struct
) {
814 /* FINISHME: Handle annonymous structures. */
817 type_name
= (spec
->type_specifier
== ast_type_name
)
818 ? spec
->type_name
: type_names
[spec
->type_specifier
];
821 _mesa_symbol_table_find_symbol(state
->symbols
, 0, type_name
);
824 /* FINISHME: Handle array declarations. Note that this requires complete
825 * FINSIHME: handling of constant expressions.
834 apply_type_qualifier_to_variable(const struct ast_type_qualifier
*qual
,
835 struct ir_variable
*var
,
836 struct _mesa_glsl_parse_state
*state
)
841 /* FINISHME: Mark 'in' variables at global scope as read-only. */
842 if (qual
->constant
|| qual
->attribute
|| qual
->uniform
843 || (qual
->varying
&& (state
->target
== fragment_shader
)))
849 if (qual
->in
&& qual
->out
)
850 var
->mode
= ir_var_inout
;
851 else if (qual
->attribute
|| qual
->in
852 || (qual
->varying
&& (state
->target
== fragment_shader
)))
853 var
->mode
= ir_var_in
;
855 var
->mode
= ir_var_out
;
856 else if (qual
->uniform
)
857 var
->mode
= ir_var_uniform
;
859 var
->mode
= ir_var_auto
;
862 var
->interpolation
= ir_var_flat
;
863 else if (qual
->noperspective
)
864 var
->interpolation
= ir_var_noperspective
;
866 var
->interpolation
= ir_var_smooth
;
871 ast_declarator_list::hir(exec_list
*instructions
,
872 struct _mesa_glsl_parse_state
*state
)
874 struct simple_node
*ptr
;
875 const struct glsl_type
*decl_type
;
876 const char *type_name
= NULL
;
879 /* FINISHME: Handle vertex shader "invariant" declarations that do not
880 * FINISHME: include a type. These re-declare built-in variables to be
881 * FINISHME: invariant.
884 decl_type
= type_specifier_to_glsl_type(this->type
->specifier
,
887 foreach (ptr
, &this->declarations
) {
888 struct ast_declaration
*const decl
= (struct ast_declaration
* )ptr
;
889 const struct glsl_type
*var_type
;
890 struct ir_variable
*var
;
893 /* FINISHME: Emit a warning if a variable declaration shadows a
894 * FINISHME: declaration at a higher scope.
897 if (decl_type
== NULL
) {
900 loc
= this->get_location();
901 if (type_name
!= NULL
) {
902 _mesa_glsl_error(& loc
, state
,
903 "invalid type `%s' in declaration of `%s'",
904 type_name
, decl
->identifier
);
906 _mesa_glsl_error(& loc
, state
,
907 "invalid type in declaration of `%s'",
913 if (decl
->is_array
) {
914 /* FINISHME: Handle array declarations. Note that this requires
915 * FINISHME: complete handling of constant expressions.
918 /* FINISHME: Reject delcarations of multidimensional arrays. */
920 var_type
= decl_type
;
923 var
= new ir_variable(var_type
, decl
->identifier
);
925 /* FINSIHME: Variables that are attribute, uniform, varying, in, or
926 * FINISHME: out varibles must be declared either at global scope or
927 * FINISHME: in a parameter list (in and out only).
930 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
);
932 /* Attempt to add the variable to the symbol table. If this fails, it
933 * means the variable has already been declared at this scope.
935 if (_mesa_symbol_table_add_symbol(state
->symbols
, 0, decl
->identifier
,
937 YYLTYPE loc
= this->get_location();
939 _mesa_glsl_error(& loc
, state
, "`%s' redeclared",
944 instructions
->push_tail(var
);
946 /* FINISHME: Process the declaration initializer. */
949 /* Variable declarations do not have r-values.
956 ast_parameter_declarator::hir(exec_list
*instructions
,
957 struct _mesa_glsl_parse_state
*state
)
959 const struct glsl_type
*type
;
960 const char *name
= NULL
;
963 type
= type_specifier_to_glsl_type(this->type
->specifier
, & name
, state
);
966 YYLTYPE loc
= this->get_location();
968 _mesa_glsl_error(& loc
, state
,
969 "invalid type `%s' in declaration of `%s'",
970 name
, this->identifier
);
972 _mesa_glsl_error(& loc
, state
,
973 "invalid type in declaration of `%s'",
977 type
= glsl_error_type
;
980 ir_variable
*var
= new ir_variable(type
, this->identifier
);
982 /* FINISHME: Handle array declarations. Note that this requires
983 * FINISHME: complete handling of constant expressions.
986 apply_type_qualifier_to_variable(& this->type
->qualifier
, var
, state
);
988 instructions
->push_tail(var
);
990 /* Parameter declarations do not have r-values.
997 ast_function_parameters_to_hir(struct simple_node
*ast_parameters
,
998 exec_list
*ir_parameters
,
999 struct _mesa_glsl_parse_state
*state
)
1001 struct simple_node
*ptr
;
1003 foreach (ptr
, ast_parameters
) {
1004 ((ast_node
*)ptr
)->hir(ir_parameters
, state
);
1010 parameter_lists_match(exec_list
*list_a
, exec_list
*list_b
)
1012 exec_list_iterator iter_a
= list_a
->iterator();
1013 exec_list_iterator iter_b
= list_b
->iterator();
1015 while (iter_a
.has_next()) {
1016 /* If all of the parameters from the other parameter list have been
1017 * exhausted, the lists have different length and, by definition,
1020 if (!iter_b
.has_next())
1023 /* If the types of the parameters do not match, the parameters lists
1038 ast_function_definition::hir(exec_list
*instructions
,
1039 struct _mesa_glsl_parse_state
*state
)
1042 ir_function_signature
*signature
= NULL
;
1043 ir_function
*f
= NULL
;
1044 exec_list parameters
;
1047 /* Convert the list of function parameters to HIR now so that they can be
1048 * used below to compare this function's signature with previously seen
1049 * signatures for functions with the same name.
1051 ast_function_parameters_to_hir(& this->prototype
->parameters
, & parameters
,
1055 /* Verify that this function's signature either doesn't match a previously
1056 * seen signature for a function with the same name, or, if a match is found,
1057 * that the previously seen signature does not have an associated definition.
1060 _mesa_symbol_table_find_symbol(state
->symbols
, 0,
1061 this->prototype
->identifier
);
1063 foreach_iter(exec_list_iterator
, iter
, f
->signatures
) {
1064 signature
= (struct ir_function_signature
*) iter
.get();
1066 /* Compare the parameter list of the function being defined to the
1067 * existing function. If the parameter lists match, then the return
1068 * type must also match and the existing function must not have a
1071 if (parameter_lists_match(& parameters
, & signature
->parameters
)) {
1072 /* FINISHME: Compare return types. */
1074 if (signature
->definition
!= NULL
) {
1075 YYLTYPE loc
= this->get_location();
1077 _mesa_glsl_error(& loc
, state
, "function `%s' redefined",
1078 this->prototype
->identifier
);
1088 f
= new ir_function();
1089 f
->name
= this->prototype
->identifier
;
1091 _mesa_symbol_table_add_symbol(state
->symbols
, 0, f
->name
, f
);
1095 /* Finish storing the information about this new function in its signature.
1097 if (signature
== NULL
) {
1098 signature
= new ir_function_signature();
1099 f
->signatures
.push_tail(signature
);
1101 /* Destroy all of the previous parameter information. The previous
1102 * parameter information comes from the function prototype, and it can
1103 * either include invalid parameter names or may not have names at all.
1105 foreach_iter(exec_list_iterator
, iter
, signature
->parameters
) {
1106 assert(((struct ir_instruction
*)iter
.get())->mode
== ir_op_var_decl
);
1114 ast_function_parameters_to_hir(& this->prototype
->parameters
,
1115 & signature
->parameters
,
1117 /* FINISHME: Set signature->return_type */
1119 label
= new ir_label(this->prototype
->identifier
);
1120 if (signature
->definition
== NULL
) {
1121 signature
->definition
= label
;
1123 instructions
->push_tail(label
);
1125 /* Add the function parameters to the symbol table. During this step the
1126 * parameter declarations are also moved from the temporary "parameters" list
1127 * to the instruction list. There are other more efficient ways to do this,
1128 * but they involve ugly linked-list gymnastics.
1130 _mesa_symbol_table_push_scope(state
->symbols
);
1131 foreach_iter(exec_list_iterator
, iter
, parameters
) {
1132 ir_variable
*const var
= (ir_variable
*) iter
.get();
1134 assert(var
->mode
== ir_op_var_decl
);
1137 instructions
->push_tail(var
);
1139 _mesa_symbol_table_add_symbol(state
->symbols
, 0, var
->name
, var
);
1142 /* Convert the body of the function to HIR, and append the resulting
1143 * instructions to the list that currently consists of the function label
1144 * and the function parameters.
1146 this->body
->hir(instructions
, state
);
1148 _mesa_symbol_table_pop_scope(state
->symbols
);
1151 /* Function definitions do not have r-values.