+ return true;
+}
+
+
+static ir_function_signature *
+choose_best_inexact_overload(_mesa_glsl_parse_state *state,
+ const exec_list *actual_parameters,
+ ir_function_signature **matches,
+ int num_matches)
+{
+ if (num_matches == 0)
+ return NULL;
+
+ if (num_matches == 1)
+ return *matches;
+
+ /* Without GLSL 4.0 / ARB_gpu_shader5, there is no overload resolution
+ * among multiple inexact matches. Note that state may be NULL here if
+ * called from the linker; in that case we assume everything supported in
+ * any GLSL version is available. */
+ if (!state || state->is_version(400, 0) || state->ARB_gpu_shader5_enable) {
+ for (ir_function_signature **sig = matches; sig < matches + num_matches; sig++) {
+ if (is_best_inexact_overload(actual_parameters, matches, num_matches, *sig))
+ return *sig;
+ }
+ }
+
+ return NULL; /* no best candidate */
+}
+
+
+ir_function_signature *
+ir_function::matching_signature(_mesa_glsl_parse_state *state,
+ const exec_list *actual_parameters,
+ bool allow_builtins)
+{
+ bool is_exact;
+ return matching_signature(state, actual_parameters, allow_builtins,
+ &is_exact);
+}
+
+ir_function_signature *
+ir_function::matching_signature(_mesa_glsl_parse_state *state,
+ const exec_list *actual_parameters,
+ bool allow_builtins,
+ bool *is_exact)
+{
+ ir_function_signature **inexact_matches = NULL;
+ ir_function_signature **inexact_matches_temp;
+ ir_function_signature *match = NULL;
+ int num_inexact_matches = 0;
+
+ /* From page 42 (page 49 of the PDF) of the GLSL 1.20 spec:
+ *
+ * "If an exact match is found, the other signatures are ignored, and
+ * the exact match is used. Otherwise, if no exact match is found, then
+ * the implicit conversions in Section 4.1.10 "Implicit Conversions" will
+ * be applied to the calling arguments if this can make their types match
+ * a signature. In this case, it is a semantic error if there are
+ * multiple ways to apply these conversions to the actual arguments of a
+ * call such that the call can be made to match multiple signatures."
+ */
+ foreach_in_list(ir_function_signature, sig, &this->signatures) {
+ /* Skip over any built-ins that aren't available in this shader. */
+ if (sig->is_builtin() && (!allow_builtins ||
+ !sig->is_builtin_available(state)))
+ continue;
+
+ switch (parameter_lists_match(state, & sig->parameters, actual_parameters)) {
+ case PARAMETER_LIST_EXACT_MATCH:
+ *is_exact = true;
+ free(inexact_matches);
+ return sig;
+ case PARAMETER_LIST_INEXACT_MATCH:
+ inexact_matches_temp = (ir_function_signature **)
+ realloc(inexact_matches,
+ sizeof(*inexact_matches) *
+ (num_inexact_matches + 1));
+ if (inexact_matches_temp == NULL) {
+ _mesa_error_no_memory(__func__);
+ free(inexact_matches);
+ return NULL;
+ }
+ inexact_matches = inexact_matches_temp;
+ inexact_matches[num_inexact_matches++] = sig;
+ continue;
+ case PARAMETER_LIST_NO_MATCH:
+ continue;
+ default:
+ assert(false);
+ return NULL;
+ }
+ }
+
+ /* There is no exact match (we would have returned it by now). If there
+ * are multiple inexact matches, the call is ambiguous, which is an error.
+ *
+ * FINISHME: Report a decent error. Returning NULL will likely result in
+ * FINISHME: a "no matching signature" error; it should report that the
+ * FINISHME: call is ambiguous. But reporting errors from here is hard.
+ */
+ *is_exact = false;
+
+ match = choose_best_inexact_overload(state, actual_parameters,
+ inexact_matches, num_inexact_matches);
+
+ free(inexact_matches);