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