gtest: Update to 1.7.0.
[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 void
29 ast_array_specifier::print(void) const
30 {
31 if (this->is_unsized_array) {
32 printf("[ ] ");
33 }
34
35 foreach_list_typed (ast_node, array_dimension, link, &this->array_dimensions) {
36 printf("[ ");
37 array_dimension->print();
38 printf("] ");
39 }
40 }
41
42 /**
43 * If \c ir is a reference to an array for which we are tracking the max array
44 * element accessed, track that the given element has been accessed.
45 * Otherwise do nothing.
46 *
47 * This function also checks whether the array is a built-in array whose
48 * maximum size is too small to accommodate the given index, and if so uses
49 * loc and state to report the error.
50 */
51 static void
52 update_max_array_access(ir_rvalue *ir, unsigned idx, YYLTYPE *loc,
53 struct _mesa_glsl_parse_state *state)
54 {
55 if (ir_dereference_variable *deref_var = ir->as_dereference_variable()) {
56 ir_variable *var = deref_var->var;
57 if (idx > var->data.max_array_access) {
58 var->data.max_array_access = idx;
59
60 /* Check whether this access will, as a side effect, implicitly cause
61 * the size of a built-in array to be too large.
62 */
63 check_builtin_array_max_size(var->name, idx+1, *loc, state);
64 }
65 } else if (ir_dereference_record *deref_record =
66 ir->as_dereference_record()) {
67 /* There are two possibilities we need to consider:
68 *
69 * - Accessing an element of an array that is a member of a named
70 * interface block (e.g. ifc.foo[i])
71 *
72 * - Accessing an element of an array that is a member of a named
73 * interface block array (e.g. ifc[j].foo[i]).
74 */
75 ir_dereference_variable *deref_var =
76 deref_record->record->as_dereference_variable();
77 if (deref_var == NULL) {
78 if (ir_dereference_array *deref_array =
79 deref_record->record->as_dereference_array()) {
80 deref_var = deref_array->array->as_dereference_variable();
81 }
82 }
83
84 if (deref_var != NULL) {
85 if (deref_var->var->is_interface_instance()) {
86 const glsl_type *interface_type =
87 deref_var->var->get_interface_type();
88 unsigned field_index =
89 deref_record->record->type->field_index(deref_record->field);
90 assert(field_index < interface_type->length);
91 if (idx > deref_var->var->max_ifc_array_access[field_index]) {
92 deref_var->var->max_ifc_array_access[field_index] = idx;
93
94 /* Check whether this access will, as a side effect, implicitly
95 * cause the size of a built-in array to be too large.
96 */
97 check_builtin_array_max_size(deref_record->field, idx+1, *loc,
98 state);
99 }
100 }
101 }
102 }
103 }
104
105
106 ir_rvalue *
107 _mesa_ast_array_index_to_hir(void *mem_ctx,
108 struct _mesa_glsl_parse_state *state,
109 ir_rvalue *array, ir_rvalue *idx,
110 YYLTYPE &loc, YYLTYPE &idx_loc)
111 {
112 if (!array->type->is_error()
113 && !array->type->is_array()
114 && !array->type->is_matrix()
115 && !array->type->is_vector()) {
116 _mesa_glsl_error(& idx_loc, state,
117 "cannot dereference non-array / non-matrix / "
118 "non-vector");
119 }
120
121 if (!idx->type->is_error()) {
122 if (!idx->type->is_integer()) {
123 _mesa_glsl_error(& idx_loc, state, "array index must be integer type");
124 } else if (!idx->type->is_scalar()) {
125 _mesa_glsl_error(& idx_loc, state, "array index must be scalar");
126 }
127 }
128
129 /* If the array index is a constant expression and the array has a
130 * declared size, ensure that the access is in-bounds. If the array
131 * index is not a constant expression, ensure that the array has a
132 * declared size.
133 */
134 ir_constant *const const_index = idx->constant_expression_value();
135 if (const_index != NULL && idx->type->is_integer()) {
136 const int idx = const_index->value.i[0];
137 const char *type_name = "error";
138 unsigned bound = 0;
139
140 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
141 *
142 * "It is illegal to declare an array with a size, and then
143 * later (in the same shader) index the same array with an
144 * integral constant expression greater than or equal to the
145 * declared size. It is also illegal to index an array with a
146 * negative constant expression."
147 */
148 if (array->type->is_matrix()) {
149 if (array->type->row_type()->vector_elements <= idx) {
150 type_name = "matrix";
151 bound = array->type->row_type()->vector_elements;
152 }
153 } else if (array->type->is_vector()) {
154 if (array->type->vector_elements <= idx) {
155 type_name = "vector";
156 bound = array->type->vector_elements;
157 }
158 } else {
159 /* glsl_type::array_size() returns -1 for non-array types. This means
160 * that we don't need to verify that the type is an array before
161 * doing the bounds checking.
162 */
163 if ((array->type->array_size() > 0)
164 && (array->type->array_size() <= idx)) {
165 type_name = "array";
166 bound = array->type->array_size();
167 }
168 }
169
170 if (bound > 0) {
171 _mesa_glsl_error(& loc, state, "%s index must be < %u",
172 type_name, bound);
173 } else if (idx < 0) {
174 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
175 type_name);
176 }
177
178 if (array->type->is_array())
179 update_max_array_access(array, idx, &loc, state);
180 } else if (const_index == NULL && array->type->is_array()) {
181 if (array->type->is_unsized_array()) {
182 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
183 } else if (array->type->fields.array->is_interface()
184 && array->variable_referenced()->data.mode == ir_var_uniform) {
185 /* Page 46 in section 4.3.7 of the OpenGL ES 3.00 spec says:
186 *
187 * "All indexes used to index a uniform block array must be
188 * constant integral expressions."
189 */
190 _mesa_glsl_error(&loc, state,
191 "uniform block array index must be constant");
192 } else {
193 /* whole_variable_referenced can return NULL if the array is a
194 * member of a structure. In this case it is safe to not update
195 * the max_array_access field because it is never used for fields
196 * of structures.
197 */
198 ir_variable *v = array->whole_variable_referenced();
199 if (v != NULL)
200 v->data.max_array_access = array->type->array_size() - 1;
201 }
202
203 /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
204 *
205 * "Samplers aggregated into arrays within a shader (using square
206 * brackets [ ]) can only be indexed with integral constant
207 * expressions [...]."
208 *
209 * This restriction was added in GLSL 1.30. Shaders using earlier
210 * version of the language should not be rejected by the compiler
211 * front-end for using this construct. This allows useful things such
212 * as using a loop counter as the index to an array of samplers. If the
213 * loop in unrolled, the code should compile correctly. Instead, emit a
214 * warning.
215 */
216 if (array->type->element_type()->is_sampler()) {
217 if (!state->is_version(130, 100)) {
218 if (state->es_shader) {
219 _mesa_glsl_warning(&loc, state,
220 "sampler arrays indexed with non-constant "
221 "expressions is optional in %s",
222 state->get_version_string());
223 } else {
224 _mesa_glsl_warning(&loc, state,
225 "sampler arrays indexed with non-constant "
226 "expressions will be forbidden in GLSL 1.30 "
227 "and later");
228 }
229 } else {
230 _mesa_glsl_error(&loc, state,
231 "sampler arrays indexed with non-constant "
232 "expressions is forbidden in GLSL 1.30 and "
233 "later");
234 }
235 }
236 }
237
238 /* After performing all of the error checking, generate the IR for the
239 * expression.
240 */
241 if (array->type->is_array()
242 || array->type->is_matrix()) {
243 return new(mem_ctx) ir_dereference_array(array, idx);
244 } else if (array->type->is_vector()) {
245 return new(mem_ctx) ir_expression(ir_binop_vector_extract, array, idx);
246 } else if (array->type->is_error()) {
247 return array;
248 } else {
249 ir_rvalue *result = new(mem_ctx) ir_dereference_array(array, idx);
250 result->type = glsl_type::error_type;
251
252 return result;
253 }
254 }