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"
27 #include "main/errors.h"
30 PARAMETER_LIST_NO_MATCH
,
31 PARAMETER_LIST_EXACT_MATCH
,
32 PARAMETER_LIST_INEXACT_MATCH
/*< Match requires implicit conversion. */
33 } parameter_list_match_t
;
36 * \brief Check if two parameter lists match.
38 * \param list_a Parameters of the function definition.
39 * \param list_b Actual parameters passed to the function.
40 * \see matching_signature()
42 static parameter_list_match_t
43 parameter_lists_match(_mesa_glsl_parse_state
*state
,
44 const exec_list
*list_a
, const exec_list
*list_b
)
46 const exec_node
*node_a
= list_a
->head
;
47 const exec_node
*node_b
= list_b
->head
;
49 /* This is set to true if there is an inexact match requiring an implicit
51 bool inexact_match
= false;
54 ; !node_a
->is_tail_sentinel()
55 ; node_a
= node_a
->next
, node_b
= node_b
->next
) {
56 /* If all of the parameters from the other parameter list have been
57 * exhausted, the lists have different length and, by definition,
60 if (node_b
->is_tail_sentinel())
61 return PARAMETER_LIST_NO_MATCH
;
64 const ir_variable
*const param
= (ir_variable
*) node_a
;
65 const ir_rvalue
*const actual
= (ir_rvalue
*) node_b
;
67 if (param
->type
== actual
->type
)
70 /* Try to find an implicit conversion from actual to param. */
72 switch ((enum ir_variable_mode
)(param
->data
.mode
)) {
75 case ir_var_temporary
:
76 /* These are all error conditions. It is invalid for a parameter to
77 * a function to be declared as auto (not in, out, or inout) or
81 return PARAMETER_LIST_NO_MATCH
;
84 case ir_var_function_in
:
85 if (!actual
->type
->can_implicitly_convert_to(param
->type
, state
))
86 return PARAMETER_LIST_NO_MATCH
;
89 case ir_var_function_out
:
90 if (!param
->type
->can_implicitly_convert_to(actual
->type
, state
))
91 return PARAMETER_LIST_NO_MATCH
;
94 case ir_var_function_inout
:
95 /* Since there are no bi-directional automatic conversions (e.g.,
96 * there is int -> float but no float -> int), inout parameters must
99 return PARAMETER_LIST_NO_MATCH
;
103 return PARAMETER_LIST_NO_MATCH
;
107 /* If all of the parameters from the other parameter list have been
108 * exhausted, the lists have different length and, by definition, do not
111 if (!node_b
->is_tail_sentinel())
112 return PARAMETER_LIST_NO_MATCH
;
115 return PARAMETER_LIST_INEXACT_MATCH
;
117 return PARAMETER_LIST_EXACT_MATCH
;
121 /* Classes of parameter match, sorted (mostly) best matches first.
122 * See is_better_parameter_match() below for the exceptions.
125 PARAMETER_EXACT_MATCH
,
126 PARAMETER_FLOAT_TO_DOUBLE
,
127 PARAMETER_INT_TO_FLOAT
,
128 PARAMETER_INT_TO_DOUBLE
,
129 PARAMETER_OTHER_CONVERSION
,
133 static parameter_match_t
134 get_parameter_match_type(const ir_variable
*param
,
135 const ir_rvalue
*actual
)
137 const glsl_type
*from_type
;
138 const glsl_type
*to_type
;
140 if (param
->data
.mode
== ir_var_function_out
) {
141 from_type
= param
->type
;
142 to_type
= actual
->type
;
144 from_type
= actual
->type
;
145 to_type
= param
->type
;
148 if (from_type
== to_type
)
149 return PARAMETER_EXACT_MATCH
;
151 /* XXX: When ARB_gpu_shader_fp64 support is added, check for float->double,
152 * and int/uint->double conversions
155 if (to_type
->base_type
== GLSL_TYPE_FLOAT
)
156 return PARAMETER_INT_TO_FLOAT
;
158 /* int -> uint and any other oddball conversions */
159 return PARAMETER_OTHER_CONVERSION
;
164 is_better_parameter_match(parameter_match_t a_match
,
165 parameter_match_t b_match
)
167 /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec):
169 * 1. An exact match is better than a match involving any implicit
172 * 2. A match involving an implicit conversion from float to double
173 * is better than match involving any other implicit conversion.
175 * [XXX: Not in GLSL 4.0: Only in ARB_gpu_shader5:
176 * 3. A match involving an implicit conversion from either int or uint
177 * to float is better than a match involving an implicit conversion
178 * from either int or uint to double.]
180 * If none of the rules above apply to a particular pair of conversions,
181 * neither conversion is considered better than the other.
185 * Notably, the int->uint conversion is *not* considered to be better
186 * or worse than int/uint->float or int/uint->double.
189 if (a_match
>= PARAMETER_INT_TO_FLOAT
&& b_match
== PARAMETER_OTHER_CONVERSION
)
192 return a_match
< b_match
;
197 is_best_inexact_overload(const exec_list
*actual_parameters
,
198 ir_function_signature
**matches
,
200 ir_function_signature
*sig
)
202 /* From section 6.1 of the GLSL 4.00 spec (and the ARB_gpu_shader5 spec):
204 * "A function definition A is considered a better
205 * match than function definition B if:
207 * * for at least one function argument, the conversion for that argument
208 * in A is better than the corresponding conversion in B; and
210 * * there is no function argument for which the conversion in B is better
211 * than the corresponding conversion in A.
213 * If a single function definition is considered a better match than every
214 * other matching function definition, it will be used. Otherwise, a
215 * semantic error occurs and the shader will fail to compile."
217 for (ir_function_signature
**other
= matches
;
218 other
< matches
+ num_matches
; other
++) {
222 const exec_node
*node_a
= sig
->parameters
.head
;
223 const exec_node
*node_b
= (*other
)->parameters
.head
;
224 const exec_node
*node_p
= actual_parameters
->head
;
226 bool better_for_some_parameter
= false;
229 ; !node_a
->is_tail_sentinel()
230 ; node_a
= node_a
->next
,
231 node_b
= node_b
->next
,
232 node_p
= node_p
->next
) {
233 parameter_match_t a_match
= get_parameter_match_type(
234 (const ir_variable
*)node_a
,
235 (const ir_rvalue
*)node_p
);
236 parameter_match_t b_match
= get_parameter_match_type(
237 (const ir_variable
*)node_b
,
238 (const ir_rvalue
*)node_p
);
240 if (is_better_parameter_match(a_match
, b_match
))
241 better_for_some_parameter
= true;
243 if (is_better_parameter_match(b_match
, a_match
))
244 return false; /* B is better for this parameter */
247 if (!better_for_some_parameter
)
248 return false; /* A must be better than B for some parameter */
256 static ir_function_signature
*
257 choose_best_inexact_overload(_mesa_glsl_parse_state
*state
,
258 const exec_list
*actual_parameters
,
259 ir_function_signature
**matches
,
262 if (num_matches
== 0)
265 if (num_matches
== 1)
268 /* Without GLSL 4.0 / ARB_gpu_shader5, there is no overload resolution
269 * among multiple inexact matches. Note that state may be NULL here if
270 * called from the linker; in that case we assume everything supported in
271 * any GLSL version is available. */
272 if (!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) {
273 for (ir_function_signature
**sig
= matches
; sig
< matches
+ num_matches
; sig
++) {
274 if (is_best_inexact_overload(actual_parameters
, matches
, num_matches
, *sig
))
279 return NULL
; /* no best candidate */
283 ir_function_signature
*
284 ir_function::matching_signature(_mesa_glsl_parse_state
*state
,
285 const exec_list
*actual_parameters
,
289 return matching_signature(state
, actual_parameters
, allow_builtins
,
293 ir_function_signature
*
294 ir_function::matching_signature(_mesa_glsl_parse_state
*state
,
295 const exec_list
*actual_parameters
,
299 ir_function_signature
**inexact_matches
= NULL
;
300 ir_function_signature
**inexact_matches_temp
;
301 ir_function_signature
*match
= NULL
;
302 int num_inexact_matches
= 0;
304 /* From page 42 (page 49 of the PDF) of the GLSL 1.20 spec:
306 * "If an exact match is found, the other signatures are ignored, and
307 * the exact match is used. Otherwise, if no exact match is found, then
308 * the implicit conversions in Section 4.1.10 "Implicit Conversions" will
309 * be applied to the calling arguments if this can make their types match
310 * a signature. In this case, it is a semantic error if there are
311 * multiple ways to apply these conversions to the actual arguments of a
312 * call such that the call can be made to match multiple signatures."
314 foreach_in_list(ir_function_signature
, sig
, &this->signatures
) {
315 /* Skip over any built-ins that aren't available in this shader. */
316 if (sig
->is_builtin() && (!allow_builtins
||
317 !sig
->is_builtin_available(state
)))
320 switch (parameter_lists_match(state
, & sig
->parameters
, actual_parameters
)) {
321 case PARAMETER_LIST_EXACT_MATCH
:
323 free(inexact_matches
);
325 case PARAMETER_LIST_INEXACT_MATCH
:
326 inexact_matches_temp
= (ir_function_signature
**)
327 realloc(inexact_matches
,
328 sizeof(*inexact_matches
) *
329 (num_inexact_matches
+ 1));
330 if (inexact_matches_temp
== NULL
) {
331 _mesa_error_no_memory(__func__
);
332 free(inexact_matches
);
335 inexact_matches
= inexact_matches_temp
;
336 inexact_matches
[num_inexact_matches
++] = sig
;
338 case PARAMETER_LIST_NO_MATCH
:
346 /* There is no exact match (we would have returned it by now). If there
347 * are multiple inexact matches, the call is ambiguous, which is an error.
349 * FINISHME: Report a decent error. Returning NULL will likely result in
350 * FINISHME: a "no matching signature" error; it should report that the
351 * FINISHME: call is ambiguous. But reporting errors from here is hard.
355 match
= choose_best_inexact_overload(state
, actual_parameters
,
356 inexact_matches
, num_inexact_matches
);
358 free(inexact_matches
);
364 parameter_lists_match_exact(const exec_list
*list_a
, const exec_list
*list_b
)
366 const exec_node
*node_a
= list_a
->head
;
367 const exec_node
*node_b
= list_b
->head
;
370 ; !node_a
->is_tail_sentinel() && !node_b
->is_tail_sentinel()
371 ; node_a
= node_a
->next
, node_b
= node_b
->next
) {
372 ir_variable
*a
= (ir_variable
*) node_a
;
373 ir_variable
*b
= (ir_variable
*) node_b
;
375 /* If the types of the parameters do not match, the parameters lists
378 if (a
->type
!= b
->type
)
382 /* Unless both lists are exhausted, they differ in length and, by
383 * definition, do not match.
385 return (node_a
->is_tail_sentinel() == node_b
->is_tail_sentinel());
388 ir_function_signature
*
389 ir_function::exact_matching_signature(_mesa_glsl_parse_state
*state
,
390 const exec_list
*actual_parameters
)
392 foreach_in_list(ir_function_signature
, sig
, &this->signatures
) {
393 /* Skip over any built-ins that aren't available in this shader. */
394 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
397 if (parameter_lists_match_exact(&sig
->parameters
, actual_parameters
))