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