blob: 7584fdf8c6737f81cbf12f5b6dc588b8369790d5 [file] [log] [blame]
Ian Romanicka87ac252010-02-22 13:19:34 -08001/*
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 */
Eric Anholtac95f2f2010-06-22 10:38:52 -070051
Chia-I Wubfd7c9a2010-08-23 17:51:42 +080052#include "main/core.h" /* for struct gl_extensions */
Ian Romanick8bde4ce2010-03-19 11:57:24 -070053#include "glsl_symbol_table.h"
Ian Romanicka87ac252010-02-22 13:19:34 -080054#include "glsl_parser_extras.h"
55#include "ast.h"
56#include "glsl_types.h"
57#include "ir.h"
58
Ian Romanickd949a9a2010-03-10 09:55:22 -080059void
60_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
61{
Ian Romanickadfb0cd2010-03-10 10:43:16 -080062 _mesa_glsl_initialize_variables(instructions, state);
63
Kenneth Graunke814c89a2010-09-05 00:31:28 -070064 state->symbols->language_version = state->language_version;
65
Ian Romanick41ec6a42010-03-19 17:08:05 -070066 state->current_function = NULL;
67
Paul Berry0d81b0e2011-07-29 15:28:52 -070068 state->toplevel_ir = instructions;
69
Kenneth Graunkea0442852010-08-23 14:52:06 -070070 /* Section 4.2 of the GLSL 1.20 specification states:
71 * "The built-in functions are scoped in a scope outside the global scope
72 * users declare global variables in. That is, a shader's global scope,
73 * available for user-defined functions and global variables, is nested
74 * inside the scope containing the built-in functions."
75 *
76 * Since built-in functions like ftransform() access built-in variables,
77 * it follows that those must be in the outer scope as well.
78 *
79 * We push scope here to create this nesting effect...but don't pop.
80 * This way, a shader's globals are still in the symbol table for use
81 * by the linker.
82 */
83 state->symbols->push_scope();
84
Ian Romanick2b97dc62010-05-10 17:42:05 -070085 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
Ian Romanick304ea902010-05-10 11:17:53 -070086 ast->hir(instructions, state);
Ian Romanick02c5ae12011-07-11 10:46:01 -070087
88 detect_recursion_unlinked(state, instructions);
Paul Berry0d81b0e2011-07-29 15:28:52 -070089
90 state->toplevel_ir = NULL;
Ian Romanickd949a9a2010-03-10 09:55:22 -080091}
92
93
Ian Romanick01045362010-03-29 16:17:56 -070094/**
95 * If a conversion is available, convert one operand to a different type
96 *
97 * The \c from \c ir_rvalue is converted "in place".
98 *
99 * \param to Type that the operand it to be converted to
100 * \param from Operand that is being converted
101 * \param state GLSL compiler state
102 *
103 * \return
104 * If a conversion is possible (or unnecessary), \c true is returned.
105 * Otherwise \c false is returned.
106 */
Kenneth Graunkef32d3df2010-09-01 20:03:17 -0700107bool
Ian Romanickbfb09c22010-03-29 16:32:55 -0700108apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
Ian Romanick01045362010-03-29 16:17:56 -0700109 struct _mesa_glsl_parse_state *state)
110{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700111 void *ctx = state;
Ian Romanickbfb09c22010-03-29 16:32:55 -0700112 if (to->base_type == from->type->base_type)
Ian Romanick01045362010-03-29 16:17:56 -0700113 return true;
114
115 /* This conversion was added in GLSL 1.20. If the compilation mode is
116 * GLSL 1.10, the conversion is skipped.
117 */
118 if (state->language_version < 120)
119 return false;
120
121 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
122 *
123 * "There are no implicit array or structure conversions. For
124 * example, an array of int cannot be implicitly converted to an
125 * array of float. There are no implicit conversions between
126 * signed and unsigned integers."
127 */
128 /* FINISHME: The above comment is partially a lie. There is int/uint
129 * FINISHME: conversion for immediate constants.
130 */
Ian Romanickbfb09c22010-03-29 16:32:55 -0700131 if (!to->is_float() || !from->type->is_numeric())
Ian Romanick01045362010-03-29 16:17:56 -0700132 return false;
133
Kenneth Graunke506199b2010-06-29 15:59:27 -0700134 /* Convert to a floating point type with the same number of components
135 * as the original type - i.e. int to float, not int to vec4.
136 */
137 to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
138 from->type->matrix_columns);
139
Ian Romanickbfb09c22010-03-29 16:32:55 -0700140 switch (from->type->base_type) {
Ian Romanick01045362010-03-29 16:17:56 -0700141 case GLSL_TYPE_INT:
Carl Worth1660a292010-06-23 18:11:51 -0700142 from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700143 break;
144 case GLSL_TYPE_UINT:
Carl Worth1660a292010-06-23 18:11:51 -0700145 from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700146 break;
147 case GLSL_TYPE_BOOL:
Carl Worth1660a292010-06-23 18:11:51 -0700148 from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
Eric Anholtdc58b3f2010-04-02 02:13:43 -1000149 break;
Ian Romanick01045362010-03-29 16:17:56 -0700150 default:
151 assert(0);
152 }
153
154 return true;
155}
156
157
Ian Romanicka87ac252010-02-22 13:19:34 -0800158static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700159arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Ian Romanicka87ac252010-02-22 13:19:34 -0800160 bool multiply,
Eric Anholta13bb142010-03-31 16:38:11 -1000161 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800162{
Eric Anholt336b4ad2010-05-19 10:38:37 -0700163 const glsl_type *type_a = value_a->type;
164 const glsl_type *type_b = value_b->type;
Ian Romanick01045362010-03-29 16:17:56 -0700165
Ian Romanicka87ac252010-02-22 13:19:34 -0800166 /* From GLSL 1.50 spec, page 56:
167 *
168 * "The arithmetic binary operators add (+), subtract (-),
169 * multiply (*), and divide (/) operate on integer and
170 * floating-point scalars, vectors, and matrices."
171 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700172 if (!type_a->is_numeric() || !type_b->is_numeric()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000173 _mesa_glsl_error(loc, state,
174 "Operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700175 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800176 }
177
178
179 /* "If one operand is floating-point based and the other is
180 * not, then the conversions from Section 4.1.10 "Implicit
181 * Conversions" are applied to the non-floating-point-based operand."
Ian Romanicka87ac252010-02-22 13:19:34 -0800182 */
Ian Romanick01045362010-03-29 16:17:56 -0700183 if (!apply_implicit_conversion(type_a, value_b, state)
184 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholta13bb142010-03-31 16:38:11 -1000185 _mesa_glsl_error(loc, state,
186 "Could not implicitly convert operands to "
187 "arithmetic operator");
Ian Romanick01045362010-03-29 16:17:56 -0700188 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800189 }
Eric Anholt336b4ad2010-05-19 10:38:37 -0700190 type_a = value_a->type;
191 type_b = value_b->type;
192
Ian Romanicka87ac252010-02-22 13:19:34 -0800193 /* "If the operands are integer types, they must both be signed or
194 * both be unsigned."
195 *
196 * From this rule and the preceeding conversion it can be inferred that
197 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
Ian Romanick60b54d92010-03-24 17:08:13 -0700198 * The is_numeric check above already filtered out the case where either
199 * type is not one of these, so now the base types need only be tested for
200 * equality.
Ian Romanicka87ac252010-02-22 13:19:34 -0800201 */
202 if (type_a->base_type != type_b->base_type) {
Eric Anholta13bb142010-03-31 16:38:11 -1000203 _mesa_glsl_error(loc, state,
204 "base type mismatch for arithmetic operator");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700205 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800206 }
207
208 /* "All arithmetic binary operators result in the same fundamental type
209 * (signed integer, unsigned integer, or floating-point) as the
210 * operands they operate on, after operand type conversion. After
211 * conversion, the following cases are valid
212 *
213 * * The two operands are scalars. In this case the operation is
214 * applied, resulting in a scalar."
215 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800216 if (type_a->is_scalar() && type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800217 return type_a;
218
219 /* "* One operand is a scalar, and the other is a vector or matrix.
220 * In this case, the scalar operation is applied independently to each
221 * component of the vector or matrix, resulting in the same size
222 * vector or matrix."
223 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800224 if (type_a->is_scalar()) {
225 if (!type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800226 return type_b;
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800227 } else if (type_b->is_scalar()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800228 return type_a;
229 }
230
231 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
232 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
233 * handled.
234 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700235 assert(!type_a->is_scalar());
236 assert(!type_b->is_scalar());
Ian Romanicka87ac252010-02-22 13:19:34 -0800237
238 /* "* The two operands are vectors of the same size. In this case, the
239 * operation is done component-wise resulting in the same size
240 * vector."
241 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800242 if (type_a->is_vector() && type_b->is_vector()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000243 if (type_a == type_b) {
244 return type_a;
245 } else {
246 _mesa_glsl_error(loc, state,
247 "vector size mismatch for arithmetic operator");
248 return glsl_type::error_type;
249 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800250 }
251
252 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
253 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
254 * <vector, vector> have been handled. At least one of the operands must
255 * be matrix. Further, since there are no integer matrix types, the base
256 * type of both operands must be float.
257 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700258 assert(type_a->is_matrix() || type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800259 assert(type_a->base_type == GLSL_TYPE_FLOAT);
260 assert(type_b->base_type == GLSL_TYPE_FLOAT);
261
262 /* "* The operator is add (+), subtract (-), or divide (/), and the
263 * operands are matrices with the same number of rows and the same
264 * number of columns. In this case, the operation is done component-
265 * wise resulting in the same size matrix."
266 * * The operator is multiply (*), where both operands are matrices or
267 * one operand is a vector and the other a matrix. A right vector
268 * operand is treated as a column vector and a left vector operand as a
269 * row vector. In all these cases, it is required that the number of
270 * columns of the left operand is equal to the number of rows of the
271 * right operand. Then, the multiply (*) operation does a linear
272 * algebraic multiply, yielding an object that has the same number of
273 * rows as the left operand and the same number of columns as the right
274 * operand. Section 5.10 "Vector and Matrix Operations" explains in
275 * more detail how vectors and matrices are operated on."
276 */
277 if (! multiply) {
Eric Anholta13bb142010-03-31 16:38:11 -1000278 if (type_a == type_b)
279 return type_a;
Ian Romanicka87ac252010-02-22 13:19:34 -0800280 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800281 if (type_a->is_matrix() && type_b->is_matrix()) {
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700282 /* Matrix multiply. The columns of A must match the rows of B. Given
283 * the other previously tested constraints, this means the vector type
284 * of a row from A must be the same as the vector type of a column from
285 * B.
286 */
287 if (type_a->row_type() == type_b->column_type()) {
288 /* The resulting matrix has the number of columns of matrix B and
289 * the number of rows of matrix A. We get the row count of A by
290 * looking at the size of a vector that makes up a column. The
291 * transpose (size of a row) is done for B.
292 */
Eric Anholta13bb142010-03-31 16:38:11 -1000293 const glsl_type *const type =
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700294 glsl_type::get_instance(type_a->base_type,
295 type_a->column_type()->vector_elements,
296 type_b->row_type()->vector_elements);
Eric Anholta13bb142010-03-31 16:38:11 -1000297 assert(type != glsl_type::error_type);
298
299 return type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800300 }
Ian Romanickfce11502010-03-09 15:58:52 -0800301 } else if (type_a->is_matrix()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800302 /* A is a matrix and B is a column vector. Columns of A must match
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700303 * rows of B. Given the other previously tested constraints, this
304 * means the vector type of a row from A must be the same as the
305 * vector the type of B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800306 */
Carl Worth47c90b12010-07-22 14:56:14 -0700307 if (type_a->row_type() == type_b) {
308 /* The resulting vector has a number of elements equal to
309 * the number of rows of matrix A. */
310 const glsl_type *const type =
311 glsl_type::get_instance(type_a->base_type,
312 type_a->column_type()->vector_elements,
313 1);
314 assert(type != glsl_type::error_type);
315
316 return type;
317 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800318 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800319 assert(type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800320
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700321 /* A is a row vector and B is a matrix. Columns of A must match rows
322 * of B. Given the other previously tested constraints, this means
323 * the type of A must be the same as the vector type of a column from
324 * B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800325 */
Carl Worth47c90b12010-07-22 14:56:14 -0700326 if (type_a == type_b->column_type()) {
327 /* The resulting vector has a number of elements equal to
328 * the number of columns of matrix B. */
329 const glsl_type *const type =
330 glsl_type::get_instance(type_a->base_type,
331 type_b->row_type()->vector_elements,
332 1);
333 assert(type != glsl_type::error_type);
334
335 return type;
336 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800337 }
Eric Anholta13bb142010-03-31 16:38:11 -1000338
339 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
340 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800341 }
342
343
344 /* "All other cases are illegal."
345 */
Eric Anholta13bb142010-03-31 16:38:11 -1000346 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700347 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800348}
349
350
351static const struct glsl_type *
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000352unary_arithmetic_result_type(const struct glsl_type *type,
353 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800354{
355 /* From GLSL 1.50 spec, page 57:
356 *
357 * "The arithmetic unary operators negate (-), post- and pre-increment
358 * and decrement (-- and ++) operate on integer or floating-point
359 * values (including vectors and matrices). All unary operators work
360 * component-wise on their operands. These result with the same type
361 * they operated on."
362 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000363 if (!type->is_numeric()) {
364 _mesa_glsl_error(loc, state,
365 "Operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700366 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000367 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800368
369 return type;
370}
371
Chad Versacecfdbf8b2010-10-15 11:28:05 -0700372/**
373 * \brief Return the result type of a bit-logic operation.
374 *
375 * If the given types to the bit-logic operator are invalid, return
376 * glsl_type::error_type.
377 *
378 * \param type_a Type of LHS of bit-logic op
379 * \param type_b Type of RHS of bit-logic op
380 */
381static const struct glsl_type *
382bit_logic_result_type(const struct glsl_type *type_a,
383 const struct glsl_type *type_b,
384 ast_operators op,
385 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
386{
387 if (state->language_version < 130) {
388 _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
389 return glsl_type::error_type;
390 }
391
392 /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
393 *
394 * "The bitwise operators and (&), exclusive-or (^), and inclusive-or
395 * (|). The operands must be of type signed or unsigned integers or
396 * integer vectors."
397 */
398 if (!type_a->is_integer()) {
399 _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
400 ast_expression::operator_string(op));
401 return glsl_type::error_type;
402 }
403 if (!type_b->is_integer()) {
404 _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
405 ast_expression::operator_string(op));
406 return glsl_type::error_type;
407 }
408
409 /* "The fundamental types of the operands (signed or unsigned) must
410 * match,"
411 */
412 if (type_a->base_type != type_b->base_type) {
413 _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
414 "base type", ast_expression::operator_string(op));
415 return glsl_type::error_type;
416 }
417
418 /* "The operands cannot be vectors of differing size." */
419 if (type_a->is_vector() &&
420 type_b->is_vector() &&
421 type_a->vector_elements != type_b->vector_elements) {
422 _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
423 "different sizes", ast_expression::operator_string(op));
424 return glsl_type::error_type;
425 }
426
427 /* "If one operand is a scalar and the other a vector, the scalar is
428 * applied component-wise to the vector, resulting in the same type as
429 * the vector. The fundamental types of the operands [...] will be the
430 * resulting fundamental type."
431 */
432 if (type_a->is_scalar())
433 return type_b;
434 else
435 return type_a;
436}
Ian Romanicka87ac252010-02-22 13:19:34 -0800437
438static const struct glsl_type *
439modulus_result_type(const struct glsl_type *type_a,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000440 const struct glsl_type *type_b,
441 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800442{
Chad Versace82f994f2011-02-04 12:18:56 -0800443 if (state->language_version < 130) {
444 _mesa_glsl_error(loc, state,
445 "operator '%%' is reserved in %s",
446 state->version_string);
447 return glsl_type::error_type;
448 }
449
Ian Romanicka87ac252010-02-22 13:19:34 -0800450 /* From GLSL 1.50 spec, page 56:
451 * "The operator modulus (%) operates on signed or unsigned integers or
452 * integer vectors. The operand types must both be signed or both be
453 * unsigned."
454 */
Kenneth Graunke8eb97532011-06-14 22:21:41 -0700455 if (!type_a->is_integer()) {
456 _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer.");
457 return glsl_type::error_type;
458 }
459 if (!type_b->is_integer()) {
460 _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer.");
461 return glsl_type::error_type;
462 }
463 if (type_a->base_type != type_b->base_type) {
464 _mesa_glsl_error(loc, state,
465 "operands of %% must have the same base type");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700466 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800467 }
468
469 /* "The operands cannot be vectors of differing size. If one operand is
470 * a scalar and the other vector, then the scalar is applied component-
471 * wise to the vector, resulting in the same type as the vector. If both
472 * are vectors of the same size, the result is computed component-wise."
473 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800474 if (type_a->is_vector()) {
475 if (!type_b->is_vector()
Ian Romanicka87ac252010-02-22 13:19:34 -0800476 || (type_a->vector_elements == type_b->vector_elements))
477 return type_a;
478 } else
479 return type_b;
480
481 /* "The operator modulus (%) is not defined for any other data types
482 * (non-integer types)."
483 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000484 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700485 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800486}
487
488
489static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700490relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000491 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800492{
Eric Anholt336b4ad2010-05-19 10:38:37 -0700493 const glsl_type *type_a = value_a->type;
494 const glsl_type *type_b = value_b->type;
Ian Romanick0150f5f2010-03-29 16:20:07 -0700495
Ian Romanicka87ac252010-02-22 13:19:34 -0800496 /* From GLSL 1.50 spec, page 56:
497 * "The relational operators greater than (>), less than (<), greater
498 * than or equal (>=), and less than or equal (<=) operate only on
499 * scalar integer and scalar floating-point expressions."
500 */
Ian Romanicka6d653d2010-03-26 14:40:37 -0700501 if (!type_a->is_numeric()
502 || !type_b->is_numeric()
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800503 || !type_a->is_scalar()
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000504 || !type_b->is_scalar()) {
505 _mesa_glsl_error(loc, state,
506 "Operands to relational operators must be scalar and "
507 "numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700508 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000509 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800510
511 /* "Either the operands' types must match, or the conversions from
512 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
513 * operand, after which the types must match."
Ian Romanicka87ac252010-02-22 13:19:34 -0800514 */
Ian Romanick0150f5f2010-03-29 16:20:07 -0700515 if (!apply_implicit_conversion(type_a, value_b, state)
516 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000517 _mesa_glsl_error(loc, state,
518 "Could not implicitly convert operands to "
519 "relational operator");
Ian Romanick0150f5f2010-03-29 16:20:07 -0700520 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800521 }
Eric Anholt336b4ad2010-05-19 10:38:37 -0700522 type_a = value_a->type;
523 type_b = value_b->type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800524
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000525 if (type_a->base_type != type_b->base_type) {
526 _mesa_glsl_error(loc, state, "base type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700527 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000528 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800529
530 /* "The result is scalar Boolean."
531 */
Ian Romanick0471e8b2010-03-26 14:33:41 -0700532 return glsl_type::bool_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800533}
534
Chad Versacec0197ab2010-10-15 09:49:46 -0700535/**
536 * \brief Return the result type of a bit-shift operation.
537 *
538 * If the given types to the bit-shift operator are invalid, return
539 * glsl_type::error_type.
540 *
541 * \param type_a Type of LHS of bit-shift op
542 * \param type_b Type of RHS of bit-shift op
543 */
544static const struct glsl_type *
545shift_result_type(const struct glsl_type *type_a,
546 const struct glsl_type *type_b,
547 ast_operators op,
548 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
549{
550 if (state->language_version < 130) {
551 _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
552 return glsl_type::error_type;
553 }
554
555 /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
556 *
557 * "The shift operators (<<) and (>>). For both operators, the operands
558 * must be signed or unsigned integers or integer vectors. One operand
559 * can be signed while the other is unsigned."
560 */
561 if (!type_a->is_integer()) {
562 _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
563 "integer vector", ast_expression::operator_string(op));
564 return glsl_type::error_type;
565
566 }
567 if (!type_b->is_integer()) {
568 _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
569 "integer vector", ast_expression::operator_string(op));
570 return glsl_type::error_type;
571 }
572
573 /* "If the first operand is a scalar, the second operand has to be
574 * a scalar as well."
575 */
576 if (type_a->is_scalar() && !type_b->is_scalar()) {
577 _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the "
578 "second must be scalar as well",
579 ast_expression::operator_string(op));
580 return glsl_type::error_type;
581 }
582
583 /* If both operands are vectors, check that they have same number of
584 * elements.
585 */
586 if (type_a->is_vector() &&
587 type_b->is_vector() &&
588 type_a->vector_elements != type_b->vector_elements) {
589 _mesa_glsl_error(loc, state, "Vector operands to operator %s must "
590 "have same number of elements",
591 ast_expression::operator_string(op));
592 return glsl_type::error_type;
593 }
594
595 /* "In all cases, the resulting type will be the same type as the left
596 * operand."
597 */
598 return type_a;
599}
Ian Romanicka87ac252010-02-22 13:19:34 -0800600
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700601/**
602 * Validates that a value can be assigned to a location with a specified type
603 *
604 * Validates that \c rhs can be assigned to some location. If the types are
605 * not an exact match but an automatic conversion is possible, \c rhs will be
606 * converted.
607 *
608 * \return
609 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
610 * Otherwise the actual RHS to be assigned will be returned. This may be
611 * \c rhs, or it may be \c rhs after some type conversion.
612 *
613 * \note
614 * In addition to being used for assignments, this function is used to
615 * type-check return values.
616 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700617ir_rvalue *
Eric Anholt336b4ad2010-05-19 10:38:37 -0700618validate_assignment(struct _mesa_glsl_parse_state *state,
Ian Romanick85caea22011-03-15 16:33:27 -0700619 const glsl_type *lhs_type, ir_rvalue *rhs,
620 bool is_initializer)
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700621{
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700622 /* If there is already some error in the RHS, just return it. Anything
623 * else will lead to an avalanche of error message back to the user.
624 */
Ian Romanickec530102010-12-10 15:47:11 -0800625 if (rhs->type->is_error())
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700626 return rhs;
627
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700628 /* If the types are identical, the assignment can trivially proceed.
629 */
Ian Romanickec530102010-12-10 15:47:11 -0800630 if (rhs->type == lhs_type)
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700631 return rhs;
632
Ian Romanick0157f412010-04-02 17:44:39 -0700633 /* If the array element types are the same and the size of the LHS is zero,
Ian Romanick85caea22011-03-15 16:33:27 -0700634 * the assignment is okay for initializers embedded in variable
635 * declarations.
Ian Romanick0157f412010-04-02 17:44:39 -0700636 *
637 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
638 * is handled by ir_dereference::is_lvalue.
639 */
Ian Romanick85caea22011-03-15 16:33:27 -0700640 if (is_initializer && lhs_type->is_array() && rhs->type->is_array()
Ian Romanick0157f412010-04-02 17:44:39 -0700641 && (lhs_type->element_type() == rhs->type->element_type())
642 && (lhs_type->array_size() == 0)) {
643 return rhs;
644 }
645
Eric Anholt336b4ad2010-05-19 10:38:37 -0700646 /* Check for implicit conversion in GLSL 1.20 */
647 if (apply_implicit_conversion(lhs_type, rhs, state)) {
Ian Romanickec530102010-12-10 15:47:11 -0800648 if (rhs->type == lhs_type)
Eric Anholt336b4ad2010-05-19 10:38:37 -0700649 return rhs;
650 }
651
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700652 return NULL;
653}
654
Eric Anholta313c292011-08-05 21:40:50 -0700655static void
656mark_whole_array_access(ir_rvalue *access)
657{
658 ir_dereference_variable *deref = access->as_dereference_variable();
659
660 if (deref && deref->var) {
661 deref->var->max_array_access = deref->type->length - 1;
662 }
663}
664
Eric Anholt10a68522010-03-26 11:53:37 -0700665ir_rvalue *
666do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
Ian Romanick85caea22011-03-15 16:33:27 -0700667 ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer,
Eric Anholt10a68522010-03-26 11:53:37 -0700668 YYLTYPE lhs_loc)
669{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700670 void *ctx = state;
Eric Anholt10a68522010-03-26 11:53:37 -0700671 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
672
673 if (!error_emitted) {
Chad Versaceb66be752011-01-21 13:44:08 -0800674 if (lhs->variable_referenced() != NULL
675 && lhs->variable_referenced()->read_only) {
676 _mesa_glsl_error(&lhs_loc, state,
677 "assignment to read-only variable '%s'",
678 lhs->variable_referenced()->name);
679 error_emitted = true;
680
Eric Anholt525cec92011-09-07 12:03:36 -0700681 } else if (state->language_version <= 110 && lhs->type->is_array()) {
682 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
683 *
684 * "Other binary or unary expressions, non-dereferenced
685 * arrays, function names, swizzles with repeated fields,
686 * and constants cannot be l-values."
687 */
688 _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not "
689 "allowed in GLSL 1.10 or GLSL ES 1.00.");
690 error_emitted = true;
Chad Versaceb66be752011-01-21 13:44:08 -0800691 } else if (!lhs->is_lvalue()) {
Eric Anholt10a68522010-03-26 11:53:37 -0700692 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
693 error_emitted = true;
694 }
695 }
696
Ian Romanick85caea22011-03-15 16:33:27 -0700697 ir_rvalue *new_rhs =
698 validate_assignment(state, lhs->type, rhs, is_initializer);
Eric Anholt10a68522010-03-26 11:53:37 -0700699 if (new_rhs == NULL) {
700 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
701 } else {
702 rhs = new_rhs;
Ian Romanick0157f412010-04-02 17:44:39 -0700703
704 /* If the LHS array was not declared with a size, it takes it size from
705 * the RHS. If the LHS is an l-value and a whole array, it must be a
706 * dereference of a variable. Any other case would require that the LHS
707 * is either not an l-value or not a whole array.
708 */
709 if (lhs->type->array_size() == 0) {
710 ir_dereference *const d = lhs->as_dereference();
711
712 assert(d != NULL);
713
Ian Romanick36ea2862010-05-19 13:52:29 +0200714 ir_variable *const var = d->variable_referenced();
Ian Romanick0157f412010-04-02 17:44:39 -0700715
716 assert(var != NULL);
717
Ian Romanick63f39422010-04-05 14:35:47 -0700718 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
719 /* FINISHME: This should actually log the location of the RHS. */
720 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
721 "previous access",
722 var->max_array_access);
723 }
724
Ian Romanickf38d15b2010-07-20 15:33:40 -0700725 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
Ian Romanick0157f412010-04-02 17:44:39 -0700726 rhs->type->array_size());
Eric Anholt9703ed02010-07-22 15:50:37 -0700727 d->type = var->type;
Ian Romanick0157f412010-04-02 17:44:39 -0700728 }
Eric Anholt407a1002011-09-07 11:53:20 -0700729 mark_whole_array_access(rhs);
Eric Anholta313c292011-08-05 21:40:50 -0700730 mark_whole_array_access(lhs);
Eric Anholt10a68522010-03-26 11:53:37 -0700731 }
732
Eric Anholt2731a732010-06-23 14:51:14 -0700733 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
734 * but not post_inc) need the converted assigned value as an rvalue
735 * to handle things like:
736 *
737 * i = j += 1;
738 *
739 * So we always just store the computed value being assigned to a
740 * temporary and return a deref of that temporary. If the rvalue
741 * ends up not being used, the temp will get copy-propagated out.
742 */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700743 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
744 ir_var_temporary);
Eric Anholte33c1032010-06-24 15:13:03 -0700745 ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
Eric Anholtae805922010-06-24 09:06:12 -0700746 instructions->push_tail(var);
Eric Anholte33c1032010-06-24 15:13:03 -0700747 instructions->push_tail(new(ctx) ir_assignment(deref_var,
Carl Worth1660a292010-06-23 18:11:51 -0700748 rhs,
749 NULL));
Eric Anholte33c1032010-06-24 15:13:03 -0700750 deref_var = new(ctx) ir_dereference_variable(var);
Eric Anholt10a68522010-03-26 11:53:37 -0700751
Ian Romanick8e9ce2e2010-08-03 15:02:35 -0700752 if (!error_emitted)
753 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
Eric Anholt2731a732010-06-23 14:51:14 -0700754
Carl Worth1660a292010-06-23 18:11:51 -0700755 return new(ctx) ir_dereference_variable(var);
Eric Anholt10a68522010-03-26 11:53:37 -0700756}
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700757
Eric Anholtde38f0e2010-03-26 12:14:54 -0700758static ir_rvalue *
Eric Anholt959a9ec2010-06-23 14:43:50 -0700759get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
Eric Anholtde38f0e2010-03-26 12:14:54 -0700760{
Kenneth Graunked3073f52011-01-21 14:32:31 -0800761 void *ctx = ralloc_parent(lvalue);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700762 ir_variable *var;
Eric Anholtde38f0e2010-03-26 12:14:54 -0700763
Ian Romanick7e2aa912010-07-19 17:12:42 -0700764 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
765 ir_var_temporary);
Eric Anholt43b5b032010-07-07 14:04:30 -0700766 instructions->push_tail(var);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700767 var->mode = ir_var_auto;
768
Carl Worth1660a292010-06-23 18:11:51 -0700769 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
770 lvalue, NULL));
Eric Anholtde38f0e2010-03-26 12:14:54 -0700771
772 /* Once we've created this temporary, mark it read only so it's no
773 * longer considered an lvalue.
774 */
775 var->read_only = true;
776
Carl Worth1660a292010-06-23 18:11:51 -0700777 return new(ctx) ir_dereference_variable(var);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700778}
779
780
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700781ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -0800782ast_node::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -0800783 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -0800784{
Ian Romanick18238de2010-03-01 13:49:10 -0800785 (void) instructions;
786 (void) state;
787
788 return NULL;
789}
790
Eric Anholtff796332010-11-30 11:23:28 -0800791static ir_rvalue *
792do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
793{
794 int join_op;
Ian Romanick6d36be52010-12-02 12:17:36 -0800795 ir_rvalue *cmp = NULL;
Eric Anholtff796332010-11-30 11:23:28 -0800796
797 if (operation == ir_binop_all_equal)
798 join_op = ir_binop_logic_and;
799 else
800 join_op = ir_binop_logic_or;
801
802 switch (op0->type->base_type) {
803 case GLSL_TYPE_FLOAT:
804 case GLSL_TYPE_UINT:
805 case GLSL_TYPE_INT:
806 case GLSL_TYPE_BOOL:
807 return new(mem_ctx) ir_expression(operation, op0, op1);
808
809 case GLSL_TYPE_ARRAY: {
Eric Anholtff796332010-11-30 11:23:28 -0800810 for (unsigned int i = 0; i < op0->type->length; i++) {
811 ir_rvalue *e0, *e1, *result;
812
813 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
814 new(mem_ctx) ir_constant(i));
815 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
816 new(mem_ctx) ir_constant(i));
817 result = do_comparison(mem_ctx, operation, e0, e1);
818
Ian Romanick6d36be52010-12-02 12:17:36 -0800819 if (cmp) {
820 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
Eric Anholtff796332010-11-30 11:23:28 -0800821 } else {
Ian Romanick6d36be52010-12-02 12:17:36 -0800822 cmp = result;
Eric Anholtff796332010-11-30 11:23:28 -0800823 }
824 }
Eric Anholtb4f58562010-12-01 15:55:53 -0800825
826 mark_whole_array_access(op0);
827 mark_whole_array_access(op1);
Ian Romanick6d36be52010-12-02 12:17:36 -0800828 break;
Eric Anholtff796332010-11-30 11:23:28 -0800829 }
830
831 case GLSL_TYPE_STRUCT: {
Eric Anholtff796332010-11-30 11:23:28 -0800832 for (unsigned int i = 0; i < op0->type->length; i++) {
833 ir_rvalue *e0, *e1, *result;
834 const char *field_name = op0->type->fields.structure[i].name;
835
836 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
837 field_name);
838 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
839 field_name);
840 result = do_comparison(mem_ctx, operation, e0, e1);
841
Ian Romanick6d36be52010-12-02 12:17:36 -0800842 if (cmp) {
843 cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
Eric Anholtff796332010-11-30 11:23:28 -0800844 } else {
Ian Romanick6d36be52010-12-02 12:17:36 -0800845 cmp = result;
Eric Anholtff796332010-11-30 11:23:28 -0800846 }
847 }
Ian Romanick6d36be52010-12-02 12:17:36 -0800848 break;
Eric Anholtff796332010-11-30 11:23:28 -0800849 }
850
851 case GLSL_TYPE_ERROR:
852 case GLSL_TYPE_VOID:
853 case GLSL_TYPE_SAMPLER:
854 /* I assume a comparison of a struct containing a sampler just
855 * ignores the sampler present in the type.
856 */
Ian Romanick6d36be52010-12-02 12:17:36 -0800857 break;
858
859 default:
860 assert(!"Should not get here.");
861 break;
Eric Anholtff796332010-11-30 11:23:28 -0800862 }
Eric Anholtd56c9742010-11-30 13:28:47 -0800863
Ian Romanick6d36be52010-12-02 12:17:36 -0800864 if (cmp == NULL)
865 cmp = new(mem_ctx) ir_constant(true);
866
867 return cmp;
Eric Anholtff796332010-11-30 11:23:28 -0800868}
Ian Romanick18238de2010-03-01 13:49:10 -0800869
Eric Anholt01822702011-04-09 15:59:20 -1000870/* For logical operations, we want to ensure that the operands are
871 * scalar booleans. If it isn't, emit an error and return a constant
872 * boolean to avoid triggering cascading error messages.
873 */
874ir_rvalue *
875get_scalar_boolean_operand(exec_list *instructions,
876 struct _mesa_glsl_parse_state *state,
877 ast_expression *parent_expr,
878 int operand,
879 const char *operand_name,
880 bool *error_emitted)
881{
882 ast_expression *expr = parent_expr->subexpressions[operand];
883 void *ctx = state;
884 ir_rvalue *val = expr->hir(instructions, state);
885
886 if (val->type->is_boolean() && val->type->is_scalar())
887 return val;
888
889 if (!*error_emitted) {
890 YYLTYPE loc = expr->get_location();
891 _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
892 operand_name,
893 parent_expr->operator_string(parent_expr->oper));
894 *error_emitted = true;
895 }
896
897 return new(ctx) ir_constant(true);
898}
899
Paul Berry93b97582011-09-06 10:01:51 -0700900/**
901 * If name refers to a builtin array whose maximum allowed size is less than
902 * size, report an error and return true. Otherwise return false.
903 */
904static bool
905check_builtin_array_max_size(const char *name, unsigned size,
906 YYLTYPE loc, struct _mesa_glsl_parse_state *state)
907{
908 if ((strcmp("gl_TexCoord", name) == 0)
909 && (size > state->Const.MaxTextureCoords)) {
910 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
911 *
912 * "The size [of gl_TexCoord] can be at most
913 * gl_MaxTextureCoords."
914 */
915 _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
916 "be larger than gl_MaxTextureCoords (%u)\n",
917 state->Const.MaxTextureCoords);
918 return true;
Paul Berry37bb1c42011-08-11 15:23:33 -0700919 } else if (strcmp("gl_ClipDistance", name) == 0
920 && size > state->Const.MaxClipPlanes) {
921 /* From section 7.1 (Vertex Shader Special Variables) of the
922 * GLSL 1.30 spec:
923 *
924 * "The gl_ClipDistance array is predeclared as unsized and
925 * must be sized by the shader either redeclaring it with a
926 * size or indexing it only with integral constant
927 * expressions. ... The size can be at most
928 * gl_MaxClipDistances."
929 */
930 _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
931 "be larger than gl_MaxClipDistances (%u)\n",
932 state->Const.MaxClipPlanes);
933 return true;
Paul Berry93b97582011-09-06 10:01:51 -0700934 }
935 return false;
936}
937
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700938ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -0800939ast_expression::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -0800940 struct _mesa_glsl_parse_state *state)
941{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700942 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -0800943 static const int operations[AST_NUM_OPERATORS] = {
944 -1, /* ast_assign doesn't convert to ir_expression. */
945 -1, /* ast_plus doesn't convert to ir_expression. */
946 ir_unop_neg,
947 ir_binop_add,
948 ir_binop_sub,
949 ir_binop_mul,
950 ir_binop_div,
951 ir_binop_mod,
952 ir_binop_lshift,
953 ir_binop_rshift,
954 ir_binop_less,
955 ir_binop_greater,
956 ir_binop_lequal,
957 ir_binop_gequal,
Luca Barbieri4dfb8992010-09-08 01:31:39 +0200958 ir_binop_all_equal,
959 ir_binop_any_nequal,
Ian Romanicka87ac252010-02-22 13:19:34 -0800960 ir_binop_bit_and,
961 ir_binop_bit_xor,
962 ir_binop_bit_or,
963 ir_unop_bit_not,
964 ir_binop_logic_and,
965 ir_binop_logic_xor,
966 ir_binop_logic_or,
967 ir_unop_logic_not,
968
969 /* Note: The following block of expression types actually convert
970 * to multiple IR instructions.
971 */
972 ir_binop_mul, /* ast_mul_assign */
973 ir_binop_div, /* ast_div_assign */
974 ir_binop_mod, /* ast_mod_assign */
975 ir_binop_add, /* ast_add_assign */
976 ir_binop_sub, /* ast_sub_assign */
977 ir_binop_lshift, /* ast_ls_assign */
978 ir_binop_rshift, /* ast_rs_assign */
979 ir_binop_bit_and, /* ast_and_assign */
980 ir_binop_bit_xor, /* ast_xor_assign */
981 ir_binop_bit_or, /* ast_or_assign */
982
983 -1, /* ast_conditional doesn't convert to ir_expression. */
Eric Anholtde38f0e2010-03-26 12:14:54 -0700984 ir_binop_add, /* ast_pre_inc. */
985 ir_binop_sub, /* ast_pre_dec. */
986 ir_binop_add, /* ast_post_inc. */
987 ir_binop_sub, /* ast_post_dec. */
Ian Romanicka87ac252010-02-22 13:19:34 -0800988 -1, /* ast_field_selection doesn't conv to ir_expression. */
989 -1, /* ast_array_index doesn't convert to ir_expression. */
990 -1, /* ast_function_call doesn't conv to ir_expression. */
991 -1, /* ast_identifier doesn't convert to ir_expression. */
992 -1, /* ast_int_constant doesn't convert to ir_expression. */
993 -1, /* ast_uint_constant doesn't conv to ir_expression. */
994 -1, /* ast_float_constant doesn't conv to ir_expression. */
995 -1, /* ast_bool_constant doesn't conv to ir_expression. */
996 -1, /* ast_sequence doesn't convert to ir_expression. */
997 };
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700998 ir_rvalue *result = NULL;
Aras Pranckevicius1c325af2010-07-29 15:35:22 +0300999 ir_rvalue *op[3];
Kenneth Graunke08ba9772011-04-14 17:21:59 -07001000 const struct glsl_type *type; /* a temporary variable for switch cases */
Ian Romanicka87ac252010-02-22 13:19:34 -08001001 bool error_emitted = false;
1002 YYLTYPE loc;
1003
Ian Romanick18238de2010-03-01 13:49:10 -08001004 loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08001005
Ian Romanick18238de2010-03-01 13:49:10 -08001006 switch (this->oper) {
Ian Romanick6652af32010-03-09 16:38:02 -08001007 case ast_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -08001008 op[0] = this->subexpressions[0]->hir(instructions, state);
1009 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001010
Ian Romanick85caea22011-03-15 16:33:27 -07001011 result = do_assignment(instructions, state, op[0], op[1], false,
Eric Anholt10a68522010-03-26 11:53:37 -07001012 this->subexpressions[0]->get_location());
1013 error_emitted = result->type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001014 break;
Ian Romanick6652af32010-03-09 16:38:02 -08001015 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001016
1017 case ast_plus:
Ian Romanick18238de2010-03-01 13:49:10 -08001018 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001019
Carl Worthc24bcad2010-07-21 11:23:51 -07001020 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1021
1022 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001023
1024 result = op[0];
1025 break;
1026
1027 case ast_neg:
Ian Romanick18238de2010-03-01 13:49:10 -08001028 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001029
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001030 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001031
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001032 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001033
Carl Worth1660a292010-06-23 18:11:51 -07001034 result = new(ctx) ir_expression(operations[this->oper], type,
1035 op[0], NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08001036 break;
1037
1038 case ast_add:
1039 case ast_sub:
1040 case ast_mul:
1041 case ast_div:
Ian Romanick18238de2010-03-01 13:49:10 -08001042 op[0] = this->subexpressions[0]->hir(instructions, state);
1043 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001044
Ian Romanickbfb09c22010-03-29 16:32:55 -07001045 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -08001046 (this->oper == ast_mul),
Eric Anholta13bb142010-03-31 16:38:11 -10001047 state, & loc);
1048 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001049
Carl Worth1660a292010-06-23 18:11:51 -07001050 result = new(ctx) ir_expression(operations[this->oper], type,
1051 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -08001052 break;
1053
1054 case ast_mod:
Ian Romanick18238de2010-03-01 13:49:10 -08001055 op[0] = this->subexpressions[0]->hir(instructions, state);
1056 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001057
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001058 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001059
Ian Romanick18238de2010-03-01 13:49:10 -08001060 assert(operations[this->oper] == ir_binop_mod);
Ian Romanicka87ac252010-02-22 13:19:34 -08001061
Carl Worth1660a292010-06-23 18:11:51 -07001062 result = new(ctx) ir_expression(operations[this->oper], type,
1063 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001064 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001065 break;
1066
1067 case ast_lshift:
1068 case ast_rshift:
Chad Versace5c4c36f2010-10-08 16:22:28 -07001069 if (state->language_version < 130) {
1070 _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30",
1071 operator_string(this->oper));
1072 error_emitted = true;
Chad Versace5c4c36f2010-10-08 16:22:28 -07001073 }
1074
Chad Versace5c4c36f2010-10-08 16:22:28 -07001075 op[0] = this->subexpressions[0]->hir(instructions, state);
1076 op[1] = this->subexpressions[1]->hir(instructions, state);
Chad Versacec0197ab2010-10-15 09:49:46 -07001077 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1078 &loc);
Chad Versace5c4c36f2010-10-08 16:22:28 -07001079 result = new(ctx) ir_expression(operations[this->oper], type,
1080 op[0], op[1]);
1081 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1082 break;
Ian Romanicka87ac252010-02-22 13:19:34 -08001083
1084 case ast_less:
1085 case ast_greater:
1086 case ast_lequal:
1087 case ast_gequal:
Ian Romanick18238de2010-03-01 13:49:10 -08001088 op[0] = this->subexpressions[0]->hir(instructions, state);
1089 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001090
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001091 type = relational_result_type(op[0], op[1], state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001092
1093 /* The relational operators must either generate an error or result
1094 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
1095 */
Ian Romanicka43817a2010-03-26 14:27:23 -07001096 assert(type->is_error()
Ian Romanicka87ac252010-02-22 13:19:34 -08001097 || ((type->base_type == GLSL_TYPE_BOOL)
Ian Romanickcb36f8a2010-03-09 15:51:22 -08001098 && type->is_scalar()));
Ian Romanicka87ac252010-02-22 13:19:34 -08001099
Carl Worth1660a292010-06-23 18:11:51 -07001100 result = new(ctx) ir_expression(operations[this->oper], type,
1101 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001102 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001103 break;
1104
1105 case ast_nequal:
1106 case ast_equal:
Ian Romanick6e659ca2010-03-29 15:11:05 -07001107 op[0] = this->subexpressions[0]->hir(instructions, state);
1108 op[1] = this->subexpressions[1]->hir(instructions, state);
1109
1110 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
1111 *
1112 * "The equality operators equal (==), and not equal (!=)
1113 * operate on all types. They result in a scalar Boolean. If
1114 * the operand types do not match, then there must be a
1115 * conversion from Section 4.1.10 "Implicit Conversions"
1116 * applied to one operand that can make them match, in which
1117 * case this conversion is done."
1118 */
Ian Romanickbfb09c22010-03-29 16:32:55 -07001119 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1120 && !apply_implicit_conversion(op[1]->type, op[0], state))
Ian Romanick212b0322010-03-29 16:22:38 -07001121 || (op[0]->type != op[1]->type)) {
Ian Romanick6e659ca2010-03-29 15:11:05 -07001122 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
1123 "type", (this->oper == ast_equal) ? "==" : "!=");
1124 error_emitted = true;
Ian Romanicka80cbd62010-03-30 17:04:48 -07001125 } else if ((state->language_version <= 110)
1126 && (op[0]->type->is_array() || op[1]->type->is_array())) {
1127 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
1128 "GLSL 1.10");
1129 error_emitted = true;
Ian Romanick6e659ca2010-03-29 15:11:05 -07001130 }
1131
Eric Anholt175829f2011-04-09 12:54:34 -10001132 if (error_emitted) {
1133 result = new(ctx) ir_constant(false);
1134 } else {
1135 result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
1136 assert(result->type == glsl_type::bool_type);
Eric Anholt175829f2011-04-09 12:54:34 -10001137 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001138 break;
1139
1140 case ast_bit_and:
1141 case ast_bit_xor:
1142 case ast_bit_or:
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001143 op[0] = this->subexpressions[0]->hir(instructions, state);
1144 op[1] = this->subexpressions[1]->hir(instructions, state);
Chad Versacecfdbf8b2010-10-15 11:28:05 -07001145 type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1146 state, &loc);
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001147 result = new(ctx) ir_expression(operations[this->oper], type,
1148 op[0], op[1]);
1149 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1150 break;
1151
Ian Romanicka87ac252010-02-22 13:19:34 -08001152 case ast_bit_not:
Kenneth Graunke1eea9632010-08-31 10:56:24 -07001153 op[0] = this->subexpressions[0]->hir(instructions, state);
1154
1155 if (state->language_version < 130) {
1156 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
1157 error_emitted = true;
1158 }
1159
1160 if (!op[0]->type->is_integer()) {
1161 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
1162 error_emitted = true;
1163 }
1164
1165 type = op[0]->type;
1166 result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08001167 break;
1168
Eric Anholt4950a682010-04-16 01:10:32 -07001169 case ast_logic_and: {
Eric Anholt7ec0c972011-04-09 10:27:02 -10001170 exec_list rhs_instructions;
Eric Anholt01822702011-04-09 15:59:20 -10001171 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1172 "LHS", &error_emitted);
Eric Anholt7ec0c972011-04-09 10:27:02 -10001173 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1174 "RHS", &error_emitted);
Eric Anholtb82c0c32010-03-31 09:11:39 -10001175
Eric Anholt44b694e2010-04-16 13:49:04 -07001176 ir_constant *op0_const = op[0]->constant_expression_value();
1177 if (op0_const) {
1178 if (op0_const->value.b[0]) {
Eric Anholt7ec0c972011-04-09 10:27:02 -10001179 instructions->append_list(&rhs_instructions);
1180 result = op[1];
Eric Anholt44b694e2010-04-16 13:49:04 -07001181 } else {
1182 result = op0_const;
1183 }
1184 type = glsl_type::bool_type;
1185 } else {
Ian Romanick81d664f2010-07-12 15:18:55 -07001186 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
Ian Romanick7e2aa912010-07-19 17:12:42 -07001187 "and_tmp",
1188 ir_var_temporary);
Ian Romanick81d664f2010-07-12 15:18:55 -07001189 instructions->push_tail(tmp);
1190
Carl Worth1660a292010-06-23 18:11:51 -07001191 ir_if *const stmt = new(ctx) ir_if(op[0]);
Eric Anholt44b694e2010-04-16 13:49:04 -07001192 instructions->push_tail(stmt);
Eric Anholt4950a682010-04-16 01:10:32 -07001193
Eric Anholt7ec0c972011-04-09 10:27:02 -10001194 stmt->then_instructions.append_list(&rhs_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001195 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001196 ir_assignment *const then_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001197 new(ctx) ir_assignment(then_deref, op[1], NULL);
Eric Anholt44b694e2010-04-16 13:49:04 -07001198 stmt->then_instructions.push_tail(then_assign);
1199
Carl Worth1660a292010-06-23 18:11:51 -07001200 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001201 ir_assignment *const else_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001202 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
Eric Anholt44b694e2010-04-16 13:49:04 -07001203 stmt->else_instructions.push_tail(else_assign);
1204
Carl Worth1660a292010-06-23 18:11:51 -07001205 result = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001206 type = tmp->type;
Eric Anholtb82c0c32010-03-31 09:11:39 -10001207 }
Eric Anholt4950a682010-04-16 01:10:32 -07001208 break;
1209 }
1210
1211 case ast_logic_or: {
Eric Anholt9e04b192011-04-09 10:27:02 -10001212 exec_list rhs_instructions;
Eric Anholt01822702011-04-09 15:59:20 -10001213 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1214 "LHS", &error_emitted);
Eric Anholt9e04b192011-04-09 10:27:02 -10001215 op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
1216 "RHS", &error_emitted);
Eric Anholt4950a682010-04-16 01:10:32 -07001217
Eric Anholt44b694e2010-04-16 13:49:04 -07001218 ir_constant *op0_const = op[0]->constant_expression_value();
1219 if (op0_const) {
1220 if (op0_const->value.b[0]) {
1221 result = op0_const;
1222 } else {
Eric Anholt9e04b192011-04-09 10:27:02 -10001223 result = op[1];
Eric Anholt44b694e2010-04-16 13:49:04 -07001224 }
1225 type = glsl_type::bool_type;
1226 } else {
Kenneth Graunkedfd30ca2010-07-08 12:40:52 -07001227 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
Ian Romanick7e2aa912010-07-19 17:12:42 -07001228 "or_tmp",
1229 ir_var_temporary);
Ian Romanick0b9ae3b2010-07-12 14:22:05 -07001230 instructions->push_tail(tmp);
Eric Anholt4950a682010-04-16 01:10:32 -07001231
Ian Romanick81d664f2010-07-12 15:18:55 -07001232 ir_if *const stmt = new(ctx) ir_if(op[0]);
1233 instructions->push_tail(stmt);
1234
Carl Worth1660a292010-06-23 18:11:51 -07001235 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001236 ir_assignment *const then_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001237 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
Eric Anholt44b694e2010-04-16 13:49:04 -07001238 stmt->then_instructions.push_tail(then_assign);
1239
Eric Anholt9e04b192011-04-09 10:27:02 -10001240 stmt->else_instructions.append_list(&rhs_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001241 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001242 ir_assignment *const else_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001243 new(ctx) ir_assignment(else_deref, op[1], NULL);
Eric Anholt44b694e2010-04-16 13:49:04 -07001244 stmt->else_instructions.push_tail(else_assign);
1245
Carl Worth1660a292010-06-23 18:11:51 -07001246 result = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -07001247 type = tmp->type;
Eric Anholt4950a682010-04-16 01:10:32 -07001248 }
Eric Anholt4950a682010-04-16 01:10:32 -07001249 break;
1250 }
1251
1252 case ast_logic_xor:
Eric Anholt756c2622011-04-09 14:57:17 -10001253 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1254 *
1255 * "The logical binary operators and (&&), or ( | | ), and
1256 * exclusive or (^^). They operate only on two Boolean
1257 * expressions and result in a Boolean expression."
1258 */
1259 op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
1260 &error_emitted);
1261 op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
1262 &error_emitted);
Eric Anholt4950a682010-04-16 01:10:32 -07001263
Carl Worth1660a292010-06-23 18:11:51 -07001264 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1265 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -08001266 break;
1267
Eric Anholta5827fe2010-03-31 16:50:55 -10001268 case ast_logic_not:
Eric Anholt01822702011-04-09 15:59:20 -10001269 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1270 "operand", &error_emitted);
Eric Anholta5827fe2010-03-31 16:50:55 -10001271
Carl Worth1660a292010-06-23 18:11:51 -07001272 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
1273 op[0], NULL);
Eric Anholta5827fe2010-03-31 16:50:55 -10001274 break;
1275
Ian Romanicka87ac252010-02-22 13:19:34 -08001276 case ast_mul_assign:
1277 case ast_div_assign:
1278 case ast_add_assign:
1279 case ast_sub_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -08001280 op[0] = this->subexpressions[0]->hir(instructions, state);
1281 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001282
Ian Romanickbfb09c22010-03-29 16:32:55 -07001283 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -08001284 (this->oper == ast_mul_assign),
Eric Anholta13bb142010-03-31 16:38:11 -10001285 state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001286
Carl Worth1660a292010-06-23 18:11:51 -07001287 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1288 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -08001289
Eric Anholt3e24ef62010-06-23 12:40:17 -07001290 result = do_assignment(instructions, state,
Ian Romanick85caea22011-03-15 16:33:27 -07001291 op[0]->clone(ctx, NULL), temp_rhs, false,
Eric Anholt10a68522010-03-26 11:53:37 -07001292 this->subexpressions[0]->get_location());
Eric Anholt10a68522010-03-26 11:53:37 -07001293 error_emitted = (op[0]->type->is_error());
Ian Romanicka87ac252010-02-22 13:19:34 -08001294
1295 /* GLSL 1.10 does not allow array assignment. However, we don't have to
1296 * explicitly test for this because none of the binary expression
1297 * operators allow array operands either.
1298 */
1299
Ian Romanicka87ac252010-02-22 13:19:34 -08001300 break;
1301 }
1302
Eric Anholt48a0e642010-03-26 11:57:46 -07001303 case ast_mod_assign: {
1304 op[0] = this->subexpressions[0]->hir(instructions, state);
1305 op[1] = this->subexpressions[1]->hir(instructions, state);
1306
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001307 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Eric Anholt48a0e642010-03-26 11:57:46 -07001308
1309 assert(operations[this->oper] == ir_binop_mod);
1310
Ian Romanick768b55a2010-08-13 16:46:43 -07001311 ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001312 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1313 op[0], op[1]);
Eric Anholt48a0e642010-03-26 11:57:46 -07001314
Eric Anholt3e24ef62010-06-23 12:40:17 -07001315 result = do_assignment(instructions, state,
Ian Romanick85caea22011-03-15 16:33:27 -07001316 op[0]->clone(ctx, NULL), temp_rhs, false,
Eric Anholt48a0e642010-03-26 11:57:46 -07001317 this->subexpressions[0]->get_location());
Eric Anholt65e1a7ac2010-03-31 16:45:20 -10001318 error_emitted = type->is_error();
Eric Anholt48a0e642010-03-26 11:57:46 -07001319 break;
1320 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001321
1322 case ast_ls_assign:
Chad Versace338ed6e2010-10-15 10:05:50 -07001323 case ast_rs_assign: {
1324 op[0] = this->subexpressions[0]->hir(instructions, state);
1325 op[1] = this->subexpressions[1]->hir(instructions, state);
1326 type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1327 &loc);
1328 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1329 type, op[0], op[1]);
1330 result = do_assignment(instructions, state, op[0]->clone(ctx, NULL),
Ian Romanick85caea22011-03-15 16:33:27 -07001331 temp_rhs, false,
Chad Versace338ed6e2010-10-15 10:05:50 -07001332 this->subexpressions[0]->get_location());
1333 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
Ian Romanick251eb752010-03-29 14:15:05 -07001334 break;
Chad Versace338ed6e2010-10-15 10:05:50 -07001335 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001336
1337 case ast_and_assign:
1338 case ast_xor_assign:
Chad Versaced03ac0f2010-10-15 12:08:28 -07001339 case ast_or_assign: {
1340 op[0] = this->subexpressions[0]->hir(instructions, state);
1341 op[1] = this->subexpressions[1]->hir(instructions, state);
1342 type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1343 state, &loc);
1344 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1345 type, op[0], op[1]);
1346 result = do_assignment(instructions, state, op[0]->clone(ctx, NULL),
Ian Romanick85caea22011-03-15 16:33:27 -07001347 temp_rhs, false,
Chad Versaced03ac0f2010-10-15 12:08:28 -07001348 this->subexpressions[0]->get_location());
1349 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
Ian Romanick251eb752010-03-29 14:15:05 -07001350 break;
Chad Versaced03ac0f2010-10-15 12:08:28 -07001351 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001352
Ian Romanick96f9cea2010-03-29 15:33:54 -07001353 case ast_conditional: {
Ian Romanick96f9cea2010-03-29 15:33:54 -07001354 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1355 *
1356 * "The ternary selection operator (?:). It operates on three
1357 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1358 * first expression, which must result in a scalar Boolean."
1359 */
Eric Anholt01822702011-04-09 15:59:20 -10001360 op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
1361 "condition", &error_emitted);
Ian Romanick96f9cea2010-03-29 15:33:54 -07001362
1363 /* The :? operator is implemented by generating an anonymous temporary
1364 * followed by an if-statement. The last instruction in each branch of
1365 * the if-statement assigns a value to the anonymous temporary. This
1366 * temporary is the r-value of the expression.
1367 */
Ian Romanick0ad76c62010-06-11 12:56:26 -07001368 exec_list then_instructions;
1369 exec_list else_instructions;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001370
Ian Romanick0ad76c62010-06-11 12:56:26 -07001371 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1372 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
Ian Romanick96f9cea2010-03-29 15:33:54 -07001373
1374 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1375 *
1376 * "The second and third expressions can be any type, as
1377 * long their types match, or there is a conversion in
1378 * Section 4.1.10 "Implicit Conversions" that can be applied
1379 * to one of the expressions to make their types match. This
1380 * resulting matching type is the type of the entire
1381 * expression."
1382 */
Ian Romanickbfb09c22010-03-29 16:32:55 -07001383 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1384 && !apply_implicit_conversion(op[2]->type, op[1], state))
Ian Romanickdb9be2e2010-03-29 16:25:56 -07001385 || (op[1]->type != op[2]->type)) {
Ian Romanick96f9cea2010-03-29 15:33:54 -07001386 YYLTYPE loc = this->subexpressions[1]->get_location();
1387
1388 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1389 "operator must have matching types.");
1390 error_emitted = true;
Ian Romanick0ad76c62010-06-11 12:56:26 -07001391 type = glsl_type::error_type;
Ian Romanickdb9be2e2010-03-29 16:25:56 -07001392 } else {
Ian Romanick0ad76c62010-06-11 12:56:26 -07001393 type = op[1]->type;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001394 }
1395
Ian Romanickf09fabc2010-09-07 14:30:06 -07001396 /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1397 *
1398 * "The second and third expressions must be the same type, but can
1399 * be of any type other than an array."
1400 */
1401 if ((state->language_version <= 110) && type->is_array()) {
1402 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1403 "operator must not be arrays.");
1404 error_emitted = true;
1405 }
1406
Ian Romanick7825d3d2010-06-11 13:45:51 -07001407 ir_constant *cond_val = op[0]->constant_expression_value();
1408 ir_constant *then_val = op[1]->constant_expression_value();
1409 ir_constant *else_val = op[2]->constant_expression_value();
Ian Romanick0ad76c62010-06-11 12:56:26 -07001410
Ian Romanick7825d3d2010-06-11 13:45:51 -07001411 if (then_instructions.is_empty()
1412 && else_instructions.is_empty()
1413 && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
1414 result = (cond_val->value.b[0]) ? then_val : else_val;
1415 } else {
Ian Romanick7e2aa912010-07-19 17:12:42 -07001416 ir_variable *const tmp =
1417 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
Ian Romanick0b9ae3b2010-07-12 14:22:05 -07001418 instructions->push_tail(tmp);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001419
Carl Worth1660a292010-06-23 18:11:51 -07001420 ir_if *const stmt = new(ctx) ir_if(op[0]);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001421 instructions->push_tail(stmt);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001422
Ian Romanick7825d3d2010-06-11 13:45:51 -07001423 then_instructions.move_nodes_to(& stmt->then_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001424 ir_dereference *const then_deref =
1425 new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001426 ir_assignment *const then_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001427 new(ctx) ir_assignment(then_deref, op[1], NULL);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001428 stmt->then_instructions.push_tail(then_assign);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001429
Ian Romanick7825d3d2010-06-11 13:45:51 -07001430 else_instructions.move_nodes_to(& stmt->else_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001431 ir_dereference *const else_deref =
1432 new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001433 ir_assignment *const else_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001434 new(ctx) ir_assignment(else_deref, op[2], NULL);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001435 stmt->else_instructions.push_tail(else_assign);
1436
Carl Worth1660a292010-06-23 18:11:51 -07001437 result = new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001438 }
Ian Romanick251eb752010-03-29 14:15:05 -07001439 break;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001440 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001441
1442 case ast_pre_inc:
Eric Anholt76ea56c2010-03-26 12:16:54 -07001443 case ast_pre_dec: {
1444 op[0] = this->subexpressions[0]->hir(instructions, state);
1445 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
Carl Worth1660a292010-06-23 18:11:51 -07001446 op[1] = new(ctx) ir_constant(1.0f);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001447 else
Carl Worth1660a292010-06-23 18:11:51 -07001448 op[1] = new(ctx) ir_constant(1);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001449
Eric Anholta13bb142010-03-31 16:38:11 -10001450 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001451
Ian Romanick768b55a2010-08-13 16:46:43 -07001452 ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001453 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1454 op[0], op[1]);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001455
Eric Anholt3e24ef62010-06-23 12:40:17 -07001456 result = do_assignment(instructions, state,
Ian Romanick85caea22011-03-15 16:33:27 -07001457 op[0]->clone(ctx, NULL), temp_rhs, false,
Eric Anholt76ea56c2010-03-26 12:16:54 -07001458 this->subexpressions[0]->get_location());
Eric Anholt76ea56c2010-03-26 12:16:54 -07001459 error_emitted = op[0]->type->is_error();
1460 break;
1461 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001462
1463 case ast_post_inc:
Eric Anholtde38f0e2010-03-26 12:14:54 -07001464 case ast_post_dec: {
1465 op[0] = this->subexpressions[0]->hir(instructions, state);
1466 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
Carl Worth1660a292010-06-23 18:11:51 -07001467 op[1] = new(ctx) ir_constant(1.0f);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001468 else
Carl Worth1660a292010-06-23 18:11:51 -07001469 op[1] = new(ctx) ir_constant(1);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001470
1471 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1472
Eric Anholta13bb142010-03-31 16:38:11 -10001473 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001474
Ian Romanick768b55a2010-08-13 16:46:43 -07001475 ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001476 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1477 op[0], op[1]);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001478
1479 /* Get a temporary of a copy of the lvalue before it's modified.
1480 * This may get thrown away later.
1481 */
Eric Anholt8273bd42010-08-04 12:34:56 -07001482 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
Eric Anholtde38f0e2010-03-26 12:14:54 -07001483
Eric Anholt3e24ef62010-06-23 12:40:17 -07001484 (void)do_assignment(instructions, state,
Ian Romanick85caea22011-03-15 16:33:27 -07001485 op[0]->clone(ctx, NULL), temp_rhs, false,
Eric Anholtde38f0e2010-03-26 12:14:54 -07001486 this->subexpressions[0]->get_location());
1487
Eric Anholtde38f0e2010-03-26 12:14:54 -07001488 error_emitted = op[0]->type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001489 break;
Eric Anholtde38f0e2010-03-26 12:14:54 -07001490 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001491
1492 case ast_field_selection:
Ian Romanick18238de2010-03-01 13:49:10 -08001493 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001494 break;
1495
Ian Romanick27e3cf82010-04-01 18:03:59 -07001496 case ast_array_index: {
1497 YYLTYPE index_loc = subexpressions[1]->get_location();
1498
1499 op[0] = subexpressions[0]->hir(instructions, state);
1500 op[1] = subexpressions[1]->hir(instructions, state);
1501
1502 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1503
Ian Romanicka9159f92010-05-26 15:08:11 -07001504 ir_rvalue *const array = op[0];
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001505
Carl Worth1660a292010-06-23 18:11:51 -07001506 result = new(ctx) ir_dereference_array(op[0], op[1]);
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001507
1508 /* Do not use op[0] after this point. Use array.
1509 */
1510 op[0] = NULL;
1511
Ian Romanick27e3cf82010-04-01 18:03:59 -07001512
1513 if (error_emitted)
1514 break;
1515
Ian Romanick63038e12010-04-05 13:16:00 -07001516 if (!array->type->is_array()
1517 && !array->type->is_matrix()
1518 && !array->type->is_vector()) {
Ian Romanick27e3cf82010-04-01 18:03:59 -07001519 _mesa_glsl_error(& index_loc, state,
Ian Romanick63038e12010-04-05 13:16:00 -07001520 "cannot dereference non-array / non-matrix / "
1521 "non-vector");
Ian Romanick27e3cf82010-04-01 18:03:59 -07001522 error_emitted = true;
1523 }
1524
1525 if (!op[1]->type->is_integer()) {
1526 _mesa_glsl_error(& index_loc, state,
1527 "array index must be integer type");
1528 error_emitted = true;
1529 } else if (!op[1]->type->is_scalar()) {
1530 _mesa_glsl_error(& index_loc, state,
1531 "array index must be scalar");
1532 error_emitted = true;
1533 }
1534
1535 /* If the array index is a constant expression and the array has a
1536 * declared size, ensure that the access is in-bounds. If the array
1537 * index is not a constant expression, ensure that the array has a
1538 * declared size.
1539 */
1540 ir_constant *const const_index = op[1]->constant_expression_value();
1541 if (const_index != NULL) {
1542 const int idx = const_index->value.i[0];
Ian Romanick63038e12010-04-05 13:16:00 -07001543 const char *type_name;
1544 unsigned bound = 0;
1545
1546 if (array->type->is_matrix()) {
1547 type_name = "matrix";
1548 } else if (array->type->is_vector()) {
1549 type_name = "vector";
1550 } else {
1551 type_name = "array";
1552 }
Ian Romanick27e3cf82010-04-01 18:03:59 -07001553
1554 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1555 *
1556 * "It is illegal to declare an array with a size, and then
1557 * later (in the same shader) index the same array with an
1558 * integral constant expression greater than or equal to the
1559 * declared size. It is also illegal to index an array with a
1560 * negative constant expression."
1561 */
Ian Romanick63038e12010-04-05 13:16:00 -07001562 if (array->type->is_matrix()) {
1563 if (array->type->row_type()->vector_elements <= idx) {
1564 bound = array->type->row_type()->vector_elements;
1565 }
1566 } else if (array->type->is_vector()) {
1567 if (array->type->vector_elements <= idx) {
1568 bound = array->type->vector_elements;
1569 }
1570 } else {
1571 if ((array->type->array_size() > 0)
1572 && (array->type->array_size() <= idx)) {
1573 bound = array->type->array_size();
1574 }
1575 }
1576
1577 if (bound > 0) {
1578 _mesa_glsl_error(& loc, state, "%s index must be < %u",
1579 type_name, bound);
1580 error_emitted = true;
1581 } else if (idx < 0) {
1582 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
1583 type_name);
Ian Romanick27e3cf82010-04-01 18:03:59 -07001584 error_emitted = true;
1585 }
1586
Ian Romanick63038e12010-04-05 13:16:00 -07001587 if (array->type->is_array()) {
Ian Romanicka9159f92010-05-26 15:08:11 -07001588 /* If the array is a variable dereference, it dereferences the
1589 * whole array, by definition. Use this to get the variable.
1590 *
1591 * FINISHME: Should some methods for getting / setting / testing
1592 * FINISHME: array access limits be added to ir_dereference?
1593 */
1594 ir_variable *const v = array->whole_variable_referenced();
Paul Berry93b97582011-09-06 10:01:51 -07001595 if ((v != NULL) && (unsigned(idx) > v->max_array_access)) {
Ian Romanick63038e12010-04-05 13:16:00 -07001596 v->max_array_access = idx;
Paul Berry93b97582011-09-06 10:01:51 -07001597
1598 /* Check whether this access will, as a side effect, implicitly
1599 * cause the size of a built-in array to be too large.
1600 */
1601 if (check_builtin_array_max_size(v->name, idx+1, loc, state))
1602 error_emitted = true;
1603 }
Ian Romanick27e3cf82010-04-01 18:03:59 -07001604 }
Kenneth Graunke2b7c42b2010-07-16 18:28:44 -07001605 } else if (array->type->array_size() == 0) {
1606 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
Eric Anholta721abf2010-08-23 10:32:01 -07001607 } else {
1608 if (array->type->is_array()) {
Aras Pranckevicius5226f8c2010-08-25 22:42:13 +03001609 /* whole_variable_referenced can return NULL if the array is a
1610 * member of a structure. In this case it is safe to not update
1611 * the max_array_access field because it is never used for fields
1612 * of structures.
1613 */
Eric Anholta721abf2010-08-23 10:32:01 -07001614 ir_variable *v = array->whole_variable_referenced();
Aras Pranckevicius5226f8c2010-08-25 22:42:13 +03001615 if (v != NULL)
Ian Romanick0d9d0362011-03-24 16:50:23 -07001616 v->max_array_access = array->type->array_size() - 1;
Eric Anholta721abf2010-08-23 10:32:01 -07001617 }
Ian Romanick27e3cf82010-04-01 18:03:59 -07001618 }
1619
Ian Romanicke942f322011-01-04 16:09:00 -08001620 /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
1621 *
Chad Versacef0f2ec42010-12-07 10:35:36 -08001622 * "Samplers aggregated into arrays within a shader (using square
1623 * brackets [ ]) can only be indexed with integral constant
1624 * expressions [...]."
Ian Romanicke942f322011-01-04 16:09:00 -08001625 *
1626 * This restriction was added in GLSL 1.30. Shaders using earlier version
1627 * of the language should not be rejected by the compiler front-end for
1628 * using this construct. This allows useful things such as using a loop
1629 * counter as the index to an array of samplers. If the loop in unrolled,
1630 * the code should compile correctly. Instead, emit a warning.
Chad Versacef0f2ec42010-12-07 10:35:36 -08001631 */
1632 if (array->type->is_array() &&
1633 array->type->element_type()->is_sampler() &&
1634 const_index == NULL) {
1635
Ian Romanicke942f322011-01-04 16:09:00 -08001636 if (state->language_version == 100) {
1637 _mesa_glsl_warning(&loc, state,
1638 "sampler arrays indexed with non-constant "
1639 "expressions is optional in GLSL ES 1.00");
1640 } else if (state->language_version < 130) {
1641 _mesa_glsl_warning(&loc, state,
1642 "sampler arrays indexed with non-constant "
1643 "expressions is forbidden in GLSL 1.30 and "
1644 "later");
1645 } else {
1646 _mesa_glsl_error(&loc, state,
1647 "sampler arrays indexed with non-constant "
1648 "expressions is forbidden in GLSL 1.30 and "
1649 "later");
1650 error_emitted = true;
1651 }
Chad Versacef0f2ec42010-12-07 10:35:36 -08001652 }
1653
Ian Romanick27e3cf82010-04-01 18:03:59 -07001654 if (error_emitted)
1655 result->type = glsl_type::error_type;
1656
Ian Romanicka87ac252010-02-22 13:19:34 -08001657 break;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001658 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001659
1660 case ast_function_call:
Ian Romanick7cfddf12010-03-10 13:26:52 -08001661 /* Should *NEVER* get here. ast_function_call should always be handled
1662 * by ast_function_expression::hir.
Ian Romanicka87ac252010-02-22 13:19:34 -08001663 */
Ian Romanick7cfddf12010-03-10 13:26:52 -08001664 assert(0);
Ian Romanicka87ac252010-02-22 13:19:34 -08001665 break;
1666
1667 case ast_identifier: {
1668 /* ast_identifier can appear several places in a full abstract syntax
1669 * tree. This particular use must be at location specified in the grammar
1670 * as 'variable_identifier'.
1671 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001672 ir_variable *var =
1673 state->symbols->get_variable(this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001674
Carl Worth1660a292010-06-23 18:11:51 -07001675 result = new(ctx) ir_dereference_variable(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08001676
1677 if (var != NULL) {
Ian Romanickbd330552011-01-07 18:34:58 -08001678 var->used = true;
Ian Romanicka87ac252010-02-22 13:19:34 -08001679 } else {
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001680 _mesa_glsl_error(& loc, state, "`%s' undeclared",
Ian Romanick18238de2010-03-01 13:49:10 -08001681 this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001682
1683 error_emitted = true;
1684 }
1685 break;
1686 }
1687
1688 case ast_int_constant:
Carl Worth1660a292010-06-23 18:11:51 -07001689 result = new(ctx) ir_constant(this->primary_expression.int_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001690 break;
1691
1692 case ast_uint_constant:
Carl Worth1660a292010-06-23 18:11:51 -07001693 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001694 break;
1695
1696 case ast_float_constant:
Carl Worth1660a292010-06-23 18:11:51 -07001697 result = new(ctx) ir_constant(this->primary_expression.float_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001698 break;
1699
1700 case ast_bool_constant:
Carl Worth1660a292010-06-23 18:11:51 -07001701 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
Ian Romanicka87ac252010-02-22 13:19:34 -08001702 break;
1703
1704 case ast_sequence: {
Ian Romanicka87ac252010-02-22 13:19:34 -08001705 /* It should not be possible to generate a sequence in the AST without
1706 * any expressions in it.
1707 */
Ian Romanick304ea902010-05-10 11:17:53 -07001708 assert(!this->expressions.is_empty());
Ian Romanicka87ac252010-02-22 13:19:34 -08001709
1710 /* The r-value of a sequence is the last expression in the sequence. If
1711 * the other expressions in the sequence do not have side-effects (and
1712 * therefore add instructions to the instruction list), they get dropped
1713 * on the floor.
1714 */
Ian Romanick3d5cfcf2011-04-11 10:10:30 -07001715 exec_node *previous_tail_pred = NULL;
1716 YYLTYPE previous_operand_loc = loc;
1717
1718 foreach_list_typed (ast_node, ast, link, &this->expressions) {
1719 /* If one of the operands of comma operator does not generate any
1720 * code, we want to emit a warning. At each pass through the loop
1721 * previous_tail_pred will point to the last instruction in the
1722 * stream *before* processing the previous operand. Naturally,
1723 * instructions->tail_pred will point to the last instruction in the
1724 * stream *after* processing the previous operand. If the two
1725 * pointers match, then the previous operand had no effect.
1726 *
1727 * The warning behavior here differs slightly from GCC. GCC will
1728 * only emit a warning if none of the left-hand operands have an
1729 * effect. However, it will emit a warning for each. I believe that
1730 * there are some cases in C (especially with GCC extensions) where
1731 * it is useful to have an intermediate step in a sequence have no
1732 * effect, but I don't think these cases exist in GLSL. Either way,
1733 * it would be a giant hassle to replicate that behavior.
1734 */
1735 if (previous_tail_pred == instructions->tail_pred) {
1736 _mesa_glsl_warning(&previous_operand_loc, state,
1737 "left-hand operand of comma expression has "
1738 "no effect");
1739 }
1740
1741 /* tail_pred is directly accessed instead of using the get_tail()
1742 * method for performance reasons. get_tail() has extra code to
1743 * return NULL when the list is empty. We don't care about that
1744 * here, so using tail_pred directly is fine.
1745 */
1746 previous_tail_pred = instructions->tail_pred;
1747 previous_operand_loc = ast->get_location();
1748
Ian Romanick304ea902010-05-10 11:17:53 -07001749 result = ast->hir(instructions, state);
Ian Romanick3d5cfcf2011-04-11 10:10:30 -07001750 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001751
Ian Romanicka87ac252010-02-22 13:19:34 -08001752 /* Any errors should have already been emitted in the loop above.
1753 */
1754 error_emitted = true;
1755 break;
1756 }
1757 }
Kenneth Graunke08ba9772011-04-14 17:21:59 -07001758 type = NULL; /* use result->type, not type. */
1759 assert(result != NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08001760
Kenneth Graunke08ba9772011-04-14 17:21:59 -07001761 if (result->type->is_error() && !error_emitted)
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001762 _mesa_glsl_error(& loc, state, "type mismatch");
Ian Romanicka87ac252010-02-22 13:19:34 -08001763
1764 return result;
1765}
1766
1767
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001768ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001769ast_expression_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001770 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001771{
Ian Romanicka87ac252010-02-22 13:19:34 -08001772 /* It is possible to have expression statements that don't have an
1773 * expression. This is the solitary semicolon:
1774 *
1775 * for (i = 0; i < 5; i++)
1776 * ;
1777 *
1778 * In this case the expression will be NULL. Test for NULL and don't do
1779 * anything in that case.
1780 */
Ian Romanick18238de2010-03-01 13:49:10 -08001781 if (expression != NULL)
1782 expression->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001783
1784 /* Statements do not have r-values.
1785 */
1786 return NULL;
1787}
1788
1789
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001790ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001791ast_compound_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001792 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001793{
Ian Romanick18238de2010-03-01 13:49:10 -08001794 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001795 state->symbols->push_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001796
Ian Romanick2b97dc62010-05-10 17:42:05 -07001797 foreach_list_typed (ast_node, ast, link, &this->statements)
Ian Romanick304ea902010-05-10 11:17:53 -07001798 ast->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001799
Ian Romanick18238de2010-03-01 13:49:10 -08001800 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001801 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001802
1803 /* Compound statements do not have r-values.
1804 */
1805 return NULL;
1806}
1807
1808
Ian Romanick28009cd2010-03-30 16:59:27 -07001809static const glsl_type *
Kenneth Graunked8e34e22010-08-07 02:56:01 -07001810process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
Ian Romanick28009cd2010-03-30 16:59:27 -07001811 struct _mesa_glsl_parse_state *state)
1812{
1813 unsigned length = 0;
1814
Ian Romanicka04211e2011-10-24 11:45:50 -07001815 /* From page 19 (page 25) of the GLSL 1.20 spec:
1816 *
1817 * "Only one-dimensional arrays may be declared."
1818 */
1819 if (base->is_array()) {
1820 _mesa_glsl_error(loc, state,
1821 "invalid array of `%s' (only one-dimensional arrays "
1822 "may be declared)",
1823 base->name);
1824 return glsl_type::error_type;
1825 }
Ian Romanick28009cd2010-03-30 16:59:27 -07001826
1827 if (array_size != NULL) {
1828 exec_list dummy_instructions;
1829 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1830 YYLTYPE loc = array_size->get_location();
1831
Ian Romanick28009cd2010-03-30 16:59:27 -07001832 if (ir != NULL) {
1833 if (!ir->type->is_integer()) {
1834 _mesa_glsl_error(& loc, state, "array size must be integer type");
1835 } else if (!ir->type->is_scalar()) {
1836 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1837 } else {
1838 ir_constant *const size = ir->constant_expression_value();
1839
1840 if (size == NULL) {
1841 _mesa_glsl_error(& loc, state, "array size must be a "
1842 "constant valued expression");
1843 } else if (size->value.i[0] <= 0) {
1844 _mesa_glsl_error(& loc, state, "array size must be > 0");
1845 } else {
1846 assert(size->type == ir->type);
1847 length = size->value.u[0];
Paul Berryd4144a12011-08-01 15:23:07 -07001848
1849 /* If the array size is const (and we've verified that
1850 * it is) then no instructions should have been emitted
1851 * when we converted it to HIR. If they were emitted,
1852 * then either the array size isn't const after all, or
1853 * we are emitting unnecessary instructions.
1854 */
1855 assert(dummy_instructions.is_empty());
Ian Romanick28009cd2010-03-30 16:59:27 -07001856 }
1857 }
1858 }
Kenneth Graunked8e34e22010-08-07 02:56:01 -07001859 } else if (state->es_shader) {
1860 /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
1861 * array declarations have been removed from the language.
1862 */
1863 _mesa_glsl_error(loc, state, "unsized array declarations are not "
1864 "allowed in GLSL ES 1.00.");
Ian Romanick28009cd2010-03-30 16:59:27 -07001865 }
1866
Ian Romanickf38d15b2010-07-20 15:33:40 -07001867 return glsl_type::get_array_instance(base, length);
Ian Romanick28009cd2010-03-30 16:59:27 -07001868}
1869
1870
Ian Romanickd612a122010-03-31 16:22:06 -07001871const glsl_type *
1872ast_type_specifier::glsl_type(const char **name,
1873 struct _mesa_glsl_parse_state *state) const
Ian Romanicka87ac252010-02-22 13:19:34 -08001874{
Ian Romanickd612a122010-03-31 16:22:06 -07001875 const struct glsl_type *type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001876
Kenneth Graunkeca92ae22010-09-18 11:11:09 +02001877 type = state->symbols->get_type(this->type_name);
1878 *name = this->type_name;
Ian Romanicka87ac252010-02-22 13:19:34 -08001879
Kenneth Graunkeca92ae22010-09-18 11:11:09 +02001880 if (this->is_array) {
1881 YYLTYPE loc = this->get_location();
1882 type = process_array_type(&loc, type, this->array_size, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001883 }
1884
1885 return type;
1886}
1887
1888
1889static void
1890apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
Ian Romanick768b55a2010-08-13 16:46:43 -07001891 ir_variable *var,
Eric Anholt2e063f12010-03-28 00:56:22 -07001892 struct _mesa_glsl_parse_state *state,
1893 YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -08001894{
Ian Romanickbd330552011-01-07 18:34:58 -08001895 if (qual->flags.q.invariant) {
1896 if (var->used) {
1897 _mesa_glsl_error(loc, state,
1898 "variable `%s' may not be redeclared "
1899 "`invariant' after being used",
1900 var->name);
1901 } else {
1902 var->invariant = 1;
1903 }
1904 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001905
Ian Romanicke24d35a2010-10-05 16:38:47 -07001906 if (qual->flags.q.constant || qual->flags.q.attribute
1907 || qual->flags.q.uniform
1908 || (qual->flags.q.varying && (state->target == fragment_shader)))
Ian Romanicka87ac252010-02-22 13:19:34 -08001909 var->read_only = 1;
1910
Ian Romanicke24d35a2010-10-05 16:38:47 -07001911 if (qual->flags.q.centroid)
Ian Romanicka87ac252010-02-22 13:19:34 -08001912 var->centroid = 1;
1913
Ian Romanicke24d35a2010-10-05 16:38:47 -07001914 if (qual->flags.q.attribute && state->target != vertex_shader) {
Eric Anholt2e063f12010-03-28 00:56:22 -07001915 var->type = glsl_type::error_type;
1916 _mesa_glsl_error(loc, state,
1917 "`attribute' variables may not be declared in the "
Ian Romanickae4c4c02010-04-07 16:41:40 -07001918 "%s shader",
1919 _mesa_glsl_shader_target_name(state->target));
Eric Anholt2e063f12010-03-28 00:56:22 -07001920 }
1921
Eric Anholt90b78252010-03-31 21:21:20 -10001922 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1923 *
1924 * "The varying qualifier can be used only with the data types
1925 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1926 * these."
1927 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07001928 if (qual->flags.q.varying) {
Eric Anholt0ca17192010-05-19 13:38:15 -07001929 const glsl_type *non_array_type;
1930
1931 if (var->type && var->type->is_array())
1932 non_array_type = var->type->fields.array;
1933 else
1934 non_array_type = var->type;
1935
1936 if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
1937 var->type = glsl_type::error_type;
1938 _mesa_glsl_error(loc, state,
1939 "varying variables must be of base type float");
1940 }
Eric Anholt90b78252010-03-31 21:21:20 -10001941 }
1942
Ian Romanick7e2aa912010-07-19 17:12:42 -07001943 /* If there is no qualifier that changes the mode of the variable, leave
1944 * the setting alone.
1945 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07001946 if (qual->flags.q.in && qual->flags.q.out)
Ian Romanicka87ac252010-02-22 13:19:34 -08001947 var->mode = ir_var_inout;
Ian Romanicke24d35a2010-10-05 16:38:47 -07001948 else if (qual->flags.q.attribute || qual->flags.q.in
1949 || (qual->flags.q.varying && (state->target == fragment_shader)))
Ian Romanicka87ac252010-02-22 13:19:34 -08001950 var->mode = ir_var_in;
Ian Romanicke24d35a2010-10-05 16:38:47 -07001951 else if (qual->flags.q.out
1952 || (qual->flags.q.varying && (state->target == vertex_shader)))
Ian Romanicka87ac252010-02-22 13:19:34 -08001953 var->mode = ir_var_out;
Ian Romanicke24d35a2010-10-05 16:38:47 -07001954 else if (qual->flags.q.uniform)
Ian Romanicka87ac252010-02-22 13:19:34 -08001955 var->mode = ir_var_uniform;
Ian Romanicka87ac252010-02-22 13:19:34 -08001956
Ian Romanick86b43982011-01-06 10:49:56 -08001957 if (state->all_invariant && (state->current_function == NULL)) {
1958 switch (state->target) {
1959 case vertex_shader:
1960 if (var->mode == ir_var_out)
1961 var->invariant = true;
1962 break;
1963 case geometry_shader:
1964 if ((var->mode == ir_var_in) || (var->mode == ir_var_out))
1965 var->invariant = true;
1966 break;
1967 case fragment_shader:
1968 if (var->mode == ir_var_in)
1969 var->invariant = true;
1970 break;
1971 }
1972 }
1973
Ian Romanicke24d35a2010-10-05 16:38:47 -07001974 if (qual->flags.q.flat)
Paul Berrycf459492011-10-25 18:06:37 -07001975 var->interpolation = INTERP_QUALIFIER_FLAT;
Ian Romanicke24d35a2010-10-05 16:38:47 -07001976 else if (qual->flags.q.noperspective)
Paul Berrycf459492011-10-25 18:06:37 -07001977 var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
Paul Berryc4881502011-10-21 07:40:37 -07001978 else if (qual->flags.q.smooth)
Paul Berrycf459492011-10-25 18:06:37 -07001979 var->interpolation = INTERP_QUALIFIER_SMOOTH;
Paul Berryc4881502011-10-21 07:40:37 -07001980 else
1981 var->interpolation = INTERP_QUALIFIER_NONE;
Ian Romanick9d975372010-04-02 17:17:47 -07001982
Ian Romanicke24d35a2010-10-05 16:38:47 -07001983 var->pixel_center_integer = qual->flags.q.pixel_center_integer;
1984 var->origin_upper_left = qual->flags.q.origin_upper_left;
1985 if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
Ian Romanick8d8469e2010-06-30 17:48:09 -07001986 && (strcmp(var->name, "gl_FragCoord") != 0)) {
Ian Romanicke24d35a2010-10-05 16:38:47 -07001987 const char *const qual_string = (qual->flags.q.origin_upper_left)
Ian Romanick8d8469e2010-06-30 17:48:09 -07001988 ? "origin_upper_left" : "pixel_center_integer";
1989
1990 _mesa_glsl_error(loc, state,
1991 "layout qualifier `%s' can only be applied to "
1992 "fragment shader input `gl_FragCoord'",
1993 qual_string);
1994 }
1995
Ian Romanickeee68d32010-10-07 15:13:38 -07001996 if (qual->flags.q.explicit_location) {
1997 const bool global_scope = (state->current_function == NULL);
1998 bool fail = false;
1999 const char *string = "";
2000
2001 /* In the vertex shader only shader inputs can be given explicit
2002 * locations.
2003 *
2004 * In the fragment shader only shader outputs can be given explicit
2005 * locations.
2006 */
2007 switch (state->target) {
2008 case vertex_shader:
2009 if (!global_scope || (var->mode != ir_var_in)) {
2010 fail = true;
2011 string = "input";
2012 }
2013 break;
2014
2015 case geometry_shader:
2016 _mesa_glsl_error(loc, state,
2017 "geometry shader variables cannot be given "
2018 "explicit locations\n");
2019 break;
2020
2021 case fragment_shader:
Paul Berryb078aad2011-06-28 09:42:24 -07002022 if (!global_scope || (var->mode != ir_var_out)) {
Ian Romanickeee68d32010-10-07 15:13:38 -07002023 fail = true;
2024 string = "output";
2025 }
2026 break;
Kenneth Graunkea75da2c2010-10-20 14:59:40 -07002027 };
Ian Romanickeee68d32010-10-07 15:13:38 -07002028
2029 if (fail) {
2030 _mesa_glsl_error(loc, state,
2031 "only %s shader %s variables can be given an "
2032 "explicit location\n",
2033 _mesa_glsl_shader_target_name(state->target),
2034 string);
2035 } else {
2036 var->explicit_location = true;
Ian Romanick68a4fc92010-10-07 17:21:22 -07002037
2038 /* This bit of silliness is needed because invalid explicit locations
2039 * are supposed to be flagged during linking. Small negative values
2040 * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
2041 * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
2042 * The linker needs to be able to differentiate these cases. This
2043 * ensures that negative values stay negative.
2044 */
2045 if (qual->location >= 0) {
2046 var->location = (state->target == vertex_shader)
2047 ? (qual->location + VERT_ATTRIB_GENERIC0)
2048 : (qual->location + FRAG_RESULT_DATA0);
2049 } else {
2050 var->location = qual->location;
2051 }
Ian Romanickeee68d32010-10-07 15:13:38 -07002052 }
2053 }
2054
Ian Romanick4bcff0c2011-01-07 16:53:59 -08002055 /* Does the declaration use the 'layout' keyword?
2056 */
2057 const bool uses_layout = qual->flags.q.pixel_center_integer
2058 || qual->flags.q.origin_upper_left
2059 || qual->flags.q.explicit_location;
2060
2061 /* Does the declaration use the deprecated 'attribute' or 'varying'
2062 * keywords?
2063 */
2064 const bool uses_deprecated_qualifier = qual->flags.q.attribute
2065 || qual->flags.q.varying;
2066
2067 /* Is the 'layout' keyword used with parameters that allow relaxed checking.
2068 * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
2069 * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
2070 * allowed the layout qualifier to be used with 'varying' and 'attribute'.
2071 * These extensions and all following extensions that add the 'layout'
2072 * keyword have been modified to require the use of 'in' or 'out'.
2073 *
2074 * The following extension do not allow the deprecated keywords:
2075 *
2076 * GL_AMD_conservative_depth
2077 * GL_ARB_gpu_shader5
2078 * GL_ARB_separate_shader_objects
2079 * GL_ARB_tesselation_shader
2080 * GL_ARB_transform_feedback3
2081 * GL_ARB_uniform_buffer_object
2082 *
2083 * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
2084 * allow layout with the deprecated keywords.
2085 */
2086 const bool relaxed_layout_qualifier_checking =
2087 state->ARB_fragment_coord_conventions_enable;
2088
2089 if (uses_layout && uses_deprecated_qualifier) {
2090 if (relaxed_layout_qualifier_checking) {
2091 _mesa_glsl_warning(loc, state,
2092 "`layout' qualifier may not be used with "
2093 "`attribute' or `varying'");
2094 } else {
2095 _mesa_glsl_error(loc, state,
2096 "`layout' qualifier may not be used with "
2097 "`attribute' or `varying'");
2098 }
2099 }
2100
Chad Versacebc04d242011-01-27 01:40:26 -08002101 /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2102 * AMD_conservative_depth.
2103 */
2104 int depth_layout_count = qual->flags.q.depth_any
2105 + qual->flags.q.depth_greater
2106 + qual->flags.q.depth_less
2107 + qual->flags.q.depth_unchanged;
2108 if (depth_layout_count > 0
2109 && !state->AMD_conservative_depth_enable) {
2110 _mesa_glsl_error(loc, state,
2111 "extension GL_AMD_conservative_depth must be enabled "
2112 "to use depth layout qualifiers");
2113 } else if (depth_layout_count > 0
2114 && strcmp(var->name, "gl_FragDepth") != 0) {
2115 _mesa_glsl_error(loc, state,
2116 "depth layout qualifiers can be applied only to "
2117 "gl_FragDepth");
2118 } else if (depth_layout_count > 1
2119 && strcmp(var->name, "gl_FragDepth") == 0) {
2120 _mesa_glsl_error(loc, state,
2121 "at most one depth layout qualifier can be applied to "
2122 "gl_FragDepth");
2123 }
2124 if (qual->flags.q.depth_any)
2125 var->depth_layout = ir_depth_layout_any;
2126 else if (qual->flags.q.depth_greater)
2127 var->depth_layout = ir_depth_layout_greater;
2128 else if (qual->flags.q.depth_less)
2129 var->depth_layout = ir_depth_layout_less;
2130 else if (qual->flags.q.depth_unchanged)
2131 var->depth_layout = ir_depth_layout_unchanged;
2132 else
2133 var->depth_layout = ir_depth_layout_none;
Ian Romanicka87ac252010-02-22 13:19:34 -08002134}
2135
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002136/**
2137 * Get the variable that is being redeclared by this declaration
2138 *
2139 * Semantic checks to verify the validity of the redeclaration are also
2140 * performed. If semantic checks fail, compilation error will be emitted via
2141 * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
2142 *
2143 * \returns
2144 * A pointer to an existing variable in the current scope if the declaration
2145 * is a redeclaration, \c NULL otherwise.
2146 */
2147ir_variable *
2148get_variable_being_redeclared(ir_variable *var, ast_declaration *decl,
2149 struct _mesa_glsl_parse_state *state)
2150{
2151 /* Check if this declaration is actually a re-declaration, either to
2152 * resize an array or add qualifiers to an existing variable.
2153 *
2154 * This is allowed for variables in the current scope, or when at
2155 * global scope (for built-ins in the implicit outer scope).
2156 */
2157 ir_variable *earlier = state->symbols->get_variable(decl->identifier);
2158 if (earlier == NULL ||
2159 (state->current_function != NULL &&
2160 !state->symbols->name_declared_this_scope(decl->identifier))) {
2161 return NULL;
2162 }
2163
2164
2165 YYLTYPE loc = decl->get_location();
2166
2167 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
2168 *
2169 * "It is legal to declare an array without a size and then
2170 * later re-declare the same name as an array of the same
2171 * type and specify a size."
2172 */
2173 if ((earlier->type->array_size() == 0)
2174 && var->type->is_array()
2175 && (var->type->element_type() == earlier->type->element_type())) {
2176 /* FINISHME: This doesn't match the qualifiers on the two
2177 * FINISHME: declarations. It's not 100% clear whether this is
2178 * FINISHME: required or not.
2179 */
2180
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002181 const unsigned size = unsigned(var->type->array_size());
Paul Berry93b97582011-09-06 10:01:51 -07002182 check_builtin_array_max_size(var->name, size, loc, state);
2183 if ((size > 0) && (size <= earlier->max_array_access)) {
Ian Romanick8e6cb9f2011-03-04 15:28:40 -08002184 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
2185 "previous access",
2186 earlier->max_array_access);
2187 }
2188
2189 earlier->type = var->type;
2190 delete var;
2191 var = NULL;
2192 } else if (state->ARB_fragment_coord_conventions_enable
2193 && strcmp(var->name, "gl_FragCoord") == 0
2194 && earlier->type == var->type
2195 && earlier->mode == var->mode) {
2196 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
2197 * qualifiers.
2198 */
2199 earlier->origin_upper_left = var->origin_upper_left;
2200 earlier->pixel_center_integer = var->pixel_center_integer;
2201
2202 /* According to section 4.3.7 of the GLSL 1.30 spec,
2203 * the following built-in varaibles can be redeclared with an
2204 * interpolation qualifier:
2205 * * gl_FrontColor
2206 * * gl_BackColor
2207 * * gl_FrontSecondaryColor
2208 * * gl_BackSecondaryColor
2209 * * gl_Color
2210 * * gl_SecondaryColor
2211 */
2212 } else if (state->language_version >= 130
2213 && (strcmp(var->name, "gl_FrontColor") == 0
2214 || strcmp(var->name, "gl_BackColor") == 0
2215 || strcmp(var->name, "gl_FrontSecondaryColor") == 0
2216 || strcmp(var->name, "gl_BackSecondaryColor") == 0
2217 || strcmp(var->name, "gl_Color") == 0
2218 || strcmp(var->name, "gl_SecondaryColor") == 0)
2219 && earlier->type == var->type
2220 && earlier->mode == var->mode) {
2221 earlier->interpolation = var->interpolation;
2222
2223 /* Layout qualifiers for gl_FragDepth. */
2224 } else if (state->AMD_conservative_depth_enable
2225 && strcmp(var->name, "gl_FragDepth") == 0
2226 && earlier->type == var->type
2227 && earlier->mode == var->mode) {
2228
2229 /** From the AMD_conservative_depth spec:
2230 * Within any shader, the first redeclarations of gl_FragDepth
2231 * must appear before any use of gl_FragDepth.
2232 */
2233 if (earlier->used) {
2234 _mesa_glsl_error(&loc, state,
2235 "the first redeclaration of gl_FragDepth "
2236 "must appear before any use of gl_FragDepth");
2237 }
2238
2239 /* Prevent inconsistent redeclaration of depth layout qualifier. */
2240 if (earlier->depth_layout != ir_depth_layout_none
2241 && earlier->depth_layout != var->depth_layout) {
2242 _mesa_glsl_error(&loc, state,
2243 "gl_FragDepth: depth layout is declared here "
2244 "as '%s, but it was previously declared as "
2245 "'%s'",
2246 depth_layout_string(var->depth_layout),
2247 depth_layout_string(earlier->depth_layout));
2248 }
2249
2250 earlier->depth_layout = var->depth_layout;
2251
2252 } else {
2253 _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
2254 }
2255
2256 return earlier;
2257}
Ian Romanicka87ac252010-02-22 13:19:34 -08002258
Ian Romanick0292ffb2011-03-04 15:29:33 -08002259/**
2260 * Generate the IR for an initializer in a variable declaration
2261 */
2262ir_rvalue *
2263process_initializer(ir_variable *var, ast_declaration *decl,
2264 ast_fully_specified_type *type,
2265 exec_list *initializer_instructions,
2266 struct _mesa_glsl_parse_state *state)
2267{
2268 ir_rvalue *result = NULL;
2269
2270 YYLTYPE initializer_loc = decl->initializer->get_location();
2271
2272 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
2273 *
2274 * "All uniform variables are read-only and are initialized either
2275 * directly by an application via API commands, or indirectly by
2276 * OpenGL."
2277 */
2278 if ((state->language_version <= 110)
2279 && (var->mode == ir_var_uniform)) {
2280 _mesa_glsl_error(& initializer_loc, state,
2281 "cannot initialize uniforms in GLSL 1.10");
2282 }
2283
2284 if (var->type->is_sampler()) {
2285 _mesa_glsl_error(& initializer_loc, state,
2286 "cannot initialize samplers");
2287 }
2288
2289 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
2290 _mesa_glsl_error(& initializer_loc, state,
2291 "cannot initialize %s shader input / %s",
2292 _mesa_glsl_shader_target_name(state->target),
2293 (state->target == vertex_shader)
2294 ? "attribute" : "varying");
2295 }
2296
2297 ir_dereference *const lhs = new(state) ir_dereference_variable(var);
2298 ir_rvalue *rhs = decl->initializer->hir(initializer_instructions,
2299 state);
2300
2301 /* Calculate the constant value if this is a const or uniform
2302 * declaration.
2303 */
2304 if (type->qualifier.flags.q.constant
2305 || type->qualifier.flags.q.uniform) {
Ian Romanick85caea22011-03-15 16:33:27 -07002306 ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs, true);
Ian Romanick0292ffb2011-03-04 15:29:33 -08002307 if (new_rhs != NULL) {
2308 rhs = new_rhs;
2309
2310 ir_constant *constant_value = rhs->constant_expression_value();
2311 if (!constant_value) {
2312 _mesa_glsl_error(& initializer_loc, state,
2313 "initializer of %s variable `%s' must be a "
2314 "constant expression",
2315 (type->qualifier.flags.q.constant)
2316 ? "const" : "uniform",
2317 decl->identifier);
2318 if (var->type->is_numeric()) {
2319 /* Reduce cascading errors. */
2320 var->constant_value = ir_constant::zero(state, var->type);
2321 }
2322 } else {
2323 rhs = constant_value;
2324 var->constant_value = constant_value;
2325 }
2326 } else {
2327 _mesa_glsl_error(&initializer_loc, state,
2328 "initializer of type %s cannot be assigned to "
2329 "variable of type %s",
2330 rhs->type->name, var->type->name);
2331 if (var->type->is_numeric()) {
2332 /* Reduce cascading errors. */
2333 var->constant_value = ir_constant::zero(state, var->type);
2334 }
2335 }
2336 }
2337
2338 if (rhs && !rhs->type->is_error()) {
2339 bool temp = var->read_only;
2340 if (type->qualifier.flags.q.constant)
2341 var->read_only = false;
2342
2343 /* Never emit code to initialize a uniform.
2344 */
2345 const glsl_type *initializer_type;
2346 if (!type->qualifier.flags.q.uniform) {
2347 result = do_assignment(initializer_instructions, state,
Ian Romanick85caea22011-03-15 16:33:27 -07002348 lhs, rhs, true,
Ian Romanick0292ffb2011-03-04 15:29:33 -08002349 type->get_location());
2350 initializer_type = result->type;
2351 } else
2352 initializer_type = rhs->type;
2353
2354 /* If the declared variable is an unsized array, it must inherrit
2355 * its full type from the initializer. A declaration such as
2356 *
2357 * uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
2358 *
2359 * becomes
2360 *
2361 * uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
2362 *
2363 * The assignment generated in the if-statement (below) will also
2364 * automatically handle this case for non-uniforms.
2365 *
2366 * If the declared variable is not an array, the types must
2367 * already match exactly. As a result, the type assignment
2368 * here can be done unconditionally. For non-uniforms the call
2369 * to do_assignment can change the type of the initializer (via
2370 * the implicit conversion rules). For uniforms the initializer
2371 * must be a constant expression, and the type of that expression
2372 * was validated above.
2373 */
2374 var->type = initializer_type;
2375
2376 var->read_only = temp;
2377 }
2378
2379 return result;
2380}
2381
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07002382ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08002383ast_declarator_list::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08002384 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08002385{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002386 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -08002387 const struct glsl_type *decl_type;
2388 const char *type_name = NULL;
Eric Anholt85584592010-04-14 15:38:52 -07002389 ir_rvalue *result = NULL;
Ian Romanickc824e352010-04-23 15:55:19 -07002390 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08002391
Ian Romanick6f0823d2010-07-01 20:39:08 -07002392 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
2393 *
2394 * "To ensure that a particular output variable is invariant, it is
2395 * necessary to use the invariant qualifier. It can either be used to
2396 * qualify a previously declared variable as being invariant
2397 *
2398 * invariant gl_Position; // make existing gl_Position be invariant"
2399 *
2400 * In these cases the parser will set the 'invariant' flag in the declarator
2401 * list, and the type will be NULL.
2402 */
2403 if (this->invariant) {
2404 assert(this->type == NULL);
2405
2406 if (state->current_function != NULL) {
2407 _mesa_glsl_error(& loc, state,
2408 "All uses of `invariant' keyword must be at global "
2409 "scope\n");
2410 }
2411
2412 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
2413 assert(!decl->is_array);
2414 assert(decl->array_size == NULL);
2415 assert(decl->initializer == NULL);
2416
2417 ir_variable *const earlier =
2418 state->symbols->get_variable(decl->identifier);
2419 if (earlier == NULL) {
2420 _mesa_glsl_error(& loc, state,
2421 "Undeclared variable `%s' cannot be marked "
2422 "invariant\n", decl->identifier);
2423 } else if ((state->target == vertex_shader)
2424 && (earlier->mode != ir_var_out)) {
2425 _mesa_glsl_error(& loc, state,
2426 "`%s' cannot be marked invariant, vertex shader "
2427 "outputs only\n", decl->identifier);
2428 } else if ((state->target == fragment_shader)
2429 && (earlier->mode != ir_var_in)) {
2430 _mesa_glsl_error(& loc, state,
2431 "`%s' cannot be marked invariant, fragment shader "
2432 "inputs only\n", decl->identifier);
Ian Romanickbd330552011-01-07 18:34:58 -08002433 } else if (earlier->used) {
2434 _mesa_glsl_error(& loc, state,
2435 "variable `%s' may not be redeclared "
2436 "`invariant' after being used",
2437 earlier->name);
Ian Romanick6f0823d2010-07-01 20:39:08 -07002438 } else {
2439 earlier->invariant = true;
2440 }
2441 }
2442
2443 /* Invariant redeclarations do not have r-values.
2444 */
2445 return NULL;
2446 }
2447
2448 assert(this->type != NULL);
2449 assert(!this->invariant);
2450
Ian Romanick3455ce62010-04-19 15:13:15 -07002451 /* The type specifier may contain a structure definition. Process that
2452 * before any of the variable declarations.
2453 */
2454 (void) this->type->specifier->hir(instructions, state);
2455
Ian Romanickd612a122010-03-31 16:22:06 -07002456 decl_type = this->type->specifier->glsl_type(& type_name, state);
Ian Romanick304ea902010-05-10 11:17:53 -07002457 if (this->declarations.is_empty()) {
Ian Romanickf5ba4d02011-10-25 17:49:07 -07002458 /* If there is no structure involved in the program text, there are two
2459 * possible scenarios:
2460 *
2461 * - The program text contained something like 'vec4;'. This is an
2462 * empty declaration. It is valid but weird. Emit a warning.
2463 *
2464 * - The program text contained something like 'S;' and 'S' is not the
2465 * name of a known structure type. This is both invalid and weird.
2466 * Emit an error.
2467 *
2468 * Note that if decl_type is NULL and there is a structure involved,
2469 * there must have been some sort of error with the structure. In this
2470 * case we assume that an error was already generated on this line of
2471 * code for the structure. There is no need to generate an additional,
2472 * confusing error.
2473 */
2474 assert(this->type->specifier->structure == NULL || decl_type != NULL
2475 || state->error);
2476 if (this->type->specifier->structure == NULL) {
2477 if (decl_type != NULL) {
Chia-I Wu547212d2011-08-04 00:39:07 +09002478 _mesa_glsl_warning(&loc, state, "empty declaration");
Ian Romanickf5ba4d02011-10-25 17:49:07 -07002479 } else {
2480 _mesa_glsl_error(&loc, state,
2481 "invalid type `%s' in empty declaration",
2482 type_name);
Chia-I Wu547212d2011-08-04 00:39:07 +09002483 }
Ian Romanickc824e352010-04-23 15:55:19 -07002484 }
2485 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002486
Ian Romanick2b97dc62010-05-10 17:42:05 -07002487 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
Ian Romanicka87ac252010-02-22 13:19:34 -08002488 const struct glsl_type *var_type;
Ian Romanick768b55a2010-08-13 16:46:43 -07002489 ir_variable *var;
Ian Romanicka87ac252010-02-22 13:19:34 -08002490
2491 /* FINISHME: Emit a warning if a variable declaration shadows a
2492 * FINISHME: declaration at a higher scope.
2493 */
2494
Ian Romanickcec65a62010-03-23 12:28:44 -07002495 if ((decl_type == NULL) || decl_type->is_void()) {
Ian Romanicka87ac252010-02-22 13:19:34 -08002496 if (type_name != NULL) {
2497 _mesa_glsl_error(& loc, state,
2498 "invalid type `%s' in declaration of `%s'",
2499 type_name, decl->identifier);
2500 } else {
2501 _mesa_glsl_error(& loc, state,
2502 "invalid type in declaration of `%s'",
2503 decl->identifier);
2504 }
2505 continue;
2506 }
2507
2508 if (decl->is_array) {
Kenneth Graunked8e34e22010-08-07 02:56:01 -07002509 var_type = process_array_type(&loc, decl_type, decl->array_size,
2510 state);
Ian Romanicka04211e2011-10-24 11:45:50 -07002511 if (var_type->is_error())
2512 continue;
Ian Romanicka87ac252010-02-22 13:19:34 -08002513 } else {
2514 var_type = decl_type;
2515 }
2516
Ian Romanick7e2aa912010-07-19 17:12:42 -07002517 var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
Ian Romanicka87ac252010-02-22 13:19:34 -08002518
Eric Anholt3f151502010-04-02 01:53:57 -10002519 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
2520 *
2521 * "Global variables can only use the qualifiers const,
2522 * attribute, uni form, or varying. Only one may be
2523 * specified.
2524 *
2525 * Local variables can only use the qualifier const."
2526 *
Ian Romanick82c4b4f2011-01-07 16:53:07 -08002527 * This is relaxed in GLSL 1.30. It is also relaxed by any extension
2528 * that adds the 'layout' keyword.
Eric Anholt3f151502010-04-02 01:53:57 -10002529 */
Ian Romanick82c4b4f2011-01-07 16:53:07 -08002530 if ((state->language_version < 130)
2531 && !state->ARB_explicit_attrib_location_enable
2532 && !state->ARB_fragment_coord_conventions_enable) {
Ian Romanicke24d35a2010-10-05 16:38:47 -07002533 if (this->type->qualifier.flags.q.out) {
Eric Anholt3f151502010-04-02 01:53:57 -10002534 _mesa_glsl_error(& loc, state,
2535 "`out' qualifier in declaration of `%s' "
Ian Romanick469ea692011-01-07 16:05:59 -08002536 "only valid for function parameters in %s.",
2537 decl->identifier, state->version_string);
Eric Anholt3f151502010-04-02 01:53:57 -10002538 }
Ian Romanicke24d35a2010-10-05 16:38:47 -07002539 if (this->type->qualifier.flags.q.in) {
Eric Anholt3f151502010-04-02 01:53:57 -10002540 _mesa_glsl_error(& loc, state,
2541 "`in' qualifier in declaration of `%s' "
Ian Romanick469ea692011-01-07 16:05:59 -08002542 "only valid for function parameters in %s.",
2543 decl->identifier, state->version_string);
Eric Anholt3f151502010-04-02 01:53:57 -10002544 }
2545 /* FINISHME: Test for other invalid qualifiers. */
2546 }
2547
Eric Anholt2e063f12010-03-28 00:56:22 -07002548 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
2549 & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08002550
Ian Romanicke24d35a2010-10-05 16:38:47 -07002551 if (this->type->qualifier.flags.q.invariant) {
Eric Anholt046bef22010-08-04 20:33:57 -07002552 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
2553 var->mode == ir_var_inout)) {
2554 /* FINISHME: Note that this doesn't work for invariant on
2555 * a function signature outval
2556 */
Ian Romanick6f0823d2010-07-01 20:39:08 -07002557 _mesa_glsl_error(& loc, state,
2558 "`%s' cannot be marked invariant, vertex shader "
2559 "outputs only\n", var->name);
Eric Anholt046bef22010-08-04 20:33:57 -07002560 } else if ((state->target == fragment_shader) &&
2561 !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
2562 /* FINISHME: Note that this doesn't work for invariant on
2563 * a function signature inval
2564 */
Ian Romanick6f0823d2010-07-01 20:39:08 -07002565 _mesa_glsl_error(& loc, state,
2566 "`%s' cannot be marked invariant, fragment shader "
2567 "inputs only\n", var->name);
2568 }
2569 }
2570
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002571 if (state->current_function != NULL) {
Ian Romanickb168e532010-03-31 12:31:18 -07002572 const char *mode = NULL;
Ian Romanicke0800062010-03-31 13:15:23 -07002573 const char *extra = "";
Ian Romanickb168e532010-03-31 12:31:18 -07002574
Ian Romanicke0800062010-03-31 13:15:23 -07002575 /* There is no need to check for 'inout' here because the parser will
2576 * only allow that in function parameter lists.
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002577 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07002578 if (this->type->qualifier.flags.q.attribute) {
Ian Romanickb168e532010-03-31 12:31:18 -07002579 mode = "attribute";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002580 } else if (this->type->qualifier.flags.q.uniform) {
Ian Romanickb168e532010-03-31 12:31:18 -07002581 mode = "uniform";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002582 } else if (this->type->qualifier.flags.q.varying) {
Ian Romanickb168e532010-03-31 12:31:18 -07002583 mode = "varying";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002584 } else if (this->type->qualifier.flags.q.in) {
Ian Romanicke0800062010-03-31 13:15:23 -07002585 mode = "in";
2586 extra = " or in function parameter list";
Ian Romanicke24d35a2010-10-05 16:38:47 -07002587 } else if (this->type->qualifier.flags.q.out) {
Ian Romanicke0800062010-03-31 13:15:23 -07002588 mode = "out";
2589 extra = " or in function parameter list";
Ian Romanickb168e532010-03-31 12:31:18 -07002590 }
2591
2592 if (mode) {
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002593 _mesa_glsl_error(& loc, state,
Ian Romanickb168e532010-03-31 12:31:18 -07002594 "%s variable `%s' must be declared at "
Ian Romanicke0800062010-03-31 13:15:23 -07002595 "global scope%s",
2596 mode, var->name, extra);
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07002597 }
2598 } else if (var->mode == ir_var_in) {
Chad Versace01a584d2011-01-20 14:12:16 -08002599 var->read_only = true;
2600
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002601 if (state->target == vertex_shader) {
2602 bool error_emitted = false;
2603
2604 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2605 *
2606 * "Vertex shader inputs can only be float, floating-point
2607 * vectors, matrices, signed and unsigned integers and integer
2608 * vectors. Vertex shader inputs can also form arrays of these
2609 * types, but not structures."
2610 *
Ian Romanick2d816202010-03-29 17:40:11 -07002611 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
2612 *
2613 * "Vertex shader inputs can only be float, floating-point
2614 * vectors, matrices, signed and unsigned integers and integer
2615 * vectors. They cannot be arrays or structures."
2616 *
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002617 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
2618 *
2619 * "The attribute qualifier can be used only with float,
2620 * floating-point vectors, and matrices. Attribute variables
2621 * cannot be declared as arrays or structures."
2622 */
2623 const glsl_type *check_type = var->type->is_array()
2624 ? var->type->fields.array : var->type;
2625
2626 switch (check_type->base_type) {
2627 case GLSL_TYPE_FLOAT:
2628 break;
2629 case GLSL_TYPE_UINT:
2630 case GLSL_TYPE_INT:
2631 if (state->language_version > 120)
2632 break;
2633 /* FALLTHROUGH */
2634 default:
2635 _mesa_glsl_error(& loc, state,
2636 "vertex shader input / attribute cannot have "
2637 "type %s`%s'",
2638 var->type->is_array() ? "array of " : "",
2639 check_type->name);
2640 error_emitted = true;
2641 }
2642
Ian Romanick2d816202010-03-29 17:40:11 -07002643 if (!error_emitted && (state->language_version <= 130)
Ian Romanickfb9f5b02010-03-29 17:16:35 -07002644 && var->type->is_array()) {
2645 _mesa_glsl_error(& loc, state,
2646 "vertex shader input / attribute cannot have "
2647 "array type");
2648 error_emitted = true;
2649 }
2650 }
2651 }
2652
Chad Versace68d06b12010-12-16 11:06:19 -08002653 /* Integer vertex outputs must be qualified with 'flat'.
2654 *
2655 * From section 4.3.6 of the GLSL 1.30 spec:
2656 * "If a vertex output is a signed or unsigned integer or integer
2657 * vector, then it must be qualified with the interpolation qualifier
2658 * flat."
2659 */
2660 if (state->language_version >= 130
2661 && state->target == vertex_shader
2662 && state->current_function == NULL
2663 && var->type->is_integer()
2664 && var->mode == ir_var_out
Paul Berrycf459492011-10-25 18:06:37 -07002665 && var->interpolation != INTERP_QUALIFIER_FLAT) {
Chad Versace68d06b12010-12-16 11:06:19 -08002666
2667 _mesa_glsl_error(&loc, state, "If a vertex output is an integer, "
2668 "then it must be qualified with 'flat'");
2669 }
2670
2671
Chad Versace605aacc2011-01-11 17:21:18 -08002672 /* Interpolation qualifiers cannot be applied to 'centroid' and
2673 * 'centroid varying'.
2674 *
2675 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
2676 * "interpolation qualifiers may only precede the qualifiers in,
2677 * centroid in, out, or centroid out in a declaration. They do not apply
2678 * to the deprecated storage qualifiers varying or centroid varying."
2679 */
2680 if (state->language_version >= 130
2681 && this->type->qualifier.has_interpolation()
2682 && this->type->qualifier.flags.q.varying) {
2683
2684 const char *i = this->type->qualifier.interpolation_string();
2685 assert(i != NULL);
2686 const char *s;
2687 if (this->type->qualifier.flags.q.centroid)
2688 s = "centroid varying";
2689 else
2690 s = "varying";
2691
2692 _mesa_glsl_error(&loc, state,
2693 "qualifier '%s' cannot be applied to the "
2694 "deprecated storage qualifier '%s'", i, s);
2695 }
2696
2697
Chad Versace8faaa4a2011-01-11 18:13:26 -08002698 /* Interpolation qualifiers can only apply to vertex shader outputs and
2699 * fragment shader inputs.
2700 *
2701 * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
2702 * "Outputs from a vertex shader (out) and inputs to a fragment
2703 * shader (in) can be further qualified with one or more of these
2704 * interpolation qualifiers"
2705 */
2706 if (state->language_version >= 130
2707 && this->type->qualifier.has_interpolation()) {
2708
2709 const char *i = this->type->qualifier.interpolation_string();
2710 assert(i != NULL);
2711
2712 switch (state->target) {
2713 case vertex_shader:
2714 if (this->type->qualifier.flags.q.in) {
2715 _mesa_glsl_error(&loc, state,
2716 "qualifier '%s' cannot be applied to vertex "
2717 "shader inputs", i);
2718 }
2719 break;
2720 case fragment_shader:
2721 if (this->type->qualifier.flags.q.out) {
2722 _mesa_glsl_error(&loc, state,
2723 "qualifier '%s' cannot be applied to fragment "
2724 "shader outputs", i);
2725 }
2726 break;
2727 default:
2728 assert(0);
2729 }
2730 }
2731
2732
Chad Versace1eb0f172011-01-11 18:24:17 -08002733 /* From section 4.3.4 of the GLSL 1.30 spec:
2734 * "It is an error to use centroid in in a vertex shader."
2735 */
2736 if (state->language_version >= 130
2737 && this->type->qualifier.flags.q.centroid
2738 && this->type->qualifier.flags.q.in
2739 && state->target == vertex_shader) {
2740
2741 _mesa_glsl_error(&loc, state,
2742 "'centroid in' cannot be used in a vertex shader");
2743 }
2744
2745
Chad Versace889e1a52011-01-16 22:38:45 -08002746 /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
2747 */
2748 if (this->type->specifier->precision != ast_precision_none
2749 && state->language_version != 100
2750 && state->language_version < 130) {
2751
2752 _mesa_glsl_error(&loc, state,
2753 "precision qualifiers are supported only in GLSL ES "
2754 "1.00, and GLSL 1.30 and later");
2755 }
2756
2757
Chad Versace45e8e6c2011-01-17 15:28:39 -08002758 /* Precision qualifiers only apply to floating point and integer types.
Chad Versace889e1a52011-01-16 22:38:45 -08002759 *
2760 * From section 4.5.2 of the GLSL 1.30 spec:
2761 * "Any floating point or any integer declaration can have the type
2762 * preceded by one of these precision qualifiers [...] Literal
2763 * constants do not have precision qualifiers. Neither do Boolean
2764 * variables.
Kenneth Graunke87528242011-03-26 23:37:09 -07002765 *
2766 * In GLSL ES, sampler types are also allowed.
2767 *
2768 * From page 87 of the GLSL ES spec:
2769 * "RESOLUTION: Allow sampler types to take a precision qualifier."
Chad Versace889e1a52011-01-16 22:38:45 -08002770 */
2771 if (this->type->specifier->precision != ast_precision_none
Chad Versace45e8e6c2011-01-17 15:28:39 -08002772 && !var->type->is_float()
2773 && !var->type->is_integer()
Kenneth Graunke87528242011-03-26 23:37:09 -07002774 && !(var->type->is_sampler() && state->es_shader)
Chad Versace45e8e6c2011-01-17 15:28:39 -08002775 && !(var->type->is_array()
2776 && (var->type->fields.array->is_float()
2777 || var->type->fields.array->is_integer()))) {
Chad Versace889e1a52011-01-16 22:38:45 -08002778
2779 _mesa_glsl_error(&loc, state,
Kenneth Graunke87528242011-03-26 23:37:09 -07002780 "precision qualifiers apply only to floating point"
2781 "%s types", state->es_shader ? ", integer, and sampler"
2782 : "and integer");
Chad Versace889e1a52011-01-16 22:38:45 -08002783 }
2784
Paul Berryf0722102011-07-12 12:03:02 -07002785 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
2786 *
2787 * "[Sampler types] can only be declared as function
2788 * parameters or uniform variables (see Section 4.3.5
2789 * "Uniform")".
2790 */
2791 if (var_type->contains_sampler() &&
2792 !this->type->qualifier.flags.q.uniform) {
2793 _mesa_glsl_error(&loc, state, "samplers must be declared uniform");
2794 }
2795
Ian Romanicke78e0fa2010-07-07 12:13:34 -07002796 /* Process the initializer and add its instructions to a temporary
2797 * list. This list will be added to the instruction stream (below) after
2798 * the declaration is added. This is done because in some cases (such as
2799 * redeclarations) the declaration may not actually be added to the
2800 * instruction stream.
2801 */
Eric Anholtfa33d0b2010-07-29 13:50:17 -07002802 exec_list initializer_instructions;
Ian Romanick09a4ba02011-03-04 16:15:20 -08002803 ir_variable *earlier = get_variable_being_redeclared(var, decl, state);
2804
Ian Romanick66faec42010-03-27 18:56:53 -07002805 if (decl->initializer != NULL) {
Ian Romanick09a4ba02011-03-04 16:15:20 -08002806 result = process_initializer((earlier == NULL) ? var : earlier,
2807 decl, this->type,
Ian Romanick0292ffb2011-03-04 15:29:33 -08002808 &initializer_instructions, state);
Ian Romanick19360152010-03-26 18:05:27 -07002809 }
Ian Romanick17d86f42010-03-29 12:59:02 -07002810
Eric Anholt0ed61252010-03-31 09:29:33 -10002811 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
2812 *
2813 * "It is an error to write to a const variable outside of
2814 * its declaration, so they must be initialized when
2815 * declared."
2816 */
Ian Romanicke24d35a2010-10-05 16:38:47 -07002817 if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
Eric Anholt0ed61252010-03-31 09:29:33 -10002818 _mesa_glsl_error(& loc, state,
Chad Versace46f71052011-01-18 15:15:19 -08002819 "const declaration of `%s' must be initialized",
2820 decl->identifier);
Eric Anholt0ed61252010-03-31 09:29:33 -10002821 }
2822
Ian Romanick09a4ba02011-03-04 16:15:20 -08002823 /* If the declaration is not a redeclaration, there are a few additional
2824 * semantic checks that must be applied. In addition, variable that was
2825 * created for the declaration should be added to the IR stream.
2826 */
2827 if (earlier == NULL) {
2828 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2829 *
2830 * "Identifiers starting with "gl_" are reserved for use by
2831 * OpenGL, and may not be declared in a shader as either a
2832 * variable or a function."
2833 */
2834 if (strncmp(decl->identifier, "gl_", 3) == 0)
2835 _mesa_glsl_error(& loc, state,
2836 "identifier `%s' uses reserved `gl_' prefix",
2837 decl->identifier);
Jason Woodc475a542011-10-06 22:37:48 -07002838 else if (strstr(decl->identifier, "__")) {
Eric Anholt684b7012011-10-03 16:27:59 -07002839 /* From page 14 (page 20 of the PDF) of the GLSL 1.10
2840 * spec:
2841 *
2842 * "In addition, all identifiers containing two
2843 * consecutive underscores (__) are reserved as
2844 * possible future keywords."
2845 */
2846 _mesa_glsl_error(& loc, state,
2847 "identifier `%s' uses reserved `__' string",
2848 decl->identifier);
2849 }
Ian Romanick09a4ba02011-03-04 16:15:20 -08002850
2851 /* Add the variable to the symbol table. Note that the initializer's
2852 * IR was already processed earlier (though it hasn't been emitted
2853 * yet), without the variable in scope.
2854 *
2855 * This differs from most C-like languages, but it follows the GLSL
2856 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
2857 * spec:
2858 *
2859 * "Within a declaration, the scope of a name starts immediately
2860 * after the initializer if present or immediately after the name
2861 * being declared if not."
2862 */
2863 if (!state->symbols->add_variable(var)) {
2864 YYLTYPE loc = this->get_location();
2865 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
2866 "current scope", decl->identifier);
2867 continue;
2868 }
2869
2870 /* Push the variable declaration to the top. It means that all the
2871 * variable declarations will appear in a funny last-to-first order,
2872 * but otherwise we run into trouble if a function is prototyped, a
2873 * global var is decled, then the function is defined with usage of
2874 * the global var. See glslparsertest's CorrectModule.frag.
2875 */
2876 instructions->push_head(var);
Ian Romanick5466b632010-07-01 12:46:55 -07002877 }
2878
Eric Anholtfa33d0b2010-07-29 13:50:17 -07002879 instructions->append_list(&initializer_instructions);
Ian Romanicka87ac252010-02-22 13:19:34 -08002880 }
2881
Eric Anholt85584592010-04-14 15:38:52 -07002882
2883 /* Generally, variable declarations do not have r-values. However,
2884 * one is used for the declaration in
2885 *
2886 * while (bool b = some_condition()) {
2887 * ...
2888 * }
2889 *
2890 * so we return the rvalue from the last seen declaration here.
Ian Romanicka87ac252010-02-22 13:19:34 -08002891 */
Eric Anholt85584592010-04-14 15:38:52 -07002892 return result;
Ian Romanicka87ac252010-02-22 13:19:34 -08002893}
2894
2895
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07002896ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08002897ast_parameter_declarator::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08002898 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08002899{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002900 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -08002901 const struct glsl_type *type;
2902 const char *name = NULL;
Eric Anholt2e063f12010-03-28 00:56:22 -07002903 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08002904
Ian Romanickd612a122010-03-31 16:22:06 -07002905 type = this->type->specifier->glsl_type(& name, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08002906
2907 if (type == NULL) {
Ian Romanicka87ac252010-02-22 13:19:34 -08002908 if (name != NULL) {
2909 _mesa_glsl_error(& loc, state,
2910 "invalid type `%s' in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08002911 name, this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08002912 } else {
2913 _mesa_glsl_error(& loc, state,
2914 "invalid type in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08002915 this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08002916 }
2917
Ian Romanick0471e8b2010-03-26 14:33:41 -07002918 type = glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -08002919 }
2920
Eric Anholt068c80c2010-03-31 09:56:36 -10002921 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2922 *
2923 * "Functions that accept no input arguments need not use void in the
2924 * argument list because prototypes (or definitions) are required and
2925 * therefore there is no ambiguity when an empty argument list "( )" is
2926 * declared. The idiom "(void)" as a parameter list is provided for
2927 * convenience."
2928 *
2929 * Placing this check here prevents a void parameter being set up
2930 * for a function, which avoids tripping up checks for main taking
2931 * parameters and lookups of an unnamed symbol.
2932 */
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002933 if (type->is_void()) {
2934 if (this->identifier != NULL)
2935 _mesa_glsl_error(& loc, state,
2936 "named parameter cannot have type `void'");
2937
2938 is_void = true;
Eric Anholt068c80c2010-03-31 09:56:36 -10002939 return NULL;
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002940 }
Eric Anholt068c80c2010-03-31 09:56:36 -10002941
Ian Romanick45d8a702010-04-02 15:09:33 -07002942 if (formal_parameter && (this->identifier == NULL)) {
2943 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
2944 return NULL;
2945 }
2946
Kenneth Graunkee511a352010-08-21 15:30:34 -07002947 /* This only handles "vec4 foo[..]". The earlier specifier->glsl_type(...)
2948 * call already handled the "vec4[..] foo" case.
2949 */
2950 if (this->is_array) {
Kenneth Graunked8e34e22010-08-07 02:56:01 -07002951 type = process_array_type(&loc, type, this->array_size, state);
Kenneth Graunkee511a352010-08-21 15:30:34 -07002952 }
2953
Ian Romanicka04211e2011-10-24 11:45:50 -07002954 if (!type->is_error() && type->array_size() == 0) {
Kenneth Graunkee511a352010-08-21 15:30:34 -07002955 _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
2956 "a declared size.");
2957 type = glsl_type::error_type;
2958 }
2959
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002960 is_void = false;
Ian Romanick7e2aa912010-07-19 17:12:42 -07002961 ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
Ian Romanicka87ac252010-02-22 13:19:34 -08002962
Ian Romanickcdb8d542010-03-11 14:48:51 -08002963 /* Apply any specified qualifiers to the parameter declaration. Note that
2964 * for function parameters the default mode is 'in'.
2965 */
Eric Anholt2e063f12010-03-28 00:56:22 -07002966 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08002967
Paul Berryf0722102011-07-12 12:03:02 -07002968 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
2969 *
2970 * "Samplers cannot be treated as l-values; hence cannot be used
2971 * as out or inout function parameters, nor can they be assigned
2972 * into."
2973 */
2974 if ((var->mode == ir_var_inout || var->mode == ir_var_out)
2975 && type->contains_sampler()) {
2976 _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers");
2977 type = glsl_type::error_type;
2978 }
2979
Paul Berry00792e32011-09-10 07:48:46 -07002980 /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
2981 *
2982 * "When calling a function, expressions that do not evaluate to
2983 * l-values cannot be passed to parameters declared as out or inout."
2984 *
2985 * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
2986 *
2987 * "Other binary or unary expressions, non-dereferenced arrays,
2988 * function names, swizzles with repeated fields, and constants
2989 * cannot be l-values."
2990 *
2991 * So for GLSL 1.10, passing an array as an out or inout parameter is not
2992 * allowed. This restriction is removed in GLSL 1.20, and in GLSL ES.
2993 */
2994 if ((var->mode == ir_var_inout || var->mode == ir_var_out)
2995 && type->is_array() && state->language_version == 110) {
2996 _mesa_glsl_error(&loc, state, "Arrays cannot be out or inout parameters in GLSL 1.10");
2997 type = glsl_type::error_type;
2998 }
2999
Ian Romanick0044e7e2010-03-08 23:44:00 -08003000 instructions->push_tail(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08003001
3002 /* Parameter declarations do not have r-values.
3003 */
3004 return NULL;
3005}
3006
3007
Ian Romanick45d8a702010-04-02 15:09:33 -07003008void
Ian Romanick304ea902010-05-10 11:17:53 -07003009ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
Ian Romanick45d8a702010-04-02 15:09:33 -07003010 bool formal,
3011 exec_list *ir_parameters,
3012 _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08003013{
Ian Romanickcf37c9e2010-04-02 15:30:45 -07003014 ast_parameter_declarator *void_param = NULL;
3015 unsigned count = 0;
Ian Romanicka87ac252010-02-22 13:19:34 -08003016
Ian Romanick2b97dc62010-05-10 17:42:05 -07003017 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
Ian Romanick45d8a702010-04-02 15:09:33 -07003018 param->formal_parameter = formal;
Eric Anholt068c80c2010-03-31 09:56:36 -10003019 param->hir(ir_parameters, state);
Ian Romanickcf37c9e2010-04-02 15:30:45 -07003020
3021 if (param->is_void)
3022 void_param = param;
3023
3024 count++;
3025 }
3026
3027 if ((void_param != NULL) && (count > 1)) {
3028 YYLTYPE loc = void_param->get_location();
3029
3030 _mesa_glsl_error(& loc, state,
3031 "`void' parameter must be only parameter");
Ian Romanicka87ac252010-02-22 13:19:34 -08003032 }
3033}
3034
3035
Kenneth Graunke6fae1e42010-12-06 10:54:05 -08003036void
Paul Berry0d81b0e2011-07-29 15:28:52 -07003037emit_function(_mesa_glsl_parse_state *state, ir_function *f)
Kenneth Graunke6fae1e42010-12-06 10:54:05 -08003038{
Paul Berry0d81b0e2011-07-29 15:28:52 -07003039 /* IR invariants disallow function declarations or definitions
3040 * nested within other function definitions. But there is no
3041 * requirement about the relative order of function declarations
3042 * and definitions with respect to one another. So simply insert
3043 * the new ir_function block at the end of the toplevel instruction
3044 * list.
3045 */
3046 state->toplevel_ir->push_tail(f);
Kenneth Graunke6fae1e42010-12-06 10:54:05 -08003047}
3048
3049
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07003050ir_rvalue *
Ian Romanick92318a92010-03-31 18:23:21 -07003051ast_function::hir(exec_list *instructions,
3052 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08003053{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003054 void *ctx = state;
Ian Romanick18238de2010-03-01 13:49:10 -08003055 ir_function *f = NULL;
Ian Romanick92318a92010-03-31 18:23:21 -07003056 ir_function_signature *sig = NULL;
3057 exec_list hir_parameters;
Ian Romanicka87ac252010-02-22 13:19:34 -08003058
Kenneth Graunkeac04c252010-06-29 00:48:10 -07003059 const char *const name = identifier;
Ian Romanicka87ac252010-02-22 13:19:34 -08003060
Ian Romanick9a3bd5e2011-08-29 14:56:29 -07003061 /* New functions are always added to the top-level IR instruction stream,
3062 * so this instruction list pointer is ignored. See also emit_function
3063 * (called below).
3064 */
3065 (void) instructions;
3066
Ian Romanick63b80f82010-09-01 06:34:58 -07003067 /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
3068 *
3069 * "Function declarations (prototypes) cannot occur inside of functions;
3070 * they must be at global scope, or for the built-in functions, outside
3071 * the global scope."
3072 *
3073 * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
3074 *
3075 * "User defined functions may only be defined within the global scope."
3076 *
3077 * Note that this language does not appear in GLSL 1.10.
3078 */
3079 if ((state->current_function != NULL) && (state->language_version != 110)) {
3080 YYLTYPE loc = this->get_location();
3081 _mesa_glsl_error(&loc, state,
3082 "declaration of function `%s' not allowed within "
3083 "function body", name);
3084 }
3085
Kenneth Graunkeedd180f2010-08-20 02:14:35 -07003086 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3087 *
3088 * "Identifiers starting with "gl_" are reserved for use by
3089 * OpenGL, and may not be declared in a shader as either a
3090 * variable or a function."
3091 */
3092 if (strncmp(name, "gl_", 3) == 0) {
3093 YYLTYPE loc = this->get_location();
3094 _mesa_glsl_error(&loc, state,
3095 "identifier `%s' uses reserved `gl_' prefix", name);
3096 }
3097
Ian Romanicka87ac252010-02-22 13:19:34 -08003098 /* Convert the list of function parameters to HIR now so that they can be
3099 * used below to compare this function's signature with previously seen
3100 * signatures for functions with the same name.
3101 */
Ian Romanick45d8a702010-04-02 15:09:33 -07003102 ast_parameter_declarator::parameters_to_hir(& this->parameters,
3103 is_definition,
3104 & hir_parameters, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08003105
Ian Romanicke39cc692010-03-23 12:19:13 -07003106 const char *return_type_name;
3107 const glsl_type *return_type =
Ian Romanick92318a92010-03-31 18:23:21 -07003108 this->return_type->specifier->glsl_type(& return_type_name, state);
Ian Romanicke39cc692010-03-23 12:19:13 -07003109
Eric Anholt76e96d72010-08-23 13:26:52 -07003110 if (!return_type) {
3111 YYLTYPE loc = this->get_location();
3112 _mesa_glsl_error(&loc, state,
3113 "function `%s' has undeclared return type `%s'",
3114 name, return_type_name);
3115 return_type = glsl_type::error_type;
3116 }
Ian Romanicke39cc692010-03-23 12:19:13 -07003117
Kenneth Graunkeac04c252010-06-29 00:48:10 -07003118 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3119 * "No qualifier is allowed on the return type of a function."
3120 */
3121 if (this->return_type->has_qualifiers()) {
3122 YYLTYPE loc = this->get_location();
3123 _mesa_glsl_error(& loc, state,
3124 "function `%s' return type has qualifiers", name);
3125 }
3126
Paul Berryf0722102011-07-12 12:03:02 -07003127 /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3128 *
3129 * "[Sampler types] can only be declared as function parameters
3130 * or uniform variables (see Section 4.3.5 "Uniform")".
3131 */
3132 if (return_type->contains_sampler()) {
3133 YYLTYPE loc = this->get_location();
3134 _mesa_glsl_error(&loc, state,
3135 "function `%s' return type can't contain a sampler",
3136 name);
3137 }
3138
Ian Romanicka87ac252010-02-22 13:19:34 -08003139 /* Verify that this function's signature either doesn't match a previously
3140 * seen signature for a function with the same name, or, if a match is found,
3141 * that the previously seen signature does not have an associated definition.
3142 */
Ian Romanicke466b182010-09-01 14:16:53 -07003143 f = state->symbols->get_function(name);
Kenneth Graunke81f03392010-09-16 02:52:25 -07003144 if (f != NULL && (state->es_shader || f->has_user_signature())) {
Ian Romanick202604e2010-08-11 16:58:25 -07003145 sig = f->exact_matching_signature(&hir_parameters);
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003146 if (sig != NULL) {
3147 const char *badvar = sig->qualifiers_match(&hir_parameters);
3148 if (badvar != NULL) {
3149 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08003150
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003151 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
3152 "qualifiers don't match prototype", name, badvar);
Ian Romanicka87ac252010-02-22 13:19:34 -08003153 }
3154
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003155 if (sig->return_type != return_type) {
3156 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08003157
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003158 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
3159 "match prototype", name);
3160 }
3161
3162 if (is_definition && sig->is_defined) {
3163 YYLTYPE loc = this->get_location();
3164
3165 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07003166 }
3167 }
Ian Romanicka87ac252010-02-22 13:19:34 -08003168 } else {
Carl Worth1660a292010-06-23 18:11:51 -07003169 f = new(ctx) ir_function(name);
Eric Anholte8f5ebf2010-11-05 06:08:45 -07003170 if (!state->symbols->add_function(f)) {
Kenneth Graunkee0959132010-08-25 16:37:46 -07003171 /* This function name shadows a non-function use of the same name. */
3172 YYLTYPE loc = this->get_location();
3173
3174 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
3175 "non-function", name);
3176 return NULL;
3177 }
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07003178
Paul Berry0d81b0e2011-07-29 15:28:52 -07003179 emit_function(state, f);
Ian Romanicka87ac252010-02-22 13:19:34 -08003180 }
3181
Eric Anholtab372da2010-03-28 01:24:55 -07003182 /* Verify the return type of main() */
3183 if (strcmp(name, "main") == 0) {
Ian Romanick25711a82010-03-31 17:39:10 -07003184 if (! return_type->is_void()) {
Eric Anholtab372da2010-03-28 01:24:55 -07003185 YYLTYPE loc = this->get_location();
3186
3187 _mesa_glsl_error(& loc, state, "main() must return void");
3188 }
Eric Anholt174cc032010-03-30 23:37:51 -10003189
Ian Romanick92318a92010-03-31 18:23:21 -07003190 if (!hir_parameters.is_empty()) {
Eric Anholt174cc032010-03-30 23:37:51 -10003191 YYLTYPE loc = this->get_location();
3192
3193 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
3194 }
Eric Anholtab372da2010-03-28 01:24:55 -07003195 }
Ian Romanicka87ac252010-02-22 13:19:34 -08003196
3197 /* Finish storing the information about this new function in its signature.
3198 */
Ian Romanick92318a92010-03-31 18:23:21 -07003199 if (sig == NULL) {
Carl Worth1660a292010-06-23 18:11:51 -07003200 sig = new(ctx) ir_function_signature(return_type);
Ian Romanick92318a92010-03-31 18:23:21 -07003201 f->add_signature(sig);
Ian Romanicka87ac252010-02-22 13:19:34 -08003202 }
3203
Kenneth Graunkebff60132010-04-28 12:44:24 -07003204 sig->replace_parameters(&hir_parameters);
Ian Romanick92318a92010-03-31 18:23:21 -07003205 signature = sig;
Ian Romanicke29a5852010-03-31 17:54:26 -07003206
Ian Romanick92318a92010-03-31 18:23:21 -07003207 /* Function declarations (prototypes) do not have r-values.
3208 */
3209 return NULL;
3210}
3211
3212
3213ir_rvalue *
3214ast_function_definition::hir(exec_list *instructions,
3215 struct _mesa_glsl_parse_state *state)
3216{
3217 prototype->is_definition = true;
3218 prototype->hir(instructions, state);
3219
3220 ir_function_signature *signature = prototype->signature;
Kenneth Graunke826a39c2010-08-20 02:04:52 -07003221 if (signature == NULL)
3222 return NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08003223
Ian Romanick41ec6a42010-03-19 17:08:05 -07003224 assert(state->current_function == NULL);
3225 state->current_function = signature;
Kenneth Graunke6de82562010-06-29 09:59:40 -07003226 state->found_return = false;
Ian Romanick41ec6a42010-03-19 17:08:05 -07003227
Ian Romanicke29a5852010-03-31 17:54:26 -07003228 /* Duplicate parameters declared in the prototype as concrete variables.
3229 * Add these to the symbol table.
Ian Romanicka87ac252010-02-22 13:19:34 -08003230 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07003231 state->symbols->push_scope();
Ian Romanicke29a5852010-03-31 17:54:26 -07003232 foreach_iter(exec_list_iterator, iter, signature->parameters) {
Eric Anholtfbc7c0b2010-04-07 14:32:53 -07003233 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
Ian Romanicka87ac252010-02-22 13:19:34 -08003234
Eric Anholtfbc7c0b2010-04-07 14:32:53 -07003235 assert(var != NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08003236
Ian Romanick3359e582010-03-19 15:38:52 -07003237 /* The only way a parameter would "exist" is if two parameters have
3238 * the same name.
3239 */
3240 if (state->symbols->name_declared_this_scope(var->name)) {
3241 YYLTYPE loc = this->get_location();
3242
3243 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
3244 } else {
Eric Anholt001eee52010-11-05 06:11:24 -07003245 state->symbols->add_variable(var);
Ian Romanick3359e582010-03-19 15:38:52 -07003246 }
Ian Romanicka87ac252010-02-22 13:19:34 -08003247 }
3248
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07003249 /* Convert the body of the function to HIR. */
Eric Anholt894ea972010-04-07 13:19:11 -07003250 this->body->hir(&signature->body, state);
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07003251 signature->is_defined = true;
Ian Romanicka87ac252010-02-22 13:19:34 -08003252
Ian Romanick8bde4ce2010-03-19 11:57:24 -07003253 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08003254
Ian Romanick41ec6a42010-03-19 17:08:05 -07003255 assert(state->current_function == signature);
3256 state->current_function = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08003257
Kenneth Graunke6de82562010-06-29 09:59:40 -07003258 if (!signature->return_type->is_void() && !state->found_return) {
3259 YYLTYPE loc = this->get_location();
3260 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
3261 "%s, but no return statement",
3262 signature->function_name(),
3263 signature->return_type->name);
3264 }
3265
Ian Romanicka87ac252010-02-22 13:19:34 -08003266 /* Function definitions do not have r-values.
3267 */
3268 return NULL;
3269}
Ian Romanick16a246c2010-03-19 16:45:19 -07003270
3271
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07003272ir_rvalue *
Ian Romanick16a246c2010-03-19 16:45:19 -07003273ast_jump_statement::hir(exec_list *instructions,
3274 struct _mesa_glsl_parse_state *state)
3275{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003276 void *ctx = state;
Ian Romanick16a246c2010-03-19 16:45:19 -07003277
Ian Romanickc0e76d82010-04-05 16:53:19 -07003278 switch (mode) {
3279 case ast_return: {
Ian Romanick16a246c2010-03-19 16:45:19 -07003280 ir_return *inst;
Eric Anholtaad7c772010-03-30 23:28:20 -10003281 assert(state->current_function);
Ian Romanick16a246c2010-03-19 16:45:19 -07003282
3283 if (opt_return_value) {
Chad Versaceb4cdba62010-11-17 10:28:01 -08003284 ir_rvalue *const ret = opt_return_value->hir(instructions, state);
Ian Romanick2db46fe2011-01-22 17:47:05 -08003285
3286 /* The value of the return type can be NULL if the shader says
3287 * 'return foo();' and foo() is a function that returns void.
3288 *
3289 * NOTE: The GLSL spec doesn't say that this is an error. The type
3290 * of the return value is void. If the return type of the function is
3291 * also void, then this should compile without error. Seriously.
3292 */
3293 const glsl_type *const ret_type =
3294 (ret == NULL) ? glsl_type::void_type : ret->type;
Ian Romanick16a246c2010-03-19 16:45:19 -07003295
Kenneth Graunke18707eb2010-06-28 23:38:04 -07003296 /* Implicit conversions are not allowed for return values. */
Ian Romanick2db46fe2011-01-22 17:47:05 -08003297 if (state->current_function->return_type != ret_type) {
Kenneth Graunke18707eb2010-06-28 23:38:04 -07003298 YYLTYPE loc = this->get_location();
3299
3300 _mesa_glsl_error(& loc, state,
3301 "`return' with wrong type %s, in function `%s' "
3302 "returning %s",
Ian Romanick2db46fe2011-01-22 17:47:05 -08003303 ret_type->name,
Kenneth Graunke18707eb2010-06-28 23:38:04 -07003304 state->current_function->function_name(),
3305 state->current_function->return_type->name);
3306 }
Ian Romanick16a246c2010-03-19 16:45:19 -07003307
Carl Worth1660a292010-06-23 18:11:51 -07003308 inst = new(ctx) ir_return(ret);
Ian Romanick16a246c2010-03-19 16:45:19 -07003309 } else {
Eric Anholtaad7c772010-03-30 23:28:20 -10003310 if (state->current_function->return_type->base_type !=
3311 GLSL_TYPE_VOID) {
3312 YYLTYPE loc = this->get_location();
3313
3314 _mesa_glsl_error(& loc, state,
3315 "`return' with no value, in function %s returning "
3316 "non-void",
Kenneth Graunkef96c52b2010-04-21 15:17:26 -07003317 state->current_function->function_name());
Eric Anholtaad7c772010-03-30 23:28:20 -10003318 }
Carl Worth1660a292010-06-23 18:11:51 -07003319 inst = new(ctx) ir_return;
Ian Romanick16a246c2010-03-19 16:45:19 -07003320 }
3321
Kenneth Graunke6de82562010-06-29 09:59:40 -07003322 state->found_return = true;
Ian Romanick16a246c2010-03-19 16:45:19 -07003323 instructions->push_tail(inst);
Ian Romanickc0e76d82010-04-05 16:53:19 -07003324 break;
Ian Romanick16a246c2010-03-19 16:45:19 -07003325 }
3326
Ian Romanickc0e76d82010-04-05 16:53:19 -07003327 case ast_discard:
Eric Anholtb9802072010-03-30 23:40:14 -10003328 if (state->target != fragment_shader) {
3329 YYLTYPE loc = this->get_location();
3330
3331 _mesa_glsl_error(& loc, state,
3332 "`discard' may only appear in a fragment shader");
3333 }
Kenneth Graunke77049a72010-06-30 14:11:00 -07003334 instructions->push_tail(new(ctx) ir_discard);
Ian Romanickc0e76d82010-04-05 16:53:19 -07003335 break;
3336
3337 case ast_break:
3338 case ast_continue:
Ian Romanick4cf20cd2010-04-05 17:13:47 -07003339 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
3340 * FINISHME: and they use a different IR instruction for 'break'.
3341 */
3342 /* FINISHME: Correctly handle the nesting. If a switch-statement is
3343 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
3344 * FINISHME: loop.
3345 */
3346 if (state->loop_or_switch_nesting == NULL) {
3347 YYLTYPE loc = this->get_location();
3348
3349 _mesa_glsl_error(& loc, state,
3350 "`%s' may only appear in a loop",
3351 (mode == ast_break) ? "break" : "continue");
3352 } else {
3353 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
3354
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07003355 /* Inline the for loop expression again, since we don't know
3356 * where near the end of the loop body the normal copy of it
3357 * is going to be placed.
3358 */
3359 if (mode == ast_continue &&
3360 state->loop_or_switch_nesting_ast->rest_expression) {
3361 state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
3362 state);
3363 }
3364
Ian Romanick4cf20cd2010-04-05 17:13:47 -07003365 if (loop != NULL) {
3366 ir_loop_jump *const jump =
Carl Worth1660a292010-06-23 18:11:51 -07003367 new(ctx) ir_loop_jump((mode == ast_break)
3368 ? ir_loop_jump::jump_break
3369 : ir_loop_jump::jump_continue);
Ian Romanick4cf20cd2010-04-05 17:13:47 -07003370 instructions->push_tail(jump);
3371 }
3372 }
3373
Ian Romanickc0e76d82010-04-05 16:53:19 -07003374 break;
Eric Anholtb9802072010-03-30 23:40:14 -10003375 }
3376
Ian Romanick16a246c2010-03-19 16:45:19 -07003377 /* Jump instructions do not have r-values.
3378 */
3379 return NULL;
3380}
Ian Romanick3c6fea32010-03-29 14:11:25 -07003381
3382
3383ir_rvalue *
3384ast_selection_statement::hir(exec_list *instructions,
3385 struct _mesa_glsl_parse_state *state)
3386{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003387 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07003388
Ian Romanick3c6fea32010-03-29 14:11:25 -07003389 ir_rvalue *const condition = this->condition->hir(instructions, state);
Ian Romanick3c6fea32010-03-29 14:11:25 -07003390
3391 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
3392 *
3393 * "Any expression whose type evaluates to a Boolean can be used as the
3394 * conditional expression bool-expression. Vector types are not accepted
3395 * as the expression to if."
3396 *
3397 * The checks are separated so that higher quality diagnostics can be
3398 * generated for cases where both rules are violated.
3399 */
3400 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
3401 YYLTYPE loc = this->condition->get_location();
3402
3403 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
3404 "boolean");
3405 }
3406
Carl Worth1660a292010-06-23 18:11:51 -07003407 ir_if *const stmt = new(ctx) ir_if(condition);
Ian Romanick3c6fea32010-03-29 14:11:25 -07003408
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003409 if (then_statement != NULL) {
3410 state->symbols->push_scope();
Ian Romanick4f9d72f2010-05-10 11:10:26 -07003411 then_statement->hir(& stmt->then_instructions, state);
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003412 state->symbols->pop_scope();
3413 }
Ian Romanick3c6fea32010-03-29 14:11:25 -07003414
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003415 if (else_statement != NULL) {
3416 state->symbols->push_scope();
Ian Romanick4f9d72f2010-05-10 11:10:26 -07003417 else_statement->hir(& stmt->else_instructions, state);
Kenneth Graunke665d75c2010-08-18 13:54:50 -07003418 state->symbols->pop_scope();
3419 }
Ian Romanick3c6fea32010-03-29 14:11:25 -07003420
3421 instructions->push_tail(stmt);
3422
3423 /* if-statements do not have r-values.
3424 */
3425 return NULL;
3426}
Ian Romanick9e7d0102010-04-05 16:37:49 -07003427
3428
Ian Romanick8c46ed22010-04-05 18:07:27 -07003429void
3430ast_iteration_statement::condition_to_hir(ir_loop *stmt,
3431 struct _mesa_glsl_parse_state *state)
Ian Romanick9e7d0102010-04-05 16:37:49 -07003432{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003433 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07003434
Ian Romanick9e7d0102010-04-05 16:37:49 -07003435 if (condition != NULL) {
3436 ir_rvalue *const cond =
3437 condition->hir(& stmt->body_instructions, state);
3438
3439 if ((cond == NULL)
3440 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
3441 YYLTYPE loc = condition->get_location();
3442
3443 _mesa_glsl_error(& loc, state,
3444 "loop condition must be scalar boolean");
3445 } else {
3446 /* As the first code in the loop body, generate a block that looks
3447 * like 'if (!condition) break;' as the loop termination condition.
3448 */
3449 ir_rvalue *const not_cond =
Carl Worth1660a292010-06-23 18:11:51 -07003450 new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
3451 NULL);
Ian Romanick9e7d0102010-04-05 16:37:49 -07003452
Carl Worth1660a292010-06-23 18:11:51 -07003453 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
Ian Romanick9e7d0102010-04-05 16:37:49 -07003454
3455 ir_jump *const break_stmt =
Carl Worth1660a292010-06-23 18:11:51 -07003456 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
Ian Romanick9e7d0102010-04-05 16:37:49 -07003457
3458 if_stmt->then_instructions.push_tail(break_stmt);
3459 stmt->body_instructions.push_tail(if_stmt);
3460 }
3461 }
Ian Romanick8c46ed22010-04-05 18:07:27 -07003462}
3463
3464
3465ir_rvalue *
3466ast_iteration_statement::hir(exec_list *instructions,
3467 struct _mesa_glsl_parse_state *state)
3468{
Kenneth Graunke953ff122010-06-25 13:14:37 -07003469 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07003470
Ian Romanick48460662010-04-16 16:42:43 -07003471 /* For-loops and while-loops start a new scope, but do-while loops do not.
Ian Romanick8c46ed22010-04-05 18:07:27 -07003472 */
Ian Romanick48460662010-04-16 16:42:43 -07003473 if (mode != ast_do_while)
Ian Romanick8c46ed22010-04-05 18:07:27 -07003474 state->symbols->push_scope();
3475
3476 if (init_statement != NULL)
3477 init_statement->hir(instructions, state);
3478
Carl Worth1660a292010-06-23 18:11:51 -07003479 ir_loop *const stmt = new(ctx) ir_loop();
Ian Romanick8c46ed22010-04-05 18:07:27 -07003480 instructions->push_tail(stmt);
3481
3482 /* Track the current loop and / or switch-statement nesting.
3483 */
3484 ir_instruction *const nesting = state->loop_or_switch_nesting;
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07003485 ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
3486
Ian Romanick8c46ed22010-04-05 18:07:27 -07003487 state->loop_or_switch_nesting = stmt;
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07003488 state->loop_or_switch_nesting_ast = this;
Ian Romanick8c46ed22010-04-05 18:07:27 -07003489
3490 if (mode != ast_do_while)
3491 condition_to_hir(stmt, state);
Ian Romanick9e7d0102010-04-05 16:37:49 -07003492
Ian Romanick4f9d72f2010-05-10 11:10:26 -07003493 if (body != NULL)
3494 body->hir(& stmt->body_instructions, state);
Ian Romanick9e7d0102010-04-05 16:37:49 -07003495
3496 if (rest_expression != NULL)
3497 rest_expression->hir(& stmt->body_instructions, state);
3498
Ian Romanick8c46ed22010-04-05 18:07:27 -07003499 if (mode == ast_do_while)
3500 condition_to_hir(stmt, state);
3501
Ian Romanick48460662010-04-16 16:42:43 -07003502 if (mode != ast_do_while)
Ian Romanick9e7d0102010-04-05 16:37:49 -07003503 state->symbols->pop_scope();
3504
Ian Romanicke9d0f262010-04-05 17:01:53 -07003505 /* Restore previous nesting before returning.
3506 */
3507 state->loop_or_switch_nesting = nesting;
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07003508 state->loop_or_switch_nesting_ast = nesting_ast;
Ian Romanicke9d0f262010-04-05 17:01:53 -07003509
Ian Romanick9e7d0102010-04-05 16:37:49 -07003510 /* Loops do not have r-values.
3511 */
3512 return NULL;
3513}
Ian Romanick3455ce62010-04-19 15:13:15 -07003514
3515
3516ir_rvalue *
3517ast_type_specifier::hir(exec_list *instructions,
3518 struct _mesa_glsl_parse_state *state)
3519{
Chad Versace08a286c2011-01-16 21:44:57 -08003520 if (!this->is_precision_statement && this->structure == NULL)
3521 return NULL;
3522
3523 YYLTYPE loc = this->get_location();
3524
3525 if (this->precision != ast_precision_none
3526 && state->language_version != 100
3527 && state->language_version < 130) {
3528 _mesa_glsl_error(&loc, state,
3529 "precision qualifiers exist only in "
3530 "GLSL ES 1.00, and GLSL 1.30 and later");
3531 return NULL;
3532 }
3533 if (this->precision != ast_precision_none
3534 && this->structure != NULL) {
3535 _mesa_glsl_error(&loc, state,
3536 "precision qualifiers do not apply to structures");
3537 return NULL;
3538 }
3539
3540 /* If this is a precision statement, check that the type to which it is
3541 * applied is either float or int.
3542 *
3543 * From section 4.5.3 of the GLSL 1.30 spec:
3544 * "The precision statement
3545 * precision precision-qualifier type;
3546 * can be used to establish a default precision qualifier. The type
3547 * field can be either int or float [...]. Any other types or
3548 * qualifiers will result in an error.
3549 */
3550 if (this->is_precision_statement) {
3551 assert(this->precision != ast_precision_none);
3552 assert(this->structure == NULL); /* The check for structures was
3553 * performed above. */
3554 if (this->is_array) {
3555 _mesa_glsl_error(&loc, state,
3556 "default precision statements do not apply to "
3557 "arrays");
3558 return NULL;
3559 }
3560 if (this->type_specifier != ast_float
3561 && this->type_specifier != ast_int) {
3562 _mesa_glsl_error(&loc, state,
3563 "default precision statements apply only to types "
3564 "float and int");
3565 return NULL;
3566 }
3567
3568 /* FINISHME: Translate precision statements into IR. */
3569 return NULL;
3570 }
3571
Ian Romanick3455ce62010-04-19 15:13:15 -07003572 if (this->structure != NULL)
3573 return this->structure->hir(instructions, state);
Ian Romanick85ba37b2010-04-21 14:33:34 -07003574
3575 return NULL;
Ian Romanick3455ce62010-04-19 15:13:15 -07003576}
3577
3578
3579ir_rvalue *
3580ast_struct_specifier::hir(exec_list *instructions,
3581 struct _mesa_glsl_parse_state *state)
3582{
Ian Romanick3455ce62010-04-19 15:13:15 -07003583 unsigned decl_count = 0;
3584
3585 /* Make an initial pass over the list of structure fields to determine how
3586 * many there are. Each element in this list is an ast_declarator_list.
3587 * This means that we actually need to count the number of elements in the
3588 * 'declarations' list in each of the elements.
3589 */
Ian Romanick2b97dc62010-05-10 17:42:05 -07003590 foreach_list_typed (ast_declarator_list, decl_list, link,
3591 &this->declarations) {
Ian Romanick304ea902010-05-10 11:17:53 -07003592 foreach_list_const (decl_ptr, & decl_list->declarations) {
Ian Romanick3455ce62010-04-19 15:13:15 -07003593 decl_count++;
3594 }
3595 }
3596
Ian Romanick3455ce62010-04-19 15:13:15 -07003597 /* Allocate storage for the structure fields and process the field
3598 * declarations. As the declarations are processed, try to also convert
3599 * the types to HIR. This ensures that structure definitions embedded in
3600 * other structure definitions are processed.
3601 */
Kenneth Graunked3073f52011-01-21 14:32:31 -08003602 glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field,
Eric Anholt21b0dbd2010-07-20 16:47:25 -07003603 decl_count);
Ian Romanick3455ce62010-04-19 15:13:15 -07003604
3605 unsigned i = 0;
Ian Romanick2b97dc62010-05-10 17:42:05 -07003606 foreach_list_typed (ast_declarator_list, decl_list, link,
3607 &this->declarations) {
Ian Romanick3455ce62010-04-19 15:13:15 -07003608 const char *type_name;
3609
3610 decl_list->type->specifier->hir(instructions, state);
3611
Kenneth Graunkec98deb12010-08-16 14:02:25 -07003612 /* Section 10.9 of the GLSL ES 1.00 specification states that
3613 * embedded structure definitions have been removed from the language.
3614 */
3615 if (state->es_shader && decl_list->type->specifier->structure != NULL) {
3616 YYLTYPE loc = this->get_location();
3617 _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
3618 "not allowed in GLSL ES 1.00.");
3619 }
3620
Ian Romanick3455ce62010-04-19 15:13:15 -07003621 const glsl_type *decl_type =
3622 decl_list->type->specifier->glsl_type(& type_name, state);
3623
Ian Romanick2b97dc62010-05-10 17:42:05 -07003624 foreach_list_typed (ast_declaration, decl, link,
3625 &decl_list->declarations) {
Kenneth Graunked8e34e22010-08-07 02:56:01 -07003626 const struct glsl_type *field_type = decl_type;
3627 if (decl->is_array) {
3628 YYLTYPE loc = decl->get_location();
3629 field_type = process_array_type(&loc, decl_type, decl->array_size,
3630 state);
3631 }
Ian Romanick73986a72010-04-20 16:49:03 -07003632 fields[i].type = (field_type != NULL)
3633 ? field_type : glsl_type::error_type;
Ian Romanick3455ce62010-04-19 15:13:15 -07003634 fields[i].name = decl->identifier;
3635 i++;
3636 }
3637 }
3638
3639 assert(i == decl_count);
3640
Ian Romanick49e35772010-06-28 11:54:57 -07003641 const glsl_type *t =
Kenneth Graunkeca92ae22010-09-18 11:11:09 +02003642 glsl_type::get_record_instance(fields, decl_count, this->name);
Ian Romanick1d28b612010-04-20 16:48:24 -07003643
Ian Romanickab899272010-04-23 13:24:08 -07003644 YYLTYPE loc = this->get_location();
Ian Romanicka789ca62010-09-01 14:08:08 -07003645 if (!state->symbols->add_type(name, t)) {
Ian Romanickab899272010-04-23 13:24:08 -07003646 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
3647 } else {
Kenneth Graunkeeb639342011-02-27 01:17:29 -08003648 const glsl_type **s = reralloc(state, state->user_structures,
3649 const glsl_type *,
3650 state->num_user_structures + 1);
Ian Romanicka2c6df52010-04-28 13:14:53 -07003651 if (s != NULL) {
3652 s[state->num_user_structures] = t;
3653 state->user_structures = s;
3654 state->num_user_structures++;
3655 }
Ian Romanickab899272010-04-23 13:24:08 -07003656 }
Ian Romanick3455ce62010-04-19 15:13:15 -07003657
3658 /* Structure type definitions do not have r-values.
3659 */
3660 return NULL;
3661}