dd63e3078f880ea13ed6cd6cfd12d719d786408e
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 * \brief Check if two parameter lists match.
30 * \param list_a Parameters of the function definition.
31 * \param list_b Actual parameters passed to the function.
32 * \return If an exact match, return 0.
33 * If an inexact match requiring implicit conversion, return 1.
34 * If not a match, return -1.
35 * \see matching_signature()
38 parameter_lists_match(const exec_list
*list_a
, const exec_list
*list_b
)
40 const exec_node
*node_a
= list_a
->head
;
41 const exec_node
*node_b
= list_b
->head
;
43 /* This is set to true if there is an inexact match requiring an implicit
45 bool inexact_match
= false;
48 ; !node_a
->is_tail_sentinel()
49 ; node_a
= node_a
->next
, node_b
= node_b
->next
) {
50 /* If all of the parameters from the other parameter list have been
51 * exhausted, the lists have different length and, by definition,
54 if (node_b
->is_tail_sentinel())
58 const ir_variable
*const param
= (ir_variable
*) node_a
;
59 const ir_instruction
*const actual
= (ir_instruction
*) node_b
;
61 if (param
->type
== actual
->type
)
64 /* Try to find an implicit conversion from actual to param. */
66 switch ((enum ir_variable_mode
)(param
->mode
)) {
69 case ir_var_temporary
:
70 /* These are all error conditions. It is invalid for a parameter to
71 * a function to be declared as auto (not in, out, or inout) or
79 if (!actual
->type
->can_implicitly_convert_to(param
->type
))
84 if (!param
->type
->can_implicitly_convert_to(actual
->type
))
89 /* Since there are no bi-directional automatic conversions (e.g.,
90 * there is int -> float but no float -> int), inout parameters must
101 /* If all of the parameters from the other parameter list have been
102 * exhausted, the lists have different length and, by definition, do not
105 if (!node_b
->is_tail_sentinel())
115 ir_function_signature
*
116 ir_function::matching_signature(const exec_list
*actual_parameters
)
118 ir_function_signature
*match
= NULL
;
119 bool multiple_inexact_matches
= false;
121 /* From page 42 (page 49 of the PDF) of the GLSL 1.20 spec:
123 * "If an exact match is found, the other signatures are ignored, and
124 * the exact match is used. Otherwise, if no exact match is found, then
125 * the implicit conversions in Section 4.1.10 "Implicit Conversions" will
126 * be applied to the calling arguments if this can make their types match
127 * a signature. In this case, it is a semantic error if there are
128 * multiple ways to apply these conversions to the actual arguments of a
129 * call such that the call can be made to match multiple signatures."
131 foreach_iter(exec_list_iterator
, iter
, signatures
) {
132 ir_function_signature
*const sig
=
133 (ir_function_signature
*) iter
.get();
135 const int score
= parameter_lists_match(& sig
->parameters
,
138 /* If we found an exact match, simply return it */
146 multiple_inexact_matches
= true;
150 /* There is no exact match (we would have returned it by now). If there
151 * are multiple inexact matches, the call is ambiguous, which is an error.
153 * FINISHME: Report a decent error. Returning NULL will likely result in
154 * FINISHME: a "no matching signature" error; it should report that the
155 * FINISHME: call is ambiguous. But reporting errors from here is hard.
157 if (multiple_inexact_matches
)
165 parameter_lists_match_exact(const exec_list
*list_a
, const exec_list
*list_b
)
167 const exec_node
*node_a
= list_a
->head
;
168 const exec_node
*node_b
= list_b
->head
;
171 ; !node_a
->is_tail_sentinel() && !node_b
->is_tail_sentinel()
172 ; node_a
= node_a
->next
, node_b
= node_b
->next
) {
173 ir_variable
*a
= (ir_variable
*) node_a
;
174 ir_variable
*b
= (ir_variable
*) node_b
;
176 /* If the types of the parameters do not match, the parameters lists
179 if (a
->type
!= b
->type
)
183 /* Unless both lists are exhausted, they differ in length and, by
184 * definition, do not match.
186 return (node_a
->is_tail_sentinel() == node_b
->is_tail_sentinel());
189 ir_function_signature
*
190 ir_function::exact_matching_signature(const exec_list
*actual_parameters
)
192 foreach_iter(exec_list_iterator
, iter
, signatures
) {
193 ir_function_signature
*const sig
=
194 (ir_function_signature
*) iter
.get();
196 if (parameter_lists_match_exact(&sig
->parameters
, actual_parameters
))