Bug Summary

File:root/firefox-clang/third_party/rust/glslopt/glsl-optimizer/src/compiler/glsl/ast_to_hir.cpp
Warning:line 7991, column 7
Value stored to 'iface_type_name' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ast_to_hir.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/third_party/rust/glslopt -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/root/firefox-clang/third_party/rust/glslopt -resource-dir /usr/lib/llvm-21/lib/clang/21 -include /root/firefox-clang/config/gcc_hidden.h -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -I glsl-optimizer/include -I glsl-optimizer/src/mesa -I glsl-optimizer/src/mapi -I glsl-optimizer/src/compiler -I glsl-optimizer/src/compiler/glsl -I glsl-optimizer/src/gallium/auxiliary -I glsl-optimizer/src/gallium/include -I glsl-optimizer/src -I glsl-optimizer/src/util -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/stl_wrappers -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D _GLIBCXX_ASSERTIONS -D DEBUG=1 -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -D MOZILLA_CONFIG_H -D __STDC_FORMAT_MACROS -D _GNU_SOURCE -D HAVE_ENDIAN_H -D HAVE_PTHREAD -D HAVE_TIMESPEC_GET -D MOZ_INCLUDE_MOZALLOC_H -D mozilla_throw_gcc_h -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -internal-isystem /usr/lib/llvm-21/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-error=pessimizing-move -Wno-error=large-by-value-copy=128 -Wno-error=implicit-int-float-conversion -Wno-error=thread-safety-analysis -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-deprecated-this-capture -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -fdeprecated-macro -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fno-rtti -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2025-06-27-100320-3286336-1 -x c++ glsl-optimizer/src/compiler/glsl/ast_to_hir.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/**
25 * \file ast_to_hir.c
26 * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
27 *
28 * During the conversion to HIR, the majority of the symantic checking is
29 * preformed on the program. This includes:
30 *
31 * * Symbol table management
32 * * Type checking
33 * * Function binding
34 *
35 * The majority of this work could be done during parsing, and the parser could
36 * probably generate HIR directly. However, this results in frequent changes
37 * to the parser code. Since we do not assume that every system this complier
38 * is built on will have Flex and Bison installed, we have to store the code
39 * generated by these tools in our version control system. In other parts of
40 * the system we've seen problems where a parser was changed but the generated
41 * code was not committed, merge conflicts where created because two developers
42 * had slightly different versions of Bison installed, etc.
43 *
44 * I have also noticed that running Bison generated parsers in GDB is very
45 * irritating. When you get a segfault on '$$ = $1->foo', you can't very
46 * well 'print $1' in GDB.
47 *
48 * As a result, my preference is to put as little C code as possible in the
49 * parser (and lexer) sources.
50 */
51
52#include "glsl_symbol_table.h"
53#include "glsl_parser_extras.h"
54#include "ast.h"
55#include "compiler/glsl_types.h"
56#include "util/hash_table.h"
57#include "main/mtypes.h"
58#include "main/macros.h"
59#include "main/shaderobj.h"
60#include "ir.h"
61#include "ir_builder.h"
62#include "builtin_functions.h"
63
64using namespace ir_builder;
65
66static void
67detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
68 exec_list *instructions);
69static void
70verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state *state);
71
72static void
73remove_per_vertex_blocks(exec_list *instructions,
74 _mesa_glsl_parse_state *state, ir_variable_mode mode);
75
76/**
77 * Visitor class that finds the first instance of any write-only variable that
78 * is ever read, if any
79 */
80class read_from_write_only_variable_visitor : public ir_hierarchical_visitor
81{
82public:
83 read_from_write_only_variable_visitor() : found(NULL__null)
84 {
85 }
86
87 virtual ir_visitor_status visit(ir_dereference_variable *ir)
88 {
89 if (this->in_assignee)
90 return visit_continue;
91
92 ir_variable *var = ir->variable_referenced();
93 /* We can have memory_write_only set on both images and buffer variables,
94 * but in the former there is a distinction between reads from
95 * the variable itself (write_only) and from the memory they point to
96 * (memory_write_only), while in the case of buffer variables there is
97 * no such distinction, that is why this check here is limited to
98 * buffer variables alone.
99 */
100 if (!var || var->data.mode != ir_var_shader_storage)
101 return visit_continue;
102
103 if (var->data.memory_write_only) {
104 found = var;
105 return visit_stop;
106 }
107
108 return visit_continue;
109 }
110
111 ir_variable *get_variable() {
112 return found;
113 }
114
115 virtual ir_visitor_status visit_enter(ir_expression *ir)
116 {
117 /* .length() doesn't actually read anything */
118 if (ir->operation == ir_unop_ssbo_unsized_array_length)
119 return visit_continue_with_parent;
120
121 return visit_continue;
122 }
123
124private:
125 ir_variable *found;
126};
127
128void
129_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
130{
131 _mesa_glsl_initialize_variables(instructions, state);
132
133 state->symbols->separate_function_namespace = state->language_version == 110;
134
135 state->current_function = NULL__null;
136
137 state->toplevel_ir = instructions;
138
139 state->gs_input_prim_type_specified = false;
140 state->tcs_output_vertices_specified = false;
141 state->cs_input_local_size_specified = false;
142
143 /* Section 4.2 of the GLSL 1.20 specification states:
144 * "The built-in functions are scoped in a scope outside the global scope
145 * users declare global variables in. That is, a shader's global scope,
146 * available for user-defined functions and global variables, is nested
147 * inside the scope containing the built-in functions."
148 *
149 * Since built-in functions like ftransform() access built-in variables,
150 * it follows that those must be in the outer scope as well.
151 *
152 * We push scope here to create this nesting effect...but don't pop.
153 * This way, a shader's globals are still in the symbol table for use
154 * by the linker.
155 */
156 state->symbols->push_scope();
157
158 foreach_list_typed (ast_node, ast, link, & state->translation_unit)for (ast_node * ast = (!exec_node_is_tail_sentinel((& state
->translation_unit)->head_sentinel.next) ? ((ast_node *
) (((uintptr_t) (& state->translation_unit)->head_sentinel
.next) - (((char *) &((ast_node *) (& state->translation_unit
)->head_sentinel.next)->link) - ((char *) (& state->
translation_unit)->head_sentinel.next)))) : __null); (ast)
!= __null; (ast) = (!exec_node_is_tail_sentinel((ast)->link
.next) ? ((ast_node *) (((uintptr_t) (ast)->link.next) - (
((char *) &((ast_node *) (ast)->link.next)->link) -
((char *) (ast)->link.next)))) : __null))
159 ast->hir(instructions, state);
160
161 verify_subroutine_associated_funcs(state);
162 detect_recursion_unlinked(state, instructions);
163 detect_conflicting_assignments(state, instructions);
164
165 state->toplevel_ir = NULL__null;
166
167 /* Move all of the variable declarations to the front of the IR list, and
168 * reverse the order. This has the (intended!) side effect that vertex
169 * shader inputs and fragment shader outputs will appear in the IR in the
170 * same order that they appeared in the shader code. This results in the
171 * locations being assigned in the declared order. Many (arguably buggy)
172 * applications depend on this behavior, and it matches what nearly all
173 * other drivers do.
174 * However, do not push the declarations before struct decls or precision
175 * statements.
176 */
177 ir_instruction* before_node = (ir_instruction*)instructions->get_head();
178 ir_instruction* after_node = NULL__null;
179 while (before_node && (before_node->ir_type == ir_type_precision || before_node->ir_type == ir_type_typedecl))
180 {
181 after_node = before_node;
182 before_node = (ir_instruction*)before_node->next;
183 }
184
185 foreach_in_list_safe(ir_instruction, node, instructions)for (ir_instruction *node = (!exec_node_is_tail_sentinel((instructions
)->head_sentinel.next) ? (ir_instruction *) ((instructions
)->head_sentinel.next) : __null), *__next = (node) ? (!exec_node_is_tail_sentinel
((instructions)->head_sentinel.next->next) ? (ir_instruction
*) ((instructions)->head_sentinel.next->next) : __null
) : __null; (node) != __null; (node) = __next, __next = __next
? (!exec_node_is_tail_sentinel(__next->next) ? (ir_instruction
*) (__next->next) : __null) : __null)
{
186 ir_variable *const var = node->as_variable();
187
188 if (var == NULL__null)
189 continue;
190
191 var->remove();
192 if (after_node)
193 after_node->insert_after(var);
194 else
195 instructions->push_head(var);
196 }
197
198 /* Figure out if gl_FragCoord is actually used in fragment shader */
199 ir_variable *const var = state->symbols->get_variable("gl_FragCoord");
200 if (var != NULL__null)
201 state->fs_uses_gl_fragcoord = var->data.used;
202
203 /* From section 7.1 (Built-In Language Variables) of the GLSL 4.10 spec:
204 *
205 * If multiple shaders using members of a built-in block belonging to
206 * the same interface are linked together in the same program, they
207 * must all redeclare the built-in block in the same way, as described
208 * in section 4.3.7 "Interface Blocks" for interface block matching, or
209 * a link error will result.
210 *
211 * The phrase "using members of a built-in block" implies that if two
212 * shaders are linked together and one of them *does not use* any members
213 * of the built-in block, then that shader does not need to have a matching
214 * redeclaration of the built-in block.
215 *
216 * This appears to be a clarification to the behaviour established for
217 * gl_PerVertex by GLSL 1.50, therefore implement it regardless of GLSL
218 * version.
219 *
220 * The definition of "interface" in section 4.3.7 that applies here is as
221 * follows:
222 *
223 * The boundary between adjacent programmable pipeline stages: This
224 * spans all the outputs in all compilation units of the first stage
225 * and all the inputs in all compilation units of the second stage.
226 *
227 * Therefore this rule applies to both inter- and intra-stage linking.
228 *
229 * The easiest way to implement this is to check whether the shader uses
230 * gl_PerVertex right after ast-to-ir conversion, and if it doesn't, simply
231 * remove all the relevant variable declaration from the IR, so that the
232 * linker won't see them and complain about mismatches.
233 */
234 remove_per_vertex_blocks(instructions, state, ir_var_shader_in);
235 remove_per_vertex_blocks(instructions, state, ir_var_shader_out);
236
237 /* Check that we don't have reads from write-only variables */
238 read_from_write_only_variable_visitor v;
239 v.run(instructions);
240 ir_variable *error_var = v.get_variable();
241 if (error_var) {
242 /* It would be nice to have proper location information, but for that
243 * we would need to check this as we process each kind of AST node
244 */
245 YYLTYPE loc;
246 memset(&loc, 0, sizeof(loc));
247 _mesa_glsl_error(&loc, state, "Read from write-only variable `%s'",
248 error_var->name);
249 }
250}
251
252
253static ir_expression_operation
254get_implicit_conversion_operation(const glsl_type *to, const glsl_type *from,
255 struct _mesa_glsl_parse_state *state)
256{
257 switch (to->base_type) {
258 case GLSL_TYPE_FLOAT:
259 switch (from->base_type) {
260 case GLSL_TYPE_INT: return ir_unop_i2f;
261 case GLSL_TYPE_UINT: return ir_unop_u2f;
262 default: return (ir_expression_operation)0;
263 }
264
265 case GLSL_TYPE_UINT:
266 if (!state->has_implicit_uint_to_int_conversion())
267 return (ir_expression_operation)0;
268 switch (from->base_type) {
269 case GLSL_TYPE_INT: return ir_unop_i2u;
270 default: return (ir_expression_operation)0;
271 }
272
273 case GLSL_TYPE_DOUBLE:
274 if (!state->has_double())
275 return (ir_expression_operation)0;
276 switch (from->base_type) {
277 case GLSL_TYPE_INT: return ir_unop_i2d;
278 case GLSL_TYPE_UINT: return ir_unop_u2d;
279 case GLSL_TYPE_FLOAT: return ir_unop_f2d;
280 case GLSL_TYPE_INT64: return ir_unop_i642d;
281 case GLSL_TYPE_UINT64: return ir_unop_u642d;
282 default: return (ir_expression_operation)0;
283 }
284
285 case GLSL_TYPE_UINT64:
286 if (!state->has_int64())
287 return (ir_expression_operation)0;
288 switch (from->base_type) {
289 case GLSL_TYPE_INT: return ir_unop_i2u64;
290 case GLSL_TYPE_UINT: return ir_unop_u2u64;
291 case GLSL_TYPE_INT64: return ir_unop_i642u64;
292 default: return (ir_expression_operation)0;
293 }
294
295 case GLSL_TYPE_INT64:
296 if (!state->has_int64())
297 return (ir_expression_operation)0;
298 switch (from->base_type) {
299 case GLSL_TYPE_INT: return ir_unop_i2i64;
300 default: return (ir_expression_operation)0;
301 }
302
303 default: return (ir_expression_operation)0;
304 }
305}
306
307
308/**
309 * If a conversion is available, convert one operand to a different type
310 *
311 * The \c from \c ir_rvalue is converted "in place".
312 *
313 * \param to Type that the operand it to be converted to
314 * \param from Operand that is being converted
315 * \param state GLSL compiler state
316 *
317 * \return
318 * If a conversion is possible (or unnecessary), \c true is returned.
319 * Otherwise \c false is returned.
320 */
321static bool
322apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
323 struct _mesa_glsl_parse_state *state)
324{
325 void *ctx = state;
326 if (to->base_type == from->type->base_type)
327 return true;
328
329 /* Prior to GLSL 1.20, there are no implicit conversions */
330 if (!state->has_implicit_conversions())
331 return false;
332
333 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
334 *
335 * "There are no implicit array or structure conversions. For
336 * example, an array of int cannot be implicitly converted to an
337 * array of float.
338 */
339 if (!to->is_numeric() || !from->type->is_numeric())
340 return false;
341
342 /* We don't actually want the specific type `to`, we want a type
343 * with the same base type as `to`, but the same vector width as
344 * `from`.
345 */
346 to = glsl_type::get_instance(to->base_type, from->type->vector_elements,
347 from->type->matrix_columns);
348
349 ir_expression_operation op = get_implicit_conversion_operation(to, from->type, state);
350 if (op) {
351 from = new(ctx) ir_expression(op, to, from, NULL__null);
352 return true;
353 } else {
354 return false;
355 }
356}
357
358
359static const struct glsl_type *
360arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
361 bool multiply,
362 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
363{
364 const glsl_type *type_a = value_a->type;
365 const glsl_type *type_b = value_b->type;
366
367 /* From GLSL 1.50 spec, page 56:
368 *
369 * "The arithmetic binary operators add (+), subtract (-),
370 * multiply (*), and divide (/) operate on integer and
371 * floating-point scalars, vectors, and matrices."
372 */
373 if (!type_a->is_numeric() || !type_b->is_numeric()) {
374 _mesa_glsl_error(loc, state,
375 "operands to arithmetic operators must be numeric");
376 return glsl_type::error_type;
377 }
378
379
380 /* "If one operand is floating-point based and the other is
381 * not, then the conversions from Section 4.1.10 "Implicit
382 * Conversions" are applied to the non-floating-point-based operand."
383 */
384 if (!apply_implicit_conversion(type_a, value_b, state)
385 && !apply_implicit_conversion(type_b, value_a, state)) {
386 _mesa_glsl_error(loc, state,
387 "could not implicitly convert operands to "
388 "arithmetic operator");
389 return glsl_type::error_type;
390 }
391 type_a = value_a->type;
392 type_b = value_b->type;
393
394 /* "If the operands are integer types, they must both be signed or
395 * both be unsigned."
396 *
397 * From this rule and the preceeding conversion it can be inferred that
398 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
399 * The is_numeric check above already filtered out the case where either
400 * type is not one of these, so now the base types need only be tested for
401 * equality.
402 */
403 if (type_a->base_type != type_b->base_type) {
404 _mesa_glsl_error(loc, state,
405 "base type mismatch for arithmetic operator");
406 return glsl_type::error_type;
407 }
408
409 /* "All arithmetic binary operators result in the same fundamental type
410 * (signed integer, unsigned integer, or floating-point) as the
411 * operands they operate on, after operand type conversion. After
412 * conversion, the following cases are valid
413 *
414 * * The two operands are scalars. In this case the operation is
415 * applied, resulting in a scalar."
416 */
417 if (type_a->is_scalar() && type_b->is_scalar())
418 return type_a;
419
420 /* "* One operand is a scalar, and the other is a vector or matrix.
421 * In this case, the scalar operation is applied independently to each
422 * component of the vector or matrix, resulting in the same size
423 * vector or matrix."
424 */
425 if (type_a->is_scalar()) {
426 if (!type_b->is_scalar())
427 return type_b;
428 } else if (type_b->is_scalar()) {
429 return type_a;
430 }
431
432 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
433 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
434 * handled.
435 */
436 assert(!type_a->is_scalar())(static_cast <bool> (!type_a->is_scalar()) ? void (0
) : __assert_fail ("!type_a->is_scalar()", __builtin_FILE (
), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
437 assert(!type_b->is_scalar())(static_cast <bool> (!type_b->is_scalar()) ? void (0
) : __assert_fail ("!type_b->is_scalar()", __builtin_FILE (
), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
438
439 /* "* The two operands are vectors of the same size. In this case, the
440 * operation is done component-wise resulting in the same size
441 * vector."
442 */
443 if (type_a->is_vector() && type_b->is_vector()) {
444 if (type_a == type_b) {
445 return type_a;
446 } else {
447 _mesa_glsl_error(loc, state,
448 "vector size mismatch for arithmetic operator");
449 return glsl_type::error_type;
450 }
451 }
452
453 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
454 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
455 * <vector, vector> have been handled. At least one of the operands must
456 * be matrix. Further, since there are no integer matrix types, the base
457 * type of both operands must be float.
458 */
459 assert(type_a->is_matrix() || type_b->is_matrix())(static_cast <bool> (type_a->is_matrix() || type_b->
is_matrix()) ? void (0) : __assert_fail ("type_a->is_matrix() || type_b->is_matrix()"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
460 assert(type_a->is_float() || type_a->is_double())(static_cast <bool> (type_a->is_float() || type_a->
is_double()) ? void (0) : __assert_fail ("type_a->is_float() || type_a->is_double()"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
461 assert(type_b->is_float() || type_b->is_double())(static_cast <bool> (type_b->is_float() || type_b->
is_double()) ? void (0) : __assert_fail ("type_b->is_float() || type_b->is_double()"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
462
463 /* "* The operator is add (+), subtract (-), or divide (/), and the
464 * operands are matrices with the same number of rows and the same
465 * number of columns. In this case, the operation is done component-
466 * wise resulting in the same size matrix."
467 * * The operator is multiply (*), where both operands are matrices or
468 * one operand is a vector and the other a matrix. A right vector
469 * operand is treated as a column vector and a left vector operand as a
470 * row vector. In all these cases, it is required that the number of
471 * columns of the left operand is equal to the number of rows of the
472 * right operand. Then, the multiply (*) operation does a linear
473 * algebraic multiply, yielding an object that has the same number of
474 * rows as the left operand and the same number of columns as the right
475 * operand. Section 5.10 "Vector and Matrix Operations" explains in
476 * more detail how vectors and matrices are operated on."
477 */
478 if (! multiply) {
479 if (type_a == type_b)
480 return type_a;
481 } else {
482 const glsl_type *type = glsl_type::get_mul_type(type_a, type_b);
483
484 if (type == glsl_type::error_type) {
485 _mesa_glsl_error(loc, state,
486 "size mismatch for matrix multiplication");
487 }
488
489 return type;
490 }
491
492
493 /* "All other cases are illegal."
494 */
495 _mesa_glsl_error(loc, state, "type mismatch");
496 return glsl_type::error_type;
497}
498
499
500static const struct glsl_type *
501unary_arithmetic_result_type(const struct glsl_type *type,
502 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
503{
504 /* From GLSL 1.50 spec, page 57:
505 *
506 * "The arithmetic unary operators negate (-), post- and pre-increment
507 * and decrement (-- and ++) operate on integer or floating-point
508 * values (including vectors and matrices). All unary operators work
509 * component-wise on their operands. These result with the same type
510 * they operated on."
511 */
512 if (!type->is_numeric()) {
513 _mesa_glsl_error(loc, state,
514 "operands to arithmetic operators must be numeric");
515 return glsl_type::error_type;
516 }
517
518 return type;
519}
520
521/**
522 * \brief Return the result type of a bit-logic operation.
523 *
524 * If the given types to the bit-logic operator are invalid, return
525 * glsl_type::error_type.
526 *
527 * \param value_a LHS of bit-logic op
528 * \param value_b RHS of bit-logic op
529 */
530static const struct glsl_type *
531bit_logic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
532 ast_operators op,
533 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
534{
535 const glsl_type *type_a = value_a->type;
536 const glsl_type *type_b = value_b->type;
537
538 if (!state->check_bitwise_operations_allowed(loc)) {
539 return glsl_type::error_type;
540 }
541
542 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
543 *
544 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
545 * (|). The operands must be of type signed or unsigned integers or
546 * integer vectors."
547 */
548 if (!type_a->is_integer_32_64()) {
549 _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
550 ast_expression::operator_string(op));
551 return glsl_type::error_type;
552 }
553 if (!type_b->is_integer_32_64()) {
554 _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
555 ast_expression::operator_string(op));
556 return glsl_type::error_type;
557 }
558
559 /* Prior to GLSL 4.0 / GL_ARB_gpu_shader5, implicit conversions didn't
560 * make sense for bitwise operations, as they don't operate on floats.
561 *
562 * GLSL 4.0 added implicit int -> uint conversions, which are relevant
563 * here. It wasn't clear whether or not we should apply them to bitwise
564 * operations. However, Khronos has decided that they should in future
565 * language revisions. Applications also rely on this behavior. We opt
566 * to apply them in general, but issue a portability warning.
567 *
568 * See https://www.khronos.org/bugzilla/show_bug.cgi?id=1405
569 */
570 if (type_a->base_type != type_b->base_type) {
571 if (!apply_implicit_conversion(type_a, value_b, state)
572 && !apply_implicit_conversion(type_b, value_a, state)) {
573 _mesa_glsl_error(loc, state,
574 "could not implicitly convert operands to "
575 "`%s` operator",
576 ast_expression::operator_string(op));
577 return glsl_type::error_type;
578 } else {
579 _mesa_glsl_warning(loc, state,
580 "some implementations may not support implicit "
581 "int -> uint conversions for `%s' operators; "
582 "consider casting explicitly for portability",
583 ast_expression::operator_string(op));
584 }
585 type_a = value_a->type;
586 type_b = value_b->type;
587 }
588
589 /* "The fundamental types of the operands (signed or unsigned) must
590 * match,"
591 */
592 if (type_a->base_type != type_b->base_type) {
593 _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
594 "base type", ast_expression::operator_string(op));
595 return glsl_type::error_type;
596 }
597
598 /* "The operands cannot be vectors of differing size." */
599 if (type_a->is_vector() &&
600 type_b->is_vector() &&
601 type_a->vector_elements != type_b->vector_elements) {
602 _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
603 "different sizes", ast_expression::operator_string(op));
604 return glsl_type::error_type;
605 }
606
607 /* "If one operand is a scalar and the other a vector, the scalar is
608 * applied component-wise to the vector, resulting in the same type as
609 * the vector. The fundamental types of the operands [...] will be the
610 * resulting fundamental type."
611 */
612 if (type_a->is_scalar())
613 return type_b;
614 else
615 return type_a;
616}
617
618static const struct glsl_type *
619modulus_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
620 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
621{
622 const glsl_type *type_a = value_a->type;
623 const glsl_type *type_b = value_b->type;
624
625 if (!state->EXT_gpu_shader4_enable &&
626 !state->check_version(130, 300, loc, "operator '%%' is reserved")) {
627 return glsl_type::error_type;
628 }
629
630 /* Section 5.9 (Expressions) of the GLSL 4.00 specification says:
631 *
632 * "The operator modulus (%) operates on signed or unsigned integers or
633 * integer vectors."
634 */
635 if (!type_a->is_integer_32_64()) {
636 _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer");
637 return glsl_type::error_type;
638 }
639 if (!type_b->is_integer_32_64()) {
640 _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer");
641 return glsl_type::error_type;
642 }
643
644 /* "If the fundamental types in the operands do not match, then the
645 * conversions from section 4.1.10 "Implicit Conversions" are applied
646 * to create matching types."
647 *
648 * Note that GLSL 4.00 (and GL_ARB_gpu_shader5) introduced implicit
649 * int -> uint conversion rules. Prior to that, there were no implicit
650 * conversions. So it's harmless to apply them universally - no implicit
651 * conversions will exist. If the types don't match, we'll receive false,
652 * and raise an error, satisfying the GLSL 1.50 spec, page 56:
653 *
654 * "The operand types must both be signed or unsigned."
655 */
656 if (!apply_implicit_conversion(type_a, value_b, state) &&
657 !apply_implicit_conversion(type_b, value_a, state)) {
658 _mesa_glsl_error(loc, state,
659 "could not implicitly convert operands to "
660 "modulus (%%) operator");
661 return glsl_type::error_type;
662 }
663 type_a = value_a->type;
664 type_b = value_b->type;
665
666 /* "The operands cannot be vectors of differing size. If one operand is
667 * a scalar and the other vector, then the scalar is applied component-
668 * wise to the vector, resulting in the same type as the vector. If both
669 * are vectors of the same size, the result is computed component-wise."
670 */
671 if (type_a->is_vector()) {
672 if (!type_b->is_vector()
673 || (type_a->vector_elements == type_b->vector_elements))
674 return type_a;
675 } else
676 return type_b;
677
678 /* "The operator modulus (%) is not defined for any other data types
679 * (non-integer types)."
680 */
681 _mesa_glsl_error(loc, state, "type mismatch");
682 return glsl_type::error_type;
683}
684
685
686static const struct glsl_type *
687relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
688 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
689{
690 const glsl_type *type_a = value_a->type;
691 const glsl_type *type_b = value_b->type;
692
693 /* From GLSL 1.50 spec, page 56:
694 * "The relational operators greater than (>), less than (<), greater
695 * than or equal (>=), and less than or equal (<=) operate only on
696 * scalar integer and scalar floating-point expressions."
697 */
698 if (!type_a->is_numeric()
699 || !type_b->is_numeric()
700 || !type_a->is_scalar()
701 || !type_b->is_scalar()) {
702 _mesa_glsl_error(loc, state,
703 "operands to relational operators must be scalar and "
704 "numeric");
705 return glsl_type::error_type;
706 }
707
708 /* "Either the operands' types must match, or the conversions from
709 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
710 * operand, after which the types must match."
711 */
712 if (!apply_implicit_conversion(type_a, value_b, state)
713 && !apply_implicit_conversion(type_b, value_a, state)) {
714 _mesa_glsl_error(loc, state,
715 "could not implicitly convert operands to "
716 "relational operator");
717 return glsl_type::error_type;
718 }
719 type_a = value_a->type;
720 type_b = value_b->type;
721
722 if (type_a->base_type != type_b->base_type) {
723 _mesa_glsl_error(loc, state, "base type mismatch");
724 return glsl_type::error_type;
725 }
726
727 /* "The result is scalar Boolean."
728 */
729 return glsl_type::bool_type;
730}
731
732/**
733 * \brief Return the result type of a bit-shift operation.
734 *
735 * If the given types to the bit-shift operator are invalid, return
736 * glsl_type::error_type.
737 *
738 * \param type_a Type of LHS of bit-shift op
739 * \param type_b Type of RHS of bit-shift op
740 */
741static const struct glsl_type *
742shift_result_type(const struct glsl_type *type_a,
743 const struct glsl_type *type_b,
744 ast_operators op,
745 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
746{
747 if (!state->check_bitwise_operations_allowed(loc)) {
748 return glsl_type::error_type;
749 }
750
751 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
752 *
753 * "The shift operators (<<) and (>>). For both operators, the operands
754 * must be signed or unsigned integers or integer vectors. One operand
755 * can be signed while the other is unsigned."
756 */
757 if (!type_a->is_integer_32_64()) {
758 _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
759 "integer vector", ast_expression::operator_string(op));
760 return glsl_type::error_type;
761
762 }
763 if (!type_b->is_integer_32()) {
764 _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
765 "integer vector", ast_expression::operator_string(op));
766 return glsl_type::error_type;
767 }
768
769 /* "If the first operand is a scalar, the second operand has to be
770 * a scalar as well."
771 */
772 if (type_a->is_scalar() && !type_b->is_scalar()) {
773 _mesa_glsl_error(loc, state, "if the first operand of %s is scalar, the "
774 "second must be scalar as well",
775 ast_expression::operator_string(op));
776 return glsl_type::error_type;
777 }
778
779 /* If both operands are vectors, check that they have same number of
780 * elements.
781 */
782 if (type_a->is_vector() &&
783 type_b->is_vector() &&
784 type_a->vector_elements != type_b->vector_elements) {
785 _mesa_glsl_error(loc, state, "vector operands to operator %s must "
786 "have same number of elements",
787 ast_expression::operator_string(op));
788 return glsl_type::error_type;
789 }
790
791 /* "In all cases, the resulting type will be the same type as the left
792 * operand."
793 */
794 return type_a;
795}
796
797/**
798 * Returns the innermost array index expression in an rvalue tree.
799 * This is the largest indexing level -- if an array of blocks, then
800 * it is the block index rather than an indexing expression for an
801 * array-typed member of an array of blocks.
802 */
803static ir_rvalue *
804find_innermost_array_index(ir_rvalue *rv)
805{
806 ir_dereference_array *last = NULL__null;
807 while (rv) {
808 if (rv->as_dereference_array()) {
809 last = rv->as_dereference_array();
810 rv = last->array;
811 } else if (rv->as_dereference_record())
812 rv = rv->as_dereference_record()->record;
813 else if (rv->as_swizzle())
814 rv = rv->as_swizzle()->val;
815 else
816 rv = NULL__null;
817 }
818
819 if (last)
820 return last->array_index;
821
822 return NULL__null;
823}
824
825/**
826 * Validates that a value can be assigned to a location with a specified type
827 *
828 * Validates that \c rhs can be assigned to some location. If the types are
829 * not an exact match but an automatic conversion is possible, \c rhs will be
830 * converted.
831 *
832 * \return
833 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
834 * Otherwise the actual RHS to be assigned will be returned. This may be
835 * \c rhs, or it may be \c rhs after some type conversion.
836 *
837 * \note
838 * In addition to being used for assignments, this function is used to
839 * type-check return values.
840 */
841static ir_rvalue *
842validate_assignment(struct _mesa_glsl_parse_state *state,
843 YYLTYPE loc, ir_rvalue *lhs,
844 ir_rvalue *rhs, bool is_initializer)
845{
846 /* If there is already some error in the RHS, just return it. Anything
847 * else will lead to an avalanche of error message back to the user.
848 */
849 if (rhs->type->is_error())
850 return rhs;
851
852 /* In the Tessellation Control Shader:
853 * If a per-vertex output variable is used as an l-value, it is an error
854 * if the expression indicating the vertex number is not the identifier
855 * `gl_InvocationID`.
856 */
857 if (state->stage == MESA_SHADER_TESS_CTRL && !lhs->type->is_error()) {
858 ir_variable *var = lhs->variable_referenced();
859 if (var && var->data.mode == ir_var_shader_out && !var->data.patch) {
860 ir_rvalue *index = find_innermost_array_index(lhs);
861 ir_variable *index_var = index ? index->variable_referenced() : NULL__null;
862 if (!index_var || strcmp(index_var->name, "gl_InvocationID") != 0) {
863 _mesa_glsl_error(&loc, state,
864 "Tessellation control shader outputs can only "
865 "be indexed by gl_InvocationID");
866 return NULL__null;
867 }
868 }
869 }
870
871 /* If the types are identical, the assignment can trivially proceed.
872 */
873 if (rhs->type == lhs->type)
874 return rhs;
875
876 /* If the array element types are the same and the LHS is unsized,
877 * the assignment is okay for initializers embedded in variable
878 * declarations.
879 *
880 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
881 * is handled by ir_dereference::is_lvalue.
882 */
883 const glsl_type *lhs_t = lhs->type;
884 const glsl_type *rhs_t = rhs->type;
885 bool unsized_array = false;
886 while(lhs_t->is_array()) {
887 if (rhs_t == lhs_t)
888 break; /* the rest of the inner arrays match so break out early */
889 if (!rhs_t->is_array()) {
890 unsized_array = false;
891 break; /* number of dimensions mismatch */
892 }
893 if (lhs_t->length == rhs_t->length) {
894 lhs_t = lhs_t->fields.array;
895 rhs_t = rhs_t->fields.array;
896 continue;
897 } else if (lhs_t->is_unsized_array()) {
898 unsized_array = true;
899 } else {
900 unsized_array = false;
901 break; /* sized array mismatch */
902 }
903 lhs_t = lhs_t->fields.array;
904 rhs_t = rhs_t->fields.array;
905 }
906 if (unsized_array) {
907 if (is_initializer) {
908 if (rhs->type->get_scalar_type() == lhs->type->get_scalar_type())
909 return rhs;
910 } else {
911 _mesa_glsl_error(&loc, state,
912 "implicitly sized arrays cannot be assigned");
913 return NULL__null;
914 }
915 }
916
917 /* Check for implicit conversion in GLSL 1.20 */
918 if (apply_implicit_conversion(lhs->type, rhs, state)) {
919 if (rhs->type == lhs->type)
920 return rhs;
921 }
922
923 _mesa_glsl_error(&loc, state,
924 "%s of type %s cannot be assigned to "
925 "variable of type %s",
926 is_initializer ? "initializer" : "value",
927 rhs->type->name, lhs->type->name);
928
929 return NULL__null;
930}
931
932static void
933mark_whole_array_access(ir_rvalue *access)
934{
935 ir_dereference_variable *deref = access->as_dereference_variable();
936
937 if (deref && deref->var) {
938 deref->var->data.max_array_access = deref->type->length - 1;
939 }
940}
941
942static bool
943do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
944 const char *non_lvalue_description,
945 ir_rvalue *lhs, ir_rvalue *rhs,
946 ir_rvalue **out_rvalue, bool needs_rvalue,
947 bool is_initializer,
948 YYLTYPE lhs_loc)
949{
950 void *ctx = state;
951 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
952
953 ir_variable *lhs_var = lhs->variable_referenced();
954 if (lhs_var)
955 lhs_var->data.assigned = true;
956
957 if (!error_emitted) {
958 if (non_lvalue_description != NULL__null) {
959 _mesa_glsl_error(&lhs_loc, state,
960 "assignment to %s",
961 non_lvalue_description);
962 error_emitted = true;
963 } else if (lhs_var != NULL__null && (lhs_var->data.read_only ||
964 (lhs_var->data.mode == ir_var_shader_storage &&
965 lhs_var->data.memory_read_only))) {
966 /* We can have memory_read_only set on both images and buffer variables,
967 * but in the former there is a distinction between assignments to
968 * the variable itself (read_only) and to the memory they point to
969 * (memory_read_only), while in the case of buffer variables there is
970 * no such distinction, that is why this check here is limited to
971 * buffer variables alone.
972 */
973 _mesa_glsl_error(&lhs_loc, state,
974 "assignment to read-only variable '%s'",
975 lhs_var->name);
976 error_emitted = true;
977 } else if (lhs->type->is_array() &&
978 !state->check_version(120, 300, &lhs_loc,
979 "whole array assignment forbidden")) {
980 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
981 *
982 * "Other binary or unary expressions, non-dereferenced
983 * arrays, function names, swizzles with repeated fields,
984 * and constants cannot be l-values."
985 *
986 * The restriction on arrays is lifted in GLSL 1.20 and GLSL ES 3.00.
987 */
988 error_emitted = true;
989 } else if (!lhs->is_lvalue(state)) {
990 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
991 error_emitted = true;
992 }
993 }
994
995 ir_rvalue *new_rhs =
996 validate_assignment(state, lhs_loc, lhs, rhs, is_initializer);
997 if (new_rhs != NULL__null) {
998 rhs = new_rhs;
999
1000 /* If the LHS array was not declared with a size, it takes it size from
1001 * the RHS. If the LHS is an l-value and a whole array, it must be a
1002 * dereference of a variable. Any other case would require that the LHS
1003 * is either not an l-value or not a whole array.
1004 */
1005 if (lhs->type->is_unsized_array()) {
1006 ir_dereference *const d = lhs->as_dereference();
1007
1008 assert(d != NULL)(static_cast <bool> (d != __null) ? void (0) : __assert_fail
("d != NULL", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
1009
1010 ir_variable *const var = d->variable_referenced();
1011
1012 assert(var != NULL)(static_cast <bool> (var != __null) ? void (0) : __assert_fail
("var != NULL", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
1013
1014 if (var->data.max_array_access >= rhs->type->array_size()) {
1015 /* FINISHME: This should actually log the location of the RHS. */
1016 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
1017 "previous access",
1018 var->data.max_array_access);
1019 }
1020
1021 var->type = glsl_type::get_array_instance(lhs->type->fields.array,
1022 rhs->type->array_size());
1023 d->type = var->type;
1024 }
1025 if (lhs->type->is_array()) {
1026 mark_whole_array_access(rhs);
1027 mark_whole_array_access(lhs);
1028 }
1029 } else {
1030 error_emitted = true;
1031 }
1032
1033 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
1034 * but not post_inc) need the converted assigned value as an rvalue
1035 * to handle things like:
1036 *
1037 * i = j += 1;
1038 */
1039 if (needs_rvalue) {
1040 ir_rvalue *rvalue;
1041 if (!error_emitted) {
1042 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
1043 ir_var_temporary);
1044 instructions->push_tail(var);
1045 instructions->push_tail(assign(var, rhs));
1046
1047 ir_dereference_variable *deref_var =
1048 new(ctx) ir_dereference_variable(var);
1049 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var));
1050 rvalue = new(ctx) ir_dereference_variable(var);
1051 } else {
1052 rvalue = ir_rvalue::error_value(ctx);
1053 }
1054 *out_rvalue = rvalue;
1055 } else {
1056 if (!error_emitted)
1057 instructions->push_tail(new(ctx) ir_assignment(lhs, rhs));
1058 *out_rvalue = NULL__null;
1059 }
1060
1061 return error_emitted;
1062}
1063
1064static ir_rvalue *
1065get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
1066{
1067 void *ctx = ralloc_parent(lvalue);
1068 ir_variable *var;
1069
1070 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
1071 ir_var_temporary);
1072 instructions->push_tail(var);
1073
1074 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
1075 lvalue));
1076
1077 return new(ctx) ir_dereference_variable(var);
1078}
1079
1080
1081ir_rvalue *
1082ast_node::hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
1083{
1084 (void) instructions;
1085 (void) state;
1086
1087 return NULL__null;
1088}
1089
1090bool
1091ast_node::has_sequence_subexpression() const
1092{
1093 return false;
1094}
1095
1096void
1097ast_node::set_is_lhs(bool /* new_value */)
1098{
1099}
1100
1101void
1102ast_function_expression::hir_no_rvalue(exec_list *instructions,
1103 struct _mesa_glsl_parse_state *state)
1104{
1105 (void)hir(instructions, state);
1106}
1107
1108void
1109ast_aggregate_initializer::hir_no_rvalue(exec_list *instructions,
1110 struct _mesa_glsl_parse_state *state)
1111{
1112 (void)hir(instructions, state);
1113}
1114
1115static ir_rvalue *
1116do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
1117{
1118 int join_op;
1119 ir_rvalue *cmp = NULL__null;
1120
1121 if (operation == ir_binop_all_equal)
1122 join_op = ir_binop_logic_and;
1123 else
1124 join_op = ir_binop_logic_or;
1125
1126 switch (op0->type->base_type) {
1127 case GLSL_TYPE_FLOAT:
1128 case GLSL_TYPE_FLOAT16:
1129 case GLSL_TYPE_UINT:
1130 case GLSL_TYPE_INT:
1131 case GLSL_TYPE_BOOL:
1132 case GLSL_TYPE_DOUBLE:
1133 case GLSL_TYPE_UINT64:
1134 case GLSL_TYPE_INT64:
1135 case GLSL_TYPE_UINT16:
1136 case GLSL_TYPE_INT16:
1137 case GLSL_TYPE_UINT8:
1138 case GLSL_TYPE_INT8:
1139 return new(mem_ctx) ir_expression(operation, op0, op1);
1140
1141 case GLSL_TYPE_ARRAY: {
1142 for (unsigned int i = 0; i < op0->type->length; i++) {
1143 ir_rvalue *e0, *e1, *result;
1144
1145 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL__null),
1146 new(mem_ctx) ir_constant(i));
1147 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL__null),
1148 new(mem_ctx) ir_constant(i));
1149 result = do_comparison(mem_ctx, operation, e0, e1);
1150
1151 if (cmp) {
1152 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
1153 } else {
1154 cmp = result;
1155 }
1156 }
1157
1158 mark_whole_array_access(op0);
1159 mark_whole_array_access(op1);
1160 break;
1161 }
1162
1163 case GLSL_TYPE_STRUCT: {
1164 for (unsigned int i = 0; i < op0->type->length; i++) {
1165 ir_rvalue *e0, *e1, *result;
1166 const char *field_name = op0->type->fields.structure[i].name;
1167
1168 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL__null),
1169 field_name);
1170 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL__null),
1171 field_name);
1172 result = do_comparison(mem_ctx, operation, e0, e1);
1173
1174 if (cmp) {
1175 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
1176 } else {
1177 cmp = result;
1178 }
1179 }
1180 break;
1181 }
1182
1183 case GLSL_TYPE_ERROR:
1184 case GLSL_TYPE_VOID:
1185 case GLSL_TYPE_SAMPLER:
1186 case GLSL_TYPE_IMAGE:
1187 case GLSL_TYPE_INTERFACE:
1188 case GLSL_TYPE_ATOMIC_UINT:
1189 case GLSL_TYPE_SUBROUTINE:
1190 case GLSL_TYPE_FUNCTION:
1191 /* I assume a comparison of a struct containing a sampler just
1192 * ignores the sampler present in the type.
1193 */
1194 break;
1195 }
1196
1197 if (cmp == NULL__null)
1198 cmp = new(mem_ctx) ir_constant(true);
1199
1200 return cmp;
1201}
1202
1203/* For logical operations, we want to ensure that the operands are
1204 * scalar booleans. If it isn't, emit an error and return a constant
1205 * boolean to avoid triggering cascading error messages.
1206 */
1207static ir_rvalue *
1208get_scalar_boolean_operand(exec_list *instructions,
1209 struct _mesa_glsl_parse_state *state,
1210 ast_expression *parent_expr,
1211 int operand,
1212 const char *operand_name,
1213 bool *error_emitted)
1214{
1215 ast_expression *expr = parent_expr->subexpressions[operand];
1216 void *ctx = state;
1217 ir_rvalue *val = expr->hir(instructions, state);
1218
1219 if (val->type->is_boolean() && val->type->is_scalar())
1220 return val;
1221
1222 if (!*error_emitted) {
1223 YYLTYPE loc = expr->get_location();
1224 _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
1225 operand_name,
1226 parent_expr->operator_string(parent_expr->oper));
1227 *error_emitted = true;
1228 }
1229
1230 return new(ctx) ir_constant(true);
1231}
1232
1233/**
1234 * If name refers to a builtin array whose maximum allowed size is less than
1235 * size, report an error and return true. Otherwise return false.
1236 */
1237void
1238check_builtin_array_max_size(const char *name, unsigned size,
1239 YYLTYPE loc, struct _mesa_glsl_parse_state *state)
1240{
1241 if ((strcmp("gl_TexCoord", name) == 0)
1242 && (size > state->Const.MaxTextureCoords)) {
1243 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1244 *
1245 * "The size [of gl_TexCoord] can be at most
1246 * gl_MaxTextureCoords."
1247 */
1248 _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
1249 "be larger than gl_MaxTextureCoords (%u)",
1250 state->Const.MaxTextureCoords);
1251 } else if (strcmp("gl_ClipDistance", name) == 0) {
1252 state->clip_dist_size = size;
1253 if (size + state->cull_dist_size > state->Const.MaxClipPlanes) {
1254 /* From section 7.1 (Vertex Shader Special Variables) of the
1255 * GLSL 1.30 spec:
1256 *
1257 * "The gl_ClipDistance array is predeclared as unsized and
1258 * must be sized by the shader either redeclaring it with a
1259 * size or indexing it only with integral constant
1260 * expressions. ... The size can be at most
1261 * gl_MaxClipDistances."
1262 */
1263 _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
1264 "be larger than gl_MaxClipDistances (%u)",
1265 state->Const.MaxClipPlanes);
1266 }
1267 } else if (strcmp("gl_CullDistance", name) == 0) {
1268 state->cull_dist_size = size;
1269 if (size + state->clip_dist_size > state->Const.MaxClipPlanes) {
1270 /* From the ARB_cull_distance spec:
1271 *
1272 * "The gl_CullDistance array is predeclared as unsized and
1273 * must be sized by the shader either redeclaring it with
1274 * a size or indexing it only with integral constant
1275 * expressions. The size determines the number and set of
1276 * enabled cull distances and can be at most
1277 * gl_MaxCullDistances."
1278 */
1279 _mesa_glsl_error(&loc, state, "`gl_CullDistance' array size cannot "
1280 "be larger than gl_MaxCullDistances (%u)",
1281 state->Const.MaxClipPlanes);
1282 }
1283 }
1284}
1285
1286/**
1287 * Create the constant 1, of a which is appropriate for incrementing and
1288 * decrementing values of the given GLSL type. For example, if type is vec4,
1289 * this creates a constant value of 1.0 having type float.
1290 *
1291 * If the given type is invalid for increment and decrement operators, return
1292 * a floating point 1--the error will be detected later.
1293 */
1294static ir_rvalue *
1295constant_one_for_inc_dec(void *ctx, const glsl_type *type)
1296{
1297 switch (type->base_type) {
1298 case GLSL_TYPE_UINT:
1299 return new(ctx) ir_constant((unsigned) 1);
1300 case GLSL_TYPE_INT:
1301 return new(ctx) ir_constant(1);
1302 case GLSL_TYPE_UINT64:
1303 return new(ctx) ir_constant((uint64_t) 1);
1304 case GLSL_TYPE_INT64:
1305 return new(ctx) ir_constant((int64_t) 1);
1306 default:
1307 case GLSL_TYPE_FLOAT:
1308 return new(ctx) ir_constant(1.0f);
1309 }
1310}
1311
1312ir_rvalue *
1313ast_expression::hir(exec_list *instructions,
1314 struct _mesa_glsl_parse_state *state)
1315{
1316 return do_hir(instructions, state, true);
1317}
1318
1319void
1320ast_expression::hir_no_rvalue(exec_list *instructions,
1321 struct _mesa_glsl_parse_state *state)
1322{
1323 do_hir(instructions, state, false);
1324}
1325
1326void
1327ast_expression::set_is_lhs(bool new_value)
1328{
1329 /* is_lhs is tracked only to print "variable used uninitialized" warnings,
1330 * if we lack an identifier we can just skip it.
1331 */
1332 if (this->primary_expression.identifier == NULL__null)
1333 return;
1334
1335 this->is_lhs = new_value;
1336
1337 /* We need to go through the subexpressions tree to cover cases like
1338 * ast_field_selection
1339 */
1340 if (this->subexpressions[0] != NULL__null)
1341 this->subexpressions[0]->set_is_lhs(new_value);
1342}
1343
1344ir_rvalue *
1345ast_expression::do_hir(exec_list *instructions,
1346 struct _mesa_glsl_parse_state *state,
1347 bool needs_rvalue)
1348{
1349 void *ctx = state;
1350 static const int operations[AST_NUM_OPERATORS(ast_aggregate + 1)] = {
1351 -1, /* ast_assign doesn't convert to ir_expression. */
1352 -1, /* ast_plus doesn't convert to ir_expression. */
1353 ir_unop_neg,
1354 ir_binop_add,
1355 ir_binop_sub,
1356 ir_binop_mul,
1357 ir_binop_div,
1358 ir_binop_mod,
1359 ir_binop_lshift,
1360 ir_binop_rshift,
1361 ir_binop_less,
1362 ir_binop_less, /* This is correct. See the ast_greater case below. */
1363 ir_binop_gequal, /* This is correct. See the ast_lequal case below. */
1364 ir_binop_gequal,
1365 ir_binop_all_equal,
1366 ir_binop_any_nequal,
1367 ir_binop_bit_and,
1368 ir_binop_bit_xor,
1369 ir_binop_bit_or,
1370 ir_unop_bit_not,
1371 ir_binop_logic_and,
1372 ir_binop_logic_xor,
1373 ir_binop_logic_or,
1374 ir_unop_logic_not,
1375
1376 /* Note: The following block of expression types actually convert
1377 * to multiple IR instructions.
1378 */
1379 ir_binop_mul, /* ast_mul_assign */
1380 ir_binop_div, /* ast_div_assign */
1381 ir_binop_mod, /* ast_mod_assign */
1382 ir_binop_add, /* ast_add_assign */
1383 ir_binop_sub, /* ast_sub_assign */
1384 ir_binop_lshift, /* ast_ls_assign */
1385 ir_binop_rshift, /* ast_rs_assign */
1386 ir_binop_bit_and, /* ast_and_assign */
1387 ir_binop_bit_xor, /* ast_xor_assign */
1388 ir_binop_bit_or, /* ast_or_assign */
1389
1390 -1, /* ast_conditional doesn't convert to ir_expression. */
1391 ir_binop_add, /* ast_pre_inc. */
1392 ir_binop_sub, /* ast_pre_dec. */
1393 ir_binop_add, /* ast_post_inc. */
1394 ir_binop_sub, /* ast_post_dec. */
1395 -1, /* ast_field_selection doesn't conv to ir_expression. */
1396 -1, /* ast_array_index doesn't convert to ir_expression. */
1397 -1, /* ast_function_call doesn't conv to ir_expression. */
1398 -1, /* ast_identifier doesn't convert to ir_expression. */
1399 -1, /* ast_int_constant doesn't convert to ir_expression. */
1400 -1, /* ast_uint_constant doesn't conv to ir_expression. */
1401 -1, /* ast_float_constant doesn't conv to ir_expression. */
1402 -1, /* ast_bool_constant doesn't conv to ir_expression. */
1403 -1, /* ast_sequence doesn't convert to ir_expression. */
1404 -1, /* ast_aggregate shouldn't ever even get here. */
1405 };
1406 ir_rvalue *result = NULL__null;
1407 ir_rvalue *op[3];
1408 const struct glsl_type *type, *orig_type;
1409 bool error_emitted = false;
1410 YYLTYPE loc;
1411
1412 loc = this->get_location();
1413
1414 switch (this->oper) {
1415 case ast_aggregate:
1416 unreachable("ast_aggregate: Should never get here.")do { (static_cast <bool> (!"ast_aggregate: Should never get here."
) ? void (0) : __assert_fail ("!\"ast_aggregate: Should never get here.\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
1417
1418 case ast_assign: {
1419 this->subexpressions[0]->set_is_lhs(true);
1420 op[0] = this->subexpressions[0]->hir(instructions, state);
1421 op[1] = this->subexpressions[1]->hir(instructions, state);
1422
1423 error_emitted =
1424 do_assignment(instructions, state,
1425 this->subexpressions[0]->non_lvalue_description,
1426 op[0], op[1], &result, needs_rvalue, false,
1427 this->subexpressions[0]->get_location());
1428 break;
1429 }
1430
1431 case ast_plus:
1432 op[0] = this->subexpressions[0]->hir(instructions, state);
1433
1434 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1435
1436 error_emitted = type->is_error();
1437
1438 result = op[0];
1439 break;
1440
1441 case ast_neg:
1442 op[0] = this->subexpressions[0]->hir(instructions, state);
1443
1444 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1445
1446 error_emitted = type->is_error();
1447
1448 result = new(ctx) ir_expression(operations[this->oper], type,
1449 op[0], NULL__null);
1450 break;
1451
1452 case ast_add:
1453 case ast_sub:
1454 case ast_mul:
1455 case ast_div:
1456 op[0] = this->subexpressions[0]->hir(instructions, state);
1457 op[1] = this->subexpressions[1]->hir(instructions, state);
1458
1459 type = arithmetic_result_type(op[0], op[1],
1460 (this->oper == ast_mul),
1461 state, & loc);
1462 error_emitted = type->is_error();
1463
1464 result = new(ctx) ir_expression(operations[this->oper], type,
1465 op[0], op[1]);
1466 break;
1467
1468 case ast_mod:
1469 op[0] = this->subexpressions[0]->hir(instructions, state);
1470 op[1] = this->subexpressions[1]->hir(instructions, state);
1471
1472 type = modulus_result_type(op[0], op[1], state, &loc);
1473
1474 assert(operations[this->oper] == ir_binop_mod)(static_cast <bool> (operations[this->oper] == ir_binop_mod
) ? void (0) : __assert_fail ("operations[this->oper] == ir_binop_mod"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1475
1476 result = new(ctx) ir_expression(operations[this->oper], type,
1477 op[0], op[1]);
1478 error_emitted = type->is_error();
1479 break;
1480
1481 case ast_lshift:
1482 case ast_rshift:
1483 if (!state->check_bitwise_operations_allowed(&loc)) {
1484 error_emitted = true;
1485 }
1486
1487 op[0] = this->subexpressions[0]->hir(instructions, state);
1488 op[1] = this->subexpressions[1]->hir(instructions, state);
1489 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1490 &loc);
1491 result = new(ctx) ir_expression(operations[this->oper], type,
1492 op[0], op[1]);
1493 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1494 break;
1495
1496 case ast_less:
1497 case ast_greater:
1498 case ast_lequal:
1499 case ast_gequal:
1500 op[0] = this->subexpressions[0]->hir(instructions, state);
1501 op[1] = this->subexpressions[1]->hir(instructions, state);
1502
1503 type = relational_result_type(op[0], op[1], state, & loc);
1504
1505 /* The relational operators must either generate an error or result
1506 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1507 */
1508 assert(type->is_error()(static_cast <bool> (type->is_error() || (type->is_boolean
() && type->is_scalar())) ? void (0) : __assert_fail
("type->is_error() || (type->is_boolean() && type->is_scalar())"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
1509 || (type->is_boolean() && type->is_scalar()))(static_cast <bool> (type->is_error() || (type->is_boolean
() && type->is_scalar())) ? void (0) : __assert_fail
("type->is_error() || (type->is_boolean() && type->is_scalar())"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1510
1511 /* Like NIR, GLSL IR does not have opcodes for > or <=. Instead, swap
1512 * the arguments and use < or >=.
1513 */
1514 if (this->oper == ast_greater || this->oper == ast_lequal) {
1515 ir_rvalue *const tmp = op[0];
1516 op[0] = op[1];
1517 op[1] = tmp;
1518 }
1519
1520 result = new(ctx) ir_expression(operations[this->oper], type,
1521 op[0], op[1]);
1522 error_emitted = type->is_error();
1523 break;
1524
1525 case ast_nequal:
1526 case ast_equal:
1527 op[0] = this->subexpressions[0]->hir(instructions, state);
1528 op[1] = this->subexpressions[1]->hir(instructions, state);
1529
1530 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1531 *
1532 * "The equality operators equal (==), and not equal (!=)
1533 * operate on all types. They result in a scalar Boolean. If
1534 * the operand types do not match, then there must be a
1535 * conversion from Section 4.1.10 "Implicit Conversions"
1536 * applied to one operand that can make them match, in which
1537 * case this conversion is done."
1538 */
1539
1540 if (op[0]->type == glsl_type::void_type || op[1]->type == glsl_type::void_type) {
1541 _mesa_glsl_error(& loc, state, "`%s': wrong operand types: "
1542 "no operation `%1$s' exists that takes a left-hand "
1543 "operand of type 'void' or a right operand of type "
1544 "'void'", (this->oper == ast_equal) ? "==" : "!=");
1545 error_emitted = true;
1546 } else if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1547 && !apply_implicit_conversion(op[1]->type, op[0], state))
1548 || (op[0]->type != op[1]->type)) {
1549 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
1550 "type", (this->oper == ast_equal) ? "==" : "!=");
1551 error_emitted = true;
1552 } else if ((op[0]->type->is_array() || op[1]->type->is_array()) &&
1553 !state->check_version(120, 300, &loc,
1554 "array comparisons forbidden")) {
1555 error_emitted = true;
1556 } else if ((op[0]->type->contains_subroutine() ||
1557 op[1]->type->contains_subroutine())) {
1558 _mesa_glsl_error(&loc, state, "subroutine comparisons forbidden");
1559 error_emitted = true;
1560 } else if ((op[0]->type->contains_opaque() ||
1561 op[1]->type->contains_opaque())) {
1562 _mesa_glsl_error(&loc, state, "opaque type comparisons forbidden");
1563 error_emitted = true;
1564 }
1565
1566 if (error_emitted) {
1567 result = new(ctx) ir_constant(false);
1568 } else {
1569 result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
1570 assert(result->type == glsl_type::bool_type)(static_cast <bool> (result->type == glsl_type::bool_type
) ? void (0) : __assert_fail ("result->type == glsl_type::bool_type"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1571 }
1572 break;
1573
1574 case ast_bit_and:
1575 case ast_bit_xor:
1576 case ast_bit_or:
1577 op[0] = this->subexpressions[0]->hir(instructions, state);
1578 op[1] = this->subexpressions[1]->hir(instructions, state);
1579 type = bit_logic_result_type(op[0], op[1], this->oper, state, &loc);
1580 result = new(ctx) ir_expression(operations[this->oper], type,
1581 op[0], op[1]);
1582 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1583 break;
1584
1585 case ast_bit_not:
1586 op[0] = this->subexpressions[0]->hir(instructions, state);
1587
1588 if (!state->check_bitwise_operations_allowed(&loc)) {
1589 error_emitted = true;
1590 }
1591
1592 if (!op[0]->type->is_integer_32_64()) {
1593 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
1594 error_emitted = true;
1595 }
1596
1597 type = error_emitted ? glsl_type::error_type : op[0]->type;
1598 result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL__null);
1599 break;
1600
1601 case ast_logic_and: {
1602 exec_list rhs_instructions;
1603 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1604 "LHS", &error_emitted);
1605 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1606 "RHS", &error_emitted);
1607
1608 if (rhs_instructions.is_empty()) {
1609 result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
1610 } else {
1611 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
1612 "and_tmp",
1613 ir_var_temporary);
1614 instructions->push_tail(tmp);
1615
1616 ir_if *const stmt = new(ctx) ir_if(op[0]);
1617 instructions->push_tail(stmt);
1618
1619 stmt->then_instructions.append_list(&rhs_instructions);
1620 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1621 ir_assignment *const then_assign =
1622 new(ctx) ir_assignment(then_deref, op[1]);
1623 stmt->then_instructions.push_tail(then_assign);
1624
1625 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1626 ir_assignment *const else_assign =
1627 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false));
1628 stmt->else_instructions.push_tail(else_assign);
1629
1630 result = new(ctx) ir_dereference_variable(tmp);
1631 }
1632 break;
1633 }
1634
1635 case ast_logic_or: {
1636 exec_list rhs_instructions;
1637 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1638 "LHS", &error_emitted);
1639 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1640 "RHS", &error_emitted);
1641
1642 if (rhs_instructions.is_empty()) {
1643 result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
1644 } else {
1645 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
1646 "or_tmp",
1647 ir_var_temporary);
1648 instructions->push_tail(tmp);
1649
1650 ir_if *const stmt = new(ctx) ir_if(op[0]);
1651 instructions->push_tail(stmt);
1652
1653 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
1654 ir_assignment *const then_assign =
1655 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true));
1656 stmt->then_instructions.push_tail(then_assign);
1657
1658 stmt->else_instructions.append_list(&rhs_instructions);
1659 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
1660 ir_assignment *const else_assign =
1661 new(ctx) ir_assignment(else_deref, op[1]);
1662 stmt->else_instructions.push_tail(else_assign);
1663
1664 result = new(ctx) ir_dereference_variable(tmp);
1665 }
1666 break;
1667 }
1668
1669 case ast_logic_xor:
1670 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1671 *
1672 * "The logical binary operators and (&&), or ( | | ), and
1673 * exclusive or (^^). They operate only on two Boolean
1674 * expressions and result in a Boolean expression."
1675 */
1676 op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
1677 &error_emitted);
1678 op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
1679 &error_emitted);
1680
1681 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1682 op[0], op[1]);
1683 break;
1684
1685 case ast_logic_not:
1686 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1687 "operand", &error_emitted);
1688
1689 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1690 op[0], NULL__null);
1691 break;
1692
1693 case ast_mul_assign:
1694 case ast_div_assign:
1695 case ast_add_assign:
1696 case ast_sub_assign: {
1697 this->subexpressions[0]->set_is_lhs(true);
1698 op[0] = this->subexpressions[0]->hir(instructions, state);
1699 op[1] = this->subexpressions[1]->hir(instructions, state);
1700
1701 orig_type = op[0]->type;
1702
1703 /* Break out if operand types were not parsed successfully. */
1704 if ((op[0]->type == glsl_type::error_type ||
1705 op[1]->type == glsl_type::error_type)) {
1706 error_emitted = true;
1707 break;
1708 }
1709
1710 type = arithmetic_result_type(op[0], op[1],
1711 (this->oper == ast_mul_assign),
1712 state, & loc);
1713
1714 if (type != orig_type) {
1715 _mesa_glsl_error(& loc, state,
1716 "could not implicitly convert "
1717 "%s to %s", type->name, orig_type->name);
1718 type = glsl_type::error_type;
1719 }
1720
1721 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1722 op[0], op[1]);
1723
1724 error_emitted =
1725 do_assignment(instructions, state,
1726 this->subexpressions[0]->non_lvalue_description,
1727 op[0]->clone(ctx, NULL__null), temp_rhs,
1728 &result, needs_rvalue, false,
1729 this->subexpressions[0]->get_location());
1730
1731 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1732 * explicitly test for this because none of the binary expression
1733 * operators allow array operands either.
1734 */
1735
1736 break;
1737 }
1738
1739 case ast_mod_assign: {
1740 this->subexpressions[0]->set_is_lhs(true);
1741 op[0] = this->subexpressions[0]->hir(instructions, state);
1742 op[1] = this->subexpressions[1]->hir(instructions, state);
1743
1744 orig_type = op[0]->type;
1745 type = modulus_result_type(op[0], op[1], state, &loc);
1746
1747 if (type != orig_type) {
1748 _mesa_glsl_error(& loc, state,
1749 "could not implicitly convert "
1750 "%s to %s", type->name, orig_type->name);
1751 type = glsl_type::error_type;
1752 }
1753
1754 assert(operations[this->oper] == ir_binop_mod)(static_cast <bool> (operations[this->oper] == ir_binop_mod
) ? void (0) : __assert_fail ("operations[this->oper] == ir_binop_mod"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
1755
1756 ir_rvalue *temp_rhs;
1757 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1758 op[0], op[1]);
1759
1760 error_emitted =
1761 do_assignment(instructions, state,
1762 this->subexpressions[0]->non_lvalue_description,
1763 op[0]->clone(ctx, NULL__null), temp_rhs,
1764 &result, needs_rvalue, false,
1765 this->subexpressions[0]->get_location());
1766 break;
1767 }
1768
1769 case ast_ls_assign:
1770 case ast_rs_assign: {
1771 this->subexpressions[0]->set_is_lhs(true);
1772 op[0] = this->subexpressions[0]->hir(instructions, state);
1773 op[1] = this->subexpressions[1]->hir(instructions, state);
1774 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1775 &loc);
1776 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1777 type, op[0], op[1]);
1778 error_emitted =
1779 do_assignment(instructions, state,
1780 this->subexpressions[0]->non_lvalue_description,
1781 op[0]->clone(ctx, NULL__null), temp_rhs,
1782 &result, needs_rvalue, false,
1783 this->subexpressions[0]->get_location());
1784 break;
1785 }
1786
1787 case ast_and_assign:
1788 case ast_xor_assign:
1789 case ast_or_assign: {
1790 this->subexpressions[0]->set_is_lhs(true);
1791 op[0] = this->subexpressions[0]->hir(instructions, state);
1792 op[1] = this->subexpressions[1]->hir(instructions, state);
1793
1794 orig_type = op[0]->type;
1795 type = bit_logic_result_type(op[0], op[1], this->oper, state, &loc);
1796
1797 if (type != orig_type) {
1798 _mesa_glsl_error(& loc, state,
1799 "could not implicitly convert "
1800 "%s to %s", type->name, orig_type->name);
1801 type = glsl_type::error_type;
1802 }
1803
1804 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1805 type, op[0], op[1]);
1806 error_emitted =
1807 do_assignment(instructions, state,
1808 this->subexpressions[0]->non_lvalue_description,
1809 op[0]->clone(ctx, NULL__null), temp_rhs,
1810 &result, needs_rvalue, false,
1811 this->subexpressions[0]->get_location());
1812 break;
1813 }
1814
1815 case ast_conditional: {
1816 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1817 *
1818 * "The ternary selection operator (?:). It operates on three
1819 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1820 * first expression, which must result in a scalar Boolean."
1821 */
1822 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1823 "condition", &error_emitted);
1824
1825 /* The :? operator is implemented by generating an anonymous temporary
1826 * followed by an if-statement. The last instruction in each branch of
1827 * the if-statement assigns a value to the anonymous temporary. This
1828 * temporary is the r-value of the expression.
1829 */
1830 exec_list then_instructions;
1831 exec_list else_instructions;
1832
1833 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1834 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
1835
1836 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1837 *
1838 * "The second and third expressions can be any type, as
1839 * long their types match, or there is a conversion in
1840 * Section 4.1.10 "Implicit Conversions" that can be applied
1841 * to one of the expressions to make their types match. This
1842 * resulting matching type is the type of the entire
1843 * expression."
1844 */
1845 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1846 && !apply_implicit_conversion(op[2]->type, op[1], state))
1847 || (op[1]->type != op[2]->type)) {
1848 YYLTYPE loc = this->subexpressions[1]->get_location();
1849
1850 _mesa_glsl_error(& loc, state, "second and third operands of ?: "
1851 "operator must have matching types");
1852 error_emitted = true;
1853 type = glsl_type::error_type;
1854 } else {
1855 type = op[1]->type;
1856 }
1857
1858 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1859 *
1860 * "The second and third expressions must be the same type, but can
1861 * be of any type other than an array."
1862 */
1863 if (type->is_array() &&
1864 !state->check_version(120, 300, &loc,
1865 "second and third operands of ?: operator "
1866 "cannot be arrays")) {
1867 error_emitted = true;
1868 }
1869
1870 /* From section 4.1.7 of the GLSL 4.50 spec (Opaque Types):
1871 *
1872 * "Except for array indexing, structure member selection, and
1873 * parentheses, opaque variables are not allowed to be operands in
1874 * expressions; such use results in a compile-time error."
1875 */
1876 if (type->contains_opaque()) {
1877 if (!(state->has_bindless() && (type->is_image() || type->is_sampler()))) {
1878 _mesa_glsl_error(&loc, state, "variables of type %s cannot be "
1879 "operands of the ?: operator", type->name);
1880 error_emitted = true;
1881 }
1882 }
1883
1884 ir_constant *cond_val = op[0]->constant_expression_value(ctx);
1885
1886 if (then_instructions.is_empty()
1887 && else_instructions.is_empty()
1888 && cond_val != NULL__null) {
1889 result = cond_val->value.b[0] ? op[1] : op[2];
1890 } else {
1891 /* The copy to conditional_tmp reads the whole array. */
1892 if (type->is_array()) {
1893 mark_whole_array_access(op[1]);
1894 mark_whole_array_access(op[2]);
1895 }
1896
1897 ir_variable *const tmp =
1898 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
1899 instructions->push_tail(tmp);
1900
1901 ir_if *const stmt = new(ctx) ir_if(op[0]);
1902 instructions->push_tail(stmt);
1903
1904 then_instructions.move_nodes_to(& stmt->then_instructions);
1905 ir_dereference *const then_deref =
1906 new(ctx) ir_dereference_variable(tmp);
1907 ir_assignment *const then_assign =
1908 new(ctx) ir_assignment(then_deref, op[1]);
1909 stmt->then_instructions.push_tail(then_assign);
1910
1911 else_instructions.move_nodes_to(& stmt->else_instructions);
1912 ir_dereference *const else_deref =
1913 new(ctx) ir_dereference_variable(tmp);
1914 ir_assignment *const else_assign =
1915 new(ctx) ir_assignment(else_deref, op[2]);
1916 stmt->else_instructions.push_tail(else_assign);
1917
1918 result = new(ctx) ir_dereference_variable(tmp);
1919 }
1920 break;
1921 }
1922
1923 case ast_pre_inc:
1924 case ast_pre_dec: {
1925 this->non_lvalue_description = (this->oper == ast_pre_inc)
1926 ? "pre-increment operation" : "pre-decrement operation";
1927
1928 op[0] = this->subexpressions[0]->hir(instructions, state);
1929 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1930
1931 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1932
1933 ir_rvalue *temp_rhs;
1934 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1935 op[0], op[1]);
1936
1937 error_emitted =
1938 do_assignment(instructions, state,
1939 this->subexpressions[0]->non_lvalue_description,
1940 op[0]->clone(ctx, NULL__null), temp_rhs,
1941 &result, needs_rvalue, false,
1942 this->subexpressions[0]->get_location());
1943 break;
1944 }
1945
1946 case ast_post_inc:
1947 case ast_post_dec: {
1948 this->non_lvalue_description = (this->oper == ast_post_inc)
1949 ? "post-increment operation" : "post-decrement operation";
1950 op[0] = this->subexpressions[0]->hir(instructions, state);
1951 op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1952
1953 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1954
1955 if (error_emitted) {
1956 result = ir_rvalue::error_value(ctx);
1957 break;
1958 }
1959
1960 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1961
1962 ir_rvalue *temp_rhs;
1963 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1964 op[0], op[1]);
1965
1966 /* Get a temporary of a copy of the lvalue before it's modified.
1967 * This may get thrown away later.
1968 */
1969 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL__null));
1970
1971 ir_rvalue *junk_rvalue;
1972 error_emitted =
1973 do_assignment(instructions, state,
1974 this->subexpressions[0]->non_lvalue_description,
1975 op[0]->clone(ctx, NULL__null), temp_rhs,
1976 &junk_rvalue, false, false,
1977 this->subexpressions[0]->get_location());
1978
1979 break;
1980 }
1981
1982 case ast_field_selection:
1983 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1984 break;
1985
1986 case ast_array_index: {
1987 YYLTYPE index_loc = subexpressions[1]->get_location();
1988
1989 /* Getting if an array is being used uninitialized is beyond what we get
1990 * from ir_value.data.assigned. Setting is_lhs as true would force to
1991 * not raise a uninitialized warning when using an array
1992 */
1993 subexpressions[0]->set_is_lhs(true);
1994 op[0] = subexpressions[0]->hir(instructions, state);
1995 op[1] = subexpressions[1]->hir(instructions, state);
1996
1997 result = _mesa_ast_array_index_to_hir(ctx, state, op[0], op[1],
1998 loc, index_loc);
1999
2000 if (result->type->is_error())
2001 error_emitted = true;
2002
2003 break;
2004 }
2005
2006 case ast_unsized_array_dim:
2007 unreachable("ast_unsized_array_dim: Should never get here.")do { (static_cast <bool> (!"ast_unsized_array_dim: Should never get here."
) ? void (0) : __assert_fail ("!\"ast_unsized_array_dim: Should never get here.\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
2008
2009 case ast_function_call:
2010 /* Should *NEVER* get here. ast_function_call should always be handled
2011 * by ast_function_expression::hir.
2012 */
2013 unreachable("ast_function_call: handled elsewhere ")do { (static_cast <bool> (!"ast_function_call: handled elsewhere "
) ? void (0) : __assert_fail ("!\"ast_function_call: handled elsewhere \""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
2014
2015 case ast_identifier: {
2016 /* ast_identifier can appear several places in a full abstract syntax
2017 * tree. This particular use must be at location specified in the grammar
2018 * as 'variable_identifier'.
2019 */
2020 ir_variable *var =
2021 state->symbols->get_variable(this->primary_expression.identifier);
2022
2023 if (var == NULL__null) {
2024 /* the identifier might be a subroutine name */
2025 char *sub_name;
2026 sub_name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), this->primary_expression.identifier);
2027 var = state->symbols->get_variable(sub_name);
2028 ralloc_free(sub_name);
2029 }
2030
2031 if (var != NULL__null) {
2032 var->data.used = true;
2033 result = new(ctx) ir_dereference_variable(var);
2034
2035 if ((var->data.mode == ir_var_auto || var->data.mode == ir_var_shader_out)
2036 && !this->is_lhs
2037 && result->variable_referenced()->data.assigned != true
2038 && !is_gl_identifier(var->name)) {
2039 _mesa_glsl_warning(&loc, state, "`%s' used uninitialized",
2040 this->primary_expression.identifier);
2041 }
2042
2043 /* From the EXT_shader_framebuffer_fetch spec:
2044 *
2045 * "Unless the GL_EXT_shader_framebuffer_fetch extension has been
2046 * enabled in addition, it's an error to use gl_LastFragData if it
2047 * hasn't been explicitly redeclared with layout(noncoherent)."
2048 */
2049 if (var->data.fb_fetch_output && var->data.memory_coherent &&
2050 !state->EXT_shader_framebuffer_fetch_enable) {
2051 _mesa_glsl_error(&loc, state,
2052 "invalid use of framebuffer fetch output not "
2053 "qualified with layout(noncoherent)");
2054 }
2055
2056 } else {
2057 _mesa_glsl_error(& loc, state, "`%s' undeclared",
2058 this->primary_expression.identifier);
2059
2060 result = ir_rvalue::error_value(ctx);
2061 error_emitted = true;
2062 }
2063 break;
2064 }
2065
2066 case ast_int_constant:
2067 result = new(ctx) ir_constant(this->primary_expression.int_constant);
2068 break;
2069
2070 case ast_uint_constant:
2071 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
2072 break;
2073
2074 case ast_float_constant:
2075 result = new(ctx) ir_constant(this->primary_expression.float_constant);
2076 break;
2077
2078 case ast_bool_constant:
2079 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
2080 break;
2081
2082 case ast_double_constant:
2083 result = new(ctx) ir_constant(this->primary_expression.double_constant);
2084 break;
2085
2086 case ast_uint64_constant:
2087 result = new(ctx) ir_constant(this->primary_expression.uint64_constant);
2088 break;
2089
2090 case ast_int64_constant:
2091 result = new(ctx) ir_constant(this->primary_expression.int64_constant);
2092 break;
2093
2094 case ast_sequence: {
2095 /* It should not be possible to generate a sequence in the AST without
2096 * any expressions in it.
2097 */
2098 assert(!this->expressions.is_empty())(static_cast <bool> (!this->expressions.is_empty()) ?
void (0) : __assert_fail ("!this->expressions.is_empty()"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2099
2100 /* The r-value of a sequence is the last expression in the sequence. If
2101 * the other expressions in the sequence do not have side-effects (and
2102 * therefore add instructions to the instruction list), they get dropped
2103 * on the floor.
2104 */
2105 exec_node *previous_tail = NULL__null;
2106 YYLTYPE previous_operand_loc = loc;
2107
2108 foreach_list_typed (ast_node, ast, link, &this->expressions)for (ast_node * ast = (!exec_node_is_tail_sentinel((&this
->expressions)->head_sentinel.next) ? ((ast_node *) (((
uintptr_t) (&this->expressions)->head_sentinel.next
) - (((char *) &((ast_node *) (&this->expressions)
->head_sentinel.next)->link) - ((char *) (&this->
expressions)->head_sentinel.next)))) : __null); (ast) != __null
; (ast) = (!exec_node_is_tail_sentinel((ast)->link.next) ?
((ast_node *) (((uintptr_t) (ast)->link.next) - (((char *
) &((ast_node *) (ast)->link.next)->link) - ((char *
) (ast)->link.next)))) : __null))
{
2109 /* If one of the operands of comma operator does not generate any
2110 * code, we want to emit a warning. At each pass through the loop
2111 * previous_tail will point to the last instruction in the stream
2112 * *before* processing the previous operand. Naturally,
2113 * instructions->get_tail_raw() will point to the last instruction in
2114 * the stream *after* processing the previous operand. If the two
2115 * pointers match, then the previous operand had no effect.
2116 *
2117 * The warning behavior here differs slightly from GCC. GCC will
2118 * only emit a warning if none of the left-hand operands have an
2119 * effect. However, it will emit a warning for each. I believe that
2120 * there are some cases in C (especially with GCC extensions) where
2121 * it is useful to have an intermediate step in a sequence have no
2122 * effect, but I don't think these cases exist in GLSL. Either way,
2123 * it would be a giant hassle to replicate that behavior.
2124 */
2125 if (previous_tail == instructions->get_tail_raw()) {
2126 _mesa_glsl_warning(&previous_operand_loc, state,
2127 "left-hand operand of comma expression has "
2128 "no effect");
2129 }
2130
2131 /* The tail is directly accessed instead of using the get_tail()
2132 * method for performance reasons. get_tail() has extra code to
2133 * return NULL when the list is empty. We don't care about that
2134 * here, so using get_tail_raw() is fine.
2135 */
2136 previous_tail = instructions->get_tail_raw();
2137 previous_operand_loc = ast->get_location();
2138
2139 result = ast->hir(instructions, state);
2140 }
2141
2142 /* Any errors should have already been emitted in the loop above.
2143 */
2144 error_emitted = true;
2145 break;
2146 }
2147 }
2148 type = NULL__null; /* use result->type, not type. */
2149 assert(error_emitted || (result != NULL || !needs_rvalue))(static_cast <bool> (error_emitted || (result != __null
|| !needs_rvalue)) ? void (0) : __assert_fail ("error_emitted || (result != NULL || !needs_rvalue)"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2150
2151 if (result && result->type->is_error() && !error_emitted)
2152 _mesa_glsl_error(& loc, state, "type mismatch");
2153
2154 return result;
2155}
2156
2157bool
2158ast_expression::has_sequence_subexpression() const
2159{
2160 switch (this->oper) {
2161 case ast_plus:
2162 case ast_neg:
2163 case ast_bit_not:
2164 case ast_logic_not:
2165 case ast_pre_inc:
2166 case ast_pre_dec:
2167 case ast_post_inc:
2168 case ast_post_dec:
2169 return this->subexpressions[0]->has_sequence_subexpression();
2170
2171 case ast_assign:
2172 case ast_add:
2173 case ast_sub:
2174 case ast_mul:
2175 case ast_div:
2176 case ast_mod:
2177 case ast_lshift:
2178 case ast_rshift:
2179 case ast_less:
2180 case ast_greater:
2181 case ast_lequal:
2182 case ast_gequal:
2183 case ast_nequal:
2184 case ast_equal:
2185 case ast_bit_and:
2186 case ast_bit_xor:
2187 case ast_bit_or:
2188 case ast_logic_and:
2189 case ast_logic_or:
2190 case ast_logic_xor:
2191 case ast_array_index:
2192 case ast_mul_assign:
2193 case ast_div_assign:
2194 case ast_add_assign:
2195 case ast_sub_assign:
2196 case ast_mod_assign:
2197 case ast_ls_assign:
2198 case ast_rs_assign:
2199 case ast_and_assign:
2200 case ast_xor_assign:
2201 case ast_or_assign:
2202 return this->subexpressions[0]->has_sequence_subexpression() ||
2203 this->subexpressions[1]->has_sequence_subexpression();
2204
2205 case ast_conditional:
2206 return this->subexpressions[0]->has_sequence_subexpression() ||
2207 this->subexpressions[1]->has_sequence_subexpression() ||
2208 this->subexpressions[2]->has_sequence_subexpression();
2209
2210 case ast_sequence:
2211 return true;
2212
2213 case ast_field_selection:
2214 case ast_identifier:
2215 case ast_int_constant:
2216 case ast_uint_constant:
2217 case ast_float_constant:
2218 case ast_bool_constant:
2219 case ast_double_constant:
2220 case ast_int64_constant:
2221 case ast_uint64_constant:
2222 return false;
2223
2224 case ast_aggregate:
2225 return false;
2226
2227 case ast_function_call:
2228 unreachable("should be handled by ast_function_expression::hir")do { (static_cast <bool> (!"should be handled by ast_function_expression::hir"
) ? void (0) : __assert_fail ("!\"should be handled by ast_function_expression::hir\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
2229
2230 case ast_unsized_array_dim:
2231 unreachable("ast_unsized_array_dim: Should never get here.")do { (static_cast <bool> (!"ast_unsized_array_dim: Should never get here."
) ? void (0) : __assert_fail ("!\"ast_unsized_array_dim: Should never get here.\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
2232 }
2233
2234 return false;
2235}
2236
2237ir_rvalue *
2238ast_expression_statement::hir(exec_list *instructions,
2239 struct _mesa_glsl_parse_state *state)
2240{
2241 /* It is possible to have expression statements that don't have an
2242 * expression. This is the solitary semicolon:
2243 *
2244 * for (i = 0; i < 5; i++)
2245 * ;
2246 *
2247 * In this case the expression will be NULL. Test for NULL and don't do
2248 * anything in that case.
2249 */
2250 if (expression != NULL__null)
2251 expression->hir_no_rvalue(instructions, state);
2252
2253 /* Statements do not have r-values.
2254 */
2255 return NULL__null;
2256}
2257
2258
2259ir_rvalue *
2260ast_compound_statement::hir(exec_list *instructions,
2261 struct _mesa_glsl_parse_state *state)
2262{
2263 if (new_scope)
2264 state->symbols->push_scope();
2265
2266 foreach_list_typed (ast_node, ast, link, &this->statements)for (ast_node * ast = (!exec_node_is_tail_sentinel((&this
->statements)->head_sentinel.next) ? ((ast_node *) (((uintptr_t
) (&this->statements)->head_sentinel.next) - (((char
*) &((ast_node *) (&this->statements)->head_sentinel
.next)->link) - ((char *) (&this->statements)->head_sentinel
.next)))) : __null); (ast) != __null; (ast) = (!exec_node_is_tail_sentinel
((ast)->link.next) ? ((ast_node *) (((uintptr_t) (ast)->
link.next) - (((char *) &((ast_node *) (ast)->link.next
)->link) - ((char *) (ast)->link.next)))) : __null))
2267 ast->hir(instructions, state);
2268
2269 if (new_scope)
2270 state->symbols->pop_scope();
2271
2272 /* Compound statements do not have r-values.
2273 */
2274 return NULL__null;
2275}
2276
2277/**
2278 * Evaluate the given exec_node (which should be an ast_node representing
2279 * a single array dimension) and return its integer value.
2280 */
2281static unsigned
2282process_array_size(exec_node *node,
2283 struct _mesa_glsl_parse_state *state)
2284{
2285 void *mem_ctx = state;
2286
2287 exec_list dummy_instructions;
2288
2289 ast_node *array_size = exec_node_data(ast_node, node, link)((ast_node *) (((uintptr_t) node) - (((char *) &((ast_node
*) node)->link) - ((char *) node))))
;
2290
2291 /**
2292 * Dimensions other than the outermost dimension can by unsized if they
2293 * are immediately sized by a constructor or initializer.
2294 */
2295 if (((ast_expression*)array_size)->oper == ast_unsized_array_dim)
2296 return 0;
2297
2298 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
2299 YYLTYPE loc = array_size->get_location();
2300
2301 if (ir == NULL__null) {
2302 _mesa_glsl_error(& loc, state,
2303 "array size could not be resolved");
2304 return 0;
2305 }
2306
2307 if (!ir->type->is_integer_32()) {
2308 _mesa_glsl_error(& loc, state,
2309 "array size must be integer type");
2310 return 0;
2311 }
2312
2313 if (!ir->type->is_scalar()) {
2314 _mesa_glsl_error(& loc, state,
2315 "array size must be scalar type");
2316 return 0;
2317 }
2318
2319 ir_constant *const size = ir->constant_expression_value(mem_ctx);
2320 if (size == NULL__null ||
2321 (state->is_version(120, 300) &&
2322 array_size->has_sequence_subexpression())) {
2323 _mesa_glsl_error(& loc, state, "array size must be a "
2324 "constant valued expression");
2325 return 0;
2326 }
2327
2328 if (size->value.i[0] <= 0) {
2329 _mesa_glsl_error(& loc, state, "array size must be > 0");
2330 return 0;
2331 }
2332
2333 assert(size->type == ir->type)(static_cast <bool> (size->type == ir->type) ? void
(0) : __assert_fail ("size->type == ir->type", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
2334
2335 /* If the array size is const (and we've verified that
2336 * it is) then no instructions should have been emitted
2337 * when we converted it to HIR. If they were emitted,
2338 * then either the array size isn't const after all, or
2339 * we are emitting unnecessary instructions.
2340 */
2341 assert(dummy_instructions.is_empty())(static_cast <bool> (dummy_instructions.is_empty()) ? void
(0) : __assert_fail ("dummy_instructions.is_empty()", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
2342
2343 return size->value.u[0];
2344}
2345
2346static const glsl_type *
2347process_array_type(YYLTYPE *loc, const glsl_type *base,
2348 ast_array_specifier *array_specifier,
2349 struct _mesa_glsl_parse_state *state)
2350{
2351 const glsl_type *array_type = base;
2352
2353 if (array_specifier != NULL__null) {
2354 if (base->is_array()) {
2355
2356 /* From page 19 (page 25) of the GLSL 1.20 spec:
2357 *
2358 * "Only one-dimensional arrays may be declared."
2359 */
2360 if (!state->check_arrays_of_arrays_allowed(loc)) {
2361 return glsl_type::error_type;
2362 }
2363 }
2364
2365 for (exec_node *node = array_specifier->array_dimensions.get_tail_raw();
2366 !node->is_head_sentinel(); node = node->prev) {
2367 unsigned array_size = process_array_size(node, state);
2368 array_type = glsl_type::get_array_instance(array_type, array_size);
2369 }
2370 }
2371
2372 return array_type;
2373}
2374
2375static bool
2376precision_qualifier_allowed(const glsl_type *type)
2377{
2378 /* Precision qualifiers apply to floating point, integer and opaque
2379 * types.
2380 *
2381 * Section 4.5.2 (Precision Qualifiers) of the GLSL 1.30 spec says:
2382 * "Any floating point or any integer declaration can have the type
2383 * preceded by one of these precision qualifiers [...] Literal
2384 * constants do not have precision qualifiers. Neither do Boolean
2385 * variables.
2386 *
2387 * Section 4.5 (Precision and Precision Qualifiers) of the GLSL 1.30
2388 * spec also says:
2389 *
2390 * "Precision qualifiers are added for code portability with OpenGL
2391 * ES, not for functionality. They have the same syntax as in OpenGL
2392 * ES."
2393 *
2394 * Section 8 (Built-In Functions) of the GLSL ES 1.00 spec says:
2395 *
2396 * "uniform lowp sampler2D sampler;
2397 * highp vec2 coord;
2398 * ...
2399 * lowp vec4 col = texture2D (sampler, coord);
2400 * // texture2D returns lowp"
2401 *
2402 * From this, we infer that GLSL 1.30 (and later) should allow precision
2403 * qualifiers on sampler types just like float and integer types.
2404 */
2405 const glsl_type *const t = type->without_array();
2406
2407 return (t->is_float() || t->is_integer_32() || t->contains_opaque()) &&
2408 !t->is_struct();
2409}
2410
2411const glsl_type *
2412ast_type_specifier::glsl_type(const char **name,
2413 struct _mesa_glsl_parse_state *state) const
2414{
2415 const struct glsl_type *type;
2416
2417 if (this->type != NULL__null)
2418 type = this->type;
2419 else if (structure)
2420 type = structure->type;
2421 else
2422 type = state->symbols->get_type(this->type_name);
2423 *name = this->type_name;
2424
2425 YYLTYPE loc = this->get_location();
2426 type = process_array_type(&loc, type, this->array_specifier, state);
2427
2428 return type;
2429}
2430
2431/**
2432 * From the OpenGL ES 3.0 spec, 4.5.4 Default Precision Qualifiers:
2433 *
2434 * "The precision statement
2435 *
2436 * precision precision-qualifier type;
2437 *
2438 * can be used to establish a default precision qualifier. The type field can
2439 * be either int or float or any of the sampler types, (...) If type is float,
2440 * the directive applies to non-precision-qualified floating point type
2441 * (scalar, vector, and matrix) declarations. If type is int, the directive
2442 * applies to all non-precision-qualified integer type (scalar, vector, signed,
2443 * and unsigned) declarations."
2444 *
2445 * We use the symbol table to keep the values of the default precisions for
2446 * each 'type' in each scope and we use the 'type' string from the precision
2447 * statement as key in the symbol table. When we want to retrieve the default
2448 * precision associated with a given glsl_type we need to know the type string
2449 * associated with it. This is what this function returns.
2450 */
2451static const char *
2452get_type_name_for_precision_qualifier(const glsl_type *type)
2453{
2454 switch (type->base_type) {
2455 case GLSL_TYPE_FLOAT:
2456 return "float";
2457 case GLSL_TYPE_UINT:
2458 case GLSL_TYPE_INT:
2459 return "int";
2460 case GLSL_TYPE_ATOMIC_UINT:
2461 return "atomic_uint";
2462 case GLSL_TYPE_IMAGE:
2463 /* fallthrough */
2464 case GLSL_TYPE_SAMPLER: {
2465 const unsigned type_idx =
2466 type->sampler_array + 2 * type->sampler_shadow;
2467 const unsigned offset = type->is_sampler() ? 0 : 4;
2468 assert(type_idx < 4)(static_cast <bool> (type_idx < 4) ? void (0) : __assert_fail
("type_idx < 4", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
2469 switch (type->sampled_type) {
2470 case GLSL_TYPE_FLOAT:
2471 switch (type->sampler_dimensionality) {
2472 case GLSL_SAMPLER_DIM_1D: {
2473 assert(type->is_sampler())(static_cast <bool> (type->is_sampler()) ? void (0) :
__assert_fail ("type->is_sampler()", __builtin_FILE (), __builtin_LINE
(), __extension__ __PRETTY_FUNCTION__))
;
2474 static const char *const names[4] = {
2475 "sampler1D", "sampler1DArray",
2476 "sampler1DShadow", "sampler1DArrayShadow"
2477 };
2478 return names[type_idx];
2479 }
2480 case GLSL_SAMPLER_DIM_2D: {
2481 static const char *const names[8] = {
2482 "sampler2D", "sampler2DArray",
2483 "sampler2DShadow", "sampler2DArrayShadow",
2484 "image2D", "image2DArray", NULL__null, NULL__null
2485 };
2486 return names[offset + type_idx];
2487 }
2488 case GLSL_SAMPLER_DIM_3D: {
2489 static const char *const names[8] = {
2490 "sampler3D", NULL__null, NULL__null, NULL__null,
2491 "image3D", NULL__null, NULL__null, NULL__null
2492 };
2493 return names[offset + type_idx];
2494 }
2495 case GLSL_SAMPLER_DIM_CUBE: {
2496 static const char *const names[8] = {
2497 "samplerCube", "samplerCubeArray",
2498 "samplerCubeShadow", "samplerCubeArrayShadow",
2499 "imageCube", NULL__null, NULL__null, NULL__null
2500 };
2501 return names[offset + type_idx];
2502 }
2503 case GLSL_SAMPLER_DIM_MS: {
2504 assert(type->is_sampler())(static_cast <bool> (type->is_sampler()) ? void (0) :
__assert_fail ("type->is_sampler()", __builtin_FILE (), __builtin_LINE
(), __extension__ __PRETTY_FUNCTION__))
;
2505 static const char *const names[4] = {
2506 "sampler2DMS", "sampler2DMSArray", NULL__null, NULL__null
2507 };
2508 return names[type_idx];
2509 }
2510 case GLSL_SAMPLER_DIM_RECT: {
2511 assert(type->is_sampler())(static_cast <bool> (type->is_sampler()) ? void (0) :
__assert_fail ("type->is_sampler()", __builtin_FILE (), __builtin_LINE
(), __extension__ __PRETTY_FUNCTION__))
;
2512 static const char *const names[4] = {
2513 "samplerRect", NULL__null, "samplerRectShadow", NULL__null
2514 };
2515 return names[type_idx];
2516 }
2517 case GLSL_SAMPLER_DIM_BUF: {
2518 static const char *const names[8] = {
2519 "samplerBuffer", NULL__null, NULL__null, NULL__null,
2520 "imageBuffer", NULL__null, NULL__null, NULL__null
2521 };
2522 return names[offset + type_idx];
2523 }
2524 case GLSL_SAMPLER_DIM_EXTERNAL: {
2525 assert(type->is_sampler())(static_cast <bool> (type->is_sampler()) ? void (0) :
__assert_fail ("type->is_sampler()", __builtin_FILE (), __builtin_LINE
(), __extension__ __PRETTY_FUNCTION__))
;
2526 static const char *const names[4] = {
2527 "samplerExternalOES", NULL__null, NULL__null, NULL__null
2528 };
2529 return names[type_idx];
2530 }
2531 default:
2532 unreachable("Unsupported sampler/image dimensionality")do { (static_cast <bool> (!"Unsupported sampler/image dimensionality"
) ? void (0) : __assert_fail ("!\"Unsupported sampler/image dimensionality\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
2533 } /* sampler/image float dimensionality */
2534 break;
2535 case GLSL_TYPE_INT:
2536 switch (type->sampler_dimensionality) {
2537 case GLSL_SAMPLER_DIM_1D: {
2538 assert(type->is_sampler())(static_cast <bool> (type->is_sampler()) ? void (0) :
__assert_fail ("type->is_sampler()", __builtin_FILE (), __builtin_LINE
(), __extension__ __PRETTY_FUNCTION__))
;
2539 static const char *const names[4] = {
2540 "isampler1D", "isampler1DArray", NULL__null, NULL__null
2541 };
2542 return names[type_idx];
2543 }
2544 case GLSL_SAMPLER_DIM_2D: {
2545 static const char *const names[8] = {
2546 "isampler2D", "isampler2DArray", NULL__null, NULL__null,
2547 "iimage2D", "iimage2DArray", NULL__null, NULL__null
2548 };
2549 return names[offset + type_idx];
2550 }
2551 case GLSL_SAMPLER_DIM_3D: {
2552 static const char *const names[8] = {
2553 "isampler3D", NULL__null, NULL__null, NULL__null,
2554 "iimage3D", NULL__null, NULL__null, NULL__null
2555 };
2556 return names[offset + type_idx];
2557 }
2558 case GLSL_SAMPLER_DIM_CUBE: {
2559 static const char *const names[8] = {
2560 "isamplerCube", "isamplerCubeArray", NULL__null, NULL__null,
2561 "iimageCube", NULL__null, NULL__null, NULL__null
2562 };
2563 return names[offset + type_idx];
2564 }
2565 case GLSL_SAMPLER_DIM_MS: {
2566 assert(type->is_sampler())(static_cast <bool> (type->is_sampler()) ? void (0) :
__assert_fail ("type->is_sampler()", __builtin_FILE (), __builtin_LINE
(), __extension__ __PRETTY_FUNCTION__))
;
2567 static const char *const names[4] = {
2568 "isampler2DMS", "isampler2DMSArray", NULL__null, NULL__null
2569 };
2570 return names[type_idx];
2571 }
2572 case GLSL_SAMPLER_DIM_RECT: {
2573 assert(type->is_sampler())(static_cast <bool> (type->is_sampler()) ? void (0) :
__assert_fail ("type->is_sampler()", __builtin_FILE (), __builtin_LINE
(), __extension__ __PRETTY_FUNCTION__))
;
2574 static const char *const names[4] = {
2575 "isamplerRect", NULL__null, "isamplerRectShadow", NULL__null
2576 };
2577 return names[type_idx];
2578 }
2579 case GLSL_SAMPLER_DIM_BUF: {
2580 static const char *const names[8] = {
2581 "isamplerBuffer", NULL__null, NULL__null, NULL__null,
2582 "iimageBuffer", NULL__null, NULL__null, NULL__null
2583 };
2584 return names[offset + type_idx];
2585 }
2586 default:
2587 unreachable("Unsupported isampler/iimage dimensionality")do { (static_cast <bool> (!"Unsupported isampler/iimage dimensionality"
) ? void (0) : __assert_fail ("!\"Unsupported isampler/iimage dimensionality\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
2588 } /* sampler/image int dimensionality */
2589 break;
2590 case GLSL_TYPE_UINT:
2591 switch (type->sampler_dimensionality) {
2592 case GLSL_SAMPLER_DIM_1D: {
2593 assert(type->is_sampler())(static_cast <bool> (type->is_sampler()) ? void (0) :
__assert_fail ("type->is_sampler()", __builtin_FILE (), __builtin_LINE
(), __extension__ __PRETTY_FUNCTION__))
;
2594 static const char *const names[4] = {
2595 "usampler1D", "usampler1DArray", NULL__null, NULL__null
2596 };
2597 return names[type_idx];
2598 }
2599 case GLSL_SAMPLER_DIM_2D: {
2600 static const char *const names[8] = {
2601 "usampler2D", "usampler2DArray", NULL__null, NULL__null,
2602 "uimage2D", "uimage2DArray", NULL__null, NULL__null
2603 };
2604 return names[offset + type_idx];
2605 }
2606 case GLSL_SAMPLER_DIM_3D: {
2607 static const char *const names[8] = {
2608 "usampler3D", NULL__null, NULL__null, NULL__null,
2609 "uimage3D", NULL__null, NULL__null, NULL__null
2610 };
2611 return names[offset + type_idx];
2612 }
2613 case GLSL_SAMPLER_DIM_CUBE: {
2614 static const char *const names[8] = {
2615 "usamplerCube", "usamplerCubeArray", NULL__null, NULL__null,
2616 "uimageCube", NULL__null, NULL__null, NULL__null
2617 };
2618 return names[offset + type_idx];
2619 }
2620 case GLSL_SAMPLER_DIM_MS: {
2621 assert(type->is_sampler())(static_cast <bool> (type->is_sampler()) ? void (0) :
__assert_fail ("type->is_sampler()", __builtin_FILE (), __builtin_LINE
(), __extension__ __PRETTY_FUNCTION__))
;
2622 static const char *const names[4] = {
2623 "usampler2DMS", "usampler2DMSArray", NULL__null, NULL__null
2624 };
2625 return names[type_idx];
2626 }
2627 case GLSL_SAMPLER_DIM_RECT: {
2628 assert(type->is_sampler())(static_cast <bool> (type->is_sampler()) ? void (0) :
__assert_fail ("type->is_sampler()", __builtin_FILE (), __builtin_LINE
(), __extension__ __PRETTY_FUNCTION__))
;
2629 static const char *const names[4] = {
2630 "usamplerRect", NULL__null, "usamplerRectShadow", NULL__null
2631 };
2632 return names[type_idx];
2633 }
2634 case GLSL_SAMPLER_DIM_BUF: {
2635 static const char *const names[8] = {
2636 "usamplerBuffer", NULL__null, NULL__null, NULL__null,
2637 "uimageBuffer", NULL__null, NULL__null, NULL__null
2638 };
2639 return names[offset + type_idx];
2640 }
2641 default:
2642 unreachable("Unsupported usampler/uimage dimensionality")do { (static_cast <bool> (!"Unsupported usampler/uimage dimensionality"
) ? void (0) : __assert_fail ("!\"Unsupported usampler/uimage dimensionality\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
2643 } /* sampler/image uint dimensionality */
2644 break;
2645 default:
2646 unreachable("Unsupported sampler/image type")do { (static_cast <bool> (!"Unsupported sampler/image type"
) ? void (0) : __assert_fail ("!\"Unsupported sampler/image type\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
)); __builtin_unreachable(); } while (0)
;
2647 } /* sampler/image type */
2648 break;
2649 } /* GLSL_TYPE_SAMPLER/GLSL_TYPE_IMAGE */
2650 break;
2651 default:
2652 unreachable("Unsupported type")do { (static_cast <bool> (!"Unsupported type") ? void (
0) : __assert_fail ("!\"Unsupported type\"", __builtin_FILE (
), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__)); __builtin_unreachable
(); } while (0)
;
2653 } /* base type */
2654}
2655
2656static unsigned
2657select_gles_precision(unsigned qual_precision,
2658 const glsl_type *type,
2659 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
2660{
2661 /* Precision qualifiers do not have any meaning in Desktop GLSL.
2662 * In GLES we take the precision from the type qualifier if present,
2663 * otherwise, if the type of the variable allows precision qualifiers at
2664 * all, we look for the default precision qualifier for that type in the
2665 * current scope.
2666 */
2667 assert(state->es_shader)(static_cast <bool> (state->es_shader) ? void (0) : __assert_fail
("state->es_shader", __builtin_FILE (), __builtin_LINE ()
, __extension__ __PRETTY_FUNCTION__))
;
2668
2669 unsigned precision = GLSL_PRECISION_NONE;
2670 if (qual_precision) {
2671 precision = qual_precision;
2672 } else if (precision_qualifier_allowed(type)) {
2673 const char *type_name =
2674 get_type_name_for_precision_qualifier(type->without_array());
2675 assert(type_name != NULL)(static_cast <bool> (type_name != __null) ? void (0) : __assert_fail
("type_name != NULL", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
2676
2677 precision =
2678 state->symbols->get_default_precision_qualifier(type_name);
2679 if (precision == ast_precision_none) {
2680 _mesa_glsl_error(loc, state,
2681 "No precision specified in this scope for type `%s'",
2682 type->name);
2683 }
2684 }
2685
2686
2687 /* Section 4.1.7.3 (Atomic Counters) of the GLSL ES 3.10 spec says:
2688 *
2689 * "The default precision of all atomic types is highp. It is an error to
2690 * declare an atomic type with a different precision or to specify the
2691 * default precision for an atomic type to be lowp or mediump."
2692 */
2693 if (type->is_atomic_uint() && precision != ast_precision_high) {
2694 _mesa_glsl_error(loc, state,
2695 "atomic_uint can only have highp precision qualifier");
2696 }
2697
2698 return precision;
2699}
2700
2701const glsl_type *
2702ast_fully_specified_type::glsl_type(const char **name,
2703 struct _mesa_glsl_parse_state *state) const
2704{
2705 return this->specifier->glsl_type(name, state);
2706}
2707
2708/**
2709 * Determine whether a toplevel variable declaration declares a varying. This
2710 * function operates by examining the variable's mode and the shader target,
2711 * so it correctly identifies linkage variables regardless of whether they are
2712 * declared using the deprecated "varying" syntax or the new "in/out" syntax.
2713 *
2714 * Passing a non-toplevel variable declaration (e.g. a function parameter) to
2715 * this function will produce undefined results.
2716 */
2717static bool
2718is_varying_var(ir_variable *var, gl_shader_stage target)
2719{
2720 switch (target) {
2721 case MESA_SHADER_VERTEX:
2722 return var->data.mode == ir_var_shader_out;
2723 case MESA_SHADER_FRAGMENT:
2724 return var->data.mode == ir_var_shader_in ||
2725 (var->data.mode == ir_var_system_value &&
2726 var->data.location == SYSTEM_VALUE_FRAG_COORD);
2727 default:
2728 return var->data.mode == ir_var_shader_out || var->data.mode == ir_var_shader_in;
2729 }
2730}
2731
2732static bool
2733is_allowed_invariant(ir_variable *var, struct _mesa_glsl_parse_state *state)
2734{
2735 if (is_varying_var(var, state->stage))
2736 return true;
2737
2738 /* From Section 4.6.1 ("The Invariant Qualifier") GLSL 1.20 spec:
2739 * "Only variables output from a vertex shader can be candidates
2740 * for invariance".
2741 */
2742 if (!state->is_version(130, 100))
2743 return false;
2744
2745 /*
2746 * Later specs remove this language - so allowed invariant
2747 * on fragment shader outputs as well.
2748 */
2749 if (state->stage == MESA_SHADER_FRAGMENT &&
2750 var->data.mode == ir_var_shader_out)
2751 return true;
2752 return false;
2753}
2754
2755/**
2756 * Matrix layout qualifiers are only allowed on certain types
2757 */
2758static void
2759validate_matrix_layout_for_type(struct _mesa_glsl_parse_state *state,
2760 YYLTYPE *loc,
2761 const glsl_type *type,
2762 ir_variable *var)
2763{
2764 if (var && !var->is_in_buffer_block()) {
2765 /* Layout qualifiers may only apply to interface blocks and fields in
2766 * them.
2767 */
2768 _mesa_glsl_error(loc, state,
2769 "uniform block layout qualifiers row_major and "
2770 "column_major may not be applied to variables "
2771 "outside of uniform blocks");
2772 } else if (!type->without_array()->is_matrix()) {
2773 /* The OpenGL ES 3.0 conformance tests did not originally allow
2774 * matrix layout qualifiers on non-matrices. However, the OpenGL
2775 * 4.4 and OpenGL ES 3.0 (revision TBD) specifications were
2776 * amended to specifically allow these layouts on all types. Emit
2777 * a warning so that people know their code may not be portable.
2778 */
2779 _mesa_glsl_warning(loc, state,
2780 "uniform block layout qualifiers row_major and "
2781 "column_major applied to non-matrix types may "
2782 "be rejected by older compilers");
2783 }
2784}
2785
2786static bool
2787validate_xfb_buffer_qualifier(YYLTYPE *loc,
2788 struct _mesa_glsl_parse_state *state,
2789 unsigned xfb_buffer) {
2790 if (xfb_buffer >= state->Const.MaxTransformFeedbackBuffers) {
2791 _mesa_glsl_error(loc, state,
2792 "invalid xfb_buffer specified %d is larger than "
2793 "MAX_TRANSFORM_FEEDBACK_BUFFERS - 1 (%d).",
2794 xfb_buffer,
2795 state->Const.MaxTransformFeedbackBuffers - 1);
2796 return false;
2797 }
2798
2799 return true;
2800}
2801
2802/* From the ARB_enhanced_layouts spec:
2803 *
2804 * "Variables and block members qualified with *xfb_offset* can be
2805 * scalars, vectors, matrices, structures, and (sized) arrays of these.
2806 * The offset must be a multiple of the size of the first component of
2807 * the first qualified variable or block member, or a compile-time error
2808 * results. Further, if applied to an aggregate containing a double,
2809 * the offset must also be a multiple of 8, and the space taken in the
2810 * buffer will be a multiple of 8.
2811 */
2812static bool
2813validate_xfb_offset_qualifier(YYLTYPE *loc,
2814 struct _mesa_glsl_parse_state *state,
2815 int xfb_offset, const glsl_type *type,
2816 unsigned component_size) {
2817 const glsl_type *t_without_array = type->without_array();
2818
2819 if (xfb_offset != -1 && type->is_unsized_array()) {
2820 _mesa_glsl_error(loc, state,
2821 "xfb_offset can't be used with unsized arrays.");
2822 return false;
2823 }
2824
2825 /* Make sure nested structs don't contain unsized arrays, and validate
2826 * any xfb_offsets on interface members.
2827 */
2828 if (t_without_array->is_struct() || t_without_array->is_interface())
2829 for (unsigned int i = 0; i < t_without_array->length; i++) {
2830 const glsl_type *member_t = t_without_array->fields.structure[i].type;
2831
2832 /* When the interface block doesn't have an xfb_offset qualifier then
2833 * we apply the component size rules at the member level.
2834 */
2835 if (xfb_offset == -1)
2836 component_size = member_t->contains_double() ? 8 : 4;
2837
2838 int xfb_offset = t_without_array->fields.structure[i].offset;
2839 validate_xfb_offset_qualifier(loc, state, xfb_offset, member_t,
2840 component_size);
2841 }
2842
2843 /* Nested structs or interface block without offset may not have had an
2844 * offset applied yet so return.
2845 */
2846 if (xfb_offset == -1) {
2847 return true;
2848 }
2849
2850 if (xfb_offset % component_size) {
2851 _mesa_glsl_error(loc, state,
2852 "invalid qualifier xfb_offset=%d must be a multiple "
2853 "of the first component size of the first qualified "
2854 "variable or block member. Or double if an aggregate "
2855 "that contains a double (%d).",
2856 xfb_offset, component_size);
2857 return false;
2858 }
2859
2860 return true;
2861}
2862
2863static bool
2864validate_stream_qualifier(YYLTYPE *loc, struct _mesa_glsl_parse_state *state,
2865 unsigned stream)
2866{
2867 if (stream >= state->ctx->Const.MaxVertexStreams) {
2868 _mesa_glsl_error(loc, state,
2869 "invalid stream specified %d is larger than "
2870 "MAX_VERTEX_STREAMS - 1 (%d).",
2871 stream, state->ctx->Const.MaxVertexStreams - 1);
2872 return false;
2873 }
2874
2875 return true;
2876}
2877
2878static void
2879apply_explicit_binding(struct _mesa_glsl_parse_state *state,
2880 YYLTYPE *loc,
2881 ir_variable *var,
2882 const glsl_type *type,
2883 const ast_type_qualifier *qual)
2884{
2885 if (!qual->flags.q.uniform && !qual->flags.q.buffer) {
2886 _mesa_glsl_error(loc, state,
2887 "the \"binding\" qualifier only applies to uniforms and "
2888 "shader storage buffer objects");
2889 return;
2890 }
2891
2892 unsigned qual_binding;
2893 if (!process_qualifier_constant(state, loc, "binding", qual->binding,
2894 &qual_binding)) {
2895 return;
2896 }
2897
2898 const struct gl_context *const ctx = state->ctx;
2899 unsigned elements = type->is_array() ? type->arrays_of_arrays_size() : 1;
2900 unsigned max_index = qual_binding + elements - 1;
2901 const glsl_type *base_type = type->without_array();
2902
2903 if (base_type->is_interface()) {
2904 /* UBOs. From page 60 of the GLSL 4.20 specification:
2905 * "If the binding point for any uniform block instance is less than zero,
2906 * or greater than or equal to the implementation-dependent maximum
2907 * number of uniform buffer bindings, a compilation error will occur.
2908 * When the binding identifier is used with a uniform block instanced as
2909 * an array of size N, all elements of the array from binding through
2910 * binding + N – 1 must be within this range."
2911 *
2912 * The implementation-dependent maximum is GL_MAX_UNIFORM_BUFFER_BINDINGS.
2913 */
2914 if (qual->flags.q.uniform &&
2915 max_index >= ctx->Const.MaxUniformBufferBindings) {
2916 _mesa_glsl_error(loc, state, "layout(binding = %u) for %d UBOs exceeds "
2917 "the maximum number of UBO binding points (%d)",
2918 qual_binding, elements,
2919 ctx->Const.MaxUniformBufferBindings);
2920 return;
2921 }
2922
2923 /* SSBOs. From page 67 of the GLSL 4.30 specification:
2924 * "If the binding point for any uniform or shader storage block instance
2925 * is less than zero, or greater than or equal to the
2926 * implementation-dependent maximum number of uniform buffer bindings, a
2927 * compile-time error will occur. When the binding identifier is used
2928 * with a uniform or shader storage block instanced as an array of size
2929 * N, all elements of the array from binding through binding + N – 1 must
2930 * be within this range."
2931 */
2932 if (qual->flags.q.buffer &&
2933 max_index >= ctx->Const.MaxShaderStorageBufferBindings) {
2934 _mesa_glsl_error(loc, state, "layout(binding = %u) for %d SSBOs exceeds "
2935 "the maximum number of SSBO binding points (%d)",
2936 qual_binding, elements,
2937 ctx->Const.MaxShaderStorageBufferBindings);
2938 return;
2939 }
2940 } else if (base_type->is_sampler()) {
2941 /* Samplers. From page 63 of the GLSL 4.20 specification:
2942 * "If the binding is less than zero, or greater than or equal to the
2943 * implementation-dependent maximum supported number of units, a
2944 * compilation error will occur. When the binding identifier is used
2945 * with an array of size N, all elements of the array from binding
2946 * through binding + N - 1 must be within this range."
2947 */
2948 unsigned limit = ctx->Const.MaxCombinedTextureImageUnits;
2949
2950 if (max_index >= limit) {
2951 _mesa_glsl_error(loc, state, "layout(binding = %d) for %d samplers "
2952 "exceeds the maximum number of texture image units "
2953 "(%u)", qual_binding, elements, limit);
2954
2955 return;
2956 }
2957 } else if (base_type->contains_atomic()) {
2958 assert(ctx->Const.MaxAtomicBufferBindings <= MAX_COMBINED_ATOMIC_BUFFERS)(static_cast <bool> (ctx->Const.MaxAtomicBufferBindings
<= (15 * 6)) ? void (0) : __assert_fail ("ctx->Const.MaxAtomicBufferBindings <= MAX_COMBINED_ATOMIC_BUFFERS"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2959 if (qual_binding >= ctx->Const.MaxAtomicBufferBindings) {
2960 _mesa_glsl_error(loc, state, "layout(binding = %d) exceeds the "
2961 "maximum number of atomic counter buffer bindings "
2962 "(%u)", qual_binding,
2963 ctx->Const.MaxAtomicBufferBindings);
2964
2965 return;
2966 }
2967 } else if ((state->is_version(420, 310) ||
2968 state->ARB_shading_language_420pack_enable) &&
2969 base_type->is_image()) {
2970 assert(ctx->Const.MaxImageUnits <= MAX_IMAGE_UNITS)(static_cast <bool> (ctx->Const.MaxImageUnits <= (
32 * 6)) ? void (0) : __assert_fail ("ctx->Const.MaxImageUnits <= MAX_IMAGE_UNITS"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
2971 if (max_index >= ctx->Const.MaxImageUnits) {
2972 _mesa_glsl_error(loc, state, "Image binding %d exceeds the "
2973 "maximum number of image units (%d)", max_index,
2974 ctx->Const.MaxImageUnits);
2975 return;
2976 }
2977
2978 } else {
2979 _mesa_glsl_error(loc, state,
2980 "the \"binding\" qualifier only applies to uniform "
2981 "blocks, storage blocks, opaque variables, or arrays "
2982 "thereof");
2983 return;
2984 }
2985
2986 var->data.explicit_binding = true;
2987 var->data.binding = qual_binding;
2988
2989 return;
2990}
2991
2992static void
2993validate_fragment_flat_interpolation_input(struct _mesa_glsl_parse_state *state,
2994 YYLTYPE *loc,
2995 const glsl_interp_mode interpolation,
2996 const struct glsl_type *var_type,
2997 ir_variable_mode mode)
2998{
2999 if (state->stage != MESA_SHADER_FRAGMENT ||
3000 interpolation == INTERP_MODE_FLAT ||
3001 mode != ir_var_shader_in)
3002 return;
3003
3004 /* Integer fragment inputs must be qualified with 'flat'. In GLSL ES,
3005 * so must integer vertex outputs.
3006 *
3007 * From section 4.3.4 ("Inputs") of the GLSL 1.50 spec:
3008 * "Fragment shader inputs that are signed or unsigned integers or
3009 * integer vectors must be qualified with the interpolation qualifier
3010 * flat."
3011 *
3012 * From section 4.3.4 ("Input Variables") of the GLSL 3.00 ES spec:
3013 * "Fragment shader inputs that are, or contain, signed or unsigned
3014 * integers or integer vectors must be qualified with the
3015 * interpolation qualifier flat."
3016 *
3017 * From section 4.3.6 ("Output Variables") of the GLSL 3.00 ES spec:
3018 * "Vertex shader outputs that are, or contain, signed or unsigned
3019 * integers or integer vectors must be qualified with the
3020 * interpolation qualifier flat."
3021 *
3022 * Note that prior to GLSL 1.50, this requirement applied to vertex
3023 * outputs rather than fragment inputs. That creates problems in the
3024 * presence of geometry shaders, so we adopt the GLSL 1.50 rule for all
3025 * desktop GL shaders. For GLSL ES shaders, we follow the spec and
3026 * apply the restriction to both vertex outputs and fragment inputs.
3027 *
3028 * Note also that the desktop GLSL specs are missing the text "or
3029 * contain"; this is presumably an oversight, since there is no
3030 * reasonable way to interpolate a fragment shader input that contains
3031 * an integer. See Khronos bug #15671.
3032 */
3033 if ((state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
3034 && var_type->contains_integer()) {
3035 _mesa_glsl_error(loc, state, "if a fragment input is (or contains) "
3036 "an integer, then it must be qualified with 'flat'");
3037 }
3038
3039 /* Double fragment inputs must be qualified with 'flat'.
3040 *
3041 * From the "Overview" of the ARB_gpu_shader_fp64 extension spec:
3042 * "This extension does not support interpolation of double-precision
3043 * values; doubles used as fragment shader inputs must be qualified as
3044 * "flat"."
3045 *
3046 * From section 4.3.4 ("Inputs") of the GLSL 4.00 spec:
3047 * "Fragment shader inputs that are signed or unsigned integers, integer
3048 * vectors, or any double-precision floating-point type must be
3049 * qualified with the interpolation qualifier flat."
3050 *
3051 * Note that the GLSL specs are missing the text "or contain"; this is
3052 * presumably an oversight. See Khronos bug #15671.
3053 *
3054 * The 'double' type does not exist in GLSL ES so far.
3055 */
3056 if (state->has_double()
3057 && var_type->contains_double()) {
3058 _mesa_glsl_error(loc, state, "if a fragment input is (or contains) "
3059 "a double, then it must be qualified with 'flat'");
3060 }
3061
3062 /* Bindless sampler/image fragment inputs must be qualified with 'flat'.
3063 *
3064 * From section 4.3.4 of the ARB_bindless_texture spec:
3065 *
3066 * "(modify last paragraph, p. 35, allowing samplers and images as
3067 * fragment shader inputs) ... Fragment inputs can only be signed and
3068 * unsigned integers and integer vectors, floating point scalars,
3069 * floating-point vectors, matrices, sampler and image types, or arrays
3070 * or structures of these. Fragment shader inputs that are signed or
3071 * unsigned integers, integer vectors, or any double-precision floating-
3072 * point type, or any sampler or image type must be qualified with the
3073 * interpolation qualifier "flat"."
3074 */
3075 if (state->has_bindless()
3076 && (var_type->contains_sampler() || var_type->contains_image())) {
3077 _mesa_glsl_error(loc, state, "if a fragment input is (or contains) "
3078 "a bindless sampler (or image), then it must be "
3079 "qualified with 'flat'");
3080 }
3081}
3082
3083static void
3084validate_interpolation_qualifier(struct _mesa_glsl_parse_state *state,
3085 YYLTYPE *loc,
3086 const glsl_interp_mode interpolation,
3087 const struct ast_type_qualifier *qual,
3088 const struct glsl_type *var_type,
3089 ir_variable_mode mode)
3090{
3091 /* Interpolation qualifiers can only apply to shader inputs or outputs, but
3092 * not to vertex shader inputs nor fragment shader outputs.
3093 *
3094 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3095 * "Outputs from a vertex shader (out) and inputs to a fragment
3096 * shader (in) can be further qualified with one or more of these
3097 * interpolation qualifiers"
3098 * ...
3099 * "These interpolation qualifiers may only precede the qualifiers in,
3100 * centroid in, out, or centroid out in a declaration. They do not apply
3101 * to the deprecated storage qualifiers varying or centroid
3102 * varying. They also do not apply to inputs into a vertex shader or
3103 * outputs from a fragment shader."
3104 *
3105 * From section 4.3 ("Storage Qualifiers") of the GLSL ES 3.00 spec:
3106 * "Outputs from a shader (out) and inputs to a shader (in) can be
3107 * further qualified with one of these interpolation qualifiers."
3108 * ...
3109 * "These interpolation qualifiers may only precede the qualifiers
3110 * in, centroid in, out, or centroid out in a declaration. They do
3111 * not apply to inputs into a vertex shader or outputs from a
3112 * fragment shader."
3113 */
3114 if ((state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
3115 && interpolation != INTERP_MODE_NONE) {
3116 const char *i = interpolation_string(interpolation);
3117 if (mode != ir_var_shader_in && mode != ir_var_shader_out)
3118 _mesa_glsl_error(loc, state,
3119 "interpolation qualifier `%s' can only be applied to "
3120 "shader inputs or outputs.", i);
3121
3122 switch (state->stage) {
3123 case MESA_SHADER_VERTEX:
3124 if (mode == ir_var_shader_in) {
3125 _mesa_glsl_error(loc, state,
3126 "interpolation qualifier '%s' cannot be applied to "
3127 "vertex shader inputs", i);
3128 }
3129 break;
3130 case MESA_SHADER_FRAGMENT:
3131 if (mode == ir_var_shader_out) {
3132 _mesa_glsl_error(loc, state,
3133 "interpolation qualifier '%s' cannot be applied to "
3134 "fragment shader outputs", i);
3135 }
3136 break;
3137 default:
3138 break;
3139 }
3140 }
3141
3142 /* Interpolation qualifiers cannot be applied to 'centroid' and
3143 * 'centroid varying'.
3144 *
3145 * From section 4.3 ("Storage Qualifiers") of the GLSL 1.30 spec:
3146 * "interpolation qualifiers may only precede the qualifiers in,
3147 * centroid in, out, or centroid out in a declaration. They do not apply
3148 * to the deprecated storage qualifiers varying or centroid varying."
3149 *
3150 * These deprecated storage qualifiers do not exist in GLSL ES 3.00.
3151 *
3152 * GL_EXT_gpu_shader4 allows this.
3153 */
3154 if (state->is_version(130, 0) && !state->EXT_gpu_shader4_enable
3155 && interpolation != INTERP_MODE_NONE
3156 && qual->flags.q.varying) {
3157
3158 const char *i = interpolation_string(interpolation);
3159 const char *s;
3160 if (qual->flags.q.centroid)
3161 s = "centroid varying";
3162 else
3163 s = "varying";
3164
3165 _mesa_glsl_error(loc, state,
3166 "qualifier '%s' cannot be applied to the "
3167 "deprecated storage qualifier '%s'", i, s);
3168 }
3169
3170 validate_fragment_flat_interpolation_input(state, loc, interpolation,
3171 var_type, mode);
3172}
3173
3174static glsl_interp_mode
3175interpret_interpolation_qualifier(const struct ast_type_qualifier *qual,
3176 const struct glsl_type *var_type,
3177 ir_variable_mode mode,
3178 struct _mesa_glsl_parse_state *state,
3179 YYLTYPE *loc)
3180{
3181 glsl_interp_mode interpolation;
3182 if (qual->flags.q.flat)
3183 interpolation = INTERP_MODE_FLAT;
3184 else if (qual->flags.q.noperspective)
3185 interpolation = INTERP_MODE_NOPERSPECTIVE;
3186 else if (qual->flags.q.smooth)
3187 interpolation = INTERP_MODE_SMOOTH;
3188 else
3189 interpolation = INTERP_MODE_NONE;
3190
3191 validate_interpolation_qualifier(state, loc,
3192 interpolation,
3193 qual, var_type, mode);
3194
3195 return interpolation;
3196}
3197
3198
3199static void
3200apply_explicit_location(const struct ast_type_qualifier *qual,
3201 ir_variable *var,
3202 struct _mesa_glsl_parse_state *state,
3203 YYLTYPE *loc)
3204{
3205 bool fail = false;
3206
3207 unsigned qual_location;
3208 if (!process_qualifier_constant(state, loc, "location", qual->location,
3209 &qual_location)) {
3210 return;
3211 }
3212
3213 /* Checks for GL_ARB_explicit_uniform_location. */
3214 if (qual->flags.q.uniform) {
3215 if (!state->check_explicit_uniform_location_allowed(loc, var))
3216 return;
3217
3218 const struct gl_context *const ctx = state->ctx;
3219 unsigned max_loc = qual_location + var->type->uniform_locations() - 1;
3220
3221 if (max_loc >= ctx->Const.MaxUserAssignableUniformLocations) {
3222 _mesa_glsl_error(loc, state, "location(s) consumed by uniform %s "
3223 ">= MAX_UNIFORM_LOCATIONS (%u)", var->name,
3224 ctx->Const.MaxUserAssignableUniformLocations);
3225 return;
3226 }
3227
3228 var->data.explicit_location = true;
3229 var->data.location = qual_location;
3230 return;
3231 }
3232
3233 /* Between GL_ARB_explicit_attrib_location an
3234 * GL_ARB_separate_shader_objects, the inputs and outputs of any shader
3235 * stage can be assigned explicit locations. The checking here associates
3236 * the correct extension with the correct stage's input / output:
3237 *
3238 * input output
3239 * ----- ------
3240 * vertex explicit_loc sso
3241 * tess control sso sso
3242 * tess eval sso sso
3243 * geometry sso sso
3244 * fragment sso explicit_loc
3245 */
3246 switch (state->stage) {
3247 case MESA_SHADER_VERTEX:
3248 if (var->data.mode == ir_var_shader_in) {
3249 if (!state->check_explicit_attrib_location_allowed(loc, var))
3250 return;
3251
3252 break;
3253 }
3254
3255 if (var->data.mode == ir_var_shader_out) {
3256 if (!state->check_separate_shader_objects_allowed(loc, var))
3257 return;
3258
3259 break;
3260 }
3261
3262 fail = true;
3263 break;
3264
3265 case MESA_SHADER_TESS_CTRL:
3266 case MESA_SHADER_TESS_EVAL:
3267 case MESA_SHADER_GEOMETRY:
3268 if (var->data.mode == ir_var_shader_in || var->data.mode == ir_var_shader_out) {
3269 if (!state->check_separate_shader_objects_allowed(loc, var))
3270 return;
3271
3272 break;
3273 }
3274
3275 fail = true;
3276 break;
3277
3278 case MESA_SHADER_FRAGMENT:
3279 if (var->data.mode == ir_var_shader_in) {
3280 if (!state->check_separate_shader_objects_allowed(loc, var))
3281 return;
3282
3283 break;
3284 }
3285
3286 if (var->data.mode == ir_var_shader_out) {
3287 if (!state->check_explicit_attrib_location_allowed(loc, var))
3288 return;
3289
3290 break;
3291 }
3292
3293 fail = true;
3294 break;
3295
3296 case MESA_SHADER_COMPUTE:
3297 _mesa_glsl_error(loc, state,
3298 "compute shader variables cannot be given "
3299 "explicit locations");
3300 return;
3301 default:
3302 fail = true;
3303 break;
3304 };
3305
3306 if (fail) {
3307 _mesa_glsl_error(loc, state,
3308 "%s cannot be given an explicit location in %s shader",
3309 mode_string(var),
3310 _mesa_shader_stage_to_string(state->stage));
3311 } else {
3312 var->data.explicit_location = true;
3313
3314 switch (state->stage) {
3315 case MESA_SHADER_VERTEX:
3316 var->data.location = (var->data.mode == ir_var_shader_in)
3317 ? (qual_location + VERT_ATTRIB_GENERIC0)
3318 : (qual_location + VARYING_SLOT_VAR0);
3319 break;
3320
3321 case MESA_SHADER_TESS_CTRL:
3322 case MESA_SHADER_TESS_EVAL:
3323 case MESA_SHADER_GEOMETRY:
3324 if (var->data.patch)
3325 var->data.location = qual_location + VARYING_SLOT_PATCH0((VARYING_SLOT_VAR0 + 32));
3326 else
3327 var->data.location = qual_location + VARYING_SLOT_VAR0;
3328 break;
3329
3330 case MESA_SHADER_FRAGMENT:
3331 var->data.location = (var->data.mode == ir_var_shader_out)
3332 ? (qual_location + FRAG_RESULT_DATA0)
3333 : (qual_location + VARYING_SLOT_VAR0);
3334 break;
3335 default:
3336 assert(!"Unexpected shader type")(static_cast <bool> (!"Unexpected shader type") ? void (
0) : __assert_fail ("!\"Unexpected shader type\"", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
3337 break;
3338 }
3339
3340 /* Check if index was set for the uniform instead of the function */
3341 if (qual->flags.q.explicit_index && qual->is_subroutine_decl()) {
3342 _mesa_glsl_error(loc, state, "an index qualifier can only be "
3343 "used with subroutine functions");
3344 return;
3345 }
3346
3347 unsigned qual_index;
3348 if (qual->flags.q.explicit_index &&
3349 process_qualifier_constant(state, loc, "index", qual->index,
3350 &qual_index)) {
3351 /* From the GLSL 4.30 specification, section 4.4.2 (Output
3352 * Layout Qualifiers):
3353 *
3354 * "It is also a compile-time error if a fragment shader
3355 * sets a layout index to less than 0 or greater than 1."
3356 *
3357 * Older specifications don't mandate a behavior; we take
3358 * this as a clarification and always generate the error.
3359 */
3360 if (qual_index > 1) {
3361 _mesa_glsl_error(loc, state,
3362 "explicit index may only be 0 or 1");
3363 } else {
3364 var->data.explicit_index = true;
3365 var->data.index = qual_index;
3366 }
3367 }
3368 }
3369}
3370
3371static bool
3372validate_storage_for_sampler_image_types(ir_variable *var,
3373 struct _mesa_glsl_parse_state *state,
3374 YYLTYPE *loc)
3375{
3376 /* From section 4.1.7 of the GLSL 4.40 spec:
3377 *
3378 * "[Opaque types] can only be declared as function
3379 * parameters or uniform-qualified variables."
3380 *
3381 * From section 4.1.7 of the ARB_bindless_texture spec:
3382 *
3383 * "Samplers may be declared as shader inputs and outputs, as uniform
3384 * variables, as temporary variables, and as function parameters."
3385 *
3386 * From section 4.1.X of the ARB_bindless_texture spec:
3387 *
3388 * "Images may be declared as shader inputs and outputs, as uniform
3389 * variables, as temporary variables, and as function parameters."
3390 */
3391 if (state->has_bindless()) {
3392 if (var->data.mode != ir_var_auto &&
3393 var->data.mode != ir_var_uniform &&
3394 var->data.mode != ir_var_shader_in &&
3395 var->data.mode != ir_var_shader_out &&
3396 var->data.mode != ir_var_function_in &&
3397 var->data.mode != ir_var_function_out &&
3398 var->data.mode != ir_var_function_inout) {
3399 _mesa_glsl_error(loc, state, "bindless image/sampler variables may "
3400 "only be declared as shader inputs and outputs, as "
3401 "uniform variables, as temporary variables and as "
3402 "function parameters");
3403 return false;
3404 }
3405 } else {
3406 if (var->data.mode != ir_var_uniform &&
3407 var->data.mode != ir_var_function_in) {
3408 _mesa_glsl_error(loc, state, "image/sampler variables may only be "
3409 "declared as function parameters or "
3410 "uniform-qualified global variables");
3411 return false;
3412 }
3413 }
3414 return true;
3415}
3416
3417static bool
3418validate_memory_qualifier_for_type(struct _mesa_glsl_parse_state *state,
3419 YYLTYPE *loc,
3420 const struct ast_type_qualifier *qual,
3421 const glsl_type *type)
3422{
3423 /* From Section 4.10 (Memory Qualifiers) of the GLSL 4.50 spec:
3424 *
3425 * "Memory qualifiers are only supported in the declarations of image
3426 * variables, buffer variables, and shader storage blocks; it is an error
3427 * to use such qualifiers in any other declarations.
3428 */
3429 if (!type->is_image() && !qual->flags.q.buffer) {
3430 if (qual->flags.q.read_only ||
3431 qual->flags.q.write_only ||
3432 qual->flags.q.coherent ||
3433 qual->flags.q._volatile ||
3434 qual->flags.q.restrict_flag) {
3435 _mesa_glsl_error(loc, state, "memory qualifiers may only be applied "
3436 "in the declarations of image variables, buffer "
3437 "variables, and shader storage blocks");
3438 return false;
3439 }
3440 }
3441 return true;
3442}
3443
3444static bool
3445validate_image_format_qualifier_for_type(struct _mesa_glsl_parse_state *state,
3446 YYLTYPE *loc,
3447 const struct ast_type_qualifier *qual,
3448 const glsl_type *type)
3449{
3450 /* From section 4.4.6.2 (Format Layout Qualifiers) of the GLSL 4.50 spec:
3451 *
3452 * "Format layout qualifiers can be used on image variable declarations
3453 * (those declared with a basic type having “image ” in its keyword)."
3454 */
3455 if (!type->is_image() && qual->flags.q.explicit_image_format) {
3456 _mesa_glsl_error(loc, state, "format layout qualifiers may only be "
3457 "applied to images");
3458 return false;
3459 }
3460 return true;
3461}
3462
3463static void
3464apply_image_qualifier_to_variable(const struct ast_type_qualifier *qual,
3465 ir_variable *var,
3466 struct _mesa_glsl_parse_state *state,
3467 YYLTYPE *loc)
3468{
3469 const glsl_type *base_type = var->type->without_array();
3470
3471 if (!validate_image_format_qualifier_for_type(state, loc, qual, base_type) ||
3472 !validate_memory_qualifier_for_type(state, loc, qual, base_type))
3473 return;
3474
3475 if (!base_type->is_image())
3476 return;
3477
3478 if (!validate_storage_for_sampler_image_types(var, state, loc))
3479 return;
3480
3481 var->data.memory_read_only |= qual->flags.q.read_only;
3482 var->data.memory_write_only |= qual->flags.q.write_only;
3483 var->data.memory_coherent |= qual->flags.q.coherent;
3484 var->data.memory_volatile |= qual->flags.q._volatile;
3485 var->data.memory_restrict |= qual->flags.q.restrict_flag;
3486
3487 if (qual->flags.q.explicit_image_format) {
3488 if (var->data.mode == ir_var_function_in) {
3489 _mesa_glsl_error(loc, state, "format qualifiers cannot be used on "
3490 "image function parameters");
3491 }
3492
3493 if (qual->image_base_type != base_type->sampled_type) {
3494 _mesa_glsl_error(loc, state, "format qualifier doesn't match the base "
3495 "data type of the image");
3496 }
3497
3498 var->data.image_format = qual->image_format;
3499 } else if (state->has_image_load_formatted()) {
3500 if (var->data.mode == ir_var_uniform &&
3501 state->EXT_shader_image_load_formatted_warn) {
3502 _mesa_glsl_warning(loc, state, "GL_EXT_image_load_formatted used");
3503 }
3504 } else {
3505 if (var->data.mode == ir_var_uniform) {
3506 if (state->es_shader ||
3507 !(state->is_version(420, 310) || state->ARB_shader_image_load_store_enable)) {
3508 _mesa_glsl_error(loc, state, "all image uniforms must have a "
3509 "format layout qualifier");
3510 } else if (!qual->flags.q.write_only) {
3511 _mesa_glsl_error(loc, state, "image uniforms not qualified with "
3512 "`writeonly' must have a format layout qualifier");
3513 }
3514 }
3515 var->data.image_format = PIPE_FORMAT_NONE;
3516 }
3517
3518 /* From page 70 of the GLSL ES 3.1 specification:
3519 *
3520 * "Except for image variables qualified with the format qualifiers r32f,
3521 * r32i, and r32ui, image variables must specify either memory qualifier
3522 * readonly or the memory qualifier writeonly."
3523 */
3524 if (state->es_shader &&
3525 var->data.image_format != PIPE_FORMAT_R32_FLOAT &&
3526 var->data.image_format != PIPE_FORMAT_R32_SINT &&
3527 var->data.image_format != PIPE_FORMAT_R32_UINT &&
3528 !var->data.memory_read_only &&
3529 !var->data.memory_write_only) {
3530 _mesa_glsl_error(loc, state, "image variables of format other than r32f, "
3531 "r32i or r32ui must be qualified `readonly' or "
3532 "`writeonly'");
3533 }
3534}
3535
3536static inline const char*
3537get_layout_qualifier_string(bool origin_upper_left, bool pixel_center_integer)
3538{
3539 if (origin_upper_left && pixel_center_integer)
3540 return "origin_upper_left, pixel_center_integer";
3541 else if (origin_upper_left)
3542 return "origin_upper_left";
3543 else if (pixel_center_integer)
3544 return "pixel_center_integer";
3545 else
3546 return " ";
3547}
3548
3549static inline bool
3550is_conflicting_fragcoord_redeclaration(struct _mesa_glsl_parse_state *state,
3551 const struct ast_type_qualifier *qual)
3552{
3553 /* If gl_FragCoord was previously declared, and the qualifiers were
3554 * different in any way, return true.
3555 */
3556 if (state->fs_redeclares_gl_fragcoord) {
3557 return (state->fs_pixel_center_integer != qual->flags.q.pixel_center_integer
3558 || state->fs_origin_upper_left != qual->flags.q.origin_upper_left);
3559 }
3560
3561 return false;
3562}
3563
3564static inline bool
3565is_conflicting_layer_redeclaration(struct _mesa_glsl_parse_state *state,
3566 const struct ast_type_qualifier *qual)
3567{
3568 if (state->redeclares_gl_layer) {
3569 return state->layer_viewport_relative != qual->flags.q.viewport_relative;
3570 }
3571 return false;
3572}
3573
3574static inline void
3575validate_array_dimensions(const glsl_type *t,
3576 struct _mesa_glsl_parse_state *state,
3577 YYLTYPE *loc) {
3578 if (t->is_array()) {
3579 t = t->fields.array;
3580 while (t->is_array()) {
3581 if (t->is_unsized_array()) {
3582 _mesa_glsl_error(loc, state,
3583 "only the outermost array dimension can "
3584 "be unsized",
3585 t->name);
3586 break;
3587 }
3588 t = t->fields.array;
3589 }
3590 }
3591}
3592
3593static void
3594apply_bindless_qualifier_to_variable(const struct ast_type_qualifier *qual,
3595 ir_variable *var,
3596 struct _mesa_glsl_parse_state *state,
3597 YYLTYPE *loc)
3598{
3599 bool has_local_qualifiers = qual->flags.q.bindless_sampler ||
3600 qual->flags.q.bindless_image ||
3601 qual->flags.q.bound_sampler ||
3602 qual->flags.q.bound_image;
3603
3604 /* The ARB_bindless_texture spec says:
3605 *
3606 * "Modify Section 4.4.6 Opaque-Uniform Layout Qualifiers of the GLSL 4.30
3607 * spec"
3608 *
3609 * "If these layout qualifiers are applied to other types of default block
3610 * uniforms, or variables with non-uniform storage, a compile-time error
3611 * will be generated."
3612 */
3613 if (has_local_qualifiers && !qual->flags.q.uniform) {
3614 _mesa_glsl_error(loc, state, "ARB_bindless_texture layout qualifiers "
3615 "can only be applied to default block uniforms or "
3616 "variables with uniform storage");
3617 return;
3618 }
3619
3620 /* The ARB_bindless_texture spec doesn't state anything in this situation,
3621 * but it makes sense to only allow bindless_sampler/bound_sampler for
3622 * sampler types, and respectively bindless_image/bound_image for image
3623 * types.
3624 */
3625 if ((qual->flags.q.bindless_sampler || qual->flags.q.bound_sampler) &&
3626 !var->type->contains_sampler()) {
3627 _mesa_glsl_error(loc, state, "bindless_sampler or bound_sampler can only "
3628 "be applied to sampler types");
3629 return;
3630 }
3631
3632 if ((qual->flags.q.bindless_image || qual->flags.q.bound_image) &&
3633 !var->type->contains_image()) {
3634 _mesa_glsl_error(loc, state, "bindless_image or bound_image can only be "
3635 "applied to image types");
3636 return;
3637 }
3638
3639 /* The bindless_sampler/bindless_image (and respectively
3640 * bound_sampler/bound_image) layout qualifiers can be set at global and at
3641 * local scope.
3642 */
3643 if (var->type->contains_sampler() || var->type->contains_image()) {
3644 var->data.bindless = qual->flags.q.bindless_sampler ||
3645 qual->flags.q.bindless_image ||
3646 state->bindless_sampler_specified ||
3647 state->bindless_image_specified;
3648
3649 var->data.bound = qual->flags.q.bound_sampler ||
3650 qual->flags.q.bound_image ||
3651 state->bound_sampler_specified ||
3652 state->bound_image_specified;
3653 }
3654}
3655
3656static void
3657apply_layout_qualifier_to_variable(const struct ast_type_qualifier *qual,
3658 ir_variable *var,
3659 struct _mesa_glsl_parse_state *state,
3660 YYLTYPE *loc)
3661{
3662 if (var->name != NULL__null && strcmp(var->name, "gl_FragCoord") == 0) {
3663
3664 /* Section 4.3.8.1, page 39 of GLSL 1.50 spec says:
3665 *
3666 * "Within any shader, the first redeclarations of gl_FragCoord
3667 * must appear before any use of gl_FragCoord."
3668 *
3669 * Generate a compiler error if above condition is not met by the
3670 * fragment shader.
3671 */
3672 ir_variable *earlier = state->symbols->get_variable("gl_FragCoord");
3673 if (earlier != NULL__null &&
3674 earlier->data.used &&
3675 !state->fs_redeclares_gl_fragcoord) {
3676 _mesa_glsl_error(loc, state,
3677 "gl_FragCoord used before its first redeclaration "
3678 "in fragment shader");
3679 }
3680
3681 /* Make sure all gl_FragCoord redeclarations specify the same layout
3682 * qualifiers.
3683 */
3684 if (is_conflicting_fragcoord_redeclaration(state, qual)) {
3685 const char *const qual_string =
3686 get_layout_qualifier_string(qual->flags.q.origin_upper_left,
3687 qual->flags.q.pixel_center_integer);
3688
3689 const char *const state_string =
3690 get_layout_qualifier_string(state->fs_origin_upper_left,
3691 state->fs_pixel_center_integer);
3692
3693 _mesa_glsl_error(loc, state,
3694 "gl_FragCoord redeclared with different layout "
3695 "qualifiers (%s) and (%s) ",
3696 state_string,
3697 qual_string);
3698 }
3699 state->fs_origin_upper_left = qual->flags.q.origin_upper_left;
3700 state->fs_pixel_center_integer = qual->flags.q.pixel_center_integer;
3701 state->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers =
3702 !qual->flags.q.origin_upper_left && !qual->flags.q.pixel_center_integer;
3703 state->fs_redeclares_gl_fragcoord =
3704 state->fs_origin_upper_left ||
3705 state->fs_pixel_center_integer ||
3706 state->fs_redeclares_gl_fragcoord_with_no_layout_qualifiers;
3707 }
3708
3709 if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
3710 && (strcmp(var->name, "gl_FragCoord") != 0)) {
3711 const char *const qual_string = (qual->flags.q.origin_upper_left)
3712 ? "origin_upper_left" : "pixel_center_integer";
3713
3714 _mesa_glsl_error(loc, state,
3715 "layout qualifier `%s' can only be applied to "
3716 "fragment shader input `gl_FragCoord'",
3717 qual_string);
3718 }
3719
3720 if (qual->flags.q.explicit_location) {
3721 apply_explicit_location(qual, var, state, loc);
3722
3723 if (qual->flags.q.explicit_component) {
3724 unsigned qual_component;
3725 if (process_qualifier_constant(state, loc, "component",
3726 qual->component, &qual_component)) {
3727 const glsl_type *type = var->type->without_array();
3728 unsigned components = type->component_slots();
3729
3730 if (type->is_matrix() || type->is_struct()) {
3731 _mesa_glsl_error(loc, state, "component layout qualifier "
3732 "cannot be applied to a matrix, a structure, "
3733 "a block, or an array containing any of "
3734 "these.");
3735 } else if (components > 4 && type->is_64bit()) {
3736 _mesa_glsl_error(loc, state, "component layout qualifier "
3737 "cannot be applied to dvec%u.",
3738 components / 2);
3739 } else if (qual_component != 0 &&
3740 (qual_component + components - 1) > 3) {
3741 _mesa_glsl_error(loc, state, "component overflow (%u > 3)",
3742 (qual_component + components - 1));
3743 } else if (qual_component == 1 && type->is_64bit()) {
3744 /* We don't bother checking for 3 as it should be caught by the
3745 * overflow check above.
3746 */
3747 _mesa_glsl_error(loc, state, "doubles cannot begin at "
3748 "component 1 or 3");
3749 } else {
3750 var->data.explicit_component = true;
3751 var->data.location_frac = qual_component;
3752 }
3753 }
3754 }
3755 } else if (qual->flags.q.explicit_index) {
3756 if (!qual->subroutine_list)
3757 _mesa_glsl_error(loc, state,
3758 "explicit index requires explicit location");
3759 } else if (qual->flags.q.explicit_component) {
3760 _mesa_glsl_error(loc, state,
3761 "explicit component requires explicit location");
3762 }
3763
3764 if (qual->flags.q.explicit_binding) {
3765 apply_explicit_binding(state, loc, var, var->type, qual);
3766 }
3767
3768 if (state->stage == MESA_SHADER_GEOMETRY &&
3769 qual->flags.q.out && qual->flags.q.stream) {
3770 unsigned qual_stream;
3771 if (process_qualifier_constant(state, loc, "stream", qual->stream,
3772 &qual_stream) &&
3773 validate_stream_qualifier(loc, state, qual_stream)) {
3774 var->data.stream = qual_stream;
3775 }
3776 }
3777
3778 if (qual->flags.q.out && qual->flags.q.xfb_buffer) {
3779 unsigned qual_xfb_buffer;
3780 if (process_qualifier_constant(state, loc, "xfb_buffer",
3781 qual->xfb_buffer, &qual_xfb_buffer) &&
3782 validate_xfb_buffer_qualifier(loc, state, qual_xfb_buffer)) {
3783 var->data.xfb_buffer = qual_xfb_buffer;
3784 if (qual->flags.q.explicit_xfb_buffer)
3785 var->data.explicit_xfb_buffer = true;
3786 }
3787 }
3788
3789 if (qual->flags.q.explicit_xfb_offset) {
3790 unsigned qual_xfb_offset;
3791 unsigned component_size = var->type->contains_double() ? 8 : 4;
3792
3793 if (process_qualifier_constant(state, loc, "xfb_offset",
3794 qual->offset, &qual_xfb_offset) &&
3795 validate_xfb_offset_qualifier(loc, state, (int) qual_xfb_offset,
3796 var->type, component_size)) {
3797 var->data.offset = qual_xfb_offset;
3798 var->data.explicit_xfb_offset = true;
3799 }
3800 }
3801
3802 if (qual->flags.q.explicit_xfb_stride) {
3803 unsigned qual_xfb_stride;
3804 if (process_qualifier_constant(state, loc, "xfb_stride",
3805 qual->xfb_stride, &qual_xfb_stride)) {
3806 var->data.xfb_stride = qual_xfb_stride;
3807 var->data.explicit_xfb_stride = true;
3808 }
3809 }
3810
3811 if (var->type->contains_atomic()) {
3812 if (var->data.mode == ir_var_uniform) {
3813 if (var->data.explicit_binding) {
3814 unsigned *offset =
3815 &state->atomic_counter_offsets[var->data.binding];
3816
3817 if (*offset % ATOMIC_COUNTER_SIZE4)
3818 _mesa_glsl_error(loc, state,
3819 "misaligned atomic counter offset");
3820
3821 var->data.offset = *offset;
3822 *offset += var->type->atomic_size();
3823
3824 } else {
3825 _mesa_glsl_error(loc, state,
3826 "atomic counters require explicit binding point");
3827 }
3828 } else if (var->data.mode != ir_var_function_in) {
3829 _mesa_glsl_error(loc, state, "atomic counters may only be declared as "
3830 "function parameters or uniform-qualified "
3831 "global variables");
3832 }
3833 }
3834
3835 if (var->type->contains_sampler() &&
3836 !validate_storage_for_sampler_image_types(var, state, loc))
3837 return;
3838
3839 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
3840 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
3841 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
3842 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
3843 * These extensions and all following extensions that add the 'layout'
3844 * keyword have been modified to require the use of 'in' or 'out'.
3845 *
3846 * The following extension do not allow the deprecated keywords:
3847 *
3848 * GL_AMD_conservative_depth
3849 * GL_ARB_conservative_depth
3850 * GL_ARB_gpu_shader5
3851 * GL_ARB_separate_shader_objects
3852 * GL_ARB_tessellation_shader
3853 * GL_ARB_transform_feedback3
3854 * GL_ARB_uniform_buffer_object
3855 *
3856 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
3857 * allow layout with the deprecated keywords.
3858 */
3859 const bool relaxed_layout_qualifier_checking =
3860 state->ARB_fragment_coord_conventions_enable;
3861
3862 const bool uses_deprecated_qualifier = qual->flags.q.attribute
3863 || qual->flags.q.varying;
3864 if (qual->has_layout() && uses_deprecated_qualifier) {
3865 if (relaxed_layout_qualifier_checking) {
3866 _mesa_glsl_warning(loc, state,
3867 "`layout' qualifier may not be used with "
3868 "`attribute' or `varying'");
3869 } else {
3870 _mesa_glsl_error(loc, state,
3871 "`layout' qualifier may not be used with "
3872 "`attribute' or `varying'");
3873 }
3874 }
3875
3876 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
3877 * AMD_conservative_depth.
3878 */
3879 if (qual->flags.q.depth_type
3880 && !state->is_version(420, 0)
3881 && !state->AMD_conservative_depth_enable
3882 && !state->ARB_conservative_depth_enable) {
3883 _mesa_glsl_error(loc, state,
3884 "extension GL_AMD_conservative_depth or "
3885 "GL_ARB_conservative_depth must be enabled "
3886 "to use depth layout qualifiers");
3887 } else if (qual->flags.q.depth_type
3888 && strcmp(var->name, "gl_FragDepth") != 0) {
3889 _mesa_glsl_error(loc, state,
3890 "depth layout qualifiers can be applied only to "
3891 "gl_FragDepth");
3892 }
3893
3894 switch (qual->depth_type) {
3895 case ast_depth_any:
3896 var->data.depth_layout = ir_depth_layout_any;
3897 break;
3898 case ast_depth_greater:
3899 var->data.depth_layout = ir_depth_layout_greater;
3900 break;
3901 case ast_depth_less:
3902 var->data.depth_layout = ir_depth_layout_less;
3903 break;
3904 case ast_depth_unchanged:
3905 var->data.depth_layout = ir_depth_layout_unchanged;
3906 break;
3907 default:
3908 var->data.depth_layout = ir_depth_layout_none;
3909 break;
3910 }
3911
3912 if (qual->flags.q.std140 ||
3913 qual->flags.q.std430 ||
3914 qual->flags.q.packed ||
3915 qual->flags.q.shared) {
3916 _mesa_glsl_error(loc, state,
3917 "uniform and shader storage block layout qualifiers "
3918 "std140, std430, packed, and shared can only be "
3919 "applied to uniform or shader storage blocks, not "
3920 "members");
3921 }
3922
3923 if (qual->flags.q.row_major || qual->flags.q.column_major) {
3924 validate_matrix_layout_for_type(state, loc, var->type, var);
3925 }
3926
3927 /* From section 4.4.1.3 of the GLSL 4.50 specification (Fragment Shader
3928 * Inputs):
3929 *
3930 * "Fragment shaders also allow the following layout qualifier on in only
3931 * (not with variable declarations)
3932 * layout-qualifier-id
3933 * early_fragment_tests
3934 * [...]"
3935 */
3936 if (qual->flags.q.early_fragment_tests) {
3937 _mesa_glsl_error(loc, state, "early_fragment_tests layout qualifier only "
3938 "valid in fragment shader input layout declaration.");
3939 }
3940
3941 if (qual->flags.q.inner_coverage) {
3942 _mesa_glsl_error(loc, state, "inner_coverage layout qualifier only "
3943 "valid in fragment shader input layout declaration.");
3944 }
3945
3946 if (qual->flags.q.post_depth_coverage) {
3947 _mesa_glsl_error(loc, state, "post_depth_coverage layout qualifier only "
3948 "valid in fragment shader input layout declaration.");
3949 }
3950
3951 if (state->has_bindless())
3952 apply_bindless_qualifier_to_variable(qual, var, state, loc);
3953
3954 if (qual->flags.q.pixel_interlock_ordered ||
3955 qual->flags.q.pixel_interlock_unordered ||
3956 qual->flags.q.sample_interlock_ordered ||
3957 qual->flags.q.sample_interlock_unordered) {
3958 _mesa_glsl_error(loc, state, "interlock layout qualifiers: "
3959 "pixel_interlock_ordered, pixel_interlock_unordered, "
3960 "sample_interlock_ordered and sample_interlock_unordered, "
3961 "only valid in fragment shader input layout declaration.");
3962 }
3963
3964 if (var->name != NULL__null && strcmp(var->name, "gl_Layer") == 0) {
3965 if (is_conflicting_layer_redeclaration(state, qual)) {
3966 _mesa_glsl_error(loc, state, "gl_Layer redeclaration with "
3967 "different viewport_relative setting than earlier");
3968 }
3969 state->redeclares_gl_layer = 1;
3970 if (qual->flags.q.viewport_relative) {
3971 state->layer_viewport_relative = 1;
3972 }
3973 } else if (qual->flags.q.viewport_relative) {
3974 _mesa_glsl_error(loc, state,
3975 "viewport_relative qualifier "
3976 "can only be applied to gl_Layer.");
3977 }
3978}
3979
3980static void
3981apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
3982 ir_variable *var,
3983 struct _mesa_glsl_parse_state *state,
3984 YYLTYPE *loc,
3985 bool is_parameter)
3986{
3987 STATIC_ASSERT(sizeof(qual->flags.q) <= sizeof(qual->flags.i))do { (void) sizeof(char [1 - 2*!(sizeof(qual->flags.q) <=
sizeof(qual->flags.i))]); } while (0)
;
3988
3989 if (qual->flags.q.invariant) {
3990 if (var->data.used) {
3991 _mesa_glsl_error(loc, state,
3992 "variable `%s' may not be redeclared "
3993 "`invariant' after being used",
3994 var->name);
3995 } else {
3996 var->data.explicit_invariant = true;
3997 var->data.invariant = true;
3998 }
3999 }
4000
4001 if (qual->flags.q.precise) {
4002 if (var->data.used) {
4003 _mesa_glsl_error(loc, state,
4004 "variable `%s' may not be redeclared "
4005 "`precise' after being used",
4006 var->name);
4007 } else {
4008 var->data.precise = 1;
4009 }
4010 }
4011
4012 if (qual->is_subroutine_decl() && !qual->flags.q.uniform) {
4013 _mesa_glsl_error(loc, state,
4014 "`subroutine' may only be applied to uniforms, "
4015 "subroutine type declarations, or function definitions");
4016 }
4017
4018 if (qual->flags.q.constant || qual->flags.q.attribute
4019 || qual->flags.q.uniform
4020 || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT)))
4021 var->data.read_only = 1;
4022
4023 if (qual->flags.q.centroid)
4024 var->data.centroid = 1;
4025
4026 if (qual->flags.q.sample)
4027 var->data.sample = 1;
4028
4029 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
4030 if (state->es_shader) {
4031 var->data.precision =
4032 select_gles_precision(qual->precision, var->type, state, loc);
4033 }
4034
4035 if (qual->flags.q.patch)
4036 var->data.patch = 1;
4037
4038 if (qual->flags.q.attribute && state->stage != MESA_SHADER_VERTEX) {
4039 var->type = glsl_type::error_type;
4040 _mesa_glsl_error(loc, state,
4041 "`attribute' variables may not be declared in the "
4042 "%s shader",
4043 _mesa_shader_stage_to_string(state->stage));
4044 }
4045
4046 /* Disallow layout qualifiers which may only appear on layout declarations. */
4047 if (qual->flags.q.prim_type) {
4048 _mesa_glsl_error(loc, state,
4049 "Primitive type may only be specified on GS input or output "
4050 "layout declaration, not on variables.");
4051 }
4052
4053 /* Section 6.1.1 (Function Calling Conventions) of the GLSL 1.10 spec says:
4054 *
4055 * "However, the const qualifier cannot be used with out or inout."
4056 *
4057 * The same section of the GLSL 4.40 spec further clarifies this saying:
4058 *
4059 * "The const qualifier cannot be used with out or inout, or a
4060 * compile-time error results."
4061 */
4062 if (is_parameter && qual->flags.q.constant && qual->flags.q.out) {
4063 _mesa_glsl_error(loc, state,
4064 "`const' may not be applied to `out' or `inout' "
4065 "function parameters");
4066 }
4067
4068 /* If there is no qualifier that changes the mode of the variable, leave
4069 * the setting alone.
4070 */
4071 assert(var->data.mode != ir_var_temporary)(static_cast <bool> (var->data.mode != ir_var_temporary
) ? void (0) : __assert_fail ("var->data.mode != ir_var_temporary"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
4072 if (qual->flags.q.in && qual->flags.q.out)
4073 var->data.mode = is_parameter ? ir_var_function_inout : ir_var_shader_out;
4074 else if (qual->flags.q.in)
4075 var->data.mode = is_parameter ? ir_var_function_in : ir_var_shader_in;
4076 else if (qual->flags.q.attribute
4077 || (qual->flags.q.varying && (state->stage == MESA_SHADER_FRAGMENT)))
4078 var->data.mode = ir_var_shader_in;
4079 else if (qual->flags.q.out)
4080 var->data.mode = is_parameter ? ir_var_function_out : ir_var_shader_out;
4081 else if (qual->flags.q.varying && (state->stage == MESA_SHADER_VERTEX))
4082 var->data.mode = ir_var_shader_out;
4083 else if (qual->flags.q.uniform)
4084 var->data.mode = ir_var_uniform;
4085 else if (qual->flags.q.buffer)
4086 var->data.mode = ir_var_shader_storage;
4087 else if (qual->flags.q.shared_storage)
4088 var->data.mode = ir_var_shader_shared;
4089
4090 if (!is_parameter && state->has_framebuffer_fetch() &&
4091 state->stage == MESA_SHADER_FRAGMENT) {
4092 if (state->is_version(130, 300))
4093 var->data.fb_fetch_output = qual->flags.q.in && qual->flags.q.out;
4094 else
4095 var->data.fb_fetch_output = (strcmp(var->name, "gl_LastFragData") == 0);
4096 }
4097
4098 if (var->data.fb_fetch_output) {
4099 var->data.assigned = true;
4100 var->data.memory_coherent = !qual->flags.q.non_coherent;
4101
4102 /* From the EXT_shader_framebuffer_fetch spec:
4103 *
4104 * "It is an error to declare an inout fragment output not qualified
4105 * with layout(noncoherent) if the GL_EXT_shader_framebuffer_fetch
4106 * extension hasn't been enabled."
4107 */
4108 if (var->data.memory_coherent &&
4109 !state->EXT_shader_framebuffer_fetch_enable)
4110 _mesa_glsl_error(loc, state,
4111 "invalid declaration of framebuffer fetch output not "
4112 "qualified with layout(noncoherent)");
4113
4114 } else {
4115 /* From the EXT_shader_framebuffer_fetch spec:
4116 *
4117 * "Fragment outputs declared inout may specify the following layout
4118 * qualifier: [...] noncoherent"
4119 */
4120 if (qual->flags.q.non_coherent)
4121 _mesa_glsl_error(loc, state,
4122 "invalid layout(noncoherent) qualifier not part of "
4123 "framebuffer fetch output declaration");
4124 }
4125
4126 if (!is_parameter && is_varying_var(var, state->stage)) {
4127 /* User-defined ins/outs are not permitted in compute shaders. */
4128 if (state->stage == MESA_SHADER_COMPUTE) {
4129 _mesa_glsl_error(loc, state,
4130 "user-defined input and output variables are not "
4131 "permitted in compute shaders");
4132 }
4133
4134 /* This variable is being used to link data between shader stages (in
4135 * pre-glsl-1.30 parlance, it's a "varying"). Check that it has a type
4136 * that is allowed for such purposes.
4137 *
4138 * From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
4139 *
4140 * "The varying qualifier can be used only with the data types
4141 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
4142 * these."
4143 *
4144 * This was relaxed in GLSL version 1.30 and GLSL ES version 3.00. From
4145 * page 31 (page 37 of the PDF) of the GLSL 1.30 spec:
4146 *
4147 * "Fragment inputs can only be signed and unsigned integers and
4148 * integer vectors, float, floating-point vectors, matrices, or
4149 * arrays of these. Structures cannot be input.
4150 *
4151 * Similar text exists in the section on vertex shader outputs.
4152 *
4153 * Similar text exists in the GLSL ES 3.00 spec, except that the GLSL ES
4154 * 3.00 spec allows structs as well. Varying structs are also allowed
4155 * in GLSL 1.50.
4156 *
4157 * From section 4.3.4 of the ARB_bindless_texture spec:
4158 *
4159 * "(modify third paragraph of the section to allow sampler and image
4160 * types) ... Vertex shader inputs can only be float,
4161 * single-precision floating-point scalars, single-precision
4162 * floating-point vectors, matrices, signed and unsigned integers
4163 * and integer vectors, sampler and image types."
4164 *
4165 * From section 4.3.6 of the ARB_bindless_texture spec:
4166 *
4167 * "Output variables can only be floating-point scalars,
4168 * floating-point vectors, matrices, signed or unsigned integers or
4169 * integer vectors, sampler or image types, or arrays or structures
4170 * of any these."
4171 */
4172 switch (var->type->without_array()->base_type) {
4173 case GLSL_TYPE_FLOAT:
4174 /* Ok in all GLSL versions */
4175 break;
4176 case GLSL_TYPE_UINT:
4177 case GLSL_TYPE_INT:
4178 if (state->is_version(130, 300) || state->EXT_gpu_shader4_enable)
4179 break;
4180 _mesa_glsl_error(loc, state,
4181 "varying variables must be of base type float in %s",
4182 state->get_version_string());
4183 break;
4184 case GLSL_TYPE_STRUCT:
4185 if (state->is_version(150, 300))
4186 break;
4187 _mesa_glsl_error(loc, state,
4188 "varying variables may not be of type struct");
4189 break;
4190 case GLSL_TYPE_DOUBLE:
4191 case GLSL_TYPE_UINT64:
4192 case GLSL_TYPE_INT64:
4193 break;
4194 case GLSL_TYPE_SAMPLER:
4195 case GLSL_TYPE_IMAGE:
4196 if (state->has_bindless())
4197 break;
4198 /* fallthrough */
4199 default:
4200 _mesa_glsl_error(loc, state, "illegal type for a varying variable");
4201 break;
4202 }
4203 }
4204
4205 if (state->all_invariant && var->data.mode == ir_var_shader_out) {
4206 var->data.explicit_invariant = true;
4207 var->data.invariant = true;
4208 }
4209
4210 var->data.interpolation =
4211 interpret_interpolation_qualifier(qual, var->type,
4212 (ir_variable_mode) var->data.mode,
4213 state, loc);
4214
4215 /* Does the declaration use the deprecated 'attribute' or 'varying'
4216 * keywords?
4217 */
4218 const bool uses_deprecated_qualifier = qual->flags.q.attribute
4219 || qual->flags.q.varying;
4220
4221
4222 /* Validate auxiliary storage qualifiers */
4223
4224 /* From section 4.3.4 of the GLSL 1.30 spec:
4225 * "It is an error to use centroid in in a vertex shader."
4226 *
4227 * From section 4.3.4 of the GLSL ES 3.00 spec:
4228 * "It is an error to use centroid in or interpolation qualifiers in
4229 * a vertex shader input."
4230 */
4231
4232 /* Section 4.3.6 of the GLSL 1.30 specification states:
4233 * "It is an error to use centroid out in a fragment shader."
4234 *
4235 * The GL_ARB_shading_language_420pack extension specification states:
4236 * "It is an error to use auxiliary storage qualifiers or interpolation
4237 * qualifiers on an output in a fragment shader."
4238 */
4239 if (qual->flags.q.sample && (!is_varying_var(var, state->stage) || uses_deprecated_qualifier)) {
4240 _mesa_glsl_error(loc, state,
4241 "sample qualifier may only be used on `in` or `out` "
4242 "variables between shader stages");
4243 }
4244 if (qual->flags.q.centroid && !is_varying_var(var, state->stage)) {
4245 _mesa_glsl_error(loc, state,
4246 "centroid qualifier may only be used with `in', "
4247 "`out' or `varying' variables between shader stages");
4248 }
4249
4250 if (qual->flags.q.shared_storage && state->stage != MESA_SHADER_COMPUTE) {
4251 _mesa_glsl_error(loc, state,
4252 "the shared storage qualifiers can only be used with "
4253 "compute shaders");
4254 }
4255
4256 apply_image_qualifier_to_variable(qual, var, state, loc);
4257}
4258
4259/**
4260 * Get the variable that is being redeclared by this declaration or if it
4261 * does not exist, the current declared variable.
4262 *
4263 * Semantic checks to verify the validity of the redeclaration are also
4264 * performed. If semantic checks fail, compilation error will be emitted via
4265 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
4266 *
4267 * \returns
4268 * A pointer to an existing variable in the current scope if the declaration
4269 * is a redeclaration, current variable otherwise. \c is_declared boolean
4270 * will return \c true if the declaration is a redeclaration, \c false
4271 * otherwise.
4272 */
4273static ir_variable *
4274get_variable_being_redeclared(ir_variable **var_ptr, YYLTYPE loc,
4275 struct _mesa_glsl_parse_state *state,
4276 bool allow_all_redeclarations,
4277 bool *is_redeclaration)
4278{
4279 ir_variable *var = *var_ptr;
4280
4281 /* Check if this declaration is actually a re-declaration, either to
4282 * resize an array or add qualifiers to an existing variable.
4283 *
4284 * This is allowed for variables in the current scope, or when at
4285 * global scope (for built-ins in the implicit outer scope).
4286 */
4287 ir_variable *earlier = state->symbols->get_variable(var->name);
4288 if (earlier == NULL__null ||
4289 (state->current_function != NULL__null &&
4290 !state->symbols->name_declared_this_scope(var->name))) {
4291 *is_redeclaration = false;
4292 return var;
4293 }
4294
4295 *is_redeclaration = true;
4296
4297 if (earlier->data.how_declared == ir_var_declared_implicitly) {
4298 /* Verify that the redeclaration of a built-in does not change the
4299 * storage qualifier. There are a couple special cases.
4300 *
4301 * 1. Some built-in variables that are defined as 'in' in the
4302 * specification are implemented as system values. Allow
4303 * ir_var_system_value -> ir_var_shader_in.
4304 *
4305 * 2. gl_LastFragData is implemented as a ir_var_shader_out, but the
4306 * specification requires that redeclarations omit any qualifier.
4307 * Allow ir_var_shader_out -> ir_var_auto for this one variable.
4308 */
4309 if (earlier->data.mode != var->data.mode &&
4310 !(earlier->data.mode == ir_var_system_value &&
4311 var->data.mode == ir_var_shader_in) &&
4312 !(strcmp(var->name, "gl_LastFragData") == 0 &&
4313 var->data.mode == ir_var_auto)) {
4314 _mesa_glsl_error(&loc, state,
4315 "redeclaration cannot change qualification of `%s'",
4316 var->name);
4317 }
4318 }
4319
4320 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
4321 *
4322 * "It is legal to declare an array without a size and then
4323 * later re-declare the same name as an array of the same
4324 * type and specify a size."
4325 */
4326 if (earlier->type->is_unsized_array() && var->type->is_array()
4327 && (var->type->fields.array == earlier->type->fields.array)) {
4328 const int size = var->type->array_size();
4329 check_builtin_array_max_size(var->name, size, loc, state);
4330 if ((size > 0) && (size <= earlier->data.max_array_access)) {
4331 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
4332 "previous access",
4333 earlier->data.max_array_access);
4334 }
4335
4336 earlier->type = var->type;
4337 delete var;
4338 var = NULL__null;
4339 *var_ptr = NULL__null;
4340 } else if (earlier->type != var->type) {
4341 _mesa_glsl_error(&loc, state,
4342 "redeclaration of `%s' has incorrect type",
4343 var->name);
4344 } else if ((state->ARB_fragment_coord_conventions_enable ||
4345 state->is_version(150, 0))
4346 && strcmp(var->name, "gl_FragCoord") == 0) {
4347 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
4348 * qualifiers.
4349 *
4350 * We don't really need to do anything here, just allow the
4351 * redeclaration. Any error on the gl_FragCoord is handled on the ast
4352 * level at apply_layout_qualifier_to_variable using the
4353 * ast_type_qualifier and _mesa_glsl_parse_state, or later at
4354 * linker.cpp.
4355 */
4356 /* According to section 4.3.7 of the GLSL 1.30 spec,
4357 * the following built-in varaibles can be redeclared with an
4358 * interpolation qualifier:
4359 * * gl_FrontColor
4360 * * gl_BackColor
4361 * * gl_FrontSecondaryColor
4362 * * gl_BackSecondaryColor
4363 * * gl_Color
4364 * * gl_SecondaryColor
4365 */
4366 } else if (state->is_version(130, 0)
4367 && (strcmp(var->name, "gl_FrontColor") == 0
4368 || strcmp(var->name, "gl_BackColor") == 0
4369 || strcmp(var->name, "gl_FrontSecondaryColor") == 0
4370 || strcmp(var->name, "gl_BackSecondaryColor") == 0
4371 || strcmp(var->name, "gl_Color") == 0
4372 || strcmp(var->name, "gl_SecondaryColor") == 0)) {
4373 earlier->data.interpolation = var->data.interpolation;
4374
4375 /* Layout qualifiers for gl_FragDepth. */
4376 } else if ((state->is_version(420, 0) ||
4377 state->AMD_conservative_depth_enable ||
4378 state->ARB_conservative_depth_enable)
4379 && strcmp(var->name, "gl_FragDepth") == 0) {
4380
4381 /** From the AMD_conservative_depth spec:
4382 * Within any shader, the first redeclarations of gl_FragDepth
4383 * must appear before any use of gl_FragDepth.
4384 */
4385 if (earlier->data.used) {
4386 _mesa_glsl_error(&loc, state,
4387 "the first redeclaration of gl_FragDepth "
4388 "must appear before any use of gl_FragDepth");
4389 }
4390
4391 /* Prevent inconsistent redeclaration of depth layout qualifier. */
4392 if (earlier->data.depth_layout != ir_depth_layout_none
4393 && earlier->data.depth_layout != var->data.depth_layout) {
4394 _mesa_glsl_error(&loc, state,
4395 "gl_FragDepth: depth layout is declared here "
4396 "as '%s, but it was previously declared as "
4397 "'%s'",
4398 depth_layout_string(var->data.depth_layout),
4399 depth_layout_string(earlier->data.depth_layout));
4400 }
4401
4402 earlier->data.depth_layout = var->data.depth_layout;
4403
4404 } else if (state->has_framebuffer_fetch() &&
4405 strcmp(var->name, "gl_LastFragData") == 0 &&
4406 var->data.mode == ir_var_auto) {
4407 /* According to the EXT_shader_framebuffer_fetch spec:
4408 *
4409 * "By default, gl_LastFragData is declared with the mediump precision
4410 * qualifier. This can be changed by redeclaring the corresponding
4411 * variables with the desired precision qualifier."
4412 *
4413 * "Fragment shaders may specify the following layout qualifier only for
4414 * redeclaring the built-in gl_LastFragData array [...]: noncoherent"
4415 */
4416 earlier->data.precision = var->data.precision;
4417 earlier->data.memory_coherent = var->data.memory_coherent;
4418
4419 } else if (state->NV_viewport_array2_enable &&
4420 strcmp(var->name, "gl_Layer") == 0 &&
4421 earlier->data.how_declared == ir_var_declared_implicitly) {
4422 /* No need to do anything, just allow it. Qualifier is stored in state */
4423
4424 } else if ((earlier->data.how_declared == ir_var_declared_implicitly &&
4425 state->allow_builtin_variable_redeclaration) ||
4426 allow_all_redeclarations) {
4427 /* Allow verbatim redeclarations of built-in variables. Not explicitly
4428 * valid, but some applications do it.
4429 */
4430 } else {
4431 _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
4432 }
4433
4434 return earlier;
4435}
4436
4437/**
4438 * Generate the IR for an initializer in a variable declaration
4439 */
4440static ir_rvalue *
4441process_initializer(ir_variable *var, ast_declaration *decl,
4442 ast_fully_specified_type *type,
4443 exec_list *initializer_instructions,
4444 struct _mesa_glsl_parse_state *state)
4445{
4446 void *mem_ctx = state;
4447 ir_rvalue *result = NULL__null;
4448
4449 YYLTYPE initializer_loc = decl->initializer->get_location();
4450
4451 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
4452 *
4453 * "All uniform variables are read-only and are initialized either
4454 * directly by an application via API commands, or indirectly by
4455 * OpenGL."
4456 */
4457 if (var->data.mode == ir_var_uniform) {
4458 state->check_version(120, 0, &initializer_loc,
4459 "cannot initialize uniform %s",
4460 var->name);
4461 }
4462
4463 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
4464 *
4465 * "Buffer variables cannot have initializers."
4466 */
4467 if (var->data.mode == ir_var_shader_storage) {
4468 _mesa_glsl_error(&initializer_loc, state,
4469 "cannot initialize buffer variable %s",
4470 var->name);
4471 }
4472
4473 /* From section 4.1.7 of the GLSL 4.40 spec:
4474 *
4475 * "Opaque variables [...] are initialized only through the
4476 * OpenGL API; they cannot be declared with an initializer in a
4477 * shader."
4478 *
4479 * From section 4.1.7 of the ARB_bindless_texture spec:
4480 *
4481 * "Samplers may be declared as shader inputs and outputs, as uniform
4482 * variables, as temporary variables, and as function parameters."
4483 *
4484 * From section 4.1.X of the ARB_bindless_texture spec:
4485 *
4486 * "Images may be declared as shader inputs and outputs, as uniform
4487 * variables, as temporary variables, and as function parameters."
4488 */
4489 if (var->type->contains_atomic() ||
4490 (!state->has_bindless() && var->type->contains_opaque())) {
4491 _mesa_glsl_error(&initializer_loc, state,
4492 "cannot initialize %s variable %s",
4493 var->name, state->has_bindless() ? "atomic" : "opaque");
4494 }
4495
4496 if ((var->data.mode == ir_var_shader_in) && (state->current_function == NULL__null)) {
4497 _mesa_glsl_error(&initializer_loc, state,
4498 "cannot initialize %s shader input / %s %s",
4499 _mesa_shader_stage_to_string(state->stage),
4500 (state->stage == MESA_SHADER_VERTEX)
4501 ? "attribute" : "varying",
4502 var->name);
4503 }
4504
4505 if (var->data.mode == ir_var_shader_out && state->current_function == NULL__null) {
4506 _mesa_glsl_error(&initializer_loc, state,
4507 "cannot initialize %s shader output %s",
4508 _mesa_shader_stage_to_string(state->stage),
4509 var->name);
4510 }
4511
4512 /* If the initializer is an ast_aggregate_initializer, recursively store
4513 * type information from the LHS into it, so that its hir() function can do
4514 * type checking.
4515 */
4516 if (decl->initializer->oper == ast_aggregate)
4517 _mesa_ast_set_aggregate_type(var->type, decl->initializer);
4518
4519 ir_dereference *const lhs = new(state) ir_dereference_variable(var);
4520 ir_rvalue *rhs = decl->initializer->hir(initializer_instructions, state);
4521
4522 /* Calculate the constant value if this is a const or uniform
4523 * declaration.
4524 *
4525 * Section 4.3 (Storage Qualifiers) of the GLSL ES 1.00.17 spec says:
4526 *
4527 * "Declarations of globals without a storage qualifier, or with
4528 * just the const qualifier, may include initializers, in which case
4529 * they will be initialized before the first line of main() is
4530 * executed. Such initializers must be a constant expression."
4531 *
4532 * The same section of the GLSL ES 3.00.4 spec has similar language.
4533 */
4534 if (type->qualifier.flags.q.constant
4535 || type->qualifier.flags.q.uniform
4536 || (state->es_shader && state->current_function == NULL__null)) {
4537 ir_rvalue *new_rhs = validate_assignment(state, initializer_loc,
4538 lhs, rhs, true);
4539 if (new_rhs != NULL__null) {
4540 rhs = new_rhs;
4541
4542 /* Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec
4543 * says:
4544 *
4545 * "A constant expression is one of
4546 *
4547 * ...
4548 *
4549 * - an expression formed by an operator on operands that are
4550 * all constant expressions, including getting an element of
4551 * a constant array, or a field of a constant structure, or
4552 * components of a constant vector. However, the sequence
4553 * operator ( , ) and the assignment operators ( =, +=, ...)
4554 * are not included in the operators that can create a
4555 * constant expression."
4556 *
4557 * Section 12.43 (Sequence operator and constant expressions) says:
4558 *
4559 * "Should the following construct be allowed?
4560 *
4561 * float a[2,3];
4562 *
4563 * The expression within the brackets uses the sequence operator
4564 * (',') and returns the integer 3 so the construct is declaring
4565 * a single-dimensional array of size 3. In some languages, the
4566 * construct declares a two-dimensional array. It would be
4567 * preferable to make this construct illegal to avoid confusion.
4568 *
4569 * One possibility is to change the definition of the sequence
4570 * operator so that it does not return a constant-expression and
4571 * hence cannot be used to declare an array size.
4572 *
4573 * RESOLUTION: The result of a sequence operator is not a
4574 * constant-expression."
4575 *
4576 * Section 4.3.3 (Constant Expressions) of the GLSL 4.30.9 spec
4577 * contains language almost identical to the section 4.3.3 in the
4578 * GLSL ES 3.00.4 spec. This is a new limitation for these GLSL
4579 * versions.
4580 */
4581 ir_constant *constant_value =
4582 rhs->constant_expression_value(mem_ctx);
4583
4584 if (!constant_value ||
4585 (state->is_version(430, 300) &&
4586 decl->initializer->has_sequence_subexpression())) {
4587 const char *const variable_mode =
4588 (type->qualifier.flags.q.constant)
4589 ? "const"
4590 : ((type->qualifier.flags.q.uniform) ? "uniform" : "global");
4591
4592 /* If ARB_shading_language_420pack is enabled, initializers of
4593 * const-qualified local variables do not have to be constant
4594 * expressions. Const-qualified global variables must still be
4595 * initialized with constant expressions.
4596 */
4597 if (!state->has_420pack()
4598 || state->current_function == NULL__null) {
4599 _mesa_glsl_error(& initializer_loc, state,
4600 "initializer of %s variable `%s' must be a "
4601 "constant expression",
4602 variable_mode,
4603 decl->identifier);
4604 if (var->type->is_numeric()) {
4605 /* Reduce cascading errors. */
4606 var->constant_value = type->qualifier.flags.q.constant
4607 ? ir_constant::zero(state, var->type) : NULL__null;
4608 }
4609 }
4610 } else {
4611 rhs = constant_value;
4612 var->constant_value = type->qualifier.flags.q.constant
4613 ? constant_value : NULL__null;
4614 }
4615 } else {
4616 if (var->type->is_numeric()) {
4617 /* Reduce cascading errors. */
4618 rhs = var->constant_value = type->qualifier.flags.q.constant
4619 ? ir_constant::zero(state, var->type) : NULL__null;
4620 }
4621 }
4622 }
4623
4624 if (rhs && !rhs->type->is_error()) {
4625 bool temp = var->data.read_only;
4626 if (type->qualifier.flags.q.constant)
4627 var->data.read_only = false;
4628
4629 /* Never emit code to initialize a uniform.
4630 */
4631 const glsl_type *initializer_type;
4632 bool error_emitted = false;
4633 if (!type->qualifier.flags.q.uniform) {
4634 error_emitted =
4635 do_assignment(initializer_instructions, state,
4636 NULL__null, lhs, rhs,
4637 &result, true, true,
4638 type->get_location());
4639 initializer_type = result->type;
4640 } else
4641 initializer_type = rhs->type;
4642
4643 if (!error_emitted) {
4644 var->constant_initializer = rhs->constant_expression_value(mem_ctx);
4645 var->data.has_initializer = true;
4646
4647 /* If the declared variable is an unsized array, it must inherrit
4648 * its full type from the initializer. A declaration such as
4649 *
4650 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
4651 *
4652 * becomes
4653 *
4654 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
4655 *
4656 * The assignment generated in the if-statement (below) will also
4657 * automatically handle this case for non-uniforms.
4658 *
4659 * If the declared variable is not an array, the types must
4660 * already match exactly. As a result, the type assignment
4661 * here can be done unconditionally. For non-uniforms the call
4662 * to do_assignment can change the type of the initializer (via
4663 * the implicit conversion rules). For uniforms the initializer
4664 * must be a constant expression, and the type of that expression
4665 * was validated above.
4666 */
4667 var->type = initializer_type;
4668 }
4669
4670 var->data.read_only = temp;
4671 }
4672
4673 return result;
4674}
4675
4676static void
4677validate_layout_qualifier_vertex_count(struct _mesa_glsl_parse_state *state,
4678 YYLTYPE loc, ir_variable *var,
4679 unsigned num_vertices,
4680 unsigned *size,
4681 const char *var_category)
4682{
4683 if (var->type->is_unsized_array()) {
4684 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec says:
4685 *
4686 * All geometry shader input unsized array declarations will be
4687 * sized by an earlier input layout qualifier, when present, as per
4688 * the following table.
4689 *
4690 * Followed by a table mapping each allowed input layout qualifier to
4691 * the corresponding input length.
4692 *
4693 * Similarly for tessellation control shader outputs.
4694 */
4695 if (num_vertices != 0)
4696 var->type = glsl_type::get_array_instance(var->type->fields.array,
4697 num_vertices);
4698 } else {
4699 /* Section 4.3.8.1 (Input Layout Qualifiers) of the GLSL 1.50 spec
4700 * includes the following examples of compile-time errors:
4701 *
4702 * // code sequence within one shader...
4703 * in vec4 Color1[]; // size unknown
4704 * ...Color1.length()...// illegal, length() unknown
4705 * in vec4 Color2[2]; // size is 2
4706 * ...Color1.length()...// illegal, Color1 still has no size
4707 * in vec4 Color3[3]; // illegal, input sizes are inconsistent
4708 * layout(lines) in; // legal, input size is 2, matching
4709 * in vec4 Color4[3]; // illegal, contradicts layout
4710 * ...
4711 *
4712 * To detect the case illustrated by Color3, we verify that the size of
4713 * an explicitly-sized array matches the size of any previously declared
4714 * explicitly-sized array. To detect the case illustrated by Color4, we
4715 * verify that the size of an explicitly-sized array is consistent with
4716 * any previously declared input layout.
4717 */
4718 if (num_vertices != 0 && var->type->length != num_vertices) {
4719 _mesa_glsl_error(&loc, state,
4720 "%s size contradicts previously declared layout "
4721 "(size is %u, but layout requires a size of %u)",
4722 var_category, var->type->length, num_vertices);
4723 } else if (*size != 0 && var->type->length != *size) {
4724 _mesa_glsl_error(&loc, state,
4725 "%s sizes are inconsistent (size is %u, but a "
4726 "previous declaration has size %u)",
4727 var_category, var->type->length, *size);
4728 } else {
4729 *size = var->type->length;
4730 }
4731 }
4732}
4733
4734static void
4735handle_tess_ctrl_shader_output_decl(struct _mesa_glsl_parse_state *state,
4736 YYLTYPE loc, ir_variable *var)
4737{
4738 unsigned num_vertices = 0;
4739
4740 if (state->tcs_output_vertices_specified) {
4741 if (!state->out_qualifier->vertices->
4742 process_qualifier_constant(state, "vertices",
4743 &num_vertices, false)) {
4744 return;
4745 }
4746
4747 if (num_vertices > state->Const.MaxPatchVertices) {
4748 _mesa_glsl_error(&loc, state, "vertices (%d) exceeds "
4749 "GL_MAX_PATCH_VERTICES", num_vertices);
4750 return;
4751 }
4752 }
4753
4754 if (!var->type->is_array() && !var->data.patch) {
4755 _mesa_glsl_error(&loc, state,
4756 "tessellation control shader outputs must be arrays");
4757
4758 /* To avoid cascading failures, short circuit the checks below. */
4759 return;
4760 }
4761
4762 if (var->data.patch)
4763 return;
4764
4765 validate_layout_qualifier_vertex_count(state, loc, var, num_vertices,
4766 &state->tcs_output_size,
4767 "tessellation control shader output");
4768}
4769
4770/**
4771 * Do additional processing necessary for tessellation control/evaluation shader
4772 * input declarations. This covers both interface block arrays and bare input
4773 * variables.
4774 */
4775static void
4776handle_tess_shader_input_decl(struct _mesa_glsl_parse_state *state,
4777 YYLTYPE loc, ir_variable *var)
4778{
4779 if (!var->type->is_array() && !var->data.patch) {
4780 _mesa_glsl_error(&loc, state,
4781 "per-vertex tessellation shader inputs must be arrays");
4782 /* Avoid cascading failures. */
4783 return;
4784 }
4785
4786 if (var->data.patch)
4787 return;
4788
4789 /* The ARB_tessellation_shader spec says:
4790 *
4791 * "Declaring an array size is optional. If no size is specified, it
4792 * will be taken from the implementation-dependent maximum patch size
4793 * (gl_MaxPatchVertices). If a size is specified, it must match the
4794 * maximum patch size; otherwise, a compile or link error will occur."
4795 *
4796 * This text appears twice, once for TCS inputs, and again for TES inputs.
4797 */
4798 if (var->type->is_unsized_array()) {
4799 var->type = glsl_type::get_array_instance(var->type->fields.array,
4800 state->Const.MaxPatchVertices);
4801 } else if (var->type->length != state->Const.MaxPatchVertices) {
4802 _mesa_glsl_error(&loc, state,
4803 "per-vertex tessellation shader input arrays must be "
4804 "sized to gl_MaxPatchVertices (%d).",
4805 state->Const.MaxPatchVertices);
4806 }
4807}
4808
4809
4810/**
4811 * Do additional processing necessary for geometry shader input declarations
4812 * (this covers both interface blocks arrays and bare input variables).
4813 */
4814static void
4815handle_geometry_shader_input_decl(struct _mesa_glsl_parse_state *state,
4816 YYLTYPE loc, ir_variable *var)
4817{
4818 unsigned num_vertices = 0;
4819
4820 if (state->gs_input_prim_type_specified) {
4821 num_vertices = vertices_per_prim(state->in_qualifier->prim_type);
4822 }
4823
4824 /* Geometry shader input variables must be arrays. Caller should have
4825 * reported an error for this.
4826 */
4827 if (!var->type->is_array()) {
4828 assert(state->error)(static_cast <bool> (state->error) ? void (0) : __assert_fail
("state->error", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
4829
4830 /* To avoid cascading failures, short circuit the checks below. */
4831 return;
4832 }
4833
4834 validate_layout_qualifier_vertex_count(state, loc, var, num_vertices,
4835 &state->gs_input_size,
4836 "geometry shader input");
4837}
4838
4839static void
4840validate_identifier(const char *identifier, YYLTYPE loc,
4841 struct _mesa_glsl_parse_state *state)
4842{
4843 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
4844 *
4845 * "Identifiers starting with "gl_" are reserved for use by
4846 * OpenGL, and may not be declared in a shader as either a
4847 * variable or a function."
4848 */
4849 if (is_gl_identifier(identifier)) {
4850 _mesa_glsl_error(&loc, state,
4851 "identifier `%s' uses reserved `gl_' prefix",
4852 identifier);
4853 } else if (strstr(identifier, "__")) {
4854 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
4855 * spec:
4856 *
4857 * "In addition, all identifiers containing two
4858 * consecutive underscores (__) are reserved as
4859 * possible future keywords."
4860 *
4861 * The intention is that names containing __ are reserved for internal
4862 * use by the implementation, and names prefixed with GL_ are reserved
4863 * for use by Khronos. Names simply containing __ are dangerous to use,
4864 * but should be allowed.
4865 *
4866 * A future version of the GLSL specification will clarify this.
4867 */
4868 _mesa_glsl_warning(&loc, state,
4869 "identifier `%s' uses reserved `__' string",
4870 identifier);
4871 }
4872}
4873
4874ir_rvalue *
4875ast_declarator_list::hir(exec_list *instructions,
4876 struct _mesa_glsl_parse_state *state)
4877{
4878 void *ctx = state;
4879 const struct glsl_type *decl_type;
4880 const char *type_name = NULL__null;
4881 ir_rvalue *result = NULL__null;
4882 YYLTYPE loc = this->get_location();
4883
4884 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
4885 *
4886 * "To ensure that a particular output variable is invariant, it is
4887 * necessary to use the invariant qualifier. It can either be used to
4888 * qualify a previously declared variable as being invariant
4889 *
4890 * invariant gl_Position; // make existing gl_Position be invariant"
4891 *
4892 * In these cases the parser will set the 'invariant' flag in the declarator
4893 * list, and the type will be NULL.
4894 */
4895 if (this->invariant) {
4896 assert(this->type == NULL)(static_cast <bool> (this->type == __null) ? void (0
) : __assert_fail ("this->type == NULL", __builtin_FILE ()
, __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
4897
4898 if (state->current_function != NULL__null) {
4899 _mesa_glsl_error(& loc, state,
4900 "all uses of `invariant' keyword must be at global "
4901 "scope");
4902 }
4903
4904 foreach_list_typed (ast_declaration, decl, link, &this->declarations)for (ast_declaration * decl = (!exec_node_is_tail_sentinel((&
this->declarations)->head_sentinel.next) ? ((ast_declaration
*) (((uintptr_t) (&this->declarations)->head_sentinel
.next) - (((char *) &((ast_declaration *) (&this->
declarations)->head_sentinel.next)->link) - ((char *) (
&this->declarations)->head_sentinel.next)))) : __null
); (decl) != __null; (decl) = (!exec_node_is_tail_sentinel((decl
)->link.next) ? ((ast_declaration *) (((uintptr_t) (decl)->
link.next) - (((char *) &((ast_declaration *) (decl)->
link.next)->link) - ((char *) (decl)->link.next)))) : __null
))
{
4905 assert(decl->array_specifier == NULL)(static_cast <bool> (decl->array_specifier == __null
) ? void (0) : __assert_fail ("decl->array_specifier == NULL"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
4906 assert(decl->initializer == NULL)(static_cast <bool> (decl->initializer == __null) ? void
(0) : __assert_fail ("decl->initializer == NULL", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
4907
4908 ir_variable *const earlier =
4909 state->symbols->get_variable(decl->identifier);
4910 if (earlier == NULL__null) {
4911 _mesa_glsl_error(& loc, state,
4912 "undeclared variable `%s' cannot be marked "
4913 "invariant", decl->identifier);
4914 } else if (!is_allowed_invariant(earlier, state)) {
4915 _mesa_glsl_error(&loc, state,
4916 "`%s' cannot be marked invariant; interfaces between "
4917 "shader stages only.", decl->identifier);
4918 } else if (earlier->data.used) {
4919 _mesa_glsl_error(& loc, state,
4920 "variable `%s' may not be redeclared "
4921 "`invariant' after being used",
4922 earlier->name);
4923 } else {
4924 earlier->data.explicit_invariant = true;
4925 earlier->data.invariant = true;
4926 }
4927 }
4928
4929 /* Invariant redeclarations do not have r-values.
4930 */
4931 return NULL__null;
4932 }
4933
4934 if (this->precise) {
4935 assert(this->type == NULL)(static_cast <bool> (this->type == __null) ? void (0
) : __assert_fail ("this->type == NULL", __builtin_FILE ()
, __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
4936
4937 foreach_list_typed (ast_declaration, decl, link, &this->declarations)for (ast_declaration * decl = (!exec_node_is_tail_sentinel((&
this->declarations)->head_sentinel.next) ? ((ast_declaration
*) (((uintptr_t) (&this->declarations)->head_sentinel
.next) - (((char *) &((ast_declaration *) (&this->
declarations)->head_sentinel.next)->link) - ((char *) (
&this->declarations)->head_sentinel.next)))) : __null
); (decl) != __null; (decl) = (!exec_node_is_tail_sentinel((decl
)->link.next) ? ((ast_declaration *) (((uintptr_t) (decl)->
link.next) - (((char *) &((ast_declaration *) (decl)->
link.next)->link) - ((char *) (decl)->link.next)))) : __null
))
{
4938 assert(decl->array_specifier == NULL)(static_cast <bool> (decl->array_specifier == __null
) ? void (0) : __assert_fail ("decl->array_specifier == NULL"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
4939 assert(decl->initializer == NULL)(static_cast <bool> (decl->initializer == __null) ? void
(0) : __assert_fail ("decl->initializer == NULL", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
4940
4941 ir_variable *const earlier =
4942 state->symbols->get_variable(decl->identifier);
4943 if (earlier == NULL__null) {
4944 _mesa_glsl_error(& loc, state,
4945 "undeclared variable `%s' cannot be marked "
4946 "precise", decl->identifier);
4947 } else if (state->current_function != NULL__null &&
4948 !state->symbols->name_declared_this_scope(decl->identifier)) {
4949 /* Note: we have to check if we're in a function, since
4950 * builtins are treated as having come from another scope.
4951 */
4952 _mesa_glsl_error(& loc, state,
4953 "variable `%s' from an outer scope may not be "
4954 "redeclared `precise' in this scope",
4955 earlier->name);
4956 } else if (earlier->data.used) {
4957 _mesa_glsl_error(& loc, state,
4958 "variable `%s' may not be redeclared "
4959 "`precise' after being used",
4960 earlier->name);
4961 } else {
4962 earlier->data.precise = true;
4963 }
4964 }
4965
4966 /* Precise redeclarations do not have r-values either. */
4967 return NULL__null;
4968 }
4969
4970 assert(this->type != NULL)(static_cast <bool> (this->type != __null) ? void (0
) : __assert_fail ("this->type != NULL", __builtin_FILE ()
, __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
4971 assert(!this->invariant)(static_cast <bool> (!this->invariant) ? void (0) : __assert_fail
("!this->invariant", __builtin_FILE (), __builtin_LINE ()
, __extension__ __PRETTY_FUNCTION__))
;
4972 assert(!this->precise)(static_cast <bool> (!this->precise) ? void (0) : __assert_fail
("!this->precise", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
4973
4974 /* GL_EXT_shader_image_load_store base type uses GLSL_TYPE_VOID as a special value to
4975 * indicate that it needs to be updated later (see glsl_parser.yy).
4976 * This is done here, based on the layout qualifier and the type of the image var
4977 */
4978 if (this->type->qualifier.flags.q.explicit_image_format &&
4979 this->type->specifier->type->is_image() &&
4980 this->type->qualifier.image_base_type == GLSL_TYPE_VOID) {
4981 /* "The ARB_shader_image_load_store says:
4982 * If both extensions are enabled in the shading language, the "size*" layout
4983 * qualifiers are treated as format qualifiers, and are mapped to equivalent
4984 * format qualifiers in the table below, according to the type of image
4985 * variable.
4986 * image* iimage* uimage*
4987 * -------- -------- --------
4988 * size1x8 n/a r8i r8ui
4989 * size1x16 r16f r16i r16ui
4990 * size1x32 r32f r32i r32ui
4991 * size2x32 rg32f rg32i rg32ui
4992 * size4x32 rgba32f rgba32i rgba32ui"
4993 */
4994 if (strncmp(this->type->specifier->type_name, "image", strlen("image")) == 0) {
4995 switch (this->type->qualifier.image_format) {
4996 case PIPE_FORMAT_R8_SINT:
4997 /* No valid qualifier in this case, driver will need to look at
4998 * the underlying image's format (just like no qualifier being
4999 * present).
5000 */
5001 this->type->qualifier.image_format = PIPE_FORMAT_NONE;
5002 break;
5003 case PIPE_FORMAT_R16_SINT:
5004 this->type->qualifier.image_format = PIPE_FORMAT_R16_FLOAT;
5005 break;
5006 case PIPE_FORMAT_R32_SINT:
5007 this->type->qualifier.image_format = PIPE_FORMAT_R32_FLOAT;
5008 break;
5009 case PIPE_FORMAT_R32G32_SINT:
5010 this->type->qualifier.image_format = PIPE_FORMAT_R32G32_FLOAT;
5011 break;
5012 case PIPE_FORMAT_R32G32B32A32_SINT:
5013 this->type->qualifier.image_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
5014 break;
5015 default:
5016 unreachable("Unknown image format")do { (static_cast <bool> (!"Unknown image format") ? void
(0) : __assert_fail ("!\"Unknown image format\"", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__)); __builtin_unreachable
(); } while (0)
;
5017 }
5018 this->type->qualifier.image_base_type = GLSL_TYPE_FLOAT;
5019 } else if (strncmp(this->type->specifier->type_name, "uimage", strlen("uimage")) == 0) {
5020 switch (this->type->qualifier.image_format) {
5021 case PIPE_FORMAT_R8_SINT:
5022 this->type->qualifier.image_format = PIPE_FORMAT_R8_UINT;
5023 break;
5024 case PIPE_FORMAT_R16_SINT:
5025 this->type->qualifier.image_format = PIPE_FORMAT_R16_UINT;
5026 break;
5027 case PIPE_FORMAT_R32_SINT:
5028 this->type->qualifier.image_format = PIPE_FORMAT_R32_UINT;
5029 break;
5030 case PIPE_FORMAT_R32G32_SINT:
5031 this->type->qualifier.image_format = PIPE_FORMAT_R32G32_UINT;
5032 break;
5033 case PIPE_FORMAT_R32G32B32A32_SINT:
5034 this->type->qualifier.image_format = PIPE_FORMAT_R32G32B32A32_UINT;
5035 break;
5036 default:
5037 unreachable("Unknown image format")do { (static_cast <bool> (!"Unknown image format") ? void
(0) : __assert_fail ("!\"Unknown image format\"", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__)); __builtin_unreachable
(); } while (0)
;
5038 }
5039 this->type->qualifier.image_base_type = GLSL_TYPE_UINT;
5040 } else if (strncmp(this->type->specifier->type_name, "iimage", strlen("iimage")) == 0) {
5041 this->type->qualifier.image_base_type = GLSL_TYPE_INT;
5042 } else {
5043 assert(false)(static_cast <bool> (false) ? void (0) : __assert_fail (
"false", __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
5044 }
5045 }
5046
5047 /* The type specifier may contain a structure definition. Process that
5048 * before any of the variable declarations.
5049 */
5050 (void) this->type->specifier->hir(instructions, state);
5051
5052 decl_type = this->type->glsl_type(& type_name, state);
5053
5054 /* Section 4.3.7 "Buffer Variables" of the GLSL 4.30 spec:
5055 * "Buffer variables may only be declared inside interface blocks
5056 * (section 4.3.9 “Interface Blocks”), which are then referred to as
5057 * shader storage blocks. It is a compile-time error to declare buffer
5058 * variables at global scope (outside a block)."
5059 */
5060 if (type->qualifier.flags.q.buffer && !decl_type->is_interface()) {
5061 _mesa_glsl_error(&loc, state,
5062 "buffer variables cannot be declared outside "
5063 "interface blocks");
5064 }
5065
5066 /* An offset-qualified atomic counter declaration sets the default
5067 * offset for the next declaration within the same atomic counter
5068 * buffer.
5069 */
5070 if (decl_type && decl_type->contains_atomic()) {
5071 if (type->qualifier.flags.q.explicit_binding &&
5072 type->qualifier.flags.q.explicit_offset) {
5073 unsigned qual_binding;
5074 unsigned qual_offset;
5075 if (process_qualifier_constant(state, &loc, "binding",
5076 type->qualifier.binding,
5077 &qual_binding)
5078 && process_qualifier_constant(state, &loc, "offset",
5079 type->qualifier.offset,
5080 &qual_offset)) {
5081 if (qual_binding < ARRAY_SIZE(state->atomic_counter_offsets)(sizeof(state->atomic_counter_offsets) / sizeof((state->
atomic_counter_offsets)[0]))
)
5082 state->atomic_counter_offsets[qual_binding] = qual_offset;
5083 }
5084 }
5085
5086 ast_type_qualifier allowed_atomic_qual_mask;
5087 allowed_atomic_qual_mask.flags.i = 0;
5088 allowed_atomic_qual_mask.flags.q.explicit_binding = 1;
5089 allowed_atomic_qual_mask.flags.q.explicit_offset = 1;
5090 allowed_atomic_qual_mask.flags.q.uniform = 1;
5091
5092 type->qualifier.validate_flags(&loc, state, allowed_atomic_qual_mask,
5093 "invalid layout qualifier for",
5094 "atomic_uint");
5095 }
5096
5097 if (this->declarations.is_empty()) {
5098 /* If there is no structure involved in the program text, there are two
5099 * possible scenarios:
5100 *
5101 * - The program text contained something like 'vec4;'. This is an
5102 * empty declaration. It is valid but weird. Emit a warning.
5103 *
5104 * - The program text contained something like 'S;' and 'S' is not the
5105 * name of a known structure type. This is both invalid and weird.
5106 * Emit an error.
5107 *
5108 * - The program text contained something like 'mediump float;'
5109 * when the programmer probably meant 'precision mediump
5110 * float;' Emit a warning with a description of what they
5111 * probably meant to do.
5112 *
5113 * Note that if decl_type is NULL and there is a structure involved,
5114 * there must have been some sort of error with the structure. In this
5115 * case we assume that an error was already generated on this line of
5116 * code for the structure. There is no need to generate an additional,
5117 * confusing error.
5118 */
5119 assert(this->type->specifier->structure == NULL || decl_type != NULL(static_cast <bool> (this->type->specifier->structure
== __null || decl_type != __null || state->error) ? void (
0) : __assert_fail ("this->type->specifier->structure == NULL || decl_type != NULL || state->error"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
5120 || state->error)(static_cast <bool> (this->type->specifier->structure
== __null || decl_type != __null || state->error) ? void (
0) : __assert_fail ("this->type->specifier->structure == NULL || decl_type != NULL || state->error"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
5121
5122 if (decl_type == NULL__null) {
5123 _mesa_glsl_error(&loc, state,
5124 "invalid type `%s' in empty declaration",
5125 type_name);
5126 } else {
5127 if (decl_type->is_array()) {
5128 /* From Section 13.22 (Array Declarations) of the GLSL ES 3.2
5129 * spec:
5130 *
5131 * "... any declaration that leaves the size undefined is
5132 * disallowed as this would add complexity and there are no
5133 * use-cases."
5134 */
5135 if (state->es_shader && decl_type->is_unsized_array()) {
5136 _mesa_glsl_error(&loc, state, "array size must be explicitly "
5137 "or implicitly defined");
5138 }
5139
5140 /* From Section 4.12 (Empty Declarations) of the GLSL 4.5 spec:
5141 *
5142 * "The combinations of types and qualifiers that cause
5143 * compile-time or link-time errors are the same whether or not
5144 * the declaration is empty."
5145 */
5146 validate_array_dimensions(decl_type, state, &loc);
5147 }
5148
5149 if (decl_type->is_atomic_uint()) {
5150 /* Empty atomic counter declarations are allowed and useful
5151 * to set the default offset qualifier.
5152 */
5153 return NULL__null;
5154 } else if (this->type->qualifier.precision != ast_precision_none) {
5155 if (this->type->specifier->structure != NULL__null) {
5156 _mesa_glsl_error(&loc, state,
5157 "precision qualifiers can't be applied "
5158 "to structures");
5159 } else {
5160 static const char *const precision_names[] = {
5161 "highp",
5162 "highp",
5163 "mediump",
5164 "lowp"
5165 };
5166
5167 _mesa_glsl_warning(&loc, state,
5168 "empty declaration with precision "
5169 "qualifier, to set the default precision, "
5170 "use `precision %s %s;'",
5171 precision_names[this->type->
5172 qualifier.precision],
5173 type_name);
5174 }
5175 } else if (this->type->specifier->structure == NULL__null) {
5176 _mesa_glsl_warning(&loc, state, "empty declaration");
5177 }
5178 }
5179 }
5180
5181 foreach_list_typed (ast_declaration, decl, link, &this->declarations)for (ast_declaration * decl = (!exec_node_is_tail_sentinel((&
this->declarations)->head_sentinel.next) ? ((ast_declaration
*) (((uintptr_t) (&this->declarations)->head_sentinel
.next) - (((char *) &((ast_declaration *) (&this->
declarations)->head_sentinel.next)->link) - ((char *) (
&this->declarations)->head_sentinel.next)))) : __null
); (decl) != __null; (decl) = (!exec_node_is_tail_sentinel((decl
)->link.next) ? ((ast_declaration *) (((uintptr_t) (decl)->
link.next) - (((char *) &((ast_declaration *) (decl)->
link.next)->link) - ((char *) (decl)->link.next)))) : __null
))
{
5182 const struct glsl_type *var_type;
5183 ir_variable *var;
5184 const char *identifier = decl->identifier;
5185 /* FINISHME: Emit a warning if a variable declaration shadows a
5186 * FINISHME: declaration at a higher scope.
5187 */
5188
5189 if ((decl_type == NULL__null) || decl_type->is_void()) {
5190 if (type_name != NULL__null) {
5191 _mesa_glsl_error(& loc, state,
5192 "invalid type `%s' in declaration of `%s'",
5193 type_name, decl->identifier);
5194 } else {
5195 _mesa_glsl_error(& loc, state,
5196 "invalid type in declaration of `%s'",
5197 decl->identifier);
5198 }
5199 continue;
5200 }
5201
5202 if (this->type->qualifier.is_subroutine_decl()) {
5203 const glsl_type *t;
5204 const char *name;
5205
5206 t = state->symbols->get_type(this->type->specifier->type_name);
5207 if (!t)
5208 _mesa_glsl_error(& loc, state,
5209 "invalid type in declaration of `%s'",
5210 decl->identifier);
5211 name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), decl->identifier);
5212
5213 identifier = name;
5214
5215 }
5216 var_type = process_array_type(&loc, decl_type, decl->array_specifier,
5217 state);
5218
5219 var = new(ctx) ir_variable(var_type, identifier, ir_var_auto);
5220
5221 /* The 'varying in' and 'varying out' qualifiers can only be used with
5222 * ARB_geometry_shader4 and EXT_geometry_shader4, which we don't support
5223 * yet.
5224 */
5225 if (this->type->qualifier.flags.q.varying) {
5226 if (this->type->qualifier.flags.q.in) {
5227 _mesa_glsl_error(& loc, state,
5228 "`varying in' qualifier in declaration of "
5229 "`%s' only valid for geometry shaders using "
5230 "ARB_geometry_shader4 or EXT_geometry_shader4",
5231 decl->identifier);
5232 } else if (this->type->qualifier.flags.q.out) {
5233 _mesa_glsl_error(& loc, state,
5234 "`varying out' qualifier in declaration of "
5235 "`%s' only valid for geometry shaders using "
5236 "ARB_geometry_shader4 or EXT_geometry_shader4",
5237 decl->identifier);
5238 }
5239 }
5240
5241 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
5242 *
5243 * "Global variables can only use the qualifiers const,
5244 * attribute, uniform, or varying. Only one may be
5245 * specified.
5246 *
5247 * Local variables can only use the qualifier const."
5248 *
5249 * This is relaxed in GLSL 1.30 and GLSL ES 3.00. It is also relaxed by
5250 * any extension that adds the 'layout' keyword.
5251 */
5252 if (!state->is_version(130, 300)
5253 && !state->has_explicit_attrib_location()
5254 && !state->has_separate_shader_objects()
5255 && !state->ARB_fragment_coord_conventions_enable) {
5256 /* GL_EXT_gpu_shader4 only allows "varying out" on fragment shader
5257 * outputs. (the varying flag is not set by the parser)
5258 */
5259 if (this->type->qualifier.flags.q.out &&
5260 (!state->EXT_gpu_shader4_enable ||
5261 state->stage != MESA_SHADER_FRAGMENT)) {
5262 _mesa_glsl_error(& loc, state,
5263 "`out' qualifier in declaration of `%s' "
5264 "only valid for function parameters in %s",
5265 decl->identifier, state->get_version_string());
5266 }
5267 if (this->type->qualifier.flags.q.in) {
5268 _mesa_glsl_error(& loc, state,
5269 "`in' qualifier in declaration of `%s' "
5270 "only valid for function parameters in %s",
5271 decl->identifier, state->get_version_string());
5272 }
5273 /* FINISHME: Test for other invalid qualifiers. */
5274 }
5275
5276 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
5277 & loc, false);
5278 apply_layout_qualifier_to_variable(&this->type->qualifier, var, state,
5279 &loc);
5280
5281 if ((var->data.mode == ir_var_auto || var->data.mode == ir_var_temporary
5282 || var->data.mode == ir_var_shader_out)
5283 && (var->type->is_numeric() || var->type->is_boolean())
5284 && state->zero_init) {
5285 const ir_constant_data data = { { 0 } };
5286 var->data.has_initializer = true;
5287 var->constant_initializer = new(var) ir_constant(var->type, &data);
5288 }
5289
5290 if (this->type->qualifier.flags.q.invariant) {
5291 if (!is_allowed_invariant(var, state)) {
5292 _mesa_glsl_error(&loc, state,
5293 "`%s' cannot be marked invariant; interfaces between "
5294 "shader stages only", var->name);
5295 }
5296 }
5297
5298 if (state->current_function != NULL__null) {
5299 const char *mode = NULL__null;
5300 const char *extra = "";
5301
5302 /* There is no need to check for 'inout' here because the parser will
5303 * only allow that in function parameter lists.
5304 */
5305 if (this->type->qualifier.flags.q.attribute) {
5306 mode = "attribute";
5307 } else if (this->type->qualifier.is_subroutine_decl()) {
5308 mode = "subroutine uniform";
5309 } else if (this->type->qualifier.flags.q.uniform) {
5310 mode = "uniform";
5311 } else if (this->type->qualifier.flags.q.varying) {
5312 mode = "varying";
5313 } else if (this->type->qualifier.flags.q.in) {
5314 mode = "in";
5315 extra = " or in function parameter list";
5316 } else if (this->type->qualifier.flags.q.out) {
5317 mode = "out";
5318 extra = " or in function parameter list";
5319 }
5320
5321 if (mode) {
5322 _mesa_glsl_error(& loc, state,
5323 "%s variable `%s' must be declared at "
5324 "global scope%s",
5325 mode, var->name, extra);
5326 }
5327 } else if (var->data.mode == ir_var_shader_in) {
5328 var->data.read_only = true;
5329
5330 if (state->stage == MESA_SHADER_VERTEX) {
5331 bool error_emitted = false;
5332
5333 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
5334 *
5335 * "Vertex shader inputs can only be float, floating-point
5336 * vectors, matrices, signed and unsigned integers and integer
5337 * vectors. Vertex shader inputs can also form arrays of these
5338 * types, but not structures."
5339 *
5340 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
5341 *
5342 * "Vertex shader inputs can only be float, floating-point
5343 * vectors, matrices, signed and unsigned integers and integer
5344 * vectors. They cannot be arrays or structures."
5345 *
5346 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
5347 *
5348 * "The attribute qualifier can be used only with float,
5349 * floating-point vectors, and matrices. Attribute variables
5350 * cannot be declared as arrays or structures."
5351 *
5352 * From page 33 (page 39 of the PDF) of the GLSL ES 3.00 spec:
5353 *
5354 * "Vertex shader inputs can only be float, floating-point
5355 * vectors, matrices, signed and unsigned integers and integer
5356 * vectors. Vertex shader inputs cannot be arrays or
5357 * structures."
5358 *
5359 * From section 4.3.4 of the ARB_bindless_texture spec:
5360 *
5361 * "(modify third paragraph of the section to allow sampler and
5362 * image types) ... Vertex shader inputs can only be float,
5363 * single-precision floating-point scalars, single-precision
5364 * floating-point vectors, matrices, signed and unsigned
5365 * integers and integer vectors, sampler and image types."
5366 */
5367 const glsl_type *check_type = var->type->without_array();
5368
5369 switch (check_type->base_type) {
5370 case GLSL_TYPE_FLOAT:
5371 break;
5372 case GLSL_TYPE_UINT64:
5373 case GLSL_TYPE_INT64:
5374 break;
5375 case GLSL_TYPE_UINT:
5376 case GLSL_TYPE_INT:
5377 if (state->is_version(120, 300) || state->EXT_gpu_shader4_enable)
5378 break;
5379 case GLSL_TYPE_DOUBLE:
5380 if (check_type->is_double() && (state->is_version(410, 0) || state->ARB_vertex_attrib_64bit_enable))
5381 break;
5382 case GLSL_TYPE_SAMPLER:
5383 if (check_type->is_sampler() && state->has_bindless())
5384 break;
5385 case GLSL_TYPE_IMAGE:
5386 if (check_type->is_image() && state->has_bindless())
5387 break;
5388 /* FALLTHROUGH */
5389 default:
5390 _mesa_glsl_error(& loc, state,
5391 "vertex shader input / attribute cannot have "
5392 "type %s`%s'",
5393 var->type->is_array() ? "array of " : "",
5394 check_type->name);
5395 error_emitted = true;
5396 }
5397
5398 if (!error_emitted && var->type->is_array() &&
5399 !state->check_version(150, 0, &loc,
5400 "vertex shader input / attribute "
5401 "cannot have array type")) {
5402 error_emitted = true;
5403 }
5404 } else if (state->stage == MESA_SHADER_GEOMETRY) {
5405 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
5406 *
5407 * Geometry shader input variables get the per-vertex values
5408 * written out by vertex shader output variables of the same
5409 * names. Since a geometry shader operates on a set of
5410 * vertices, each input varying variable (or input block, see
5411 * interface blocks below) needs to be declared as an array.
5412 */
5413 if (!var->type->is_array()) {
5414 _mesa_glsl_error(&loc, state,
5415 "geometry shader inputs must be arrays");
5416 }
5417
5418 handle_geometry_shader_input_decl(state, loc, var);
5419 } else if (state->stage == MESA_SHADER_FRAGMENT) {
5420 /* From section 4.3.4 (Input Variables) of the GLSL ES 3.10 spec:
5421 *
5422 * It is a compile-time error to declare a fragment shader
5423 * input with, or that contains, any of the following types:
5424 *
5425 * * A boolean type
5426 * * An opaque type
5427 * * An array of arrays
5428 * * An array of structures
5429 * * A structure containing an array
5430 * * A structure containing a structure
5431 */
5432 if (state->es_shader) {
5433 const glsl_type *check_type = var->type->without_array();
5434 if (check_type->is_boolean() ||
5435 check_type->contains_opaque()) {
5436 _mesa_glsl_error(&loc, state,
5437 "fragment shader input cannot have type %s",
5438 check_type->name);
5439 }
5440 if (var->type->is_array() &&
5441 var->type->fields.array->is_array()) {
5442 _mesa_glsl_error(&loc, state,
5443 "%s shader output "
5444 "cannot have an array of arrays",
5445 _mesa_shader_stage_to_string(state->stage));
5446 }
5447 if (var->type->is_array() &&
5448 var->type->fields.array->is_struct()) {
5449 _mesa_glsl_error(&loc, state,
5450 "fragment shader input "
5451 "cannot have an array of structs");
5452 }
5453 if (var->type->is_struct()) {
5454 for (unsigned i = 0; i < var->type->length; i++) {
5455 if (var->type->fields.structure[i].type->is_array() ||
5456 var->type->fields.structure[i].type->is_struct())
5457 _mesa_glsl_error(&loc, state,
5458 "fragment shader input cannot have "
5459 "a struct that contains an "
5460 "array or struct");
5461 }
5462 }
5463 }
5464 } else if (state->stage == MESA_SHADER_TESS_CTRL ||
5465 state->stage == MESA_SHADER_TESS_EVAL) {
5466 handle_tess_shader_input_decl(state, loc, var);
5467 }
5468 } else if (var->data.mode == ir_var_shader_out) {
5469 const glsl_type *check_type = var->type->without_array();
5470
5471 /* From section 4.3.6 (Output variables) of the GLSL 4.40 spec:
5472 *
5473 * It is a compile-time error to declare a fragment shader output
5474 * that contains any of the following:
5475 *
5476 * * A Boolean type (bool, bvec2 ...)
5477 * * A double-precision scalar or vector (double, dvec2 ...)
5478 * * An opaque type
5479 * * Any matrix type
5480 * * A structure
5481 */
5482 if (state->stage == MESA_SHADER_FRAGMENT) {
5483 if (check_type->is_struct() || check_type->is_matrix())
5484 _mesa_glsl_error(&loc, state,
5485 "fragment shader output "
5486 "cannot have struct or matrix type");
5487 switch (check_type->base_type) {
5488 case GLSL_TYPE_UINT:
5489 case GLSL_TYPE_INT:
5490 case GLSL_TYPE_FLOAT:
5491 break;
5492 default:
5493 _mesa_glsl_error(&loc, state,
5494 "fragment shader output cannot have "
5495 "type %s", check_type->name);
5496 }
5497 }
5498
5499 /* From section 4.3.6 (Output Variables) of the GLSL ES 3.10 spec:
5500 *
5501 * It is a compile-time error to declare a vertex shader output
5502 * with, or that contains, any of the following types:
5503 *
5504 * * A boolean type
5505 * * An opaque type
5506 * * An array of arrays
5507 * * An array of structures
5508 * * A structure containing an array
5509 * * A structure containing a structure
5510 *
5511 * It is a compile-time error to declare a fragment shader output
5512 * with, or that contains, any of the following types:
5513 *
5514 * * A boolean type
5515 * * An opaque type
5516 * * A matrix
5517 * * A structure
5518 * * An array of array
5519 *
5520 * ES 3.20 updates this to apply to tessellation and geometry shaders
5521 * as well. Because there are per-vertex arrays in the new stages,
5522 * it strikes the "array of..." rules and replaces them with these:
5523 *
5524 * * For per-vertex-arrayed variables (applies to tessellation
5525 * control, tessellation evaluation and geometry shaders):
5526 *
5527 * * Per-vertex-arrayed arrays of arrays
5528 * * Per-vertex-arrayed arrays of structures
5529 *
5530 * * For non-per-vertex-arrayed variables:
5531 *
5532 * * An array of arrays
5533 * * An array of structures
5534 *
5535 * which basically says to unwrap the per-vertex aspect and apply
5536 * the old rules.
5537 */
5538 if (state->es_shader) {
5539 if (var->type->is_array() &&
5540 var->type->fields.array->is_array()) {
5541 _mesa_glsl_error(&loc, state,
5542 "%s shader output "
5543 "cannot have an array of arrays",
5544 _mesa_shader_stage_to_string(state->stage));
5545 }
5546 if (state->stage <= MESA_SHADER_GEOMETRY) {
5547 const glsl_type *type = var->type;
5548
5549 if (state->stage == MESA_SHADER_TESS_CTRL &&
5550 !var->data.patch && var->type->is_array()) {
5551 type = var->type->fields.array;
5552 }
5553
5554 if (type->is_array() && type->fields.array->is_struct()) {
5555 _mesa_glsl_error(&loc, state,
5556 "%s shader output cannot have "
5557 "an array of structs",
5558 _mesa_shader_stage_to_string(state->stage));
5559 }
5560 if (type->is_struct()) {
5561 for (unsigned i = 0; i < type->length; i++) {
5562 if (type->fields.structure[i].type->is_array() ||
5563 type->fields.structure[i].type->is_struct())
5564 _mesa_glsl_error(&loc, state,
5565 "%s shader output cannot have a "
5566 "struct that contains an "
5567 "array or struct",
5568 _mesa_shader_stage_to_string(state->stage));
5569 }
5570 }
5571 }
5572 }
5573
5574 if (state->stage == MESA_SHADER_TESS_CTRL) {
5575 handle_tess_ctrl_shader_output_decl(state, loc, var);
5576 }
5577 } else if (var->type->contains_subroutine()) {
5578 /* declare subroutine uniforms as hidden */
5579 var->data.how_declared = ir_var_hidden;
5580 }
5581
5582 /* From section 4.3.4 of the GLSL 4.00 spec:
5583 * "Input variables may not be declared using the patch in qualifier
5584 * in tessellation control or geometry shaders."
5585 *
5586 * From section 4.3.6 of the GLSL 4.00 spec:
5587 * "It is an error to use patch out in a vertex, tessellation
5588 * evaluation, or geometry shader."
5589 *
5590 * This doesn't explicitly forbid using them in a fragment shader, but
5591 * that's probably just an oversight.
5592 */
5593 if (state->stage != MESA_SHADER_TESS_EVAL
5594 && this->type->qualifier.flags.q.patch
5595 && this->type->qualifier.flags.q.in) {
5596
5597 _mesa_glsl_error(&loc, state, "'patch in' can only be used in a "
5598 "tessellation evaluation shader");
5599 }
5600
5601 if (state->stage != MESA_SHADER_TESS_CTRL
5602 && this->type->qualifier.flags.q.patch
5603 && this->type->qualifier.flags.q.out) {
5604
5605 _mesa_glsl_error(&loc, state, "'patch out' can only be used in a "
5606 "tessellation control shader");
5607 }
5608
5609 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
5610 */
5611 if (this->type->qualifier.precision != ast_precision_none) {
5612 state->check_precision_qualifiers_allowed(&loc);
5613 }
5614
5615 if (this->type->qualifier.precision != ast_precision_none &&
5616 !precision_qualifier_allowed(var->type)) {
5617 _mesa_glsl_error(&loc, state,
5618 "precision qualifiers apply only to floating point"
5619 ", integer and opaque types");
5620 }
5621
5622 /* From section 4.1.7 of the GLSL 4.40 spec:
5623 *
5624 * "[Opaque types] can only be declared as function
5625 * parameters or uniform-qualified variables."
5626 *
5627 * From section 4.1.7 of the ARB_bindless_texture spec:
5628 *
5629 * "Samplers may be declared as shader inputs and outputs, as uniform
5630 * variables, as temporary variables, and as function parameters."
5631 *
5632 * From section 4.1.X of the ARB_bindless_texture spec:
5633 *
5634 * "Images may be declared as shader inputs and outputs, as uniform
5635 * variables, as temporary variables, and as function parameters."
5636 */
5637 if (!this->type->qualifier.flags.q.uniform &&
5638 (var_type->contains_atomic() ||
5639 (!state->has_bindless() && var_type->contains_opaque()))) {
5640 _mesa_glsl_error(&loc, state,
5641 "%s variables must be declared uniform",
5642 state->has_bindless() ? "atomic" : "opaque");
5643 }
5644
5645 /* Process the initializer and add its instructions to a temporary
5646 * list. This list will be added to the instruction stream (below) after
5647 * the declaration is added. This is done because in some cases (such as
5648 * redeclarations) the declaration may not actually be added to the
5649 * instruction stream.
5650 */
5651 exec_list initializer_instructions;
5652
5653 /* Examine var name here since var may get deleted in the next call */
5654 bool var_is_gl_id = is_gl_identifier(var->name);
5655
5656 bool is_redeclaration;
5657 var = get_variable_being_redeclared(&var, decl->get_location(), state,
5658 false /* allow_all_redeclarations */,
5659 &is_redeclaration);
5660 if (is_redeclaration) {
5661 if (var_is_gl_id &&
5662 var->data.how_declared == ir_var_declared_in_block) {
5663 _mesa_glsl_error(&loc, state,
5664 "`%s' has already been redeclared using "
5665 "gl_PerVertex", var->name);
5666 }
5667 var->data.how_declared = ir_var_declared_normally;
5668 }
5669
5670 if (decl->initializer != NULL__null) {
5671 result = process_initializer(var,
5672 decl, this->type,
5673 &initializer_instructions, state);
5674 } else {
5675 validate_array_dimensions(var_type, state, &loc);
5676 }
5677
5678 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
5679 *
5680 * "It is an error to write to a const variable outside of
5681 * its declaration, so they must be initialized when
5682 * declared."
5683 */
5684 if (this->type->qualifier.flags.q.constant && decl->initializer == NULL__null) {
5685 _mesa_glsl_error(& loc, state,
5686 "const declaration of `%s' must be initialized",
5687 decl->identifier);
5688 }
5689
5690 if (state->es_shader) {
5691 const glsl_type *const t = var->type;
5692
5693 /* Skip the unsized array check for TCS/TES/GS inputs & TCS outputs.
5694 *
5695 * The GL_OES_tessellation_shader spec says about inputs:
5696 *
5697 * "Declaring an array size is optional. If no size is specified,
5698 * it will be taken from the implementation-dependent maximum
5699 * patch size (gl_MaxPatchVertices)."
5700 *
5701 * and about TCS outputs:
5702 *
5703 * "If no size is specified, it will be taken from output patch
5704 * size declared in the shader."
5705 *
5706 * The GL_OES_geometry_shader spec says:
5707 *
5708 * "All geometry shader input unsized array declarations will be
5709 * sized by an earlier input primitive layout qualifier, when
5710 * present, as per the following table."
5711 */
5712 const bool implicitly_sized =
5713 (var->data.mode == ir_var_shader_in &&
5714 state->stage >= MESA_SHADER_TESS_CTRL &&
5715 state->stage <= MESA_SHADER_GEOMETRY) ||
5716 (var->data.mode == ir_var_shader_out &&
5717 state->stage == MESA_SHADER_TESS_CTRL);
5718
5719 if (t->is_unsized_array() && !implicitly_sized)
5720 /* Section 10.17 of the GLSL ES 1.00 specification states that
5721 * unsized array declarations have been removed from the language.
5722 * Arrays that are sized using an initializer are still explicitly
5723 * sized. However, GLSL ES 1.00 does not allow array
5724 * initializers. That is only allowed in GLSL ES 3.00.
5725 *
5726 * Section 4.1.9 (Arrays) of the GLSL ES 3.00 spec says:
5727 *
5728 * "An array type can also be formed without specifying a size
5729 * if the definition includes an initializer:
5730 *
5731 * float x[] = float[2] (1.0, 2.0); // declares an array of size 2
5732 * float y[] = float[] (1.0, 2.0, 3.0); // declares an array of size 3
5733 *
5734 * float a[5];
5735 * float b[] = a;"
5736 */
5737 _mesa_glsl_error(& loc, state,
5738 "unsized array declarations are not allowed in "
5739 "GLSL ES");
5740 }
5741
5742 /* Section 4.4.6.1 Atomic Counter Layout Qualifiers of the GLSL 4.60 spec:
5743 *
5744 * "It is a compile-time error to declare an unsized array of
5745 * atomic_uint"
5746 */
5747 if (var->type->is_unsized_array() &&
5748 var->type->without_array()->base_type == GLSL_TYPE_ATOMIC_UINT) {
5749 _mesa_glsl_error(& loc, state,
5750 "Unsized array of atomic_uint is not allowed");
5751 }
5752
5753 /* If the declaration is not a redeclaration, there are a few additional
5754 * semantic checks that must be applied. In addition, variable that was
5755 * created for the declaration should be added to the IR stream.
5756 */
5757 if (!is_redeclaration) {
5758 validate_identifier(decl->identifier, loc, state);
5759
5760 /* Add the variable to the symbol table. Note that the initializer's
5761 * IR was already processed earlier (though it hasn't been emitted
5762 * yet), without the variable in scope.
5763 *
5764 * This differs from most C-like languages, but it follows the GLSL
5765 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
5766 * spec:
5767 *
5768 * "Within a declaration, the scope of a name starts immediately
5769 * after the initializer if present or immediately after the name
5770 * being declared if not."
5771 */
5772 if (!state->symbols->add_variable(var)) {
5773 YYLTYPE loc = this->get_location();
5774 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
5775 "current scope", decl->identifier);
5776 continue;
5777 }
5778
5779 /* Push the variable declaration to the top. It means that all the
5780 * variable declarations will appear in a funny last-to-first order,
5781 * but otherwise we run into trouble if a function is prototyped, a
5782 * global var is decled, then the function is defined with usage of
5783 * the global var. See glslparsertest's CorrectModule.frag.
5784 * However, do not insert declarations before default precision statements
5785 * or type declarations.
5786 */
5787 ir_instruction* before_node = (ir_instruction*)instructions->get_head();
5788 while (before_node && (before_node->ir_type == ir_type_precision || before_node->ir_type == ir_type_typedecl))
5789 before_node = (ir_instruction*)before_node->next;
5790 if (before_node)
5791 before_node->insert_before(var);
5792 else
5793 instructions->push_head(var);
5794 }
5795
5796 instructions->append_list(&initializer_instructions);
5797 }
5798
5799
5800 /* Generally, variable declarations do not have r-values. However,
5801 * one is used for the declaration in
5802 *
5803 * while (bool b = some_condition()) {
5804 * ...
5805 * }
5806 *
5807 * so we return the rvalue from the last seen declaration here.
5808 */
5809 return result;
5810}
5811
5812
5813ir_rvalue *
5814ast_parameter_declarator::hir(exec_list *instructions,
5815 struct _mesa_glsl_parse_state *state)
5816{
5817 void *ctx = state;
5818 const struct glsl_type *type;
5819 const char *name = NULL__null;
5820 YYLTYPE loc = this->get_location();
5821
5822 type = this->type->glsl_type(& name, state);
5823
5824 if (type == NULL__null) {
5825 if (name != NULL__null) {
5826 _mesa_glsl_error(& loc, state,
5827 "invalid type `%s' in declaration of `%s'",
5828 name, this->identifier);
5829 } else {
5830 _mesa_glsl_error(& loc, state,
5831 "invalid type in declaration of `%s'",
5832 this->identifier);
5833 }
5834
5835 type = glsl_type::error_type;
5836 }
5837
5838 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
5839 *
5840 * "Functions that accept no input arguments need not use void in the
5841 * argument list because prototypes (or definitions) are required and
5842 * therefore there is no ambiguity when an empty argument list "( )" is
5843 * declared. The idiom "(void)" as a parameter list is provided for
5844 * convenience."
5845 *
5846 * Placing this check here prevents a void parameter being set up
5847 * for a function, which avoids tripping up checks for main taking
5848 * parameters and lookups of an unnamed symbol.
5849 */
5850 if (type->is_void()) {
5851 if (this->identifier != NULL__null)
5852 _mesa_glsl_error(& loc, state,
5853 "named parameter cannot have type `void'");
5854
5855 is_void = true;
5856 return NULL__null;
5857 }
5858
5859 if (formal_parameter && (this->identifier == NULL__null)) {
5860 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
5861 return NULL__null;
5862 }
5863
5864 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
5865 * call already handled the "vec4[..] foo" case.
5866 */
5867 type = process_array_type(&loc, type, this->array_specifier, state);
5868
5869 if (!type->is_error() && type->is_unsized_array()) {
5870 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
5871 "a declared size");
5872 type = glsl_type::error_type;
5873 }
5874
5875 is_void = false;
5876 ir_variable *var = new(ctx)
5877 ir_variable(type, this->identifier, ir_var_function_in);
5878
5879 /* Apply any specified qualifiers to the parameter declaration. Note that
5880 * for function parameters the default mode is 'in'.
5881 */
5882 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc,
5883 true);
5884
5885 /* From section 4.1.7 of the GLSL 4.40 spec:
5886 *
5887 * "Opaque variables cannot be treated as l-values; hence cannot
5888 * be used as out or inout function parameters, nor can they be
5889 * assigned into."
5890 *
5891 * From section 4.1.7 of the ARB_bindless_texture spec:
5892 *
5893 * "Samplers can be used as l-values, so can be assigned into and used
5894 * as "out" and "inout" function parameters."
5895 *
5896 * From section 4.1.X of the ARB_bindless_texture spec:
5897 *
5898 * "Images can be used as l-values, so can be assigned into and used as
5899 * "out" and "inout" function parameters."
5900 */
5901 if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out)
5902 && (type->contains_atomic() ||
5903 (!state->has_bindless() && type->contains_opaque()))) {
5904 _mesa_glsl_error(&loc, state, "out and inout parameters cannot "
5905 "contain %s variables",
5906 state->has_bindless() ? "atomic" : "opaque");
5907 type = glsl_type::error_type;
5908 }
5909
5910 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
5911 *
5912 * "When calling a function, expressions that do not evaluate to
5913 * l-values cannot be passed to parameters declared as out or inout."
5914 *
5915 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
5916 *
5917 * "Other binary or unary expressions, non-dereferenced arrays,
5918 * function names, swizzles with repeated fields, and constants
5919 * cannot be l-values."
5920 *
5921 * So for GLSL 1.10, passing an array as an out or inout parameter is not
5922 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
5923 */
5924 if ((var->data.mode == ir_var_function_inout || var->data.mode == ir_var_function_out)
5925 && type->is_array()
5926 && !state->check_version(120, 100, &loc,
5927 "arrays cannot be out or inout parameters")) {
5928 type = glsl_type::error_type;
5929 }
5930
5931 instructions->push_tail(var);
5932
5933 /* Parameter declarations do not have r-values.
5934 */
5935 return NULL__null;
5936}
5937
5938
5939void
5940ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
5941 bool formal,
5942 exec_list *ir_parameters,
5943 _mesa_glsl_parse_state *state)
5944{
5945 ast_parameter_declarator *void_param = NULL__null;
5946 unsigned count = 0;
5947
5948 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters)for (ast_parameter_declarator * param = (!exec_node_is_tail_sentinel
((ast_parameters)->head_sentinel.next) ? ((ast_parameter_declarator
*) (((uintptr_t) (ast_parameters)->head_sentinel.next) - (
((char *) &((ast_parameter_declarator *) (ast_parameters)
->head_sentinel.next)->link) - ((char *) (ast_parameters
)->head_sentinel.next)))) : __null); (param) != __null; (param
) = (!exec_node_is_tail_sentinel((param)->link.next) ? ((ast_parameter_declarator
*) (((uintptr_t) (param)->link.next) - (((char *) &((
ast_parameter_declarator *) (param)->link.next)->link) -
((char *) (param)->link.next)))) : __null))
{
5949 param->formal_parameter = formal;
5950 param->hir(ir_parameters, state);
5951
5952 if (param->is_void)
5953 void_param = param;
5954
5955 count++;
5956 }
5957
5958 if ((void_param != NULL__null) && (count > 1)) {
5959 YYLTYPE loc = void_param->get_location();
5960
5961 _mesa_glsl_error(& loc, state,
5962 "`void' parameter must be only parameter");
5963 }
5964}
5965
5966
5967void
5968emit_function(_mesa_glsl_parse_state *state, ir_function *f)
5969{
5970 /* IR invariants disallow function declarations or definitions
5971 * nested within other function definitions. But there is no
5972 * requirement about the relative order of function declarations
5973 * and definitions with respect to one another. So simply insert
5974 * the new ir_function block at the end of the toplevel instruction
5975 * list.
5976 */
5977 state->toplevel_ir->push_tail(f);
5978}
5979
5980
5981ir_rvalue *
5982ast_function::hir(exec_list *instructions,
5983 struct _mesa_glsl_parse_state *state)
5984{
5985 void *ctx = state;
5986 ir_function *f = NULL__null;
5987 ir_function_signature *sig = NULL__null;
5988 exec_list hir_parameters;
5989 YYLTYPE loc = this->get_location();
5990
5991 const char *const name = identifier;
5992
5993 /* New functions are always added to the top-level IR instruction stream,
5994 * so this instruction list pointer is ignored. See also emit_function
5995 * (called below).
5996 */
5997 (void) instructions;
5998
5999 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
6000 *
6001 * "Function declarations (prototypes) cannot occur inside of functions;
6002 * they must be at global scope, or for the built-in functions, outside
6003 * the global scope."
6004 *
6005 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
6006 *
6007 * "User defined functions may only be defined within the global scope."
6008 *
6009 * Note that this language does not appear in GLSL 1.10.
6010 */
6011 if ((state->current_function != NULL__null) &&
6012 state->is_version(120, 100)) {
6013 YYLTYPE loc = this->get_location();
6014 _mesa_glsl_error(&loc, state,
6015 "declaration of function `%s' not allowed within "
6016 "function body", name);
6017 }
6018
6019 validate_identifier(name, this->get_location(), state);
6020
6021 /* Convert the list of function parameters to HIR now so that they can be
6022 * used below to compare this function's signature with previously seen
6023 * signatures for functions with the same name.
6024 */
6025 ast_parameter_declarator::parameters_to_hir(& this->parameters,
6026 is_definition,
6027 & hir_parameters, state);
6028
6029 const char *return_type_name;
6030 const glsl_type *return_type =
6031 this->return_type->glsl_type(& return_type_name, state);
6032
6033 if (!return_type) {
6034 YYLTYPE loc = this->get_location();
6035 _mesa_glsl_error(&loc, state,
6036 "function `%s' has undeclared return type `%s'",
6037 name, return_type_name);
6038 return_type = glsl_type::error_type;
6039 }
6040
6041 /* ARB_shader_subroutine states:
6042 * "Subroutine declarations cannot be prototyped. It is an error to prepend
6043 * subroutine(...) to a function declaration."
6044 */
6045 if (this->return_type->qualifier.subroutine_list && !is_definition) {
6046 YYLTYPE loc = this->get_location();
6047 _mesa_glsl_error(&loc, state,
6048 "function declaration `%s' cannot have subroutine prepended",
6049 name);
6050 }
6051
6052 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
6053 * "No qualifier is allowed on the return type of a function."
6054 */
6055 if (this->return_type->has_qualifiers(state)) {
6056 YYLTYPE loc = this->get_location();
6057 _mesa_glsl_error(& loc, state,
6058 "function `%s' return type has qualifiers", name);
6059 }
6060
6061 /* Section 6.1 (Function Definitions) of the GLSL 1.20 spec says:
6062 *
6063 * "Arrays are allowed as arguments and as the return type. In both
6064 * cases, the array must be explicitly sized."
6065 */
6066 if (return_type->is_unsized_array()) {
6067 YYLTYPE loc = this->get_location();
6068 _mesa_glsl_error(& loc, state,
6069 "function `%s' return type array must be explicitly "
6070 "sized", name);
6071 }
6072
6073 /* From Section 6.1 (Function Definitions) of the GLSL 1.00 spec:
6074 *
6075 * "Arrays are allowed as arguments, but not as the return type. [...]
6076 * The return type can also be a structure if the structure does not
6077 * contain an array."
6078 */
6079 if (state->language_version == 100 && return_type->contains_array()) {
6080 YYLTYPE loc = this->get_location();
6081 _mesa_glsl_error(& loc, state,
6082 "function `%s' return type contains an array", name);
6083 }
6084
6085 /* From section 4.1.7 of the GLSL 4.40 spec:
6086 *
6087 * "[Opaque types] can only be declared as function parameters
6088 * or uniform-qualified variables."
6089 *
6090 * The ARB_bindless_texture spec doesn't clearly state this, but as it says
6091 * "Replace Section 4.1.7 (Samplers), p. 25" and, "Replace Section 4.1.X,
6092 * (Images)", this should be allowed.
6093 */
6094 if (return_type->contains_atomic() ||
6095 (!state->has_bindless() && return_type->contains_opaque())) {
6096 YYLTYPE loc = this->get_location();
6097 _mesa_glsl_error(&loc, state,
6098 "function `%s' return type can't contain an %s type",
6099 name, state->has_bindless() ? "atomic" : "opaque");
6100 }
6101
6102 /**/
6103 if (return_type->is_subroutine()) {
6104 YYLTYPE loc = this->get_location();
6105 _mesa_glsl_error(&loc, state,
6106 "function `%s' return type can't be a subroutine type",
6107 name);
6108 }
6109
6110 /* Get the precision for the return type */
6111 unsigned return_precision;
6112
6113 if (state->es_shader) {
6114 YYLTYPE loc = this->get_location();
6115 return_precision =
6116 select_gles_precision(this->return_type->qualifier.precision,
6117 return_type,
6118 state,
6119 &loc);
6120 } else {
6121 return_precision = GLSL_PRECISION_NONE;
6122 }
6123
6124 /* Create an ir_function if one doesn't already exist. */
6125 f = state->symbols->get_function(name);
6126 if (f == NULL__null) {
6127 f = new(ctx) ir_function(name);
6128 if (!this->return_type->qualifier.is_subroutine_decl()) {
6129 if (!state->symbols->add_function(f)) {
6130 /* This function name shadows a non-function use of the same name. */
6131 YYLTYPE loc = this->get_location();
6132 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
6133 "non-function", name);
6134 return NULL__null;
6135 }
6136 }
6137 emit_function(state, f);
6138 }
6139
6140 /* From GLSL ES 3.0 spec, chapter 6.1 "Function Definitions", page 71:
6141 *
6142 * "A shader cannot redefine or overload built-in functions."
6143 *
6144 * While in GLSL ES 1.0 specification, chapter 8 "Built-in Functions":
6145 *
6146 * "User code can overload the built-in functions but cannot redefine
6147 * them."
6148 */
6149 if (state->es_shader) {
6150 /* Local shader has no exact candidates; check the built-ins. */
6151 if (state->language_version >= 300 &&
6152 _mesa_glsl_has_builtin_function(state, name)) {
6153 YYLTYPE loc = this->get_location();
6154 _mesa_glsl_error(& loc, state,
6155 "A shader cannot redefine or overload built-in "
6156 "function `%s' in GLSL ES 3.00", name);
6157 return NULL__null;
6158 }
6159
6160 if (state->language_version == 100) {
6161 ir_function_signature *sig =
6162 _mesa_glsl_find_builtin_function(state, name, &hir_parameters);
6163 if (sig && sig->is_builtin()) {
6164 _mesa_glsl_error(& loc, state,
6165 "A shader cannot redefine built-in "
6166 "function `%s' in GLSL ES 1.00", name);
6167 }
6168 }
6169 }
6170
6171 /* Verify that this function's signature either doesn't match a previously
6172 * seen signature for a function with the same name, or, if a match is found,
6173 * that the previously seen signature does not have an associated definition.
6174 */
6175 if (state->es_shader || f->has_user_signature()) {
6176 sig = f->exact_matching_signature(state, &hir_parameters);
6177 if (sig != NULL__null) {
6178 const char *badvar = sig->qualifiers_match(&hir_parameters);
6179 if (badvar != NULL__null) {
6180 YYLTYPE loc = this->get_location();
6181
6182 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
6183 "qualifiers don't match prototype", name, badvar);
6184 }
6185
6186 if (sig->return_type != return_type) {
6187 YYLTYPE loc = this->get_location();
6188
6189 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
6190 "match prototype", name);
6191 }
6192
6193 if (sig->return_precision != return_precision) {
6194 YYLTYPE loc = this->get_location();
6195
6196 _mesa_glsl_error(&loc, state, "function `%s' return type precision "
6197 "doesn't match prototype", name);
6198 }
6199
6200 if (sig->is_defined) {
6201 if (is_definition) {
6202 YYLTYPE loc = this->get_location();
6203 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
6204 } else {
6205 /* We just encountered a prototype that exactly matches a
6206 * function that's already been defined. This is redundant,
6207 * and we should ignore it.
6208 */
6209 return NULL__null;
6210 }
6211 } else if (state->language_version == 100 && !is_definition) {
6212 /* From the GLSL 1.00 spec, section 4.2.7:
6213 *
6214 * "A particular variable, structure or function declaration
6215 * may occur at most once within a scope with the exception
6216 * that a single function prototype plus the corresponding
6217 * function definition are allowed."
6218 */
6219 YYLTYPE loc = this->get_location();
6220 _mesa_glsl_error(&loc, state, "function `%s' redeclared", name);
6221 }
6222 }
6223 }
6224
6225 /* Verify the return type of main() */
6226 if (strcmp(name, "main") == 0) {
6227 if (! return_type->is_void()) {
6228 YYLTYPE loc = this->get_location();
6229
6230 _mesa_glsl_error(& loc, state, "main() must return void");
6231 }
6232
6233 if (!hir_parameters.is_empty()) {
6234 YYLTYPE loc = this->get_location();
6235
6236 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
6237 }
6238 }
6239
6240 /* Finish storing the information about this new function in its signature.
6241 */
6242 if (sig == NULL__null) {
6243 sig = new(ctx) ir_function_signature(return_type);
6244 sig->return_precision = return_precision;
6245 f->add_signature(sig);
6246 }
6247
6248 sig->replace_parameters(&hir_parameters);
6249 signature = sig;
6250
6251 if (this->return_type->qualifier.subroutine_list) {
6252 int idx;
6253
6254 if (this->return_type->qualifier.flags.q.explicit_index) {
6255 unsigned qual_index;
6256 if (process_qualifier_constant(state, &loc, "index",
6257 this->return_type->qualifier.index,
6258 &qual_index)) {
6259 if (!state->has_explicit_uniform_location()) {
6260 _mesa_glsl_error(&loc, state, "subroutine index requires "
6261 "GL_ARB_explicit_uniform_location or "
6262 "GLSL 4.30");
6263 } else if (qual_index >= MAX_SUBROUTINES256) {
6264 _mesa_glsl_error(&loc, state,
6265 "invalid subroutine index (%d) index must "
6266 "be a number between 0 and "
6267 "GL_MAX_SUBROUTINES - 1 (%d)", qual_index,
6268 MAX_SUBROUTINES256 - 1);
6269 } else {
6270 f->subroutine_index = qual_index;
6271 }
6272 }
6273 }
6274
6275 f->num_subroutine_types = this->return_type->qualifier.subroutine_list->declarations.length();
6276 f->subroutine_types = ralloc_array(state, const struct glsl_type *,((const struct glsl_type * *) ralloc_array_size(state, sizeof
(const struct glsl_type *), f->num_subroutine_types))
6277 f->num_subroutine_types)((const struct glsl_type * *) ralloc_array_size(state, sizeof
(const struct glsl_type *), f->num_subroutine_types))
;
6278 idx = 0;
6279 foreach_list_typed(ast_declaration, decl, link, &this->return_type->qualifier.subroutine_list->declarations)for (ast_declaration * decl = (!exec_node_is_tail_sentinel((&
this->return_type->qualifier.subroutine_list->declarations
)->head_sentinel.next) ? ((ast_declaration *) (((uintptr_t
) (&this->return_type->qualifier.subroutine_list->
declarations)->head_sentinel.next) - (((char *) &((ast_declaration
*) (&this->return_type->qualifier.subroutine_list->
declarations)->head_sentinel.next)->link) - ((char *) (
&this->return_type->qualifier.subroutine_list->declarations
)->head_sentinel.next)))) : __null); (decl) != __null; (decl
) = (!exec_node_is_tail_sentinel((decl)->link.next) ? ((ast_declaration
*) (((uintptr_t) (decl)->link.next) - (((char *) &((ast_declaration
*) (decl)->link.next)->link) - ((char *) (decl)->link
.next)))) : __null))
{
6280 const struct glsl_type *type;
6281 /* the subroutine type must be already declared */
6282 type = state->symbols->get_type(decl->identifier);
6283 if (!type) {
6284 _mesa_glsl_error(& loc, state, "unknown type '%s' in subroutine function definition", decl->identifier);
6285 }
6286
6287 for (int i = 0; i < state->num_subroutine_types; i++) {
6288 ir_function *fn = state->subroutine_types[i];
6289 ir_function_signature *tsig = NULL__null;
6290
6291 if (strcmp(fn->name, decl->identifier))
6292 continue;
6293
6294 tsig = fn->matching_signature(state, &sig->parameters,
6295 false);
6296 if (!tsig) {
6297 _mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - signatures do not match\n", decl->identifier);
6298 } else {
6299 if (tsig->return_type != sig->return_type) {
6300 _mesa_glsl_error(& loc, state, "subroutine type mismatch '%s' - return types do not match\n", decl->identifier);
6301 }
6302 }
6303 }
6304 f->subroutine_types[idx++] = type;
6305 }
6306 state->subroutines = (ir_function **)reralloc(state, state->subroutines,((ir_function * *) reralloc_array_size(state, state->subroutines
, sizeof(ir_function *), state->num_subroutines + 1))
6307 ir_function *,((ir_function * *) reralloc_array_size(state, state->subroutines
, sizeof(ir_function *), state->num_subroutines + 1))
6308 state->num_subroutines + 1)((ir_function * *) reralloc_array_size(state, state->subroutines
, sizeof(ir_function *), state->num_subroutines + 1))
;
6309 state->subroutines[state->num_subroutines] = f;
6310 state->num_subroutines++;
6311
6312 }
6313
6314 if (this->return_type->qualifier.is_subroutine_decl()) {
6315 if (!state->symbols->add_type(this->identifier, glsl_type::get_subroutine_instance(this->identifier))) {
6316 _mesa_glsl_error(& loc, state, "type '%s' previously defined", this->identifier);
6317 return NULL__null;
6318 }
6319 state->subroutine_types = (ir_function **)reralloc(state, state->subroutine_types,((ir_function * *) reralloc_array_size(state, state->subroutine_types
, sizeof(ir_function *), state->num_subroutine_types + 1))
6320 ir_function *,((ir_function * *) reralloc_array_size(state, state->subroutine_types
, sizeof(ir_function *), state->num_subroutine_types + 1))
6321 state->num_subroutine_types + 1)((ir_function * *) reralloc_array_size(state, state->subroutine_types
, sizeof(ir_function *), state->num_subroutine_types + 1))
;
6322 state->subroutine_types[state->num_subroutine_types] = f;
6323 state->num_subroutine_types++;
6324
6325 f->is_subroutine = true;
6326 }
6327
6328 /* Function declarations (prototypes) do not have r-values.
6329 */
6330 return NULL__null;
6331}
6332
6333
6334ir_rvalue *
6335ast_function_definition::hir(exec_list *instructions,
6336 struct _mesa_glsl_parse_state *state)
6337{
6338 prototype->is_definition = true;
6339 prototype->hir(instructions, state);
6340
6341 ir_function_signature *signature = prototype->signature;
6342 if (signature == NULL__null)
6343 return NULL__null;
6344
6345 assert(state->current_function == NULL)(static_cast <bool> (state->current_function == __null
) ? void (0) : __assert_fail ("state->current_function == NULL"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
6346 state->current_function = signature;
6347 state->found_return = false;
6348 state->found_begin_interlock = false;
6349 state->found_end_interlock = false;
6350
6351 /* Duplicate parameters declared in the prototype as concrete variables.
6352 * Add these to the symbol table.
6353 */
6354 state->symbols->push_scope();
6355 foreach_in_list(ir_variable, var, &signature->parameters)for (ir_variable *var = (!exec_node_is_tail_sentinel((&signature
->parameters)->head_sentinel.next) ? (ir_variable *) ((
&signature->parameters)->head_sentinel.next) : __null
); (var) != __null; (var) = (!exec_node_is_tail_sentinel((var
)->next) ? (ir_variable *) ((var)->next) : __null))
{
6356 assert(var->as_variable() != NULL)(static_cast <bool> (var->as_variable() != __null) ?
void (0) : __assert_fail ("var->as_variable() != NULL", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
6357
6358 /* The only way a parameter would "exist" is if two parameters have
6359 * the same name.
6360 */
6361 if (state->symbols->name_declared_this_scope(var->name)) {
6362 YYLTYPE loc = this->get_location();
6363
6364 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
6365 } else {
6366 state->symbols->add_variable(var);
6367 }
6368 }
6369
6370 /* Convert the body of the function to HIR. */
6371 this->body->hir(&signature->body, state);
6372 signature->is_defined = true;
6373
6374 state->symbols->pop_scope();
6375
6376 assert(state->current_function == signature)(static_cast <bool> (state->current_function == signature
) ? void (0) : __assert_fail ("state->current_function == signature"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
6377 state->current_function = NULL__null;
6378
6379 if (!signature->return_type->is_void() && !state->found_return) {
6380 YYLTYPE loc = this->get_location();
6381 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
6382 "%s, but no return statement",
6383 signature->function_name(),
6384 signature->return_type->name);
6385 }
6386
6387 /* Function definitions do not have r-values.
6388 */
6389 return NULL__null;
6390}
6391
6392
6393ir_rvalue *
6394ast_jump_statement::hir(exec_list *instructions,
6395 struct _mesa_glsl_parse_state *state)
6396{
6397 void *ctx = state;
6398
6399 switch (mode) {
6400 case ast_return: {
6401 ir_return *inst;
6402 assert(state->current_function)(static_cast <bool> (state->current_function) ? void
(0) : __assert_fail ("state->current_function", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
6403
6404 if (opt_return_value) {
6405 ir_rvalue *ret = opt_return_value->hir(instructions, state);
6406
6407 /* The value of the return type can be NULL if the shader says
6408 * 'return foo();' and foo() is a function that returns void.
6409 *
6410 * NOTE: The GLSL spec doesn't say that this is an error. The type
6411 * of the return value is void. If the return type of the function is
6412 * also void, then this should compile without error. Seriously.
6413 */
6414 const glsl_type *const ret_type =
6415 (ret == NULL__null) ? glsl_type::void_type : ret->type;
6416
6417 /* Implicit conversions are not allowed for return values prior to
6418 * ARB_shading_language_420pack.
6419 */
6420 if (state->current_function->return_type != ret_type) {
6421 YYLTYPE loc = this->get_location();
6422
6423 if (state->has_420pack()) {
6424 if (!apply_implicit_conversion(state->current_function->return_type,
6425 ret, state)
6426 || (ret->type != state->current_function->return_type)) {
6427 _mesa_glsl_error(& loc, state,
6428 "could not implicitly convert return value "
6429 "to %s, in function `%s'",
6430 state->current_function->return_type->name,
6431 state->current_function->function_name());
6432 }
6433 } else {
6434 _mesa_glsl_error(& loc, state,
6435 "`return' with wrong type %s, in function `%s' "
6436 "returning %s",
6437 ret_type->name,
6438 state->current_function->function_name(),
6439 state->current_function->return_type->name);
6440 }
6441 } else if (state->current_function->return_type->base_type ==
6442 GLSL_TYPE_VOID) {
6443 YYLTYPE loc = this->get_location();
6444
6445 /* The ARB_shading_language_420pack, GLSL ES 3.0, and GLSL 4.20
6446 * specs add a clarification:
6447 *
6448 * "A void function can only use return without a return argument, even if
6449 * the return argument has void type. Return statements only accept values:
6450 *
6451 * void func1() { }
6452 * void func2() { return func1(); } // illegal return statement"
6453 */
6454 _mesa_glsl_error(& loc, state,
6455 "void functions can only use `return' without a "
6456 "return argument");
6457 }
6458
6459 inst = new(ctx) ir_return(ret);
6460 } else {
6461 if (state->current_function->return_type->base_type !=
6462 GLSL_TYPE_VOID) {
6463 YYLTYPE loc = this->get_location();
6464
6465 _mesa_glsl_error(& loc, state,
6466 "`return' with no value, in function %s returning "
6467 "non-void",
6468 state->current_function->function_name());
6469 }
6470 inst = new(ctx) ir_return;
6471 }
6472
6473 state->found_return = true;
6474 instructions->push_tail(inst);
6475 break;
6476 }
6477
6478 case ast_discard:
6479 if (state->stage != MESA_SHADER_FRAGMENT) {
6480 YYLTYPE loc = this->get_location();
6481
6482 _mesa_glsl_error(& loc, state,
6483 "`discard' may only appear in a fragment shader");
6484 }
6485 instructions->push_tail(new(ctx) ir_discard);
6486 break;
6487
6488 case ast_break:
6489 case ast_continue:
6490 if (mode == ast_continue &&
6491 state->loop_nesting_ast == NULL__null) {
6492 YYLTYPE loc = this->get_location();
6493
6494 _mesa_glsl_error(& loc, state, "continue may only appear in a loop");
6495 } else if (mode == ast_break &&
6496 state->loop_nesting_ast == NULL__null &&
6497 state->switch_state.switch_nesting_ast == NULL__null) {
6498 YYLTYPE loc = this->get_location();
6499
6500 _mesa_glsl_error(& loc, state,
6501 "break may only appear in a loop or a switch");
6502 } else {
6503 /* For a loop, inline the for loop expression again, since we don't
6504 * know where near the end of the loop body the normal copy of it is
6505 * going to be placed. Same goes for the condition for a do-while
6506 * loop.
6507 */
6508 if (state->loop_nesting_ast != NULL__null &&
6509 mode == ast_continue) {
6510 if (state->loop_nesting_ast->rest_expression) {
6511 state->loop_nesting_ast->rest_expression->hir(instructions,
6512 state);
6513 }
6514 if (state->loop_nesting_ast->mode ==
6515 ast_iteration_statement::ast_do_while) {
6516 state->loop_nesting_ast->condition_to_hir(instructions, state);
6517 }
6518 }
6519
6520 if (state->switch_state.is_switch_innermost &&
6521 mode == ast_break) {
6522 /* Force break out of switch by setting is_break switch state.
6523 */
6524 ir_variable *const is_break_var = state->switch_state.is_break_var;
6525 ir_dereference_variable *const deref_is_break_var =
6526 new(ctx) ir_dereference_variable(is_break_var);
6527 ir_constant *const true_val = new(ctx) ir_constant(true);
6528 ir_assignment *const set_break_var =
6529 new(ctx) ir_assignment(deref_is_break_var, true_val);
6530
6531 instructions->push_tail(set_break_var);
6532 } else {
6533 ir_loop_jump *const jump =
6534 new(ctx) ir_loop_jump((mode == ast_break)
6535 ? ir_loop_jump::jump_break
6536 : ir_loop_jump::jump_continue);
6537 instructions->push_tail(jump);
6538 }
6539 }
6540
6541 break;
6542 }
6543
6544 /* Jump instructions do not have r-values.
6545 */
6546 return NULL__null;
6547}
6548
6549
6550ir_rvalue *
6551ast_demote_statement::hir(exec_list *instructions,
6552 struct _mesa_glsl_parse_state *state)
6553{
6554 void *ctx = state;
6555
6556 if (state->stage != MESA_SHADER_FRAGMENT) {
6557 YYLTYPE loc = this->get_location();
6558
6559 _mesa_glsl_error(& loc, state,
6560 "`demote' may only appear in a fragment shader");
6561 }
6562
6563 instructions->push_tail(new(ctx) ir_demote);
6564
6565 return NULL__null;
6566}
6567
6568
6569ir_rvalue *
6570ast_selection_statement::hir(exec_list *instructions,
6571 struct _mesa_glsl_parse_state *state)
6572{
6573 void *ctx = state;
6574
6575 ir_rvalue *const condition = this->condition->hir(instructions, state);
6576
6577 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
6578 *
6579 * "Any expression whose type evaluates to a Boolean can be used as the
6580 * conditional expression bool-expression. Vector types are not accepted
6581 * as the expression to if."
6582 *
6583 * The checks are separated so that higher quality diagnostics can be
6584 * generated for cases where both rules are violated.
6585 */
6586 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
6587 YYLTYPE loc = this->condition->get_location();
6588
6589 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
6590 "boolean");
6591 }
6592
6593 ir_if *const stmt = new(ctx) ir_if(condition);
6594
6595 if (then_statement != NULL__null) {
6596 state->symbols->push_scope();
6597 then_statement->hir(& stmt->then_instructions, state);
6598 state->symbols->pop_scope();
6599 }
6600
6601 if (else_statement != NULL__null) {
6602 state->symbols->push_scope();
6603 else_statement->hir(& stmt->else_instructions, state);
6604 state->symbols->pop_scope();
6605 }
6606
6607 instructions->push_tail(stmt);
6608
6609 /* if-statements do not have r-values.
6610 */
6611 return NULL__null;
6612}
6613
6614
6615struct case_label {
6616 /** Value of the case label. */
6617 unsigned value;
6618
6619 /** Does this label occur after the default? */
6620 bool after_default;
6621
6622 /**
6623 * AST for the case label.
6624 *
6625 * This is only used to generate error messages for duplicate labels.
6626 */
6627 ast_expression *ast;
6628};
6629
6630/* Used for detection of duplicate case values, compare
6631 * given contents directly.
6632 */
6633static bool
6634compare_case_value(const void *a, const void *b)
6635{
6636 return ((struct case_label *) a)->value == ((struct case_label *) b)->value;
6637}
6638
6639
6640/* Used for detection of duplicate case values, just
6641 * returns key contents as is.
6642 */
6643static unsigned
6644key_contents(const void *key)
6645{
6646 return ((struct case_label *) key)->value;
6647}
6648
6649
6650ir_rvalue *
6651ast_switch_statement::hir(exec_list *instructions,
6652 struct _mesa_glsl_parse_state *state)
6653{
6654 void *ctx = state;
6655
6656 ir_rvalue *const test_expression =
6657 this->test_expression->hir(instructions, state);
6658
6659 /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
6660 *
6661 * "The type of init-expression in a switch statement must be a
6662 * scalar integer."
6663 */
6664 if (!test_expression->type->is_scalar() ||
6665 !test_expression->type->is_integer_32()) {
6666 YYLTYPE loc = this->test_expression->get_location();
6667
6668 _mesa_glsl_error(& loc,
6669 state,
6670 "switch-statement expression must be scalar "
6671 "integer");
6672 return NULL__null;
6673 }
6674
6675 /* Track the switch-statement nesting in a stack-like manner.
6676 */
6677 struct glsl_switch_state saved = state->switch_state;
6678
6679 state->switch_state.is_switch_innermost = true;
6680 state->switch_state.switch_nesting_ast = this;
6681 state->switch_state.labels_ht =
6682 _mesa_hash_table_create(NULL__null, key_contents,
6683 compare_case_value);
6684 state->switch_state.previous_default = NULL__null;
6685
6686 /* Initalize is_fallthru state to false.
6687 */
6688 ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
6689 state->switch_state.is_fallthru_var =
6690 new(ctx) ir_variable(glsl_type::bool_type,
6691 "switch_is_fallthru_tmp",
6692 ir_var_temporary);
6693 instructions->push_tail(state->switch_state.is_fallthru_var);
6694
6695 ir_dereference_variable *deref_is_fallthru_var =
6696 new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
6697 instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
6698 is_fallthru_val));
6699
6700 /* Initialize is_break state to false.
6701 */
6702 ir_rvalue *const is_break_val = new (ctx) ir_constant(false);
6703 state->switch_state.is_break_var =
6704 new(ctx) ir_variable(glsl_type::bool_type,
6705 "switch_is_break_tmp",
6706 ir_var_temporary);
6707 instructions->push_tail(state->switch_state.is_break_var);
6708
6709 ir_dereference_variable *deref_is_break_var =
6710 new(ctx) ir_dereference_variable(state->switch_state.is_break_var);
6711 instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var,
6712 is_break_val));
6713
6714 state->switch_state.run_default =
6715 new(ctx) ir_variable(glsl_type::bool_type,
6716 "run_default_tmp",
6717 ir_var_temporary);
6718 instructions->push_tail(state->switch_state.run_default);
6719
6720 /* Cache test expression.
6721 */
6722 test_to_hir(instructions, state);
6723
6724 /* Emit code for body of switch stmt.
6725 */
6726 body->hir(instructions, state);
6727
6728 _mesa_hash_table_destroy(state->switch_state.labels_ht, NULL__null);
6729
6730 state->switch_state = saved;
6731
6732 /* Switch statements do not have r-values. */
6733 return NULL__null;
6734}
6735
6736
6737void
6738ast_switch_statement::test_to_hir(exec_list *instructions,
6739 struct _mesa_glsl_parse_state *state)
6740{
6741 void *ctx = state;
6742
6743 /* set to true to avoid a duplicate "use of uninitialized variable" warning
6744 * on the switch test case. The first one would be already raised when
6745 * getting the test_expression at ast_switch_statement::hir
6746 */
6747 test_expression->set_is_lhs(true);
6748 /* Cache value of test expression. */
6749 ir_rvalue *const test_val = test_expression->hir(instructions, state);
6750
6751 state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
6752 "switch_test_tmp",
6753 ir_var_temporary);
6754 ir_dereference_variable *deref_test_var =
6755 new(ctx) ir_dereference_variable(state->switch_state.test_var);
6756
6757 instructions->push_tail(state->switch_state.test_var);
6758 instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val));
6759}
6760
6761
6762ir_rvalue *
6763ast_switch_body::hir(exec_list *instructions,
6764 struct _mesa_glsl_parse_state *state)
6765{
6766 if (stmts != NULL__null)
6767 stmts->hir(instructions, state);
6768
6769 /* Switch bodies do not have r-values. */
6770 return NULL__null;
6771}
6772
6773ir_rvalue *
6774ast_case_statement_list::hir(exec_list *instructions,
6775 struct _mesa_glsl_parse_state *state)
6776{
6777 exec_list default_case, after_default, tmp;
6778
6779 foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases)for (ast_case_statement * case_stmt = (!exec_node_is_tail_sentinel
((& this->cases)->head_sentinel.next) ? ((ast_case_statement
*) (((uintptr_t) (& this->cases)->head_sentinel.next
) - (((char *) &((ast_case_statement *) (& this->cases
)->head_sentinel.next)->link) - ((char *) (& this->
cases)->head_sentinel.next)))) : __null); (case_stmt) != __null
; (case_stmt) = (!exec_node_is_tail_sentinel((case_stmt)->
link.next) ? ((ast_case_statement *) (((uintptr_t) (case_stmt
)->link.next) - (((char *) &((ast_case_statement *) (case_stmt
)->link.next)->link) - ((char *) (case_stmt)->link.next
)))) : __null))
{
6780 case_stmt->hir(&tmp, state);
6781
6782 /* Default case. */
6783 if (state->switch_state.previous_default && default_case.is_empty()) {
6784 default_case.append_list(&tmp);
6785 continue;
6786 }
6787
6788 /* If default case found, append 'after_default' list. */
6789 if (!default_case.is_empty())
6790 after_default.append_list(&tmp);
6791 else
6792 instructions->append_list(&tmp);
6793 }
6794
6795 /* Handle the default case. This is done here because default might not be
6796 * the last case. We need to add checks against following cases first to see
6797 * if default should be chosen or not.
6798 */
6799 if (!default_case.is_empty()) {
6800 ir_factory body(instructions, state);
6801
6802 ir_expression *cmp = NULL__null;
6803
6804 hash_table_foreach(state->switch_state.labels_ht, entry)for (struct hash_entry *entry = _mesa_hash_table_next_entry(state
->switch_state.labels_ht, __null); entry != __null; entry =
_mesa_hash_table_next_entry(state->switch_state.labels_ht
, entry))
{
6805 const struct case_label *const l = (struct case_label *) entry->data;
6806
6807 /* If the switch init-value is the value of one of the labels that
6808 * occurs after the default case, disable execution of the default
6809 * case.
6810 */
6811 if (l->after_default) {
6812 ir_constant *const cnst =
6813 state->switch_state.test_var->type->base_type == GLSL_TYPE_UINT
6814 ? body.constant(unsigned(l->value))
6815 : body.constant(int(l->value));
6816
6817 cmp = cmp == NULL__null
6818 ? equal(cnst, state->switch_state.test_var)
6819 : logic_or(cmp, equal(cnst, state->switch_state.test_var));
6820 }
6821 }
6822
6823 if (cmp != NULL__null)
6824 body.emit(assign(state->switch_state.run_default, logic_not(cmp)));
6825 else
6826 body.emit(assign(state->switch_state.run_default, body.constant(true)));
6827
6828 /* Append default case and all cases after it. */
6829 instructions->append_list(&default_case);
6830 instructions->append_list(&after_default);
6831 }
6832
6833 /* Case statements do not have r-values. */
6834 return NULL__null;
6835}
6836
6837ir_rvalue *
6838ast_case_statement::hir(exec_list *instructions,
6839 struct _mesa_glsl_parse_state *state)
6840{
6841 labels->hir(instructions, state);
6842
6843 /* Conditionally set fallthru state based on break state. */
6844 ir_factory reset_fallthru(instructions, state);
6845 reset_fallthru.emit(assign(state->switch_state.is_fallthru_var,
6846 logic_and(state->switch_state.is_fallthru_var,
6847 logic_not(state->switch_state.is_break_var))));
6848
6849 /* Guard case statements depending on fallthru state. */
6850 ir_dereference_variable *const deref_fallthru_guard =
6851 new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
6852 ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
6853
6854 foreach_list_typed (ast_node, stmt, link, & this->stmts)for (ast_node * stmt = (!exec_node_is_tail_sentinel((& this
->stmts)->head_sentinel.next) ? ((ast_node *) (((uintptr_t
) (& this->stmts)->head_sentinel.next) - (((char *)
&((ast_node *) (& this->stmts)->head_sentinel.
next)->link) - ((char *) (& this->stmts)->head_sentinel
.next)))) : __null); (stmt) != __null; (stmt) = (!exec_node_is_tail_sentinel
((stmt)->link.next) ? ((ast_node *) (((uintptr_t) (stmt)->
link.next) - (((char *) &((ast_node *) (stmt)->link.next
)->link) - ((char *) (stmt)->link.next)))) : __null))
6855 stmt->hir(& test_fallthru->then_instructions, state);
6856
6857 instructions->push_tail(test_fallthru);
6858
6859 /* Case statements do not have r-values. */
6860 return NULL__null;
6861}
6862
6863
6864ir_rvalue *
6865ast_case_label_list::hir(exec_list *instructions,
6866 struct _mesa_glsl_parse_state *state)
6867{
6868 foreach_list_typed (ast_case_label, label, link, & this->labels)for (ast_case_label * label = (!exec_node_is_tail_sentinel((&
this->labels)->head_sentinel.next) ? ((ast_case_label *
) (((uintptr_t) (& this->labels)->head_sentinel.next
) - (((char *) &((ast_case_label *) (& this->labels
)->head_sentinel.next)->link) - ((char *) (& this->
labels)->head_sentinel.next)))) : __null); (label) != __null
; (label) = (!exec_node_is_tail_sentinel((label)->link.next
) ? ((ast_case_label *) (((uintptr_t) (label)->link.next) -
(((char *) &((ast_case_label *) (label)->link.next)->
link) - ((char *) (label)->link.next)))) : __null))
6869 label->hir(instructions, state);
6870
6871 /* Case labels do not have r-values. */
6872 return NULL__null;
6873}
6874
6875ir_rvalue *
6876ast_case_label::hir(exec_list *instructions,
6877 struct _mesa_glsl_parse_state *state)
6878{
6879 ir_factory body(instructions, state);
6880
6881 ir_variable *const fallthru_var = state->switch_state.is_fallthru_var;
6882
6883 /* If not default case, ... */
6884 if (this->test_value != NULL__null) {
6885 /* Conditionally set fallthru state based on
6886 * comparison of cached test expression value to case label.
6887 */
6888 ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
6889 ir_constant *label_const =
6890 label_rval->constant_expression_value(body.mem_ctx);
6891
6892 if (!label_const) {
6893 YYLTYPE loc = this->test_value->get_location();
6894
6895 _mesa_glsl_error(& loc, state,
6896 "switch statement case label must be a "
6897 "constant expression");
6898
6899 /* Stuff a dummy value in to allow processing to continue. */
6900 label_const = body.constant(0);
6901 } else {
6902 hash_entry *entry =
6903 _mesa_hash_table_search(state->switch_state.labels_ht,
6904 &label_const->value.u[0]);
6905
6906 if (entry) {
6907 const struct case_label *const l =
6908 (struct case_label *) entry->data;
6909 const ast_expression *const previous_label = l->ast;
6910 YYLTYPE loc = this->test_value->get_location();
6911
6912 _mesa_glsl_error(& loc, state, "duplicate case value");
6913
6914 loc = previous_label->get_location();
6915 _mesa_glsl_error(& loc, state, "this is the previous case label");
6916 } else {
6917 struct case_label *l = ralloc(state->switch_state.labels_ht,((struct case_label *) ralloc_size(state->switch_state.labels_ht
, sizeof(struct case_label)))
6918 struct case_label)((struct case_label *) ralloc_size(state->switch_state.labels_ht
, sizeof(struct case_label)))
;
6919
6920 l->value = label_const->value.u[0];
6921 l->after_default = state->switch_state.previous_default != NULL__null;
6922 l->ast = this->test_value;
6923
6924 _mesa_hash_table_insert(state->switch_state.labels_ht,
6925 &label_const->value.u[0],
6926 l);
6927 }
6928 }
6929
6930 /* Create an r-value version of the ir_constant label here (after we may
6931 * have created a fake one in error cases) that can be passed to
6932 * apply_implicit_conversion below.
6933 */
6934 ir_rvalue *label = label_const;
6935
6936 ir_rvalue *deref_test_var =
6937 new(body.mem_ctx) ir_dereference_variable(state->switch_state.test_var);
6938
6939 /*
6940 * From GLSL 4.40 specification section 6.2 ("Selection"):
6941 *
6942 * "The type of the init-expression value in a switch statement must
6943 * be a scalar int or uint. The type of the constant-expression value
6944 * in a case label also must be a scalar int or uint. When any pair
6945 * of these values is tested for "equal value" and the types do not
6946 * match, an implicit conversion will be done to convert the int to a
6947 * uint (see section 4.1.10 “Implicit Conversions”) before the compare
6948 * is done."
6949 */
6950 if (label->type != state->switch_state.test_var->type) {
6951 YYLTYPE loc = this->test_value->get_location();
6952
6953 const glsl_type *type_a = label->type;
6954 const glsl_type *type_b = state->switch_state.test_var->type;
6955
6956 /* Check if int->uint implicit conversion is supported. */
6957 bool integer_conversion_supported =
6958 glsl_type::int_type->can_implicitly_convert_to(glsl_type::uint_type,
6959 state);
6960
6961 if ((!type_a->is_integer_32() || !type_b->is_integer_32()) ||
6962 !integer_conversion_supported) {
6963 _mesa_glsl_error(&loc, state, "type mismatch with switch "
6964 "init-expression and case label (%s != %s)",
6965 type_a->name, type_b->name);
6966 } else {
6967 /* Conversion of the case label. */
6968 if (type_a->base_type == GLSL_TYPE_INT) {
6969 if (!apply_implicit_conversion(glsl_type::uint_type,
6970 label, state))
6971 _mesa_glsl_error(&loc, state, "implicit type conversion error");
6972 } else {
6973 /* Conversion of the init-expression value. */
6974 if (!apply_implicit_conversion(glsl_type::uint_type,
6975 deref_test_var, state))
6976 _mesa_glsl_error(&loc, state, "implicit type conversion error");
6977 }
6978 }
6979
6980 /* If the implicit conversion was allowed, the types will already be
6981 * the same. If the implicit conversion wasn't allowed, smash the
6982 * type of the label anyway. This will prevent the expression
6983 * constructor (below) from failing an assertion.
6984 */
6985 label->type = deref_test_var->type;
6986 }
6987
6988 body.emit(assign(fallthru_var,
6989 logic_or(fallthru_var, equal(label, deref_test_var))));
6990 } else { /* default case */
6991 if (state->switch_state.previous_default) {
6992 YYLTYPE loc = this->get_location();
6993 _mesa_glsl_error(& loc, state,
6994 "multiple default labels in one switch");
6995
6996 loc = state->switch_state.previous_default->get_location();
6997 _mesa_glsl_error(& loc, state, "this is the first default label");
6998 }
6999 state->switch_state.previous_default = this;
7000
7001 /* Set fallthru condition on 'run_default' bool. */
7002 body.emit(assign(fallthru_var,
7003 logic_or(fallthru_var,
7004 state->switch_state.run_default)));
7005 }
7006
7007 /* Case statements do not have r-values. */
7008 return NULL__null;
7009}
7010
7011void
7012ast_iteration_statement::condition_to_hir(exec_list *instructions,
7013 struct _mesa_glsl_parse_state *state)
7014{
7015 void *ctx = state;
7016
7017 if (condition != NULL__null) {
7018 ir_rvalue *const cond =
7019 condition->hir(instructions, state);
7020
7021 if ((cond == NULL__null)
7022 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
7023 YYLTYPE loc = condition->get_location();
7024
7025 _mesa_glsl_error(& loc, state,
7026 "loop condition must be scalar boolean");
7027 } else {
7028 /* As the first code in the loop body, generate a block that looks
7029 * like 'if (!condition) break;' as the loop termination condition.
7030 */
7031 ir_rvalue *const not_cond =
7032 new(ctx) ir_expression(ir_unop_logic_not, cond);
7033
7034 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
7035
7036 ir_jump *const break_stmt =
7037 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
7038
7039 if_stmt->then_instructions.push_tail(break_stmt);
7040 instructions->push_tail(if_stmt);
7041 }
7042 }
7043}
7044
7045
7046ir_rvalue *
7047ast_iteration_statement::hir(exec_list *instructions,
7048 struct _mesa_glsl_parse_state *state)
7049{
7050 void *ctx = state;
7051
7052 /* For-loops and while-loops start a new scope, but do-while loops do not.
7053 */
7054 if (mode != ast_do_while)
7055 state->symbols->push_scope();
7056
7057 if (init_statement != NULL__null)
7058 init_statement->hir(instructions, state);
7059
7060 ir_loop *const stmt = new(ctx) ir_loop();
7061 instructions->push_tail(stmt);
7062
7063 /* Track the current loop nesting. */
7064 ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
7065
7066 state->loop_nesting_ast = this;
7067
7068 /* Likewise, indicate that following code is closest to a loop,
7069 * NOT closest to a switch.
7070 */
7071 bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
7072 state->switch_state.is_switch_innermost = false;
7073
7074 if (mode != ast_do_while)
7075 condition_to_hir(&stmt->body_instructions, state);
7076
7077 if (body != NULL__null)
7078 body->hir(& stmt->body_instructions, state);
7079
7080 if (rest_expression != NULL__null)
7081 rest_expression->hir(& stmt->body_instructions, state);
7082
7083 if (mode == ast_do_while)
7084 condition_to_hir(&stmt->body_instructions, state);
7085
7086 if (mode != ast_do_while)
7087 state->symbols->pop_scope();
7088
7089 /* Restore previous nesting before returning. */
7090 state->loop_nesting_ast = nesting_ast;
7091 state->switch_state.is_switch_innermost = saved_is_switch_innermost;
7092
7093 /* Loops do not have r-values.
7094 */
7095 return NULL__null;
7096}
7097
7098
7099/**
7100 * Determine if the given type is valid for establishing a default precision
7101 * qualifier.
7102 *
7103 * From GLSL ES 3.00 section 4.5.4 ("Default Precision Qualifiers"):
7104 *
7105 * "The precision statement
7106 *
7107 * precision precision-qualifier type;
7108 *
7109 * can be used to establish a default precision qualifier. The type field
7110 * can be either int or float or any of the sampler types, and the
7111 * precision-qualifier can be lowp, mediump, or highp."
7112 *
7113 * GLSL ES 1.00 has similar language. GLSL 1.30 doesn't allow precision
7114 * qualifiers on sampler types, but this seems like an oversight (since the
7115 * intention of including these in GLSL 1.30 is to allow compatibility with ES
7116 * shaders). So we allow int, float, and all sampler types regardless of GLSL
7117 * version.
7118 */
7119static bool
7120is_valid_default_precision_type(const struct glsl_type *const type)
7121{
7122 if (type == NULL__null)
7123 return false;
7124
7125 switch (type->base_type) {
7126 case GLSL_TYPE_INT:
7127 case GLSL_TYPE_FLOAT:
7128 /* "int" and "float" are valid, but vectors and matrices are not. */
7129 return type->vector_elements == 1 && type->matrix_columns == 1;
7130 case GLSL_TYPE_SAMPLER:
7131 case GLSL_TYPE_IMAGE:
7132 case GLSL_TYPE_ATOMIC_UINT:
7133 return true;
7134 default:
7135 return false;
7136 }
7137}
7138
7139
7140ir_rvalue *
7141ast_type_specifier::hir(exec_list *instructions,
7142 struct _mesa_glsl_parse_state *state)
7143{
7144 if (this->default_precision == ast_precision_none && this->structure == NULL__null)
7145 return NULL__null;
7146
7147 YYLTYPE loc = this->get_location();
7148
7149 /* If this is a precision statement, check that the type to which it is
7150 * applied is either float or int.
7151 *
7152 * From section 4.5.3 of the GLSL 1.30 spec:
7153 * "The precision statement
7154 * precision precision-qualifier type;
7155 * can be used to establish a default precision qualifier. The type
7156 * field can be either int or float [...]. Any other types or
7157 * qualifiers will result in an error.
7158 */
7159 if (this->default_precision != ast_precision_none) {
7160 if (!state->check_precision_qualifiers_allowed(&loc))
7161 return NULL__null;
7162
7163 if (this->structure != NULL__null) {
7164 _mesa_glsl_error(&loc, state,
7165 "precision qualifiers do not apply to structures");
7166 return NULL__null;
7167 }
7168
7169 if (this->array_specifier != NULL__null) {
7170 _mesa_glsl_error(&loc, state,
7171 "default precision statements do not apply to "
7172 "arrays");
7173 return NULL__null;
7174 }
7175
7176 const struct glsl_type *const type =
7177 state->symbols->get_type(this->type_name);
7178 if (!is_valid_default_precision_type(type)) {
7179 _mesa_glsl_error(&loc, state,
7180 "default precision statements apply only to "
7181 "float, int, and opaque types");
7182 return NULL__null;
7183 }
7184
7185 if (state->es_shader) {
7186 /* Section 4.5.3 (Default Precision Qualifiers) of the GLSL ES 1.00
7187 * spec says:
7188 *
7189 * "Non-precision qualified declarations will use the precision
7190 * qualifier specified in the most recent precision statement
7191 * that is still in scope. The precision statement has the same
7192 * scoping rules as variable declarations. If it is declared
7193 * inside a compound statement, its effect stops at the end of
7194 * the innermost statement it was declared in. Precision
7195 * statements in nested scopes override precision statements in
7196 * outer scopes. Multiple precision statements for the same basic
7197 * type can appear inside the same scope, with later statements
7198 * overriding earlier statements within that scope."
7199 *
7200 * Default precision specifications follow the same scope rules as
7201 * variables. So, we can track the state of the default precision
7202 * qualifiers in the symbol table, and the rules will just work. This
7203 * is a slight abuse of the symbol table, but it has the semantics
7204 * that we want.
7205 */
7206 state->symbols->add_default_precision_qualifier(this->type_name,
7207 this->default_precision);
7208 }
7209
7210 {
7211 void *ctx = state;
7212
7213 const char* precision_type = NULL__null;
7214 switch (this->default_precision) {
7215 case GLSL_PRECISION_HIGH:
7216 precision_type = "highp";
7217 break;
7218 case GLSL_PRECISION_MEDIUM:
7219 precision_type = "mediump";
7220 break;
7221 case GLSL_PRECISION_LOW:
7222 precision_type = "lowp";
7223 break;
7224 case GLSL_PRECISION_NONE:
7225 precision_type = "";
7226 break;
7227 }
7228
7229 char* precision_statement = ralloc_asprintf(ctx, "precision %s %s", precision_type, this->type_name);
7230 ir_precision_statement *const stmt = new(ctx) ir_precision_statement(precision_statement);
7231
7232 instructions->push_head(stmt);
7233 }
7234
7235 return NULL__null;
7236 }
7237
7238 /* _mesa_ast_set_aggregate_type() sets the <structure> field so that
7239 * process_record_constructor() can do type-checking on C-style initializer
7240 * expressions of structs, but ast_struct_specifier should only be translated
7241 * to HIR if it is declaring the type of a structure.
7242 *
7243 * The ->is_declaration field is false for initializers of variables
7244 * declared separately from the struct's type definition.
7245 *
7246 * struct S { ... }; (is_declaration = true)
7247 * struct T { ... } t = { ... }; (is_declaration = true)
7248 * S s = { ... }; (is_declaration = false)
7249 */
7250 if (this->structure != NULL__null && this->structure->is_declaration)
7251 return this->structure->hir(instructions, state);
7252
7253 return NULL__null;
7254}
7255
7256
7257/**
7258 * Process a structure or interface block tree into an array of structure fields
7259 *
7260 * After parsing, where there are some syntax differnces, structures and
7261 * interface blocks are almost identical. They are similar enough that the
7262 * AST for each can be processed the same way into a set of
7263 * \c glsl_struct_field to describe the members.
7264 *
7265 * If we're processing an interface block, var_mode should be the type of the
7266 * interface block (ir_var_shader_in, ir_var_shader_out, ir_var_uniform or
7267 * ir_var_shader_storage). If we're processing a structure, var_mode should be
7268 * ir_var_auto.
7269 *
7270 * \return
7271 * The number of fields processed. A pointer to the array structure fields is
7272 * stored in \c *fields_ret.
7273 */
7274static unsigned
7275ast_process_struct_or_iface_block_members(exec_list *instructions,
7276 struct _mesa_glsl_parse_state *state,
7277 exec_list *declarations,
7278 glsl_struct_field **fields_ret,
7279 bool is_interface,
7280 enum glsl_matrix_layout matrix_layout,
7281 bool allow_reserved_names,
7282 ir_variable_mode var_mode,
7283 ast_type_qualifier *layout,
7284 unsigned block_stream,
7285 unsigned block_xfb_buffer,
7286 unsigned block_xfb_offset,
7287 unsigned expl_location,
7288 unsigned expl_align)
7289{
7290 unsigned decl_count = 0;
7291 unsigned next_offset = 0;
7292
7293 /* Make an initial pass over the list of fields to determine how
7294 * many there are. Each element in this list is an ast_declarator_list.
7295 * This means that we actually need to count the number of elements in the
7296 * 'declarations' list in each of the elements.
7297 */
7298 foreach_list_typed (ast_declarator_list, decl_list, link, declarations)for (ast_declarator_list * decl_list = (!exec_node_is_tail_sentinel
((declarations)->head_sentinel.next) ? ((ast_declarator_list
*) (((uintptr_t) (declarations)->head_sentinel.next) - ((
(char *) &((ast_declarator_list *) (declarations)->head_sentinel
.next)->link) - ((char *) (declarations)->head_sentinel
.next)))) : __null); (decl_list) != __null; (decl_list) = (!exec_node_is_tail_sentinel
((decl_list)->link.next) ? ((ast_declarator_list *) (((uintptr_t
) (decl_list)->link.next) - (((char *) &((ast_declarator_list
*) (decl_list)->link.next)->link) - ((char *) (decl_list
)->link.next)))) : __null))
{
7299 decl_count += decl_list->declarations.length();
7300 }
7301
7302 /* Allocate storage for the fields and process the field
7303 * declarations. As the declarations are processed, try to also convert
7304 * the types to HIR. This ensures that structure definitions embedded in
7305 * other structure definitions or in interface blocks are processed.
7306 */
7307 glsl_struct_field *const fields = rzalloc_array(state, glsl_struct_field,((glsl_struct_field *) rzalloc_array_size(state, sizeof(glsl_struct_field
), decl_count))
7308 decl_count)((glsl_struct_field *) rzalloc_array_size(state, sizeof(glsl_struct_field
), decl_count))
;
7309
7310 bool first_member = true;
7311 bool first_member_has_explicit_location = false;
7312
7313 unsigned i = 0;
7314 foreach_list_typed (ast_declarator_list, decl_list, link, declarations)for (ast_declarator_list * decl_list = (!exec_node_is_tail_sentinel
((declarations)->head_sentinel.next) ? ((ast_declarator_list
*) (((uintptr_t) (declarations)->head_sentinel.next) - ((
(char *) &((ast_declarator_list *) (declarations)->head_sentinel
.next)->link) - ((char *) (declarations)->head_sentinel
.next)))) : __null); (decl_list) != __null; (decl_list) = (!exec_node_is_tail_sentinel
((decl_list)->link.next) ? ((ast_declarator_list *) (((uintptr_t
) (decl_list)->link.next) - (((char *) &((ast_declarator_list
*) (decl_list)->link.next)->link) - ((char *) (decl_list
)->link.next)))) : __null))
{
7315 const char *type_name;
7316 YYLTYPE loc = decl_list->get_location();
7317
7318 decl_list->type->specifier->hir(instructions, state);
7319
7320 /* Section 4.1.8 (Structures) of the GLSL 1.10 spec says:
7321 *
7322 * "Anonymous structures are not supported; so embedded structures
7323 * must have a declarator. A name given to an embedded struct is
7324 * scoped at the same level as the struct it is embedded in."
7325 *
7326 * The same section of the GLSL 1.20 spec says:
7327 *
7328 * "Anonymous structures are not supported. Embedded structures are
7329 * not supported."
7330 *
7331 * The GLSL ES 1.00 and 3.00 specs have similar langauge. So, we allow
7332 * embedded structures in 1.10 only.
7333 */
7334 if (state->language_version != 110 &&
7335 decl_list->type->specifier->structure != NULL__null)
7336 _mesa_glsl_error(&loc, state,
7337 "embedded structure declarations are not allowed");
7338
7339 const glsl_type *decl_type =
7340 decl_list->type->glsl_type(& type_name, state);
7341
7342 const struct ast_type_qualifier *const qual =
7343 &decl_list->type->qualifier;
7344
7345 /* From section 4.3.9 of the GLSL 4.40 spec:
7346 *
7347 * "[In interface blocks] opaque types are not allowed."
7348 *
7349 * It should be impossible for decl_type to be NULL here. Cases that
7350 * might naturally lead to decl_type being NULL, especially for the
7351 * is_interface case, will have resulted in compilation having
7352 * already halted due to a syntax error.
7353 */
7354 assert(decl_type)(static_cast <bool> (decl_type) ? void (0) : __assert_fail
("decl_type", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
7355
7356 if (is_interface) {
7357 /* From section 4.3.7 of the ARB_bindless_texture spec:
7358 *
7359 * "(remove the following bullet from the last list on p. 39,
7360 * thereby permitting sampler types in interface blocks; image
7361 * types are also permitted in blocks by this extension)"
7362 *
7363 * * sampler types are not allowed
7364 */
7365 if (decl_type->contains_atomic() ||
7366 (!state->has_bindless() && decl_type->contains_opaque())) {
7367 _mesa_glsl_error(&loc, state, "uniform/buffer in non-default "
7368 "interface block contains %s variable",
7369 state->has_bindless() ? "atomic" : "opaque");
7370 }
7371 } else {
7372 if (decl_type->contains_atomic()) {
7373 /* From section 4.1.7.3 of the GLSL 4.40 spec:
7374 *
7375 * "Members of structures cannot be declared as atomic counter
7376 * types."
7377 */
7378 _mesa_glsl_error(&loc, state, "atomic counter in structure");
7379 }
7380
7381 if (!state->has_bindless() && decl_type->contains_image()) {
7382 /* FINISHME: Same problem as with atomic counters.
7383 * FINISHME: Request clarification from Khronos and add
7384 * FINISHME: spec quotation here.
7385 */
7386 _mesa_glsl_error(&loc, state, "image in structure");
7387 }
7388 }
7389
7390 if (qual->flags.q.explicit_binding) {
7391 _mesa_glsl_error(&loc, state,
7392 "binding layout qualifier cannot be applied "
7393 "to struct or interface block members");
7394 }
7395
7396 if (is_interface) {
7397 if (!first_member) {
7398 if (!layout->flags.q.explicit_location &&
7399 ((first_member_has_explicit_location &&
7400 !qual->flags.q.explicit_location) ||
7401 (!first_member_has_explicit_location &&
7402 qual->flags.q.explicit_location))) {
7403 _mesa_glsl_error(&loc, state,
7404 "when block-level location layout qualifier "
7405 "is not supplied either all members must "
7406 "have a location layout qualifier or all "
7407 "members must not have a location layout "
7408 "qualifier");
7409 }
7410 } else {
7411 first_member = false;
7412 first_member_has_explicit_location =
7413 qual->flags.q.explicit_location;
7414 }
7415 }
7416
7417 if (qual->flags.q.std140 ||
7418 qual->flags.q.std430 ||
7419 qual->flags.q.packed ||
7420 qual->flags.q.shared) {
7421 _mesa_glsl_error(&loc, state,
7422 "uniform/shader storage block layout qualifiers "
7423 "std140, std430, packed, and shared can only be "
7424 "applied to uniform/shader storage blocks, not "
7425 "members");
7426 }
7427
7428 if (qual->flags.q.constant) {
7429 _mesa_glsl_error(&loc, state,
7430 "const storage qualifier cannot be applied "
7431 "to struct or interface block members");
7432 }
7433
7434 validate_memory_qualifier_for_type(state, &loc, qual, decl_type);
7435 validate_image_format_qualifier_for_type(state, &loc, qual, decl_type);
7436
7437 /* From Section 4.4.2.3 (Geometry Outputs) of the GLSL 4.50 spec:
7438 *
7439 * "A block member may be declared with a stream identifier, but
7440 * the specified stream must match the stream associated with the
7441 * containing block."
7442 */
7443 if (qual->flags.q.explicit_stream) {
7444 unsigned qual_stream;
7445 if (process_qualifier_constant(state, &loc, "stream",
7446 qual->stream, &qual_stream) &&
7447 qual_stream != block_stream) {
7448 _mesa_glsl_error(&loc, state, "stream layout qualifier on "
7449 "interface block member does not match "
7450 "the interface block (%u vs %u)", qual_stream,
7451 block_stream);
7452 }
7453 }
7454
7455 int xfb_buffer;
7456 unsigned explicit_xfb_buffer = 0;
7457 if (qual->flags.q.explicit_xfb_buffer) {
7458 unsigned qual_xfb_buffer;
7459 if (process_qualifier_constant(state, &loc, "xfb_buffer",
7460 qual->xfb_buffer, &qual_xfb_buffer)) {
7461 explicit_xfb_buffer = 1;
7462 if (qual_xfb_buffer != block_xfb_buffer)
7463 _mesa_glsl_error(&loc, state, "xfb_buffer layout qualifier on "
7464 "interface block member does not match "
7465 "the interface block (%u vs %u)",
7466 qual_xfb_buffer, block_xfb_buffer);
7467 }
7468 xfb_buffer = (int) qual_xfb_buffer;
7469 } else {
7470 if (layout)
7471 explicit_xfb_buffer = layout->flags.q.explicit_xfb_buffer;
7472 xfb_buffer = (int) block_xfb_buffer;
7473 }
7474
7475 int xfb_stride = -1;
7476 if (qual->flags.q.explicit_xfb_stride) {
7477 unsigned qual_xfb_stride;
7478 if (process_qualifier_constant(state, &loc, "xfb_stride",
7479 qual->xfb_stride, &qual_xfb_stride)) {
7480 xfb_stride = (int) qual_xfb_stride;
7481 }
7482 }
7483
7484 if (qual->flags.q.uniform && qual->has_interpolation()) {
7485 _mesa_glsl_error(&loc, state,
7486 "interpolation qualifiers cannot be used "
7487 "with uniform interface blocks");
7488 }
7489
7490 if ((qual->flags.q.uniform || !is_interface) &&
7491 qual->has_auxiliary_storage()) {
7492 _mesa_glsl_error(&loc, state,
7493 "auxiliary storage qualifiers cannot be used "
7494 "in uniform blocks or structures.");
7495 }
7496
7497 if (qual->flags.q.row_major || qual->flags.q.column_major) {
7498 if (!qual->flags.q.uniform && !qual->flags.q.buffer) {
7499 _mesa_glsl_error(&loc, state,
7500 "row_major and column_major can only be "
7501 "applied to interface blocks");
7502 } else
7503 validate_matrix_layout_for_type(state, &loc, decl_type, NULL__null);
7504 }
7505
7506 foreach_list_typed (ast_declaration, decl, link,for (ast_declaration * decl = (!exec_node_is_tail_sentinel((&
decl_list->declarations)->head_sentinel.next) ? ((ast_declaration
*) (((uintptr_t) (&decl_list->declarations)->head_sentinel
.next) - (((char *) &((ast_declaration *) (&decl_list
->declarations)->head_sentinel.next)->link) - ((char
*) (&decl_list->declarations)->head_sentinel.next)
))) : __null); (decl) != __null; (decl) = (!exec_node_is_tail_sentinel
((decl)->link.next) ? ((ast_declaration *) (((uintptr_t) (
decl)->link.next) - (((char *) &((ast_declaration *) (
decl)->link.next)->link) - ((char *) (decl)->link.next
)))) : __null))
7507 &decl_list->declarations)for (ast_declaration * decl = (!exec_node_is_tail_sentinel((&
decl_list->declarations)->head_sentinel.next) ? ((ast_declaration
*) (((uintptr_t) (&decl_list->declarations)->head_sentinel
.next) - (((char *) &((ast_declaration *) (&decl_list
->declarations)->head_sentinel.next)->link) - ((char
*) (&decl_list->declarations)->head_sentinel.next)
))) : __null); (decl) != __null; (decl) = (!exec_node_is_tail_sentinel
((decl)->link.next) ? ((ast_declaration *) (((uintptr_t) (
decl)->link.next) - (((char *) &((ast_declaration *) (
decl)->link.next)->link) - ((char *) (decl)->link.next
)))) : __null))
{
7508 YYLTYPE loc = decl->get_location();
7509
7510 if (!allow_reserved_names)
7511 validate_identifier(decl->identifier, loc, state);
7512
7513 const struct glsl_type *field_type =
7514 process_array_type(&loc, decl_type, decl->array_specifier, state);
7515 validate_array_dimensions(field_type, state, &loc);
7516 fields[i].type = field_type;
7517 fields[i].name = decl->identifier;
7518 fields[i].interpolation =
7519 interpret_interpolation_qualifier(qual, field_type,
7520 var_mode, state, &loc);
7521 fields[i].centroid = qual->flags.q.centroid ? 1 : 0;
7522 fields[i].sample = qual->flags.q.sample ? 1 : 0;
7523 fields[i].patch = qual->flags.q.patch ? 1 : 0;
7524 fields[i].offset = -1;
7525 fields[i].explicit_xfb_buffer = explicit_xfb_buffer;
7526 fields[i].xfb_buffer = xfb_buffer;
7527 fields[i].xfb_stride = xfb_stride;
7528
7529 if (qual->flags.q.explicit_location) {
7530 unsigned qual_location;
7531 if (process_qualifier_constant(state, &loc, "location",
7532 qual->location, &qual_location)) {
7533 fields[i].location = qual_location +
7534 (fields[i].patch ? VARYING_SLOT_PATCH0((VARYING_SLOT_VAR0 + 32)) : VARYING_SLOT_VAR0);
7535 expl_location = fields[i].location +
7536 fields[i].type->count_attribute_slots(false);
7537 }
7538 } else {
7539 if (layout && layout->flags.q.explicit_location) {
7540 fields[i].location = expl_location;
7541 expl_location += fields[i].type->count_attribute_slots(false);
7542 } else {
7543 fields[i].location = -1;
7544 }
7545 }
7546
7547 /* Offset can only be used with std430 and std140 layouts an initial
7548 * value of 0 is used for error detection.
7549 */
7550 unsigned align = 0;
7551 unsigned size = 0;
7552 if (layout) {
7553 bool row_major;
7554 if (qual->flags.q.row_major ||
7555 matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR) {
7556 row_major = true;
7557 } else {
7558 row_major = false;
7559 }
7560
7561 if(layout->flags.q.std140) {
7562 align = field_type->std140_base_alignment(row_major);
7563 size = field_type->std140_size(row_major);
7564 } else if (layout->flags.q.std430) {
7565 align = field_type->std430_base_alignment(row_major);
7566 size = field_type->std430_size(row_major);
7567 }
7568 }
7569
7570 if (qual->flags.q.explicit_offset) {
7571 unsigned qual_offset;
7572 if (process_qualifier_constant(state, &loc, "offset",
7573 qual->offset, &qual_offset)) {
7574 if (align != 0 && size != 0) {
7575 if (next_offset > qual_offset)
7576 _mesa_glsl_error(&loc, state, "layout qualifier "
7577 "offset overlaps previous member");
7578
7579 if (qual_offset % align) {
7580 _mesa_glsl_error(&loc, state, "layout qualifier offset "
7581 "must be a multiple of the base "
7582 "alignment of %s", field_type->name);
7583 }
7584 fields[i].offset = qual_offset;
7585 next_offset = qual_offset + size;
7586 } else {
7587 _mesa_glsl_error(&loc, state, "offset can only be used "
7588 "with std430 and std140 layouts");
7589 }
7590 }
7591 }
7592
7593 if (qual->flags.q.explicit_align || expl_align != 0) {
7594 unsigned offset = fields[i].offset != -1 ? fields[i].offset :
7595 next_offset;
7596 if (align == 0 || size == 0) {
7597 _mesa_glsl_error(&loc, state, "align can only be used with "
7598 "std430 and std140 layouts");
7599 } else if (qual->flags.q.explicit_align) {
7600 unsigned member_align;
7601 if (process_qualifier_constant(state, &loc, "align",
7602 qual->align, &member_align)) {
7603 if (member_align == 0 ||
7604 member_align & (member_align - 1)) {
7605 _mesa_glsl_error(&loc, state, "align layout qualifier "
7606 "is not a power of 2");
7607 } else {
7608 fields[i].offset = glsl_align(offset, member_align);
7609 next_offset = fields[i].offset + size;
7610 }
7611 }
7612 } else {
7613 fields[i].offset = glsl_align(offset, expl_align);
7614 next_offset = fields[i].offset + size;
7615 }
7616 } else if (!qual->flags.q.explicit_offset) {
7617 if (align != 0 && size != 0)
7618 next_offset = glsl_align(next_offset, align) + size;
7619 }
7620
7621 /* From the ARB_enhanced_layouts spec:
7622 *
7623 * "The given offset applies to the first component of the first
7624 * member of the qualified entity. Then, within the qualified
7625 * entity, subsequent components are each assigned, in order, to
7626 * the next available offset aligned to a multiple of that
7627 * component's size. Aggregate types are flattened down to the
7628 * component level to get this sequence of components."
7629 */
7630 if (qual->flags.q.explicit_xfb_offset) {
7631 unsigned xfb_offset;
7632 if (process_qualifier_constant(state, &loc, "xfb_offset",
7633 qual->offset, &xfb_offset)) {
7634 fields[i].offset = xfb_offset;
7635 block_xfb_offset = fields[i].offset +
7636 4 * field_type->component_slots();
7637 }
7638 } else {
7639 if (layout && layout->flags.q.explicit_xfb_offset) {
7640 unsigned align = field_type->is_64bit() ? 8 : 4;
7641 fields[i].offset = glsl_align(block_xfb_offset, align);
7642 block_xfb_offset += 4 * field_type->component_slots();
7643 }
7644 }
7645
7646 /* Propogate row- / column-major information down the fields of the
7647 * structure or interface block. Structures need this data because
7648 * the structure may contain a structure that contains ... a matrix
7649 * that need the proper layout.
7650 */
7651 if (is_interface && layout &&
7652 (layout->flags.q.uniform || layout->flags.q.buffer) &&
7653 (field_type->without_array()->is_matrix()
7654 || field_type->without_array()->is_struct())) {
7655 /* If no layout is specified for the field, inherit the layout
7656 * from the block.
7657 */
7658 fields[i].matrix_layout = matrix_layout;
7659
7660 if (qual->flags.q.row_major)
7661 fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR;
7662 else if (qual->flags.q.column_major)
7663 fields[i].matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR;
7664
7665 /* If we're processing an uniform or buffer block, the matrix
7666 * layout must be decided by this point.
7667 */
7668 assert(fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR(static_cast <bool> (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR
|| fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) ? void (0) : __assert_fail ("fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR || fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
7669 || fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR)(static_cast <bool> (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR
|| fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) ? void (0) : __assert_fail ("fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_ROW_MAJOR || fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_COLUMN_MAJOR"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
7670 }
7671
7672 /* Memory qualifiers are allowed on buffer and image variables, while
7673 * the format qualifier is only accepted for images.
7674 */
7675 if (var_mode == ir_var_shader_storage ||
7676 field_type->without_array()->is_image()) {
7677 /* For readonly and writeonly qualifiers the field definition,
7678 * if set, overwrites the layout qualifier.
7679 */
7680 if (qual->flags.q.read_only || qual->flags.q.write_only) {
7681 fields[i].memory_read_only = qual->flags.q.read_only;
7682 fields[i].memory_write_only = qual->flags.q.write_only;
7683 } else {
7684 fields[i].memory_read_only =
7685 layout ? layout->flags.q.read_only : 0;
7686 fields[i].memory_write_only =
7687 layout ? layout->flags.q.write_only : 0;
7688 }
7689
7690 /* For other qualifiers, we set the flag if either the layout
7691 * qualifier or the field qualifier are set
7692 */
7693 fields[i].memory_coherent = qual->flags.q.coherent ||
7694 (layout && layout->flags.q.coherent);
7695 fields[i].memory_volatile = qual->flags.q._volatile ||
7696 (layout && layout->flags.q._volatile);
7697 fields[i].memory_restrict = qual->flags.q.restrict_flag ||
7698 (layout && layout->flags.q.restrict_flag);
7699
7700 if (field_type->without_array()->is_image()) {
7701 if (qual->flags.q.explicit_image_format) {
7702 if (qual->image_base_type !=
7703 field_type->without_array()->sampled_type) {
7704 _mesa_glsl_error(&loc, state, "format qualifier doesn't "
7705 "match the base data type of the image");
7706 }
7707
7708 fields[i].image_format = qual->image_format;
7709 } else {
7710 if (!qual->flags.q.write_only) {
7711 _mesa_glsl_error(&loc, state, "image not qualified with "
7712 "`writeonly' must have a format layout "
7713 "qualifier");
7714 }
7715
7716 fields[i].image_format = PIPE_FORMAT_NONE;
7717 }
7718 }
7719 }
7720
7721 /* Precision qualifiers do not hold any meaning in Desktop GLSL */
7722 if (state->es_shader) {
7723 fields[i].precision = select_gles_precision(qual->precision,
7724 field_type,
7725 state,
7726 &loc);
7727 } else {
7728 fields[i].precision = qual->precision;
7729 }
7730
7731 i++;
7732 }
7733 }
7734
7735 assert(i == decl_count)(static_cast <bool> (i == decl_count) ? void (0) : __assert_fail
("i == decl_count", __builtin_FILE (), __builtin_LINE (), __extension__
__PRETTY_FUNCTION__))
;
7736
7737 *fields_ret = fields;
7738 return decl_count;
7739}
7740
7741
7742ir_rvalue *
7743ast_struct_specifier::hir(exec_list *instructions,
7744 struct _mesa_glsl_parse_state *state)
7745{
7746 YYLTYPE loc = this->get_location();
7747
7748 unsigned expl_location = 0;
7749 if (layout && layout->flags.q.explicit_location) {
7750 if (!process_qualifier_constant(state, &loc, "location",
7751 layout->location, &expl_location)) {
7752 return NULL__null;
7753 } else {
7754 expl_location = VARYING_SLOT_VAR0 + expl_location;
7755 }
7756 }
7757
7758 glsl_struct_field *fields;
7759 unsigned decl_count =
7760 ast_process_struct_or_iface_block_members(instructions,
7761 state,
7762 &this->declarations,
7763 &fields,
7764 false,
7765 GLSL_MATRIX_LAYOUT_INHERITED,
7766 false /* allow_reserved_names */,
7767 ir_var_auto,
7768 layout,
7769 0, /* for interface only */
7770 0, /* for interface only */
7771 0, /* for interface only */
7772 expl_location,
7773 0 /* for interface only */);
7774
7775 validate_identifier(this->name, loc, state);
7776
7777 type = glsl_type::get_struct_instance(fields, decl_count, this->name);
7778
7779 if (!type->is_anonymous() && !state->symbols->add_type(name, type)) {
7780 const glsl_type *match = state->symbols->get_type(name);
7781 /* allow struct matching for desktop GL - older UE4 does this */
7782 if (match != NULL__null && state->is_version(130, 0) && match->record_compare(type, true, false))
7783 _mesa_glsl_warning(& loc, state, "struct `%s' previously defined", name);
7784 else
7785 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
7786 } else {
7787 const glsl_type **s = reralloc(state, state->user_structures,((const glsl_type * *) reralloc_array_size(state, state->user_structures
, sizeof(const glsl_type *), state->num_user_structures + 1
))
7788 const glsl_type *,((const glsl_type * *) reralloc_array_size(state, state->user_structures
, sizeof(const glsl_type *), state->num_user_structures + 1
))
7789 state->num_user_structures + 1)((const glsl_type * *) reralloc_array_size(state, state->user_structures
, sizeof(const glsl_type *), state->num_user_structures + 1
))
;
7790 if (s != NULL__null) {
7791 s[state->num_user_structures] = type;
7792 state->user_structures = s;
7793 state->num_user_structures++;
7794
7795 ir_typedecl_statement* stmt = new(state) ir_typedecl_statement(type);
7796 /* Push the struct declarations to the top.
7797 * However, do not insert declarations before default precision
7798 * statements or other declarations
7799 */
7800 ir_instruction* before_node = (ir_instruction*)instructions->get_head();
7801 while (before_node &&
7802 (before_node->ir_type == ir_type_precision ||
7803 before_node->ir_type == ir_type_typedecl))
7804 before_node = (ir_instruction*)before_node->next;
7805 if (before_node)
7806 before_node->insert_before(stmt);
7807 else
7808 instructions->push_head(stmt);
7809 }
7810 }
7811
7812 /* Structure type definitions do not have r-values.
7813 */
7814 return NULL__null;
7815}
7816
7817
7818/**
7819 * Visitor class which detects whether a given interface block has been used.
7820 */
7821class interface_block_usage_visitor : public ir_hierarchical_visitor
7822{
7823public:
7824 interface_block_usage_visitor(ir_variable_mode mode, const glsl_type *block)
7825 : mode(mode), block(block), found(false)
7826 {
7827 }
7828
7829 virtual ir_visitor_status visit(ir_dereference_variable *ir)
7830 {
7831 if (ir->var->data.mode == mode && ir->var->get_interface_type() == block) {
7832 found = true;
7833 return visit_stop;
7834 }
7835 return visit_continue;
7836 }
7837
7838 bool usage_found() const
7839 {
7840 return this->found;
7841 }
7842
7843private:
7844 ir_variable_mode mode;
7845 const glsl_type *block;
7846 bool found;
7847};
7848
7849static bool
7850is_unsized_array_last_element(ir_variable *v)
7851{
7852 const glsl_type *interface_type = v->get_interface_type();
7853 int length = interface_type->length;
7854
7855 assert(v->type->is_unsized_array())(static_cast <bool> (v->type->is_unsized_array())
? void (0) : __assert_fail ("v->type->is_unsized_array()"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
7856
7857 /* Check if it is the last element of the interface */
7858 if (strcmp(interface_type->fields.structure[length-1].name, v->name) == 0)
7859 return true;
7860 return false;
7861}
7862
7863static void
7864apply_memory_qualifiers(ir_variable *var, glsl_struct_field field)
7865{
7866 var->data.memory_read_only = field.memory_read_only;
7867 var->data.memory_write_only = field.memory_write_only;
7868 var->data.memory_coherent = field.memory_coherent;
7869 var->data.memory_volatile = field.memory_volatile;
7870 var->data.memory_restrict = field.memory_restrict;
7871}
7872
7873ir_rvalue *
7874ast_interface_block::hir(exec_list *instructions,
7875 struct _mesa_glsl_parse_state *state)
7876{
7877 YYLTYPE loc = this->get_location();
7878
7879 /* Interface blocks must be declared at global scope */
7880 if (state->current_function != NULL__null) {
7881 _mesa_glsl_error(&loc, state,
7882 "Interface block `%s' must be declared "
7883 "at global scope",
7884 this->block_name);
7885 }
7886
7887 /* Validate qualifiers:
7888 *
7889 * - Layout Qualifiers as per the table in Section 4.4
7890 * ("Layout Qualifiers") of the GLSL 4.50 spec.
7891 *
7892 * - Memory Qualifiers as per Section 4.10 ("Memory Qualifiers") of the
7893 * GLSL 4.50 spec:
7894 *
7895 * "Additionally, memory qualifiers may also be used in the declaration
7896 * of shader storage blocks"
7897 *
7898 * Note the table in Section 4.4 says std430 is allowed on both uniform and
7899 * buffer blocks however Section 4.4.5 (Uniform and Shader Storage Block
7900 * Layout Qualifiers) of the GLSL 4.50 spec says:
7901 *
7902 * "The std430 qualifier is supported only for shader storage blocks;
7903 * using std430 on a uniform block will result in a compile-time error."
7904 */
7905 ast_type_qualifier allowed_blk_qualifiers;
7906 allowed_blk_qualifiers.flags.i = 0;
7907 if (this->layout.flags.q.buffer || this->layout.flags.q.uniform) {
7908 allowed_blk_qualifiers.flags.q.shared = 1;
7909 allowed_blk_qualifiers.flags.q.packed = 1;
7910 allowed_blk_qualifiers.flags.q.std140 = 1;
7911 allowed_blk_qualifiers.flags.q.row_major = 1;
7912 allowed_blk_qualifiers.flags.q.column_major = 1;
7913 allowed_blk_qualifiers.flags.q.explicit_align = 1;
7914 allowed_blk_qualifiers.flags.q.explicit_binding = 1;
7915 if (this->layout.flags.q.buffer) {
7916 allowed_blk_qualifiers.flags.q.buffer = 1;
7917 allowed_blk_qualifiers.flags.q.std430 = 1;
7918 allowed_blk_qualifiers.flags.q.coherent = 1;
7919 allowed_blk_qualifiers.flags.q._volatile = 1;
7920 allowed_blk_qualifiers.flags.q.restrict_flag = 1;
7921 allowed_blk_qualifiers.flags.q.read_only = 1;
7922 allowed_blk_qualifiers.flags.q.write_only = 1;
7923 } else {
7924 allowed_blk_qualifiers.flags.q.uniform = 1;
7925 }
7926 } else {
7927 /* Interface block */
7928 assert(this->layout.flags.q.in || this->layout.flags.q.out)(static_cast <bool> (this->layout.flags.q.in || this
->layout.flags.q.out) ? void (0) : __assert_fail ("this->layout.flags.q.in || this->layout.flags.q.out"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
7929
7930 allowed_blk_qualifiers.flags.q.explicit_location = 1;
7931 if (this->layout.flags.q.out) {
7932 allowed_blk_qualifiers.flags.q.out = 1;
7933 if (state->stage == MESA_SHADER_GEOMETRY ||
7934 state->stage == MESA_SHADER_TESS_CTRL ||
7935 state->stage == MESA_SHADER_TESS_EVAL ||
7936 state->stage == MESA_SHADER_VERTEX ) {
7937 allowed_blk_qualifiers.flags.q.explicit_xfb_offset = 1;
7938 allowed_blk_qualifiers.flags.q.explicit_xfb_buffer = 1;
7939 allowed_blk_qualifiers.flags.q.xfb_buffer = 1;
7940 allowed_blk_qualifiers.flags.q.explicit_xfb_stride = 1;
7941 allowed_blk_qualifiers.flags.q.xfb_stride = 1;
7942 if (state->stage == MESA_SHADER_GEOMETRY) {
7943 allowed_blk_qualifiers.flags.q.stream = 1;
7944 allowed_blk_qualifiers.flags.q.explicit_stream = 1;
7945 }
7946 if (state->stage == MESA_SHADER_TESS_CTRL) {
7947 allowed_blk_qualifiers.flags.q.patch = 1;
7948 }
7949 }
7950 } else {
7951 allowed_blk_qualifiers.flags.q.in = 1;
7952 if (state->stage == MESA_SHADER_TESS_EVAL) {
7953 allowed_blk_qualifiers.flags.q.patch = 1;
7954 }
7955 }
7956 }
7957
7958 this->layout.validate_flags(&loc, state, allowed_blk_qualifiers,
7959 "invalid qualifier for block",
7960 this->block_name);
7961
7962 enum glsl_interface_packing packing;
7963 if (this->layout.flags.q.std140) {
7964 packing = GLSL_INTERFACE_PACKING_STD140;
7965 } else if (this->layout.flags.q.packed) {
7966 packing = GLSL_INTERFACE_PACKING_PACKED;
7967 } else if (this->layout.flags.q.std430) {
7968 packing = GLSL_INTERFACE_PACKING_STD430;
7969 } else {
7970 /* The default layout is shared.
7971 */
7972 packing = GLSL_INTERFACE_PACKING_SHARED;
7973 }
7974
7975 ir_variable_mode var_mode;
7976 const char *iface_type_name;
7977 if (this->layout.flags.q.in) {
7978 var_mode = ir_var_shader_in;
7979 iface_type_name = "in";
7980 } else if (this->layout.flags.q.out) {
7981 var_mode = ir_var_shader_out;
7982 iface_type_name = "out";
7983 } else if (this->layout.flags.q.uniform) {
7984 var_mode = ir_var_uniform;
7985 iface_type_name = "uniform";
7986 } else if (this->layout.flags.q.buffer) {
7987 var_mode = ir_var_shader_storage;
7988 iface_type_name = "buffer";
7989 } else {
7990 var_mode = ir_var_auto;
7991 iface_type_name = "UNKNOWN";
Value stored to 'iface_type_name' is never read
7992 assert(!"interface block layout qualifier not found!")(static_cast <bool> (!"interface block layout qualifier not found!"
) ? void (0) : __assert_fail ("!\"interface block layout qualifier not found!\""
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
7993 }
7994
7995 enum glsl_matrix_layout matrix_layout = GLSL_MATRIX_LAYOUT_INHERITED;
7996 if (this->layout.flags.q.row_major)
7997 matrix_layout = GLSL_MATRIX_LAYOUT_ROW_MAJOR;
7998 else if (this->layout.flags.q.column_major)
7999 matrix_layout = GLSL_MATRIX_LAYOUT_COLUMN_MAJOR;
8000
8001 bool redeclaring_per_vertex = strcmp(this->block_name, "gl_PerVertex") == 0;
8002 exec_list declared_variables;
8003 glsl_struct_field *fields;
8004
8005 /* For blocks that accept memory qualifiers (i.e. shader storage), verify
8006 * that we don't have incompatible qualifiers
8007 */
8008 if (this->layout.flags.q.read_only && this->layout.flags.q.write_only) {
8009 _mesa_glsl_error(&loc, state,
8010 "Interface block sets both readonly and writeonly");
8011 }
8012
8013 unsigned qual_stream;
8014 if (!process_qualifier_constant(state, &loc, "stream", this->layout.stream,
8015 &qual_stream) ||
8016 !validate_stream_qualifier(&loc, state, qual_stream)) {
8017 /* If the stream qualifier is invalid it doesn't make sense to continue
8018 * on and try to compare stream layouts on member variables against it
8019 * so just return early.
8020 */
8021 return NULL__null;
8022 }
8023
8024 unsigned qual_xfb_buffer;
8025 if (!process_qualifier_constant(state, &loc, "xfb_buffer",
8026 layout.xfb_buffer, &qual_xfb_buffer) ||
8027 !validate_xfb_buffer_qualifier(&loc, state, qual_xfb_buffer)) {
8028 return NULL__null;
8029 }
8030
8031 unsigned qual_xfb_offset;
8032 if (layout.flags.q.explicit_xfb_offset) {
8033 if (!process_qualifier_constant(state, &loc, "xfb_offset",
8034 layout.offset, &qual_xfb_offset)) {
8035 return NULL__null;
8036 }
8037 }
8038
8039 unsigned qual_xfb_stride;
8040 if (layout.flags.q.explicit_xfb_stride) {
8041 if (!process_qualifier_constant(state, &loc, "xfb_stride",
8042 layout.xfb_stride, &qual_xfb_stride)) {
8043 return NULL__null;
8044 }
8045 }
8046
8047 unsigned expl_location = 0;
8048 if (layout.flags.q.explicit_location) {
8049 if (!process_qualifier_constant(state, &loc, "location",
8050 layout.location, &expl_location)) {
8051 return NULL__null;
8052 } else {
8053 expl_location += this->layout.flags.q.patch ? VARYING_SLOT_PATCH0((VARYING_SLOT_VAR0 + 32))
8054 : VARYING_SLOT_VAR0;
8055 }
8056 }
8057
8058 unsigned expl_align = 0;
8059 if (layout.flags.q.explicit_align) {
8060 if (!process_qualifier_constant(state, &loc, "align",
8061 layout.align, &expl_align)) {
8062 return NULL__null;
8063 } else {
8064 if (expl_align == 0 || expl_align & (expl_align - 1)) {
8065 _mesa_glsl_error(&loc, state, "align layout qualifier is not a "
8066 "power of 2.");
8067 return NULL__null;
8068 }
8069 }
8070 }
8071
8072 unsigned int num_variables =
8073 ast_process_struct_or_iface_block_members(&declared_variables,
8074 state,
8075 &this->declarations,
8076 &fields,
8077 true,
8078 matrix_layout,
8079 redeclaring_per_vertex,
8080 var_mode,
8081 &this->layout,
8082 qual_stream,
8083 qual_xfb_buffer,
8084 qual_xfb_offset,
8085 expl_location,
8086 expl_align);
8087
8088 if (!redeclaring_per_vertex) {
8089 validate_identifier(this->block_name, loc, state);
8090
8091 /* From section 4.3.9 ("Interface Blocks") of the GLSL 4.50 spec:
8092 *
8093 * "Block names have no other use within a shader beyond interface
8094 * matching; it is a compile-time error to use a block name at global
8095 * scope for anything other than as a block name."
8096 */
8097 ir_variable *var = state->symbols->get_variable(this->block_name);
8098 if (var && !var->type->is_interface()) {
8099 _mesa_glsl_error(&loc, state, "Block name `%s' is "
8100 "already used in the scope.",
8101 this->block_name);
8102 }
8103 }
8104
8105 const glsl_type *earlier_per_vertex = NULL__null;
8106 if (redeclaring_per_vertex) {
8107 /* Find the previous declaration of gl_PerVertex. If we're redeclaring
8108 * the named interface block gl_in, we can find it by looking at the
8109 * previous declaration of gl_in. Otherwise we can find it by looking
8110 * at the previous decalartion of any of the built-in outputs,
8111 * e.g. gl_Position.
8112 *
8113 * Also check that the instance name and array-ness of the redeclaration
8114 * are correct.
8115 */
8116 switch (var_mode) {
8117 case ir_var_shader_in:
8118 if (ir_variable *earlier_gl_in =
8119 state->symbols->get_variable("gl_in")) {
8120 earlier_per_vertex = earlier_gl_in->get_interface_type();
8121 } else {
8122 _mesa_glsl_error(&loc, state,
8123 "redeclaration of gl_PerVertex input not allowed "
8124 "in the %s shader",
8125 _mesa_shader_stage_to_string(state->stage));
8126 }
8127 if (this->instance_name == NULL__null ||
8128 strcmp(this->instance_name, "gl_in") != 0 || this->array_specifier == NULL__null ||
8129 !this->array_specifier->is_single_dimension()) {
8130 _mesa_glsl_error(&loc, state,
8131 "gl_PerVertex input must be redeclared as "
8132 "gl_in[]");
8133 }
8134 break;
8135 case ir_var_shader_out:
8136 if (ir_variable *earlier_gl_Position =
8137 state->symbols->get_variable("gl_Position")) {
8138 earlier_per_vertex = earlier_gl_Position->get_interface_type();
8139 } else if (ir_variable *earlier_gl_out =
8140 state->symbols->get_variable("gl_out")) {
8141 earlier_per_vertex = earlier_gl_out->get_interface_type();
8142 } else {
8143 _mesa_glsl_error(&loc, state,
8144 "redeclaration of gl_PerVertex output not "
8145 "allowed in the %s shader",
8146 _mesa_shader_stage_to_string(state->stage));
8147 }
8148 if (state->stage == MESA_SHADER_TESS_CTRL) {
8149 if (this->instance_name == NULL__null ||
8150 strcmp(this->instance_name, "gl_out") != 0 || this->array_specifier == NULL__null) {
8151 _mesa_glsl_error(&loc, state,
8152 "gl_PerVertex output must be redeclared as "
8153 "gl_out[]");
8154 }
8155 } else {
8156 if (this->instance_name != NULL__null) {
8157 _mesa_glsl_error(&loc, state,
8158 "gl_PerVertex output may not be redeclared with "
8159 "an instance name");
8160 }
8161 }
8162 break;
8163 default:
8164 _mesa_glsl_error(&loc, state,
8165 "gl_PerVertex must be declared as an input or an "
8166 "output");
8167 break;
8168 }
8169
8170 if (earlier_per_vertex == NULL__null) {
8171 /* An error has already been reported. Bail out to avoid null
8172 * dereferences later in this function.
8173 */
8174 return NULL__null;
8175 }
8176
8177 /* Copy locations from the old gl_PerVertex interface block. */
8178 for (unsigned i = 0; i < num_variables; i++) {
8179 int j = earlier_per_vertex->field_index(fields[i].name);
8180 if (j == -1) {
8181 _mesa_glsl_error(&loc, state,
8182 "redeclaration of gl_PerVertex must be a subset "
8183 "of the built-in members of gl_PerVertex");
8184 } else {
8185 fields[i].location =
8186 earlier_per_vertex->fields.structure[j].location;
8187 fields[i].offset =
8188 earlier_per_vertex->fields.structure[j].offset;
8189 fields[i].interpolation =
8190 earlier_per_vertex->fields.structure[j].interpolation;
8191 fields[i].centroid =
8192 earlier_per_vertex->fields.structure[j].centroid;
8193 fields[i].sample =
8194 earlier_per_vertex->fields.structure[j].sample;
8195 fields[i].patch =
8196 earlier_per_vertex->fields.structure[j].patch;
8197 fields[i].precision =
8198 earlier_per_vertex->fields.structure[j].precision;
8199 fields[i].explicit_xfb_buffer =
8200 earlier_per_vertex->fields.structure[j].explicit_xfb_buffer;
8201 fields[i].xfb_buffer =
8202 earlier_per_vertex->fields.structure[j].xfb_buffer;
8203 fields[i].xfb_stride =
8204 earlier_per_vertex->fields.structure[j].xfb_stride;
8205 }
8206 }
8207
8208 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10
8209 * spec:
8210 *
8211 * If a built-in interface block is redeclared, it must appear in
8212 * the shader before any use of any member included in the built-in
8213 * declaration, or a compilation error will result.
8214 *
8215 * This appears to be a clarification to the behaviour established for
8216 * gl_PerVertex by GLSL 1.50, therefore we implement this behaviour
8217 * regardless of GLSL version.
8218 */
8219 interface_block_usage_visitor v(var_mode, earlier_per_vertex);
8220 v.run(instructions);
8221 if (v.usage_found()) {
8222 _mesa_glsl_error(&loc, state,
8223 "redeclaration of a built-in interface block must "
8224 "appear before any use of any member of the "
8225 "interface block");
8226 }
8227 }
8228
8229 const glsl_type *block_type =
8230 glsl_type::get_interface_instance(fields,
8231 num_variables,
8232 packing,
8233 matrix_layout ==
8234 GLSL_MATRIX_LAYOUT_ROW_MAJOR,
8235 this->block_name);
8236
8237 unsigned component_size = block_type->contains_double() ? 8 : 4;
8238 int xfb_offset =
8239 layout.flags.q.explicit_xfb_offset ? (int) qual_xfb_offset : -1;
8240 validate_xfb_offset_qualifier(&loc, state, xfb_offset, block_type,
8241 component_size);
8242
8243 if (!state->symbols->add_interface(block_type->name, block_type, var_mode)) {
8244 YYLTYPE loc = this->get_location();
8245 _mesa_glsl_error(&loc, state, "interface block `%s' with type `%s' "
8246 "already taken in the current scope",
8247 this->block_name, iface_type_name);
8248 }
8249
8250 /* Since interface blocks cannot contain statements, it should be
8251 * impossible for the block to generate any instructions.
8252 */
8253 assert(declared_variables.is_empty())(static_cast <bool> (declared_variables.is_empty()) ? void
(0) : __assert_fail ("declared_variables.is_empty()", __builtin_FILE
(), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))
;
8254
8255 /* From section 4.3.4 (Inputs) of the GLSL 1.50 spec:
8256 *
8257 * Geometry shader input variables get the per-vertex values written
8258 * out by vertex shader output variables of the same names. Since a
8259 * geometry shader operates on a set of vertices, each input varying
8260 * variable (or input block, see interface blocks below) needs to be
8261 * declared as an array.
8262 */
8263 if (state->stage == MESA_SHADER_GEOMETRY && this->array_specifier == NULL__null &&
8264 var_mode == ir_var_shader_in) {
8265 _mesa_glsl_error(&loc, state, "geometry shader inputs must be arrays");
8266 } else if ((state->stage == MESA_SHADER_TESS_CTRL ||
8267 state->stage == MESA_SHADER_TESS_EVAL) &&
8268 !this->layout.flags.q.patch &&
8269 this->array_specifier == NULL__null &&
8270 var_mode == ir_var_shader_in) {
8271 _mesa_glsl_error(&loc, state, "per-vertex tessellation shader inputs must be arrays");
8272 } else if (state->stage == MESA_SHADER_TESS_CTRL &&
8273 !this->layout.flags.q.patch &&
8274 this->array_specifier == NULL__null &&
8275 var_mode == ir_var_shader_out) {
8276 _mesa_glsl_error(&loc, state, "tessellation control shader outputs must be arrays");
8277 }
8278
8279
8280 ir_typedecl_statement* stmt = new(state) ir_typedecl_statement(block_type);
8281 /* Push the interface declarations to the top.
8282 * However, do not insert declarations before default precision
8283 * statements or other declarations
8284 */
8285 ir_instruction* before_node = (ir_instruction*)instructions->get_head();
8286 while (before_node &&
8287 (before_node->ir_type == ir_type_precision ||
8288 before_node->ir_type == ir_type_typedecl))
8289 before_node = (ir_instruction*)before_node->next;
8290 if (before_node)
8291 before_node->insert_before(stmt);
8292 else
8293 instructions->push_head(stmt);
8294
8295 /* Page 39 (page 45 of the PDF) of section 4.3.7 in the GLSL ES 3.00 spec
8296 * says:
8297 *
8298 * "If an instance name (instance-name) is used, then it puts all the
8299 * members inside a scope within its own name space, accessed with the
8300 * field selector ( . ) operator (analogously to structures)."
8301 */
8302 if (this->instance_name) {
8303 if (redeclaring_per_vertex) {
8304 /* When a built-in in an unnamed interface block is redeclared,
8305 * get_variable_being_redeclared() calls
8306 * check_builtin_array_max_size() to make sure that built-in array
8307 * variables aren't redeclared to illegal sizes. But we're looking
8308 * at a redeclaration of a named built-in interface block. So we
8309 * have to manually call check_builtin_array_max_size() for all parts
8310 * of the interface that are arrays.
8311 */
8312 for (unsigned i = 0; i < num_variables; i++) {
8313 if (fields[i].type->is_array()) {
8314 const unsigned size = fields[i].type->array_size();
8315 check_builtin_array_max_size(fields[i].name, size, loc, state);
8316 }
8317 }
8318 } else {
8319 validate_identifier(this->instance_name, loc, state);
8320 }
8321
8322 ir_variable *var;
8323
8324 if (this->array_specifier != NULL__null) {
8325 const glsl_type *block_array_type =
8326 process_array_type(&loc, block_type, this->array_specifier, state);
8327
8328 /* Section 4.3.7 (Interface Blocks) of the GLSL 1.50 spec says:
8329 *
8330 * For uniform blocks declared an array, each individual array
8331 * element corresponds to a separate buffer object backing one
8332 * instance of the block. As the array size indicates the number
8333 * of buffer objects needed, uniform block array declarations
8334 * must specify an array size.
8335 *
8336 * And a few paragraphs later:
8337 *
8338 * Geometry shader input blocks must be declared as arrays and
8339 * follow the array declaration and linking rules for all
8340 * geometry shader inputs. All other input and output block
8341 * arrays must specify an array size.
8342 *
8343 * The same applies to tessellation shaders.
8344 *
8345 * The upshot of this is that the only circumstance where an
8346 * interface array size *doesn't* need to be specified is on a
8347 * geometry shader input, tessellation control shader input,
8348 * tessellation control shader output, and tessellation evaluation
8349 * shader input.
8350 */
8351 if (block_array_type->is_unsized_array()) {
8352 bool allow_inputs = state->stage == MESA_SHADER_GEOMETRY ||
8353 state->stage == MESA_SHADER_TESS_CTRL ||
8354 state->stage == MESA_SHADER_TESS_EVAL;
8355 bool allow_outputs = state->stage == MESA_SHADER_TESS_CTRL;
8356
8357 if (this->layout.flags.q.in) {
8358 if (!allow_inputs)
8359 _mesa_glsl_error(&loc, state,
8360 "unsized input block arrays not allowed in "
8361 "%s shader",
8362 _mesa_shader_stage_to_string(state->stage));
8363 } else if (this->layout.flags.q.out) {
8364 if (!allow_outputs)
8365 _mesa_glsl_error(&loc, state,
8366 "unsized output block arrays not allowed in "
8367 "%s shader",
8368 _mesa_shader_stage_to_string(state->stage));
8369 } else {
8370 /* by elimination, this is a uniform block array */
8371 _mesa_glsl_error(&loc, state,
8372 "unsized uniform block arrays not allowed in "
8373 "%s shader",
8374 _mesa_shader_stage_to_string(state->stage));
8375 }
8376 }
8377
8378 /* From section 4.3.9 (Interface Blocks) of the GLSL ES 3.10 spec:
8379 *
8380 * * Arrays of arrays of blocks are not allowed
8381 */
8382 if (state->es_shader && block_array_type->is_array() &&
8383 block_array_type->fields.array->is_array()) {
8384 _mesa_glsl_error(&loc, state,
8385 "arrays of arrays interface blocks are "
8386 "not allowed");
8387 }
8388
8389 var = new(state) ir_variable(block_array_type,
8390 this->instance_name,
8391 var_mode);
8392 } else {
8393 var = new(state) ir_variable(block_type,
8394 this->instance_name,
8395 var_mode);
8396 }
8397
8398 var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED
8399 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout;
8400
8401 if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform)
8402 var->data.read_only = true;
8403
8404 var->data.patch = this->layout.flags.q.patch;
8405
8406 if (state->stage == MESA_SHADER_GEOMETRY && var_mode == ir_var_shader_in)
8407 handle_geometry_shader_input_decl(state, loc, var);
8408 else if ((state->stage == MESA_SHADER_TESS_CTRL ||
8409 state->stage == MESA_SHADER_TESS_EVAL) && var_mode == ir_var_shader_in)
8410 handle_tess_shader_input_decl(state, loc, var);
8411 else if (state->stage == MESA_SHADER_TESS_CTRL && var_mode == ir_var_shader_out)
8412 handle_tess_ctrl_shader_output_decl(state, loc, var);
8413
8414 for (unsigned i = 0; i < num_variables; i++) {
8415 if (var->data.mode == ir_var_shader_storage)
8416 apply_memory_qualifiers(var, fields[i]);
8417 }
8418
8419 if (ir_variable *earlier =
8420 state->symbols->get_variable(this->instance_name)) {
8421 if (!redeclaring_per_vertex) {
8422 _mesa_glsl_error(&loc, state, "`%s' redeclared",
8423 this->instance_name);
8424 }
8425 earlier->data.how_declared = ir_var_declared_normally;
8426 earlier->type = var->type;
8427 earlier->reinit_interface_type(block_type);
8428 delete var;
8429 } else {
8430 if (this->layout.flags.q.explicit_binding) {
8431 apply_explicit_binding(state, &loc, var, var->type,
8432 &this->layout);
8433 }
8434
8435 var->data.stream = qual_stream;
8436 if (layout.flags.q.explicit_location) {
8437 var->data.location = expl_location;
8438 var->data.explicit_location = true;
8439 }
8440
8441 state->symbols->add_variable(var);
8442 instructions->push_tail(var);
8443 }
8444 } else {
8445 /* In order to have an array size, the block must also be declared with
8446 * an instance name.
8447 */
8448 assert(this->array_specifier == NULL)(static_cast <bool> (this->array_specifier == __null
) ? void (0) : __assert_fail ("this->array_specifier == NULL"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
8449
8450 for (unsigned i = 0; i < num_variables; i++) {
8451 ir_variable *var =
8452 new(state) ir_variable(fields[i].type,
8453 ralloc_strdup(state, fields[i].name),
8454 var_mode);
8455 var->data.interpolation = fields[i].interpolation;
8456 var->data.centroid = fields[i].centroid;
8457 var->data.sample = fields[i].sample;
8458 var->data.patch = fields[i].patch;
8459 var->data.stream = qual_stream;
8460 var->data.location = fields[i].location;
8461
8462 if (fields[i].location != -1)
8463 var->data.explicit_location = true;
8464
8465 var->data.explicit_xfb_buffer = fields[i].explicit_xfb_buffer;
8466 var->data.xfb_buffer = fields[i].xfb_buffer;
8467
8468 if (fields[i].offset != -1)
8469 var->data.explicit_xfb_offset = true;
8470 var->data.offset = fields[i].offset;
8471
8472 var->init_interface_type(block_type);
8473
8474 if (var_mode == ir_var_shader_in || var_mode == ir_var_uniform)
8475 var->data.read_only = true;
8476
8477 /* Precision qualifiers do not have any meaning in Desktop GLSL */
8478 if (state->es_shader) {
8479 var->data.precision =
8480 select_gles_precision(fields[i].precision, fields[i].type,
8481 state, &loc);
8482 }
8483
8484 if (fields[i].matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED) {
8485 var->data.matrix_layout = matrix_layout == GLSL_MATRIX_LAYOUT_INHERITED
8486 ? GLSL_MATRIX_LAYOUT_COLUMN_MAJOR : matrix_layout;
8487 } else {
8488 var->data.matrix_layout = fields[i].matrix_layout;
8489 }
8490
8491 if (var->data.mode == ir_var_shader_storage)
8492 apply_memory_qualifiers(var, fields[i]);
8493
8494 /* Examine var name here since var may get deleted in the next call */
8495 bool var_is_gl_id = is_gl_identifier(var->name);
8496
8497 if (redeclaring_per_vertex) {
8498 bool is_redeclaration;
8499 var =
8500 get_variable_being_redeclared(&var, loc, state,
8501 true /* allow_all_redeclarations */,
8502 &is_redeclaration);
8503 if (!var_is_gl_id || !is_redeclaration) {
8504 _mesa_glsl_error(&loc, state,
8505 "redeclaration of gl_PerVertex can only "
8506 "include built-in variables");
8507 } else if (var->data.how_declared == ir_var_declared_normally) {
8508 _mesa_glsl_error(&loc, state,
8509 "`%s' has already been redeclared",
8510 var->name);
8511 } else {
8512 var->data.how_declared = ir_var_declared_in_block;
8513 var->reinit_interface_type(block_type);
8514 }
8515 continue;
8516 }
8517
8518 if (state->symbols->get_variable(var->name) != NULL__null)
8519 _mesa_glsl_error(&loc, state, "`%s' redeclared", var->name);
8520
8521 /* Propagate the "binding" keyword into this UBO/SSBO's fields.
8522 * The UBO declaration itself doesn't get an ir_variable unless it
8523 * has an instance name. This is ugly.
8524 */
8525 if (this->layout.flags.q.explicit_binding) {
8526 apply_explicit_binding(state, &loc, var,
8527 var->get_interface_type(), &this->layout);
8528 }
8529
8530 if (var->type->is_unsized_array()) {
8531 if (var->is_in_shader_storage_block() &&
8532 is_unsized_array_last_element(var)) {
8533 var->data.from_ssbo_unsized_array = true;
8534 } else {
8535 /* From GLSL ES 3.10 spec, section 4.1.9 "Arrays":
8536 *
8537 * "If an array is declared as the last member of a shader storage
8538 * block and the size is not specified at compile-time, it is
8539 * sized at run-time. In all other cases, arrays are sized only
8540 * at compile-time."
8541 *
8542 * In desktop GLSL it is allowed to have unsized-arrays that are
8543 * not last, as long as we can determine that they are implicitly
8544 * sized.
8545 */
8546 if (state->es_shader) {
8547 _mesa_glsl_error(&loc, state, "unsized array `%s' "
8548 "definition: only last member of a shader "
8549 "storage block can be defined as unsized "
8550 "array", fields[i].name);
8551 }
8552 }
8553 }
8554
8555 state->symbols->add_variable(var);
8556 instructions->push_tail(var);
8557 }
8558
8559 if (redeclaring_per_vertex && block_type != earlier_per_vertex) {
8560 /* From section 7.1 ("Built-in Language Variables") of the GLSL 4.10 spec:
8561 *
8562 * It is also a compilation error ... to redeclare a built-in
8563 * block and then use a member from that built-in block that was
8564 * not included in the redeclaration.
8565 *
8566 * This appears to be a clarification to the behaviour established
8567 * for gl_PerVertex by GLSL 1.50, therefore we implement this
8568 * behaviour regardless of GLSL version.
8569 *
8570 * To prevent the shader from using a member that was not included in
8571 * the redeclaration, we disable any ir_variables that are still
8572 * associated with the old declaration of gl_PerVertex (since we've
8573 * already updated all of the variables contained in the new
8574 * gl_PerVertex to point to it).
8575 *
8576 * As a side effect this will prevent
8577 * validate_intrastage_interface_blocks() from getting confused and
8578 * thinking there are conflicting definitions of gl_PerVertex in the
8579 * shader.
8580 */
8581 foreach_in_list_safe(ir_instruction, node, instructions)for (ir_instruction *node = (!exec_node_is_tail_sentinel((instructions
)->head_sentinel.next) ? (ir_instruction *) ((instructions
)->head_sentinel.next) : __null), *__next = (node) ? (!exec_node_is_tail_sentinel
((instructions)->head_sentinel.next->next) ? (ir_instruction
*) ((instructions)->head_sentinel.next->next) : __null
) : __null; (node) != __null; (node) = __next, __next = __next
? (!exec_node_is_tail_sentinel(__next->next) ? (ir_instruction
*) (__next->next) : __null) : __null)
{
8582 ir_variable *const var = node->as_variable();
8583 if (var != NULL__null &&
8584 var->get_interface_type() == earlier_per_vertex &&
8585 var->data.mode == var_mode) {
8586 if (var->data.how_declared == ir_var_declared_normally) {
8587 _mesa_glsl_error(&loc, state,
8588 "redeclaration of gl_PerVertex cannot "
8589 "follow a redeclaration of `%s'",
8590 var->name);
8591 }
8592 state->symbols->disable_variable(var->name);
8593 var->remove();
8594 }
8595 }
8596 }
8597 }
8598
8599 return NULL__null;
8600}
8601
8602
8603ir_rvalue *
8604ast_tcs_output_layout::hir(exec_list *instructions,
8605 struct _mesa_glsl_parse_state *state)
8606{
8607 YYLTYPE loc = this->get_location();
8608
8609 unsigned num_vertices;
8610 if (!state->out_qualifier->vertices->
8611 process_qualifier_constant(state, "vertices", &num_vertices,
8612 false)) {
8613 /* return here to stop cascading incorrect error messages */
8614 return NULL__null;
8615 }
8616
8617 /* If any shader outputs occurred before this declaration and specified an
8618 * array size, make sure the size they specified is consistent with the
8619 * primitive type.
8620 */
8621 if (state->tcs_output_size != 0 && state->tcs_output_size != num_vertices) {
8622 _mesa_glsl_error(&loc, state,
8623 "this tessellation control shader output layout "
8624 "specifies %u vertices, but a previous output "
8625 "is declared with size %u",
8626 num_vertices, state->tcs_output_size);
8627 return NULL__null;
8628 }
8629
8630 state->tcs_output_vertices_specified = true;
8631
8632 /* If any shader outputs occurred before this declaration and did not
8633 * specify an array size, their size is determined now.
8634 */
8635 foreach_in_list (ir_instruction, node, instructions)for (ir_instruction *node = (!exec_node_is_tail_sentinel((instructions
)->head_sentinel.next) ? (ir_instruction *) ((instructions
)->head_sentinel.next) : __null); (node) != __null; (node)
= (!exec_node_is_tail_sentinel((node)->next) ? (ir_instruction
*) ((node)->next) : __null))
{
8636 ir_variable *var = node->as_variable();
8637 if (var == NULL__null || var->data.mode != ir_var_shader_out)
8638 continue;
8639
8640 /* Note: Not all tessellation control shader output are arrays. */
8641 if (!var->type->is_unsized_array() || var->data.patch)
8642 continue;
8643
8644 if (var->data.max_array_access >= (int)num_vertices) {
8645 _mesa_glsl_error(&loc, state,
8646 "this tessellation control shader output layout "
8647 "specifies %u vertices, but an access to element "
8648 "%u of output `%s' already exists", num_vertices,
8649 var->data.max_array_access, var->name);
8650 } else {
8651 var->type = glsl_type::get_array_instance(var->type->fields.array,
8652 num_vertices);
8653 }
8654 }
8655
8656 return NULL__null;
8657}
8658
8659
8660ir_rvalue *
8661ast_gs_input_layout::hir(exec_list *instructions,
8662 struct _mesa_glsl_parse_state *state)
8663{
8664 YYLTYPE loc = this->get_location();
8665
8666 /* Should have been prevented by the parser. */
8667 assert(!state->gs_input_prim_type_specified(static_cast <bool> (!state->gs_input_prim_type_specified
|| state->in_qualifier->prim_type == this->prim_type
) ? void (0) : __assert_fail ("!state->gs_input_prim_type_specified || state->in_qualifier->prim_type == this->prim_type"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
8668 || state->in_qualifier->prim_type == this->prim_type)(static_cast <bool> (!state->gs_input_prim_type_specified
|| state->in_qualifier->prim_type == this->prim_type
) ? void (0) : __assert_fail ("!state->gs_input_prim_type_specified || state->in_qualifier->prim_type == this->prim_type"
, __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__
))
;
8669
8670 /* If any shader inputs occurred before this declaration and specified an
8671 * array size, make sure the size they specified is consistent with the
8672 * primitive type.
8673 */
8674 unsigned num_vertices = vertices_per_prim(this->prim_type);
8675 if (state->gs_input_size != 0 && state->gs_input_size != num_vertices) {
8676 _mesa_glsl_error(&loc, state,
8677 "this geometry shader input layout implies %u vertices"
8678 " per primitive, but a previous input is declared"
8679 " with size %u", num_vertices, state->gs_input_size);
8680 return NULL__null;
8681 }
8682
8683 state->gs_input_prim_type_specified = true;
8684
8685 /* If any shader inputs occurred before this declaration and did not
8686 * specify an array size, their size is determined now.
8687 */
8688 foreach_in_list(ir_instruction, node, instructions)for (ir_instruction *node = (!exec_node_is_tail_sentinel((instructions
)->head_sentinel.next) ? (ir_instruction *) ((instructions
)->head_sentinel.next) : __null); (node) != __null; (node)
= (!exec_node_is_tail_sentinel((node)->next) ? (ir_instruction
*) ((node)->next) : __null))
{
8689 ir_variable *var = node->as_variable();
8690 if (var == NULL__null || var->data.mode != ir_var_shader_in)
8691 continue;
8692
8693 /* Note: gl_PrimitiveIDIn has mode ir_var_shader_in, but it's not an
8694 * array; skip it.
8695 */
8696
8697 if (var->type->is_unsized_array()) {
8698 if (var->data.max_array_access >= (int)num_vertices) {
8699 _mesa_glsl_error(&loc, state,
8700 "this geometry shader input layout implies %u"
8701 " vertices, but an access to element %u of input"
8702 " `%s' already exists", num_vertices,
8703 var->data.max_array_access, var->name);
8704 } else {
8705 var->type = glsl_type::get_array_instance(var->type->fields.array,
8706 num_vertices);
8707 }
8708 }
8709 }
8710
8711 return NULL__null;
8712}
8713
8714
8715ir_rvalue *
8716ast_cs_input_layout::hir(exec_list *instructions,
8717 struct _mesa_glsl_parse_state *state)
8718{
8719 YYLTYPE loc = this->get_location();
8720
8721 /* From the ARB_compute_shader specification:
8722 *
8723 * If the local size of the shader in any dimension is greater
8724 * than the maximum size supported by the implementation for that
8725 * dimension, a compile-time error results.
8726 *
8727 * It is not clear from the spec how the error should be reported if
8728 * the total size of the work group exceeds
8729 * MAX_COMPUTE_WORK_GROUP_INVOCATIONS, but it seems reasonable to
8730 * report it at compile time as well.
8731 */
8732 GLuint64 total_invocations = 1;
8733 unsigned qual_local_size[3];
8734 for (int i = 0; i < 3; i++) {
8735
8736 char *local_size_str = ralloc_asprintf(NULL__null, "invalid local_size_%c",
8737 'x' + i);
8738 /* Infer a local_size of 1 for unspecified dimensions */
8739 if (this->local_size[i] == NULL__null) {
8740 qual_local_size[i] = 1;
8741 } else if (!this->local_size[i]->
8742 process_qualifier_constant(state, local_size_str,
8743 &qual_local_size[i], false)) {
8744 ralloc_free(local_size_str);
8745 return NULL__null;
8746 }
8747 ralloc_free(local_size_str);
8748
8749 if (qual_local_size[i] > state->ctx->Const.MaxComputeWorkGroupSize[i]) {
8750 _mesa_glsl_error(&loc, state,
8751 "local_size_%c exceeds MAX_COMPUTE_WORK_GROUP_SIZE"
8752 " (%d)", 'x' + i,
8753 state->ctx->Const.MaxComputeWorkGroupSize[i]);
8754 break;
8755 }
8756 total_invocations *= qual_local_size[i];
8757 if (total_invocations >
8758 state->ctx->Const.MaxComputeWorkGroupInvocations) {
8759 _mesa_glsl_error(&loc, state,
8760 "product of local_sizes exceeds "
8761 "MAX_COMPUTE_WORK_GROUP_INVOCATIONS (%d)",
8762 state->ctx->Const.MaxComputeWorkGroupInvocations);
8763 break;
8764 }
8765 }
8766
8767 /* If any compute input layout declaration preceded this one, make sure it
8768 * was consistent with this one.
8769 */
8770 if (state->cs_input_local_size_specified) {
8771 for (int i = 0; i < 3; i++) {
8772 if (state->cs_input_local_size[i] != qual_local_size[i]) {
8773 _mesa_glsl_error(&loc, state,
8774 "compute shader input layout does not match"
8775 " previous declaration");
8776 return NULL__null;
8777 }
8778 }
8779 }
8780
8781 /* The ARB_compute_variable_group_size spec says:
8782 *
8783 * If a compute shader including a *local_size_variable* qualifier also
8784 * declares a fixed local group size using the *local_size_x*,
8785 * *local_size_y*, or *local_size_z* qualifiers, a compile-time error
8786 * results
8787 */
8788 if (state->cs_input_local_size_variable_specified) {
8789 _mesa_glsl_error(&loc, state,
8790 "compute shader can't include both a variable and a "
8791 "fixed local group size");
8792 return NULL__null;
8793 }
8794
8795 state->cs_input_local_size_specified = true;
8796 for (int i = 0; i < 3; i++)
8797 state->cs_input_local_size[i] = qual_local_size[i];
8798
8799 /* We may now declare the built-in constant gl_WorkGroupSize (see
8800 * builtin_variable_generator::generate_constants() for why we didn't
8801 * declare it earlier).
8802 */
8803 ir_variable *var = new(state->symbols)
8804 ir_variable(glsl_type::uvec3_type, "gl_WorkGroupSize", ir_var_auto);
8805 var->data.how_declared = ir_var_declared_implicitly;
8806 var->data.read_only = true;
8807 instructions->push_tail(var);
8808 state->symbols->add_variable(var);
8809 ir_constant_data data;
8810 memset(&data, 0, sizeof(data));
8811 for (int i = 0; i < 3; i++)
8812 data.u[i] = qual_local_size[i];
8813 var->constant_value = new(var) ir_constant(glsl_type::uvec3_type, &data);
8814 var->constant_initializer =
8815 new(var) ir_constant(glsl_type::uvec3_type, &data);
8816 var->data.has_initializer = true;
8817
8818 return NULL__null;
8819}
8820
8821
8822static void
8823detect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
8824 exec_list *instructions)
8825{
8826 bool gl_FragColor_assigned = false;
8827 bool gl_FragData_assigned = false;
8828 bool gl_FragSecondaryColor_assigned = false;
8829 bool gl_FragSecondaryData_assigned = false;
8830 bool user_defined_fs_output_assigned = false;
8831 ir_variable *user_defined_fs_output = NULL__null;
8832
8833 /* It would be nice to have proper location information. */
8834 YYLTYPE loc;
8835 memset(&loc, 0, sizeof(loc));
8836
8837 foreach_in_list(ir_instruction, node, instructions)for (ir_instruction *node = (!exec_node_is_tail_sentinel((instructions
)->head_sentinel.next) ? (ir_instruction *) ((instructions
)->head_sentinel.next) : __null); (node) != __null; (node)
= (!exec_node_is_tail_sentinel((node)->next) ? (ir_instruction
*) ((node)->next) : __null))
{
8838 ir_variable *var = node->as_variable();
8839
8840 if (!var || !var->data.assigned)
8841 continue;
8842
8843 if (strcmp(var->name, "gl_FragColor") == 0)
8844 gl_FragColor_assigned = true;
8845 else if (strcmp(var->name, "gl_FragData") == 0)
8846 gl_FragData_assigned = true;
8847 else if (strcmp(var->name, "gl_SecondaryFragColorEXT") == 0)
8848 gl_FragSecondaryColor_assigned = true;
8849 else if (strcmp(var->name, "gl_SecondaryFragDataEXT") == 0)
8850 gl_FragSecondaryData_assigned = true;
8851 else if (!is_gl_identifier(var->name)) {
8852 if (state->stage == MESA_SHADER_FRAGMENT &&
8853 var->data.mode == ir_var_shader_out) {
8854 user_defined_fs_output_assigned = true;
8855 user_defined_fs_output = var;
8856 }
8857 }
8858 }
8859
8860 /* From the GLSL 1.30 spec:
8861 *
8862 * "If a shader statically assigns a value to gl_FragColor, it
8863 * may not assign a value to any element of gl_FragData. If a
8864 * shader statically writes a value to any element of
8865 * gl_FragData, it may not assign a value to
8866 * gl_FragColor. That is, a shader may assign values to either
8867 * gl_FragColor or gl_FragData, but not both. Multiple shaders
8868 * linked together must also consistently write just one of
8869 * these variables. Similarly, if user declared output
8870 * variables are in use (statically assigned to), then the
8871 * built-in variables gl_FragColor and gl_FragData may not be
8872 * assigned to. These incorrect usages all generate compile
8873 * time errors."
8874 */
8875 if (gl_FragColor_assigned && gl_FragData_assigned) {
8876 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
8877 "`gl_FragColor' and `gl_FragData'");
8878 } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
8879 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
8880 "`gl_FragColor' and `%s'",
8881 user_defined_fs_output->name);
8882 } else if (gl_FragSecondaryColor_assigned && gl_FragSecondaryData_assigned) {
8883 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
8884 "`gl_FragSecondaryColorEXT' and"
8885 " `gl_FragSecondaryDataEXT'");
8886 } else if (gl_FragColor_assigned && gl_FragSecondaryData_assigned) {
8887 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
8888 "`gl_FragColor' and"
8889 " `gl_FragSecondaryDataEXT'");
8890 } else if (gl_FragData_assigned && gl_FragSecondaryColor_assigned) {
8891 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
8892 "`gl_FragData' and"
8893 " `gl_FragSecondaryColorEXT'");
8894 } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
8895 _mesa_glsl_error(&loc, state, "fragment shader writes to both "
8896 "`gl_FragData' and `%s'",
8897 user_defined_fs_output->name);
8898 }
8899
8900 if ((gl_FragSecondaryColor_assigned || gl_FragSecondaryData_assigned) &&
8901 !state->EXT_blend_func_extended_enable) {
8902 _mesa_glsl_error(&loc, state,
8903 "Dual source blending requires EXT_blend_func_extended");
8904 }
8905}
8906
8907static void
8908verify_subroutine_associated_funcs(struct _mesa_glsl_parse_state *state)
8909{
8910 YYLTYPE loc;
8911 memset(&loc, 0, sizeof(loc));
8912
8913 /* Section 6.1.2 (Subroutines) of the GLSL 4.00 spec says:
8914 *
8915 * "A program will fail to compile or link if any shader
8916 * or stage contains two or more functions with the same
8917 * name if the name is associated with a subroutine type."
8918 */
8919
8920 for (int i = 0; i < state->num_subroutines; i++) {
8921 unsigned definitions = 0;
8922 ir_function *fn = state->subroutines[i];
8923 /* Calculate number of function definitions with the same name */
8924 foreach_in_list(ir_function_signature, sig, &fn->signatures)for (ir_function_signature *sig = (!exec_node_is_tail_sentinel
((&fn->signatures)->head_sentinel.next) ? (ir_function_signature
*) ((&fn->signatures)->head_sentinel.next) : __null
); (sig) != __null; (sig) = (!exec_node_is_tail_sentinel((sig
)->next) ? (ir_function_signature *) ((sig)->next) : __null
))
{
8925 if (sig->is_defined) {
8926 if (++definitions > 1) {
8927 _mesa_glsl_error(&loc, state,
8928 "%s shader contains two or more function "
8929 "definitions with name `%s', which is "
8930 "associated with a subroutine type.\n",
8931 _mesa_shader_stage_to_string(state->stage),
8932 fn->name);
8933 return;
8934 }
8935 }
8936 }
8937 }
8938}
8939
8940static void
8941remove_per_vertex_blocks(exec_list *instructions,
8942 _mesa_glsl_parse_state *state, ir_variable_mode mode)
8943{
8944 /* Find the gl_PerVertex interface block of the appropriate (in/out) mode,
8945 * if it exists in this shader type.
8946 */
8947 const glsl_type *per_vertex = NULL__null;
8948 switch (mode) {
8949 case ir_var_shader_in:
8950 if (ir_variable *gl_in = state->symbols->get_variable("gl_in"))
8951 per_vertex = gl_in->get_interface_type();
8952 break;
8953 case ir_var_shader_out:
8954 if (ir_variable *gl_Position =
8955 state->symbols->get_variable("gl_Position")) {
8956 per_vertex = gl_Position->get_interface_type();
8957 }
8958 break;
8959 default:
8960 assert(!"Unexpected mode")(static_cast <bool> (!"Unexpected mode") ? void (0) : __assert_fail
("!\"Unexpected mode\"", __builtin_FILE (), __builtin_LINE (
), __extension__ __PRETTY_FUNCTION__))
;
8961 break;
8962 }
8963
8964 /* If we didn't find a built-in gl_PerVertex interface block, then we don't
8965 * need to do anything.
8966 */
8967 if (per_vertex == NULL__null)
8968 return;
8969
8970 /* If the interface block is used by the shader, then we don't need to do
8971 * anything.
8972 */
8973 interface_block_usage_visitor v(mode, per_vertex);
8974 v.run(instructions);
8975 if (v.usage_found())
8976 return;
8977
8978 /* Remove any ir_variable declarations that refer to the interface block
8979 * we're removing.
8980 */
8981 foreach_in_list_safe(ir_instruction, node, instructions)for (ir_instruction *node = (!exec_node_is_tail_sentinel((instructions
)->head_sentinel.next) ? (ir_instruction *) ((instructions
)->head_sentinel.next) : __null), *__next = (node) ? (!exec_node_is_tail_sentinel
((instructions)->head_sentinel.next->next) ? (ir_instruction
*) ((instructions)->head_sentinel.next->next) : __null
) : __null; (node) != __null; (node) = __next, __next = __next
? (!exec_node_is_tail_sentinel(__next->next) ? (ir_instruction
*) (__next->next) : __null) : __null)
{
8982 ir_variable *const var = node->as_variable();
8983 if (var != NULL__null && var->get_interface_type() == per_vertex &&
8984 var->data.mode == mode) {
8985 state->symbols->disable_variable(var->name);
8986 var->remove();
8987 }
8988 }
8989}
8990
8991ir_rvalue *
8992ast_warnings_toggle::hir(exec_list *,
8993 struct _mesa_glsl_parse_state *state)
8994{
8995 state->warnings_enabled = enable;
8996 return NULL__null;
8997}