[Mesa-dev] [PATCH 2/4] glsl: Refactor ast_function.cpp:type_compare() into a glsl_type method
Chad Versace
chad at chad-versace.us
Wed Jul 27 00:00:39 PDT 2011
On a subsequent commit, ast_function_expression::hir() will need access to
type_compare(), but it is defined in a different file. So refactor
type_compare() into glsl_type::implicit_conversion_compare().
CC: Ian Romanick <ian.d.romanick at intel.com>
Signed-off-by: Chad Versace <chad at chad-versace.us>
---
src/glsl/glsl_types.cpp | 59 ++++++++++++++++++++++++++++++++++++++
src/glsl/glsl_types.h | 11 +++++++
src/glsl/ir_function.cpp | 71 ++++-----------------------------------------
3 files changed, 77 insertions(+), 64 deletions(-)
diff --git a/src/glsl/glsl_types.cpp b/src/glsl/glsl_types.cpp
index a5e21bb..5a93180 100644
--- a/src/glsl/glsl_types.cpp
+++ b/src/glsl/glsl_types.cpp
@@ -523,3 +523,62 @@ glsl_type::component_slots() const
return 0;
}
}
+
+int glsl_type::implicit_conversion_compare(const glsl_type *desired) const {
+ /* If the types are the same, they trivially match. */
+ if (this == desired)
+ return 0;
+
+ switch (desired->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 ((this->is_integer() != desired->is_integer())
+ || (this->is_boolean() != desired->is_boolean()))
+ return -1;
+
+ /* FALLTHROUGH */
+
+ case GLSL_TYPE_FLOAT:
+ if ((this->vector_elements != desired->vector_elements)
+ || (this->matrix_columns != desired->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 ((this->base_type != GLSL_TYPE_ARRAY)
+ || (this->length != desired->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.
+ */
+ const glsl_type *this2 = this->fields.array;
+ const glsl_type *desired2 = desired->fields.array;
+ if (this2->implicit_conversion_compare(desired2) == 0)
+ return 0;
+ else
+ return -1;
+ }
+
+ 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;
+ }
+
+ assert(0);
+}
diff --git a/src/glsl/glsl_types.h b/src/glsl/glsl_types.h
index 87f57e7..d791eee 100644
--- a/src/glsl/glsl_types.h
+++ b/src/glsl/glsl_types.h
@@ -224,6 +224,17 @@ struct glsl_type {
*/
unsigned component_slots() const;
+ /**
+ * \brief Check if an implicit type conversion is possible or necessary.
+ *
+ * If the types are identical, so no conversion is necessary, return 0.
+ * If an implicit conversion is legal, return 1.
+ * If an implicit conversion is illegal, return -1.
+ *
+ * See page 20 (26 of the pdf) of the GLSL 1.20 spec, Section 4.1.10 Implicit
+ * Conversions.
+ */
+ int implicit_conversion_compare(const glsl_type *desired) const;
/**
* Query whether or not a type is a scalar (non-vector and non-matrix).
diff --git a/src/glsl/ir_function.cpp b/src/glsl/ir_function.cpp
index 6795988..98f9f51 100644
--- a/src/glsl/ir_function.cpp
+++ b/src/glsl/ir_function.cpp
@@ -24,67 +24,6 @@
#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_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);
-}
-
-
/**
* Called by matching_signature().
*
@@ -131,11 +70,11 @@ parameter_lists_match(const exec_list *list_a, const exec_list *list_b)
case ir_var_const_in:
case ir_var_in:
- score = type_compare(param->type, actual->type);
+ score = actual->type->implicit_conversion_compare(param->type);
break;
case ir_var_out:
- score = type_compare(actual->type, param->type);
+ score = actual->type->implicit_conversion_compare(actual->type);
break;
case ir_var_inout:
@@ -143,7 +82,11 @@ parameter_lists_match(const exec_list *list_a, const exec_list *list_b)
* there is int -> float but no float -> int), inout parameters must
* be exact matches.
*/
- score = (type_compare(actual->type, param->type) == 0) ? 0 : -1;
+ if (param->type->implicit_conversion_compare(actual->type) == 0)
+ score = 0;
+ else
+ score = -1;
+
break;
default:
--
1.7.6
More information about the mesa-dev
mailing list