ast_to_hir.cpp revision 76e96d74f49cc262ceaf2ed6c48d2f4ed21d219f
1a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick/*
2a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Copyright © 2010 Intel Corporation
3a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
4a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Permission is hereby granted, free of charge, to any person obtaining a
5a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * copy of this software and associated documentation files (the "Software"),
6a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * to deal in the Software without restriction, including without limitation
7a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * and/or sell copies of the Software, and to permit persons to whom the
9a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Software is furnished to do so, subject to the following conditions:
10a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
11a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * The above copyright notice and this permission notice (including the next
12a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * paragraph) shall be included in all copies or substantial portions of the
13a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Software.
14a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
15a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * DEALINGS IN THE SOFTWARE.
22a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */
23a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
24a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick/**
25a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * \file ast_to_hir.c
26a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
27a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
28a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * During the conversion to HIR, the majority of the symantic checking is
29a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * preformed on the program.  This includes:
30a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
31a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *    * Symbol table management
32a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *    * Type checking
33a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *    * Function binding
34a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
35a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * The majority of this work could be done during parsing, and the parser could
36a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * probably generate HIR directly.  However, this results in frequent changes
37a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * to the parser code.  Since we do not assume that every system this complier
38a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * is built on will have Flex and Bison installed, we have to store the code
39a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * generated by these tools in our version control system.  In other parts of
40a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * the system we've seen problems where a parser was changed but the generated
41a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * code was not committed, merge conflicts where created because two developers
42a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * had slightly different versions of Bison installed, etc.
43a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
44a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * I have also noticed that running Bison generated parsers in GDB is very
45a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * irritating.  When you get a segfault on '$$ = $1->foo', you can't very
46a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * well 'print $1' in GDB.
47a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick *
48a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * As a result, my preference is to put as little C code as possible in the
49a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick * parser (and lexer) sources.
50a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick */
51ac95f2f8c88d39aaa878f61172d9748af13e2c80Eric Anholt
52a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "main/imports.h"
534a962170d7cf4243d6ae156fca20a6167388925dEric Anholt#include "main/extensions.h"
548bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick#include "glsl_symbol_table.h"
55a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "glsl_parser_extras.h"
56a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ast.h"
57a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "glsl_types.h"
58a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ir.h"
59a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
60d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanickvoid
61d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
62d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick{
63adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick   _mesa_glsl_initialize_variables(instructions, state);
64c22c40015db32b68b33c4944b9d94bf499135ec5Eric Anholt   _mesa_glsl_initialize_functions(instructions, state);
65adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick
6641ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
6741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
682b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, & state->translation_unit)
69304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
70d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick}
71d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
72d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
730104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick/**
740104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is available, convert one operand to a different type
750104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
760104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * The \c from \c ir_rvalue is converted "in place".
770104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
780104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param to     Type that the operand it to be converted to
790104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param from   Operand that is being converted
800104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param state  GLSL compiler state
810104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
820104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \return
830104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is possible (or unnecessary), \c true is returned.
840104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * Otherwise \c false is returned.
850104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */
860104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanickstatic bool
87bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
880104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick			  struct _mesa_glsl_parse_state *state)
890104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick{
90953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
91bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (to->base_type == from->type->base_type)
920104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return true;
930104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
940104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* This conversion was added in GLSL 1.20.  If the compilation mode is
950104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * GLSL 1.10, the conversion is skipped.
960104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
970104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (state->language_version < 120)
980104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
990104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1000104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
1010104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *
1020104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    "There are no implicit array or structure conversions. For
1030104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    example, an array of int cannot be implicitly converted to an
1040104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    array of float. There are no implicit conversions between
1050104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    signed and unsigned integers."
1060104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1070104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* FINISHME: The above comment is partially a lie.  There is int/uint
1080104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * FINISHME: conversion for immediate constants.
1090104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
110bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (!to->is_float() || !from->type->is_numeric())
1110104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1120104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
113506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke   /* Convert to a floating point type with the same number of components
114506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke    * as the original type - i.e. int to float, not int to vec4.
115506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke    */
116506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke   to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
117506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke			        from->type->matrix_columns);
118506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke
119bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   switch (from->type->base_type) {
1200104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_INT:
1211660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
1220104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1230104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_UINT:
1241660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
1250104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1260104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_BOOL:
1271660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
128dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt      break;
1290104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   default:
1300104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      assert(0);
1310104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   }
1320104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1330104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   return true;
1340104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick}
1350104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1360104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
137a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
138bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
139a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       bool multiply,
140a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
141a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
142336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
143336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
1440104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
145a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
146a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
147a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic binary operators add (+), subtract (-),
148a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    multiply (*), and divide (/) operate on integer and
149a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    floating-point scalars, vectors, and matrices."
150a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
15160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   if (!type_a->is_numeric() || !type_b->is_numeric()) {
152a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
153a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Operands to arithmetic operators must be numeric");
1540471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
156a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
157a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
158a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If one operand is floating-point based and the other is
159a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    not, then the conversions from Section 4.1.10 "Implicit
160a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Conversions" are applied to the non-floating-point-based operand."
161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1620104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
1630104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
164a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
165a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Could not implicitly convert operands to "
166a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "arithmetic operator");
1670104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return glsl_type::error_type;
168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
169336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
170336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
171336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If the operands are integer types, they must both be signed or
173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    both be unsigned."
174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * From this rule and the preceeding conversion it can be inferred that
176a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
17760b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * The is_numeric check above already filtered out the case where either
17860b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * type is not one of these, so now the base types need only be tested for
17960b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * equality.
180a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
181a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type_a->base_type != type_b->base_type) {
182a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
183a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "base type mismatch for arithmetic operator");
1840471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
185a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
186a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
187a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All arithmetic binary operators result in the same fundamental type
188a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (signed integer, unsigned integer, or floating-point) as the
189a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operands they operate on, after operand type conversion. After
190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    conversion, the following cases are valid
191a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
192a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The two operands are scalars. In this case the operation is
193a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      applied, resulting in a scalar."
194a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
195cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar() && type_b->is_scalar())
196a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
197a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
198a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* One operand is a scalar, and the other is a vector or matrix.
199a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      In this case, the scalar operation is applied independently to each
200a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      component of the vector or matrix, resulting in the same size
201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector or matrix."
202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
203cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar()) {
204cb36f8aaeeb09660843316270a781948f773d90bIan Romanick      if (!type_b->is_scalar())
205a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_b;
206cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   } else if (type_b->is_scalar()) {
207a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
208a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
209a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
210a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
211a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * handled.
213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
21460b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_a->is_scalar());
21560b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_b->is_scalar());
216a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
217a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The two operands are vectors of the same size. In this case, the
218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operation is done component-wise resulting in the same size
219a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector."
220a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
221a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector() && type_b->is_vector()) {
222a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b) {
223a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
224a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      } else {
225a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 _mesa_glsl_error(loc, state,
226a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt			  "vector size mismatch for arithmetic operator");
227a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return glsl_type::error_type;
228a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      }
229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
230a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
231a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <vector, vector> have been handled.  At least one of the operands must
234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * be matrix.  Further, since there are no integer matrix types, the base
235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * type of both operands must be float.
236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
23760b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(type_a->is_matrix() || type_b->is_matrix());
238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_a->base_type == GLSL_TYPE_FLOAT);
239a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_b->base_type == GLSL_TYPE_FLOAT);
240a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
241a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The operator is add (+), subtract (-), or divide (/), and the
242a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operands are matrices with the same number of rows and the same
243a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      number of columns. In this case, the operation is done component-
244a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      wise resulting in the same size matrix."
245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The operator is multiply (*), where both operands are matrices or
246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      one operand is a vector and the other a matrix. A right vector
247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand is treated as a column vector and a left vector operand as a
248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      row vector. In all these cases, it is required that the number of
249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      columns of the left operand is equal to the number of rows of the
250a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      right operand. Then, the multiply (*) operation does a linear
251a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      algebraic multiply, yielding an object that has the same number of
252a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      rows as the left operand and the same number of columns as the right
253a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand. Section 5.10 "Vector and Matrix Operations" explains in
254a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      more detail how vectors and matrices are operated on."
255a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
256a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (! multiply) {
257a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b)
258a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
259a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
260fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      if (type_a->is_matrix() && type_b->is_matrix()) {
261c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* Matrix multiply.  The columns of A must match the rows of B.  Given
262c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the other previously tested constraints, this means the vector type
263c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of a row from A must be the same as the vector type of a column from
264c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
265c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  */
266c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b->column_type()) {
267c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	    /* The resulting matrix has the number of columns of matrix B and
268c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * the number of rows of matrix A.  We get the row count of A by
269c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * looking at the size of a vector that makes up a column.  The
270c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * transpose (size of a row) is done for B.
271c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     */
272a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    const glsl_type *const type =
273c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	       glsl_type::get_instance(type_a->base_type,
274c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_a->column_type()->vector_elements,
275c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_b->row_type()->vector_elements);
276a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    assert(type != glsl_type::error_type);
277a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
278a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    return type;
279a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
280fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      } else if (type_a->is_matrix()) {
281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* A is a matrix and B is a column vector.  Columns of A must match
282c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * rows of B.  Given the other previously tested constraints, this
283c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * means the vector type of a row from A must be the same as the
284c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * vector the type of B.
285a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
28647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 if (type_a->row_type() == type_b) {
28747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    /* The resulting vector has a number of elements equal to
28847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	     * the number of rows of matrix A. */
28947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    const glsl_type *const type =
29047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	       glsl_type::get_instance(type_a->base_type,
29147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       type_a->column_type()->vector_elements,
29247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       1);
29347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    assert(type != glsl_type::error_type);
29447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth
29547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    return type;
29647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 }
297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
298fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick	 assert(type_b->is_matrix());
299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
300c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* A is a row vector and B is a matrix.  Columns of A must match rows
301c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of B.  Given the other previously tested constraints, this means
302c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the type of A must be the same as the vector type of a column from
303c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
304a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
30547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 if (type_a == type_b->column_type()) {
30647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    /* The resulting vector has a number of elements equal to
30747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	     * the number of columns of matrix B. */
30847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    const glsl_type *const type =
30947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	       glsl_type::get_instance(type_a->base_type,
31047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       type_b->row_type()->vector_elements,
31147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       1);
31247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    assert(type != glsl_type::error_type);
31347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth
31447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    return type;
31547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 }
316a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
317a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
318a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
319a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      return glsl_type::error_type;
320a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
321a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
322a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
323a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All other cases are illegal."
324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
325a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3260471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
327a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
328a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
329a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
330a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
33165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholtunary_arithmetic_result_type(const struct glsl_type *type,
33265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt			     struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
333a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
334a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 57:
335a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic unary operators negate (-), post- and pre-increment
337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     and decrement (-- and ++) operate on integer or floating-point
338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     values (including vectors and matrices). All unary operators work
339a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     component-wise on their operands. These result with the same type
340a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     they operated on."
341a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
34265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (!type->is_numeric()) {
34365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
34465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to arithmetic operators must be numeric");
3450471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
34665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
347a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
348a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
349a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
350a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
353a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickmodulus_result_type(const struct glsl_type *type_a,
35465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    const struct glsl_type *type_b,
35565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
358a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The operator modulus (%) operates on signed or unsigned integers or
359a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    integer vectors. The operand types must both be signed or both be
360a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    unsigned."
361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
36240176e249f72b6090204611873b19aed3da67c71Ian Romanick   if (!type_a->is_integer() || !type_b->is_integer()
363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (type_a->base_type != type_b->base_type)) {
36465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "type mismatch");
3650471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
366a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
368a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operands cannot be vectors of differing size. If one operand is
369a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    a scalar and the other vector, then the scalar is applied component-
370a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    wise to the vector, resulting in the same type as the vector. If both
371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    are vectors of the same size, the result is computed component-wise."
372a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
373a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector()) {
374a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick      if (!type_b->is_vector()
375a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  || (type_a->vector_elements == type_b->vector_elements))
376a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_a;
377a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else
378a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_b;
379a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
380a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operator modulus (%) is not defined for any other data types
381a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (non-integer types)."
382a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
38365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3840471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
385a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
386a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
387a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
388a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
389bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
39065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
391a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
392336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
393336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
3940150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick
395a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
396a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The relational operators greater than (>), less than (<), greater
397a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    than or equal (>=), and less than or equal (<=) operate only on
398a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    scalar integer and scalar floating-point expressions."
399a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
400a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick   if (!type_a->is_numeric()
401a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick       || !type_b->is_numeric()
402cb36f8aaeeb09660843316270a781948f773d90bIan Romanick       || !type_a->is_scalar()
40365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt       || !type_b->is_scalar()) {
40465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
40565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to relational operators must be scalar and "
40665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "numeric");
4070471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
40865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
409a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
410a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "Either the operands' types must match, or the conversions from
411a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
412a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operand, after which the types must match."
413a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4140150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
4150150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
41665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
41765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Could not implicitly convert operands to "
41865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "relational operator");
4190150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick      return glsl_type::error_type;
420a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
421336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
422336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
423a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
42465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (type_a->base_type != type_b->base_type) {
42565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "base type mismatch");
4260471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
42765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
428a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
429a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The result is scalar Boolean."
430a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4310471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::bool_type;
432a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
433a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
434a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
4350bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/**
4360bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type
4370bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4380bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location.  If the types are
4390bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be
4400bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted.
4410bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4420bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return
4430bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
4440bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned.  This may be
4450bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion.
4460bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
4470bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note
4480bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to
4490bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values.
4500bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */
451fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
452336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholtvalidate_assignment(struct _mesa_glsl_parse_state *state,
453336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt		    const glsl_type *lhs_type, ir_rvalue *rhs)
4540bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{
455336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *rhs_type = rhs->type;
4560bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4570bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If there is already some error in the RHS, just return it.  Anything
4580bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    * else will lead to an avalanche of error message back to the user.
4590bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4600bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type->is_error())
4610bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4620bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4630bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If the types are identical, the assignment can trivially proceed.
4640bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
4650bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type == lhs_type)
4660bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
4670bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
4680157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   /* If the array element types are the same and the size of the LHS is zero,
4690157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * the assignment is okay.
4700157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    *
4710157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
4720157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * is handled by ir_dereference::is_lvalue.
4730157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    */
4740157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   if (lhs_type->is_array() && rhs->type->is_array()
4750157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->element_type() == rhs->type->element_type())
4760157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->array_size() == 0)) {
4770157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      return rhs;
4780157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   }
4790157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
480336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   /* Check for implicit conversion in GLSL 1.20 */
481336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   if (apply_implicit_conversion(lhs_type, rhs, state)) {
482336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt      rhs_type = rhs->type;
483336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt      if (rhs_type == lhs_type)
484336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt	 return rhs;
485336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   }
486336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
4870bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   return NULL;
4880bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick}
4890bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
49010a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue *
49110a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
49210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      ir_rvalue *lhs, ir_rvalue *rhs,
49310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      YYLTYPE lhs_loc)
49410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{
495953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
49610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
49710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
49810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (!error_emitted) {
49910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      /* FINISHME: This does not handle 'foo.bar.a.b.c[5].d = 5' */
50010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      if (!lhs->is_lvalue()) {
50110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
50210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 error_emitted = true;
50310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      }
50410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
50510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
506336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs);
50710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (new_rhs == NULL) {
50810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      _mesa_glsl_error(& lhs_loc, state, "type mismatch");
50910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   } else {
51010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      rhs = new_rhs;
5110157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
5120157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      /* If the LHS array was not declared with a size, it takes it size from
5130157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * the RHS.  If the LHS is an l-value and a whole array, it must be a
5140157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * dereference of a variable.  Any other case would require that the LHS
5150157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * is either not an l-value or not a whole array.
5160157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       */
5170157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      if (lhs->type->array_size() == 0) {
5180157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 ir_dereference *const d = lhs->as_dereference();
5190157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
5200157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(d != NULL);
5210157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
52236ea28646c666ac2af9b43c47e65f9f53ffcc390Ian Romanick	 ir_variable *const var = d->variable_referenced();
5230157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
5240157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(var != NULL);
5250157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
52663f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
52763f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    /* FINISHME: This should actually log the location of the RHS. */
52863f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
52963f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     "previous access",
53063f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     var->max_array_access);
53163f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 }
53263f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick
533f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick	 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
5340157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick						   rhs->type->array_size());
5359703ed05e684f4269cd8af27c94e9b6bf8781d85Eric Anholt	 d->type = var->type;
5360157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      }
53710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
53810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
5392731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt   /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
5402731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * but not post_inc) need the converted assigned value as an rvalue
5412731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * to handle things like:
5422731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
5432731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * i = j += 1;
5442731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
5452731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * So we always just store the computed value being assigned to a
5462731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * temporary and return a deref of that temporary.  If the rvalue
5472731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * ends up not being used, the temp will get copy-propagated out.
5482731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    */
5497e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
5507e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick					   ir_var_temporary);
551e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
552ae805922b7e3cdaf3aee26c3b799fe3608669bbaEric Anholt   instructions->push_tail(var);
553e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   instructions->push_tail(new(ctx) ir_assignment(deref_var,
5541660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  rhs,
5551660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  NULL));
556e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   deref_var = new(ctx) ir_dereference_variable(var);
5572731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt
5588e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick   if (!error_emitted)
5598e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick      instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
56010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
5611660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
56210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt}
5630bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
564de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue *
565959a9ecdd8fbc3375e4149f2b44d253622ff12eeEric Anholtget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
566de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{
5671660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(lvalue);
568de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_variable *var;
569de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
570de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* FINISHME: Give unique names to the temporaries. */
5717e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
5727e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick			      ir_var_temporary);
57343b5b03d67ce890e867c81d4a5cfc4871d711d43Eric Anholt   instructions->push_tail(var);
574de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->mode = ir_var_auto;
575de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
5761660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
5771660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  lvalue, NULL));
578de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
579de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* Once we've created this temporary, mark it read only so it's no
580de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    * longer considered an lvalue.
581de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    */
582de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->read_only = true;
583de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
5841660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
585de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt}
586de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
587de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
588fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
5890044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions,
59018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	      struct _mesa_glsl_parse_state *state)
59118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{
59218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) instructions;
59318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) state;
59418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
59518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   return NULL;
59618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
59718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
59818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
599fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
6000044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions,
60118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick		    struct _mesa_glsl_parse_state *state)
602a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
603953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
604a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   static const int operations[AST_NUM_OPERATORS] = {
605a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_assign doesn't convert to ir_expression. */
606a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_plus doesn't convert to ir_expression. */
607a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_neg,
608a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,
609a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,
610a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,
611a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,
612a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,
613a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,
614a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,
615a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_less,
616a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_greater,
617a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lequal,
618a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_gequal,
619a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_equal,
620a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_nequal,
621a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and,
622a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor,
623a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,
624a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_bit_not,
625a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_and,
626a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_xor,
627a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_or,
628a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_logic_not,
629a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
630a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Note: The following block of expression types actually convert
631a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * to multiple IR instructions.
632a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
633a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,     /* ast_mul_assign */
634a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,     /* ast_div_assign */
635a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,     /* ast_mod_assign */
636a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,     /* ast_add_assign */
637a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,     /* ast_sub_assign */
638a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,  /* ast_ls_assign */
639a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,  /* ast_rs_assign */
640a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and, /* ast_and_assign */
641a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor, /* ast_xor_assign */
642a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,  /* ast_or_assign */
643a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
644a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_conditional doesn't convert to ir_expression. */
645de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_pre_inc. */
646de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_pre_dec. */
647de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_post_inc. */
648de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_post_dec. */
649a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_field_selection doesn't conv to ir_expression. */
650a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_array_index doesn't convert to ir_expression. */
651a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_function_call doesn't conv to ir_expression. */
652a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_identifier doesn't convert to ir_expression. */
653a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_int_constant doesn't convert to ir_expression. */
654a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
655a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_float_constant doesn't conv to ir_expression. */
656a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
657a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_sequence doesn't convert to ir_expression. */
658a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   };
659fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *result = NULL;
6601c325af4d6b907e0a47ab7f868a2a78f054f153fAras Pranckevicius   ir_rvalue *op[3];
6610471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   const struct glsl_type *type = glsl_type::error_type;
662a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   bool error_emitted = false;
663a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc;
664a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
66518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   loc = this->get_location();
666a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
66718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   switch (this->oper) {
6686652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   case ast_assign: {
66918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
67018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
671a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
67210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], op[1],
67310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
67410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = result->type->is_error();
67510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
676a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
6776652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   }
678a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
679a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_plus:
68018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
681a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
682c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
683c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth
684c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      error_emitted = type->is_error();
685a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
686a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = op[0];
687a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
688a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
689a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_neg:
69018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
691a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
69265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
693a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
69465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
695a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
6961660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
6971660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
698a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
699a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
700a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add:
701a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub:
702a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul:
703a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div:
70418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
70518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
706a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
707bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
70818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul),
709a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
710a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      error_emitted = type->is_error();
711a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7121660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7131660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
715a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
716a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mod:
71718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
71818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
719a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
72065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
721a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
72218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(operations[this->oper] == ir_binop_mod);
723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7241660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7251660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
72665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
727a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
728a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
729a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lshift:
730a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rshift:
731183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-shift operators");
732183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
733a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
734a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
735a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_less:
736a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_greater:
737a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lequal:
738a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_gequal:
73918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
74018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
741a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
74265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = relational_result_type(op[0], op[1], state, & loc);
743a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
744a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The relational operators must either generate an error or result
745a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
746a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
747a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      assert(type->is_error()
748a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     || ((type->base_type == GLSL_TYPE_BOOL)
749cb36f8aaeeb09660843316270a781948f773d90bIan Romanick		 && type->is_scalar()));
750a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7511660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
7521660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
75365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
754a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
755a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
756a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_nequal:
757a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_equal:
7586e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
7596e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
7606e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7616e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
7626e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *
7636e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    "The equality operators equal (==), and not equal (!=)
7646e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    operate on all types. They result in a scalar Boolean. If
7656e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    the operand types do not match, then there must be a
7666e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    conversion from Section 4.1.10 "Implicit Conversions"
7676e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    applied to one operand that can make them match, in which
7686e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    case this conversion is done."
7696e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       */
770bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[0]->type, op[1], state)
771bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[1]->type, op[0], state))
772212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick	  || (op[0]->type != op[1]->type)) {
7736e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
7746e659caaa946339a2de3890a8bed091ccb65102aIan Romanick			  "type", (this->oper == ast_equal) ? "==" : "!=");
7756e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 error_emitted = true;
776a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick      } else if ((state->language_version <= 110)
777a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick		 && (op[0]->type->is_array() || op[1]->type->is_array())) {
778a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
779a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick			  "GLSL 1.10");
780a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 error_emitted = true;
7816e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      }
7826e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7831660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
7841660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
7856e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      type = glsl_type::bool_type;
7866e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
7876e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      assert(result->type == glsl_type::bool_type);
788a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
789a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
790a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_and:
791a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_xor:
792a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_or:
793a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_not:
794183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state, "FINISHME: implement bit-wise operators");
795183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
796a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
797a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
7984950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_and: {
799b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
800b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
801b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
802b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
803b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
804b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
805b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt			  operator_string(this->oper));
806ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
807b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      }
808b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
80944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      ir_constant *op0_const = op[0]->constant_expression_value();
81044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      if (op0_const) {
81144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (op0_const->value.b[0]) {
81244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    op[1] = this->subexpressions[1]->hir(instructions, state);
81344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
81444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
81544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       YYLTYPE loc = this->subexpressions[1]->get_location();
81644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
81744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       _mesa_glsl_error(& loc, state,
81844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				"RHS of `%s' must be scalar boolean",
81944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				operator_string(this->oper));
82044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       error_emitted = true;
82144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    }
82244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op[1];
82344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 } else {
82444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op0_const;
82544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
82644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = glsl_type::bool_type;
82744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
82881d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
8297e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "and_tmp",
8307e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
83181d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(tmp);
83281d664f099a5fd5fac777480532fb4307d591451Ian Romanick
8331660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
83444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 instructions->push_tail(stmt);
8354950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
83644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
8374950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
83844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
83944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    YYLTYPE loc = this->subexpressions[1]->get_location();
8404950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
84144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    _mesa_glsl_error(& loc, state,
84244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     "RHS of `%s' must be scalar boolean",
84344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     operator_string(this->oper));
84444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    error_emitted = true;
84544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
846b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
8471660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
84844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
8491660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
85044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
8514950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8521660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
85344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
8541660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
85544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
8564950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8571660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
85844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
85944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
8604950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
8614950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
8624950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8634950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_or: {
8644950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
8654950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8664950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
8674950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
8684950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
8694950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
8704950a68bf22ede6f4f368c9783e5401816159574Eric Anholt			  operator_string(this->oper));
8714950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 error_emitted = true;
8724950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      }
8734950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
87444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      ir_constant *op0_const = op[0]->constant_expression_value();
87544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      if (op0_const) {
87644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (op0_const->value.b[0]) {
87744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op0_const;
87844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 } else {
87944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    op[1] = this->subexpressions[1]->hir(instructions, state);
88044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
88144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
88244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       YYLTYPE loc = this->subexpressions[1]->get_location();
88344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
88444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       _mesa_glsl_error(& loc, state,
88544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				"RHS of `%s' must be scalar boolean",
88644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				operator_string(this->oper));
88744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       error_emitted = true;
88844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    }
88944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op[1];
89044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
89144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = glsl_type::bool_type;
89244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
893dfd30ca6a95a7d95835dad78ffe1fba4d1f4ef69Kenneth Graunke	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
8947e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "or_tmp",
8957e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
8960b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
8974950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
89881d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_if *const stmt = new(ctx) ir_if(op[0]);
89981d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(stmt);
90081d664f099a5fd5fac777480532fb4307d591451Ian Romanick
901a0879b9dd438d78635f047cdd5ed4c72bc831b60Eric Anholt	 op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state);
9024950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
90344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
90444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    YYLTYPE loc = this->subexpressions[1]->get_location();
9054950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
90644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
90744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     operator_string(this->oper));
90844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    error_emitted = true;
90944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
9104950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9111660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
91244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
9131660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
91444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
9154950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9161660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
91744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
9181660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[1], NULL);
91944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
9204950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9211660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
92244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
92344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
9244950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
9254950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
9264950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9274950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_xor:
9284950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
9294950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
9304950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9314950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
9321660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
9331660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
934ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
935a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
936a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
937a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt   case ast_logic_not:
938a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
939a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
940a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
941a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
942a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
943a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 _mesa_glsl_error(& loc, state,
944a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt			  "operand of `!' must be scalar boolean");
945ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
946a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      }
947a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
9481660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
9491660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
950ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
951a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      break;
952a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
953a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul_assign:
954a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div_assign:
955a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add_assign:
956a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub_assign: {
95718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
95818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
959a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
960bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
96118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul_assign),
962a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
963a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
9641660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
9651660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						   op[0], op[1]);
966a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
9673e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
9688273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			     op[0]->clone(ctx, NULL), temp_rhs,
96910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
97010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
97110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = (op[0]->type->is_error());
972a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
973a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
974a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * explicitly test for this because none of the binary expression
975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * operators allow array operands either.
976a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
977a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
978a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
979a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
980a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
98148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   case ast_mod_assign: {
98248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
98348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
98448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
98565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
98648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
98748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      assert(operations[this->oper] == ir_binop_mod);
98848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
989768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
9901660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
9911660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
99248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
9933e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
9948273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			     op[0]->clone(ctx, NULL), temp_rhs,
99548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt			     this->subexpressions[0]->get_location());
99648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      type = result->type;
99765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
99848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      break;
99948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   }
1000a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1001a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_ls_assign:
1002a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rs_assign:
1003183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
1004183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement bit-shift assignment operators");
1005183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
1006251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1007a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1008a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_and_assign:
1009a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_xor_assign:
1010a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_or_assign:
1011183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      _mesa_glsl_error(& loc, state,
1012183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt		       "FINISHME: implement logic assignment operators");
1013183d8c63947fcfab45c9f2a8a8a6fc311e8b1552Eric Anholt      error_emitted = true;
1014251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1015a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
101696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   case ast_conditional: {
101796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
101896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
101996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
102096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
102196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    "The ternary selection operator (?:). It operates on three
102296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
102396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    first expression, which must result in a scalar Boolean."
102496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
102596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
102696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[0]->get_location();
102796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
102896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
102996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
103096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
103196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
103296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* The :? operator is implemented by generating an anonymous temporary
103396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * followed by an if-statement.  The last instruction in each branch of
103496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * the if-statement assigns a value to the anonymous temporary.  This
103596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * temporary is the r-value of the expression.
103696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
10370ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list then_instructions;
10380ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list else_instructions;
103996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
10400ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[1] = this->subexpressions[1]->hir(&then_instructions, state);
10410ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[2] = this->subexpressions[2]->hir(&else_instructions, state);
104296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
104396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
104496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
104596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     "The second and third expressions can be any type, as
104696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     long their types match, or there is a conversion in
104796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     Section 4.1.10 "Implicit Conversions" that can be applied
104896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     to one of the expressions to make their types match. This
104996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     resulting matching type is the type of the entire
105096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     expression."
105196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
1052bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1053bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[2]->type, op[1], state))
1054db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	  || (op[1]->type != op[2]->type)) {
105596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[1]->get_location();
105696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
105796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
105896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick			  "operator must have matching types.");
105996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
10600ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = glsl_type::error_type;
1061db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick      } else {
10620ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = op[1]->type;
106396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
106496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
10657825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *cond_val = op[0]->constant_expression_value();
10667825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *then_val = op[1]->constant_expression_value();
10677825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *else_val = op[2]->constant_expression_value();
10687825d3d15710fdfcfc503754862963aac8065480Ian Romanick
10697825d3d15710fdfcfc503754862963aac8065480Ian Romanick      if (then_instructions.is_empty()
10707825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && else_instructions.is_empty()
10717825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
10727825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 result = (cond_val->value.b[0]) ? then_val : else_val;
10737825d3d15710fdfcfc503754862963aac8065480Ian Romanick      } else {
10747e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	 ir_variable *const tmp =
10757e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	    new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
10760b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
10770ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
10781660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
10797825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 instructions->push_tail(stmt);
10800ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
10817825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 then_instructions.move_nodes_to(& stmt->then_instructions);
10821660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref =
10831660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
10847825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const then_assign =
10851660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
10867825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->then_instructions.push_tail(then_assign);
10870ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
10887825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 else_instructions.move_nodes_to(& stmt->else_instructions);
10891660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref =
10901660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
10917825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const else_assign =
10921660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[2], NULL);
10937825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->else_instructions.push_tail(else_assign);
10940ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
10951660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
10967825d3d15710fdfcfc503754862963aac8065480Ian Romanick      }
1097251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
109896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   }
1099a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_pre_inc:
110176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   case ast_pre_dec: {
110276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
110376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
11041660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1.0f);
110576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      else
11061660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1);
110776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1108a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
110976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1110768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
11111660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
11121660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
111376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
11143e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
11158273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			     op[0]->clone(ctx, NULL), temp_rhs,
111676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt			     this->subexpressions[0]->get_location());
111776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      type = result->type;
111876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      error_emitted = op[0]->type->is_error();
111976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      break;
112076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   }
1121a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1122a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_post_inc:
1123de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   case ast_post_dec: {
1124de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
1125de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
11261660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1.0f);
1127de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      else
11281660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1);
1129de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1130de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1131de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1132a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1133de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1134768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
11351660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
11361660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
1137de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1138de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      /* Get a temporary of a copy of the lvalue before it's modified.
1139de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       * This may get thrown away later.
1140de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       */
11418273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1142de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
11433e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      (void)do_assignment(instructions, state,
11448273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			  op[0]->clone(ctx, NULL), temp_rhs,
1145de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			  this->subexpressions[0]->get_location());
1146de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1147de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      type = result->type;
1148de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error();
1149a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1150de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   }
1151a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1152a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_field_selection:
115318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1154a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1156a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
115727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   case ast_array_index: {
115827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      YYLTYPE index_loc = subexpressions[1]->get_location();
115927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
116027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[0] = subexpressions[0]->hir(instructions, state);
116127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[1] = subexpressions[1]->hir(instructions, state);
116227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
116327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
116427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1165a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick      ir_rvalue *const array = op[0];
1166b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
11671660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_array(op[0], op[1]);
1168b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1169b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      /* Do not use op[0] after this point.  Use array.
1170b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       */
1171b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      op[0] = NULL;
1172b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
117327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
117427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
117527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 break;
117627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
117763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick      if (!array->type->is_array()
117863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_matrix()
117963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_vector()) {
118027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
118163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "cannot dereference non-array / non-matrix / "
118263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "non-vector");
118327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
118427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
118527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
118627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (!op[1]->type->is_integer()) {
118727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
118827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be integer type");
118927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
119027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      } else if (!op[1]->type->is_scalar()) {
119127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
119227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be scalar");
119327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
119427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
119527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
119627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      /* If the array index is a constant expression and the array has a
119727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size, ensure that the access is in-bounds.  If the array
119827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * index is not a constant expression, ensure that the array has a
119927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size.
120027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       */
120127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      ir_constant *const const_index = op[1]->constant_expression_value();
120227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (const_index != NULL) {
120327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 const int idx = const_index->value.i[0];
120463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 const char *type_name;
120563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 unsigned bound = 0;
120663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick
120763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
120863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "matrix";
120963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
121063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "vector";
121163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
121263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "array";
121363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
121427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
121527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
121627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *
121727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    "It is illegal to declare an array with a size, and then
121827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    later (in the same shader) index the same array with an
121927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    integral constant expression greater than or equal to the
122027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    declared size. It is also illegal to index an array with a
122127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    negative constant expression."
122227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  */
122363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
122463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->row_type()->vector_elements <= idx) {
122563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->row_type()->vector_elements;
122663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
122763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
122863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->vector_elements <= idx) {
122963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->vector_elements;
123063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
123163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
123263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((array->type->array_size() > 0)
123363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick		&& (array->type->array_size() <= idx)) {
123463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->array_size();
123563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
123627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
123727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
123863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (bound > 0) {
123963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be < %u",
124063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name, bound);
124163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    error_emitted = true;
124263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (idx < 0) {
124363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be >= 0",
124463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name);
124527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    error_emitted = true;
124627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
1247b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
124863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_array()) {
1249a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    /* If the array is a variable dereference, it dereferences the
1250a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * whole array, by definition.  Use this to get the variable.
1251a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     *
1252a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: Should some methods for getting / setting / testing
1253a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: array access limits be added to ir_dereference?
1254a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     */
1255a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    ir_variable *const v = array->whole_variable_referenced();
125663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((v != NULL) && (unsigned(idx) > v->max_array_access))
125763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       v->max_array_access = idx;
125863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
12592b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke      } else if (array->type->array_size() == 0) {
12602b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
1261a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt      } else {
1262a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 if (array->type->is_array()) {
1263a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	    ir_variable *v = array->whole_variable_referenced();
1264a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	    v->max_array_access = array->type->array_size();
1265a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 }
126627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
126727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
126827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
126927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 result->type = glsl_type::error_type;
127027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
127127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      type = result->type;
1272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
127327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   }
1274a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_function_call:
12767cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      /* Should *NEVER* get here.  ast_function_call should always be handled
12777cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick       * by ast_function_expression::hir.
1278a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
12797cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      assert(0);
1280a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_identifier: {
1283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* ast_identifier can appear several places in a full abstract syntax
1284a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * tree.  This particular use must be at location specified in the grammar
1285a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * as 'variable_identifier'.
1286a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
12878bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      ir_variable *var =
12888bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick	 state->symbols->get_variable(this->primary_expression.identifier);
1289a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
12901660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_variable(var);
1291a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1292a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (var != NULL) {
1293a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 type = result->type;
1294a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
129571d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
129618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->primary_expression.identifier);
1297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 error_emitted = true;
1299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1302a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1303a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_int_constant:
13040471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::int_type;
13051660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.int_constant);
1306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1307a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_uint_constant:
13090471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::uint_type;
13101660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1312a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_float_constant:
13140471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::float_type;
13151660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.float_constant);
1316a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1317a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1318a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bool_constant:
13190471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::bool_type;
13201660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1321a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1322a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1323a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sequence: {
1324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* It should not be possible to generate a sequence in the AST without
1325a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * any expressions in it.
1326a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1327304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      assert(!this->expressions.is_empty());
1328a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1329a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The r-value of a sequence is the last expression in the sequence.  If
1330a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the other expressions in the sequence do not have side-effects (and
1331a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * therefore add instructions to the instruction list), they get dropped
1332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * on the floor.
1333a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
13342b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_node, ast, link, &this->expressions)
1335304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick	 result = ast->hir(instructions, state);
1336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1339a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Any errors should have already been emitted in the loop above.
1340a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1341a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      error_emitted = true;
1342a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1346cef3baecf636a30b62cd7a1e8de57c7650f7003eIan Romanick   if (type->is_error() && !error_emitted)
134771d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick      _mesa_glsl_error(& loc, state, "type mismatch");
1348a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1349a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
1350a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1353fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
13540044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions,
135518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* It is possible to have expression statements that don't have an
1358a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * expression.  This is the solitary semicolon:
1359a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1360a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for (i = 0; i < 5; i++)
1361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     ;
1362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * In this case the expression will be NULL.  Test for NULL and don't do
1364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * anything in that case.
1365a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
136618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (expression != NULL)
136718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      expression->hir(instructions, state);
1368a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1369a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Statements do not have r-values.
1370a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1372a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1373a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1374a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1375fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
13760044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions,
137718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			    struct _mesa_glsl_parse_state *state)
1378a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
137918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
13808bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->push_scope();
1381a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
13822b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, &this->statements)
1383304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
1384a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
138518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
13868bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->pop_scope();
1387a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1388a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Compound statements do not have r-values.
1389a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1390a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1391a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1392a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1393a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
139428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type *
139528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickprocess_array_type(const glsl_type *base, ast_node *array_size,
139628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick		   struct _mesa_glsl_parse_state *state)
139728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{
139828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   unsigned length = 0;
139928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
140028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   /* FINISHME: Reject delcarations of multidimensional arrays. */
140128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
140228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   if (array_size != NULL) {
140328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      exec_list dummy_instructions;
140428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
140528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      YYLTYPE loc = array_size->get_location();
140628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
140728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      /* FINISHME: Verify that the grammar forbids side-effects in array
140828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       * FINISHME: sizes.   i.e., 'vec4 [x = 12] data'
140928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       */
141028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      assert(dummy_instructions.is_empty());
141128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
141228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      if (ir != NULL) {
141328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 if (!ir->type->is_integer()) {
141428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be integer type");
141528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else if (!ir->type->is_scalar()) {
141628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be scalar type");
141728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else {
141828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    ir_constant *const size = ir->constant_expression_value();
141928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
142028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    if (size == NULL) {
142128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be a "
142228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick				"constant valued expression");
142328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else if (size->value.i[0] <= 0) {
142428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > 0");
142528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else {
142628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       assert(size->type == ir->type);
142728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       length = size->value.u[0];
142828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    }
142928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 }
143028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
143128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   }
143228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1433f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick   return glsl_type::get_array_instance(base, length);
143428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick}
143528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
143628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1437d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type *
1438d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name,
1439d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick			      struct _mesa_glsl_parse_state *state) const
1440a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1441d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   const struct glsl_type *type;
1442a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
14433455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   if ((this->type_specifier == ast_struct) && (this->type_name == NULL)) {
1444a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Handle annonymous structures. */
1445a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = NULL;
1446a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
1447d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      type = state->symbols->get_type(this->type_name);
1448d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      *name = this->type_name;
1449a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1450d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick      if (this->is_array) {
1451d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick	 type = process_array_type(type, this->array_size, state);
145228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
1453a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1454a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1455a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
1456a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1457a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1458a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1459a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1460a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1461768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick				 ir_variable *var,
14622e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 struct _mesa_glsl_parse_state *state,
14632e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 YYLTYPE *loc)
1464a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1465a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->invariant)
1466a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->invariant = 1;
1467a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1468a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Mark 'in' variables at global scope as read-only. */
1469a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->constant || qual->attribute || qual->uniform
1470a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (qual->varying && (state->target == fragment_shader)))
1471a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->read_only = 1;
1472a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1473a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->centroid)
1474a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->centroid = 1;
1475a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1476ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick   if (qual->attribute && state->target != vertex_shader) {
14772e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      var->type = glsl_type::error_type;
14782e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      _mesa_glsl_error(loc, state,
14792e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "`attribute' variables may not be declared in the "
1480ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       "%s shader",
1481ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       _mesa_glsl_shader_target_name(state->target));
14822e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   }
14832e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
148490b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
148590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *
148690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     "The varying qualifier can be used only with the data types
148790b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
148890b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     these."
148990b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    */
14900ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt   if (qual->varying) {
14910ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      const glsl_type *non_array_type;
14920ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
14930ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (var->type && var->type->is_array())
14940ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type->fields.array;
14950ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      else
14960ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type;
14970ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
14980ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
14990ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 var->type = glsl_type::error_type;
15000ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 _mesa_glsl_error(loc, state,
15010ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt			  "varying variables must be of base type float");
15020ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      }
150390b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   }
150490b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt
15057e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   /* If there is no qualifier that changes the mode of the variable, leave
15067e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    * the setting alone.
15077e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    */
1508a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->in && qual->out)
1509a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_inout;
1510a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->attribute || qual->in
1511a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    || (qual->varying && (state->target == fragment_shader)))
1512a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_in;
15130b678234625fac67a89285ad2871dedc891fb1b1Ian Romanick   else if (qual->out || (qual->varying && (state->target == vertex_shader)))
1514a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_out;
1515a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->uniform)
1516a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_uniform;
1517a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1518a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (qual->flat)
1519a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_flat;
1520a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else if (qual->noperspective)
1521a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_noperspective;
1522a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
1523a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_smooth;
15249d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick
15254a962170d7cf4243d6ae156fca20a6167388925dEric Anholt   var->pixel_center_integer = qual->pixel_center_integer;
15264a962170d7cf4243d6ae156fca20a6167388925dEric Anholt   var->origin_upper_left = qual->origin_upper_left;
15278d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick   if ((qual->origin_upper_left || qual->pixel_center_integer)
15288d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick       && (strcmp(var->name, "gl_FragCoord") != 0)) {
15298d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick      const char *const qual_string = (qual->origin_upper_left)
15308d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick	 ? "origin_upper_left" : "pixel_center_integer";
15318d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
15328d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick      _mesa_glsl_error(loc, state,
15338d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "layout qualifier `%s' can only be applied to "
15348d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "fragment shader input `gl_FragCoord'",
15358d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       qual_string);
15368d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick   }
15378d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
15389d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   if (var->type->is_array() && (state->language_version >= 120)) {
15399d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick      var->array_lvalue = true;
15409d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   }
1541a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1542a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1543a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1544fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
15450044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions,
154618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			 struct _mesa_glsl_parse_state *state)
1547a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1548953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
1549a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *decl_type;
1550a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *type_name = NULL;
15518558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   ir_rvalue *result = NULL;
1552c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   YYLTYPE loc = this->get_location();
1553a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
15546f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
15556f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
15566f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     "To ensure that a particular output variable is invariant, it is
15576f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     necessary to use the invariant qualifier. It can either be used to
15586f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     qualify a previously declared variable as being invariant
15596f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
15606f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *         invariant gl_Position; // make existing gl_Position be invariant"
15616f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
15626f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * In these cases the parser will set the 'invariant' flag in the declarator
15636f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * list, and the type will be NULL.
15646f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    */
15656f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   if (this->invariant) {
15666f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      assert(this->type == NULL);
15676f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
15686f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (state->current_function != NULL) {
15696f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 _mesa_glsl_error(& loc, state,
15706f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "All uses of `invariant' keyword must be at global "
15716f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "scope\n");
15726f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
15736f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
15746f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
15756f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(!decl->is_array);
15766f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->array_size == NULL);
15776f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->initializer == NULL);
15786f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
15796f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 ir_variable *const earlier =
15806f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    state->symbols->get_variable(decl->identifier);
15816f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 if (earlier == NULL) {
15826f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
15836f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "Undeclared variable `%s' cannot be marked "
15846f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "invariant\n", decl->identifier);
15856f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == vertex_shader)
15866f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_out)) {
15876f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
15886f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
15896f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", decl->identifier);
15906f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == fragment_shader)
15916f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_in)) {
15926f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
15936f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
15946f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", decl->identifier);
15956f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else {
15966f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    earlier->invariant = true;
15976f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
15986f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
15996f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16006f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      /* Invariant redeclarations do not have r-values.
16016f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       */
16026f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      return NULL;
16036f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   }
16046f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16056f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(this->type != NULL);
16066f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(!this->invariant);
16076f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
16083455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* The type specifier may contain a structure definition.  Process that
16093455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * before any of the variable declarations.
16103455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
16113455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   (void) this->type->specifier->hir(instructions, state);
16123455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
1613d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   decl_type = this->type->specifier->glsl_type(& type_name, state);
1614304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick   if (this->declarations.is_empty()) {
16156f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      /* The only valid case where the declaration list can be empty is when
16166f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       * the declaration is setting the default precision of a built-in type
16176f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       * (e.g., 'precision highp vec4;').
1618c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick       */
1619c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick
16206f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (decl_type != NULL) {
1621c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      } else {
1622c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick	    _mesa_glsl_error(& loc, state, "incomplete declaration");
1623c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      }
1624c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   }
1625a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16262b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1627a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      const struct glsl_type *var_type;
1628768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_variable *var;
1629a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1630a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Emit a warning if a variable declaration shadows a
1631a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: declaration at a higher scope.
1632a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1633a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1634cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick      if ((decl_type == NULL) || decl_type->is_void()) {
1635a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (type_name != NULL) {
1636a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1637a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type `%s' in declaration of `%s'",
1638a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     type_name, decl->identifier);
1639a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 } else {
1640a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1641a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type in declaration of `%s'",
1642a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     decl->identifier);
1643a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
1644a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
1645a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1646a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1647a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (decl->is_array) {
164828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 var_type = process_array_type(decl_type, decl->array_size, state);
1649a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
1650a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 var_type = decl_type;
1651a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1652a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16537e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick      var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
1654a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16553f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
16563f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
16573f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     "Global variables can only use the qualifiers const,
16583f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     attribute, uni form, or varying. Only one may be
16593f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     specified.
16603f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
16613f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     Local variables can only use the qualifier const."
16623f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
16633f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       * This is relaxed in GLSL 1.30.
16643f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       */
16653f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      if (state->language_version < 120) {
16663f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 if (this->type->qualifier.out) {
16673f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
16683f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`out' qualifier in declaration of `%s' "
16693f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "only valid for function parameters in GLSL 1.10.",
16703f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
16713f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
16723f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 if (this->type->qualifier.in) {
16733f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
16743f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`in' qualifier in declaration of `%s' "
16753f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "only valid for function parameters in GLSL 1.10.",
16763f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
16773f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
16783f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 /* FINISHME: Test for other invalid qualifiers. */
16793f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      }
16803f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt
16812e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
16822e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				       & loc);
1683a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16846f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (this->type->qualifier.invariant) {
1685046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
1686046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt						   var->mode == ir_var_inout)) {
1687046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
1688046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature outval
1689046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
16906f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
16916f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
16926f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", var->name);
1693046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 } else if ((state->target == fragment_shader) &&
1694046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt		    !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
1695046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
1696046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature inval
1697046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
16986f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
16996f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
17006f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", var->name);
17016f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
17026f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
17036f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
1704e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      if (state->current_function != NULL) {
1705b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 const char *mode = NULL;
1706e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 const char *extra = "";
1707b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1708e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 /* There is no need to check for 'inout' here because the parser will
1709e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	  * only allow that in function parameter lists.
1710e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	  */
1711e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 if (this->type->qualifier.attribute) {
1712b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "attribute";
1713b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 } else if (this->type->qualifier.uniform) {
1714b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "uniform";
1715b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 } else if (this->type->qualifier.varying) {
1716b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "varying";
1717e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 } else if (this->type->qualifier.in) {
1718e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "in";
1719e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1720e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 } else if (this->type->qualifier.out) {
1721e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "out";
1722e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1723b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 }
1724b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1725b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 if (mode) {
1726e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	    _mesa_glsl_error(& loc, state,
1727b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick			     "%s variable `%s' must be declared at "
1728e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     "global scope%s",
1729e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     mode, var->name, extra);
1730e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 }
1731e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      } else if (var->mode == ir_var_in) {
1732fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 if (state->target == vertex_shader) {
1733fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    bool error_emitted = false;
1734fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1735fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1736fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1737fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
1738fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
1739fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors. Vertex shader inputs can also form arrays of these
1740fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    types, but not structures."
1741fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
17422d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
17432d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
17442d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
17452d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
17462d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors. They cannot be arrays or structures."
17472d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
1748fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
1749fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
1750fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "The attribute qualifier can be used only with float,
1751fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    floating-point vectors, and matrices. Attribute variables
1752fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    cannot be declared as arrays or structures."
1753fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     */
1754fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    const glsl_type *check_type = var->type->is_array()
1755fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       ? var->type->fields.array : var->type;
1756fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1757fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    switch (check_type->base_type) {
1758fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_FLOAT:
1759fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       break;
1760fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_UINT:
1761fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_INT:
1762fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       if (state->language_version > 120)
1763fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		  break;
1764fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       /* FALLTHROUGH */
1765fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    default:
1766fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
1767fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
1768fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"type %s`%s'",
1769fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				var->type->is_array() ? "array of " : "",
1770fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				check_type->name);
1771fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
1772fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
1773fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
17742d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    if (!error_emitted && (state->language_version <= 130)
1775fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		&& var->type->is_array()) {
1776fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
1777fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
1778fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"array type");
1779fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
1780fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
1781fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 }
1782fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick      }
1783fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
1784e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick      /* Process the initializer and add its instructions to a temporary
1785e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * list.  This list will be added to the instruction stream (below) after
1786e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * the declaration is added.  This is done because in some cases (such as
1787e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * redeclarations) the declaration may not actually be added to the
1788e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * instruction stream.
1789e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       */
1790fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      exec_list initializer_instructions;
179166faec4895b7bb59a614087a200c05157191b4aeIan Romanick      if (decl->initializer != NULL) {
179243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 YYLTYPE initializer_loc = decl->initializer->get_location();
179343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
179466faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
179566faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *
179666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    "All uniform variables are read-only and are initialized either
179766faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    directly by an application via API commands, or indirectly by
179866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    OpenGL."
179966faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
180066faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 if ((state->language_version <= 110)
180166faec4895b7bb59a614087a200c05157191b4aeIan Romanick	     && (var->mode == ir_var_uniform)) {
180243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
180343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize uniforms in GLSL 1.10");
180443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
180543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
180643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if (var->type->is_sampler()) {
180743de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
180843de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize samplers");
180943de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
181019360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
181143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
181243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
181343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize %s shader input / %s",
1814ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick			     _mesa_glsl_shader_target_name(state->target),
181543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     (state->target == vertex_shader)
181643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     ? "attribute" : "varying");
181766faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
181866faec4895b7bb59a614087a200c05157191b4aeIan Romanick
18191660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
1820fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt	 ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions,
1821e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick						 state);
182219360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
1823ce030884064046925a655413097dd8257e9392ddIan Romanick	 /* Calculate the constant value if this is a const or uniform
1824307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	  * declaration.
182566faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
1826ce030884064046925a655413097dd8257e9392ddIan Romanick	 if (this->type->qualifier.constant || this->type->qualifier.uniform) {
1827e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs);
1828e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    if (new_rhs != NULL) {
1829e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	       rhs = new_rhs;
1830e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    } else {
1831e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	       _mesa_glsl_error(&initializer_loc, state,
1832e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke			        "initializer of type %s cannot be assigned to "
1833e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke				"variable of type %s",
1834e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke				rhs->type->name, var->type->name);
1835e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    }
1836e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke
1837326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	    ir_constant *constant_value = rhs->constant_expression_value();
1838326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	    if (!constant_value) {
1839307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	       _mesa_glsl_error(& initializer_loc, state,
1840ce030884064046925a655413097dd8257e9392ddIan Romanick				"initializer of %s variable `%s' must be a "
1841307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt				"constant expression",
1842ce030884064046925a655413097dd8257e9392ddIan Romanick				(this->type->qualifier.constant)
1843ce030884064046925a655413097dd8257e9392ddIan Romanick				? "const" : "uniform",
1844307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt				decl->identifier);
1845326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	    } else {
1846326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	       rhs = constant_value;
1847326c676236e6a3c90db63e4d0c893aa4f9c21876Eric Anholt	       var->constant_value = constant_value;
1848307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	    }
1849307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 }
185066faec4895b7bb59a614087a200c05157191b4aeIan Romanick
1851307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 if (rhs && !rhs->type->is_error()) {
1852ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    bool temp = var->read_only;
1853ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    if (this->type->qualifier.constant)
1854ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	       var->read_only = false;
1855ce030884064046925a655413097dd8257e9392ddIan Romanick
1856ce030884064046925a655413097dd8257e9392ddIan Romanick	    /* Never emit code to initialize a uniform.
1857ce030884064046925a655413097dd8257e9392ddIan Romanick	     */
1858ce030884064046925a655413097dd8257e9392ddIan Romanick	    if (!this->type->qualifier.uniform)
1859fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt	       result = do_assignment(&initializer_instructions, state,
1860fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt				      lhs, rhs,
1861ce030884064046925a655413097dd8257e9392ddIan Romanick				      this->get_location());
1862ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    var->read_only = temp;
186366faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
186466faec4895b7bb59a614087a200c05157191b4aeIan Romanick      }
186517d86f4371da413176ba365ca26a58bac172d365Ian Romanick
18660ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
18670ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *
18680ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *     "It is an error to write to a const variable outside of
18690ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      its declaration, so they must be initialized when
18700ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      declared."
18710ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       */
18720ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      if (this->type->qualifier.constant && decl->initializer == NULL) {
18730ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	 _mesa_glsl_error(& loc, state,
18740ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt			  "const declaration of `%s' must be initialized");
18750ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      }
18760ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt
18775466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick      /* Attempt to add the variable to the symbol table.  If this fails, it
18785466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       * means the variable has already been declared at this scope.  Arrays
18795466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       * fudge this rule a little bit.
18805466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *
18815466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       * From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
18825466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *
18835466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *    "It is legal to declare an array without a size and then
18845466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *    later re-declare the same name as an array of the same
18855466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *    type and specify a size."
18865466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       */
18875466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick      if (state->symbols->name_declared_this_scope(decl->identifier)) {
18885466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 ir_variable *const earlier =
18895466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    state->symbols->get_variable(decl->identifier);
18905466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
18915466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 if ((earlier != NULL)
18925466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     && (earlier->type->array_size() == 0)
18935466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     && var->type->is_array()
18945466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     && (var->type->element_type() == earlier->type->element_type())) {
18955466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    /* FINISHME: This doesn't match the qualifiers on the two
18965466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     * FINISHME: declarations.  It's not 100% clear whether this is
18975466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     * FINISHME: required or not.
18985466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     */
18995466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
1900cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	    /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
1901cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     *
1902cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     *     "The size [of gl_TexCoord] can be at most
1903cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     *     gl_MaxTextureCoords."
1904cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     */
1905127308b4be077e5bdf60f76320307550921e86bbIan Romanick	    const unsigned size = unsigned(var->type->array_size());
1906cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	    if ((strcmp("gl_TexCoord", var->name) == 0)
1907127308b4be077e5bdf60f76320307550921e86bbIan Romanick		&& (size > state->Const.MaxTextureCoords)) {
1908cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	       YYLTYPE loc = this->get_location();
1909cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick
1910cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	       _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot "
1911cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick				"be larger than gl_MaxTextureCoords (%u)\n",
1912127308b4be077e5bdf60f76320307550921e86bbIan Romanick				state->Const.MaxTextureCoords);
191312873fa4e332959295154edfe957c0af79af5e74Ian Romanick	    } else if ((size > 0) && (size <= earlier->max_array_access)) {
19145466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	       YYLTYPE loc = this->get_location();
19155466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
19165466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > %u due to "
19175466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick				"previous access",
19185466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick				earlier->max_array_access);
19195466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    }
19205466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
19215466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    earlier->type = var->type;
19225466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    delete var;
19235466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    var = NULL;
19244a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	 } else if (state->extensions->ARB_fragment_coord_conventions &&
19254a962170d7cf4243d6ae156fca20a6167388925dEric Anholt		    (earlier != NULL) &&
19264a962170d7cf4243d6ae156fca20a6167388925dEric Anholt		    (strcmp(var->name, "gl_FragCoord") == 0) &&
19274a962170d7cf4243d6ae156fca20a6167388925dEric Anholt		    earlier->type == var->type &&
19284a962170d7cf4243d6ae156fca20a6167388925dEric Anholt		    earlier->mode == var->mode) {
19294a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	    /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
19304a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	     * qualifiers.
19314a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	     */
19324a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	    earlier->origin_upper_left = var->origin_upper_left;
19334a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	    earlier->pixel_center_integer = var->pixel_center_integer;
19345466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 } else {
19355466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    YYLTYPE loc = this->get_location();
19365466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
19375466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    _mesa_glsl_error(& loc, state, "`%s' redeclared",
19385466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick			     decl->identifier);
19395466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 }
19405466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
19415466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 continue;
19425466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick      }
19435466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
19445466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick      /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
19455466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *
19465466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *   "Identifiers starting with "gl_" are reserved for use by
19475466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *   OpenGL, and may not be declared in a shader as either a
19485466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *   variable or a function."
19495466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       */
19505466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick      if (strncmp(decl->identifier, "gl_", 3) == 0) {
19515466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 /* FINISHME: This should only trigger if we're not redefining
19525466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	  * FINISHME: a builtin (to add a qualifier, for example).
19535466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	  */
19545466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 _mesa_glsl_error(& loc, state,
19555466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick			  "identifier `%s' uses reserved `gl_' prefix",
19565466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick			  decl->identifier);
19575466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick      }
19585466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
19598048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt      /* Push the variable declaration to the top.  It means that all
19608048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * the variable declarations will appear in a funny
19618048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * last-to-first order, but otherwise we run into trouble if a
19628048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * function is prototyped, a global var is decled, then the
19638048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * function is defined with usage of the global var.  See
19648048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * glslparsertest's CorrectModule.frag.
19658048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       */
19668048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt      instructions->push_head(var);
1967fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      instructions->append_list(&initializer_instructions);
19682e85f993d8a014b53ad2f6d295cf66d3fb38b091Ian Romanick
1969c7f4ff193a6f7cfae2e4cdc6c4b9162a16226dc0Kenneth Graunke      /* Add the variable to the symbol table after processing the initializer.
197017d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * This differs from most C-like languages, but it follows the GLSL
197117d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
197217d86f4371da413176ba365ca26a58bac172d365Ian Romanick       * spec:
197317d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *
197417d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     "Within a declaration, the scope of a name starts immediately
197517d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     after the initializer if present or immediately after the name
197617d86f4371da413176ba365ca26a58bac172d365Ian Romanick       *     being declared if not."
197717d86f4371da413176ba365ca26a58bac172d365Ian Romanick       */
197817d86f4371da413176ba365ca26a58bac172d365Ian Romanick      const bool added_variable =
1979c7f4ff193a6f7cfae2e4cdc6c4b9162a16226dc0Kenneth Graunke	 state->symbols->add_variable(var->name, var);
198017d86f4371da413176ba365ca26a58bac172d365Ian Romanick      assert(added_variable);
198113b3d4c23d9d088a5a2b3bd657f1f163e42e4d8bVinson Lee      (void) added_variable;
1982a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1983a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
19848558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt
19858558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   /* Generally, variable declarations do not have r-values.  However,
19868558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * one is used for the declaration in
19878558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
19888558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * while (bool b = some_condition()) {
19898558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *   ...
19908558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * }
19918558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
19928558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * so we return the rvalue from the last seen declaration here.
1993a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
19948558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   return result;
1995a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1996a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1997a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1998fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
19990044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions,
200018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
2001a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2002953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
2003a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *type;
2004a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *name = NULL;
20052e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   YYLTYPE loc = this->get_location();
2006a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2007d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   type = this->type->specifier->glsl_type(& name, state);
2008a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2009a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type == NULL) {
2010a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (name != NULL) {
2011a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2012a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type `%s' in declaration of `%s'",
201318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  name, this->identifier);
2014a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
2015a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2016a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type in declaration of `%s'",
201718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->identifier);
2018a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2019a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
20200471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::error_type;
2021a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2022a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2023068c80cfe0a280490353b6b007165d717c672eedEric Anholt   /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2024068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
2025068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    "Functions that accept no input arguments need not use void in the
2026068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    argument list because prototypes (or definitions) are required and
2027068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    therefore there is no ambiguity when an empty argument list "( )" is
2028068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    declared. The idiom "(void)" as a parameter list is provided for
2029068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    convenience."
2030068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
2031068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * Placing this check here prevents a void parameter being set up
2032068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * for a function, which avoids tripping up checks for main taking
2033068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * parameters and lookups of an unnamed symbol.
2034068c80cfe0a280490353b6b007165d717c672eedEric Anholt    */
2035cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if (type->is_void()) {
2036cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (this->identifier != NULL)
2037cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 _mesa_glsl_error(& loc, state,
2038cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick			  "named parameter cannot have type `void'");
2039cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2040cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      is_void = true;
2041068c80cfe0a280490353b6b007165d717c672eedEric Anholt      return NULL;
2042cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
2043068c80cfe0a280490353b6b007165d717c672eedEric Anholt
204445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   if (formal_parameter && (this->identifier == NULL)) {
204545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
204645d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      return NULL;
204745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   }
204845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick
2049e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   /* This only handles "vec4 foo[..]".  The earlier specifier->glsl_type(...)
2050e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    * call already handled the "vec4[..] foo" case.
2051e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    */
2052e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   if (this->is_array) {
2053e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      type = process_array_type(type, this->array_size, state);
2054e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
2055e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
2056e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   if (type->array_size() == 0) {
2057e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
2058e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke		       "a declared size.");
2059e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      type = glsl_type::error_type;
2060e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
2061e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
2062cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   is_void = false;
20637e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
2064a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2065cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   /* Apply any specified qualifiers to the parameter declaration.  Note that
2066cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    * for function parameters the default mode is 'in'.
2067cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    */
20682e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
2069a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
20700044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(var);
2071a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2072a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Parameter declarations do not have r-values.
2073a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2074a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
2075a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2076a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2077a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
207845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanickvoid
2079304ea90233baeac6801a98e981658cb7a2d2501cIan Romanickast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
208045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    bool formal,
208145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    exec_list *ir_parameters,
208245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    _mesa_glsl_parse_state *state)
2083a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2084cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   ast_parameter_declarator *void_param = NULL;
2085cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   unsigned count = 0;
2086a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
20872b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
208845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      param->formal_parameter = formal;
2089068c80cfe0a280490353b6b007165d717c672eedEric Anholt      param->hir(ir_parameters, state);
2090cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2091cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (param->is_void)
2092cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 void_param = param;
2093cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2094cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      count++;
2095cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
2096cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2097cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if ((void_param != NULL) && (count > 1)) {
2098cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      YYLTYPE loc = void_param->get_location();
2099cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2100cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      _mesa_glsl_error(& loc, state,
2101cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick		       "`void' parameter must be only parameter");
2102a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2103a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2104a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2105a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2106fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
210792318a947958892497722772b03c643ebc943294Ian Romanickast_function::hir(exec_list *instructions,
210892318a947958892497722772b03c643ebc943294Ian Romanick		  struct _mesa_glsl_parse_state *state)
2109a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2110953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
211118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_function *f = NULL;
211292318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *sig = NULL;
211392318a947958892497722772b03c643ebc943294Ian Romanick   exec_list hir_parameters;
2114a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2115ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   const char *const name = identifier;
2116a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2117edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2118edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *
2119edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   "Identifiers starting with "gl_" are reserved for use by
2120edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   OpenGL, and may not be declared in a shader as either a
2121edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   variable or a function."
2122edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    */
2123edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   if (strncmp(name, "gl_", 3) == 0) {
2124edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      YYLTYPE loc = this->get_location();
2125edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      _mesa_glsl_error(&loc, state,
2126edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke		       "identifier `%s' uses reserved `gl_' prefix", name);
2127edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   }
2128edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke
2129a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the list of function parameters to HIR now so that they can be
2130a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * used below to compare this function's signature with previously seen
2131a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * signatures for functions with the same name.
2132a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
213345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   ast_parameter_declarator::parameters_to_hir(& this->parameters,
213445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       is_definition,
213545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       & hir_parameters, state);
2136a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2137e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const char *return_type_name;
2138e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const glsl_type *return_type =
213992318a947958892497722772b03c643ebc943294Ian Romanick      this->return_type->specifier->glsl_type(& return_type_name, state);
2140e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
214176e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   if (!return_type) {
214276e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      YYLTYPE loc = this->get_location();
214376e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      _mesa_glsl_error(&loc, state,
214476e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       "function `%s' has undeclared return type `%s'",
214576e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       name, return_type_name);
214676e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      return_type = glsl_type::error_type;
214776e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   }
2148e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
2149ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2150ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    * "No qualifier is allowed on the return type of a function."
2151ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    */
2152ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   if (this->return_type->has_qualifiers()) {
2153ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      YYLTYPE loc = this->get_location();
2154ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      _mesa_glsl_error(& loc, state,
2155ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke		       "function `%s' return type has qualifiers", name);
2156ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   }
2157ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke
2158a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Verify that this function's signature either doesn't match a previously
2159a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * seen signature for a function with the same name, or, if a match is found,
2160a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * that the previously seen signature does not have an associated definition.
2161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
21623359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   f = state->symbols->get_function(name);
2163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (f != NULL) {
2164202604e8160157e4e80b3458175e0170d168e557Ian Romanick      sig = f->exact_matching_signature(&hir_parameters);
21650d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke      if (sig != NULL) {
21660d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 const char *badvar = sig->qualifiers_match(&hir_parameters);
21670d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (badvar != NULL) {
21680d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
2169abd40b15210c17b2a3ba8fcffc868fda203efa01Kenneth Graunke
21700d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
21710d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "qualifiers don't match prototype", name, badvar);
21720d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
21731e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
21740d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (sig->return_type != return_type) {
21750d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
217660be7626b829af7e1d07330b9a88468924ba350eEric Anholt
21770d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
21780d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "match prototype", name);
21790d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
2180a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
21810d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (is_definition && sig->is_defined) {
21820d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
2183a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
21840d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
2185a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
2186a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
21873359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick   } else if (state->symbols->name_declared_this_scope(name)) {
21883359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* This function name shadows a non-function use of the same name.
21893359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
21903359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      YYLTYPE loc = this->get_location();
21913359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
21923359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      _mesa_glsl_error(& loc, state, "function name `%s' conflicts with "
21933359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick		       "non-function", name);
2194826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke      return NULL;
2195a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
21961660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      f = new(ctx) ir_function(name);
21978bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->add_function(f->name, f);
21989fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke
21999fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke      /* Emit the new function header */
22009fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke      instructions->push_tail(f);
2201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2203ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   /* Verify the return type of main() */
2204ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   if (strcmp(name, "main") == 0) {
220525711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick      if (! return_type->is_void()) {
2206ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 YYLTYPE loc = this->get_location();
2207ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
2208ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must return void");
2209ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt      }
2210174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
221192318a947958892497722772b03c643ebc943294Ian Romanick      if (!hir_parameters.is_empty()) {
2212174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 YYLTYPE loc = this->get_location();
2213174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
2214174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
2215174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt      }
2216ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   }
2217a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Finish storing the information about this new function in its signature.
2219a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
222092318a947958892497722772b03c643ebc943294Ian Romanick   if (sig == NULL) {
22211660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      sig = new(ctx) ir_function_signature(return_type);
222292318a947958892497722772b03c643ebc943294Ian Romanick      f->add_signature(sig);
2223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2225bff6013d469b3d4e54cdc5731801c56994a523ecKenneth Graunke   sig->replace_parameters(&hir_parameters);
222692318a947958892497722772b03c643ebc943294Ian Romanick   signature = sig;
222792318a947958892497722772b03c643ebc943294Ian Romanick
222892318a947958892497722772b03c643ebc943294Ian Romanick   /* Function declarations (prototypes) do not have r-values.
222992318a947958892497722772b03c643ebc943294Ian Romanick    */
223092318a947958892497722772b03c643ebc943294Ian Romanick   return NULL;
223192318a947958892497722772b03c643ebc943294Ian Romanick}
223292318a947958892497722772b03c643ebc943294Ian Romanick
223392318a947958892497722772b03c643ebc943294Ian Romanick
223492318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue *
223592318a947958892497722772b03c643ebc943294Ian Romanickast_function_definition::hir(exec_list *instructions,
223692318a947958892497722772b03c643ebc943294Ian Romanick			     struct _mesa_glsl_parse_state *state)
223792318a947958892497722772b03c643ebc943294Ian Romanick{
223892318a947958892497722772b03c643ebc943294Ian Romanick   prototype->is_definition = true;
223992318a947958892497722772b03c643ebc943294Ian Romanick   prototype->hir(instructions, state);
2240e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
224192318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *signature = prototype->signature;
2242826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke   if (signature == NULL)
2243826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke      return NULL;
2244a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
224541ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == NULL);
224641ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = signature;
22476de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   state->found_return = false;
224841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
2249e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   /* Duplicate parameters declared in the prototype as concrete variables.
2250e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick    * Add these to the symbol table.
2251a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
22528bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->push_scope();
2253e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   foreach_iter(exec_list_iterator, iter, signature->parameters) {
2254fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
2255e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
2256fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      assert(var != NULL);
2257a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
22583359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* The only way a parameter would "exist" is if two parameters have
22593359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       * the same name.
22603359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
22613359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(var->name)) {
22623359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 YYLTYPE loc = this->get_location();
22633359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
22643359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
22653359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      } else {
22663359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 state->symbols->add_variable(var->name, var);
22673359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      }
2268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
22709fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   /* Convert the body of the function to HIR. */
2271894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt   this->body->hir(&signature->body, state);
22729fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   signature->is_defined = true;
2273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
22748bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->pop_scope();
2275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
227641ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == signature);
227741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
2278a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
22796de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   if (!signature->return_type->is_void() && !state->found_return) {
22806de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      YYLTYPE loc = this->get_location();
22816de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
22826de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       "%s, but no return statement",
22836de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->function_name(),
22846de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->return_type->name);
22856de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   }
22866de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke
2287a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Function definitions do not have r-values.
2288a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2289a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
2290a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
229116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
229216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2293fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
229416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions,
229516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			struct _mesa_glsl_parse_state *state)
229616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{
2297953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
229816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2299c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   switch (mode) {
2300c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_return: {
230116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      ir_return *inst;
2302aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt      assert(state->current_function);
230316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
230416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      if (opt_return_value) {
2305ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 if (state->current_function->return_type->base_type ==
2306ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	     GLSL_TYPE_VOID) {
2307ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    YYLTYPE loc = this->get_location();
2308ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt
2309ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    _mesa_glsl_error(& loc, state,
2310ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "`return` with a value, in function `%s' "
2311ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "returning void",
2312f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
2313ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 }
231416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
231516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 ir_expression *const ret = (ir_expression *)
231616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	    opt_return_value->hir(instructions, state);
231716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 assert(ret != NULL);
231816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
231918707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 /* Implicit conversions are not allowed for return values. */
232018707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 if (state->current_function->return_type != ret->type) {
232118707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    YYLTYPE loc = this->get_location();
232218707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke
232318707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    _mesa_glsl_error(& loc, state,
232418707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "`return' with wrong type %s, in function `%s' "
232518707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "returning %s",
232618707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     ret->type->name,
232718707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->function_name(),
232818707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->return_type->name);
232918707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 }
233016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
23311660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return(ret);
233216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      } else {
2333aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 if (state->current_function->return_type->base_type !=
2334aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	     GLSL_TYPE_VOID) {
2335aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    YYLTYPE loc = this->get_location();
2336aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt
2337aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    _mesa_glsl_error(& loc, state,
2338aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "`return' with no value, in function %s returning "
2339aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "non-void",
2340f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
2341aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 }
23421660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return;
234316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      }
234416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
23456de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      state->found_return = true;
234616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      instructions->push_tail(inst);
2347c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
234816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   }
234916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2350c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_discard:
2351b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      if (state->target != fragment_shader) {
2352b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 YYLTYPE loc = this->get_location();
2353b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
2354b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 _mesa_glsl_error(& loc, state,
2355b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt			  "`discard' may only appear in a fragment shader");
2356b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      }
235777049a702ad54e09c4102fe8c964e069944f83e5Kenneth Graunke      instructions->push_tail(new(ctx) ir_discard);
2358c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
2359c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick
2360c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_break:
2361c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_continue:
23624cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      /* FINISHME: Handle switch-statements.  They cannot contain 'continue',
23634cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: and they use a different IR instruction for 'break'.
23644cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       */
23654cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      /* FINISHME: Correctly handle the nesting.  If a switch-statement is
23664cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: inside a loop, a 'continue' is valid and will bind to the
23674cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: loop.
23684cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       */
23694cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      if (state->loop_or_switch_nesting == NULL) {
23704cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 YYLTYPE loc = this->get_location();
23714cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
23724cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 _mesa_glsl_error(& loc, state,
23734cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick			  "`%s' may only appear in a loop",
23744cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick			  (mode == ast_break) ? "break" : "continue");
23754cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      } else {
23764cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
23774cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
23782d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 /* Inline the for loop expression again, since we don't know
23792d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  * where near the end of the loop body the normal copy of it
23802d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  * is going to be placed.
23812d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  */
23822d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 if (mode == ast_continue &&
23832d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	     state->loop_or_switch_nesting_ast->rest_expression) {
23842d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	    state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
23852d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt								    state);
23862d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 }
23872d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt
23884cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 if (loop != NULL) {
23894cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    ir_loop_jump *const jump =
23901660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	       new(ctx) ir_loop_jump((mode == ast_break)
23911660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     ? ir_loop_jump::jump_break
23921660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     : ir_loop_jump::jump_continue);
23934cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    instructions->push_tail(jump);
23944cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 }
23954cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      }
23964cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
2397c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
2398b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   }
2399b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
240016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   /* Jump instructions do not have r-values.
240116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
240216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
240316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
24043c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
24053c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
24063c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue *
24073c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions,
24083c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			     struct _mesa_glsl_parse_state *state)
24093c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
2410953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
24111660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
24123c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_rvalue *const condition = this->condition->hir(instructions, state);
24133c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
24143c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
24153c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
24163c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    "Any expression whose type evaluates to a Boolean can be used as the
24173c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    conditional expression bool-expression. Vector types are not accepted
24183c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    as the expression to if."
24193c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
24203c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * The checks are separated so that higher quality diagnostics can be
24213c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * generated for cases where both rules are violated.
24223c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
24233c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
24243c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      YYLTYPE loc = this->condition->get_location();
24253c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
24263c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
24273c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick		       "boolean");
24283c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
24293c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
24301660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_if *const stmt = new(ctx) ir_if(condition);
24313c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
2432665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (then_statement != NULL) {
2433665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
24344f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      then_statement->hir(& stmt->then_instructions, state);
2435665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
2436665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
24373c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
2438665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (else_statement != NULL) {
2439665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
24404f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      else_statement->hir(& stmt->else_instructions, state);
2441665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
2442665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
24433c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
24443c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   instructions->push_tail(stmt);
24453c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
24463c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* if-statements do not have r-values.
24473c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
24483c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   return NULL;
24493c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick}
24509e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
24519e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
24528c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickvoid
24538c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::condition_to_hir(ir_loop *stmt,
24548c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick					  struct _mesa_glsl_parse_state *state)
24559e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick{
2456953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
24571660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
24589e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (condition != NULL) {
24599e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      ir_rvalue *const cond =
24609e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 condition->hir(& stmt->body_instructions, state);
24619e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
24629e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      if ((cond == NULL)
24639e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  || !cond->type->is_boolean() || !cond->type->is_scalar()) {
24649e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 YYLTYPE loc = condition->get_location();
24659e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
24669e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 _mesa_glsl_error(& loc, state,
24679e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick			  "loop condition must be scalar boolean");
24689e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      } else {
24699e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 /* As the first code in the loop body, generate a block that looks
24709e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  * like 'if (!condition) break;' as the loop termination condition.
24719e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  */
24729e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_rvalue *const not_cond =
24731660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
24741660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				   NULL);
24759e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
24761660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
24779e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
24789e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_jump *const break_stmt =
24791660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
24809e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
24819e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 if_stmt->then_instructions.push_tail(break_stmt);
24829e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 stmt->body_instructions.push_tail(if_stmt);
24839e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      }
24849e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   }
24858c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick}
24868c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
24878c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
24888c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickir_rvalue *
24898c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::hir(exec_list *instructions,
24908c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick			     struct _mesa_glsl_parse_state *state)
24918c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick{
2492953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
24931660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
2494484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   /* For-loops and while-loops start a new scope, but do-while loops do not.
24958c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
2496484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   if (mode != ast_do_while)
24978c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      state->symbols->push_scope();
24988c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
24998c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (init_statement != NULL)
25008c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      init_statement->hir(instructions, state);
25018c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
25021660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_loop *const stmt = new(ctx) ir_loop();
25038c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   instructions->push_tail(stmt);
25048c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
25058c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   /* Track the current loop and / or switch-statement nesting.
25068c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
25078c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   ir_instruction *const nesting = state->loop_or_switch_nesting;
25082d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt   ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
25092d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt
25108c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   state->loop_or_switch_nesting = stmt;
25112d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt   state->loop_or_switch_nesting_ast = this;
25128c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
25138c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode != ast_do_while)
25148c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      condition_to_hir(stmt, state);
25159e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25164f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick   if (body != NULL)
25174f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      body->hir(& stmt->body_instructions, state);
25189e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25199e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (rest_expression != NULL)
25209e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      rest_expression->hir(& stmt->body_instructions, state);
25219e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
25228c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode == ast_do_while)
25238c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      condition_to_hir(stmt, state);
25248c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
2525484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   if (mode != ast_do_while)
25269e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      state->symbols->pop_scope();
25279e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
2528e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   /* Restore previous nesting before returning.
2529e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick    */
2530e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   state->loop_or_switch_nesting = nesting;
25312d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt   state->loop_or_switch_nesting_ast = nesting_ast;
2532e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick
25339e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   /* Loops do not have r-values.
25349e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
25359e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   return NULL;
25369e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick}
25373455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
25383455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
25393455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
25403455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_type_specifier::hir(exec_list *instructions,
25413455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
25423455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
25433455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   if (this->structure != NULL)
25443455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      return this->structure->hir(instructions, state);
254585ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick
254685ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick   return NULL;
25473455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
25483455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
25493455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
25503455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
25513455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_struct_specifier::hir(exec_list *instructions,
25523455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
25533455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
25543455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned decl_count = 0;
25553455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
25563455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Make an initial pass over the list of structure fields to determine how
25573455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * many there are.  Each element in this list is an ast_declarator_list.
25583455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * This means that we actually need to count the number of elements in the
25593455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * 'declarations' list in each of the elements.
25603455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
25612b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
25622b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
2563304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      foreach_list_const (decl_ptr, & decl_list->declarations) {
25643455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_count++;
25653455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
25663455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
25673455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
25683455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
25693455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Allocate storage for the structure fields and process the field
25703455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * declarations.  As the declarations are processed, try to also convert
25713455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * the types to HIR.  This ensures that structure definitions embedded in
25723455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * other structure definitions are processed.
25733455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
257421b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt   glsl_struct_field *const fields = talloc_array(state, glsl_struct_field,
257521b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt						  decl_count);
25763455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
25773455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned i = 0;
25782b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
25792b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
25803455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const char *type_name;
25813455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
25823455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      decl_list->type->specifier->hir(instructions, state);
25833455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
25843455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const glsl_type *decl_type =
25853455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_list->type->specifier->glsl_type(& type_name, state);
25863455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
25872b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_declaration, decl, link,
25882b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick			  &decl_list->declarations) {
25893455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 const struct glsl_type *const field_type =
25903455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	    (decl->is_array)
25913455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	    ? process_array_type(decl_type, decl->array_size, state)
25923455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	    : decl_type;
25933455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
259473986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	 fields[i].type = (field_type != NULL)
259573986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	    ? field_type : glsl_type::error_type;
25963455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 fields[i].name = decl->identifier;
25973455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 i++;
25983455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
25993455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
26003455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26013455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   assert(i == decl_count);
26023455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26031d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   const char *name;
26041d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   if (this->name == NULL) {
26051d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      static unsigned anon_count = 1;
26061d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      char buf[32];
26073455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26081d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      snprintf(buf, sizeof(buf), "#anon_struct_%04x", anon_count);
26091d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      anon_count++;
26101d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
26111d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      name = strdup(buf);
26121d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   } else {
26131d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick      name = this->name;
26141d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick   }
26151d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
261649e3577b91f44013746f7a3db411e7041b7d899eIan Romanick   const glsl_type *t =
261749e3577b91f44013746f7a3db411e7041b7d899eIan Romanick      glsl_type::get_record_instance(fields, decl_count, name);
26181d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
2619ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   YYLTYPE loc = this->get_location();
2620ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   if (!state->symbols->add_type(name, t)) {
2621ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
2622ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   } else {
2623ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      /* This logic is a bit tricky.  It is an error to declare a structure at
2624ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick       * global scope if there is also a function with the same name.
2625ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick       */
2626ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      if ((state->current_function == NULL)
2627ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick	  && (state->symbols->get_function(name) != NULL)) {
2628ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick	 _mesa_glsl_error(& loc, state, "name `%s' previously defined", name);
2629ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      } else {
2630ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick	 t->generate_constructor(state->symbols);
2631ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      }
2632a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick
2633a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      const glsl_type **s = (const glsl_type **)
2634a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 realloc(state->user_structures,
2635a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick		 sizeof(state->user_structures[0]) *
2636a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick		 (state->num_user_structures + 1));
2637a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      if (s != NULL) {
2638a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 s[state->num_user_structures] = t;
2639a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->user_structures = s;
2640a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->num_user_structures++;
2641a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      }
2642ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   }
26433455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
26443455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Structure type definitions do not have r-values.
26453455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
26463455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   return NULL;
26473455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
2648