[Mesa-dev] [PATCH 5/8] glsl: Minor code compaction in _mesa_ast_array_index_to_hir

Ian Romanick idr at freedesktop.org
Mon Apr 1 11:25:21 PDT 2013


From: Ian Romanick <ian.d.romanick at intel.com>

Also, document the reason for not checking for type->is_array in some of
the bound-checking cases.

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
---
 src/glsl/ast_array_index.cpp | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/src/glsl/ast_array_index.cpp b/src/glsl/ast_array_index.cpp
index 9331bef..5f3ae8e 100644
--- a/src/glsl/ast_array_index.cpp
+++ b/src/glsl/ast_array_index.cpp
@@ -60,17 +60,9 @@ _mesa_ast_array_index_to_hir(void *mem_ctx,
    ir_constant *const const_index = idx->constant_expression_value();
    if (const_index != NULL) {
       const int idx = const_index->value.i[0];
-      const char *type_name;
+      const char *type_name = "error";
       unsigned bound = 0;
 
-      if (array->type->is_matrix()) {
-	 type_name = "matrix";
-      } else if (array->type->is_vector()) {
-	 type_name = "vector";
-      } else {
-	 type_name = "array";
-      }
-
       /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
        *
        *    "It is illegal to declare an array with a size, and then
@@ -81,15 +73,22 @@ _mesa_ast_array_index_to_hir(void *mem_ctx,
        */
       if (array->type->is_matrix()) {
 	 if (array->type->row_type()->vector_elements <= idx) {
+	    type_name = "matrix";
 	    bound = array->type->row_type()->vector_elements;
 	 }
       } else if (array->type->is_vector()) {
 	 if (array->type->vector_elements <= idx) {
+	    type_name = "vector";
 	    bound = array->type->vector_elements;
 	 }
       } else {
+	 /* glsl_type::array_size() returns 0 for non-array types.  This means
+	  * that we don't need to verify that the type is an array before
+	  * doing the bounds checking.
+	  */
 	 if ((array->type->array_size() > 0)
 	     && (array->type->array_size() <= idx)) {
+	    type_name = "array";
 	    bound = array->type->array_size();
 	 }
       }
-- 
1.8.1.4



More information about the mesa-dev mailing list