glsl: Move update of max_array_access into a separate function.
[mesa.git] / src / glsl / ast_array_index.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 #include "ast.h"
25 #include "glsl_types.h"
26 #include "ir.h"
27
28
29 /**
30 * If \c ir is a reference to an array for which we are tracking the max array
31 * element accessed, track that the given element has been accessed.
32 * Otherwise do nothing.
33 *
34 * This function also checks whether the array is a built-in array whose
35 * maximum size is too small to accommodate the given index, and if so uses
36 * loc and state to report the error.
37 */
38 static void
39 update_max_array_access(ir_rvalue *ir, unsigned idx, YYLTYPE *loc,
40 struct _mesa_glsl_parse_state *state)
41 {
42 if (ir_dereference_variable *deref_var = ir->as_dereference_variable()) {
43 ir_variable *var = deref_var->var;
44 if (idx > var->max_array_access) {
45 var->max_array_access = idx;
46
47 /* Check whether this access will, as a side effect, implicitly cause
48 * the size of a built-in array to be too large.
49 */
50 check_builtin_array_max_size(var->name, idx+1, *loc, state);
51 }
52 }
53 }
54
55
56 ir_rvalue *
57 _mesa_ast_array_index_to_hir(void *mem_ctx,
58 struct _mesa_glsl_parse_state *state,
59 ir_rvalue *array, ir_rvalue *idx,
60 YYLTYPE &loc, YYLTYPE &idx_loc)
61 {
62 if (!array->type->is_error()
63 && !array->type->is_array()
64 && !array->type->is_matrix()
65 && !array->type->is_vector()) {
66 _mesa_glsl_error(& idx_loc, state,
67 "cannot dereference non-array / non-matrix / "
68 "non-vector");
69 }
70
71 if (!idx->type->is_error()) {
72 if (!idx->type->is_integer()) {
73 _mesa_glsl_error(& idx_loc, state, "array index must be integer type");
74 } else if (!idx->type->is_scalar()) {
75 _mesa_glsl_error(& idx_loc, state, "array index must be scalar");
76 }
77 }
78
79 /* If the array index is a constant expression and the array has a
80 * declared size, ensure that the access is in-bounds. If the array
81 * index is not a constant expression, ensure that the array has a
82 * declared size.
83 */
84 ir_constant *const const_index = idx->constant_expression_value();
85 if (const_index != NULL && idx->type->is_integer()) {
86 const int idx = const_index->value.i[0];
87 const char *type_name = "error";
88 unsigned bound = 0;
89
90 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
91 *
92 * "It is illegal to declare an array with a size, and then
93 * later (in the same shader) index the same array with an
94 * integral constant expression greater than or equal to the
95 * declared size. It is also illegal to index an array with a
96 * negative constant expression."
97 */
98 if (array->type->is_matrix()) {
99 if (array->type->row_type()->vector_elements <= idx) {
100 type_name = "matrix";
101 bound = array->type->row_type()->vector_elements;
102 }
103 } else if (array->type->is_vector()) {
104 if (array->type->vector_elements <= idx) {
105 type_name = "vector";
106 bound = array->type->vector_elements;
107 }
108 } else {
109 /* glsl_type::array_size() returns 0 for non-array types. This means
110 * that we don't need to verify that the type is an array before
111 * doing the bounds checking.
112 */
113 if ((array->type->array_size() > 0)
114 && (array->type->array_size() <= idx)) {
115 type_name = "array";
116 bound = array->type->array_size();
117 }
118 }
119
120 if (bound > 0) {
121 _mesa_glsl_error(& loc, state, "%s index must be < %u",
122 type_name, bound);
123 } else if (idx < 0) {
124 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
125 type_name);
126 }
127
128 if (array->type->is_array())
129 update_max_array_access(array, idx, &loc, state);
130 } else if (const_index == NULL && array->type->is_array()) {
131 if (array->type->array_size() == 0) {
132 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
133 } else if (array->type->fields.array->is_interface()
134 && array->variable_referenced()->mode == ir_var_uniform) {
135 /* Page 46 in section 4.3.7 of the OpenGL ES 3.00 spec says:
136 *
137 * "All indexes used to index a uniform block array must be
138 * constant integral expressions."
139 */
140 _mesa_glsl_error(&loc, state,
141 "uniform block array index must be constant");
142 } else {
143 /* whole_variable_referenced can return NULL if the array is a
144 * member of a structure. In this case it is safe to not update
145 * the max_array_access field because it is never used for fields
146 * of structures.
147 */
148 ir_variable *v = array->whole_variable_referenced();
149 if (v != NULL)
150 v->max_array_access = array->type->array_size() - 1;
151 }
152
153 /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
154 *
155 * "Samplers aggregated into arrays within a shader (using square
156 * brackets [ ]) can only be indexed with integral constant
157 * expressions [...]."
158 *
159 * This restriction was added in GLSL 1.30. Shaders using earlier
160 * version of the language should not be rejected by the compiler
161 * front-end for using this construct. This allows useful things such
162 * as using a loop counter as the index to an array of samplers. If the
163 * loop in unrolled, the code should compile correctly. Instead, emit a
164 * warning.
165 */
166 if (array->type->element_type()->is_sampler()) {
167 if (!state->is_version(130, 100)) {
168 if (state->es_shader) {
169 _mesa_glsl_warning(&loc, state,
170 "sampler arrays indexed with non-constant "
171 "expressions is optional in %s",
172 state->get_version_string());
173 } else {
174 _mesa_glsl_warning(&loc, state,
175 "sampler arrays indexed with non-constant "
176 "expressions will be forbidden in GLSL 1.30 "
177 "and later");
178 }
179 } else {
180 _mesa_glsl_error(&loc, state,
181 "sampler arrays indexed with non-constant "
182 "expressions is forbidden in GLSL 1.30 and "
183 "later");
184 }
185 }
186 }
187
188 /* After performing all of the error checking, generate the IR for the
189 * expression.
190 */
191 if (array->type->is_array()
192 || array->type->is_matrix()) {
193 return new(mem_ctx) ir_dereference_array(array, idx);
194 } else if (array->type->is_vector()) {
195 return new(mem_ctx) ir_expression(ir_binop_vector_extract, array, idx);
196 } else if (array->type->is_error()) {
197 return array;
198 } else {
199 ir_rvalue *result = new(mem_ctx) ir_dereference_array(array, idx);
200 result->type = glsl_type::error_type;
201
202 return result;
203 }
204 }