Use line number information from entire function expression
[mesa.git] / src / glsl / ir_function.cpp
index dfdec144b2feb3ace2740ac8975fe0dc2b900ee4..bd5318d231bcef6df7302430a19a56f4a77a3ea4 100644 (file)
 #include "glsl_types.h"
 #include "ir.h"
 
-int
-type_compare(const glsl_type *a, const glsl_type *b)
-{
-   /* If the types are the same, they trivially match.
-    */
-   if (a == b)
-      return 0;
-
-   switch (a->base_type) {
-   case GLSL_TYPE_UINT:
-   case GLSL_TYPE_INT:
-   case GLSL_TYPE_BOOL:
-      /* There is no implicit conversion to or from integer types or bool.
-       */
-      if ((a->is_integer() != b->is_integer())
-         || (a->is_boolean() != b->is_boolean()))
-        return -1;
-
-      /* FALLTHROUGH */
-
-   case GLSL_TYPE_FLOAT:
-      if ((a->vector_elements != b->vector_elements)
-         || (a->matrix_columns != b->matrix_columns))
-        return -1;
-
-      return 1;
-
-   case GLSL_TYPE_SAMPLER:
-   case GLSL_TYPE_STRUCT:
-      /* Samplers and structures must match exactly.
-       */
-      return -1;
-
-   case GLSL_TYPE_ARRAY:
-      if ((b->base_type != GLSL_TYPE_ARRAY)
-         || (a->length != b->length))
-        return -1;
-
-      /* From GLSL 1.50 spec, page 27 (page 33 of the PDF):
-       *    "There are no implicit array or structure conversions."
-       *
-       * If the comparison of the array element types detects that a conversion
-       * would be required, the array types do not match.
-       */
-      return (type_compare(a->fields.array, b->fields.array) == 0) ? 0 : -1;
-
-   case GLSL_TYPE_FUNCTION:
-   case GLSL_TYPE_VOID:
-   case GLSL_TYPE_ERROR:
-   default:
-      /* These are all error conditions.  It is invalid for a parameter to
-       * a function to be declared as error, void, or a function.
-       */
-      return -1;
-   }
-
-   /* This point should be unreachable.
-    */
-   assert(0);
-}
-
-
-static int
+typedef enum {
+   PARAMETER_LIST_NO_MATCH,
+   PARAMETER_LIST_EXACT_MATCH,
+   PARAMETER_LIST_INEXACT_MATCH /*< Match requires implicit conversion. */
+} parameter_list_match_t;
+
+/**
+ * \brief Check if two parameter lists match.
+ *
+ * \param list_a Parameters of the function definition.
+ * \param list_b Actual parameters passed to the function.
+ * \see matching_signature()
+ */
+static parameter_list_match_t
 parameter_lists_match(const exec_list *list_a, const exec_list *list_b)
 {
    const exec_node *node_a = list_a->head;
    const exec_node *node_b = list_b->head;
-   int total_score = 0;
+
+   /* This is set to true if there is an inexact match requiring an implicit
+    * conversion. */
+   bool inexact_match = false;
 
    for (/* empty */
        ; !node_a->is_tail_sentinel()
@@ -101,19 +55,18 @@ parameter_lists_match(const exec_list *list_a, const exec_list *list_b)
        * do not match.
        */
       if (node_b->is_tail_sentinel())
-        return -1;
+        return PARAMETER_LIST_NO_MATCH;
 
 
       const ir_variable *const param = (ir_variable *) node_a;
-      const ir_instruction *const actual = (ir_instruction *) node_b;
+      const ir_rvalue *const actual = (ir_rvalue *) node_b;
 
-      /* Determine whether or not the types match.  If the types are an
-       * exact match, the match score is zero.  If the types don't match
-       * but the actual parameter can be coerced to the type of the declared
-       * parameter, the match score is one.
-       */
-      int score;
-      switch ((enum ir_variable_mode)(param->mode)) {
+      if (param->type == actual->type)
+        continue;
+
+      /* Try to find an implicit conversion from actual to param. */
+      inexact_match = true;
+      switch ((enum ir_variable_mode)(param->data.mode)) {
       case ir_var_auto:
       case ir_var_uniform:
       case ir_var_temporary:
@@ -122,32 +75,30 @@ parameter_lists_match(const exec_list *list_a, const exec_list *list_b)
          * as uniform.
          */
         assert(0);
-        return -1;
+        return PARAMETER_LIST_NO_MATCH;
 
-      case ir_var_in:
-        score = type_compare(param->type, actual->type);
+      case ir_var_const_in:
+      case ir_var_function_in:
+        if (!actual->type->can_implicitly_convert_to(param->type))
+           return PARAMETER_LIST_NO_MATCH;
         break;
 
-      case ir_var_out:
-        score = type_compare(actual->type, param->type);
+      case ir_var_function_out:
+        if (!param->type->can_implicitly_convert_to(actual->type))
+           return PARAMETER_LIST_NO_MATCH;
         break;
 
-      case ir_var_inout:
+      case ir_var_function_inout:
         /* Since there are no bi-directional automatic conversions (e.g.,
          * there is int -> float but no float -> int), inout parameters must
          * be exact matches.
          */
-        score = (type_compare(actual->type, param->type) == 0) ? 0 : -1;
-        break;
+        return PARAMETER_LIST_NO_MATCH;
 
       default:
         assert(false);
+        return PARAMETER_LIST_NO_MATCH;
       }
-
-      if (score < 0)
-        return -1;
-
-      total_score += score;
    }
 
    /* If all of the parameters from the other parameter list have been
@@ -155,35 +106,79 @@ parameter_lists_match(const exec_list *list_a, const exec_list *list_b)
     * match.
     */
    if (!node_b->is_tail_sentinel())
-      return -1;
+      return PARAMETER_LIST_NO_MATCH;
 
-   return total_score;
+   if (inexact_match)
+      return PARAMETER_LIST_INEXACT_MATCH;
+   else
+      return PARAMETER_LIST_EXACT_MATCH;
 }
 
 
 ir_function_signature *
-ir_function::matching_signature(const exec_list *actual_parameters)
+ir_function::matching_signature(_mesa_glsl_parse_state *state,
+                                const exec_list *actual_parameters)
 {
-   ir_function_signature *match = NULL;
+   bool is_exact;
+   return matching_signature(state, actual_parameters, &is_exact);
+}
 
+ir_function_signature *
+ir_function::matching_signature(_mesa_glsl_parse_state *state,
+                                const exec_list *actual_parameters,
+                               bool *is_exact)
+{
+   ir_function_signature *match = NULL;
+   bool multiple_inexact_matches = false;
+
+   /* 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_iter(exec_list_iterator, iter, signatures) {
       ir_function_signature *const sig =
         (ir_function_signature *) iter.get();
 
-      const int score = parameter_lists_match(& sig->parameters,
-                                             actual_parameters);
+      /* Skip over any built-ins that aren't available in this shader. */
+      if (sig->is_builtin() && !sig->is_builtin_available(state))
+         continue;
 
-      if (score == 0)
+      switch (parameter_lists_match(& sig->parameters, actual_parameters)) {
+      case PARAMETER_LIST_EXACT_MATCH:
+        *is_exact = true;
         return sig;
-
-      if (score > 0) {
-        if (match != NULL)
-           return NULL;
-
-        match = sig;
+      case PARAMETER_LIST_INEXACT_MATCH:
+        if (match == NULL)
+           match = sig;
+        else
+           multiple_inexact_matches = true;
+        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;
+
+   if (multiple_inexact_matches)
+      return NULL;
+
    return match;
 }
 
@@ -214,12 +209,17 @@ parameter_lists_match_exact(const exec_list *list_a, const exec_list *list_b)
 }
 
 ir_function_signature *
-ir_function::exact_matching_signature(const exec_list *actual_parameters)
+ir_function::exact_matching_signature(_mesa_glsl_parse_state *state,
+                                      const exec_list *actual_parameters)
 {
    foreach_iter(exec_list_iterator, iter, signatures) {
       ir_function_signature *const sig =
         (ir_function_signature *) iter.get();
 
+      /* Skip over any built-ins that aren't available in this shader. */
+      if (sig->is_builtin() && !sig->is_builtin_available(state))
+         continue;
+
       if (parameter_lists_match_exact(&sig->parameters, actual_parameters))
         return sig;
    }