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 PARAMETER_LIST_NO_MATCH
,
29 PARAMETER_LIST_EXACT_MATCH
,
30 PARAMETER_LIST_INEXACT_MATCH
/*< Match requires implicit conversion. */
31 } parameter_list_match_t
;
34 * \brief Check if two parameter lists match.
36 * \param list_a Parameters of the function definition.
37 * \param list_b Actual parameters passed to the function.
38 * \see matching_signature()
40 static parameter_list_match_t
41 parameter_lists_match(const exec_list
*list_a
, const exec_list
*list_b
)
43 const exec_node
*node_a
= list_a
->head
;
44 const exec_node
*node_b
= list_b
->head
;
46 /* This is set to true if there is an inexact match requiring an implicit
48 bool inexact_match
= false;
51 ; !node_a
->is_tail_sentinel()
52 ; node_a
= node_a
->next
, node_b
= node_b
->next
) {
53 /* If all of the parameters from the other parameter list have been
54 * exhausted, the lists have different length and, by definition,
57 if (node_b
->is_tail_sentinel())
58 return PARAMETER_LIST_NO_MATCH
;
61 const ir_variable
*const param
= (ir_variable
*) node_a
;
62 const ir_rvalue
*const actual
= (ir_rvalue
*) node_b
;
64 if (param
->type
== actual
->type
)
67 /* Try to find an implicit conversion from actual to param. */
69 switch ((enum ir_variable_mode
)(param
->mode
)) {
72 case ir_var_temporary
:
73 /* These are all error conditions. It is invalid for a parameter to
74 * a function to be declared as auto (not in, out, or inout) or
78 return PARAMETER_LIST_NO_MATCH
;
81 case ir_var_function_in
:
82 if (!actual
->type
->can_implicitly_convert_to(param
->type
))
83 return PARAMETER_LIST_NO_MATCH
;
86 case ir_var_function_out
:
87 if (!param
->type
->can_implicitly_convert_to(actual
->type
))
88 return PARAMETER_LIST_NO_MATCH
;
91 case ir_var_function_inout
:
92 /* Since there are no bi-directional automatic conversions (e.g.,
93 * there is int -> float but no float -> int), inout parameters must
96 return PARAMETER_LIST_NO_MATCH
;
100 return PARAMETER_LIST_NO_MATCH
;
104 /* If all of the parameters from the other parameter list have been
105 * exhausted, the lists have different length and, by definition, do not
108 if (!node_b
->is_tail_sentinel())
109 return PARAMETER_LIST_NO_MATCH
;
112 return PARAMETER_LIST_INEXACT_MATCH
;
114 return PARAMETER_LIST_EXACT_MATCH
;
118 ir_function_signature
*
119 ir_function::matching_signature(_mesa_glsl_parse_state
*state
,
120 const exec_list
*actual_parameters
)
123 return matching_signature(state
, actual_parameters
, &is_exact
);
126 ir_function_signature
*
127 ir_function::matching_signature(_mesa_glsl_parse_state
*state
,
128 const exec_list
*actual_parameters
,
131 ir_function_signature
*match
= NULL
;
132 bool multiple_inexact_matches
= false;
134 /* From page 42 (page 49 of the PDF) of the GLSL 1.20 spec:
136 * "If an exact match is found, the other signatures are ignored, and
137 * the exact match is used. Otherwise, if no exact match is found, then
138 * the implicit conversions in Section 4.1.10 "Implicit Conversions" will
139 * be applied to the calling arguments if this can make their types match
140 * a signature. In this case, it is a semantic error if there are
141 * multiple ways to apply these conversions to the actual arguments of a
142 * call such that the call can be made to match multiple signatures."
144 foreach_iter(exec_list_iterator
, iter
, signatures
) {
145 ir_function_signature
*const sig
=
146 (ir_function_signature
*) iter
.get();
148 switch (parameter_lists_match(& sig
->parameters
, actual_parameters
)) {
149 case PARAMETER_LIST_EXACT_MATCH
:
152 case PARAMETER_LIST_INEXACT_MATCH
:
156 multiple_inexact_matches
= true;
158 case PARAMETER_LIST_NO_MATCH
:
166 /* There is no exact match (we would have returned it by now). If there
167 * are multiple inexact matches, the call is ambiguous, which is an error.
169 * FINISHME: Report a decent error. Returning NULL will likely result in
170 * FINISHME: a "no matching signature" error; it should report that the
171 * FINISHME: call is ambiguous. But reporting errors from here is hard.
175 if (multiple_inexact_matches
)
183 parameter_lists_match_exact(const exec_list
*list_a
, const exec_list
*list_b
)
185 const exec_node
*node_a
= list_a
->head
;
186 const exec_node
*node_b
= list_b
->head
;
189 ; !node_a
->is_tail_sentinel() && !node_b
->is_tail_sentinel()
190 ; node_a
= node_a
->next
, node_b
= node_b
->next
) {
191 ir_variable
*a
= (ir_variable
*) node_a
;
192 ir_variable
*b
= (ir_variable
*) node_b
;
194 /* If the types of the parameters do not match, the parameters lists
197 if (a
->type
!= b
->type
)
201 /* Unless both lists are exhausted, they differ in length and, by
202 * definition, do not match.
204 return (node_a
->is_tail_sentinel() == node_b
->is_tail_sentinel());
207 ir_function_signature
*
208 ir_function::exact_matching_signature(_mesa_glsl_parse_state
*state
,
209 const exec_list
*actual_parameters
)
211 foreach_iter(exec_list_iterator
, iter
, signatures
) {
212 ir_function_signature
*const sig
=
213 (ir_function_signature
*) iter
.get();
215 if (parameter_lists_match_exact(&sig
->parameters
, actual_parameters
))