blob: 0e6cb4f72719e986a82d71c98632bdeea38611b0 [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 */
51#include <stdio.h>
52#include "main/imports.h"
Ian Romanick8bde4ce2010-03-19 11:57:24 -070053#include "glsl_symbol_table.h"
Ian Romanicka87ac252010-02-22 13:19:34 -080054#include "glsl_parser_extras.h"
55#include "ast.h"
56#include "glsl_types.h"
57#include "ir.h"
58
Ian Romanickd949a9a2010-03-10 09:55:22 -080059void
60_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
61{
Ian Romanickadfb0cd2010-03-10 10:43:16 -080062 _mesa_glsl_initialize_variables(instructions, state);
Ian Romanicka4e92c42010-03-25 17:02:22 -070063 _mesa_glsl_initialize_constructors(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 Romanick304ea902010-05-10 11:17:53 -070068 foreach_list (n, & state->translation_unit) {
69 ast_node *ast = exec_node_data(ast_node, n, link);
70 ast->hir(instructions, state);
Ian Romanickd949a9a2010-03-10 09:55:22 -080071 }
72}
73
74
Ian Romanick01045362010-03-29 16:17:56 -070075/**
76 * If a conversion is available, convert one operand to a different type
77 *
78 * The \c from \c ir_rvalue is converted "in place".
79 *
80 * \param to Type that the operand it to be converted to
81 * \param from Operand that is being converted
82 * \param state GLSL compiler state
83 *
84 * \return
85 * If a conversion is possible (or unnecessary), \c true is returned.
86 * Otherwise \c false is returned.
87 */
88static bool
Ian Romanickbfb09c22010-03-29 16:32:55 -070089apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
Ian Romanick01045362010-03-29 16:17:56 -070090 struct _mesa_glsl_parse_state *state)
91{
Ian Romanickbfb09c22010-03-29 16:32:55 -070092 if (to->base_type == from->type->base_type)
Ian Romanick01045362010-03-29 16:17:56 -070093 return true;
94
95 /* This conversion was added in GLSL 1.20. If the compilation mode is
96 * GLSL 1.10, the conversion is skipped.
97 */
98 if (state->language_version < 120)
99 return false;
100
101 /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
102 *
103 * "There are no implicit array or structure conversions. For
104 * example, an array of int cannot be implicitly converted to an
105 * array of float. There are no implicit conversions between
106 * signed and unsigned integers."
107 */
108 /* FINISHME: The above comment is partially a lie. There is int/uint
109 * FINISHME: conversion for immediate constants.
110 */
Ian Romanickbfb09c22010-03-29 16:32:55 -0700111 if (!to->is_float() || !from->type->is_numeric())
Ian Romanick01045362010-03-29 16:17:56 -0700112 return false;
113
Ian Romanickbfb09c22010-03-29 16:32:55 -0700114 switch (from->type->base_type) {
Ian Romanick01045362010-03-29 16:17:56 -0700115 case GLSL_TYPE_INT:
Ian Romanickbfb09c22010-03-29 16:32:55 -0700116 from = new ir_expression(ir_unop_i2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700117 break;
118 case GLSL_TYPE_UINT:
Ian Romanickbfb09c22010-03-29 16:32:55 -0700119 from = new ir_expression(ir_unop_u2f, to, from, NULL);
Ian Romanick01045362010-03-29 16:17:56 -0700120 break;
121 case GLSL_TYPE_BOOL:
Eric Anholtdc58b3f2010-04-02 02:13:43 -1000122 from = new ir_expression(ir_unop_b2f, to, from, NULL);
123 break;
Ian Romanick01045362010-03-29 16:17:56 -0700124 default:
125 assert(0);
126 }
127
128 return true;
129}
130
131
Ian Romanicka87ac252010-02-22 13:19:34 -0800132static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700133arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Ian Romanicka87ac252010-02-22 13:19:34 -0800134 bool multiply,
Eric Anholta13bb142010-03-31 16:38:11 -1000135 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800136{
Ian Romanickbfb09c22010-03-29 16:32:55 -0700137 const glsl_type *const type_a = value_a->type;
138 const glsl_type *const type_b = value_b->type;
Ian Romanick01045362010-03-29 16:17:56 -0700139
Ian Romanicka87ac252010-02-22 13:19:34 -0800140 /* From GLSL 1.50 spec, page 56:
141 *
142 * "The arithmetic binary operators add (+), subtract (-),
143 * multiply (*), and divide (/) operate on integer and
144 * floating-point scalars, vectors, and matrices."
145 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700146 if (!type_a->is_numeric() || !type_b->is_numeric()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000147 _mesa_glsl_error(loc, state,
148 "Operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700149 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800150 }
151
152
153 /* "If one operand is floating-point based and the other is
154 * not, then the conversions from Section 4.1.10 "Implicit
155 * Conversions" are applied to the non-floating-point-based operand."
Ian Romanicka87ac252010-02-22 13:19:34 -0800156 */
Ian Romanick01045362010-03-29 16:17:56 -0700157 if (!apply_implicit_conversion(type_a, value_b, state)
158 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholta13bb142010-03-31 16:38:11 -1000159 _mesa_glsl_error(loc, state,
160 "Could not implicitly convert operands to "
161 "arithmetic operator");
Ian Romanick01045362010-03-29 16:17:56 -0700162 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800163 }
164
165 /* "If the operands are integer types, they must both be signed or
166 * both be unsigned."
167 *
168 * From this rule and the preceeding conversion it can be inferred that
169 * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
Ian Romanick60b54d92010-03-24 17:08:13 -0700170 * The is_numeric check above already filtered out the case where either
171 * type is not one of these, so now the base types need only be tested for
172 * equality.
Ian Romanicka87ac252010-02-22 13:19:34 -0800173 */
174 if (type_a->base_type != type_b->base_type) {
Eric Anholta13bb142010-03-31 16:38:11 -1000175 _mesa_glsl_error(loc, state,
176 "base type mismatch for arithmetic operator");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700177 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800178 }
179
180 /* "All arithmetic binary operators result in the same fundamental type
181 * (signed integer, unsigned integer, or floating-point) as the
182 * operands they operate on, after operand type conversion. After
183 * conversion, the following cases are valid
184 *
185 * * The two operands are scalars. In this case the operation is
186 * applied, resulting in a scalar."
187 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800188 if (type_a->is_scalar() && type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800189 return type_a;
190
191 /* "* One operand is a scalar, and the other is a vector or matrix.
192 * In this case, the scalar operation is applied independently to each
193 * component of the vector or matrix, resulting in the same size
194 * vector or matrix."
195 */
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800196 if (type_a->is_scalar()) {
197 if (!type_b->is_scalar())
Ian Romanicka87ac252010-02-22 13:19:34 -0800198 return type_b;
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800199 } else if (type_b->is_scalar()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800200 return type_a;
201 }
202
203 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
204 * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
205 * handled.
206 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700207 assert(!type_a->is_scalar());
208 assert(!type_b->is_scalar());
Ian Romanicka87ac252010-02-22 13:19:34 -0800209
210 /* "* The two operands are vectors of the same size. In this case, the
211 * operation is done component-wise resulting in the same size
212 * vector."
213 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800214 if (type_a->is_vector() && type_b->is_vector()) {
Eric Anholta13bb142010-03-31 16:38:11 -1000215 if (type_a == type_b) {
216 return type_a;
217 } else {
218 _mesa_glsl_error(loc, state,
219 "vector size mismatch for arithmetic operator");
220 return glsl_type::error_type;
221 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800222 }
223
224 /* All of the combinations of <scalar, scalar>, <vector, scalar>,
225 * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
226 * <vector, vector> have been handled. At least one of the operands must
227 * be matrix. Further, since there are no integer matrix types, the base
228 * type of both operands must be float.
229 */
Ian Romanick60b54d92010-03-24 17:08:13 -0700230 assert(type_a->is_matrix() || type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800231 assert(type_a->base_type == GLSL_TYPE_FLOAT);
232 assert(type_b->base_type == GLSL_TYPE_FLOAT);
233
234 /* "* The operator is add (+), subtract (-), or divide (/), and the
235 * operands are matrices with the same number of rows and the same
236 * number of columns. In this case, the operation is done component-
237 * wise resulting in the same size matrix."
238 * * The operator is multiply (*), where both operands are matrices or
239 * one operand is a vector and the other a matrix. A right vector
240 * operand is treated as a column vector and a left vector operand as a
241 * row vector. In all these cases, it is required that the number of
242 * columns of the left operand is equal to the number of rows of the
243 * right operand. Then, the multiply (*) operation does a linear
244 * algebraic multiply, yielding an object that has the same number of
245 * rows as the left operand and the same number of columns as the right
246 * operand. Section 5.10 "Vector and Matrix Operations" explains in
247 * more detail how vectors and matrices are operated on."
248 */
249 if (! multiply) {
Eric Anholta13bb142010-03-31 16:38:11 -1000250 if (type_a == type_b)
251 return type_a;
Ian Romanicka87ac252010-02-22 13:19:34 -0800252 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800253 if (type_a->is_matrix() && type_b->is_matrix()) {
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700254 /* Matrix multiply. The columns of A must match the rows of B. Given
255 * the other previously tested constraints, this means the vector type
256 * of a row from A must be the same as the vector type of a column from
257 * B.
258 */
259 if (type_a->row_type() == type_b->column_type()) {
260 /* The resulting matrix has the number of columns of matrix B and
261 * the number of rows of matrix A. We get the row count of A by
262 * looking at the size of a vector that makes up a column. The
263 * transpose (size of a row) is done for B.
264 */
Eric Anholta13bb142010-03-31 16:38:11 -1000265 const glsl_type *const type =
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700266 glsl_type::get_instance(type_a->base_type,
267 type_a->column_type()->vector_elements,
268 type_b->row_type()->vector_elements);
Eric Anholta13bb142010-03-31 16:38:11 -1000269 assert(type != glsl_type::error_type);
270
271 return type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800272 }
Ian Romanickfce11502010-03-09 15:58:52 -0800273 } else if (type_a->is_matrix()) {
Ian Romanicka87ac252010-02-22 13:19:34 -0800274 /* A is a matrix and B is a column vector. Columns of A must match
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700275 * rows of B. Given the other previously tested constraints, this
276 * means the vector type of a row from A must be the same as the
277 * vector the type of B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800278 */
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700279 if (type_a->row_type() == type_b)
Ian Romanicka87ac252010-02-22 13:19:34 -0800280 return type_b;
281 } else {
Ian Romanickfce11502010-03-09 15:58:52 -0800282 assert(type_b->is_matrix());
Ian Romanicka87ac252010-02-22 13:19:34 -0800283
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700284 /* A is a row vector and B is a matrix. Columns of A must match rows
285 * of B. Given the other previously tested constraints, this means
286 * the type of A must be the same as the vector type of a column from
287 * B.
Ian Romanicka87ac252010-02-22 13:19:34 -0800288 */
Ian Romanickc1bd3a12010-03-25 13:06:58 -0700289 if (type_a == type_b->column_type())
Ian Romanicka87ac252010-02-22 13:19:34 -0800290 return type_a;
291 }
Eric Anholta13bb142010-03-31 16:38:11 -1000292
293 _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
294 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800295 }
296
297
298 /* "All other cases are illegal."
299 */
Eric Anholta13bb142010-03-31 16:38:11 -1000300 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700301 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800302}
303
304
305static const struct glsl_type *
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000306unary_arithmetic_result_type(const struct glsl_type *type,
307 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800308{
309 /* From GLSL 1.50 spec, page 57:
310 *
311 * "The arithmetic unary operators negate (-), post- and pre-increment
312 * and decrement (-- and ++) operate on integer or floating-point
313 * values (including vectors and matrices). All unary operators work
314 * component-wise on their operands. These result with the same type
315 * they operated on."
316 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000317 if (!type->is_numeric()) {
318 _mesa_glsl_error(loc, state,
319 "Operands to arithmetic operators must be numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700320 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000321 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800322
323 return type;
324}
325
326
327static const struct glsl_type *
328modulus_result_type(const struct glsl_type *type_a,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000329 const struct glsl_type *type_b,
330 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800331{
332 /* From GLSL 1.50 spec, page 56:
333 * "The operator modulus (%) operates on signed or unsigned integers or
334 * integer vectors. The operand types must both be signed or both be
335 * unsigned."
336 */
Ian Romanick40176e22010-03-26 14:38:37 -0700337 if (!type_a->is_integer() || !type_b->is_integer()
Ian Romanicka87ac252010-02-22 13:19:34 -0800338 || (type_a->base_type != type_b->base_type)) {
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000339 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700340 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800341 }
342
343 /* "The operands cannot be vectors of differing size. If one operand is
344 * a scalar and the other vector, then the scalar is applied component-
345 * wise to the vector, resulting in the same type as the vector. If both
346 * are vectors of the same size, the result is computed component-wise."
347 */
Ian Romanicka2dd22f2010-03-09 15:55:16 -0800348 if (type_a->is_vector()) {
349 if (!type_b->is_vector()
Ian Romanicka87ac252010-02-22 13:19:34 -0800350 || (type_a->vector_elements == type_b->vector_elements))
351 return type_a;
352 } else
353 return type_b;
354
355 /* "The operator modulus (%) is not defined for any other data types
356 * (non-integer types)."
357 */
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000358 _mesa_glsl_error(loc, state, "type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700359 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800360}
361
362
363static const struct glsl_type *
Ian Romanickbfb09c22010-03-29 16:32:55 -0700364relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000365 struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -0800366{
Ian Romanickbfb09c22010-03-29 16:32:55 -0700367 const glsl_type *const type_a = value_a->type;
368 const glsl_type *const type_b = value_b->type;
Ian Romanick0150f5f2010-03-29 16:20:07 -0700369
Ian Romanicka87ac252010-02-22 13:19:34 -0800370 /* From GLSL 1.50 spec, page 56:
371 * "The relational operators greater than (>), less than (<), greater
372 * than or equal (>=), and less than or equal (<=) operate only on
373 * scalar integer and scalar floating-point expressions."
374 */
Ian Romanicka6d653d2010-03-26 14:40:37 -0700375 if (!type_a->is_numeric()
376 || !type_b->is_numeric()
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800377 || !type_a->is_scalar()
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000378 || !type_b->is_scalar()) {
379 _mesa_glsl_error(loc, state,
380 "Operands to relational operators must be scalar and "
381 "numeric");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700382 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000383 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800384
385 /* "Either the operands' types must match, or the conversions from
386 * Section 4.1.10 "Implicit Conversions" will be applied to the integer
387 * operand, after which the types must match."
Ian Romanicka87ac252010-02-22 13:19:34 -0800388 */
Ian Romanick0150f5f2010-03-29 16:20:07 -0700389 if (!apply_implicit_conversion(type_a, value_b, state)
390 && !apply_implicit_conversion(type_b, value_a, state)) {
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000391 _mesa_glsl_error(loc, state,
392 "Could not implicitly convert operands to "
393 "relational operator");
Ian Romanick0150f5f2010-03-29 16:20:07 -0700394 return glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800395 }
396
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000397 if (type_a->base_type != type_b->base_type) {
398 _mesa_glsl_error(loc, state, "base type mismatch");
Ian Romanick0471e8b2010-03-26 14:33:41 -0700399 return glsl_type::error_type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000400 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800401
402 /* "The result is scalar Boolean."
403 */
Ian Romanick0471e8b2010-03-26 14:33:41 -0700404 return glsl_type::bool_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800405}
406
407
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700408/**
409 * Validates that a value can be assigned to a location with a specified type
410 *
411 * Validates that \c rhs can be assigned to some location. If the types are
412 * not an exact match but an automatic conversion is possible, \c rhs will be
413 * converted.
414 *
415 * \return
416 * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
417 * Otherwise the actual RHS to be assigned will be returned. This may be
418 * \c rhs, or it may be \c rhs after some type conversion.
419 *
420 * \note
421 * In addition to being used for assignments, this function is used to
422 * type-check return values.
423 */
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700424ir_rvalue *
425validate_assignment(const glsl_type *lhs_type, ir_rvalue *rhs)
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700426{
427 const glsl_type *const rhs_type = rhs->type;
428
429 /* If there is already some error in the RHS, just return it. Anything
430 * else will lead to an avalanche of error message back to the user.
431 */
432 if (rhs_type->is_error())
433 return rhs;
434
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700435 /* If the types are identical, the assignment can trivially proceed.
436 */
437 if (rhs_type == lhs_type)
438 return rhs;
439
Ian Romanick0157f412010-04-02 17:44:39 -0700440 /* If the array element types are the same and the size of the LHS is zero,
441 * the assignment is okay.
442 *
443 * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
444 * is handled by ir_dereference::is_lvalue.
445 */
446 if (lhs_type->is_array() && rhs->type->is_array()
447 && (lhs_type->element_type() == rhs->type->element_type())
448 && (lhs_type->array_size() == 0)) {
449 return rhs;
450 }
451
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700452 /* FINISHME: Check for and apply automatic conversions. */
453 return NULL;
454}
455
Eric Anholt10a68522010-03-26 11:53:37 -0700456ir_rvalue *
457do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
458 ir_rvalue *lhs, ir_rvalue *rhs,
459 YYLTYPE lhs_loc)
460{
461 bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
462
463 if (!error_emitted) {
464 /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
465 if (!lhs->is_lvalue()) {
466 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
467 error_emitted = true;
468 }
469 }
470
471 ir_rvalue *new_rhs = validate_assignment(lhs->type, rhs);
472 if (new_rhs == NULL) {
473 _mesa_glsl_error(& lhs_loc, state, "type mismatch");
474 } else {
475 rhs = new_rhs;
Ian Romanick0157f412010-04-02 17:44:39 -0700476
477 /* If the LHS array was not declared with a size, it takes it size from
478 * the RHS. If the LHS is an l-value and a whole array, it must be a
479 * dereference of a variable. Any other case would require that the LHS
480 * is either not an l-value or not a whole array.
481 */
482 if (lhs->type->array_size() == 0) {
483 ir_dereference *const d = lhs->as_dereference();
484
485 assert(d != NULL);
486
487 ir_variable *const var = d->var->as_variable();
488
489 assert(var != NULL);
490
Ian Romanick63f39422010-04-05 14:35:47 -0700491 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
492 /* FINISHME: This should actually log the location of the RHS. */
493 _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
494 "previous access",
495 var->max_array_access);
496 }
497
Ian Romanick0157f412010-04-02 17:44:39 -0700498 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
499 rhs->type->array_size());
500 }
Eric Anholt10a68522010-03-26 11:53:37 -0700501 }
502
503 ir_instruction *tmp = new ir_assignment(lhs, rhs, NULL);
504 instructions->push_tail(tmp);
505
506 return rhs;
507}
Ian Romanick0bb1c3c2010-03-23 13:23:31 -0700508
Ian Romanick5185a5f2010-03-29 15:20:42 -0700509
510/**
511 * Generate a new temporary and add its declaration to the instruction stream
512 */
513static ir_variable *
514generate_temporary(const glsl_type *type, exec_list *instructions,
515 struct _mesa_glsl_parse_state *state)
516{
517 char *name = (char *) malloc(sizeof(char) * 13);
518
519 snprintf(name, 13, "tmp_%08X", state->temp_index);
520 state->temp_index++;
521
522 ir_variable *const var = new ir_variable(type, name);
523 instructions->push_tail(var);
524
525 return var;
526}
527
528
Eric Anholtde38f0e2010-03-26 12:14:54 -0700529static ir_rvalue *
530get_lvalue_copy(exec_list *instructions, struct _mesa_glsl_parse_state *state,
531 ir_rvalue *lvalue, YYLTYPE loc)
532{
533 ir_variable *var;
534 ir_rvalue *var_deref;
535
536 /* FINISHME: Give unique names to the temporaries. */
537 var = new ir_variable(lvalue->type, "_internal_tmp");
538 var->mode = ir_var_auto;
539
540 var_deref = new ir_dereference(var);
541 do_assignment(instructions, state, var_deref, lvalue, loc);
542
543 /* Once we've created this temporary, mark it read only so it's no
544 * longer considered an lvalue.
545 */
546 var->read_only = true;
547
548 return var_deref;
549}
550
551
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700552ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -0800553ast_node::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -0800554 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -0800555{
Ian Romanick18238de2010-03-01 13:49:10 -0800556 (void) instructions;
557 (void) state;
558
559 return NULL;
560}
561
562
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700563ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -0800564ast_expression::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -0800565 struct _mesa_glsl_parse_state *state)
566{
Ian Romanicka87ac252010-02-22 13:19:34 -0800567 static const int operations[AST_NUM_OPERATORS] = {
568 -1, /* ast_assign doesn't convert to ir_expression. */
569 -1, /* ast_plus doesn't convert to ir_expression. */
570 ir_unop_neg,
571 ir_binop_add,
572 ir_binop_sub,
573 ir_binop_mul,
574 ir_binop_div,
575 ir_binop_mod,
576 ir_binop_lshift,
577 ir_binop_rshift,
578 ir_binop_less,
579 ir_binop_greater,
580 ir_binop_lequal,
581 ir_binop_gequal,
582 ir_binop_equal,
583 ir_binop_nequal,
584 ir_binop_bit_and,
585 ir_binop_bit_xor,
586 ir_binop_bit_or,
587 ir_unop_bit_not,
588 ir_binop_logic_and,
589 ir_binop_logic_xor,
590 ir_binop_logic_or,
591 ir_unop_logic_not,
592
593 /* Note: The following block of expression types actually convert
594 * to multiple IR instructions.
595 */
596 ir_binop_mul, /* ast_mul_assign */
597 ir_binop_div, /* ast_div_assign */
598 ir_binop_mod, /* ast_mod_assign */
599 ir_binop_add, /* ast_add_assign */
600 ir_binop_sub, /* ast_sub_assign */
601 ir_binop_lshift, /* ast_ls_assign */
602 ir_binop_rshift, /* ast_rs_assign */
603 ir_binop_bit_and, /* ast_and_assign */
604 ir_binop_bit_xor, /* ast_xor_assign */
605 ir_binop_bit_or, /* ast_or_assign */
606
607 -1, /* ast_conditional doesn't convert to ir_expression. */
Eric Anholtde38f0e2010-03-26 12:14:54 -0700608 ir_binop_add, /* ast_pre_inc. */
609 ir_binop_sub, /* ast_pre_dec. */
610 ir_binop_add, /* ast_post_inc. */
611 ir_binop_sub, /* ast_post_dec. */
Ian Romanicka87ac252010-02-22 13:19:34 -0800612 -1, /* ast_field_selection doesn't conv to ir_expression. */
613 -1, /* ast_array_index doesn't convert to ir_expression. */
614 -1, /* ast_function_call doesn't conv to ir_expression. */
615 -1, /* ast_identifier doesn't convert to ir_expression. */
616 -1, /* ast_int_constant doesn't convert to ir_expression. */
617 -1, /* ast_uint_constant doesn't conv to ir_expression. */
618 -1, /* ast_float_constant doesn't conv to ir_expression. */
619 -1, /* ast_bool_constant doesn't conv to ir_expression. */
620 -1, /* ast_sequence doesn't convert to ir_expression. */
621 };
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700622 ir_rvalue *result = NULL;
623 ir_rvalue *op[2];
Ian Romanick0471e8b2010-03-26 14:33:41 -0700624 const struct glsl_type *type = glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800625 bool error_emitted = false;
626 YYLTYPE loc;
627
Ian Romanick18238de2010-03-01 13:49:10 -0800628 loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -0800629
Ian Romanick18238de2010-03-01 13:49:10 -0800630 switch (this->oper) {
Ian Romanick6652af32010-03-09 16:38:02 -0800631 case ast_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -0800632 op[0] = this->subexpressions[0]->hir(instructions, state);
633 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800634
Eric Anholt10a68522010-03-26 11:53:37 -0700635 result = do_assignment(instructions, state, op[0], op[1],
636 this->subexpressions[0]->get_location());
637 error_emitted = result->type->is_error();
638 type = result->type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800639 break;
Ian Romanick6652af32010-03-09 16:38:02 -0800640 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800641
642 case ast_plus:
Ian Romanick18238de2010-03-01 13:49:10 -0800643 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800644
Ian Romanicka43817a2010-03-26 14:27:23 -0700645 error_emitted = op[0]->type->is_error();
646 if (type->is_error())
Ian Romanicka87ac252010-02-22 13:19:34 -0800647 op[0]->type = type;
648
649 result = op[0];
650 break;
651
652 case ast_neg:
Ian Romanick18238de2010-03-01 13:49:10 -0800653 op[0] = this->subexpressions[0]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800654
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000655 type = unary_arithmetic_result_type(op[0]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800656
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000657 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800658
Ian Romanick18238de2010-03-01 13:49:10 -0800659 result = new ir_expression(operations[this->oper], type,
Ian Romanicka87ac252010-02-22 13:19:34 -0800660 op[0], NULL);
661 break;
662
663 case ast_add:
664 case ast_sub:
665 case ast_mul:
666 case ast_div:
Ian Romanick18238de2010-03-01 13:49:10 -0800667 op[0] = this->subexpressions[0]->hir(instructions, state);
668 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800669
Ian Romanickbfb09c22010-03-29 16:32:55 -0700670 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -0800671 (this->oper == ast_mul),
Eric Anholta13bb142010-03-31 16:38:11 -1000672 state, & loc);
673 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800674
Ian Romanick18238de2010-03-01 13:49:10 -0800675 result = new ir_expression(operations[this->oper], type,
Ian Romanicka87ac252010-02-22 13:19:34 -0800676 op[0], op[1]);
677 break;
678
679 case ast_mod:
Ian Romanick18238de2010-03-01 13:49:10 -0800680 op[0] = this->subexpressions[0]->hir(instructions, state);
681 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800682
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000683 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800684
Ian Romanick18238de2010-03-01 13:49:10 -0800685 assert(operations[this->oper] == ir_binop_mod);
Ian Romanicka87ac252010-02-22 13:19:34 -0800686
Ian Romanick18238de2010-03-01 13:49:10 -0800687 result = new ir_expression(operations[this->oper], type,
Ian Romanicka87ac252010-02-22 13:19:34 -0800688 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000689 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800690 break;
691
692 case ast_lshift:
693 case ast_rshift:
Eric Anholt183d8c62010-03-31 16:53:47 -1000694 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
695 error_emitted = true;
Ian Romanicka87ac252010-02-22 13:19:34 -0800696 break;
697
698 case ast_less:
699 case ast_greater:
700 case ast_lequal:
701 case ast_gequal:
Ian Romanick18238de2010-03-01 13:49:10 -0800702 op[0] = this->subexpressions[0]->hir(instructions, state);
703 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800704
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000705 type = relational_result_type(op[0], op[1], state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800706
707 /* The relational operators must either generate an error or result
708 * in a scalar boolean. See page 57 of the GLSL 1.50 spec.
709 */
Ian Romanicka43817a2010-03-26 14:27:23 -0700710 assert(type->is_error()
Ian Romanicka87ac252010-02-22 13:19:34 -0800711 || ((type->base_type == GLSL_TYPE_BOOL)
Ian Romanickcb36f8a2010-03-09 15:51:22 -0800712 && type->is_scalar()));
Ian Romanicka87ac252010-02-22 13:19:34 -0800713
Ian Romanick18238de2010-03-01 13:49:10 -0800714 result = new ir_expression(operations[this->oper], type,
Ian Romanicka87ac252010-02-22 13:19:34 -0800715 op[0], op[1]);
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000716 error_emitted = type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -0800717 break;
718
719 case ast_nequal:
720 case ast_equal:
Ian Romanick6e659ca2010-03-29 15:11:05 -0700721 op[0] = this->subexpressions[0]->hir(instructions, state);
722 op[1] = this->subexpressions[1]->hir(instructions, state);
723
724 /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
725 *
726 * "The equality operators equal (==), and not equal (!=)
727 * operate on all types. They result in a scalar Boolean. If
728 * the operand types do not match, then there must be a
729 * conversion from Section 4.1.10 "Implicit Conversions"
730 * applied to one operand that can make them match, in which
731 * case this conversion is done."
732 */
Ian Romanickbfb09c22010-03-29 16:32:55 -0700733 if ((!apply_implicit_conversion(op[0]->type, op[1], state)
734 && !apply_implicit_conversion(op[1]->type, op[0], state))
Ian Romanick212b0322010-03-29 16:22:38 -0700735 || (op[0]->type != op[1]->type)) {
Ian Romanick6e659ca2010-03-29 15:11:05 -0700736 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
737 "type", (this->oper == ast_equal) ? "==" : "!=");
738 error_emitted = true;
Ian Romanicka80cbd62010-03-30 17:04:48 -0700739 } else if ((state->language_version <= 110)
740 && (op[0]->type->is_array() || op[1]->type->is_array())) {
741 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
742 "GLSL 1.10");
743 error_emitted = true;
Ian Romanick6e659ca2010-03-29 15:11:05 -0700744 }
745
746 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
747 op[0], op[1]);
748 type = glsl_type::bool_type;
749
750 assert(result->type == glsl_type::bool_type);
Ian Romanicka87ac252010-02-22 13:19:34 -0800751 break;
752
753 case ast_bit_and:
754 case ast_bit_xor:
755 case ast_bit_or:
756 case ast_bit_not:
Eric Anholt183d8c62010-03-31 16:53:47 -1000757 _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
758 error_emitted = true;
Ian Romanicka87ac252010-02-22 13:19:34 -0800759 break;
760
Eric Anholt4950a682010-04-16 01:10:32 -0700761 case ast_logic_and: {
Eric Anholtb82c0c32010-03-31 09:11:39 -1000762 op[0] = this->subexpressions[0]->hir(instructions, state);
Eric Anholtb82c0c32010-03-31 09:11:39 -1000763
764 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
765 YYLTYPE loc = this->subexpressions[0]->get_location();
766
767 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
768 operator_string(this->oper));
Eric Anholtebbf14b2010-03-31 17:05:32 -1000769 error_emitted = true;
Eric Anholtb82c0c32010-03-31 09:11:39 -1000770 }
771
Eric Anholt44b694e2010-04-16 13:49:04 -0700772 ir_constant *op0_const = op[0]->constant_expression_value();
773 if (op0_const) {
774 if (op0_const->value.b[0]) {
775 op[1] = this->subexpressions[1]->hir(instructions, state);
Eric Anholt4950a682010-04-16 01:10:32 -0700776
Eric Anholt44b694e2010-04-16 13:49:04 -0700777 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
778 YYLTYPE loc = this->subexpressions[1]->get_location();
Eric Anholt4950a682010-04-16 01:10:32 -0700779
Eric Anholt44b694e2010-04-16 13:49:04 -0700780 _mesa_glsl_error(& loc, state,
781 "RHS of `%s' must be scalar boolean",
782 operator_string(this->oper));
783 error_emitted = true;
784 }
785 result = op[1];
786 } else {
787 result = op0_const;
788 }
789 type = glsl_type::bool_type;
790 } else {
791 ir_if *const stmt = new ir_if(op[0]);
792 instructions->push_tail(stmt);
Eric Anholt4950a682010-04-16 01:10:32 -0700793
Eric Anholt44b694e2010-04-16 13:49:04 -0700794 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
Eric Anholtb82c0c32010-03-31 09:11:39 -1000795
Eric Anholt44b694e2010-04-16 13:49:04 -0700796 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
797 YYLTYPE loc = this->subexpressions[1]->get_location();
798
799 _mesa_glsl_error(& loc, state,
800 "RHS of `%s' must be scalar boolean",
801 operator_string(this->oper));
802 error_emitted = true;
803 }
804
805 ir_variable *const tmp = generate_temporary(glsl_type::bool_type,
806 instructions, state);
807
808 ir_dereference *const then_deref = new ir_dereference(tmp);
809 ir_assignment *const then_assign =
810 new ir_assignment(then_deref, op[1], NULL);
811 stmt->then_instructions.push_tail(then_assign);
812
813 ir_dereference *const else_deref = new ir_dereference(tmp);
814 ir_assignment *const else_assign =
815 new ir_assignment(else_deref, new ir_constant(false), NULL);
816 stmt->else_instructions.push_tail(else_assign);
817
818 result = new ir_dereference(tmp);
819 type = tmp->type;
Eric Anholtb82c0c32010-03-31 09:11:39 -1000820 }
Eric Anholt4950a682010-04-16 01:10:32 -0700821 break;
822 }
823
824 case ast_logic_or: {
825 op[0] = this->subexpressions[0]->hir(instructions, state);
826
827 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
828 YYLTYPE loc = this->subexpressions[0]->get_location();
829
830 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
831 operator_string(this->oper));
832 error_emitted = true;
833 }
834
Eric Anholt44b694e2010-04-16 13:49:04 -0700835 ir_constant *op0_const = op[0]->constant_expression_value();
836 if (op0_const) {
837 if (op0_const->value.b[0]) {
838 result = op0_const;
839 } else {
840 op[1] = this->subexpressions[1]->hir(instructions, state);
Eric Anholt4950a682010-04-16 01:10:32 -0700841
Eric Anholt44b694e2010-04-16 13:49:04 -0700842 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
843 YYLTYPE loc = this->subexpressions[1]->get_location();
Eric Anholt4950a682010-04-16 01:10:32 -0700844
Eric Anholt44b694e2010-04-16 13:49:04 -0700845 _mesa_glsl_error(& loc, state,
846 "RHS of `%s' must be scalar boolean",
847 operator_string(this->oper));
848 error_emitted = true;
849 }
850 result = op[1];
851 }
852 type = glsl_type::bool_type;
853 } else {
854 ir_if *const stmt = new ir_if(op[0]);
855 instructions->push_tail(stmt);
Eric Anholt4950a682010-04-16 01:10:32 -0700856
Eric Anholt44b694e2010-04-16 13:49:04 -0700857 ir_variable *const tmp = generate_temporary(glsl_type::bool_type,
858 instructions, state);
Eric Anholt4950a682010-04-16 01:10:32 -0700859
Eric Anholt44b694e2010-04-16 13:49:04 -0700860 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
861
862 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
863 YYLTYPE loc = this->subexpressions[1]->get_location();
864
865 _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
866 operator_string(this->oper));
867 error_emitted = true;
868 }
869
870 ir_dereference *const then_deref = new ir_dereference(tmp);
871 ir_assignment *const then_assign =
872 new ir_assignment(then_deref, new ir_constant(true), NULL);
873 stmt->then_instructions.push_tail(then_assign);
874
875 ir_dereference *const else_deref = new ir_dereference(tmp);
876 ir_assignment *const else_assign =
877 new ir_assignment(else_deref, op[1], NULL);
878 stmt->else_instructions.push_tail(else_assign);
879
880 result = new ir_dereference(tmp);
881 type = tmp->type;
Eric Anholt4950a682010-04-16 01:10:32 -0700882 }
Eric Anholt4950a682010-04-16 01:10:32 -0700883 break;
884 }
885
886 case ast_logic_xor:
887 op[0] = this->subexpressions[0]->hir(instructions, state);
888 op[1] = this->subexpressions[1]->hir(instructions, state);
889
890
Eric Anholtb82c0c32010-03-31 09:11:39 -1000891 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
892 op[0], op[1]);
Eric Anholtebbf14b2010-03-31 17:05:32 -1000893 type = glsl_type::bool_type;
Ian Romanicka87ac252010-02-22 13:19:34 -0800894 break;
895
Eric Anholta5827fe2010-03-31 16:50:55 -1000896 case ast_logic_not:
897 op[0] = this->subexpressions[0]->hir(instructions, state);
898
899 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
900 YYLTYPE loc = this->subexpressions[0]->get_location();
901
902 _mesa_glsl_error(& loc, state,
903 "operand of `!' must be scalar boolean");
Eric Anholtebbf14b2010-03-31 17:05:32 -1000904 error_emitted = true;
Eric Anholta5827fe2010-03-31 16:50:55 -1000905 }
906
907 result = new ir_expression(operations[this->oper], glsl_type::bool_type,
908 op[0], NULL);
Eric Anholtebbf14b2010-03-31 17:05:32 -1000909 type = glsl_type::bool_type;
Eric Anholta5827fe2010-03-31 16:50:55 -1000910 break;
911
Ian Romanicka87ac252010-02-22 13:19:34 -0800912 case ast_mul_assign:
913 case ast_div_assign:
914 case ast_add_assign:
915 case ast_sub_assign: {
Ian Romanick18238de2010-03-01 13:49:10 -0800916 op[0] = this->subexpressions[0]->hir(instructions, state);
917 op[1] = this->subexpressions[1]->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -0800918
Ian Romanickbfb09c22010-03-29 16:32:55 -0700919 type = arithmetic_result_type(op[0], op[1],
Ian Romanick18238de2010-03-01 13:49:10 -0800920 (this->oper == ast_mul_assign),
Eric Anholta13bb142010-03-31 16:38:11 -1000921 state, & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -0800922
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700923 ir_rvalue *temp_rhs = new ir_expression(operations[this->oper], type,
924 op[0], op[1]);
Ian Romanicka87ac252010-02-22 13:19:34 -0800925
Eric Anholt10a68522010-03-26 11:53:37 -0700926 result = do_assignment(instructions, state, op[0], temp_rhs,
927 this->subexpressions[0]->get_location());
928 type = result->type;
929 error_emitted = (op[0]->type->is_error());
Ian Romanicka87ac252010-02-22 13:19:34 -0800930
931 /* GLSL 1.10 does not allow array assignment. However, we don't have to
932 * explicitly test for this because none of the binary expression
933 * operators allow array operands either.
934 */
935
Ian Romanicka87ac252010-02-22 13:19:34 -0800936 break;
937 }
938
Eric Anholt48a0e642010-03-26 11:57:46 -0700939 case ast_mod_assign: {
940 op[0] = this->subexpressions[0]->hir(instructions, state);
941 op[1] = this->subexpressions[1]->hir(instructions, state);
942
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000943 type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
Eric Anholt48a0e642010-03-26 11:57:46 -0700944
945 assert(operations[this->oper] == ir_binop_mod);
946
947 struct ir_rvalue *temp_rhs;
948 temp_rhs = new ir_expression(operations[this->oper], type,
949 op[0], op[1]);
950
951 result = do_assignment(instructions, state, op[0], temp_rhs,
952 this->subexpressions[0]->get_location());
953 type = result->type;
Eric Anholt65e1a7ac2010-03-31 16:45:20 -1000954 error_emitted = type->is_error();
Eric Anholt48a0e642010-03-26 11:57:46 -0700955 break;
956 }
Ian Romanicka87ac252010-02-22 13:19:34 -0800957
958 case ast_ls_assign:
959 case ast_rs_assign:
Eric Anholt183d8c62010-03-31 16:53:47 -1000960 _mesa_glsl_error(& loc, state,
961 "FINISHME: implement bit-shift assignment operators");
962 error_emitted = true;
Ian Romanick251eb752010-03-29 14:15:05 -0700963 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800964
965 case ast_and_assign:
966 case ast_xor_assign:
967 case ast_or_assign:
Eric Anholt183d8c62010-03-31 16:53:47 -1000968 _mesa_glsl_error(& loc, state,
969 "FINISHME: implement logic assignment operators");
970 error_emitted = true;
Ian Romanick251eb752010-03-29 14:15:05 -0700971 break;
Ian Romanicka87ac252010-02-22 13:19:34 -0800972
Ian Romanick96f9cea2010-03-29 15:33:54 -0700973 case ast_conditional: {
974 op[0] = this->subexpressions[0]->hir(instructions, state);
975
976 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
977 *
978 * "The ternary selection operator (?:). It operates on three
979 * expressions (exp1 ? exp2 : exp3). This operator evaluates the
980 * first expression, which must result in a scalar Boolean."
981 */
982 if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
983 YYLTYPE loc = this->subexpressions[0]->get_location();
984
985 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
986 error_emitted = true;
987 }
988
989 /* The :? operator is implemented by generating an anonymous temporary
990 * followed by an if-statement. The last instruction in each branch of
991 * the if-statement assigns a value to the anonymous temporary. This
992 * temporary is the r-value of the expression.
993 */
994 ir_variable *const tmp = generate_temporary(glsl_type::error_type,
995 instructions, state);
996
997 ir_if *const stmt = new ir_if(op[0]);
998 instructions->push_tail(stmt);
999
1000 op[1] = this->subexpressions[1]->hir(& stmt->then_instructions, state);
1001 ir_dereference *const then_deref = new ir_dereference(tmp);
1002 ir_assignment *const then_assign =
1003 new ir_assignment(then_deref, op[1], NULL);
1004 stmt->then_instructions.push_tail(then_assign);
1005
1006 op[2] = this->subexpressions[2]->hir(& stmt->else_instructions, state);
1007 ir_dereference *const else_deref = new ir_dereference(tmp);
1008 ir_assignment *const else_assign =
1009 new ir_assignment(else_deref, op[2], NULL);
1010 stmt->else_instructions.push_tail(else_assign);
1011
1012 /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
1013 *
1014 * "The second and third expressions can be any type, as
1015 * long their types match, or there is a conversion in
1016 * Section 4.1.10 "Implicit Conversions" that can be applied
1017 * to one of the expressions to make their types match. This
1018 * resulting matching type is the type of the entire
1019 * expression."
1020 */
Ian Romanickbfb09c22010-03-29 16:32:55 -07001021 if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1022 && !apply_implicit_conversion(op[2]->type, op[1], state))
Ian Romanickdb9be2e2010-03-29 16:25:56 -07001023 || (op[1]->type != op[2]->type)) {
Ian Romanick96f9cea2010-03-29 15:33:54 -07001024 YYLTYPE loc = this->subexpressions[1]->get_location();
1025
1026 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1027 "operator must have matching types.");
1028 error_emitted = true;
Ian Romanickdb9be2e2010-03-29 16:25:56 -07001029 } else {
1030 tmp->type = op[1]->type;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001031 }
1032
Ian Romanick96f9cea2010-03-29 15:33:54 -07001033 result = new ir_dereference(tmp);
1034 type = tmp->type;
Ian Romanick251eb752010-03-29 14:15:05 -07001035 break;
Ian Romanick96f9cea2010-03-29 15:33:54 -07001036 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001037
1038 case ast_pre_inc:
Eric Anholt76ea56c2010-03-26 12:16:54 -07001039 case ast_pre_dec: {
1040 op[0] = this->subexpressions[0]->hir(instructions, state);
1041 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
1042 op[1] = new ir_constant(1.0f);
1043 else
1044 op[1] = new ir_constant(1);
1045
Eric Anholta13bb142010-03-31 16:38:11 -10001046 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholt76ea56c2010-03-26 12:16:54 -07001047
1048 struct ir_rvalue *temp_rhs;
1049 temp_rhs = new ir_expression(operations[this->oper], type,
1050 op[0], op[1]);
1051
1052 result = do_assignment(instructions, state, op[0], temp_rhs,
1053 this->subexpressions[0]->get_location());
1054 type = result->type;
1055 error_emitted = op[0]->type->is_error();
1056 break;
1057 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001058
1059 case ast_post_inc:
Eric Anholtde38f0e2010-03-26 12:14:54 -07001060 case ast_post_dec: {
1061 op[0] = this->subexpressions[0]->hir(instructions, state);
1062 if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
1063 op[1] = new ir_constant(1.0f);
1064 else
1065 op[1] = new ir_constant(1);
1066
1067 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1068
Eric Anholta13bb142010-03-31 16:38:11 -10001069 type = arithmetic_result_type(op[0], op[1], false, state, & loc);
Eric Anholtde38f0e2010-03-26 12:14:54 -07001070
1071 struct ir_rvalue *temp_rhs;
1072 temp_rhs = new ir_expression(operations[this->oper], type,
1073 op[0], op[1]);
1074
1075 /* Get a temporary of a copy of the lvalue before it's modified.
1076 * This may get thrown away later.
1077 */
1078 result = get_lvalue_copy(instructions, state, op[0],
1079 this->subexpressions[0]->get_location());
1080
1081 (void)do_assignment(instructions, state, op[0], temp_rhs,
1082 this->subexpressions[0]->get_location());
1083
1084 type = result->type;
1085 error_emitted = op[0]->type->is_error();
Ian Romanicka87ac252010-02-22 13:19:34 -08001086 break;
Eric Anholtde38f0e2010-03-26 12:14:54 -07001087 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001088
1089 case ast_field_selection:
Ian Romanick18238de2010-03-01 13:49:10 -08001090 result = _mesa_ast_field_selection_to_hir(this, instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001091 type = result->type;
1092 break;
1093
Ian Romanick27e3cf82010-04-01 18:03:59 -07001094 case ast_array_index: {
1095 YYLTYPE index_loc = subexpressions[1]->get_location();
1096
1097 op[0] = subexpressions[0]->hir(instructions, state);
1098 op[1] = subexpressions[1]->hir(instructions, state);
1099
1100 error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1101
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001102 ir_dereference *const lhs = op[0]->as_dereference();
1103 ir_instruction *array;
1104 if ((lhs != NULL)
1105 && (lhs->mode == ir_dereference::ir_reference_variable)) {
1106 result = new ir_dereference(lhs->var, op[1]);
1107
1108 delete op[0];
1109 array = lhs->var;
1110 } else {
1111 result = new ir_dereference(op[0], op[1]);
1112 array = op[0];
1113 }
1114
1115 /* Do not use op[0] after this point. Use array.
1116 */
1117 op[0] = NULL;
1118
Ian Romanick27e3cf82010-04-01 18:03:59 -07001119
1120 if (error_emitted)
1121 break;
1122
Ian Romanick63038e12010-04-05 13:16:00 -07001123 if (!array->type->is_array()
1124 && !array->type->is_matrix()
1125 && !array->type->is_vector()) {
Ian Romanick27e3cf82010-04-01 18:03:59 -07001126 _mesa_glsl_error(& index_loc, state,
Ian Romanick63038e12010-04-05 13:16:00 -07001127 "cannot dereference non-array / non-matrix / "
1128 "non-vector");
Ian Romanick27e3cf82010-04-01 18:03:59 -07001129 error_emitted = true;
1130 }
1131
1132 if (!op[1]->type->is_integer()) {
1133 _mesa_glsl_error(& index_loc, state,
1134 "array index must be integer type");
1135 error_emitted = true;
1136 } else if (!op[1]->type->is_scalar()) {
1137 _mesa_glsl_error(& index_loc, state,
1138 "array index must be scalar");
1139 error_emitted = true;
1140 }
1141
1142 /* If the array index is a constant expression and the array has a
1143 * declared size, ensure that the access is in-bounds. If the array
1144 * index is not a constant expression, ensure that the array has a
1145 * declared size.
1146 */
1147 ir_constant *const const_index = op[1]->constant_expression_value();
1148 if (const_index != NULL) {
1149 const int idx = const_index->value.i[0];
Ian Romanick63038e12010-04-05 13:16:00 -07001150 const char *type_name;
1151 unsigned bound = 0;
1152
1153 if (array->type->is_matrix()) {
1154 type_name = "matrix";
1155 } else if (array->type->is_vector()) {
1156 type_name = "vector";
1157 } else {
1158 type_name = "array";
1159 }
Ian Romanick27e3cf82010-04-01 18:03:59 -07001160
1161 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
1162 *
1163 * "It is illegal to declare an array with a size, and then
1164 * later (in the same shader) index the same array with an
1165 * integral constant expression greater than or equal to the
1166 * declared size. It is also illegal to index an array with a
1167 * negative constant expression."
1168 */
Ian Romanick63038e12010-04-05 13:16:00 -07001169 if (array->type->is_matrix()) {
1170 if (array->type->row_type()->vector_elements <= idx) {
1171 bound = array->type->row_type()->vector_elements;
1172 }
1173 } else if (array->type->is_vector()) {
1174 if (array->type->vector_elements <= idx) {
1175 bound = array->type->vector_elements;
1176 }
1177 } else {
1178 if ((array->type->array_size() > 0)
1179 && (array->type->array_size() <= idx)) {
1180 bound = array->type->array_size();
1181 }
1182 }
1183
1184 if (bound > 0) {
1185 _mesa_glsl_error(& loc, state, "%s index must be < %u",
1186 type_name, bound);
1187 error_emitted = true;
1188 } else if (idx < 0) {
1189 _mesa_glsl_error(& loc, state, "%s index must be >= 0",
1190 type_name);
Ian Romanick27e3cf82010-04-01 18:03:59 -07001191 error_emitted = true;
1192 }
1193
Ian Romanick63038e12010-04-05 13:16:00 -07001194 if (array->type->is_array()) {
1195 ir_variable *const v = array->as_variable();
1196 if ((v != NULL) && (unsigned(idx) > v->max_array_access))
1197 v->max_array_access = idx;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001198 }
1199 }
1200
1201 if (error_emitted)
1202 result->type = glsl_type::error_type;
1203
1204 type = result->type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001205 break;
Ian Romanick27e3cf82010-04-01 18:03:59 -07001206 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001207
1208 case ast_function_call:
Ian Romanick7cfddf12010-03-10 13:26:52 -08001209 /* Should *NEVER* get here. ast_function_call should always be handled
1210 * by ast_function_expression::hir.
Ian Romanicka87ac252010-02-22 13:19:34 -08001211 */
Ian Romanick7cfddf12010-03-10 13:26:52 -08001212 assert(0);
Ian Romanicka87ac252010-02-22 13:19:34 -08001213 break;
1214
1215 case ast_identifier: {
1216 /* ast_identifier can appear several places in a full abstract syntax
1217 * tree. This particular use must be at location specified in the grammar
1218 * as 'variable_identifier'.
1219 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001220 ir_variable *var =
1221 state->symbols->get_variable(this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001222
1223 result = new ir_dereference(var);
1224
1225 if (var != NULL) {
1226 type = result->type;
1227 } else {
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001228 _mesa_glsl_error(& loc, state, "`%s' undeclared",
Ian Romanick18238de2010-03-01 13:49:10 -08001229 this->primary_expression.identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001230
1231 error_emitted = true;
1232 }
1233 break;
1234 }
1235
1236 case ast_int_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001237 type = glsl_type::int_type;
Ian Romanick18238de2010-03-01 13:49:10 -08001238 result = new ir_constant(type, & this->primary_expression);
Ian Romanicka87ac252010-02-22 13:19:34 -08001239 break;
1240
1241 case ast_uint_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001242 type = glsl_type::uint_type;
Ian Romanick18238de2010-03-01 13:49:10 -08001243 result = new ir_constant(type, & this->primary_expression);
Ian Romanicka87ac252010-02-22 13:19:34 -08001244 break;
1245
1246 case ast_float_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001247 type = glsl_type::float_type;
Ian Romanick18238de2010-03-01 13:49:10 -08001248 result = new ir_constant(type, & this->primary_expression);
Ian Romanicka87ac252010-02-22 13:19:34 -08001249 break;
1250
1251 case ast_bool_constant:
Ian Romanick0471e8b2010-03-26 14:33:41 -07001252 type = glsl_type::bool_type;
Ian Romanick18238de2010-03-01 13:49:10 -08001253 result = new ir_constant(type, & this->primary_expression);
Ian Romanicka87ac252010-02-22 13:19:34 -08001254 break;
1255
1256 case ast_sequence: {
Ian Romanicka87ac252010-02-22 13:19:34 -08001257 /* It should not be possible to generate a sequence in the AST without
1258 * any expressions in it.
1259 */
Ian Romanick304ea902010-05-10 11:17:53 -07001260 assert(!this->expressions.is_empty());
Ian Romanicka87ac252010-02-22 13:19:34 -08001261
1262 /* The r-value of a sequence is the last expression in the sequence. If
1263 * the other expressions in the sequence do not have side-effects (and
1264 * therefore add instructions to the instruction list), they get dropped
1265 * on the floor.
1266 */
Ian Romanick304ea902010-05-10 11:17:53 -07001267 foreach_list (n, &this->expressions) {
1268 ast_node *ast = exec_node_data(ast_node, n, link);
1269 result = ast->hir(instructions, state);
1270 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001271
1272 type = result->type;
1273
1274 /* Any errors should have already been emitted in the loop above.
1275 */
1276 error_emitted = true;
1277 break;
1278 }
1279 }
1280
Ian Romanickcef3bae2010-03-26 14:41:32 -07001281 if (type->is_error() && !error_emitted)
Ian Romanick71d0bbf2010-03-23 13:21:19 -07001282 _mesa_glsl_error(& loc, state, "type mismatch");
Ian Romanicka87ac252010-02-22 13:19:34 -08001283
1284 return result;
1285}
1286
1287
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001288ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001289ast_expression_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001290 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001291{
Ian Romanicka87ac252010-02-22 13:19:34 -08001292 /* It is possible to have expression statements that don't have an
1293 * expression. This is the solitary semicolon:
1294 *
1295 * for (i = 0; i < 5; i++)
1296 * ;
1297 *
1298 * In this case the expression will be NULL. Test for NULL and don't do
1299 * anything in that case.
1300 */
Ian Romanick18238de2010-03-01 13:49:10 -08001301 if (expression != NULL)
1302 expression->hir(instructions, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001303
1304 /* Statements do not have r-values.
1305 */
1306 return NULL;
1307}
1308
1309
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001310ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001311ast_compound_statement::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001312 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001313{
Ian Romanick18238de2010-03-01 13:49:10 -08001314 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001315 state->symbols->push_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001316
Ian Romanick304ea902010-05-10 11:17:53 -07001317 foreach_list (n, &this->statements) {
1318 ast_node *ast = exec_node_data(ast_node, n, link);
1319 ast->hir(instructions, state);
1320 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001321
Ian Romanick18238de2010-03-01 13:49:10 -08001322 if (new_scope)
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001323 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08001324
1325 /* Compound statements do not have r-values.
1326 */
1327 return NULL;
1328}
1329
1330
Ian Romanick28009cd2010-03-30 16:59:27 -07001331static const glsl_type *
1332process_array_type(const glsl_type *base, ast_node *array_size,
1333 struct _mesa_glsl_parse_state *state)
1334{
1335 unsigned length = 0;
1336
1337 /* FINISHME: Reject delcarations of multidimensional arrays. */
1338
1339 if (array_size != NULL) {
1340 exec_list dummy_instructions;
1341 ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
1342 YYLTYPE loc = array_size->get_location();
1343
1344 /* FINISHME: Verify that the grammar forbids side-effects in array
1345 * FINISHME: sizes. i.e., 'vec4 [x = 12] data'
1346 */
1347 assert(dummy_instructions.is_empty());
1348
1349 if (ir != NULL) {
1350 if (!ir->type->is_integer()) {
1351 _mesa_glsl_error(& loc, state, "array size must be integer type");
1352 } else if (!ir->type->is_scalar()) {
1353 _mesa_glsl_error(& loc, state, "array size must be scalar type");
1354 } else {
1355 ir_constant *const size = ir->constant_expression_value();
1356
1357 if (size == NULL) {
1358 _mesa_glsl_error(& loc, state, "array size must be a "
1359 "constant valued expression");
1360 } else if (size->value.i[0] <= 0) {
1361 _mesa_glsl_error(& loc, state, "array size must be > 0");
1362 } else {
1363 assert(size->type == ir->type);
1364 length = size->value.u[0];
1365 }
1366 }
1367 }
1368 }
1369
1370 return glsl_type::get_array_instance(base, length);
1371}
1372
1373
Ian Romanickd612a122010-03-31 16:22:06 -07001374const glsl_type *
1375ast_type_specifier::glsl_type(const char **name,
1376 struct _mesa_glsl_parse_state *state) const
Ian Romanicka87ac252010-02-22 13:19:34 -08001377{
Ian Romanickd612a122010-03-31 16:22:06 -07001378 const struct glsl_type *type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001379
Ian Romanick3455ce62010-04-19 15:13:15 -07001380 if ((this->type_specifier == ast_struct) && (this->type_name == NULL)) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001381 /* FINISHME: Handle annonymous structures. */
1382 type = NULL;
1383 } else {
Ian Romanickd612a122010-03-31 16:22:06 -07001384 type = state->symbols->get_type(this->type_name);
1385 *name = this->type_name;
Ian Romanicka87ac252010-02-22 13:19:34 -08001386
Ian Romanickd612a122010-03-31 16:22:06 -07001387 if (this->is_array) {
1388 type = process_array_type(type, this->array_size, state);
Ian Romanick28009cd2010-03-30 16:59:27 -07001389 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001390 }
1391
1392 return type;
1393}
1394
1395
1396static void
1397apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1398 struct ir_variable *var,
Eric Anholt2e063f12010-03-28 00:56:22 -07001399 struct _mesa_glsl_parse_state *state,
1400 YYLTYPE *loc)
Ian Romanicka87ac252010-02-22 13:19:34 -08001401{
1402 if (qual->invariant)
1403 var->invariant = 1;
1404
1405 /* FINISHME: Mark 'in' variables at global scope as read-only. */
1406 if (qual->constant || qual->attribute || qual->uniform
1407 || (qual->varying && (state->target == fragment_shader)))
1408 var->read_only = 1;
1409
1410 if (qual->centroid)
1411 var->centroid = 1;
1412
Ian Romanickae4c4c02010-04-07 16:41:40 -07001413 if (qual->attribute && state->target != vertex_shader) {
Eric Anholt2e063f12010-03-28 00:56:22 -07001414 var->type = glsl_type::error_type;
1415 _mesa_glsl_error(loc, state,
1416 "`attribute' variables may not be declared in the "
Ian Romanickae4c4c02010-04-07 16:41:40 -07001417 "%s shader",
1418 _mesa_glsl_shader_target_name(state->target));
Eric Anholt2e063f12010-03-28 00:56:22 -07001419 }
1420
Eric Anholt90b78252010-03-31 21:21:20 -10001421 /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
1422 *
1423 * "The varying qualifier can be used only with the data types
1424 * float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
1425 * these."
1426 */
1427 if (qual->varying && var->type->base_type != GLSL_TYPE_FLOAT) {
1428 var->type = glsl_type::error_type;
1429 _mesa_glsl_error(loc, state,
1430 "varying variables must be of base type float");
1431 }
1432
Ian Romanicka87ac252010-02-22 13:19:34 -08001433 if (qual->in && qual->out)
1434 var->mode = ir_var_inout;
1435 else if (qual->attribute || qual->in
1436 || (qual->varying && (state->target == fragment_shader)))
1437 var->mode = ir_var_in;
Ian Romanick0b678232010-03-10 00:28:59 -08001438 else if (qual->out || (qual->varying && (state->target == vertex_shader)))
Ian Romanicka87ac252010-02-22 13:19:34 -08001439 var->mode = ir_var_out;
1440 else if (qual->uniform)
1441 var->mode = ir_var_uniform;
1442 else
1443 var->mode = ir_var_auto;
1444
Eric Anholt71df19f2010-04-19 11:10:37 -07001445 if (qual->uniform)
1446 var->shader_in = true;
1447 if (qual->varying) {
1448 if (qual->in)
1449 var->shader_in = true;
1450 if (qual->out)
1451 var->shader_out = true;
1452 }
1453
Ian Romanicka87ac252010-02-22 13:19:34 -08001454 if (qual->flat)
1455 var->interpolation = ir_var_flat;
1456 else if (qual->noperspective)
1457 var->interpolation = ir_var_noperspective;
1458 else
1459 var->interpolation = ir_var_smooth;
Ian Romanick9d975372010-04-02 17:17:47 -07001460
1461 if (var->type->is_array() && (state->language_version >= 120)) {
1462 var->array_lvalue = true;
1463 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001464}
1465
1466
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001467ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001468ast_declarator_list::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001469 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001470{
Ian Romanicka87ac252010-02-22 13:19:34 -08001471 const struct glsl_type *decl_type;
1472 const char *type_name = NULL;
Eric Anholt85584592010-04-14 15:38:52 -07001473 ir_rvalue *result = NULL;
Ian Romanickc824e352010-04-23 15:55:19 -07001474 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08001475
Ian Romanick3455ce62010-04-19 15:13:15 -07001476 /* The type specifier may contain a structure definition. Process that
1477 * before any of the variable declarations.
1478 */
1479 (void) this->type->specifier->hir(instructions, state);
1480
Ian Romanicka87ac252010-02-22 13:19:34 -08001481 /* FINISHME: Handle vertex shader "invariant" declarations that do not
1482 * FINISHME: include a type. These re-declare built-in variables to be
1483 * FINISHME: invariant.
1484 */
1485
Ian Romanickd612a122010-03-31 16:22:06 -07001486 decl_type = this->type->specifier->glsl_type(& type_name, state);
Ian Romanick304ea902010-05-10 11:17:53 -07001487 if (this->declarations.is_empty()) {
Ian Romanickc824e352010-04-23 15:55:19 -07001488 /* There are only two valid cases where the declaration list can be
1489 * empty.
1490 *
1491 * 1. The declaration is setting the default precision of a built-in
1492 * type (e.g., 'precision highp vec4;').
1493 *
1494 * 2. Adding 'invariant' to an existing vertex shader output.
1495 */
1496
1497 if (this->type->qualifier.invariant) {
1498 } else if (decl_type != NULL) {
1499 } else {
1500 _mesa_glsl_error(& loc, state, "incomplete declaration");
1501 }
1502 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001503
Ian Romanick304ea902010-05-10 11:17:53 -07001504 foreach_list (n, &this->declarations) {
1505 ast_declaration *const decl = exec_node_data(ast_declaration, n, link);
Ian Romanicka87ac252010-02-22 13:19:34 -08001506 const struct glsl_type *var_type;
1507 struct ir_variable *var;
Ian Romanicka87ac252010-02-22 13:19:34 -08001508
1509 /* FINISHME: Emit a warning if a variable declaration shadows a
1510 * FINISHME: declaration at a higher scope.
1511 */
1512
Ian Romanickcec65a62010-03-23 12:28:44 -07001513 if ((decl_type == NULL) || decl_type->is_void()) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001514 if (type_name != NULL) {
1515 _mesa_glsl_error(& loc, state,
1516 "invalid type `%s' in declaration of `%s'",
1517 type_name, decl->identifier);
1518 } else {
1519 _mesa_glsl_error(& loc, state,
1520 "invalid type in declaration of `%s'",
1521 decl->identifier);
1522 }
1523 continue;
1524 }
1525
1526 if (decl->is_array) {
Ian Romanick28009cd2010-03-30 16:59:27 -07001527 var_type = process_array_type(decl_type, decl->array_size, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001528 } else {
1529 var_type = decl_type;
1530 }
1531
1532 var = new ir_variable(var_type, decl->identifier);
1533
Eric Anholt3f151502010-04-02 01:53:57 -10001534 /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
1535 *
1536 * "Global variables can only use the qualifiers const,
1537 * attribute, uni form, or varying. Only one may be
1538 * specified.
1539 *
1540 * Local variables can only use the qualifier const."
1541 *
1542 * This is relaxed in GLSL 1.30.
1543 */
1544 if (state->language_version < 120) {
1545 if (this->type->qualifier.out) {
1546 _mesa_glsl_error(& loc, state,
1547 "`out' qualifier in declaration of `%s' "
1548 "only valid for function parameters in GLSL 1.10.",
1549 decl->identifier);
1550 }
1551 if (this->type->qualifier.in) {
1552 _mesa_glsl_error(& loc, state,
1553 "`in' qualifier in declaration of `%s' "
1554 "only valid for function parameters in GLSL 1.10.",
1555 decl->identifier);
1556 }
1557 /* FINISHME: Test for other invalid qualifiers. */
1558 }
1559
Eric Anholt2e063f12010-03-28 00:56:22 -07001560 apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
1561 & loc);
Ian Romanicka87ac252010-02-22 13:19:34 -08001562
1563 /* Attempt to add the variable to the symbol table. If this fails, it
Ian Romanicka4f308f2010-04-01 17:25:11 -07001564 * means the variable has already been declared at this scope. Arrays
1565 * fudge this rule a little bit.
1566 *
1567 * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
1568 *
1569 * "It is legal to declare an array without a size and then
1570 * later re-declare the same name as an array of the same
1571 * type and specify a size."
Ian Romanicka87ac252010-02-22 13:19:34 -08001572 */
Ian Romanick3359e582010-03-19 15:38:52 -07001573 if (state->symbols->name_declared_this_scope(decl->identifier)) {
Ian Romanicka4f308f2010-04-01 17:25:11 -07001574 ir_variable *const earlier =
1575 state->symbols->get_variable(decl->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001576
Ian Romanicka4f308f2010-04-01 17:25:11 -07001577 if ((earlier != NULL)
1578 && (earlier->type->array_size() == 0)
1579 && var->type->is_array()
1580 && (var->type->element_type() == earlier->type->element_type())) {
1581 /* FINISHME: This doesn't match the qualifiers on the two
1582 * FINISHME: declarations. It's not 100% clear whether this is
1583 * FINISHME: required or not.
1584 */
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001585
Eric Anholt894ea972010-04-07 13:19:11 -07001586 if (var->type->array_size() <= (int)earlier->max_array_access) {
Ian Romanickb8a21cc2010-04-01 18:31:11 -07001587 YYLTYPE loc = this->get_location();
1588
1589 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
1590 "previous access",
1591 earlier->max_array_access);
1592 }
1593
Ian Romanicka4f308f2010-04-01 17:25:11 -07001594 earlier->type = var->type;
1595 delete var;
1596 var = NULL;
1597 } else {
1598 YYLTYPE loc = this->get_location();
1599
1600 _mesa_glsl_error(& loc, state, "`%s' redeclared",
1601 decl->identifier);
1602 }
1603
Ian Romanicka87ac252010-02-22 13:19:34 -08001604 continue;
1605 }
1606
Eric Anholtb97ee2e2010-03-31 08:20:58 -10001607 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
1608 *
1609 * "Identifiers starting with "gl_" are reserved for use by
1610 * OpenGL, and may not be declared in a shader as either a
1611 * variable or a function."
1612 */
1613 if (strncmp(decl->identifier, "gl_", 3) == 0) {
1614 /* FINISHME: This should only trigger if we're not redefining
1615 * FINISHME: a builtin (to add a qualifier, for example).
1616 */
1617 _mesa_glsl_error(& loc, state,
1618 "identifier `%s' uses reserved `gl_' prefix",
1619 decl->identifier);
1620 }
1621
Ian Romanick0044e7e2010-03-08 23:44:00 -08001622 instructions->push_tail(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08001623
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07001624 if (state->current_function != NULL) {
Ian Romanickb168e532010-03-31 12:31:18 -07001625 const char *mode = NULL;
Ian Romanicke0800062010-03-31 13:15:23 -07001626 const char *extra = "";
Ian Romanickb168e532010-03-31 12:31:18 -07001627
Ian Romanicke0800062010-03-31 13:15:23 -07001628 /* There is no need to check for 'inout' here because the parser will
1629 * only allow that in function parameter lists.
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07001630 */
1631 if (this->type->qualifier.attribute) {
Ian Romanickb168e532010-03-31 12:31:18 -07001632 mode = "attribute";
1633 } else if (this->type->qualifier.uniform) {
1634 mode = "uniform";
1635 } else if (this->type->qualifier.varying) {
1636 mode = "varying";
Ian Romanicke0800062010-03-31 13:15:23 -07001637 } else if (this->type->qualifier.in) {
1638 mode = "in";
1639 extra = " or in function parameter list";
1640 } else if (this->type->qualifier.out) {
1641 mode = "out";
1642 extra = " or in function parameter list";
Ian Romanickb168e532010-03-31 12:31:18 -07001643 }
1644
1645 if (mode) {
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07001646 _mesa_glsl_error(& loc, state,
Ian Romanickb168e532010-03-31 12:31:18 -07001647 "%s variable `%s' must be declared at "
Ian Romanicke0800062010-03-31 13:15:23 -07001648 "global scope%s",
1649 mode, var->name, extra);
Ian Romanicke1c1a3f2010-03-31 12:26:03 -07001650 }
1651 } else if (var->mode == ir_var_in) {
Ian Romanickfb9f5b02010-03-29 17:16:35 -07001652 if (state->target == vertex_shader) {
1653 bool error_emitted = false;
1654
1655 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1656 *
1657 * "Vertex shader inputs can only be float, floating-point
1658 * vectors, matrices, signed and unsigned integers and integer
1659 * vectors. Vertex shader inputs can also form arrays of these
1660 * types, but not structures."
1661 *
Ian Romanick2d816202010-03-29 17:40:11 -07001662 * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
1663 *
1664 * "Vertex shader inputs can only be float, floating-point
1665 * vectors, matrices, signed and unsigned integers and integer
1666 * vectors. They cannot be arrays or structures."
1667 *
Ian Romanickfb9f5b02010-03-29 17:16:35 -07001668 * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1669 *
1670 * "The attribute qualifier can be used only with float,
1671 * floating-point vectors, and matrices. Attribute variables
1672 * cannot be declared as arrays or structures."
1673 */
1674 const glsl_type *check_type = var->type->is_array()
1675 ? var->type->fields.array : var->type;
1676
1677 switch (check_type->base_type) {
1678 case GLSL_TYPE_FLOAT:
1679 break;
1680 case GLSL_TYPE_UINT:
1681 case GLSL_TYPE_INT:
1682 if (state->language_version > 120)
1683 break;
1684 /* FALLTHROUGH */
1685 default:
1686 _mesa_glsl_error(& loc, state,
1687 "vertex shader input / attribute cannot have "
1688 "type %s`%s'",
1689 var->type->is_array() ? "array of " : "",
1690 check_type->name);
1691 error_emitted = true;
1692 }
1693
Ian Romanick2d816202010-03-29 17:40:11 -07001694 if (!error_emitted && (state->language_version <= 130)
Ian Romanickfb9f5b02010-03-29 17:16:35 -07001695 && var->type->is_array()) {
1696 _mesa_glsl_error(& loc, state,
1697 "vertex shader input / attribute cannot have "
1698 "array type");
1699 error_emitted = true;
1700 }
1701 }
1702 }
1703
Ian Romanick66faec42010-03-27 18:56:53 -07001704 if (decl->initializer != NULL) {
Ian Romanick43de1722010-03-28 17:03:16 -07001705 YYLTYPE initializer_loc = decl->initializer->get_location();
1706
Ian Romanick66faec42010-03-27 18:56:53 -07001707 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
1708 *
1709 * "All uniform variables are read-only and are initialized either
1710 * directly by an application via API commands, or indirectly by
1711 * OpenGL."
1712 */
1713 if ((state->language_version <= 110)
1714 && (var->mode == ir_var_uniform)) {
Ian Romanick43de1722010-03-28 17:03:16 -07001715 _mesa_glsl_error(& initializer_loc, state,
1716 "cannot initialize uniforms in GLSL 1.10");
1717 }
Ian Romanick19360152010-03-26 18:05:27 -07001718
Ian Romanick43de1722010-03-28 17:03:16 -07001719 if (var->type->is_sampler()) {
1720 _mesa_glsl_error(& initializer_loc, state,
1721 "cannot initialize samplers");
1722 }
1723
1724 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
1725 _mesa_glsl_error(& initializer_loc, state,
1726 "cannot initialize %s shader input / %s",
Ian Romanickae4c4c02010-04-07 16:41:40 -07001727 _mesa_glsl_shader_target_name(state->target),
Ian Romanick43de1722010-03-28 17:03:16 -07001728 (state->target == vertex_shader)
1729 ? "attribute" : "varying");
Ian Romanick66faec42010-03-27 18:56:53 -07001730 }
1731
1732 ir_dereference *const lhs = new ir_dereference(var);
Eric Anholt307c71b2010-03-31 15:53:26 -10001733 ir_rvalue *rhs = decl->initializer->hir(instructions, state);
Ian Romanick66faec42010-03-27 18:56:53 -07001734
Eric Anholt307c71b2010-03-31 15:53:26 -10001735 /* Calculate the constant value if this is a const
1736 * declaration.
Ian Romanick66faec42010-03-27 18:56:53 -07001737 */
Eric Anholt307c71b2010-03-31 15:53:26 -10001738 if (this->type->qualifier.constant) {
Eric Anholt326c6762010-04-06 10:30:54 -07001739 ir_constant *constant_value = rhs->constant_expression_value();
1740 if (!constant_value) {
Eric Anholt307c71b2010-03-31 15:53:26 -10001741 _mesa_glsl_error(& initializer_loc, state,
1742 "initializer of const variable `%s' must be a "
1743 "constant expression",
1744 decl->identifier);
Eric Anholt326c6762010-04-06 10:30:54 -07001745 } else {
1746 rhs = constant_value;
1747 var->constant_value = constant_value;
Eric Anholt307c71b2010-03-31 15:53:26 -10001748 }
1749 }
Ian Romanick66faec42010-03-27 18:56:53 -07001750
Eric Anholt307c71b2010-03-31 15:53:26 -10001751 if (rhs && !rhs->type->is_error()) {
Eric Anholtac3af372010-03-31 15:44:38 -10001752 bool temp = var->read_only;
1753 if (this->type->qualifier.constant)
1754 var->read_only = false;
Eric Anholt85584592010-04-14 15:38:52 -07001755 result = do_assignment(instructions, state, lhs, rhs,
1756 this->get_location());
Eric Anholtac3af372010-03-31 15:44:38 -10001757 var->read_only = temp;
Ian Romanick66faec42010-03-27 18:56:53 -07001758 }
Ian Romanick19360152010-03-26 18:05:27 -07001759 }
Ian Romanick17d86f42010-03-29 12:59:02 -07001760
Eric Anholt0ed61252010-03-31 09:29:33 -10001761 /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
1762 *
1763 * "It is an error to write to a const variable outside of
1764 * its declaration, so they must be initialized when
1765 * declared."
1766 */
1767 if (this->type->qualifier.constant && decl->initializer == NULL) {
1768 _mesa_glsl_error(& loc, state,
1769 "const declaration of `%s' must be initialized");
1770 }
1771
Ian Romanick17d86f42010-03-29 12:59:02 -07001772 /* Add the vairable to the symbol table after processing the initializer.
1773 * This differs from most C-like languages, but it follows the GLSL
1774 * specification. From page 28 (page 34 of the PDF) of the GLSL 1.50
1775 * spec:
1776 *
1777 * "Within a declaration, the scope of a name starts immediately
1778 * after the initializer if present or immediately after the name
1779 * being declared if not."
1780 */
1781 const bool added_variable =
1782 state->symbols->add_variable(decl->identifier, var);
1783 assert(added_variable);
Ian Romanicka87ac252010-02-22 13:19:34 -08001784 }
1785
Eric Anholt85584592010-04-14 15:38:52 -07001786
1787 /* Generally, variable declarations do not have r-values. However,
1788 * one is used for the declaration in
1789 *
1790 * while (bool b = some_condition()) {
1791 * ...
1792 * }
1793 *
1794 * so we return the rvalue from the last seen declaration here.
Ian Romanicka87ac252010-02-22 13:19:34 -08001795 */
Eric Anholt85584592010-04-14 15:38:52 -07001796 return result;
Ian Romanicka87ac252010-02-22 13:19:34 -08001797}
1798
1799
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001800ir_rvalue *
Ian Romanick0044e7e2010-03-08 23:44:00 -08001801ast_parameter_declarator::hir(exec_list *instructions,
Ian Romanick18238de2010-03-01 13:49:10 -08001802 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001803{
Ian Romanicka87ac252010-02-22 13:19:34 -08001804 const struct glsl_type *type;
1805 const char *name = NULL;
Eric Anholt2e063f12010-03-28 00:56:22 -07001806 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08001807
Ian Romanickd612a122010-03-31 16:22:06 -07001808 type = this->type->specifier->glsl_type(& name, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001809
1810 if (type == NULL) {
Ian Romanicka87ac252010-02-22 13:19:34 -08001811 if (name != NULL) {
1812 _mesa_glsl_error(& loc, state,
1813 "invalid type `%s' in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08001814 name, this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001815 } else {
1816 _mesa_glsl_error(& loc, state,
1817 "invalid type in declaration of `%s'",
Ian Romanick18238de2010-03-01 13:49:10 -08001818 this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001819 }
1820
Ian Romanick0471e8b2010-03-26 14:33:41 -07001821 type = glsl_type::error_type;
Ian Romanicka87ac252010-02-22 13:19:34 -08001822 }
1823
Eric Anholt068c80c2010-03-31 09:56:36 -10001824 /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
1825 *
1826 * "Functions that accept no input arguments need not use void in the
1827 * argument list because prototypes (or definitions) are required and
1828 * therefore there is no ambiguity when an empty argument list "( )" is
1829 * declared. The idiom "(void)" as a parameter list is provided for
1830 * convenience."
1831 *
1832 * Placing this check here prevents a void parameter being set up
1833 * for a function, which avoids tripping up checks for main taking
1834 * parameters and lookups of an unnamed symbol.
1835 */
Ian Romanickcf37c9e2010-04-02 15:30:45 -07001836 if (type->is_void()) {
1837 if (this->identifier != NULL)
1838 _mesa_glsl_error(& loc, state,
1839 "named parameter cannot have type `void'");
1840
1841 is_void = true;
Eric Anholt068c80c2010-03-31 09:56:36 -10001842 return NULL;
Ian Romanickcf37c9e2010-04-02 15:30:45 -07001843 }
Eric Anholt068c80c2010-03-31 09:56:36 -10001844
Ian Romanick45d8a702010-04-02 15:09:33 -07001845 if (formal_parameter && (this->identifier == NULL)) {
1846 _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
1847 return NULL;
1848 }
1849
Ian Romanickcf37c9e2010-04-02 15:30:45 -07001850 is_void = false;
Ian Romanick18238de2010-03-01 13:49:10 -08001851 ir_variable *var = new ir_variable(type, this->identifier);
Ian Romanicka87ac252010-02-22 13:19:34 -08001852
1853 /* FINISHME: Handle array declarations. Note that this requires
1854 * FINISHME: complete handling of constant expressions.
1855 */
1856
Ian Romanickcdb8d542010-03-11 14:48:51 -08001857 /* Apply any specified qualifiers to the parameter declaration. Note that
1858 * for function parameters the default mode is 'in'.
1859 */
Eric Anholt2e063f12010-03-28 00:56:22 -07001860 apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
Ian Romanickcdb8d542010-03-11 14:48:51 -08001861 if (var->mode == ir_var_auto)
1862 var->mode = ir_var_in;
Ian Romanicka87ac252010-02-22 13:19:34 -08001863
Ian Romanick0044e7e2010-03-08 23:44:00 -08001864 instructions->push_tail(var);
Ian Romanicka87ac252010-02-22 13:19:34 -08001865
1866 /* Parameter declarations do not have r-values.
1867 */
1868 return NULL;
1869}
1870
1871
Ian Romanick45d8a702010-04-02 15:09:33 -07001872void
Ian Romanick304ea902010-05-10 11:17:53 -07001873ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
Ian Romanick45d8a702010-04-02 15:09:33 -07001874 bool formal,
1875 exec_list *ir_parameters,
1876 _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001877{
Ian Romanickcf37c9e2010-04-02 15:30:45 -07001878 ast_parameter_declarator *void_param = NULL;
1879 unsigned count = 0;
Ian Romanicka87ac252010-02-22 13:19:34 -08001880
Ian Romanick304ea902010-05-10 11:17:53 -07001881 foreach_list (n, ast_parameters) {
1882 ast_parameter_declarator *param =
1883 exec_node_data(ast_parameter_declarator, n, link);
Ian Romanick45d8a702010-04-02 15:09:33 -07001884 param->formal_parameter = formal;
Eric Anholt068c80c2010-03-31 09:56:36 -10001885 param->hir(ir_parameters, state);
Ian Romanickcf37c9e2010-04-02 15:30:45 -07001886
1887 if (param->is_void)
1888 void_param = param;
1889
1890 count++;
1891 }
1892
1893 if ((void_param != NULL) && (count > 1)) {
1894 YYLTYPE loc = void_param->get_location();
1895
1896 _mesa_glsl_error(& loc, state,
1897 "`void' parameter must be only parameter");
Ian Romanicka87ac252010-02-22 13:19:34 -08001898 }
1899}
1900
1901
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07001902ir_rvalue *
Ian Romanick92318a92010-03-31 18:23:21 -07001903ast_function::hir(exec_list *instructions,
1904 struct _mesa_glsl_parse_state *state)
Ian Romanicka87ac252010-02-22 13:19:34 -08001905{
Ian Romanick18238de2010-03-01 13:49:10 -08001906 ir_function *f = NULL;
Ian Romanick92318a92010-03-31 18:23:21 -07001907 ir_function_signature *sig = NULL;
1908 exec_list hir_parameters;
Ian Romanicka87ac252010-02-22 13:19:34 -08001909
1910
1911 /* Convert the list of function parameters to HIR now so that they can be
1912 * used below to compare this function's signature with previously seen
1913 * signatures for functions with the same name.
1914 */
Ian Romanick45d8a702010-04-02 15:09:33 -07001915 ast_parameter_declarator::parameters_to_hir(& this->parameters,
1916 is_definition,
1917 & hir_parameters, state);
Ian Romanicka87ac252010-02-22 13:19:34 -08001918
Ian Romanicke39cc692010-03-23 12:19:13 -07001919 const char *return_type_name;
1920 const glsl_type *return_type =
Ian Romanick92318a92010-03-31 18:23:21 -07001921 this->return_type->specifier->glsl_type(& return_type_name, state);
Ian Romanicke39cc692010-03-23 12:19:13 -07001922
1923 assert(return_type != NULL);
1924
Ian Romanicka87ac252010-02-22 13:19:34 -08001925 /* Verify that this function's signature either doesn't match a previously
1926 * seen signature for a function with the same name, or, if a match is found,
1927 * that the previously seen signature does not have an associated definition.
1928 */
Ian Romanick92318a92010-03-31 18:23:21 -07001929 const char *const name = identifier;
Ian Romanick3359e582010-03-19 15:38:52 -07001930 f = state->symbols->get_function(name);
Ian Romanicka87ac252010-02-22 13:19:34 -08001931 if (f != NULL) {
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07001932 ir_function_signature *sig = f->exact_matching_signature(&hir_parameters);
1933 if (sig != NULL) {
1934 const char *badvar = sig->qualifiers_match(&hir_parameters);
1935 if (badvar != NULL) {
1936 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08001937
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07001938 _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
1939 "qualifiers don't match prototype", name, badvar);
Ian Romanicka87ac252010-02-22 13:19:34 -08001940 }
1941
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07001942 if (sig->return_type != return_type) {
1943 YYLTYPE loc = this->get_location();
Ian Romanicka87ac252010-02-22 13:19:34 -08001944
Kenneth Graunke0d605cb2010-04-28 12:04:23 -07001945 _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
1946 "match prototype", name);
1947 }
1948
1949 if (is_definition && sig->is_defined) {
1950 YYLTYPE loc = this->get_location();
1951
1952 _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
1953 sig = NULL;
1954 }
1955 }
Ian Romanick3359e582010-03-19 15:38:52 -07001956 } else if (state->symbols->name_declared_this_scope(name)) {
1957 /* This function name shadows a non-function use of the same name.
1958 */
1959 YYLTYPE loc = this->get_location();
1960
1961 _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
1962 "non-function", name);
Ian Romanick92318a92010-03-31 18:23:21 -07001963 sig = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08001964 } else {
Ian Romanick882dad72010-03-23 17:42:04 -07001965 f = new ir_function(name);
Ian Romanick8bde4ce2010-03-19 11:57:24 -07001966 state->symbols->add_function(f->name, f);
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07001967
1968 /* Emit the new function header */
1969 instructions->push_tail(f);
Ian Romanicka87ac252010-02-22 13:19:34 -08001970 }
1971
Eric Anholtab372da2010-03-28 01:24:55 -07001972 /* Verify the return type of main() */
1973 if (strcmp(name, "main") == 0) {
Ian Romanick25711a82010-03-31 17:39:10 -07001974 if (! return_type->is_void()) {
Eric Anholtab372da2010-03-28 01:24:55 -07001975 YYLTYPE loc = this->get_location();
1976
1977 _mesa_glsl_error(& loc, state, "main() must return void");
1978 }
Eric Anholt174cc032010-03-30 23:37:51 -10001979
Ian Romanick92318a92010-03-31 18:23:21 -07001980 if (!hir_parameters.is_empty()) {
Eric Anholt174cc032010-03-30 23:37:51 -10001981 YYLTYPE loc = this->get_location();
1982
1983 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
1984 }
Eric Anholtab372da2010-03-28 01:24:55 -07001985 }
Ian Romanicka87ac252010-02-22 13:19:34 -08001986
1987 /* Finish storing the information about this new function in its signature.
1988 */
Ian Romanick92318a92010-03-31 18:23:21 -07001989 if (sig == NULL) {
1990 sig = new ir_function_signature(return_type);
1991 f->add_signature(sig);
Ian Romanicka87ac252010-02-22 13:19:34 -08001992 }
1993
Kenneth Graunkebff60132010-04-28 12:44:24 -07001994 sig->replace_parameters(&hir_parameters);
Ian Romanick92318a92010-03-31 18:23:21 -07001995 signature = sig;
Ian Romanicke29a5852010-03-31 17:54:26 -07001996
Ian Romanick92318a92010-03-31 18:23:21 -07001997 /* Function declarations (prototypes) do not have r-values.
1998 */
1999 return NULL;
2000}
2001
2002
2003ir_rvalue *
2004ast_function_definition::hir(exec_list *instructions,
2005 struct _mesa_glsl_parse_state *state)
2006{
2007 prototype->is_definition = true;
2008 prototype->hir(instructions, state);
2009
2010 ir_function_signature *signature = prototype->signature;
Ian Romanicka87ac252010-02-22 13:19:34 -08002011
Ian Romanick41ec6a42010-03-19 17:08:05 -07002012 assert(state->current_function == NULL);
2013 state->current_function = signature;
2014
Ian Romanicke29a5852010-03-31 17:54:26 -07002015 /* Duplicate parameters declared in the prototype as concrete variables.
2016 * Add these to the symbol table.
Ian Romanicka87ac252010-02-22 13:19:34 -08002017 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -07002018 state->symbols->push_scope();
Ian Romanicke29a5852010-03-31 17:54:26 -07002019 foreach_iter(exec_list_iterator, iter, signature->parameters) {
Eric Anholtfbc7c0b2010-04-07 14:32:53 -07002020 ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
Ian Romanicka87ac252010-02-22 13:19:34 -08002021
Eric Anholtfbc7c0b2010-04-07 14:32:53 -07002022 assert(var != NULL);
Ian Romanicka87ac252010-02-22 13:19:34 -08002023
Ian Romanick3359e582010-03-19 15:38:52 -07002024 /* The only way a parameter would "exist" is if two parameters have
2025 * the same name.
2026 */
2027 if (state->symbols->name_declared_this_scope(var->name)) {
2028 YYLTYPE loc = this->get_location();
2029
2030 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
2031 } else {
2032 state->symbols->add_variable(var->name, var);
2033 }
Ian Romanicka87ac252010-02-22 13:19:34 -08002034 }
2035
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07002036 /* Convert the body of the function to HIR. */
Eric Anholt894ea972010-04-07 13:19:11 -07002037 this->body->hir(&signature->body, state);
Kenneth Graunke9fa99f32010-04-21 12:30:22 -07002038 signature->is_defined = true;
Ian Romanicka87ac252010-02-22 13:19:34 -08002039
Ian Romanick8bde4ce2010-03-19 11:57:24 -07002040 state->symbols->pop_scope();
Ian Romanicka87ac252010-02-22 13:19:34 -08002041
Ian Romanick41ec6a42010-03-19 17:08:05 -07002042 assert(state->current_function == signature);
2043 state->current_function = NULL;
Ian Romanicka87ac252010-02-22 13:19:34 -08002044
2045 /* Function definitions do not have r-values.
2046 */
2047 return NULL;
2048}
Ian Romanick16a246c2010-03-19 16:45:19 -07002049
2050
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -07002051ir_rvalue *
Ian Romanick16a246c2010-03-19 16:45:19 -07002052ast_jump_statement::hir(exec_list *instructions,
2053 struct _mesa_glsl_parse_state *state)
2054{
2055
Ian Romanickc0e76d82010-04-05 16:53:19 -07002056 switch (mode) {
2057 case ast_return: {
Ian Romanick16a246c2010-03-19 16:45:19 -07002058 ir_return *inst;
Eric Anholtaad7c772010-03-30 23:28:20 -10002059 assert(state->current_function);
Ian Romanick16a246c2010-03-19 16:45:19 -07002060
2061 if (opt_return_value) {
Eric Anholtab79d4e2010-03-30 23:23:16 -10002062 if (state->current_function->return_type->base_type ==
2063 GLSL_TYPE_VOID) {
2064 YYLTYPE loc = this->get_location();
2065
2066 _mesa_glsl_error(& loc, state,
2067 "`return` with a value, in function `%s' "
2068 "returning void",
Kenneth Graunkef96c52b2010-04-21 15:17:26 -07002069 state->current_function->function_name());
Eric Anholtab79d4e2010-03-30 23:23:16 -10002070 }
Ian Romanick16a246c2010-03-19 16:45:19 -07002071
2072 ir_expression *const ret = (ir_expression *)
2073 opt_return_value->hir(instructions, state);
2074 assert(ret != NULL);
2075
2076 /* FINISHME: Make sure the type of the return value matches the return
2077 * FINISHME: type of the enclosing function.
2078 */
2079
2080 inst = new ir_return(ret);
2081 } else {
Eric Anholtaad7c772010-03-30 23:28:20 -10002082 if (state->current_function->return_type->base_type !=
2083 GLSL_TYPE_VOID) {
2084 YYLTYPE loc = this->get_location();
2085
2086 _mesa_glsl_error(& loc, state,
2087 "`return' with no value, in function %s returning "
2088 "non-void",
Kenneth Graunkef96c52b2010-04-21 15:17:26 -07002089 state->current_function->function_name());
Eric Anholtaad7c772010-03-30 23:28:20 -10002090 }
Ian Romanick16a246c2010-03-19 16:45:19 -07002091 inst = new ir_return;
2092 }
2093
2094 instructions->push_tail(inst);
Ian Romanickc0e76d82010-04-05 16:53:19 -07002095 break;
Ian Romanick16a246c2010-03-19 16:45:19 -07002096 }
2097
Ian Romanickc0e76d82010-04-05 16:53:19 -07002098 case ast_discard:
Eric Anholtb9802072010-03-30 23:40:14 -10002099 /* FINISHME: discard support */
2100 if (state->target != fragment_shader) {
2101 YYLTYPE loc = this->get_location();
2102
2103 _mesa_glsl_error(& loc, state,
2104 "`discard' may only appear in a fragment shader");
2105 }
Ian Romanickc0e76d82010-04-05 16:53:19 -07002106 break;
2107
2108 case ast_break:
2109 case ast_continue:
Ian Romanick4cf20cd2010-04-05 17:13:47 -07002110 /* FINISHME: Handle switch-statements. They cannot contain 'continue',
2111 * FINISHME: and they use a different IR instruction for 'break'.
2112 */
2113 /* FINISHME: Correctly handle the nesting. If a switch-statement is
2114 * FINISHME: inside a loop, a 'continue' is valid and will bind to the
2115 * FINISHME: loop.
2116 */
2117 if (state->loop_or_switch_nesting == NULL) {
2118 YYLTYPE loc = this->get_location();
2119
2120 _mesa_glsl_error(& loc, state,
2121 "`%s' may only appear in a loop",
2122 (mode == ast_break) ? "break" : "continue");
2123 } else {
2124 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
2125
2126 if (loop != NULL) {
2127 ir_loop_jump *const jump =
2128 new ir_loop_jump(loop,
2129 (mode == ast_break)
2130 ? ir_loop_jump::jump_break
2131 : ir_loop_jump::jump_continue);
2132 instructions->push_tail(jump);
2133 }
2134 }
2135
Ian Romanickc0e76d82010-04-05 16:53:19 -07002136 break;
Eric Anholtb9802072010-03-30 23:40:14 -10002137 }
2138
Ian Romanick16a246c2010-03-19 16:45:19 -07002139 /* Jump instructions do not have r-values.
2140 */
2141 return NULL;
2142}
Ian Romanick3c6fea32010-03-29 14:11:25 -07002143
2144
2145ir_rvalue *
2146ast_selection_statement::hir(exec_list *instructions,
2147 struct _mesa_glsl_parse_state *state)
2148{
2149 ir_rvalue *const condition = this->condition->hir(instructions, state);
Ian Romanick3c6fea32010-03-29 14:11:25 -07002150
2151 /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
2152 *
2153 * "Any expression whose type evaluates to a Boolean can be used as the
2154 * conditional expression bool-expression. Vector types are not accepted
2155 * as the expression to if."
2156 *
2157 * The checks are separated so that higher quality diagnostics can be
2158 * generated for cases where both rules are violated.
2159 */
2160 if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
2161 YYLTYPE loc = this->condition->get_location();
2162
2163 _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
2164 "boolean");
2165 }
2166
2167 ir_if *const stmt = new ir_if(condition);
2168
Ian Romanick4f9d72f2010-05-10 11:10:26 -07002169 if (then_statement != NULL)
2170 then_statement->hir(& stmt->then_instructions, state);
Ian Romanick3c6fea32010-03-29 14:11:25 -07002171
Ian Romanick4f9d72f2010-05-10 11:10:26 -07002172 if (else_statement != NULL)
2173 else_statement->hir(& stmt->else_instructions, state);
Ian Romanick3c6fea32010-03-29 14:11:25 -07002174
2175 instructions->push_tail(stmt);
2176
2177 /* if-statements do not have r-values.
2178 */
2179 return NULL;
2180}
Ian Romanick9e7d0102010-04-05 16:37:49 -07002181
2182
Ian Romanick8c46ed22010-04-05 18:07:27 -07002183void
2184ast_iteration_statement::condition_to_hir(ir_loop *stmt,
2185 struct _mesa_glsl_parse_state *state)
Ian Romanick9e7d0102010-04-05 16:37:49 -07002186{
Ian Romanick9e7d0102010-04-05 16:37:49 -07002187 if (condition != NULL) {
2188 ir_rvalue *const cond =
2189 condition->hir(& stmt->body_instructions, state);
2190
2191 if ((cond == NULL)
2192 || !cond->type->is_boolean() || !cond->type->is_scalar()) {
2193 YYLTYPE loc = condition->get_location();
2194
2195 _mesa_glsl_error(& loc, state,
2196 "loop condition must be scalar boolean");
2197 } else {
2198 /* As the first code in the loop body, generate a block that looks
2199 * like 'if (!condition) break;' as the loop termination condition.
2200 */
2201 ir_rvalue *const not_cond =
2202 new ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
2203 NULL);
2204
2205 ir_if *const if_stmt = new ir_if(not_cond);
2206
2207 ir_jump *const break_stmt =
2208 new ir_loop_jump(stmt, ir_loop_jump::jump_break);
2209
2210 if_stmt->then_instructions.push_tail(break_stmt);
2211 stmt->body_instructions.push_tail(if_stmt);
2212 }
2213 }
Ian Romanick8c46ed22010-04-05 18:07:27 -07002214}
2215
2216
2217ir_rvalue *
2218ast_iteration_statement::hir(exec_list *instructions,
2219 struct _mesa_glsl_parse_state *state)
2220{
Ian Romanick48460662010-04-16 16:42:43 -07002221 /* For-loops and while-loops start a new scope, but do-while loops do not.
Ian Romanick8c46ed22010-04-05 18:07:27 -07002222 */
Ian Romanick48460662010-04-16 16:42:43 -07002223 if (mode != ast_do_while)
Ian Romanick8c46ed22010-04-05 18:07:27 -07002224 state->symbols->push_scope();
2225
2226 if (init_statement != NULL)
2227 init_statement->hir(instructions, state);
2228
2229 ir_loop *const stmt = new ir_loop();
2230 instructions->push_tail(stmt);
2231
2232 /* Track the current loop and / or switch-statement nesting.
2233 */
2234 ir_instruction *const nesting = state->loop_or_switch_nesting;
2235 state->loop_or_switch_nesting = stmt;
2236
2237 if (mode != ast_do_while)
2238 condition_to_hir(stmt, state);
Ian Romanick9e7d0102010-04-05 16:37:49 -07002239
Ian Romanick4f9d72f2010-05-10 11:10:26 -07002240 if (body != NULL)
2241 body->hir(& stmt->body_instructions, state);
Ian Romanick9e7d0102010-04-05 16:37:49 -07002242
2243 if (rest_expression != NULL)
2244 rest_expression->hir(& stmt->body_instructions, state);
2245
Ian Romanick8c46ed22010-04-05 18:07:27 -07002246 if (mode == ast_do_while)
2247 condition_to_hir(stmt, state);
2248
Ian Romanick48460662010-04-16 16:42:43 -07002249 if (mode != ast_do_while)
Ian Romanick9e7d0102010-04-05 16:37:49 -07002250 state->symbols->pop_scope();
2251
Ian Romanicke9d0f262010-04-05 17:01:53 -07002252 /* Restore previous nesting before returning.
2253 */
2254 state->loop_or_switch_nesting = nesting;
2255
Ian Romanick9e7d0102010-04-05 16:37:49 -07002256 /* Loops do not have r-values.
2257 */
2258 return NULL;
2259}
Ian Romanick3455ce62010-04-19 15:13:15 -07002260
2261
2262ir_rvalue *
2263ast_type_specifier::hir(exec_list *instructions,
2264 struct _mesa_glsl_parse_state *state)
2265{
2266 if (this->structure != NULL)
2267 return this->structure->hir(instructions, state);
Ian Romanick85ba37b2010-04-21 14:33:34 -07002268
2269 return NULL;
Ian Romanick3455ce62010-04-19 15:13:15 -07002270}
2271
2272
2273ir_rvalue *
2274ast_struct_specifier::hir(exec_list *instructions,
2275 struct _mesa_glsl_parse_state *state)
2276{
Ian Romanick3455ce62010-04-19 15:13:15 -07002277 unsigned decl_count = 0;
2278
2279 /* Make an initial pass over the list of structure fields to determine how
2280 * many there are. Each element in this list is an ast_declarator_list.
2281 * This means that we actually need to count the number of elements in the
2282 * 'declarations' list in each of the elements.
2283 */
Ian Romanick304ea902010-05-10 11:17:53 -07002284 foreach_list (n, & this->declarations) {
2285 ast_declarator_list *decl_list =
2286 exec_node_data(ast_declarator_list, n, link);
Ian Romanick3455ce62010-04-19 15:13:15 -07002287
Ian Romanick304ea902010-05-10 11:17:53 -07002288 foreach_list_const (decl_ptr, & decl_list->declarations) {
Ian Romanick3455ce62010-04-19 15:13:15 -07002289 decl_count++;
2290 }
2291 }
2292
2293
2294 /* Allocate storage for the structure fields and process the field
2295 * declarations. As the declarations are processed, try to also convert
2296 * the types to HIR. This ensures that structure definitions embedded in
2297 * other structure definitions are processed.
2298 */
2299 glsl_struct_field *const fields = (glsl_struct_field *)
2300 malloc(sizeof(*fields) * decl_count);
2301
2302 unsigned i = 0;
Ian Romanick304ea902010-05-10 11:17:53 -07002303 foreach_list (n, & this->declarations) {
2304 ast_declarator_list *decl_list =
2305 exec_node_data(ast_declarator_list, n, link);
Ian Romanick3455ce62010-04-19 15:13:15 -07002306 const char *type_name;
2307
2308 decl_list->type->specifier->hir(instructions, state);
2309
2310 const glsl_type *decl_type =
2311 decl_list->type->specifier->glsl_type(& type_name, state);
2312
Ian Romanick304ea902010-05-10 11:17:53 -07002313 foreach_list (decl_node, & decl_list->declarations) {
2314 ast_declaration *const decl =
2315 exec_node_data(ast_declaration, decl_node, link);
Ian Romanick3455ce62010-04-19 15:13:15 -07002316 const struct glsl_type *const field_type =
2317 (decl->is_array)
2318 ? process_array_type(decl_type, decl->array_size, state)
2319 : decl_type;
2320
Ian Romanick73986a72010-04-20 16:49:03 -07002321 fields[i].type = (field_type != NULL)
2322 ? field_type : glsl_type::error_type;
Ian Romanick3455ce62010-04-19 15:13:15 -07002323 fields[i].name = decl->identifier;
2324 i++;
2325 }
2326 }
2327
2328 assert(i == decl_count);
2329
Ian Romanick1d28b612010-04-20 16:48:24 -07002330 const char *name;
2331 if (this->name == NULL) {
2332 static unsigned anon_count = 1;
2333 char buf[32];
Ian Romanick3455ce62010-04-19 15:13:15 -07002334
Ian Romanick1d28b612010-04-20 16:48:24 -07002335 snprintf(buf, sizeof(buf), "#anon_struct_%04x", anon_count);
2336 anon_count++;
2337
2338 name = strdup(buf);
2339 } else {
2340 name = this->name;
2341 }
2342
2343 glsl_type *t = new glsl_type(fields, decl_count, name);
2344
Ian Romanickab899272010-04-23 13:24:08 -07002345 YYLTYPE loc = this->get_location();
2346 if (!state->symbols->add_type(name, t)) {
2347 _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
2348 } else {
2349 /* This logic is a bit tricky. It is an error to declare a structure at
2350 * global scope if there is also a function with the same name.
2351 */
2352 if ((state->current_function == NULL)
2353 && (state->symbols->get_function(name) != NULL)) {
2354 _mesa_glsl_error(& loc, state, "name `%s' previously defined", name);
2355 } else {
2356 t->generate_constructor(state->symbols);
2357 }
Ian Romanicka2c6df52010-04-28 13:14:53 -07002358
2359 const glsl_type **s = (const glsl_type **)
2360 realloc(state->user_structures,
2361 sizeof(state->user_structures[0]) *
2362 (state->num_user_structures + 1));
2363 if (s != NULL) {
2364 s[state->num_user_structures] = t;
2365 state->user_structures = s;
2366 state->num_user_structures++;
2367 }
Ian Romanickab899272010-04-23 13:24:08 -07002368 }
Ian Romanick3455ce62010-04-19 15:13:15 -07002369
2370 /* Structure type definitions do not have r-values.
2371 */
2372 return NULL;
2373}