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"
26 #include "glsl_parser_extras.h"
29 PARAMETER_LIST_NO_MATCH
,
30 PARAMETER_LIST_EXACT_MATCH
,
31 PARAMETER_LIST_INEXACT_MATCH
/*< Match requires implicit conversion. */
32 } parameter_list_match_t
;
35 * \brief Check if two parameter lists match.
37 * \param list_a Parameters of the function definition.
38 * \param list_b Actual parameters passed to the function.
39 * \see matching_signature()
41 static parameter_list_match_t
42 parameter_lists_match(_mesa_glsl_parse_state
*state
,
43 const exec_list
*list_a
, const exec_list
*list_b
)
45 const exec_node
*node_a
= list_a
->head
;
46 const exec_node
*node_b
= list_b
->head
;
48 /* This is set to true if there is an inexact match requiring an implicit
50 bool inexact_match
= false;
53 ; !node_a
->is_tail_sentinel()
54 ; node_a
= node_a
->next
, node_b
= node_b
->next
) {
55 /* If all of the parameters from the other parameter list have been
56 * exhausted, the lists have different length and, by definition,
59 if (node_b
->is_tail_sentinel())
60 return PARAMETER_LIST_NO_MATCH
;
63 const ir_variable
*const param
= (ir_variable
*) node_a
;
64 const ir_rvalue
*const actual
= (ir_rvalue
*) node_b
;
66 if (param
->type
== actual
->type
)
69 /* Try to find an implicit conversion from actual to param. */
71 switch ((enum ir_variable_mode
)(param
->data
.mode
)) {
74 case ir_var_temporary
:
75 /* These are all error conditions. It is invalid for a parameter to
76 * a function to be declared as auto (not in, out, or inout) or
80 return PARAMETER_LIST_NO_MATCH
;
83 case ir_var_function_in
:
84 if (!actual
->type
->can_implicitly_convert_to(param
->type
, state
))
85 return PARAMETER_LIST_NO_MATCH
;
88 case ir_var_function_out
:
89 if (!param
->type
->can_implicitly_convert_to(actual
->type
, state
))
90 return PARAMETER_LIST_NO_MATCH
;
93 case ir_var_function_inout
:
94 /* Since there are no bi-directional automatic conversions (e.g.,
95 * there is int -> float but no float -> int), inout parameters must
98 return PARAMETER_LIST_NO_MATCH
;
102 return PARAMETER_LIST_NO_MATCH
;
106 /* If all of the parameters from the other parameter list have been
107 * exhausted, the lists have different length and, by definition, do not
110 if (!node_b
->is_tail_sentinel())
111 return PARAMETER_LIST_NO_MATCH
;
114 return PARAMETER_LIST_INEXACT_MATCH
;
116 return PARAMETER_LIST_EXACT_MATCH
;
120 /* Classes of parameter match, sorted (mostly) best matches first.
121 * See is_better_parameter_match() below for the exceptions.
124 PARAMETER_EXACT_MATCH
,
125 PARAMETER_FLOAT_TO_DOUBLE
,
126 PARAMETER_INT_TO_FLOAT
,
127 PARAMETER_INT_TO_DOUBLE
,
128 PARAMETER_OTHER_CONVERSION
,
132 static parameter_match_t
133 get_parameter_match_type(const ir_variable
*param
,
134 const ir_rvalue
*actual
)
136 const glsl_type
*from_type
;
137 const glsl_type
*to_type
;
139 if (param
->data
.mode
== ir_var_function_out
) {
140 from_type
= param
->type
;
141 to_type
= actual
->type
;
143 from_type
= actual
->type
;
144 to_type
= param
->type
;
147 if (from_type
== to_type
)
148 return PARAMETER_EXACT_MATCH
;
150 /* XXX: When ARB_gpu_shader_fp64 support is added, check for float->double,
151 * and int/uint->double conversions
154 if (to_type
->base_type
== GLSL_TYPE_FLOAT
)
155 return PARAMETER_INT_TO_FLOAT
;
157 /* int -> uint and any other oddball conversions */
158 return PARAMETER_OTHER_CONVERSION
;
163 is_better_parameter_match(parameter_match_t a_match
,
164 parameter_match_t b_match
)
166 /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec):
168 * 1. An exact match is better than a match involving any implicit
171 * 2. A match involving an implicit conversion from float to double
172 * is better than match involving any other implicit conversion.
174 * [XXX: Not in GLSL 4.0: Only in ARB_gpu_shader5:
175 * 3. A match involving an implicit conversion from either int or uint
176 * to float is better than a match involving an implicit conversion
177 * from either int or uint to double.]
179 * If none of the rules above apply to a particular pair of conversions,
180 * neither conversion is considered better than the other.
184 * Notably, the int->uint conversion is *not* considered to be better
185 * or worse than int/uint->float or int/uint->double.
188 if (a_match
>= PARAMETER_INT_TO_FLOAT
&& b_match
== PARAMETER_OTHER_CONVERSION
)
191 return a_match
< b_match
;
196 is_best_inexact_overload(const exec_list
*actual_parameters
,
197 ir_function_signature
**matches
,
199 ir_function_signature
*sig
)
201 /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec):
203 * "A function definition A is considered a better
204 * match than function definition B if:
206 * * for at least one function argument, the conversion for that argument
207 * in A is better than the corresponding conversion in B; and
209 * * there is no function argument for which the conversion in B is better
210 * than the corresponding conversion in A.
212 * If a single function definition is considered a better match than every
213 * other matching function definition, it will be used. Otherwise, a
214 * semantic error occurs and the shader will fail to compile."
216 for (ir_function_signature
**other
= matches
;
217 other
< matches
+ num_matches
; other
++) {
221 const exec_node
*node_a
= sig
->parameters
.head
;
222 const exec_node
*node_b
= (*other
)->parameters
.head
;
223 const exec_node
*node_p
= actual_parameters
->head
;
225 bool better_for_some_parameter
= false;
228 ; !node_a
->is_tail_sentinel()
229 ; node_a
= node_a
->next
,
230 node_b
= node_b
->next
,
231 node_p
= node_p
->next
) {
232 parameter_match_t a_match
= get_parameter_match_type(
233 (const ir_variable
*)node_a
,
234 (const ir_rvalue
*)node_p
);
235 parameter_match_t b_match
= get_parameter_match_type(
236 (const ir_variable
*)node_b
,
237 (const ir_rvalue
*)node_p
);
239 if (is_better_parameter_match(a_match
, b_match
))
240 better_for_some_parameter
= true;
242 if (is_better_parameter_match(b_match
, a_match
))
243 return false; /* B is better for this parameter */
246 if (!better_for_some_parameter
)
247 return false; /* A must be better than B for some parameter */
255 static ir_function_signature
*
256 choose_best_inexact_overload(_mesa_glsl_parse_state
*state
,
257 const exec_list
*actual_parameters
,
258 ir_function_signature
**matches
,
261 if (num_matches
== 0)
264 if (num_matches
== 1)
267 /* Without GLSL 4.0 / ARB_gpu_shader5, there is no overload resolution
268 * among multiple inexact matches. Note that state may be NULL here if
269 * called from the linker; in that case we assume everything supported in
270 * any GLSL version is available. */
271 if (!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) {
272 for (ir_function_signature
**sig
= matches
; sig
< matches
+ num_matches
; sig
++) {
273 if (is_best_inexact_overload(actual_parameters
, matches
, num_matches
, *sig
))
278 return NULL
; /* no best candidate */
282 ir_function_signature
*
283 ir_function::matching_signature(_mesa_glsl_parse_state
*state
,
284 const exec_list
*actual_parameters
,
288 return matching_signature(state
, actual_parameters
, allow_builtins
,
292 ir_function_signature
*
293 ir_function::matching_signature(_mesa_glsl_parse_state
*state
,
294 const exec_list
*actual_parameters
,
298 ir_function_signature
**inexact_matches
= NULL
;
299 ir_function_signature
*match
= NULL
;
300 int num_inexact_matches
= 0;
302 /* From page 42 (page 49 of the PDF) of the GLSL 1.20 spec:
304 * "If an exact match is found, the other signatures are ignored, and
305 * the exact match is used. Otherwise, if no exact match is found, then
306 * the implicit conversions in Section 4.1.10 "Implicit Conversions" will
307 * be applied to the calling arguments if this can make their types match
308 * a signature. In this case, it is a semantic error if there are
309 * multiple ways to apply these conversions to the actual arguments of a
310 * call such that the call can be made to match multiple signatures."
312 foreach_in_list(ir_function_signature
, sig
, &this->signatures
) {
313 /* Skip over any built-ins that aren't available in this shader. */
314 if (sig
->is_builtin() && (!allow_builtins
||
315 !sig
->is_builtin_available(state
)))
318 switch (parameter_lists_match(state
, & sig
->parameters
, actual_parameters
)) {
319 case PARAMETER_LIST_EXACT_MATCH
:
321 free(inexact_matches
);
323 case PARAMETER_LIST_INEXACT_MATCH
:
324 inexact_matches
= (ir_function_signature
**)
325 realloc(inexact_matches
,
326 sizeof(*inexact_matches
) *
327 (num_inexact_matches
+ 1));
328 assert(inexact_matches
);
329 inexact_matches
[num_inexact_matches
++] = sig
;
331 case PARAMETER_LIST_NO_MATCH
:
339 /* There is no exact match (we would have returned it by now). If there
340 * are multiple inexact matches, the call is ambiguous, which is an error.
342 * FINISHME: Report a decent error. Returning NULL will likely result in
343 * FINISHME: a "no matching signature" error; it should report that the
344 * FINISHME: call is ambiguous. But reporting errors from here is hard.
348 match
= choose_best_inexact_overload(state
, actual_parameters
,
349 inexact_matches
, num_inexact_matches
);
351 free(inexact_matches
);
357 parameter_lists_match_exact(const exec_list
*list_a
, const exec_list
*list_b
)
359 const exec_node
*node_a
= list_a
->head
;
360 const exec_node
*node_b
= list_b
->head
;
363 ; !node_a
->is_tail_sentinel() && !node_b
->is_tail_sentinel()
364 ; node_a
= node_a
->next
, node_b
= node_b
->next
) {
365 ir_variable
*a
= (ir_variable
*) node_a
;
366 ir_variable
*b
= (ir_variable
*) node_b
;
368 /* If the types of the parameters do not match, the parameters lists
371 if (a
->type
!= b
->type
)
375 /* Unless both lists are exhausted, they differ in length and, by
376 * definition, do not match.
378 return (node_a
->is_tail_sentinel() == node_b
->is_tail_sentinel());
381 ir_function_signature
*
382 ir_function::exact_matching_signature(_mesa_glsl_parse_state
*state
,
383 const exec_list
*actual_parameters
)
385 foreach_in_list(ir_function_signature
, sig
, &this->signatures
) {
386 /* Skip over any built-ins that aren't available in this shader. */
387 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
390 if (parameter_lists_match_exact(&sig
->parameters
, actual_parameters
))