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.
24 #include "glsl_types.h"
28 type_compare(const glsl_type
*a
, const glsl_type
*b
)
30 switch (a
->base_type
) {
35 if ((a
->vector_elements
!= b
->vector_elements
)
36 || (a
->matrix_rows
!= b
->matrix_rows
))
39 if (a
->base_type
== b
->base_type
)
42 /* There is no implicit conversion to or from bool.
44 if ((a
->base_type
== GLSL_TYPE_BOOL
)
45 || (b
->base_type
== GLSL_TYPE_BOOL
))
50 case GLSL_TYPE_SAMPLER
:
51 return ((a
->sampler_dimensionality
== b
->sampler_dimensionality
)
52 && (a
->sampler_shadow
== b
->sampler_shadow
)
53 && (a
->sampler_array
== b
->sampler_array
)
54 && (a
->sampler_type
== b
->sampler_type
))
57 case GLSL_TYPE_STRUCT
:
58 return (strcmp(a
->name
, b
->name
) == 0) ? 0 : -1;
61 if ((b
->base_type
!= GLSL_TYPE_ARRAY
)
62 || (a
->length
!= b
->length
))
65 /* From GLSL 1.50 spec, page 27 (page 33 of the PDF):
66 * "There are no implicit array or structure conversions."
68 * If the comparison of the array element types detects that a conversion
69 * would be required, the array types do not match.
71 return (type_compare(a
->fields
.array
, b
->fields
.array
) == 0) ? 0 : -1;
73 case GLSL_TYPE_FUNCTION
:
77 /* These are all error conditions. It is invalid for a parameter to
78 * a function to be declared as error, void, or a function.
83 /* This point should be unreachable.
90 parameter_lists_match(exec_list
*list_a
, exec_list
*list_b
)
92 exec_list_iterator iter_a
= list_a
->iterator();
93 exec_list_iterator iter_b
= list_b
->iterator();
96 for (/* empty */ ; iter_a
.has_next(); iter_a
.next(), iter_b
.next()) {
97 /* If all of the parameters from the other parameter list have been
98 * exhausted, the lists have different length and, by definition,
101 if (!iter_b
.has_next())
105 const ir_variable
*const param
= (ir_variable
*) iter_a
.get();
106 const ir_instruction
*const actual
= (ir_instruction
*) iter_b
.get();
108 /* Determine whether or not the types match. If the types are an
109 * exact match, the match score is zero. If the types don't match
110 * but the actual parameter can be coerced to the type of the declared
111 * parameter, the match score is one.
114 switch ((enum ir_variable_mode
)(param
->mode
)) {
117 /* These are all error conditions. It is invalid for a parameter to
118 * a function to be declared as auto (not in, out, or inout) or
125 score
= type_compare(param
->type
, actual
->type
);
129 /* FINISHME: Make sure that actual is a valid lvalue. */
130 score
= type_compare(actual
->type
, param
->type
);
134 /* FINISHME: Make sure that actual is a valid lvalue. */
136 /* Since there are no bi-directional automatic conversions (e.g.,
137 * there is int -> float but no float -> int), inout parameters must
140 score
= (type_compare(actual
->type
, param
->type
) == 0) ? 0 : -1;
147 total_score
+= score
;
150 /* If all of the parameters from the other parameter list have been
151 * exhausted, the lists have different length and, by definition, do not
154 if (iter_b
.has_next())
161 const ir_function_signature
*
162 ir_function::matching_signature(exec_list
*actual_parameters
)
164 ir_function_signature
*match
= NULL
;
166 foreach_iter(exec_list_iterator
, iter
, signatures
) {
167 ir_function_signature
*const sig
=
168 (ir_function_signature
*) iter
.get();
170 const int score
= parameter_lists_match(& sig
->parameters
,