blob: 292c7be6217ddf8d4ba170e1a9e098d5d3df44ee [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
Ian Romanicka87ac252010-02-22 13:19:34 -080052#include "main/imports.h"
Eric Anholt4a962172010-07-28 14:41:51 -070053#include "main/extensions.h"
Ian Romanick8bde4ce2010-03-19 11:57:24 -070054#include "glsl_symbol_table.h"
Ian Romanicka87ac252010-02-22 13:19:34 -080055#include "glsl_parser_extras.h"
56#include "ast.h"
57#include "glsl_types.h"
58#include "ir.h"
59
Ian Romanickd949a9a2010-03-10 09:55:22 -080060void
61_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
62{
Ian Romanickadfb0cd2010-03-10 10:43:16 -080063 _mesa_glsl_initialize_variables(instructions, state);
Eric Anholtc22c4002010-03-26 18:20:30 -070064 _mesa_glsl_initialize_functions(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080065
Ian Romanick41ec6a42010-03-19 17:08:05 -070066 state->current_function = NULL;
67
Ian Romanick2b97dc62010-05-10 17:42:05 -070068 foreach_list_typed (ast_node, ast, link, & state->translation_unit)
Ian Romanick304ea902010-05-10 11:17:53 -070069 ast->hir(instructions, state);
Ian Romanickd949a9a2010-03-10 09:55:22 -080070}
71
72
Ian Romanick01045362010-03-29 16:17:56 -070073/**
74 * If a conversion is available, convert one operand to a different type
75 *
76 * The \c from \c ir_rvalue is converted "in place".
77 *
78 * \param to Type that the operand it to be converted to
79 * \param from Operand that is being converted
80 * \param state GLSL compiler state
81 *
82 * \return
83 * If a conversion is possible (or unnecessary), \c true is returned.
84 * Otherwise \c false is returned.
85 */
86static bool
Ian Romanickbfb09c22010-03-29 16:32:55 -070087apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
Ian Romanick01045362010-03-29 16:17:56 -070088 struct _mesa_glsl_parse_state *state)
89{
Kenneth Graunke953ff122010-06-25 13:14:37 -070090 void *ctx = state;
Ian Romanickbfb09c22010-03-29 16:32:55 -070091 if (to->base_type == from->type->base_type)
Ian Romanick01045362010-03-29 16:17:56 -070092 return true;
93
94 /* This conversion was added in GLSL 1.20. If the compilation mode is
95 * GLSL 1.10, the conversion is skipped.
96 */
97 if (state->language_version < 120)
98 return false;
99
100 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
101 *
102 * "There are no implicit array or structure conversions. For
103 * example, an array of int cannot be implicitly converted to an
104 * array of float. There are no implicit conversions between
105 * signed and unsigned integers."
106 */
107 /* FINISHME: The above comment is partially a lie. There is int/uint
108 * FINISHME: conversion for immediate constants.
109 */
Ian Romanickbfb09c22010-03-29 16:32:55 -0700110 if (!to->is_float() || !from->type->is_numeric())
Ian Romanick01045362010-03-29 16:17:56 -0700111 return false;
112
Kenneth Graunke506199b2010-06-29 15:59:27 -0700113 /* Convert to a floating point type with the same number of components
114 * as the original type - i.e. int to float, not int to vec4.
115 */
116 to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
117 from->type->matrix_columns);
118
Ian Romanickbfb09c22010-03-29 16:32:55 -0700119 switch (from->type->base_type) {
Ian Romanick01045362010-03-29 16:17:56 -0700120 case GLSL_TYPE_INT:
Carl Worth1660a292010-06-23 18:11:51 -0700121 from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700122 break;
123 case GLSL_TYPE_UINT:
Carl Worth1660a292010-06-23 18:11:51 -0700124 from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700125 break;
126 case GLSL_TYPE_BOOL:
Carl Worth1660a292010-06-23 18:11:51 -0700127 from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
Eric Anholtdc58b3f2010-04-02 02:13:43 -1000128 break;
Ian Romanick01045362010-03-29 16:17:56 -0700129 default:
130 assert(0);
131 }
132
133 return true;
134}
135
136
Ian Romanicka87ac252010-02-22 13:19:34 -0800137static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700138arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Ian Romanicka87ac252010-02-22 13:19:34 -0800139 bool multiply,
Eric Anholta13bb142010-03-31 16:38:11 -1000140 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800141{
Eric Anholt336b4ad2010-05-19 10:38:37 -0700142 const glsl_type *type_a = value_a->type;
143 const glsl_type *type_b = value_b->type;
Ian Romanick01045362010-03-29 16:17:56 -0700144
Ian Romanicka87ac252010-02-22 13:19:34 -0800145 /* From GLSL 1.50 spec, page 56:
146 *
147 * "The arithmetic binary operators add (+), subtract (-),
148 * multiply (*), and divide (/) operate on integer and
149 * floating-point scalars, vectors, and matrices."
150 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700151 if (!type_a->is_numeric() || !type_b->is_numeric()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000152 _mesa_glsl_error(loc, state,
153 "Operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700154 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800155 }
156
157
158 /* "If one operand is floating-point based and the other is
159 * not, then the conversions from Section 4.1.10 "Implicit
160 * Conversions" are applied to the non-floating-point-based operand."
Ian Romanicka87ac252010-02-22 13:19:34 -0800161 */
Ian Romanick01045362010-03-29 16:17:56 -0700162 if (!apply_implicit_conversion(type_a, value_b, state)
163 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholta13bb142010-03-31 16:38:11 -1000164 _mesa_glsl_error(loc, state,
165 "Could not implicitly convert operands to "
166 "arithmetic operator");
Ian Romanick01045362010-03-29 16:17:56 -0700167 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800168 }
Eric Anholt336b4ad2010-05-19 10:38:37 -0700169 type_a = value_a->type;
170 type_b = value_b->type;
171
Ian Romanicka87ac252010-02-22 13:19:34 -0800172 /* "If the operands are integer types, they must both be signed or
173 * both be unsigned."
174 *
175 * From this rule and the preceeding conversion it can be inferred that
176 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
Ian Romanick60b54d92010-03-24 17:08:13 -0700177 * The is_numeric check above already filtered out the case where either
178 * type is not one of these, so now the base types need only be tested for
179 * equality.
Ian Romanicka87ac252010-02-22 13:19:34 -0800180 */
181 if (type_a->base_type != type_b->base_type) {
Eric Anholta13bb142010-03-31 16:38:11 -1000182 _mesa_glsl_error(loc, state,
183 "base type mismatch for arithmetic operator");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700184 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800185 }
186
187 /* "All arithmetic binary operators result in the same fundamental type
188 * (signed integer, unsigned integer, or floating-point) as the
189 * operands they operate on, after operand type conversion. After
190 * conversion, the following cases are valid
191 *
192 * * The two operands are scalars. In this case the operation is
193 * applied, resulting in a scalar."
194 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800195 if (type_a->is_scalar() && type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800196 return type_a;
197
198 /* "* One operand is a scalar, and the other is a vector or matrix.
199 * In this case, the scalar operation is applied independently to each
200 * component of the vector or matrix, resulting in the same size
201 * vector or matrix."
202 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800203 if (type_a->is_scalar()) {
204 if (!type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800205 return type_b;
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800206 } else if (type_b->is_scalar()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800207 return type_a;
208 }
209
210 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
211 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
212 * handled.
213 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700214 assert(!type_a->is_scalar());
215 assert(!type_b->is_scalar());
Ian Romanicka87ac252010-02-22 13:19:34 -0800216
217 /* "* The two operands are vectors of the same size. In this case, the
218 * operation is done component-wise resulting in the same size
219 * vector."
220 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800221 if (type_a->is_vector() && type_b->is_vector()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000222 if (type_a == type_b) {
223 return type_a;
224 } else {
225 _mesa_glsl_error(loc, state,
226 "vector size mismatch for arithmetic operator");
227 return glsl_type::error_type;
228 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800229 }
230
231 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
232 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
233 * <vector, vector> have been handled. At least one of the operands must
234 * be matrix. Further, since there are no integer matrix types, the base
235 * type of both operands must be float.
236 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700237 assert(type_a->is_matrix() || type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800238 assert(type_a->base_type == GLSL_TYPE_FLOAT);
239 assert(type_b->base_type == GLSL_TYPE_FLOAT);
240
241 /* "* The operator is add (+), subtract (-), or divide (/), and the
242 * operands are matrices with the same number of rows and the same
243 * number of columns. In this case, the operation is done component-
244 * wise resulting in the same size matrix."
245 * * The operator is multiply (*), where both operands are matrices or
246 * one operand is a vector and the other a matrix. A right vector
247 * operand is treated as a column vector and a left vector operand as a
248 * row vector. In all these cases, it is required that the number of
249 * columns of the left operand is equal to the number of rows of the
250 * right operand. Then, the multiply (*) operation does a linear
251 * algebraic multiply, yielding an object that has the same number of
252 * rows as the left operand and the same number of columns as the right
253 * operand. Section 5.10 "Vector and Matrix Operations" explains in
254 * more detail how vectors and matrices are operated on."
255 */
256 if (! multiply) {
Eric Anholta13bb142010-03-31 16:38:11 -1000257 if (type_a == type_b)
258 return type_a;
Ian Romanicka87ac252010-02-22 13:19:34 -0800259 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800260 if (type_a->is_matrix() && type_b->is_matrix()) {
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700261 /* Matrix multiply. The columns of A must match the rows of B. Given
262 * the other previously tested constraints, this means the vector type
263 * of a row from A must be the same as the vector type of a column from
264 * B.
265 */
266 if (type_a->row_type() == type_b->column_type()) {
267 /* The resulting matrix has the number of columns of matrix B and
268 * the number of rows of matrix A. We get the row count of A by
269 * looking at the size of a vector that makes up a column. The
270 * transpose (size of a row) is done for B.
271 */
Eric Anholta13bb142010-03-31 16:38:11 -1000272 const glsl_type *const type =
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700273 glsl_type::get_instance(type_a->base_type,
274 type_a->column_type()->vector_elements,
275 type_b->row_type()->vector_elements);
Eric Anholta13bb142010-03-31 16:38:11 -1000276 assert(type != glsl_type::error_type);
277
278 return type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800279 }
Ian Romanickfce11502010-03-09 15:58:52 -0800280 } else if (type_a->is_matrix()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800281 /* A is a matrix and B is a column vector. Columns of A must match
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700282 * rows of B. Given the other previously tested constraints, this
283 * means the vector type of a row from A must be the same as the
284 * vector the type of B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800285 */
Carl Worth47c90b12010-07-22 14:56:14 -0700286 if (type_a->row_type() == type_b) {
287 /* The resulting vector has a number of elements equal to
288 * the number of rows of matrix A. */
289 const glsl_type *const type =
290 glsl_type::get_instance(type_a->base_type,
291 type_a->column_type()->vector_elements,
292 1);
293 assert(type != glsl_type::error_type);
294
295 return type;
296 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800297 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800298 assert(type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800299
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700300 /* A is a row vector and B is a matrix. Columns of A must match rows
301 * of B. Given the other previously tested constraints, this means
302 * the type of A must be the same as the vector type of a column from
303 * B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800304 */
Carl Worth47c90b12010-07-22 14:56:14 -0700305 if (type_a == type_b->column_type()) {
306 /* The resulting vector has a number of elements equal to
307 * the number of columns of matrix B. */
308 const glsl_type *const type =
309 glsl_type::get_instance(type_a->base_type,
310 type_b->row_type()->vector_elements,
311 1);
312 assert(type != glsl_type::error_type);
313
314 return type;
315 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800316 }
Eric Anholta13bb142010-03-31 16:38:11 -1000317
318 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
319 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800320 }
321
322
323 /* "All other cases are illegal."
324 */
Eric Anholta13bb142010-03-31 16:38:11 -1000325 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700326 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800327}
328
329
330static const struct glsl_type *
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000331unary_arithmetic_result_type(const struct glsl_type *type,
332 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800333{
334 /* From GLSL 1.50 spec, page 57:
335 *
336 * "The arithmetic unary operators negate (-), post- and pre-increment
337 * and decrement (-- and ++) operate on integer or floating-point
338 * values (including vectors and matrices). All unary operators work
339 * component-wise on their operands. These result with the same type
340 * they operated on."
341 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000342 if (!type->is_numeric()) {
343 _mesa_glsl_error(loc, state,
344 "Operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700345 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000346 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800347
348 return type;
349}
350
351
352static const struct glsl_type *
353modulus_result_type(const struct glsl_type *type_a,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000354 const struct glsl_type *type_b,
355 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800356{
357 /* From GLSL 1.50 spec, page 56:
358 * "The operator modulus (%) operates on signed or unsigned integers or
359 * integer vectors. The operand types must both be signed or both be
360 * unsigned."
361 */
Ian Romanick40176e22010-03-26 14:38:37 -0700362 if (!type_a->is_integer() || !type_b->is_integer()
Ian Romanicka87ac252010-02-22 13:19:34 -0800363 || (type_a->base_type != type_b->base_type)) {
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000364 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700365 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800366 }
367
368 /* "The operands cannot be vectors of differing size. If one operand is
369 * a scalar and the other vector, then the scalar is applied component-
370 * wise to the vector, resulting in the same type as the vector. If both
371 * are vectors of the same size, the result is computed component-wise."
372 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800373 if (type_a->is_vector()) {
374 if (!type_b->is_vector()
Ian Romanicka87ac252010-02-22 13:19:34 -0800375 || (type_a->vector_elements == type_b->vector_elements))
376 return type_a;
377 } else
378 return type_b;
379
380 /* "The operator modulus (%) is not defined for any other data types
381 * (non-integer types)."
382 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000383 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700384 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800385}
386
387
388static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700389relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000390 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800391{
Eric Anholt336b4ad2010-05-19 10:38:37 -0700392 const glsl_type *type_a = value_a->type;
393 const glsl_type *type_b = value_b->type;
Ian Romanick0150f5f2010-03-29 16:20:07 -0700394
Ian Romanicka87ac252010-02-22 13:19:34 -0800395 /* From GLSL 1.50 spec, page 56:
396 * "The relational operators greater than (>), less than (<), greater
397 * than or equal (>=), and less than or equal (<=) operate only on
398 * scalar integer and scalar floating-point expressions."
399 */
Ian Romanicka6d653d2010-03-26 14:40:37 -0700400 if (!type_a->is_numeric()
401 || !type_b->is_numeric()
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800402 || !type_a->is_scalar()
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000403 || !type_b->is_scalar()) {
404 _mesa_glsl_error(loc, state,
405 "Operands to relational operators must be scalar and "
406 "numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700407 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000408 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800409
410 /* "Either the operands' types must match, or the conversions from
411 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
412 * operand, after which the types must match."
Ian Romanicka87ac252010-02-22 13:19:34 -0800413 */
Ian Romanick0150f5f2010-03-29 16:20:07 -0700414 if (!apply_implicit_conversion(type_a, value_b, state)
415 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000416 _mesa_glsl_error(loc, state,
417 "Could not implicitly convert operands to "
418 "relational operator");
Ian Romanick0150f5f2010-03-29 16:20:07 -0700419 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800420 }
Eric Anholt336b4ad2010-05-19 10:38:37 -0700421 type_a = value_a->type;
422 type_b = value_b->type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800423
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000424 if (type_a->base_type != type_b->base_type) {
425 _mesa_glsl_error(loc, state, "base type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700426 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000427 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800428
429 /* "The result is scalar Boolean."
430 */
Ian Romanick0471e8b2010-03-26 14:33:41 -0700431 return glsl_type::bool_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800432}
433
434
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700435/**
436 * Validates that a value can be assigned to a location with a specified type
437 *
438 * Validates that \c rhs can be assigned to some location. If the types are
439 * not an exact match but an automatic conversion is possible, \c rhs will be
440 * converted.
441 *
442 * \return
443 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
444 * Otherwise the actual RHS to be assigned will be returned. This may be
445 * \c rhs, or it may be \c rhs after some type conversion.
446 *
447 * \note
448 * In addition to being used for assignments, this function is used to
449 * type-check return values.
450 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700451ir_rvalue *
Eric Anholt336b4ad2010-05-19 10:38:37 -0700452validate_assignment(struct _mesa_glsl_parse_state *state,
453 const glsl_type *lhs_type, ir_rvalue *rhs)
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700454{
Eric Anholt336b4ad2010-05-19 10:38:37 -0700455 const glsl_type *rhs_type = rhs->type;
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700456
457 /* If there is already some error in the RHS, just return it. Anything
458 * else will lead to an avalanche of error message back to the user.
459 */
460 if (rhs_type->is_error())
461 return rhs;
462
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700463 /* If the types are identical, the assignment can trivially proceed.
464 */
465 if (rhs_type == lhs_type)
466 return rhs;
467
Ian Romanick0157f412010-04-02 17:44:39 -0700468 /* If the array element types are the same and the size of the LHS is zero,
469 * the assignment is okay.
470 *
471 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
472 * is handled by ir_dereference::is_lvalue.
473 */
474 if (lhs_type->is_array() && rhs->type->is_array()
475 && (lhs_type->element_type() == rhs->type->element_type())
476 && (lhs_type->array_size() == 0)) {
477 return rhs;
478 }
479
Eric Anholt336b4ad2010-05-19 10:38:37 -0700480 /* Check for implicit conversion in GLSL 1.20 */
481 if (apply_implicit_conversion(lhs_type, rhs, state)) {
482 rhs_type = rhs->type;
483 if (rhs_type == lhs_type)
484 return rhs;
485 }
486
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700487 return NULL;
488}
489
Eric Anholt10a68522010-03-26 11:53:37 -0700490ir_rvalue *
491do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
492 ir_rvalue *lhs, ir_rvalue *rhs,
493 YYLTYPE lhs_loc)
494{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700495 void *ctx = state;
Eric Anholt10a68522010-03-26 11:53:37 -0700496 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
497
498 if (!error_emitted) {
499 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
500 if (!lhs->is_lvalue()) {
501 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
502 error_emitted = true;
503 }
504 }
505
Eric Anholt336b4ad2010-05-19 10:38:37 -0700506 ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs);
Eric Anholt10a68522010-03-26 11:53:37 -0700507 if (new_rhs == NULL) {
508 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
509 } else {
510 rhs = new_rhs;
Ian Romanick0157f412010-04-02 17:44:39 -0700511
512 /* If the LHS array was not declared with a size, it takes it size from
513 * the RHS. If the LHS is an l-value and a whole array, it must be a
514 * dereference of a variable. Any other case would require that the LHS
515 * is either not an l-value or not a whole array.
516 */
517 if (lhs->type->array_size() == 0) {
518 ir_dereference *const d = lhs->as_dereference();
519
520 assert(d != NULL);
521
Ian Romanick36ea2862010-05-19 13:52:29 +0200522 ir_variable *const var = d->variable_referenced();
Ian Romanick0157f412010-04-02 17:44:39 -0700523
524 assert(var != NULL);
525
Ian Romanick63f39422010-04-05 14:35:47 -0700526 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
527 /* FINISHME: This should actually log the location of the RHS. */
528 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
529 "previous access",
530 var->max_array_access);
531 }
532
Ian Romanickf38d15b2010-07-20 15:33:40 -0700533 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
Ian Romanick0157f412010-04-02 17:44:39 -0700534 rhs->type->array_size());
Eric Anholt9703ed02010-07-22 15:50:37 -0700535 d->type = var->type;
Ian Romanick0157f412010-04-02 17:44:39 -0700536 }
Eric Anholt10a68522010-03-26 11:53:37 -0700537 }
538
Eric Anholt2731a732010-06-23 14:51:14 -0700539 /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
540 * but not post_inc) need the converted assigned value as an rvalue
541 * to handle things like:
542 *
543 * i = j += 1;
544 *
545 * So we always just store the computed value being assigned to a
546 * temporary and return a deref of that temporary. If the rvalue
547 * ends up not being used, the temp will get copy-propagated out.
548 */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700549 ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
550 ir_var_temporary);
Eric Anholte33c1032010-06-24 15:13:03 -0700551 ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
Eric Anholtae805922010-06-24 09:06:12 -0700552 instructions->push_tail(var);
Eric Anholte33c1032010-06-24 15:13:03 -0700553 instructions->push_tail(new(ctx) ir_assignment(deref_var,
Carl Worth1660a292010-06-23 18:11:51 -0700554 rhs,
555 NULL));
Eric Anholte33c1032010-06-24 15:13:03 -0700556 deref_var = new(ctx) ir_dereference_variable(var);
Eric Anholt10a68522010-03-26 11:53:37 -0700557
Ian Romanick8e9ce2e2010-08-03 15:02:35 -0700558 if (!error_emitted)
559 instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
Eric Anholt2731a732010-06-23 14:51:14 -0700560
Carl Worth1660a292010-06-23 18:11:51 -0700561 return new(ctx) ir_dereference_variable(var);
Eric Anholt10a68522010-03-26 11:53:37 -0700562}
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700563
Eric Anholtde38f0e2010-03-26 12:14:54 -0700564static ir_rvalue *
Eric Anholt959a9ec2010-06-23 14:43:50 -0700565get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
Eric Anholtde38f0e2010-03-26 12:14:54 -0700566{
Carl Worth1660a292010-06-23 18:11:51 -0700567 void *ctx = talloc_parent(lvalue);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700568 ir_variable *var;
Eric Anholtde38f0e2010-03-26 12:14:54 -0700569
570 /* FINISHME: Give unique names to the temporaries. */
Ian Romanick7e2aa912010-07-19 17:12:42 -0700571 var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
572 ir_var_temporary);
Eric Anholt43b5b032010-07-07 14:04:30 -0700573 instructions->push_tail(var);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700574 var->mode = ir_var_auto;
575
Carl Worth1660a292010-06-23 18:11:51 -0700576 instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
577 lvalue, NULL));
Eric Anholtde38f0e2010-03-26 12:14:54 -0700578
579 /* Once we've created this temporary, mark it read only so it's no
580 * longer considered an lvalue.
581 */
582 var->read_only = true;
583
Carl Worth1660a292010-06-23 18:11:51 -0700584 return new(ctx) ir_dereference_variable(var);
Eric Anholtde38f0e2010-03-26 12:14:54 -0700585}
586
587
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700588ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -0800589ast_node::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -0800590 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -0800591{
Ian Romanick18238de2010-03-01 13:49:10 -0800592 (void) instructions;
593 (void) state;
594
595 return NULL;
596}
597
598
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700599ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -0800600ast_expression::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -0800601 struct _mesa_glsl_parse_state *state)
602{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700603 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -0800604 static const int operations[AST_NUM_OPERATORS] = {
605 -1, /* ast_assign doesn't convert to ir_expression. */
606 -1, /* ast_plus doesn't convert to ir_expression. */
607 ir_unop_neg,
608 ir_binop_add,
609 ir_binop_sub,
610 ir_binop_mul,
611 ir_binop_div,
612 ir_binop_mod,
613 ir_binop_lshift,
614 ir_binop_rshift,
615 ir_binop_less,
616 ir_binop_greater,
617 ir_binop_lequal,
618 ir_binop_gequal,
619 ir_binop_equal,
620 ir_binop_nequal,
621 ir_binop_bit_and,
622 ir_binop_bit_xor,
623 ir_binop_bit_or,
624 ir_unop_bit_not,
625 ir_binop_logic_and,
626 ir_binop_logic_xor,
627 ir_binop_logic_or,
628 ir_unop_logic_not,
629
630 /* Note: The following block of expression types actually convert
631 * to multiple IR instructions.
632 */
633 ir_binop_mul, /* ast_mul_assign */
634 ir_binop_div, /* ast_div_assign */
635 ir_binop_mod, /* ast_mod_assign */
636 ir_binop_add, /* ast_add_assign */
637 ir_binop_sub, /* ast_sub_assign */
638 ir_binop_lshift, /* ast_ls_assign */
639 ir_binop_rshift, /* ast_rs_assign */
640 ir_binop_bit_and, /* ast_and_assign */
641 ir_binop_bit_xor, /* ast_xor_assign */
642 ir_binop_bit_or, /* ast_or_assign */
643
644 -1, /* ast_conditional doesn't convert to ir_expression. */
Eric Anholtde38f0e2010-03-26 12:14:54 -0700645 ir_binop_add, /* ast_pre_inc. */
646 ir_binop_sub, /* ast_pre_dec. */
647 ir_binop_add, /* ast_post_inc. */
648 ir_binop_sub, /* ast_post_dec. */
Ian Romanicka87ac252010-02-22 13:19:34 -0800649 -1, /* ast_field_selection doesn't conv to ir_expression. */
650 -1, /* ast_array_index doesn't convert to ir_expression. */
651 -1, /* ast_function_call doesn't conv to ir_expression. */
652 -1, /* ast_identifier doesn't convert to ir_expression. */
653 -1, /* ast_int_constant doesn't convert to ir_expression. */
654 -1, /* ast_uint_constant doesn't conv to ir_expression. */
655 -1, /* ast_float_constant doesn't conv to ir_expression. */
656 -1, /* ast_bool_constant doesn't conv to ir_expression. */
657 -1, /* ast_sequence doesn't convert to ir_expression. */
658 };
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700659 ir_rvalue *result = NULL;
Aras Pranckevicius1c325af2010-07-29 15:35:22 +0300660 ir_rvalue *op[3];
Ian Romanick0471e8b2010-03-26 14:33:41 -0700661 const struct glsl_type *type = glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800662 bool error_emitted = false;
663 YYLTYPE loc;
664
Ian Romanick18238de2010-03-01 13:49:10 -0800665 loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -0800666
Ian Romanick18238de2010-03-01 13:49:10 -0800667 switch (this->oper) {
Ian Romanick6652af32010-03-09 16:38:02 -0800668 case ast_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -0800669 op[0] = this->subexpressions[0]->hir(instructions, state);
670 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800671
Eric Anholt10a68522010-03-26 11:53:37 -0700672 result = do_assignment(instructions, state, op[0], op[1],
673 this->subexpressions[0]->get_location());
674 error_emitted = result->type->is_error();
675 type = result->type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800676 break;
Ian Romanick6652af32010-03-09 16:38:02 -0800677 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800678
679 case ast_plus:
Ian Romanick18238de2010-03-01 13:49:10 -0800680 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800681
Carl Worthc24bcad2010-07-21 11:23:51 -0700682 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
683
684 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800685
686 result = op[0];
687 break;
688
689 case ast_neg:
Ian Romanick18238de2010-03-01 13:49:10 -0800690 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800691
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000692 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800693
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000694 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800695
Carl Worth1660a292010-06-23 18:11:51 -0700696 result = new(ctx) ir_expression(operations[this->oper], type,
697 op[0], NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -0800698 break;
699
700 case ast_add:
701 case ast_sub:
702 case ast_mul:
703 case ast_div:
Ian Romanick18238de2010-03-01 13:49:10 -0800704 op[0] = this->subexpressions[0]->hir(instructions, state);
705 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800706
Ian Romanickbfb09c22010-03-29 16:32:55 -0700707 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -0800708 (this->oper == ast_mul),
Eric Anholta13bb142010-03-31 16:38:11 -1000709 state, & loc);
710 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800711
Carl Worth1660a292010-06-23 18:11:51 -0700712 result = new(ctx) ir_expression(operations[this->oper], type,
713 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -0800714 break;
715
716 case ast_mod:
Ian Romanick18238de2010-03-01 13:49:10 -0800717 op[0] = this->subexpressions[0]->hir(instructions, state);
718 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800719
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000720 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800721
Ian Romanick18238de2010-03-01 13:49:10 -0800722 assert(operations[this->oper] == ir_binop_mod);
Ian Romanicka87ac252010-02-22 13:19:34 -0800723
Carl Worth1660a292010-06-23 18:11:51 -0700724 result = new(ctx) ir_expression(operations[this->oper], type,
725 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000726 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800727 break;
728
729 case ast_lshift:
730 case ast_rshift:
Eric Anholt183d8c62010-03-31 16:53:47 -1000731 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
732 error_emitted = true;
Ian Romanicka87ac252010-02-22 13:19:34 -0800733 break;
734
735 case ast_less:
736 case ast_greater:
737 case ast_lequal:
738 case ast_gequal:
Ian Romanick18238de2010-03-01 13:49:10 -0800739 op[0] = this->subexpressions[0]->hir(instructions, state);
740 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800741
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000742 type = relational_result_type(op[0], op[1], state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800743
744 /* The relational operators must either generate an error or result
745 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
746 */
Ian Romanicka43817a2010-03-26 14:27:23 -0700747 assert(type->is_error()
Ian Romanicka87ac252010-02-22 13:19:34 -0800748 || ((type->base_type == GLSL_TYPE_BOOL)
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800749 && type->is_scalar()));
Ian Romanicka87ac252010-02-22 13:19:34 -0800750
Carl Worth1660a292010-06-23 18:11:51 -0700751 result = new(ctx) ir_expression(operations[this->oper], type,
752 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000753 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800754 break;
755
756 case ast_nequal:
757 case ast_equal:
Ian Romanick6e659ca2010-03-29 15:11:05 -0700758 op[0] = this->subexpressions[0]->hir(instructions, state);
759 op[1] = this->subexpressions[1]->hir(instructions, state);
760
761 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
762 *
763 * "The equality operators equal (==), and not equal (!=)
764 * operate on all types. They result in a scalar Boolean. If
765 * the operand types do not match, then there must be a
766 * conversion from Section 4.1.10 "Implicit Conversions"
767 * applied to one operand that can make them match, in which
768 * case this conversion is done."
769 */
Ian Romanickbfb09c22010-03-29 16:32:55 -0700770 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
771 && !apply_implicit_conversion(op[1]->type, op[0], state))
Ian Romanick212b0322010-03-29 16:22:38 -0700772 || (op[0]->type != op[1]->type)) {
Ian Romanick6e659ca2010-03-29 15:11:05 -0700773 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
774 "type", (this->oper == ast_equal) ? "==" : "!=");
775 error_emitted = true;
Ian Romanicka80cbd62010-03-30 17:04:48 -0700776 } else if ((state->language_version <= 110)
777 && (op[0]->type->is_array() || op[1]->type->is_array())) {
778 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
779 "GLSL 1.10");
780 error_emitted = true;
Ian Romanick6e659ca2010-03-29 15:11:05 -0700781 }
782
Carl Worth1660a292010-06-23 18:11:51 -0700783 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
784 op[0], op[1]);
Ian Romanick6e659ca2010-03-29 15:11:05 -0700785 type = glsl_type::bool_type;
786
787 assert(result->type == glsl_type::bool_type);
Ian Romanicka87ac252010-02-22 13:19:34 -0800788 break;
789
790 case ast_bit_and:
791 case ast_bit_xor:
792 case ast_bit_or:
793 case ast_bit_not:
Eric Anholt183d8c62010-03-31 16:53:47 -1000794 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
795 error_emitted = true;
Ian Romanicka87ac252010-02-22 13:19:34 -0800796 break;
797
Eric Anholt4950a682010-04-16 01:10:32 -0700798 case ast_logic_and: {
Eric Anholtb82c0c32010-03-31 09:11:39 -1000799 op[0] = this->subexpressions[0]->hir(instructions, state);
Eric Anholtb82c0c32010-03-31 09:11:39 -1000800
801 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
802 YYLTYPE loc = this->subexpressions[0]->get_location();
803
804 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
805 operator_string(this->oper));
Eric Anholtebbf14b2010-03-31 17:05:32 -1000806 error_emitted = true;
Eric Anholtb82c0c32010-03-31 09:11:39 -1000807 }
808
Eric Anholt44b694e2010-04-16 13:49:04 -0700809 ir_constant *op0_const = op[0]->constant_expression_value();
810 if (op0_const) {
811 if (op0_const->value.b[0]) {
812 op[1] = this->subexpressions[1]->hir(instructions, state);
Eric Anholt4950a682010-04-16 01:10:32 -0700813
Eric Anholt44b694e2010-04-16 13:49:04 -0700814 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
815 YYLTYPE loc = this->subexpressions[1]->get_location();
Eric Anholt4950a682010-04-16 01:10:32 -0700816
Eric Anholt44b694e2010-04-16 13:49:04 -0700817 _mesa_glsl_error(& loc, state,
818 "RHS of `%s' must be scalar boolean",
819 operator_string(this->oper));
820 error_emitted = true;
821 }
822 result = op[1];
823 } else {
824 result = op0_const;
825 }
826 type = glsl_type::bool_type;
827 } else {
Ian Romanick81d664f2010-07-12 15:18:55 -0700828 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700829 "and_tmp",
830 ir_var_temporary);
Ian Romanick81d664f2010-07-12 15:18:55 -0700831 instructions->push_tail(tmp);
832
Carl Worth1660a292010-06-23 18:11:51 -0700833 ir_if *const stmt = new(ctx) ir_if(op[0]);
Eric Anholt44b694e2010-04-16 13:49:04 -0700834 instructions->push_tail(stmt);
Eric Anholt4950a682010-04-16 01:10:32 -0700835
Eric Anholt44b694e2010-04-16 13:49:04 -0700836 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
Eric Anholtb82c0c32010-03-31 09:11:39 -1000837
Eric Anholt44b694e2010-04-16 13:49:04 -0700838 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
839 YYLTYPE loc = this->subexpressions[1]->get_location();
840
841 _mesa_glsl_error(& loc, state,
842 "RHS of `%s' must be scalar boolean",
843 operator_string(this->oper));
844 error_emitted = true;
845 }
846
Carl Worth1660a292010-06-23 18:11:51 -0700847 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -0700848 ir_assignment *const then_assign =
Carl Worth1660a292010-06-23 18:11:51 -0700849 new(ctx) ir_assignment(then_deref, op[1], NULL);
Eric Anholt44b694e2010-04-16 13:49:04 -0700850 stmt->then_instructions.push_tail(then_assign);
851
Carl Worth1660a292010-06-23 18:11:51 -0700852 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -0700853 ir_assignment *const else_assign =
Carl Worth1660a292010-06-23 18:11:51 -0700854 new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
Eric Anholt44b694e2010-04-16 13:49:04 -0700855 stmt->else_instructions.push_tail(else_assign);
856
Carl Worth1660a292010-06-23 18:11:51 -0700857 result = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -0700858 type = tmp->type;
Eric Anholtb82c0c32010-03-31 09:11:39 -1000859 }
Eric Anholt4950a682010-04-16 01:10:32 -0700860 break;
861 }
862
863 case ast_logic_or: {
864 op[0] = this->subexpressions[0]->hir(instructions, state);
865
866 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
867 YYLTYPE loc = this->subexpressions[0]->get_location();
868
869 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
870 operator_string(this->oper));
871 error_emitted = true;
872 }
873
Eric Anholt44b694e2010-04-16 13:49:04 -0700874 ir_constant *op0_const = op[0]->constant_expression_value();
875 if (op0_const) {
876 if (op0_const->value.b[0]) {
877 result = op0_const;
878 } else {
879 op[1] = this->subexpressions[1]->hir(instructions, state);
Eric Anholt4950a682010-04-16 01:10:32 -0700880
Eric Anholt44b694e2010-04-16 13:49:04 -0700881 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
882 YYLTYPE loc = this->subexpressions[1]->get_location();
Eric Anholt4950a682010-04-16 01:10:32 -0700883
Eric Anholt44b694e2010-04-16 13:49:04 -0700884 _mesa_glsl_error(& loc, state,
885 "RHS of `%s' must be scalar boolean",
886 operator_string(this->oper));
887 error_emitted = true;
888 }
889 result = op[1];
890 }
891 type = glsl_type::bool_type;
892 } else {
Kenneth Graunkedfd30ca2010-07-08 12:40:52 -0700893 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700894 "or_tmp",
895 ir_var_temporary);
Ian Romanick0b9ae3b2010-07-12 14:22:05 -0700896 instructions->push_tail(tmp);
Eric Anholt4950a682010-04-16 01:10:32 -0700897
Ian Romanick81d664f2010-07-12 15:18:55 -0700898 ir_if *const stmt = new(ctx) ir_if(op[0]);
899 instructions->push_tail(stmt);
900
Eric Anholta0879b92010-07-22 16:30:41 -0700901 op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state);
Eric Anholt44b694e2010-04-16 13:49:04 -0700902
903 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
904 YYLTYPE loc = this->subexpressions[1]->get_location();
905
906 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
907 operator_string(this->oper));
908 error_emitted = true;
909 }
910
Carl Worth1660a292010-06-23 18:11:51 -0700911 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -0700912 ir_assignment *const then_assign =
Carl Worth1660a292010-06-23 18:11:51 -0700913 new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
Eric Anholt44b694e2010-04-16 13:49:04 -0700914 stmt->then_instructions.push_tail(then_assign);
915
Carl Worth1660a292010-06-23 18:11:51 -0700916 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -0700917 ir_assignment *const else_assign =
Carl Worth1660a292010-06-23 18:11:51 -0700918 new(ctx) ir_assignment(else_deref, op[1], NULL);
Eric Anholt44b694e2010-04-16 13:49:04 -0700919 stmt->else_instructions.push_tail(else_assign);
920
Carl Worth1660a292010-06-23 18:11:51 -0700921 result = new(ctx) ir_dereference_variable(tmp);
Eric Anholt44b694e2010-04-16 13:49:04 -0700922 type = tmp->type;
Eric Anholt4950a682010-04-16 01:10:32 -0700923 }
Eric Anholt4950a682010-04-16 01:10:32 -0700924 break;
925 }
926
927 case ast_logic_xor:
928 op[0] = this->subexpressions[0]->hir(instructions, state);
929 op[1] = this->subexpressions[1]->hir(instructions, state);
930
931
Carl Worth1660a292010-06-23 18:11:51 -0700932 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
933 op[0], op[1]);
Eric Anholtebbf14b2010-03-31 17:05:32 -1000934 type = glsl_type::bool_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800935 break;
936
Eric Anholta5827fe2010-03-31 16:50:55 -1000937 case ast_logic_not:
938 op[0] = this->subexpressions[0]->hir(instructions, state);
939
940 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
941 YYLTYPE loc = this->subexpressions[0]->get_location();
942
943 _mesa_glsl_error(& loc, state,
944 "operand of `!' must be scalar boolean");
Eric Anholtebbf14b2010-03-31 17:05:32 -1000945 error_emitted = true;
Eric Anholta5827fe2010-03-31 16:50:55 -1000946 }
947
Carl Worth1660a292010-06-23 18:11:51 -0700948 result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
949 op[0], NULL);
Eric Anholtebbf14b2010-03-31 17:05:32 -1000950 type = glsl_type::bool_type;
Eric Anholta5827fe2010-03-31 16:50:55 -1000951 break;
952
Ian Romanicka87ac252010-02-22 13:19:34 -0800953 case ast_mul_assign:
954 case ast_div_assign:
955 case ast_add_assign:
956 case ast_sub_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -0800957 op[0] = this->subexpressions[0]->hir(instructions, state);
958 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800959
Ian Romanickbfb09c22010-03-29 16:32:55 -0700960 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -0800961 (this->oper == ast_mul_assign),
Eric Anholta13bb142010-03-31 16:38:11 -1000962 state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800963
Carl Worth1660a292010-06-23 18:11:51 -0700964 ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
965 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -0800966
Eric Anholt3e24ef62010-06-23 12:40:17 -0700967 result = do_assignment(instructions, state,
Eric Anholt8273bd42010-08-04 12:34:56 -0700968 op[0]->clone(ctx, NULL), temp_rhs,
Eric Anholt10a68522010-03-26 11:53:37 -0700969 this->subexpressions[0]->get_location());
970 type = result->type;
971 error_emitted = (op[0]->type->is_error());
Ian Romanicka87ac252010-02-22 13:19:34 -0800972
973 /* GLSL 1.10 does not allow array assignment. However, we don't have to
974 * explicitly test for this because none of the binary expression
975 * operators allow array operands either.
976 */
977
Ian Romanicka87ac252010-02-22 13:19:34 -0800978 break;
979 }
980
Eric Anholt48a0e642010-03-26 11:57:46 -0700981 case ast_mod_assign: {
982 op[0] = this->subexpressions[0]->hir(instructions, state);
983 op[1] = this->subexpressions[1]->hir(instructions, state);
984
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000985 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Eric Anholt48a0e642010-03-26 11:57:46 -0700986
987 assert(operations[this->oper] == ir_binop_mod);
988
989 struct ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -0700990 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
991 op[0], op[1]);
Eric Anholt48a0e642010-03-26 11:57:46 -0700992
Eric Anholt3e24ef62010-06-23 12:40:17 -0700993 result = do_assignment(instructions, state,
Eric Anholt8273bd42010-08-04 12:34:56 -0700994 op[0]->clone(ctx, NULL), temp_rhs,
Eric Anholt48a0e642010-03-26 11:57:46 -0700995 this->subexpressions[0]->get_location());
996 type = result->type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000997 error_emitted = type->is_error();
Eric Anholt48a0e642010-03-26 11:57:46 -0700998 break;
999 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001000
1001 case ast_ls_assign:
1002 case ast_rs_assign:
Eric Anholt183d8c62010-03-31 16:53:47 -10001003 _mesa_glsl_error(& loc, state,
1004 "FINISHME: implement bit-shift assignment operators");
1005 error_emitted = true;
Ian Romanick251eb752010-03-29 14:15:05 -07001006 break;
Ian Romanicka87ac252010-02-22 13:19:34 -08001007
1008 case ast_and_assign:
1009 case ast_xor_assign:
1010 case ast_or_assign:
Eric Anholt183d8c62010-03-31 16:53:47 -10001011 _mesa_glsl_error(& loc, state,
1012 "FINISHME: implement logic assignment operators");
1013 error_emitted = true;
Ian Romanick251eb752010-03-29 14:15:05 -07001014 break;
Ian Romanicka87ac252010-02-22 13:19:34 -08001015
Ian Romanick96f9cea2010-03-29 15:33:54 -07001016 case ast_conditional: {
1017 op[0] = this->subexpressions[0]->hir(instructions, state);
1018
1019 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1020 *
1021 * "The ternary selection operator (?:). It operates on three
1022 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
1023 * first expression, which must result in a scalar Boolean."
1024 */
1025 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1026 YYLTYPE loc = this->subexpressions[0]->get_location();
1027
1028 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
1029 error_emitted = true;
1030 }
1031
1032 /* The :? operator is implemented by generating an anonymous temporary
1033 * followed by an if-statement. The last instruction in each branch of
1034 * the if-statement assigns a value to the anonymous temporary. This
1035 * temporary is the r-value of the expression.
1036 */
Ian Romanick0ad76c62010-06-11 12:56:26 -07001037 exec_list then_instructions;
1038 exec_list else_instructions;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001039
Ian Romanick0ad76c62010-06-11 12:56:26 -07001040 op[1] = this->subexpressions[1]->hir(&then_instructions, state);
1041 op[2] = this->subexpressions[2]->hir(&else_instructions, state);
Ian Romanick96f9cea2010-03-29 15:33:54 -07001042
1043 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1044 *
1045 * "The second and third expressions can be any type, as
1046 * long their types match, or there is a conversion in
1047 * Section 4.1.10 "Implicit Conversions" that can be applied
1048 * to one of the expressions to make their types match. This
1049 * resulting matching type is the type of the entire
1050 * expression."
1051 */
Ian Romanickbfb09c22010-03-29 16:32:55 -07001052 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1053 && !apply_implicit_conversion(op[2]->type, op[1], state))
Ian Romanickdb9be2e2010-03-29 16:25:56 -07001054 || (op[1]->type != op[2]->type)) {
Ian Romanick96f9cea2010-03-29 15:33:54 -07001055 YYLTYPE loc = this->subexpressions[1]->get_location();
1056
1057 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1058 "operator must have matching types.");
1059 error_emitted = true;
Ian Romanick0ad76c62010-06-11 12:56:26 -07001060 type = glsl_type::error_type;
Ian Romanickdb9be2e2010-03-29 16:25:56 -07001061 } else {
Ian Romanick0ad76c62010-06-11 12:56:26 -07001062 type = op[1]->type;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001063 }
1064
Ian Romanick7825d3d2010-06-11 13:45:51 -07001065 ir_constant *cond_val = op[0]->constant_expression_value();
1066 ir_constant *then_val = op[1]->constant_expression_value();
1067 ir_constant *else_val = op[2]->constant_expression_value();
Ian Romanick0ad76c62010-06-11 12:56:26 -07001068
Ian Romanick7825d3d2010-06-11 13:45:51 -07001069 if (then_instructions.is_empty()
1070 && else_instructions.is_empty()
1071 && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
1072 result = (cond_val->value.b[0]) ? then_val : else_val;
1073 } else {
Ian Romanick7e2aa912010-07-19 17:12:42 -07001074 ir_variable *const tmp =
1075 new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
Ian Romanick0b9ae3b2010-07-12 14:22:05 -07001076 instructions->push_tail(tmp);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001077
Carl Worth1660a292010-06-23 18:11:51 -07001078 ir_if *const stmt = new(ctx) ir_if(op[0]);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001079 instructions->push_tail(stmt);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001080
Ian Romanick7825d3d2010-06-11 13:45:51 -07001081 then_instructions.move_nodes_to(& stmt->then_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001082 ir_dereference *const then_deref =
1083 new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001084 ir_assignment *const then_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001085 new(ctx) ir_assignment(then_deref, op[1], NULL);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001086 stmt->then_instructions.push_tail(then_assign);
Ian Romanick0ad76c62010-06-11 12:56:26 -07001087
Ian Romanick7825d3d2010-06-11 13:45:51 -07001088 else_instructions.move_nodes_to(& stmt->else_instructions);
Carl Worth1660a292010-06-23 18:11:51 -07001089 ir_dereference *const else_deref =
1090 new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001091 ir_assignment *const else_assign =
Carl Worth1660a292010-06-23 18:11:51 -07001092 new(ctx) ir_assignment(else_deref, op[2], NULL);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001093 stmt->else_instructions.push_tail(else_assign);
1094
Carl Worth1660a292010-06-23 18:11:51 -07001095 result = new(ctx) ir_dereference_variable(tmp);
Ian Romanick7825d3d2010-06-11 13:45:51 -07001096 }
Ian Romanick251eb752010-03-29 14:15:05 -07001097 break;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001098 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001099
1100 case ast_pre_inc:
Eric Anholt76ea56c2010-03-26 12:16:54 -07001101 case ast_pre_dec: {
1102 op[0] = this->subexpressions[0]->hir(instructions, state);
1103 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
Carl Worth1660a292010-06-23 18:11:51 -07001104 op[1] = new(ctx) ir_constant(1.0f);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001105 else
Carl Worth1660a292010-06-23 18:11:51 -07001106 op[1] = new(ctx) ir_constant(1);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001107
Eric Anholta13bb142010-03-31 16:38:11 -10001108 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001109
1110 struct ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001111 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1112 op[0], op[1]);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001113
Eric Anholt3e24ef62010-06-23 12:40:17 -07001114 result = do_assignment(instructions, state,
Eric Anholt8273bd42010-08-04 12:34:56 -07001115 op[0]->clone(ctx, NULL), temp_rhs,
Eric Anholt76ea56c2010-03-26 12:16:54 -07001116 this->subexpressions[0]->get_location());
1117 type = result->type;
1118 error_emitted = op[0]->type->is_error();
1119 break;
1120 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001121
1122 case ast_post_inc:
Eric Anholtde38f0e2010-03-26 12:14:54 -07001123 case ast_post_dec: {
1124 op[0] = this->subexpressions[0]->hir(instructions, state);
1125 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
Carl Worth1660a292010-06-23 18:11:51 -07001126 op[1] = new(ctx) ir_constant(1.0f);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001127 else
Carl Worth1660a292010-06-23 18:11:51 -07001128 op[1] = new(ctx) ir_constant(1);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001129
1130 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1131
Eric Anholta13bb142010-03-31 16:38:11 -10001132 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001133
1134 struct ir_rvalue *temp_rhs;
Carl Worth1660a292010-06-23 18:11:51 -07001135 temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
1136 op[0], op[1]);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001137
1138 /* Get a temporary of a copy of the lvalue before it's modified.
1139 * This may get thrown away later.
1140 */
Eric Anholt8273bd42010-08-04 12:34:56 -07001141 result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
Eric Anholtde38f0e2010-03-26 12:14:54 -07001142
Eric Anholt3e24ef62010-06-23 12:40:17 -07001143 (void)do_assignment(instructions, state,
Eric Anholt8273bd42010-08-04 12:34:56 -07001144 op[0]->clone(ctx, NULL), temp_rhs,
Eric Anholtde38f0e2010-03-26 12:14:54 -07001145 this->subexpressions[0]->get_location());
1146
1147 type = result->type;
1148 error_emitted = op[0]->type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001149 break;
Eric Anholtde38f0e2010-03-26 12:14:54 -07001150 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001151
1152 case ast_field_selection:
Ian Romanick18238de2010-03-01 13:49:10 -08001153 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001154 type = result->type;
1155 break;
1156
Ian Romanick27e3cf82010-04-01 18:03:59 -07001157 case ast_array_index: {
1158 YYLTYPE index_loc = subexpressions[1]->get_location();
1159
1160 op[0] = subexpressions[0]->hir(instructions, state);
1161 op[1] = subexpressions[1]->hir(instructions, state);
1162
1163 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1164
Ian Romanicka9159f92010-05-26 15:08:11 -07001165 ir_rvalue *const array = op[0];
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001166
Carl Worth1660a292010-06-23 18:11:51 -07001167 result = new(ctx) ir_dereference_array(op[0], op[1]);
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001168
1169 /* Do not use op[0] after this point. Use array.
1170 */
1171 op[0] = NULL;
1172
Ian Romanick27e3cf82010-04-01 18:03:59 -07001173
1174 if (error_emitted)
1175 break;
1176
Ian Romanick63038e12010-04-05 13:16:00 -07001177 if (!array->type->is_array()
1178 && !array->type->is_matrix()
1179 && !array->type->is_vector()) {
Ian Romanick27e3cf82010-04-01 18:03:59 -07001180 _mesa_glsl_error(& index_loc, state,
Ian Romanick63038e12010-04-05 13:16:00 -07001181 "cannot dereference non-array / non-matrix / "
1182 "non-vector");
Ian Romanick27e3cf82010-04-01 18:03:59 -07001183 error_emitted = true;
1184 }
1185
1186 if (!op[1]->type->is_integer()) {
1187 _mesa_glsl_error(& index_loc, state,
1188 "array index must be integer type");
1189 error_emitted = true;
1190 } else if (!op[1]->type->is_scalar()) {
1191 _mesa_glsl_error(& index_loc, state,
1192 "array index must be scalar");
1193 error_emitted = true;
1194 }
1195
1196 /* If the array index is a constant expression and the array has a
1197 * declared size, ensure that the access is in-bounds. If the array
1198 * index is not a constant expression, ensure that the array has a
1199 * declared size.
1200 */
1201 ir_constant *const const_index = op[1]->constant_expression_value();
1202 if (const_index != NULL) {
1203 const int idx = const_index->value.i[0];
Ian Romanick63038e12010-04-05 13:16:00 -07001204 const char *type_name;
1205 unsigned bound = 0;
1206
1207 if (array->type->is_matrix()) {
1208 type_name = "matrix";
1209 } else if (array->type->is_vector()) {
1210 type_name = "vector";
1211 } else {
1212 type_name = "array";
1213 }
Ian Romanick27e3cf82010-04-01 18:03:59 -07001214
1215 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1216 *
1217 * "It is illegal to declare an array with a size, and then
1218 * later (in the same shader) index the same array with an
1219 * integral constant expression greater than or equal to the
1220 * declared size. It is also illegal to index an array with a
1221 * negative constant expression."
1222 */
Ian Romanick63038e12010-04-05 13:16:00 -07001223 if (array->type->is_matrix()) {
1224 if (array->type->row_type()->vector_elements <= idx) {
1225 bound = array->type->row_type()->vector_elements;
1226 }
1227 } else if (array->type->is_vector()) {
1228 if (array->type->vector_elements <= idx) {
1229 bound = array->type->vector_elements;
1230 }
1231 } else {
1232 if ((array->type->array_size() > 0)
1233 && (array->type->array_size() <= idx)) {
1234 bound = array->type->array_size();
1235 }
1236 }
1237
1238 if (bound > 0) {
1239 _mesa_glsl_error(& loc, state, "%s index must be < %u",
1240 type_name, bound);
1241 error_emitted = true;
1242 } else if (idx < 0) {
1243 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
1244 type_name);
Ian Romanick27e3cf82010-04-01 18:03:59 -07001245 error_emitted = true;
1246 }
1247
Ian Romanick63038e12010-04-05 13:16:00 -07001248 if (array->type->is_array()) {
Ian Romanicka9159f92010-05-26 15:08:11 -07001249 /* If the array is a variable dereference, it dereferences the
1250 * whole array, by definition. Use this to get the variable.
1251 *
1252 * FINISHME: Should some methods for getting / setting / testing
1253 * FINISHME: array access limits be added to ir_dereference?
1254 */
1255 ir_variable *const v = array->whole_variable_referenced();
Ian Romanick63038e12010-04-05 13:16:00 -07001256 if ((v != NULL) && (unsigned(idx) > v->max_array_access))
1257 v->max_array_access = idx;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001258 }
Kenneth Graunke2b7c42b2010-07-16 18:28:44 -07001259 } else if (array->type->array_size() == 0) {
1260 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
Ian Romanick27e3cf82010-04-01 18:03:59 -07001261 }
1262
1263 if (error_emitted)
1264 result->type = glsl_type::error_type;
1265
1266 type = result->type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001267 break;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001268 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001269
1270 case ast_function_call:
Ian Romanick7cfddf12010-03-10 13:26:52 -08001271 /* Should *NEVER* get here. ast_function_call should always be handled
1272 * by ast_function_expression::hir.
Ian Romanicka87ac252010-02-22 13:19:34 -08001273 */
Ian Romanick7cfddf12010-03-10 13:26:52 -08001274 assert(0);
Ian Romanicka87ac252010-02-22 13:19:34 -08001275 break;
1276
1277 case ast_identifier: {
1278 /* ast_identifier can appear several places in a full abstract syntax
1279 * tree. This particular use must be at location specified in the grammar
1280 * as 'variable_identifier'.
1281 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001282 ir_variable *var =
1283 state->symbols->get_variable(this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001284
Carl Worth1660a292010-06-23 18:11:51 -07001285 result = new(ctx) ir_dereference_variable(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08001286
1287 if (var != NULL) {
1288 type = result->type;
1289 } else {
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001290 _mesa_glsl_error(& loc, state, "`%s' undeclared",
Ian Romanick18238de2010-03-01 13:49:10 -08001291 this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001292
1293 error_emitted = true;
1294 }
1295 break;
1296 }
1297
1298 case ast_int_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001299 type = glsl_type::int_type;
Carl Worth1660a292010-06-23 18:11:51 -07001300 result = new(ctx) ir_constant(this->primary_expression.int_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001301 break;
1302
1303 case ast_uint_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001304 type = glsl_type::uint_type;
Carl Worth1660a292010-06-23 18:11:51 -07001305 result = new(ctx) ir_constant(this->primary_expression.uint_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001306 break;
1307
1308 case ast_float_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001309 type = glsl_type::float_type;
Carl Worth1660a292010-06-23 18:11:51 -07001310 result = new(ctx) ir_constant(this->primary_expression.float_constant);
Ian Romanicka87ac252010-02-22 13:19:34 -08001311 break;
1312
1313 case ast_bool_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001314 type = glsl_type::bool_type;
Carl Worth1660a292010-06-23 18:11:51 -07001315 result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
Ian Romanicka87ac252010-02-22 13:19:34 -08001316 break;
1317
1318 case ast_sequence: {
Ian Romanicka87ac252010-02-22 13:19:34 -08001319 /* It should not be possible to generate a sequence in the AST without
1320 * any expressions in it.
1321 */
Ian Romanick304ea902010-05-10 11:17:53 -07001322 assert(!this->expressions.is_empty());
Ian Romanicka87ac252010-02-22 13:19:34 -08001323
1324 /* The r-value of a sequence is the last expression in the sequence. If
1325 * the other expressions in the sequence do not have side-effects (and
1326 * therefore add instructions to the instruction list), they get dropped
1327 * on the floor.
1328 */
Ian Romanick2b97dc62010-05-10 17:42:05 -07001329 foreach_list_typed (ast_node, ast, link, &this->expressions)
Ian Romanick304ea902010-05-10 11:17:53 -07001330 result = ast->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001331
1332 type = result->type;
1333
1334 /* Any errors should have already been emitted in the loop above.
1335 */
1336 error_emitted = true;
1337 break;
1338 }
1339 }
1340
Ian Romanickcef3bae2010-03-26 14:41:32 -07001341 if (type->is_error() && !error_emitted)
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001342 _mesa_glsl_error(& loc, state, "type mismatch");
Ian Romanicka87ac252010-02-22 13:19:34 -08001343
1344 return result;
1345}
1346
1347
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001348ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001349ast_expression_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001350 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001351{
Ian Romanicka87ac252010-02-22 13:19:34 -08001352 /* It is possible to have expression statements that don't have an
1353 * expression. This is the solitary semicolon:
1354 *
1355 * for (i = 0; i < 5; i++)
1356 * ;
1357 *
1358 * In this case the expression will be NULL. Test for NULL and don't do
1359 * anything in that case.
1360 */
Ian Romanick18238de2010-03-01 13:49:10 -08001361 if (expression != NULL)
1362 expression->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001363
1364 /* Statements do not have r-values.
1365 */
1366 return NULL;
1367}
1368
1369
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001370ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001371ast_compound_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001372 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001373{
Ian Romanick18238de2010-03-01 13:49:10 -08001374 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001375 state->symbols->push_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001376
Ian Romanick2b97dc62010-05-10 17:42:05 -07001377 foreach_list_typed (ast_node, ast, link, &this->statements)
Ian Romanick304ea902010-05-10 11:17:53 -07001378 ast->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001379
Ian Romanick18238de2010-03-01 13:49:10 -08001380 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001381 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001382
1383 /* Compound statements do not have r-values.
1384 */
1385 return NULL;
1386}
1387
1388
Ian Romanick28009cd2010-03-30 16:59:27 -07001389static const glsl_type *
1390process_array_type(const glsl_type *base, ast_node *array_size,
1391 struct _mesa_glsl_parse_state *state)
1392{
1393 unsigned length = 0;
1394
1395 /* FINISHME: Reject delcarations of multidimensional arrays. */
1396
1397 if (array_size != NULL) {
1398 exec_list dummy_instructions;
1399 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1400 YYLTYPE loc = array_size->get_location();
1401
1402 /* FINISHME: Verify that the grammar forbids side-effects in array
1403 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1404 */
1405 assert(dummy_instructions.is_empty());
1406
1407 if (ir != NULL) {
1408 if (!ir->type->is_integer()) {
1409 _mesa_glsl_error(& loc, state, "array size must be integer type");
1410 } else if (!ir->type->is_scalar()) {
1411 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1412 } else {
1413 ir_constant *const size = ir->constant_expression_value();
1414
1415 if (size == NULL) {
1416 _mesa_glsl_error(& loc, state, "array size must be a "
1417 "constant valued expression");
1418 } else if (size->value.i[0] <= 0) {
1419 _mesa_glsl_error(& loc, state, "array size must be > 0");
1420 } else {
1421 assert(size->type == ir->type);
1422 length = size->value.u[0];
1423 }
1424 }
1425 }
1426 }
1427
Ian Romanickf38d15b2010-07-20 15:33:40 -07001428 return glsl_type::get_array_instance(base, length);
Ian Romanick28009cd2010-03-30 16:59:27 -07001429}
1430
1431
Ian Romanickd612a122010-03-31 16:22:06 -07001432const glsl_type *
1433ast_type_specifier::glsl_type(const char **name,
1434 struct _mesa_glsl_parse_state *state) const
Ian Romanicka87ac252010-02-22 13:19:34 -08001435{
Ian Romanickd612a122010-03-31 16:22:06 -07001436 const struct glsl_type *type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001437
Ian Romanick3455ce62010-04-19 15:13:15 -07001438 if ((this->type_specifier == ast_struct) && (this->type_name == NULL)) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001439 /* FINISHME: Handle annonymous structures. */
1440 type = NULL;
1441 } else {
Ian Romanickd612a122010-03-31 16:22:06 -07001442 type = state->symbols->get_type(this->type_name);
1443 *name = this->type_name;
Ian Romanicka87ac252010-02-22 13:19:34 -08001444
Ian Romanickd612a122010-03-31 16:22:06 -07001445 if (this->is_array) {
1446 type = process_array_type(type, this->array_size, state);
Ian Romanick28009cd2010-03-30 16:59:27 -07001447 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001448 }
1449
1450 return type;
1451}
1452
1453
1454static void
1455apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1456 struct ir_variable *var,
Eric Anholt2e063f12010-03-28 00:56:22 -07001457 struct _mesa_glsl_parse_state *state,
1458 YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -08001459{
1460 if (qual->invariant)
1461 var->invariant = 1;
1462
1463 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1464 if (qual->constant || qual->attribute || qual->uniform
1465 || (qual->varying && (state->target == fragment_shader)))
1466 var->read_only = 1;
1467
1468 if (qual->centroid)
1469 var->centroid = 1;
1470
Ian Romanickae4c4c02010-04-07 16:41:40 -07001471 if (qual->attribute && state->target != vertex_shader) {
Eric Anholt2e063f12010-03-28 00:56:22 -07001472 var->type = glsl_type::error_type;
1473 _mesa_glsl_error(loc, state,
1474 "`attribute' variables may not be declared in the "
Ian Romanickae4c4c02010-04-07 16:41:40 -07001475 "%s shader",
1476 _mesa_glsl_shader_target_name(state->target));
Eric Anholt2e063f12010-03-28 00:56:22 -07001477 }
1478
Eric Anholt90b78252010-03-31 21:21:20 -10001479 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1480 *
1481 * "The varying qualifier can be used only with the data types
1482 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1483 * these."
1484 */
Eric Anholt0ca17192010-05-19 13:38:15 -07001485 if (qual->varying) {
1486 const glsl_type *non_array_type;
1487
1488 if (var->type && var->type->is_array())
1489 non_array_type = var->type->fields.array;
1490 else
1491 non_array_type = var->type;
1492
1493 if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
1494 var->type = glsl_type::error_type;
1495 _mesa_glsl_error(loc, state,
1496 "varying variables must be of base type float");
1497 }
Eric Anholt90b78252010-03-31 21:21:20 -10001498 }
1499
Ian Romanick7e2aa912010-07-19 17:12:42 -07001500 /* If there is no qualifier that changes the mode of the variable, leave
1501 * the setting alone.
1502 */
Ian Romanicka87ac252010-02-22 13:19:34 -08001503 if (qual->in && qual->out)
1504 var->mode = ir_var_inout;
1505 else if (qual->attribute || qual->in
1506 || (qual->varying && (state->target == fragment_shader)))
1507 var->mode = ir_var_in;
Ian Romanick0b678232010-03-10 00:28:59 -08001508 else if (qual->out || (qual->varying && (state->target == vertex_shader)))
Ian Romanicka87ac252010-02-22 13:19:34 -08001509 var->mode = ir_var_out;
1510 else if (qual->uniform)
1511 var->mode = ir_var_uniform;
Ian Romanicka87ac252010-02-22 13:19:34 -08001512
1513 if (qual->flat)
1514 var->interpolation = ir_var_flat;
1515 else if (qual->noperspective)
1516 var->interpolation = ir_var_noperspective;
1517 else
1518 var->interpolation = ir_var_smooth;
Ian Romanick9d975372010-04-02 17:17:47 -07001519
Eric Anholt4a962172010-07-28 14:41:51 -07001520 var->pixel_center_integer = qual->pixel_center_integer;
1521 var->origin_upper_left = qual->origin_upper_left;
Ian Romanick8d8469e2010-06-30 17:48:09 -07001522 if ((qual->origin_upper_left || qual->pixel_center_integer)
1523 && (strcmp(var->name, "gl_FragCoord") != 0)) {
1524 const char *const qual_string = (qual->origin_upper_left)
1525 ? "origin_upper_left" : "pixel_center_integer";
1526
1527 _mesa_glsl_error(loc, state,
1528 "layout qualifier `%s' can only be applied to "
1529 "fragment shader input `gl_FragCoord'",
1530 qual_string);
1531 }
1532
Ian Romanick9d975372010-04-02 17:17:47 -07001533 if (var->type->is_array() && (state->language_version >= 120)) {
1534 var->array_lvalue = true;
1535 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001536}
1537
1538
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001539ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001540ast_declarator_list::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001541 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001542{
Kenneth Graunke953ff122010-06-25 13:14:37 -07001543 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -08001544 const struct glsl_type *decl_type;
1545 const char *type_name = NULL;
Eric Anholt85584592010-04-14 15:38:52 -07001546 ir_rvalue *result = NULL;
Ian Romanickc824e352010-04-23 15:55:19 -07001547 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08001548
Ian Romanick6f0823d2010-07-01 20:39:08 -07001549 /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
1550 *
1551 * "To ensure that a particular output variable is invariant, it is
1552 * necessary to use the invariant qualifier. It can either be used to
1553 * qualify a previously declared variable as being invariant
1554 *
1555 * invariant gl_Position; // make existing gl_Position be invariant"
1556 *
1557 * In these cases the parser will set the 'invariant' flag in the declarator
1558 * list, and the type will be NULL.
1559 */
1560 if (this->invariant) {
1561 assert(this->type == NULL);
1562
1563 if (state->current_function != NULL) {
1564 _mesa_glsl_error(& loc, state,
1565 "All uses of `invariant' keyword must be at global "
1566 "scope\n");
1567 }
1568
1569 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1570 assert(!decl->is_array);
1571 assert(decl->array_size == NULL);
1572 assert(decl->initializer == NULL);
1573
1574 ir_variable *const earlier =
1575 state->symbols->get_variable(decl->identifier);
1576 if (earlier == NULL) {
1577 _mesa_glsl_error(& loc, state,
1578 "Undeclared variable `%s' cannot be marked "
1579 "invariant\n", decl->identifier);
1580 } else if ((state->target == vertex_shader)
1581 && (earlier->mode != ir_var_out)) {
1582 _mesa_glsl_error(& loc, state,
1583 "`%s' cannot be marked invariant, vertex shader "
1584 "outputs only\n", decl->identifier);
1585 } else if ((state->target == fragment_shader)
1586 && (earlier->mode != ir_var_in)) {
1587 _mesa_glsl_error(& loc, state,
1588 "`%s' cannot be marked invariant, fragment shader "
1589 "inputs only\n", decl->identifier);
1590 } else {
1591 earlier->invariant = true;
1592 }
1593 }
1594
1595 /* Invariant redeclarations do not have r-values.
1596 */
1597 return NULL;
1598 }
1599
1600 assert(this->type != NULL);
1601 assert(!this->invariant);
1602
Ian Romanick3455ce62010-04-19 15:13:15 -07001603 /* The type specifier may contain a structure definition. Process that
1604 * before any of the variable declarations.
1605 */
1606 (void) this->type->specifier->hir(instructions, state);
1607
Ian Romanickd612a122010-03-31 16:22:06 -07001608 decl_type = this->type->specifier->glsl_type(& type_name, state);
Ian Romanick304ea902010-05-10 11:17:53 -07001609 if (this->declarations.is_empty()) {
Ian Romanick6f0823d2010-07-01 20:39:08 -07001610 /* The only valid case where the declaration list can be empty is when
1611 * the declaration is setting the default precision of a built-in type
1612 * (e.g., 'precision highp vec4;').
Ian Romanickc824e352010-04-23 15:55:19 -07001613 */
1614
Ian Romanick6f0823d2010-07-01 20:39:08 -07001615 if (decl_type != NULL) {
Ian Romanickc824e352010-04-23 15:55:19 -07001616 } else {
1617 _mesa_glsl_error(& loc, state, "incomplete declaration");
1618 }
1619 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001620
Ian Romanick2b97dc62010-05-10 17:42:05 -07001621 foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001622 const struct glsl_type *var_type;
1623 struct ir_variable *var;
Ian Romanicka87ac252010-02-22 13:19:34 -08001624
1625 /* FINISHME: Emit a warning if a variable declaration shadows a
1626 * FINISHME: declaration at a higher scope.
1627 */
1628
Ian Romanickcec65a62010-03-23 12:28:44 -07001629 if ((decl_type == NULL) || decl_type->is_void()) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001630 if (type_name != NULL) {
1631 _mesa_glsl_error(& loc, state,
1632 "invalid type `%s' in declaration of `%s'",
1633 type_name, decl->identifier);
1634 } else {
1635 _mesa_glsl_error(& loc, state,
1636 "invalid type in declaration of `%s'",
1637 decl->identifier);
1638 }
1639 continue;
1640 }
1641
1642 if (decl->is_array) {
Ian Romanick28009cd2010-03-30 16:59:27 -07001643 var_type = process_array_type(decl_type, decl->array_size, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001644 } else {
1645 var_type = decl_type;
1646 }
1647
Ian Romanick7e2aa912010-07-19 17:12:42 -07001648 var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
Ian Romanicka87ac252010-02-22 13:19:34 -08001649
Eric Anholt3f151502010-04-02 01:53:57 -10001650 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1651 *
1652 * "Global variables can only use the qualifiers const,
1653 * attribute, uni form, or varying. Only one may be
1654 * specified.
1655 *
1656 * Local variables can only use the qualifier const."
1657 *
1658 * This is relaxed in GLSL 1.30.
1659 */
1660 if (state->language_version < 120) {
1661 if (this->type->qualifier.out) {
1662 _mesa_glsl_error(& loc, state,
1663 "`out' qualifier in declaration of `%s' "
1664 "only valid for function parameters in GLSL 1.10.",
1665 decl->identifier);
1666 }
1667 if (this->type->qualifier.in) {
1668 _mesa_glsl_error(& loc, state,
1669 "`in' qualifier in declaration of `%s' "
1670 "only valid for function parameters in GLSL 1.10.",
1671 decl->identifier);
1672 }
1673 /* FINISHME: Test for other invalid qualifiers. */
1674 }
1675
Eric Anholt2e063f12010-03-28 00:56:22 -07001676 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
1677 & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001678
Ian Romanick6f0823d2010-07-01 20:39:08 -07001679 if (this->type->qualifier.invariant) {
Eric Anholt046bef22010-08-04 20:33:57 -07001680 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
1681 var->mode == ir_var_inout)) {
1682 /* FINISHME: Note that this doesn't work for invariant on
1683 * a function signature outval
1684 */
Ian Romanick6f0823d2010-07-01 20:39:08 -07001685 _mesa_glsl_error(& loc, state,
1686 "`%s' cannot be marked invariant, vertex shader "
1687 "outputs only\n", var->name);
Eric Anholt046bef22010-08-04 20:33:57 -07001688 } else if ((state->target == fragment_shader) &&
1689 !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
1690 /* FINISHME: Note that this doesn't work for invariant on
1691 * a function signature inval
1692 */
Ian Romanick6f0823d2010-07-01 20:39:08 -07001693 _mesa_glsl_error(& loc, state,
1694 "`%s' cannot be marked invariant, fragment shader "
1695 "inputs only\n", var->name);
1696 }
1697 }
1698
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07001699 if (state->current_function != NULL) {
Ian Romanickb168e532010-03-31 12:31:18 -07001700 const char *mode = NULL;
Ian Romanicke0800062010-03-31 13:15:23 -07001701 const char *extra = "";
Ian Romanickb168e532010-03-31 12:31:18 -07001702
Ian Romanicke0800062010-03-31 13:15:23 -07001703 /* There is no need to check for 'inout' here because the parser will
1704 * only allow that in function parameter lists.
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07001705 */
1706 if (this->type->qualifier.attribute) {
Ian Romanickb168e532010-03-31 12:31:18 -07001707 mode = "attribute";
1708 } else if (this->type->qualifier.uniform) {
1709 mode = "uniform";
1710 } else if (this->type->qualifier.varying) {
1711 mode = "varying";
Ian Romanicke0800062010-03-31 13:15:23 -07001712 } else if (this->type->qualifier.in) {
1713 mode = "in";
1714 extra = " or in function parameter list";
1715 } else if (this->type->qualifier.out) {
1716 mode = "out";
1717 extra = " or in function parameter list";
Ian Romanickb168e532010-03-31 12:31:18 -07001718 }
1719
1720 if (mode) {
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07001721 _mesa_glsl_error(& loc, state,
Ian Romanickb168e532010-03-31 12:31:18 -07001722 "%s variable `%s' must be declared at "
Ian Romanicke0800062010-03-31 13:15:23 -07001723 "global scope%s",
1724 mode, var->name, extra);
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07001725 }
1726 } else if (var->mode == ir_var_in) {
Ian Romanickfb9f5b02010-03-29 17:16:35 -07001727 if (state->target == vertex_shader) {
1728 bool error_emitted = false;
1729
1730 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1731 *
1732 * "Vertex shader inputs can only be float, floating-point
1733 * vectors, matrices, signed and unsigned integers and integer
1734 * vectors. Vertex shader inputs can also form arrays of these
1735 * types, but not structures."
1736 *
Ian Romanick2d816202010-03-29 17:40:11 -07001737 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1738 *
1739 * "Vertex shader inputs can only be float, floating-point
1740 * vectors, matrices, signed and unsigned integers and integer
1741 * vectors. They cannot be arrays or structures."
1742 *
Ian Romanickfb9f5b02010-03-29 17:16:35 -07001743 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1744 *
1745 * "The attribute qualifier can be used only with float,
1746 * floating-point vectors, and matrices. Attribute variables
1747 * cannot be declared as arrays or structures."
1748 */
1749 const glsl_type *check_type = var->type->is_array()
1750 ? var->type->fields.array : var->type;
1751
1752 switch (check_type->base_type) {
1753 case GLSL_TYPE_FLOAT:
1754 break;
1755 case GLSL_TYPE_UINT:
1756 case GLSL_TYPE_INT:
1757 if (state->language_version > 120)
1758 break;
1759 /* FALLTHROUGH */
1760 default:
1761 _mesa_glsl_error(& loc, state,
1762 "vertex shader input / attribute cannot have "
1763 "type %s`%s'",
1764 var->type->is_array() ? "array of " : "",
1765 check_type->name);
1766 error_emitted = true;
1767 }
1768
Ian Romanick2d816202010-03-29 17:40:11 -07001769 if (!error_emitted && (state->language_version <= 130)
Ian Romanickfb9f5b02010-03-29 17:16:35 -07001770 && var->type->is_array()) {
1771 _mesa_glsl_error(& loc, state,
1772 "vertex shader input / attribute cannot have "
1773 "array type");
1774 error_emitted = true;
1775 }
1776 }
1777 }
1778
Ian Romanicke78e0fa2010-07-07 12:13:34 -07001779 /* Process the initializer and add its instructions to a temporary
1780 * list. This list will be added to the instruction stream (below) after
1781 * the declaration is added. This is done because in some cases (such as
1782 * redeclarations) the declaration may not actually be added to the
1783 * instruction stream.
1784 */
Eric Anholtfa33d0b2010-07-29 13:50:17 -07001785 exec_list initializer_instructions;
Ian Romanick66faec42010-03-27 18:56:53 -07001786 if (decl->initializer != NULL) {
Ian Romanick43de1722010-03-28 17:03:16 -07001787 YYLTYPE initializer_loc = decl->initializer->get_location();
1788
Ian Romanick66faec42010-03-27 18:56:53 -07001789 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1790 *
1791 * "All uniform variables are read-only and are initialized either
1792 * directly by an application via API commands, or indirectly by
1793 * OpenGL."
1794 */
1795 if ((state->language_version <= 110)
1796 && (var->mode == ir_var_uniform)) {
Ian Romanick43de1722010-03-28 17:03:16 -07001797 _mesa_glsl_error(& initializer_loc, state,
1798 "cannot initialize uniforms in GLSL 1.10");
1799 }
Ian Romanick19360152010-03-26 18:05:27 -07001800
Ian Romanick43de1722010-03-28 17:03:16 -07001801 if (var->type->is_sampler()) {
1802 _mesa_glsl_error(& initializer_loc, state,
1803 "cannot initialize samplers");
1804 }
1805
1806 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
1807 _mesa_glsl_error(& initializer_loc, state,
1808 "cannot initialize %s shader input / %s",
Ian Romanickae4c4c02010-04-07 16:41:40 -07001809 _mesa_glsl_shader_target_name(state->target),
Ian Romanick43de1722010-03-28 17:03:16 -07001810 (state->target == vertex_shader)
1811 ? "attribute" : "varying");
Ian Romanick66faec42010-03-27 18:56:53 -07001812 }
1813
Carl Worth1660a292010-06-23 18:11:51 -07001814 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
Eric Anholtfa33d0b2010-07-29 13:50:17 -07001815 ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions,
Ian Romanicke78e0fa2010-07-07 12:13:34 -07001816 state);
Ian Romanick66faec42010-03-27 18:56:53 -07001817
Ian Romanickce030882010-06-17 20:09:34 -07001818 /* Calculate the constant value if this is a const or uniform
Eric Anholt307c71b2010-03-31 15:53:26 -10001819 * declaration.
Ian Romanick66faec42010-03-27 18:56:53 -07001820 */
Ian Romanickce030882010-06-17 20:09:34 -07001821 if (this->type->qualifier.constant || this->type->qualifier.uniform) {
Kenneth Graunkee1d71852010-07-20 03:53:47 -07001822 ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs);
1823 if (new_rhs != NULL) {
1824 rhs = new_rhs;
1825 } else {
1826 _mesa_glsl_error(&initializer_loc, state,
1827 "initializer of type %s cannot be assigned to "
1828 "variable of type %s",
1829 rhs->type->name, var->type->name);
1830 }
1831
Eric Anholt326c6762010-04-06 10:30:54 -07001832 ir_constant *constant_value = rhs->constant_expression_value();
1833 if (!constant_value) {
Eric Anholt307c71b2010-03-31 15:53:26 -10001834 _mesa_glsl_error(& initializer_loc, state,
Ian Romanickce030882010-06-17 20:09:34 -07001835 "initializer of %s variable `%s' must be a "
Eric Anholt307c71b2010-03-31 15:53:26 -10001836 "constant expression",
Ian Romanickce030882010-06-17 20:09:34 -07001837 (this->type->qualifier.constant)
1838 ? "const" : "uniform",
Eric Anholt307c71b2010-03-31 15:53:26 -10001839 decl->identifier);
Eric Anholt326c6762010-04-06 10:30:54 -07001840 } else {
1841 rhs = constant_value;
1842 var->constant_value = constant_value;
Eric Anholt307c71b2010-03-31 15:53:26 -10001843 }
1844 }
Ian Romanick66faec42010-03-27 18:56:53 -07001845
Eric Anholt307c71b2010-03-31 15:53:26 -10001846 if (rhs && !rhs->type->is_error()) {
Eric Anholtac3af372010-03-31 15:44:38 -10001847 bool temp = var->read_only;
1848 if (this->type->qualifier.constant)
1849 var->read_only = false;
Ian Romanickce030882010-06-17 20:09:34 -07001850
1851 /* Never emit code to initialize a uniform.
1852 */
1853 if (!this->type->qualifier.uniform)
Eric Anholtfa33d0b2010-07-29 13:50:17 -07001854 result = do_assignment(&initializer_instructions, state,
1855 lhs, rhs,
Ian Romanickce030882010-06-17 20:09:34 -07001856 this->get_location());
Eric Anholtac3af372010-03-31 15:44:38 -10001857 var->read_only = temp;
Ian Romanick66faec42010-03-27 18:56:53 -07001858 }
Ian Romanick19360152010-03-26 18:05:27 -07001859 }
Ian Romanick17d86f42010-03-29 12:59:02 -07001860
Eric Anholt0ed61252010-03-31 09:29:33 -10001861 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1862 *
1863 * "It is an error to write to a const variable outside of
1864 * its declaration, so they must be initialized when
1865 * declared."
1866 */
1867 if (this->type->qualifier.constant && decl->initializer == NULL) {
1868 _mesa_glsl_error(& loc, state,
1869 "const declaration of `%s' must be initialized");
1870 }
1871
Ian Romanick5466b632010-07-01 12:46:55 -07001872 /* Attempt to add the variable to the symbol table. If this fails, it
1873 * means the variable has already been declared at this scope. Arrays
1874 * fudge this rule a little bit.
1875 *
1876 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1877 *
1878 * "It is legal to declare an array without a size and then
1879 * later re-declare the same name as an array of the same
1880 * type and specify a size."
1881 */
1882 if (state->symbols->name_declared_this_scope(decl->identifier)) {
1883 ir_variable *const earlier =
1884 state->symbols->get_variable(decl->identifier);
1885
1886 if ((earlier != NULL)
1887 && (earlier->type->array_size() == 0)
1888 && var->type->is_array()
1889 && (var->type->element_type() == earlier->type->element_type())) {
1890 /* FINISHME: This doesn't match the qualifiers on the two
1891 * FINISHME: declarations. It's not 100% clear whether this is
1892 * FINISHME: required or not.
1893 */
1894
Ian Romanickcd00d5b2010-07-01 13:17:54 -07001895 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1896 *
1897 * "The size [of gl_TexCoord] can be at most
1898 * gl_MaxTextureCoords."
Ian Romanickcd00d5b2010-07-01 13:17:54 -07001899 */
Ian Romanick127308b2010-07-01 13:30:50 -07001900 const unsigned size = unsigned(var->type->array_size());
Ian Romanickcd00d5b2010-07-01 13:17:54 -07001901 if ((strcmp("gl_TexCoord", var->name) == 0)
Ian Romanick127308b2010-07-01 13:30:50 -07001902 && (size > state->Const.MaxTextureCoords)) {
Ian Romanickcd00d5b2010-07-01 13:17:54 -07001903 YYLTYPE loc = this->get_location();
1904
1905 _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot "
1906 "be larger than gl_MaxTextureCoords (%u)\n",
Ian Romanick127308b2010-07-01 13:30:50 -07001907 state->Const.MaxTextureCoords);
Ian Romanick12873fa2010-07-01 14:10:19 -07001908 } else if ((size > 0) && (size <= earlier->max_array_access)) {
Ian Romanick5466b632010-07-01 12:46:55 -07001909 YYLTYPE loc = this->get_location();
1910
1911 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
1912 "previous access",
1913 earlier->max_array_access);
1914 }
1915
1916 earlier->type = var->type;
1917 delete var;
1918 var = NULL;
Eric Anholt4a962172010-07-28 14:41:51 -07001919 } else if (state->extensions->ARB_fragment_coord_conventions &&
1920 (earlier != NULL) &&
1921 (strcmp(var->name, "gl_FragCoord") == 0) &&
1922 earlier->type == var->type &&
1923 earlier->mode == var->mode) {
1924 /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
1925 * qualifiers.
1926 */
1927 earlier->origin_upper_left = var->origin_upper_left;
1928 earlier->pixel_center_integer = var->pixel_center_integer;
Ian Romanick5466b632010-07-01 12:46:55 -07001929 } else {
1930 YYLTYPE loc = this->get_location();
1931
1932 _mesa_glsl_error(& loc, state, "`%s' redeclared",
1933 decl->identifier);
1934 }
1935
1936 continue;
1937 }
1938
1939 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1940 *
1941 * "Identifiers starting with "gl_" are reserved for use by
1942 * OpenGL, and may not be declared in a shader as either a
1943 * variable or a function."
1944 */
1945 if (strncmp(decl->identifier, "gl_", 3) == 0) {
1946 /* FINISHME: This should only trigger if we're not redefining
1947 * FINISHME: a builtin (to add a qualifier, for example).
1948 */
1949 _mesa_glsl_error(& loc, state,
1950 "identifier `%s' uses reserved `gl_' prefix",
1951 decl->identifier);
1952 }
1953
Ian Romanick2e85f992010-07-07 11:57:16 -07001954 instructions->push_tail(var);
Eric Anholtfa33d0b2010-07-29 13:50:17 -07001955 instructions->append_list(&initializer_instructions);
Ian Romanick2e85f992010-07-07 11:57:16 -07001956
Kenneth Graunkec7f4ff12010-06-30 11:57:43 -07001957 /* Add the variable to the symbol table after processing the initializer.
Ian Romanick17d86f42010-03-29 12:59:02 -07001958 * This differs from most C-like languages, but it follows the GLSL
1959 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1960 * spec:
1961 *
1962 * "Within a declaration, the scope of a name starts immediately
1963 * after the initializer if present or immediately after the name
1964 * being declared if not."
1965 */
1966 const bool added_variable =
Kenneth Graunkec7f4ff12010-06-30 11:57:43 -07001967 state->symbols->add_variable(var->name, var);
Ian Romanick17d86f42010-03-29 12:59:02 -07001968 assert(added_variable);
Ian Romanicka87ac252010-02-22 13:19:34 -08001969 }
1970
Eric Anholt85584592010-04-14 15:38:52 -07001971
1972 /* Generally, variable declarations do not have r-values. However,
1973 * one is used for the declaration in
1974 *
1975 * while (bool b = some_condition()) {
1976 * ...
1977 * }
1978 *
1979 * so we return the rvalue from the last seen declaration here.
Ian Romanicka87ac252010-02-22 13:19:34 -08001980 */
Eric Anholt85584592010-04-14 15:38:52 -07001981 return result;
Ian Romanicka87ac252010-02-22 13:19:34 -08001982}
1983
1984
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001985ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001986ast_parameter_declarator::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001987 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001988{
Kenneth Graunke953ff122010-06-25 13:14:37 -07001989 void *ctx = state;
Ian Romanicka87ac252010-02-22 13:19:34 -08001990 const struct glsl_type *type;
1991 const char *name = NULL;
Eric Anholt2e063f12010-03-28 00:56:22 -07001992 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08001993
Ian Romanickd612a122010-03-31 16:22:06 -07001994 type = this->type->specifier->glsl_type(& name, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001995
1996 if (type == NULL) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001997 if (name != NULL) {
1998 _mesa_glsl_error(& loc, state,
1999 "invalid type `%s' in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08002000 name, this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08002001 } else {
2002 _mesa_glsl_error(& loc, state,
2003 "invalid type in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08002004 this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08002005 }
2006
Ian Romanick0471e8b2010-03-26 14:33:41 -07002007 type = glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -08002008 }
2009
Eric Anholt068c80c2010-03-31 09:56:36 -10002010 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2011 *
2012 * "Functions that accept no input arguments need not use void in the
2013 * argument list because prototypes (or definitions) are required and
2014 * therefore there is no ambiguity when an empty argument list "( )" is
2015 * declared. The idiom "(void)" as a parameter list is provided for
2016 * convenience."
2017 *
2018 * Placing this check here prevents a void parameter being set up
2019 * for a function, which avoids tripping up checks for main taking
2020 * parameters and lookups of an unnamed symbol.
2021 */
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002022 if (type->is_void()) {
2023 if (this->identifier != NULL)
2024 _mesa_glsl_error(& loc, state,
2025 "named parameter cannot have type `void'");
2026
2027 is_void = true;
Eric Anholt068c80c2010-03-31 09:56:36 -10002028 return NULL;
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002029 }
Eric Anholt068c80c2010-03-31 09:56:36 -10002030
Ian Romanick45d8a702010-04-02 15:09:33 -07002031 if (formal_parameter && (this->identifier == NULL)) {
2032 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
2033 return NULL;
2034 }
2035
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002036 is_void = false;
Ian Romanick7e2aa912010-07-19 17:12:42 -07002037 ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
Ian Romanicka87ac252010-02-22 13:19:34 -08002038
2039 /* FINISHME: Handle array declarations. Note that this requires
2040 * FINISHME: complete handling of constant expressions.
2041 */
2042
Ian Romanickcdb8d542010-03-11 14:48:51 -08002043 /* Apply any specified qualifiers to the parameter declaration. Note that
2044 * for function parameters the default mode is 'in'.
2045 */
Eric Anholt2e063f12010-03-28 00:56:22 -07002046 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08002047
Ian Romanick0044e7e2010-03-08 23:44:00 -08002048 instructions->push_tail(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08002049
2050 /* Parameter declarations do not have r-values.
2051 */
2052 return NULL;
2053}
2054
2055
Ian Romanick45d8a702010-04-02 15:09:33 -07002056void
Ian Romanick304ea902010-05-10 11:17:53 -07002057ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
Ian Romanick45d8a702010-04-02 15:09:33 -07002058 bool formal,
2059 exec_list *ir_parameters,
2060 _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08002061{
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002062 ast_parameter_declarator *void_param = NULL;
2063 unsigned count = 0;
Ian Romanicka87ac252010-02-22 13:19:34 -08002064
Ian Romanick2b97dc62010-05-10 17:42:05 -07002065 foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
Ian Romanick45d8a702010-04-02 15:09:33 -07002066 param->formal_parameter = formal;
Eric Anholt068c80c2010-03-31 09:56:36 -10002067 param->hir(ir_parameters, state);
Ian Romanickcf37c9e2010-04-02 15:30:45 -07002068
2069 if (param->is_void)
2070 void_param = param;
2071
2072 count++;
2073 }
2074
2075 if ((void_param != NULL) && (count > 1)) {
2076 YYLTYPE loc = void_param->get_location();
2077
2078 _mesa_glsl_error(& loc, state,
2079 "`void' parameter must be only parameter");
Ian Romanicka87ac252010-02-22 13:19:34 -08002080 }
2081}
2082
2083
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07002084ir_rvalue *
Ian Romanick92318a92010-03-31 18:23:21 -07002085ast_function::hir(exec_list *instructions,
2086 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08002087{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002088 void *ctx = state;
Ian Romanick18238de2010-03-01 13:49:10 -08002089 ir_function *f = NULL;
Ian Romanick92318a92010-03-31 18:23:21 -07002090 ir_function_signature *sig = NULL;
2091 exec_list hir_parameters;
Ian Romanicka87ac252010-02-22 13:19:34 -08002092
Kenneth Graunkeac04c252010-06-29 00:48:10 -07002093 const char *const name = identifier;
Ian Romanicka87ac252010-02-22 13:19:34 -08002094
2095 /* Convert the list of function parameters to HIR now so that they can be
2096 * used below to compare this function's signature with previously seen
2097 * signatures for functions with the same name.
2098 */
Ian Romanick45d8a702010-04-02 15:09:33 -07002099 ast_parameter_declarator::parameters_to_hir(& this->parameters,
2100 is_definition,
2101 & hir_parameters, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08002102
Ian Romanicke39cc692010-03-23 12:19:13 -07002103 const char *return_type_name;
2104 const glsl_type *return_type =
Ian Romanick92318a92010-03-31 18:23:21 -07002105 this->return_type->specifier->glsl_type(& return_type_name, state);
Ian Romanicke39cc692010-03-23 12:19:13 -07002106
2107 assert(return_type != NULL);
2108
Kenneth Graunkeac04c252010-06-29 00:48:10 -07002109 /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2110 * "No qualifier is allowed on the return type of a function."
2111 */
2112 if (this->return_type->has_qualifiers()) {
2113 YYLTYPE loc = this->get_location();
2114 _mesa_glsl_error(& loc, state,
2115 "function `%s' return type has qualifiers", name);
2116 }
2117
Ian Romanicka87ac252010-02-22 13:19:34 -08002118 /* Verify that this function's signature either doesn't match a previously
2119 * seen signature for a function with the same name, or, if a match is found,
2120 * that the previously seen signature does not have an associated definition.
2121 */
Ian Romanick3359e582010-03-19 15:38:52 -07002122 f = state->symbols->get_function(name);
Ian Romanicka87ac252010-02-22 13:19:34 -08002123 if (f != NULL) {
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07002124 ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
2125 if (sig != NULL) {
2126 const char *badvar = sig->qualifiers_match(&hir_parameters);
2127 if (badvar != NULL) {
2128 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08002129
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07002130 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
2131 "qualifiers don't match prototype", name, badvar);
Ian Romanicka87ac252010-02-22 13:19:34 -08002132 }
2133
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07002134 if (sig->return_type != return_type) {
2135 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08002136
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07002137 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
2138 "match prototype", name);
2139 }
2140
2141 if (is_definition && sig->is_defined) {
2142 YYLTYPE loc = this->get_location();
2143
2144 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
2145 sig = NULL;
2146 }
2147 }
Ian Romanick3359e582010-03-19 15:38:52 -07002148 } else if (state->symbols->name_declared_this_scope(name)) {
2149 /* This function name shadows a non-function use of the same name.
2150 */
2151 YYLTYPE loc = this->get_location();
2152
2153 _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
2154 "non-function", name);
Ian Romanick92318a92010-03-31 18:23:21 -07002155 sig = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08002156 } else {
Carl Worth1660a292010-06-23 18:11:51 -07002157 f = new(ctx) ir_function(name);
Ian Romanick8bde4ce2010-03-19 11:57:24 -07002158 state->symbols->add_function(f->name, f);
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07002159
2160 /* Emit the new function header */
2161 instructions->push_tail(f);
Ian Romanicka87ac252010-02-22 13:19:34 -08002162 }
2163
Eric Anholtab372da2010-03-28 01:24:55 -07002164 /* Verify the return type of main() */
2165 if (strcmp(name, "main") == 0) {
Ian Romanick25711a82010-03-31 17:39:10 -07002166 if (! return_type->is_void()) {
Eric Anholtab372da2010-03-28 01:24:55 -07002167 YYLTYPE loc = this->get_location();
2168
2169 _mesa_glsl_error(& loc, state, "main() must return void");
2170 }
Eric Anholt174cc032010-03-30 23:37:51 -10002171
Ian Romanick92318a92010-03-31 18:23:21 -07002172 if (!hir_parameters.is_empty()) {
Eric Anholt174cc032010-03-30 23:37:51 -10002173 YYLTYPE loc = this->get_location();
2174
2175 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
2176 }
Eric Anholtab372da2010-03-28 01:24:55 -07002177 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002178
2179 /* Finish storing the information about this new function in its signature.
2180 */
Ian Romanick92318a92010-03-31 18:23:21 -07002181 if (sig == NULL) {
Carl Worth1660a292010-06-23 18:11:51 -07002182 sig = new(ctx) ir_function_signature(return_type);
Ian Romanick92318a92010-03-31 18:23:21 -07002183 f->add_signature(sig);
Ian Romanicka87ac252010-02-22 13:19:34 -08002184 }
2185
Kenneth Graunkebff60132010-04-28 12:44:24 -07002186 sig->replace_parameters(&hir_parameters);
Ian Romanick92318a92010-03-31 18:23:21 -07002187 signature = sig;
Ian Romanicke29a5852010-03-31 17:54:26 -07002188
Ian Romanick92318a92010-03-31 18:23:21 -07002189 /* Function declarations (prototypes) do not have r-values.
2190 */
2191 return NULL;
2192}
2193
2194
2195ir_rvalue *
2196ast_function_definition::hir(exec_list *instructions,
2197 struct _mesa_glsl_parse_state *state)
2198{
2199 prototype->is_definition = true;
2200 prototype->hir(instructions, state);
2201
2202 ir_function_signature *signature = prototype->signature;
Ian Romanicka87ac252010-02-22 13:19:34 -08002203
Ian Romanick41ec6a42010-03-19 17:08:05 -07002204 assert(state->current_function == NULL);
2205 state->current_function = signature;
Kenneth Graunke6de82562010-06-29 09:59:40 -07002206 state->found_return = false;
Ian Romanick41ec6a42010-03-19 17:08:05 -07002207
Ian Romanicke29a5852010-03-31 17:54:26 -07002208 /* Duplicate parameters declared in the prototype as concrete variables.
2209 * Add these to the symbol table.
Ian Romanicka87ac252010-02-22 13:19:34 -08002210 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07002211 state->symbols->push_scope();
Ian Romanicke29a5852010-03-31 17:54:26 -07002212 foreach_iter(exec_list_iterator, iter, signature->parameters) {
Eric Anholtfbc7c0b2010-04-07 14:32:53 -07002213 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
Ian Romanicka87ac252010-02-22 13:19:34 -08002214
Eric Anholtfbc7c0b2010-04-07 14:32:53 -07002215 assert(var != NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08002216
Ian Romanick3359e582010-03-19 15:38:52 -07002217 /* The only way a parameter would "exist" is if two parameters have
2218 * the same name.
2219 */
2220 if (state->symbols->name_declared_this_scope(var->name)) {
2221 YYLTYPE loc = this->get_location();
2222
2223 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
2224 } else {
2225 state->symbols->add_variable(var->name, var);
2226 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002227 }
2228
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07002229 /* Convert the body of the function to HIR. */
Eric Anholt894ea972010-04-07 13:19:11 -07002230 this->body->hir(&signature->body, state);
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07002231 signature->is_defined = true;
Ian Romanicka87ac252010-02-22 13:19:34 -08002232
Ian Romanick8bde4ce2010-03-19 11:57:24 -07002233 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08002234
Ian Romanick41ec6a42010-03-19 17:08:05 -07002235 assert(state->current_function == signature);
2236 state->current_function = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08002237
Kenneth Graunke6de82562010-06-29 09:59:40 -07002238 if (!signature->return_type->is_void() && !state->found_return) {
2239 YYLTYPE loc = this->get_location();
2240 _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
2241 "%s, but no return statement",
2242 signature->function_name(),
2243 signature->return_type->name);
2244 }
2245
Ian Romanicka87ac252010-02-22 13:19:34 -08002246 /* Function definitions do not have r-values.
2247 */
2248 return NULL;
2249}
Ian Romanick16a246c2010-03-19 16:45:19 -07002250
2251
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07002252ir_rvalue *
Ian Romanick16a246c2010-03-19 16:45:19 -07002253ast_jump_statement::hir(exec_list *instructions,
2254 struct _mesa_glsl_parse_state *state)
2255{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002256 void *ctx = state;
Ian Romanick16a246c2010-03-19 16:45:19 -07002257
Ian Romanickc0e76d82010-04-05 16:53:19 -07002258 switch (mode) {
2259 case ast_return: {
Ian Romanick16a246c2010-03-19 16:45:19 -07002260 ir_return *inst;
Eric Anholtaad7c772010-03-30 23:28:20 -10002261 assert(state->current_function);
Ian Romanick16a246c2010-03-19 16:45:19 -07002262
2263 if (opt_return_value) {
Eric Anholtab79d4e2010-03-30 23:23:16 -10002264 if (state->current_function->return_type->base_type ==
2265 GLSL_TYPE_VOID) {
2266 YYLTYPE loc = this->get_location();
2267
2268 _mesa_glsl_error(& loc, state,
2269 "`return` with a value, in function `%s' "
2270 "returning void",
Kenneth Graunkef96c52b2010-04-21 15:17:26 -07002271 state->current_function->function_name());
Eric Anholtab79d4e2010-03-30 23:23:16 -10002272 }
Ian Romanick16a246c2010-03-19 16:45:19 -07002273
2274 ir_expression *const ret = (ir_expression *)
2275 opt_return_value->hir(instructions, state);
2276 assert(ret != NULL);
2277
Kenneth Graunke18707eb2010-06-28 23:38:04 -07002278 /* Implicit conversions are not allowed for return values. */
2279 if (state->current_function->return_type != ret->type) {
2280 YYLTYPE loc = this->get_location();
2281
2282 _mesa_glsl_error(& loc, state,
2283 "`return' with wrong type %s, in function `%s' "
2284 "returning %s",
2285 ret->type->name,
2286 state->current_function->function_name(),
2287 state->current_function->return_type->name);
2288 }
Ian Romanick16a246c2010-03-19 16:45:19 -07002289
Carl Worth1660a292010-06-23 18:11:51 -07002290 inst = new(ctx) ir_return(ret);
Ian Romanick16a246c2010-03-19 16:45:19 -07002291 } else {
Eric Anholtaad7c772010-03-30 23:28:20 -10002292 if (state->current_function->return_type->base_type !=
2293 GLSL_TYPE_VOID) {
2294 YYLTYPE loc = this->get_location();
2295
2296 _mesa_glsl_error(& loc, state,
2297 "`return' with no value, in function %s returning "
2298 "non-void",
Kenneth Graunkef96c52b2010-04-21 15:17:26 -07002299 state->current_function->function_name());
Eric Anholtaad7c772010-03-30 23:28:20 -10002300 }
Carl Worth1660a292010-06-23 18:11:51 -07002301 inst = new(ctx) ir_return;
Ian Romanick16a246c2010-03-19 16:45:19 -07002302 }
2303
Kenneth Graunke6de82562010-06-29 09:59:40 -07002304 state->found_return = true;
Ian Romanick16a246c2010-03-19 16:45:19 -07002305 instructions->push_tail(inst);
Ian Romanickc0e76d82010-04-05 16:53:19 -07002306 break;
Ian Romanick16a246c2010-03-19 16:45:19 -07002307 }
2308
Ian Romanickc0e76d82010-04-05 16:53:19 -07002309 case ast_discard:
Eric Anholtb9802072010-03-30 23:40:14 -10002310 if (state->target != fragment_shader) {
2311 YYLTYPE loc = this->get_location();
2312
2313 _mesa_glsl_error(& loc, state,
2314 "`discard' may only appear in a fragment shader");
2315 }
Kenneth Graunke77049a72010-06-30 14:11:00 -07002316 instructions->push_tail(new(ctx) ir_discard);
Ian Romanickc0e76d82010-04-05 16:53:19 -07002317 break;
2318
2319 case ast_break:
2320 case ast_continue:
Ian Romanick4cf20cd2010-04-05 17:13:47 -07002321 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2322 * FINISHME: and they use a different IR instruction for 'break'.
2323 */
2324 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2325 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2326 * FINISHME: loop.
2327 */
2328 if (state->loop_or_switch_nesting == NULL) {
2329 YYLTYPE loc = this->get_location();
2330
2331 _mesa_glsl_error(& loc, state,
2332 "`%s' may only appear in a loop",
2333 (mode == ast_break) ? "break" : "continue");
2334 } else {
2335 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
2336
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07002337 /* Inline the for loop expression again, since we don't know
2338 * where near the end of the loop body the normal copy of it
2339 * is going to be placed.
2340 */
2341 if (mode == ast_continue &&
2342 state->loop_or_switch_nesting_ast->rest_expression) {
2343 state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
2344 state);
2345 }
2346
Ian Romanick4cf20cd2010-04-05 17:13:47 -07002347 if (loop != NULL) {
2348 ir_loop_jump *const jump =
Carl Worth1660a292010-06-23 18:11:51 -07002349 new(ctx) ir_loop_jump((mode == ast_break)
2350 ? ir_loop_jump::jump_break
2351 : ir_loop_jump::jump_continue);
Ian Romanick4cf20cd2010-04-05 17:13:47 -07002352 instructions->push_tail(jump);
2353 }
2354 }
2355
Ian Romanickc0e76d82010-04-05 16:53:19 -07002356 break;
Eric Anholtb9802072010-03-30 23:40:14 -10002357 }
2358
Ian Romanick16a246c2010-03-19 16:45:19 -07002359 /* Jump instructions do not have r-values.
2360 */
2361 return NULL;
2362}
Ian Romanick3c6fea32010-03-29 14:11:25 -07002363
2364
2365ir_rvalue *
2366ast_selection_statement::hir(exec_list *instructions,
2367 struct _mesa_glsl_parse_state *state)
2368{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002369 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07002370
Ian Romanick3c6fea32010-03-29 14:11:25 -07002371 ir_rvalue *const condition = this->condition->hir(instructions, state);
Ian Romanick3c6fea32010-03-29 14:11:25 -07002372
2373 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2374 *
2375 * "Any expression whose type evaluates to a Boolean can be used as the
2376 * conditional expression bool-expression. Vector types are not accepted
2377 * as the expression to if."
2378 *
2379 * The checks are separated so that higher quality diagnostics can be
2380 * generated for cases where both rules are violated.
2381 */
2382 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
2383 YYLTYPE loc = this->condition->get_location();
2384
2385 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
2386 "boolean");
2387 }
2388
Carl Worth1660a292010-06-23 18:11:51 -07002389 ir_if *const stmt = new(ctx) ir_if(condition);
Ian Romanick3c6fea32010-03-29 14:11:25 -07002390
Ian Romanick4f9d72f2010-05-10 11:10:26 -07002391 if (then_statement != NULL)
2392 then_statement->hir(& stmt->then_instructions, state);
Ian Romanick3c6fea32010-03-29 14:11:25 -07002393
Ian Romanick4f9d72f2010-05-10 11:10:26 -07002394 if (else_statement != NULL)
2395 else_statement->hir(& stmt->else_instructions, state);
Ian Romanick3c6fea32010-03-29 14:11:25 -07002396
2397 instructions->push_tail(stmt);
2398
2399 /* if-statements do not have r-values.
2400 */
2401 return NULL;
2402}
Ian Romanick9e7d0102010-04-05 16:37:49 -07002403
2404
Ian Romanick8c46ed22010-04-05 18:07:27 -07002405void
2406ast_iteration_statement::condition_to_hir(ir_loop *stmt,
2407 struct _mesa_glsl_parse_state *state)
Ian Romanick9e7d0102010-04-05 16:37:49 -07002408{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002409 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07002410
Ian Romanick9e7d0102010-04-05 16:37:49 -07002411 if (condition != NULL) {
2412 ir_rvalue *const cond =
2413 condition->hir(& stmt->body_instructions, state);
2414
2415 if ((cond == NULL)
2416 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
2417 YYLTYPE loc = condition->get_location();
2418
2419 _mesa_glsl_error(& loc, state,
2420 "loop condition must be scalar boolean");
2421 } else {
2422 /* As the first code in the loop body, generate a block that looks
2423 * like 'if (!condition) break;' as the loop termination condition.
2424 */
2425 ir_rvalue *const not_cond =
Carl Worth1660a292010-06-23 18:11:51 -07002426 new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
2427 NULL);
Ian Romanick9e7d0102010-04-05 16:37:49 -07002428
Carl Worth1660a292010-06-23 18:11:51 -07002429 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
Ian Romanick9e7d0102010-04-05 16:37:49 -07002430
2431 ir_jump *const break_stmt =
Carl Worth1660a292010-06-23 18:11:51 -07002432 new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
Ian Romanick9e7d0102010-04-05 16:37:49 -07002433
2434 if_stmt->then_instructions.push_tail(break_stmt);
2435 stmt->body_instructions.push_tail(if_stmt);
2436 }
2437 }
Ian Romanick8c46ed22010-04-05 18:07:27 -07002438}
2439
2440
2441ir_rvalue *
2442ast_iteration_statement::hir(exec_list *instructions,
2443 struct _mesa_glsl_parse_state *state)
2444{
Kenneth Graunke953ff122010-06-25 13:14:37 -07002445 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -07002446
Ian Romanick48460662010-04-16 16:42:43 -07002447 /* For-loops and while-loops start a new scope, but do-while loops do not.
Ian Romanick8c46ed22010-04-05 18:07:27 -07002448 */
Ian Romanick48460662010-04-16 16:42:43 -07002449 if (mode != ast_do_while)
Ian Romanick8c46ed22010-04-05 18:07:27 -07002450 state->symbols->push_scope();
2451
2452 if (init_statement != NULL)
2453 init_statement->hir(instructions, state);
2454
Carl Worth1660a292010-06-23 18:11:51 -07002455 ir_loop *const stmt = new(ctx) ir_loop();
Ian Romanick8c46ed22010-04-05 18:07:27 -07002456 instructions->push_tail(stmt);
2457
2458 /* Track the current loop and / or switch-statement nesting.
2459 */
2460 ir_instruction *const nesting = state->loop_or_switch_nesting;
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07002461 ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
2462
Ian Romanick8c46ed22010-04-05 18:07:27 -07002463 state->loop_or_switch_nesting = stmt;
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07002464 state->loop_or_switch_nesting_ast = this;
Ian Romanick8c46ed22010-04-05 18:07:27 -07002465
2466 if (mode != ast_do_while)
2467 condition_to_hir(stmt, state);
Ian Romanick9e7d0102010-04-05 16:37:49 -07002468
Ian Romanick4f9d72f2010-05-10 11:10:26 -07002469 if (body != NULL)
2470 body->hir(& stmt->body_instructions, state);
Ian Romanick9e7d0102010-04-05 16:37:49 -07002471
2472 if (rest_expression != NULL)
2473 rest_expression->hir(& stmt->body_instructions, state);
2474
Ian Romanick8c46ed22010-04-05 18:07:27 -07002475 if (mode == ast_do_while)
2476 condition_to_hir(stmt, state);
2477
Ian Romanick48460662010-04-16 16:42:43 -07002478 if (mode != ast_do_while)
Ian Romanick9e7d0102010-04-05 16:37:49 -07002479 state->symbols->pop_scope();
2480
Ian Romanicke9d0f262010-04-05 17:01:53 -07002481 /* Restore previous nesting before returning.
2482 */
2483 state->loop_or_switch_nesting = nesting;
Eric Anholt2d1ed7b2010-07-22 12:55:16 -07002484 state->loop_or_switch_nesting_ast = nesting_ast;
Ian Romanicke9d0f262010-04-05 17:01:53 -07002485
Ian Romanick9e7d0102010-04-05 16:37:49 -07002486 /* Loops do not have r-values.
2487 */
2488 return NULL;
2489}
Ian Romanick3455ce62010-04-19 15:13:15 -07002490
2491
2492ir_rvalue *
2493ast_type_specifier::hir(exec_list *instructions,
2494 struct _mesa_glsl_parse_state *state)
2495{
2496 if (this->structure != NULL)
2497 return this->structure->hir(instructions, state);
Ian Romanick85ba37b2010-04-21 14:33:34 -07002498
2499 return NULL;
Ian Romanick3455ce62010-04-19 15:13:15 -07002500}
2501
2502
2503ir_rvalue *
2504ast_struct_specifier::hir(exec_list *instructions,
2505 struct _mesa_glsl_parse_state *state)
2506{
Ian Romanick3455ce62010-04-19 15:13:15 -07002507 unsigned decl_count = 0;
2508
2509 /* Make an initial pass over the list of structure fields to determine how
2510 * many there are. Each element in this list is an ast_declarator_list.
2511 * This means that we actually need to count the number of elements in the
2512 * 'declarations' list in each of the elements.
2513 */
Ian Romanick2b97dc62010-05-10 17:42:05 -07002514 foreach_list_typed (ast_declarator_list, decl_list, link,
2515 &this->declarations) {
Ian Romanick304ea902010-05-10 11:17:53 -07002516 foreach_list_const (decl_ptr, & decl_list->declarations) {
Ian Romanick3455ce62010-04-19 15:13:15 -07002517 decl_count++;
2518 }
2519 }
2520
2521
2522 /* Allocate storage for the structure fields and process the field
2523 * declarations. As the declarations are processed, try to also convert
2524 * the types to HIR. This ensures that structure definitions embedded in
2525 * other structure definitions are processed.
2526 */
Eric Anholt21b0dbd2010-07-20 16:47:25 -07002527 glsl_struct_field *const fields = talloc_array(state, glsl_struct_field,
2528 decl_count);
Ian Romanick3455ce62010-04-19 15:13:15 -07002529
2530 unsigned i = 0;
Ian Romanick2b97dc62010-05-10 17:42:05 -07002531 foreach_list_typed (ast_declarator_list, decl_list, link,
2532 &this->declarations) {
Ian Romanick3455ce62010-04-19 15:13:15 -07002533 const char *type_name;
2534
2535 decl_list->type->specifier->hir(instructions, state);
2536
2537 const glsl_type *decl_type =
2538 decl_list->type->specifier->glsl_type(& type_name, state);
2539
Ian Romanick2b97dc62010-05-10 17:42:05 -07002540 foreach_list_typed (ast_declaration, decl, link,
2541 &decl_list->declarations) {
Ian Romanick3455ce62010-04-19 15:13:15 -07002542 const struct glsl_type *const field_type =
2543 (decl->is_array)
2544 ? process_array_type(decl_type, decl->array_size, state)
2545 : decl_type;
2546
Ian Romanick73986a72010-04-20 16:49:03 -07002547 fields[i].type = (field_type != NULL)
2548 ? field_type : glsl_type::error_type;
Ian Romanick3455ce62010-04-19 15:13:15 -07002549 fields[i].name = decl->identifier;
2550 i++;
2551 }
2552 }
2553
2554 assert(i == decl_count);
2555
Ian Romanick1d28b612010-04-20 16:48:24 -07002556 const char *name;
2557 if (this->name == NULL) {
2558 static unsigned anon_count = 1;
2559 char buf[32];
Ian Romanick3455ce62010-04-19 15:13:15 -07002560
Ian Romanick1d28b612010-04-20 16:48:24 -07002561 snprintf(buf, sizeof(buf), "#anon_struct_%04x", anon_count);
2562 anon_count++;
2563
2564 name = strdup(buf);
2565 } else {
2566 name = this->name;
2567 }
2568
Ian Romanick49e35772010-06-28 11:54:57 -07002569 const glsl_type *t =
2570 glsl_type::get_record_instance(fields, decl_count, name);
Ian Romanick1d28b612010-04-20 16:48:24 -07002571
Ian Romanickab899272010-04-23 13:24:08 -07002572 YYLTYPE loc = this->get_location();
2573 if (!state->symbols->add_type(name, t)) {
2574 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
2575 } else {
2576 /* This logic is a bit tricky. It is an error to declare a structure at
2577 * global scope if there is also a function with the same name.
2578 */
2579 if ((state->current_function == NULL)
2580 && (state->symbols->get_function(name) != NULL)) {
2581 _mesa_glsl_error(& loc, state, "name `%s' previously defined", name);
2582 } else {
2583 t->generate_constructor(state->symbols);
2584 }
Ian Romanicka2c6df52010-04-28 13:14:53 -07002585
2586 const glsl_type **s = (const glsl_type **)
2587 realloc(state->user_structures,
2588 sizeof(state->user_structures[0]) *
2589 (state->num_user_structures + 1));
2590 if (s != NULL) {
2591 s[state->num_user_structures] = t;
2592 state->user_structures = s;
2593 state->num_user_structures++;
2594 }
Ian Romanickab899272010-04-23 13:24:08 -07002595 }
Ian Romanick3455ce62010-04-19 15:13:15 -07002596
2597 /* Structure type definitions do not have r-values.
2598 */
2599 return NULL;
2600}