5f3ae8e66610d876e1b1f08492ba4d828e1698be
[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 ir_rvalue *
29 _mesa_ast_array_index_to_hir(void *mem_ctx,
30 struct _mesa_glsl_parse_state *state,
31 ir_rvalue *array, ir_rvalue *idx,
32 YYLTYPE &loc, YYLTYPE &idx_loc,
33 bool error_emitted)
34 {
35 ir_rvalue *result = new(mem_ctx) ir_dereference_array(array, idx);
36
37 if (error_emitted)
38 return result;
39
40 if (!array->type->is_array()
41 && !array->type->is_matrix()
42 && !array->type->is_vector()) {
43 _mesa_glsl_error(& idx_loc, state,
44 "cannot dereference non-array / non-matrix / "
45 "non-vector");
46 result->type = glsl_type::error_type;
47 }
48
49 if (!idx->type->is_integer()) {
50 _mesa_glsl_error(& idx_loc, state, "array index must be integer type");
51 } else if (!idx->type->is_scalar()) {
52 _mesa_glsl_error(& idx_loc, state, "array index must be scalar");
53 }
54
55 /* If the array index is a constant expression and the array has a
56 * declared size, ensure that the access is in-bounds. If the array
57 * index is not a constant expression, ensure that the array has a
58 * declared size.
59 */
60 ir_constant *const const_index = idx->constant_expression_value();
61 if (const_index != NULL) {
62 const int idx = const_index->value.i[0];
63 const char *type_name = "error";
64 unsigned bound = 0;
65
66 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
67 *
68 * "It is illegal to declare an array with a size, and then
69 * later (in the same shader) index the same array with an
70 * integral constant expression greater than or equal to the
71 * declared size. It is also illegal to index an array with a
72 * negative constant expression."
73 */
74 if (array->type->is_matrix()) {
75 if (array->type->row_type()->vector_elements <= idx) {
76 type_name = "matrix";
77 bound = array->type->row_type()->vector_elements;
78 }
79 } else if (array->type->is_vector()) {
80 if (array->type->vector_elements <= idx) {
81 type_name = "vector";
82 bound = array->type->vector_elements;
83 }
84 } else {
85 /* glsl_type::array_size() returns 0 for non-array types. This means
86 * that we don't need to verify that the type is an array before
87 * doing the bounds checking.
88 */
89 if ((array->type->array_size() > 0)
90 && (array->type->array_size() <= idx)) {
91 type_name = "array";
92 bound = array->type->array_size();
93 }
94 }
95
96 if (bound > 0) {
97 _mesa_glsl_error(& loc, state, "%s index must be < %u",
98 type_name, bound);
99 } else if (idx < 0) {
100 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
101 type_name);
102 }
103
104 if (array->type->is_array()) {
105 /* If the array is a variable dereference, it dereferences the
106 * whole array, by definition. Use this to get the variable.
107 *
108 * FINISHME: Should some methods for getting / setting / testing
109 * FINISHME: array access limits be added to ir_dereference?
110 */
111 ir_variable *const v = array->whole_variable_referenced();
112 if ((v != NULL) && (unsigned(idx) > v->max_array_access)) {
113 v->max_array_access = idx;
114
115 /* Check whether this access will, as a side effect, implicitly
116 * cause the size of a built-in array to be too large.
117 */
118 check_builtin_array_max_size(v->name, idx+1, loc, state);
119 }
120 }
121 } else if (array->type->array_size() == 0) {
122 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
123 } else if (array->type->is_array()
124 && array->type->fields.array->is_interface()) {
125 /* Page 46 in section 4.3.7 of the OpenGL ES 3.00 spec says:
126 *
127 * "All indexes used to index a uniform block array must be
128 * constant integral expressions."
129 */
130 _mesa_glsl_error(&loc, state,
131 "uniform block array index must be constant");
132 } else {
133 if (array->type->is_array()) {
134 /* whole_variable_referenced can return NULL if the array is a
135 * member of a structure. In this case it is safe to not update
136 * the max_array_access field because it is never used for fields
137 * of structures.
138 */
139 ir_variable *v = array->whole_variable_referenced();
140 if (v != NULL)
141 v->max_array_access = array->type->array_size() - 1;
142 }
143 }
144
145 /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
146 *
147 * "Samplers aggregated into arrays within a shader (using square
148 * brackets [ ]) can only be indexed with integral constant
149 * expressions [...]."
150 *
151 * This restriction was added in GLSL 1.30. Shaders using earlier version
152 * of the language should not be rejected by the compiler front-end for
153 * using this construct. This allows useful things such as using a loop
154 * counter as the index to an array of samplers. If the loop in unrolled,
155 * the code should compile correctly. Instead, emit a warning.
156 */
157 if (array->type->is_array() &&
158 array->type->element_type()->is_sampler() &&
159 const_index == NULL) {
160
161 if (!state->is_version(130, 100)) {
162 if (state->es_shader) {
163 _mesa_glsl_warning(&loc, state,
164 "sampler arrays indexed with non-constant "
165 "expressions is optional in %s",
166 state->get_version_string());
167 } else {
168 _mesa_glsl_warning(&loc, state,
169 "sampler arrays indexed with non-constant "
170 "expressions will be forbidden in GLSL 1.30 and "
171 "later");
172 }
173 } else {
174 _mesa_glsl_error(&loc, state,
175 "sampler arrays indexed with non-constant "
176 "expressions is forbidden in GLSL 1.30 and "
177 "later");
178 }
179 }
180
181 return result;
182 }