ast_to_hir.cpp revision 001eee52d461233b1e1d6ed3577965e9bcb209e8
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
52bfd7c9ac228c7ed8aec04c3b3aa33f40ee00b035Chia-I Wu#include "main/core.h" /* for struct gl_extensions */
538bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick#include "glsl_symbol_table.h"
54a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "glsl_parser_extras.h"
55a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ast.h"
56a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "glsl_types.h"
57a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ir.h"
58a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
59d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanickvoid
60d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
61d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick{
62adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick   _mesa_glsl_initialize_variables(instructions, state);
63c22c40015db32b68b33c4944b9d94bf499135ec5Eric Anholt   _mesa_glsl_initialize_functions(instructions, state);
64adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick
65814c89abdbcd5b841b98746af921796df0362238Kenneth Graunke   state->symbols->language_version = state->language_version;
66814c89abdbcd5b841b98746af921796df0362238Kenneth Graunke
6741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
6841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
69a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke   /* Section 4.2 of the GLSL 1.20 specification states:
70a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * "The built-in functions are scoped in a scope outside the global scope
71a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  users declare global variables in.  That is, a shader's global scope,
72a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  available for user-defined functions and global variables, is nested
73a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  inside the scope containing the built-in functions."
74a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *
75a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * Since built-in functions like ftransform() access built-in variables,
76a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * it follows that those must be in the outer scope as well.
77a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *
78a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * We push scope here to create this nesting effect...but don't pop.
79a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * This way, a shader's globals are still in the symbol table for use
80a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * by the linker.
81a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    */
82a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke   state->symbols->push_scope();
83a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke
842b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, & state->translation_unit)
85304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
86d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick}
87d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
88d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
890104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick/**
900104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is available, convert one operand to a different type
910104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
920104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * The \c from \c ir_rvalue is converted "in place".
930104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
940104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param to     Type that the operand it to be converted to
950104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param from   Operand that is being converted
960104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param state  GLSL compiler state
970104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
980104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \return
990104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is possible (or unnecessary), \c true is returned.
1000104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * Otherwise \c false is returned.
1010104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */
102f32d3df8ab2b7c6c746f46870edc4b284cea50caKenneth Graunkebool
103bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
1040104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick			  struct _mesa_glsl_parse_state *state)
1050104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick{
106953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
107bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (to->base_type == from->type->base_type)
1080104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return true;
1090104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1100104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* This conversion was added in GLSL 1.20.  If the compilation mode is
1110104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * GLSL 1.10, the conversion is skipped.
1120104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1130104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (state->language_version < 120)
1140104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1150104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1160104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
1170104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *
1180104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    "There are no implicit array or structure conversions. For
1190104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    example, an array of int cannot be implicitly converted to an
1200104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    array of float. There are no implicit conversions between
1210104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    signed and unsigned integers."
1220104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1230104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* FINISHME: The above comment is partially a lie.  There is int/uint
1240104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * FINISHME: conversion for immediate constants.
1250104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
126bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (!to->is_float() || !from->type->is_numeric())
1270104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1280104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
129506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke   /* Convert to a floating point type with the same number of components
130506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke    * as the original type - i.e. int to float, not int to vec4.
131506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke    */
132506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke   to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
133506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke			        from->type->matrix_columns);
134506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke
135bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   switch (from->type->base_type) {
1360104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_INT:
1371660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
1380104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1390104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_UINT:
1401660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
1410104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1420104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_BOOL:
1431660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
144dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt      break;
1450104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   default:
1460104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      assert(0);
1470104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   }
1480104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1490104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   return true;
1500104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick}
1510104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1520104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
153a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
154bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       bool multiply,
156a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
157a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
158336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
159336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
1600104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
162a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic binary operators add (+), subtract (-),
164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    multiply (*), and divide (/) operate on integer and
165a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    floating-point scalars, vectors, and matrices."
166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
16760b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   if (!type_a->is_numeric() || !type_b->is_numeric()) {
168a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
169a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Operands to arithmetic operators must be numeric");
1700471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
171a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If one operand is floating-point based and the other is
175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    not, then the conversions from Section 4.1.10 "Implicit
176a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Conversions" are applied to the non-floating-point-based operand."
177a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1780104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
1790104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
180a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
181a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Could not implicitly convert operands to "
182a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "arithmetic operator");
1830104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return glsl_type::error_type;
184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
185336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
186336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
187336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
188a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If the operands are integer types, they must both be signed or
189a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    both be unsigned."
190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
191a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * From this rule and the preceeding conversion it can be inferred that
192a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
19360b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * The is_numeric check above already filtered out the case where either
19460b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * type is not one of these, so now the base types need only be tested for
19560b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * equality.
196a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
197a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type_a->base_type != type_b->base_type) {
198a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
199a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "base type mismatch for arithmetic operator");
2000471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All arithmetic binary operators result in the same fundamental type
204a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (signed integer, unsigned integer, or floating-point) as the
205a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operands they operate on, after operand type conversion. After
206a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    conversion, the following cases are valid
207a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
208a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The two operands are scalars. In this case the operation is
209a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      applied, resulting in a scalar."
210a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
211cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar() && type_b->is_scalar())
212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* One operand is a scalar, and the other is a vector or matrix.
215a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      In this case, the scalar operation is applied independently to each
216a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      component of the vector or matrix, resulting in the same size
217a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector or matrix."
218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
219cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar()) {
220cb36f8aaeeb09660843316270a781948f773d90bIan Romanick      if (!type_b->is_scalar())
221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_b;
222cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   } else if (type_b->is_scalar()) {
223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * handled.
229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
23060b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_a->is_scalar());
23160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_b->is_scalar());
232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The two operands are vectors of the same size. In this case, the
234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operation is done component-wise resulting in the same size
235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector."
236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
237a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector() && type_b->is_vector()) {
238a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b) {
239a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
240a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      } else {
241a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 _mesa_glsl_error(loc, state,
242a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt			  "vector size mismatch for arithmetic operator");
243a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return glsl_type::error_type;
244a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      }
245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <vector, vector> have been handled.  At least one of the operands must
250a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * be matrix.  Further, since there are no integer matrix types, the base
251a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * type of both operands must be float.
252a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
25360b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(type_a->is_matrix() || type_b->is_matrix());
254a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_a->base_type == GLSL_TYPE_FLOAT);
255a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_b->base_type == GLSL_TYPE_FLOAT);
256a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
257a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The operator is add (+), subtract (-), or divide (/), and the
258a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operands are matrices with the same number of rows and the same
259a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      number of columns. In this case, the operation is done component-
260a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      wise resulting in the same size matrix."
261a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The operator is multiply (*), where both operands are matrices or
262a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      one operand is a vector and the other a matrix. A right vector
263a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand is treated as a column vector and a left vector operand as a
264a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      row vector. In all these cases, it is required that the number of
265a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      columns of the left operand is equal to the number of rows of the
266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      right operand. Then, the multiply (*) operation does a linear
267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      algebraic multiply, yielding an object that has the same number of
268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      rows as the left operand and the same number of columns as the right
269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand. Section 5.10 "Vector and Matrix Operations" explains in
270a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      more detail how vectors and matrices are operated on."
271a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (! multiply) {
273a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b)
274a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
276fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      if (type_a->is_matrix() && type_b->is_matrix()) {
277c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* Matrix multiply.  The columns of A must match the rows of B.  Given
278c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the other previously tested constraints, this means the vector type
279c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of a row from A must be the same as the vector type of a column from
280c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
281c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  */
282c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b->column_type()) {
283c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	    /* The resulting matrix has the number of columns of matrix B and
284c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * the number of rows of matrix A.  We get the row count of A by
285c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * looking at the size of a vector that makes up a column.  The
286c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * transpose (size of a row) is done for B.
287c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     */
288a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    const glsl_type *const type =
289c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	       glsl_type::get_instance(type_a->base_type,
290c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_a->column_type()->vector_elements,
291c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_b->row_type()->vector_elements);
292a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    assert(type != glsl_type::error_type);
293a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
294a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    return type;
295a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
296fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      } else if (type_a->is_matrix()) {
297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* A is a matrix and B is a column vector.  Columns of A must match
298c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * rows of B.  Given the other previously tested constraints, this
299c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * means the vector type of a row from A must be the same as the
300c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * vector the type of B.
301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
30247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 if (type_a->row_type() == type_b) {
30347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    /* The resulting vector has a number of elements equal to
30447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	     * the number of rows of matrix A. */
30547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    const glsl_type *const type =
30647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	       glsl_type::get_instance(type_a->base_type,
30747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       type_a->column_type()->vector_elements,
30847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       1);
30947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    assert(type != glsl_type::error_type);
31047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth
31147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    return type;
31247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 }
313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
314fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick	 assert(type_b->is_matrix());
315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
316c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* A is a row vector and B is a matrix.  Columns of A must match rows
317c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of B.  Given the other previously tested constraints, this means
318c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the type of A must be the same as the vector type of a column from
319c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
320a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
32147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 if (type_a == type_b->column_type()) {
32247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    /* The resulting vector has a number of elements equal to
32347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	     * the number of columns of matrix B. */
32447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    const glsl_type *const type =
32547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	       glsl_type::get_instance(type_a->base_type,
32647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       type_b->row_type()->vector_elements,
32747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       1);
32847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    assert(type != glsl_type::error_type);
32947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth
33047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    return type;
33147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 }
332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
333a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
334a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
335a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      return glsl_type::error_type;
336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
339a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All other cases are illegal."
340a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
341a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3420471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
346a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
34765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholtunary_arithmetic_result_type(const struct glsl_type *type,
34865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt			     struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
349a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
350a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 57:
351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic unary operators negate (-), post- and pre-increment
353a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     and decrement (-- and ++) operate on integer or floating-point
354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     values (including vectors and matrices). All unary operators work
355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     component-wise on their operands. These result with the same type
356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     they operated on."
357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
35865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (!type->is_numeric()) {
35965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
36065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to arithmetic operators must be numeric");
3610471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
36265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
365a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
366a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
367cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace/**
368cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \brief Return the result type of a bit-logic operation.
369cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace *
370cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * If the given types to the bit-logic operator are invalid, return
371cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * glsl_type::error_type.
372cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace *
373cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \param type_a Type of LHS of bit-logic op
374cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \param type_b Type of RHS of bit-logic op
375cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace */
376cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versacestatic const struct glsl_type *
377cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versacebit_logic_result_type(const struct glsl_type *type_a,
378cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                      const struct glsl_type *type_b,
379cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                      ast_operators op,
380cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                      struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
381cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace{
382cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (state->language_version < 130) {
383cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
384cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
385cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
386cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace
387cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
388cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *
389cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     "The bitwise operators and (&), exclusive-or (^), and inclusive-or
390cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     (|). The operands must be of type signed or unsigned integers or
391cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     integer vectors."
392cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     */
393cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (!type_a->is_integer()) {
394cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
395cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                         ast_expression::operator_string(op));
396cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
397cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
398cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (!type_b->is_integer()) {
399cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
400cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                        ast_expression::operator_string(op));
401cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
402cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
403cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace
404cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    /*     "The fundamental types of the operands (signed or unsigned) must
405cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     match,"
406cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     */
407cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (type_a->base_type != type_b->base_type) {
408cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
409cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                        "base type", ast_expression::operator_string(op));
410cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
411cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
412cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace
413cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    /*     "The operands cannot be vectors of differing size." */
414cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (type_a->is_vector() &&
415cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace        type_b->is_vector() &&
416cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace        type_a->vector_elements != type_b->vector_elements) {
417cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
418cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                        "different sizes", ast_expression::operator_string(op));
419cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
420cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
421cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace
422cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    /*     "If one operand is a scalar and the other a vector, the scalar is
423cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     applied component-wise to the vector, resulting in the same type as
424cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     the vector. The fundamental types of the operands [...] will be the
425cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     resulting fundamental type."
426cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     */
427cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (type_a->is_scalar())
428cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace        return type_b;
429cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    else
430cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace        return type_a;
431cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace}
432a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
433a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
434a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickmodulus_result_type(const struct glsl_type *type_a,
43565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    const struct glsl_type *type_b,
43665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
437a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
438a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
439a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The operator modulus (%) operates on signed or unsigned integers or
440a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    integer vectors. The operand types must both be signed or both be
441a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    unsigned."
442a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
44340176e249f72b6090204611873b19aed3da67c71Ian Romanick   if (!type_a->is_integer() || !type_b->is_integer()
444a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       || (type_a->base_type != type_b->base_type)) {
44565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "type mismatch");
4460471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
447a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
448a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
449a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operands cannot be vectors of differing size. If one operand is
450a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    a scalar and the other vector, then the scalar is applied component-
451a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    wise to the vector, resulting in the same type as the vector. If both
452a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    are vectors of the same size, the result is computed component-wise."
453a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
454a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector()) {
455a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick      if (!type_b->is_vector()
456a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  || (type_a->vector_elements == type_b->vector_elements))
457a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_a;
458a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else
459a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_b;
460a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
461a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operator modulus (%) is not defined for any other data types
462a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (non-integer types)."
463a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
46465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
4650471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
466a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
467a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
468a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
469a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
470bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
47165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
472a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
473336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
474336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
4750150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick
476a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
477a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The relational operators greater than (>), less than (<), greater
478a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    than or equal (>=), and less than or equal (<=) operate only on
479a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    scalar integer and scalar floating-point expressions."
480a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
481a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick   if (!type_a->is_numeric()
482a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick       || !type_b->is_numeric()
483cb36f8aaeeb09660843316270a781948f773d90bIan Romanick       || !type_a->is_scalar()
48465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt       || !type_b->is_scalar()) {
48565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
48665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to relational operators must be scalar and "
48765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "numeric");
4880471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
48965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
490a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
491a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "Either the operands' types must match, or the conversions from
492a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
493a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operand, after which the types must match."
494a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4950150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
4960150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
49765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
49865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Could not implicitly convert operands to "
49965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "relational operator");
5000150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick      return glsl_type::error_type;
501a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
502336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
503336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
504a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
50565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (type_a->base_type != type_b->base_type) {
50665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "base type mismatch");
5070471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
50865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
509a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
510a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The result is scalar Boolean."
511a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
5120471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::bool_type;
513a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
514a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
515c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace/**
516c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \brief Return the result type of a bit-shift operation.
517c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace *
518c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * If the given types to the bit-shift operator are invalid, return
519c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * glsl_type::error_type.
520c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace *
521c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \param type_a Type of LHS of bit-shift op
522c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \param type_b Type of RHS of bit-shift op
523c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */
524c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versacestatic const struct glsl_type *
525c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versaceshift_result_type(const struct glsl_type *type_a,
526c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                  const struct glsl_type *type_b,
527c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                  ast_operators op,
528c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                  struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
529c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace{
530c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (state->language_version < 130) {
531c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
532c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      return glsl_type::error_type;
533c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
534c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
535c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
536c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *
537c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     "The shift operators (<<) and (>>). For both operators, the operands
538c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     must be signed or unsigned integers or integer vectors. One operand
539c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     can be signed while the other is unsigned."
540c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    */
541c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (!type_a->is_integer()) {
542c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
543c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              "integer vector", ast_expression::operator_string(op));
544c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace     return glsl_type::error_type;
545c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
546c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
547c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (!type_b->is_integer()) {
548c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
549c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              "integer vector", ast_expression::operator_string(op));
550c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace     return glsl_type::error_type;
551c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
552c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
553c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   /*     "If the first operand is a scalar, the second operand has to be
554c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     a scalar as well."
555c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    */
556c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (type_a->is_scalar() && !type_b->is_scalar()) {
557c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the "
558c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              "second must be scalar as well",
559c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              ast_expression::operator_string(op));
560c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace     return glsl_type::error_type;
561c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
562c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
563c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   /* If both operands are vectors, check that they have same number of
564c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    * elements.
565c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    */
566c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (type_a->is_vector() &&
567c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      type_b->is_vector() &&
568c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      type_a->vector_elements != type_b->vector_elements) {
569c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "Vector operands to operator %s must "
570c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              "have same number of elements",
571c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              ast_expression::operator_string(op));
572c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace     return glsl_type::error_type;
573c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
574c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
575c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   /*     "In all cases, the resulting type will be the same type as the left
576c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     operand."
577c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    */
578c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   return type_a;
579c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace}
580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
5810bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/**
5820bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type
5830bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
5840bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location.  If the types are
5850bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be
5860bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted.
5870bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
5880bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return
5890bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
5900bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned.  This may be
5910bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion.
5920bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
5930bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note
5940bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to
5950bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values.
5960bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */
597fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
598336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholtvalidate_assignment(struct _mesa_glsl_parse_state *state,
599336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt		    const glsl_type *lhs_type, ir_rvalue *rhs)
6000bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{
601336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *rhs_type = rhs->type;
6020bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
6030bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If there is already some error in the RHS, just return it.  Anything
6040bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    * else will lead to an avalanche of error message back to the user.
6050bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
6060bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type->is_error())
6070bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
6080bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
6090bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If the types are identical, the assignment can trivially proceed.
6100bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
6110bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   if (rhs_type == lhs_type)
6120bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
6130bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
6140157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   /* If the array element types are the same and the size of the LHS is zero,
6150157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * the assignment is okay.
6160157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    *
6170157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
6180157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * is handled by ir_dereference::is_lvalue.
6190157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    */
6200157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   if (lhs_type->is_array() && rhs->type->is_array()
6210157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->element_type() == rhs->type->element_type())
6220157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->array_size() == 0)) {
6230157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      return rhs;
6240157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   }
6250157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
626336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   /* Check for implicit conversion in GLSL 1.20 */
627336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   if (apply_implicit_conversion(lhs_type, rhs, state)) {
628336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt      rhs_type = rhs->type;
629336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt      if (rhs_type == lhs_type)
630336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt	 return rhs;
631336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   }
632336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
6330bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   return NULL;
6340bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick}
6350bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
63610a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue *
63710a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
63810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      ir_rvalue *lhs, ir_rvalue *rhs,
63910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      YYLTYPE lhs_loc)
64010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{
641953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
64210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
64310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
64410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (!error_emitted) {
64510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      if (!lhs->is_lvalue()) {
64610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
64710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 error_emitted = true;
64810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      }
64910eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke
65010eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke      if (state->es_shader && lhs->type->is_array()) {
65110eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke	 _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not "
65210eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke			  "allowed in GLSL ES 1.00.");
65310eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke	 error_emitted = true;
65410eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke      }
65510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
65610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
657336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs);
65810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (new_rhs == NULL) {
65910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      _mesa_glsl_error(& lhs_loc, state, "type mismatch");
66010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   } else {
66110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      rhs = new_rhs;
6620157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
6630157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      /* If the LHS array was not declared with a size, it takes it size from
6640157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * the RHS.  If the LHS is an l-value and a whole array, it must be a
6650157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * dereference of a variable.  Any other case would require that the LHS
6660157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * is either not an l-value or not a whole array.
6670157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       */
6680157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      if (lhs->type->array_size() == 0) {
6690157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 ir_dereference *const d = lhs->as_dereference();
6700157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
6710157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(d != NULL);
6720157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
67336ea28646c666ac2af9b43c47e65f9f53ffcc390Ian Romanick	 ir_variable *const var = d->variable_referenced();
6740157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
6750157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(var != NULL);
6760157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
67763f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
67863f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    /* FINISHME: This should actually log the location of the RHS. */
67963f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
68063f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     "previous access",
68163f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     var->max_array_access);
68263f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 }
68363f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick
684f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick	 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
6850157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick						   rhs->type->array_size());
6869703ed05e684f4269cd8af27c94e9b6bf8781d85Eric Anholt	 d->type = var->type;
6870157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      }
68810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
68910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
6902731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt   /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
6912731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * but not post_inc) need the converted assigned value as an rvalue
6922731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * to handle things like:
6932731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
6942731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * i = j += 1;
6952731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
6962731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * So we always just store the computed value being assigned to a
6972731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * temporary and return a deref of that temporary.  If the rvalue
6982731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * ends up not being used, the temp will get copy-propagated out.
6992731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    */
7007e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
7017e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick					   ir_var_temporary);
702e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
703ae805922b7e3cdaf3aee26c3b799fe3608669bbaEric Anholt   instructions->push_tail(var);
704e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   instructions->push_tail(new(ctx) ir_assignment(deref_var,
7051660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  rhs,
7061660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  NULL));
707e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   deref_var = new(ctx) ir_dereference_variable(var);
7082731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt
7098e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick   if (!error_emitted)
7108e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick      instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
71110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
7121660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
71310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt}
7140bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
715de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue *
716959a9ecdd8fbc3375e4149f2b44d253622ff12eeEric Anholtget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
717de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{
7181660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   void *ctx = talloc_parent(lvalue);
719de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_variable *var;
720de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
7217e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
7227e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick			      ir_var_temporary);
72343b5b03d67ce890e867c81d4a5cfc4871d711d43Eric Anholt   instructions->push_tail(var);
724de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->mode = ir_var_auto;
725de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
7261660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
7271660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  lvalue, NULL));
728de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
729de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   /* Once we've created this temporary, mark it read only so it's no
730de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    * longer considered an lvalue.
731de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt    */
732de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->read_only = true;
733de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
7341660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
735de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt}
736de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
737de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
738fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
7390044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions,
74018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	      struct _mesa_glsl_parse_state *state)
74118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{
74218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) instructions;
74318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) state;
74418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
74518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   return NULL;
74618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
74718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
74818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
749fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
7500044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions,
75118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick		    struct _mesa_glsl_parse_state *state)
752a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
753953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
754a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   static const int operations[AST_NUM_OPERATORS] = {
755a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_assign doesn't convert to ir_expression. */
756a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_plus doesn't convert to ir_expression. */
757a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_neg,
758a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,
759a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,
760a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,
761a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,
762a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,
763a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,
764a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,
765a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_less,
766a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_greater,
767a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lequal,
768a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_gequal,
7694dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri      ir_binop_all_equal,
7704dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri      ir_binop_any_nequal,
771a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and,
772a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor,
773a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,
774a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_bit_not,
775a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_and,
776a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_xor,
777a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_or,
778a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_logic_not,
779a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
780a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Note: The following block of expression types actually convert
781a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * to multiple IR instructions.
782a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
783a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,     /* ast_mul_assign */
784a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,     /* ast_div_assign */
785a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,     /* ast_mod_assign */
786a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,     /* ast_add_assign */
787a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,     /* ast_sub_assign */
788a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,  /* ast_ls_assign */
789a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,  /* ast_rs_assign */
790a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and, /* ast_and_assign */
791a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor, /* ast_xor_assign */
792a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,  /* ast_or_assign */
793a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
794a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_conditional doesn't convert to ir_expression. */
795de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_pre_inc. */
796de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_pre_dec. */
797de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_post_inc. */
798de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_post_dec. */
799a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_field_selection doesn't conv to ir_expression. */
800a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_array_index doesn't convert to ir_expression. */
801a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_function_call doesn't conv to ir_expression. */
802a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_identifier doesn't convert to ir_expression. */
803a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_int_constant doesn't convert to ir_expression. */
804a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
805a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_float_constant doesn't conv to ir_expression. */
806a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
807a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_sequence doesn't convert to ir_expression. */
808a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   };
809fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *result = NULL;
8101c325af4d6b907e0a47ab7f868a2a78f054f153fAras Pranckevicius   ir_rvalue *op[3];
8110471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   const struct glsl_type *type = glsl_type::error_type;
812a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   bool error_emitted = false;
813a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc;
814a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
81518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   loc = this->get_location();
816a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
81718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   switch (this->oper) {
8186652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   case ast_assign: {
81918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
82018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
821a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
82210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      result = do_assignment(instructions, state, op[0], op[1],
82310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
82410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = result->type->is_error();
82510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
826a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
8276652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   }
828a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
829a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_plus:
83018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
831a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
832c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
833c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth
834c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      error_emitted = type->is_error();
835a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
836a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = op[0];
837a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
838a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
839a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_neg:
84018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
841a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
84265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
843a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
84465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
845a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
8461660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
8471660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
848a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
849a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
850a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add:
851a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub:
852a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul:
853a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div:
85418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
85518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
856a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
857bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
85818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul),
859a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
860a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      error_emitted = type->is_error();
861a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
8621660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
8631660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
864a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
865a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
866a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mod:
86718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
86818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
869a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
87065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
871a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
87218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(operations[this->oper] == ir_binop_mod);
873a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
8741660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
8751660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
87665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
877a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
878a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
879a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lshift:
880a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rshift:
8815c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       if (state->language_version < 130) {
8825c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace          _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30",
8835c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace              operator_string(this->oper));
8845c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace          error_emitted = true;
8855c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       }
8865c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace
8875c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       op[0] = this->subexpressions[0]->hir(instructions, state);
8885c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       op[1] = this->subexpressions[1]->hir(instructions, state);
889c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace       type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
890c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                                &loc);
8915c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       result = new(ctx) ir_expression(operations[this->oper], type,
8925c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace                                       op[0], op[1]);
8935c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
8945c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       break;
895a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
896a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_less:
897a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_greater:
898a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lequal:
899a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_gequal:
90018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
90118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
902a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
90365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = relational_result_type(op[0], op[1], state, & loc);
904a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
905a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The relational operators must either generate an error or result
906a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
907a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
908a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      assert(type->is_error()
909a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     || ((type->base_type == GLSL_TYPE_BOOL)
910cb36f8aaeeb09660843316270a781948f773d90bIan Romanick		 && type->is_scalar()));
911a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
9121660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
9131660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
91465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
915a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
916a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
917a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_nequal:
918a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_equal:
9196e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
9206e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
9216e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
9226e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
9236e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *
9246e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    "The equality operators equal (==), and not equal (!=)
9256e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    operate on all types. They result in a scalar Boolean. If
9266e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    the operand types do not match, then there must be a
9276e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    conversion from Section 4.1.10 "Implicit Conversions"
9286e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    applied to one operand that can make them match, in which
9296e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    case this conversion is done."
9306e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       */
931bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[0]->type, op[1], state)
932bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[1]->type, op[0], state))
933212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick	  || (op[0]->type != op[1]->type)) {
9346e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
9356e659caaa946339a2de3890a8bed091ccb65102aIan Romanick			  "type", (this->oper == ast_equal) ? "==" : "!=");
9366e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 error_emitted = true;
937a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick      } else if ((state->language_version <= 110)
938a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick		 && (op[0]->type->is_array() || op[1]->type->is_array())) {
939a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
940a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick			  "GLSL 1.10");
941a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 error_emitted = true;
9426e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      }
9436e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
9441660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
9451660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
9466e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      type = glsl_type::bool_type;
9476e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
9486e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      assert(result->type == glsl_type::bool_type);
949a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
950a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
951a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_and:
952a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_xor:
953a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_or:
9541eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[0] = this->subexpressions[0]->hir(instructions, state);
9551eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[1] = this->subexpressions[1]->hir(instructions, state);
956cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace      type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
957cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                                   state, &loc);
9581eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      result = new(ctx) ir_expression(operations[this->oper], type,
9591eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke				      op[0], op[1]);
9601eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
9611eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      break;
9621eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
963a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_not:
9641eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[0] = this->subexpressions[0]->hir(instructions, state);
9651eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
9661eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (state->language_version < 130) {
9671eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
9681eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
9691eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
9701eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
9711eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (!op[0]->type->is_integer()) {
9721eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
9731eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
9741eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
9751eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
9761eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      type = op[0]->type;
9771eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
978a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
979a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
9804950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_and: {
981b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
982b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
983b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
984b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
985b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
986b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
987b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt			  operator_string(this->oper));
988ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
989b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt      }
990b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
99144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      ir_constant *op0_const = op[0]->constant_expression_value();
99244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      if (op0_const) {
99344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (op0_const->value.b[0]) {
99444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    op[1] = this->subexpressions[1]->hir(instructions, state);
99544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
99644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
99744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       YYLTYPE loc = this->subexpressions[1]->get_location();
99844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
99944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       _mesa_glsl_error(& loc, state,
100044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				"RHS of `%s' must be scalar boolean",
100144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				operator_string(this->oper));
100244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       error_emitted = true;
100344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    }
100444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op[1];
100544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 } else {
100644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op0_const;
100744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
100844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = glsl_type::bool_type;
100944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
101081d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
10117e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "and_tmp",
10127e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
101381d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(tmp);
101481d664f099a5fd5fac777480532fb4307d591451Ian Romanick
10151660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
101644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 instructions->push_tail(stmt);
10174950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
101844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
10194950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
102044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
102144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    YYLTYPE loc = this->subexpressions[1]->get_location();
10224950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
102344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    _mesa_glsl_error(& loc, state,
102444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     "RHS of `%s' must be scalar boolean",
102544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     operator_string(this->oper));
102644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    error_emitted = true;
102744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
1028b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
10291660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
103044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
10311660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
103244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
10334950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
10341660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
103544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
10361660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
103744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
10384950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
10391660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
104044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
104144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
10424950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
10434950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
10444950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
10454950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_or: {
10464950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
10474950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
10484950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
10494950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
10504950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
10514950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
10524950a68bf22ede6f4f368c9783e5401816159574Eric Anholt			  operator_string(this->oper));
10534950a68bf22ede6f4f368c9783e5401816159574Eric Anholt	 error_emitted = true;
10544950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      }
10554950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
105644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      ir_constant *op0_const = op[0]->constant_expression_value();
105744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      if (op0_const) {
105844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (op0_const->value.b[0]) {
105944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op0_const;
106044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 } else {
106144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    op[1] = this->subexpressions[1]->hir(instructions, state);
106244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
106344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
106444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       YYLTYPE loc = this->subexpressions[1]->get_location();
106544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt
106644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       _mesa_glsl_error(& loc, state,
106744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				"RHS of `%s' must be scalar boolean",
106844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt				operator_string(this->oper));
106944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	       error_emitted = true;
107044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    }
107144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    result = op[1];
107244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
107344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = glsl_type::bool_type;
107444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
1075dfd30ca6a95a7d95835dad78ffe1fba4d1f4ef69Kenneth Graunke	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
10767e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "or_tmp",
10777e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
10780b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
10794950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
108081d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_if *const stmt = new(ctx) ir_if(op[0]);
108181d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(stmt);
108281d664f099a5fd5fac777480532fb4307d591451Ian Romanick
1083a0879b9dd438d78635f047cdd5ed4c72bc831b60Eric Anholt	 op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state);
10844950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
108544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
108644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    YYLTYPE loc = this->subexpressions[1]->get_location();
10874950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
108844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
108944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt			     operator_string(this->oper));
109044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	    error_emitted = true;
109144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 }
10924950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
10931660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
109444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
10951660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
109644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
10974950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
10981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
109944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
11001660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[1], NULL);
110144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
11024950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
11031660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
110444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
110544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
11064950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
11074950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
11084950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
11094950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_xor:
11104950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
11114950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
11124950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
11134950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
11141660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
11151660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
1116ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
1117a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1118a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1119a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt   case ast_logic_not:
1120a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
1121a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
1122a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
1123a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 YYLTYPE loc = this->subexpressions[0]->get_location();
1124a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
1125a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt	 _mesa_glsl_error(& loc, state,
1126a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt			  "operand of `!' must be scalar boolean");
1127ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt	 error_emitted = true;
1128a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      }
1129a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
11301660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
11311660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
1132ebbf14b9801d577adf40dcb0b63df2d3b8da934eEric Anholt      type = glsl_type::bool_type;
1133a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      break;
1134a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
1135a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul_assign:
1136a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div_assign:
1137a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add_assign:
1138a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub_assign: {
113918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
114018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1141a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1142bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
114318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul_assign),
1144a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
1145a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
11461660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
11471660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						   op[0], op[1]);
1148a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
11493e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
11508273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			     op[0]->clone(ctx, NULL), temp_rhs,
115110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
115210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      type = result->type;
115310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = (op[0]->type->is_error());
1154a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1155a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
1156a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * explicitly test for this because none of the binary expression
1157a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * operators allow array operands either.
1158a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1159a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1160a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1162a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
116348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   case ast_mod_assign: {
116448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
116548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
116648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
116765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
116848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
116948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      assert(operations[this->oper] == ir_binop_mod);
117048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
1171768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
11721660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
11731660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
117448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
11753e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
11768273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			     op[0]->clone(ctx, NULL), temp_rhs,
117748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt			     this->subexpressions[0]->get_location());
117848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      type = result->type;
117965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
118048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      break;
118148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   }
1182a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1183a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_ls_assign:
1184338ed6ec297d76746b6466c26c307722af965e60Chad Versace   case ast_rs_assign: {
1185338ed6ec297d76746b6466c26c307722af965e60Chad Versace      op[0] = this->subexpressions[0]->hir(instructions, state);
1186338ed6ec297d76746b6466c26c307722af965e60Chad Versace      op[1] = this->subexpressions[1]->hir(instructions, state);
1187338ed6ec297d76746b6466c26c307722af965e60Chad Versace      type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1188338ed6ec297d76746b6466c26c307722af965e60Chad Versace                               &loc);
1189338ed6ec297d76746b6466c26c307722af965e60Chad Versace      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1190338ed6ec297d76746b6466c26c307722af965e60Chad Versace                                                   type, op[0], op[1]);
1191338ed6ec297d76746b6466c26c307722af965e60Chad Versace      result = do_assignment(instructions, state, op[0]->clone(ctx, NULL),
1192338ed6ec297d76746b6466c26c307722af965e60Chad Versace                             temp_rhs,
1193338ed6ec297d76746b6466c26c307722af965e60Chad Versace                             this->subexpressions[0]->get_location());
1194338ed6ec297d76746b6466c26c307722af965e60Chad Versace      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1195251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1196338ed6ec297d76746b6466c26c307722af965e60Chad Versace   }
1197a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1198a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_and_assign:
1199a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_xor_assign:
1200d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace   case ast_or_assign: {
1201d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      op[0] = this->subexpressions[0]->hir(instructions, state);
1202d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      op[1] = this->subexpressions[1]->hir(instructions, state);
1203d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1204d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                                   state, &loc);
1205d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1206d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                                                   type, op[0], op[1]);
1207d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      result = do_assignment(instructions, state, op[0]->clone(ctx, NULL),
1208d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                             temp_rhs,
1209d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                             this->subexpressions[0]->get_location());
1210d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1211251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1212d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace   }
1213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
121496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   case ast_conditional: {
121596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
121696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
121796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
121896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
121996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    "The ternary selection operator (?:). It operates on three
122096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
122196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    first expression, which must result in a scalar Boolean."
122296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
122396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
122496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[0]->get_location();
122596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
122696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
122796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
122896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
122996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
123096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* The :? operator is implemented by generating an anonymous temporary
123196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * followed by an if-statement.  The last instruction in each branch of
123296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * the if-statement assigns a value to the anonymous temporary.  This
123396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * temporary is the r-value of the expression.
123496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
12350ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list then_instructions;
12360ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list else_instructions;
123796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
12380ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[1] = this->subexpressions[1]->hir(&then_instructions, state);
12390ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[2] = this->subexpressions[2]->hir(&else_instructions, state);
124096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
124196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
124296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
124396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     "The second and third expressions can be any type, as
124496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     long their types match, or there is a conversion in
124596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     Section 4.1.10 "Implicit Conversions" that can be applied
124696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     to one of the expressions to make their types match. This
124796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     resulting matching type is the type of the entire
124896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     expression."
124996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
1250bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1251bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[2]->type, op[1], state))
1252db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	  || (op[1]->type != op[2]->type)) {
125396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[1]->get_location();
125496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
125596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
125696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick			  "operator must have matching types.");
125796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
12580ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = glsl_type::error_type;
1259db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick      } else {
12600ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = op[1]->type;
126196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
126296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
1263f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1264f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *
1265f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *    "The second and third expressions must be the same type, but can
1266f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *    be of any type other than an array."
1267f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       */
1268f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      if ((state->language_version <= 110) && type->is_array()) {
1269f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1270f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick			  "operator must not be arrays.");
1271f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick	 error_emitted = true;
1272f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      }
1273f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick
12747825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *cond_val = op[0]->constant_expression_value();
12757825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *then_val = op[1]->constant_expression_value();
12767825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *else_val = op[2]->constant_expression_value();
12777825d3d15710fdfcfc503754862963aac8065480Ian Romanick
12787825d3d15710fdfcfc503754862963aac8065480Ian Romanick      if (then_instructions.is_empty()
12797825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && else_instructions.is_empty()
12807825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
12817825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 result = (cond_val->value.b[0]) ? then_val : else_val;
12827825d3d15710fdfcfc503754862963aac8065480Ian Romanick      } else {
12837e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	 ir_variable *const tmp =
12847e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	    new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
12850b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
12860ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
12871660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
12887825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 instructions->push_tail(stmt);
12890ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
12907825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 then_instructions.move_nodes_to(& stmt->then_instructions);
12911660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref =
12921660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
12937825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const then_assign =
12941660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
12957825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->then_instructions.push_tail(then_assign);
12960ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
12977825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 else_instructions.move_nodes_to(& stmt->else_instructions);
12981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref =
12991660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
13007825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const else_assign =
13011660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[2], NULL);
13027825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->else_instructions.push_tail(else_assign);
13030ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
13041660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
13057825d3d15710fdfcfc503754862963aac8065480Ian Romanick      }
1306251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
130796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   }
1308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1309a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_pre_inc:
131076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   case ast_pre_dec: {
131176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
131276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
13131660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1.0f);
131476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      else
13151660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1);
131676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1317a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
131876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1319768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
13201660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
13211660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
132276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
13233e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
13248273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			     op[0]->clone(ctx, NULL), temp_rhs,
132576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt			     this->subexpressions[0]->get_location());
132676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      type = result->type;
132776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      error_emitted = op[0]->type->is_error();
132876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      break;
132976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   }
1330a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1331a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_post_inc:
1332de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   case ast_post_dec: {
1333de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
1334de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
13351660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1.0f);
1336de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      else
13371660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 op[1] = new(ctx) ir_constant(1);
1338de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1339de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1340de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1341a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1342de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1343768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
13441660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
13451660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
1346de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1347de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      /* Get a temporary of a copy of the lvalue before it's modified.
1348de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       * This may get thrown away later.
1349de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       */
13508273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1351de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
13523e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      (void)do_assignment(instructions, state,
13538273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt			  op[0]->clone(ctx, NULL), temp_rhs,
1354de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			  this->subexpressions[0]->get_location());
1355de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1356de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      type = result->type;
1357de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error();
1358a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1359de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   }
1360a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_field_selection:
136218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1365a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
136627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   case ast_array_index: {
136727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      YYLTYPE index_loc = subexpressions[1]->get_location();
136827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
136927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[0] = subexpressions[0]->hir(instructions, state);
137027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[1] = subexpressions[1]->hir(instructions, state);
137127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
137227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
137327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1374a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick      ir_rvalue *const array = op[0];
1375b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
13761660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_array(op[0], op[1]);
1377b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1378b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      /* Do not use op[0] after this point.  Use array.
1379b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       */
1380b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      op[0] = NULL;
1381b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
138227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
138327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
138427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 break;
138527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
138663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick      if (!array->type->is_array()
138763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_matrix()
138863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_vector()) {
138927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
139063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "cannot dereference non-array / non-matrix / "
139163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "non-vector");
139227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
139327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
139427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
139527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (!op[1]->type->is_integer()) {
139627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
139727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be integer type");
139827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
139927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      } else if (!op[1]->type->is_scalar()) {
140027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
140127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be scalar");
140227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
140327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
140427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
140527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      /* If the array index is a constant expression and the array has a
140627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size, ensure that the access is in-bounds.  If the array
140727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * index is not a constant expression, ensure that the array has a
140827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size.
140927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       */
141027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      ir_constant *const const_index = op[1]->constant_expression_value();
141127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (const_index != NULL) {
141227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 const int idx = const_index->value.i[0];
141363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 const char *type_name;
141463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 unsigned bound = 0;
141563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick
141663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
141763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "matrix";
141863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
141963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "vector";
142063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
142163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "array";
142263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
142327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
142427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
142527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *
142627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    "It is illegal to declare an array with a size, and then
142727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    later (in the same shader) index the same array with an
142827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    integral constant expression greater than or equal to the
142927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    declared size. It is also illegal to index an array with a
143027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    negative constant expression."
143127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  */
143263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
143363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->row_type()->vector_elements <= idx) {
143463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->row_type()->vector_elements;
143563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
143663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
143763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->vector_elements <= idx) {
143863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->vector_elements;
143963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
144063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
144163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((array->type->array_size() > 0)
144263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick		&& (array->type->array_size() <= idx)) {
144363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->array_size();
144463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
144527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
144627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
144763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (bound > 0) {
144863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be < %u",
144963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name, bound);
145063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    error_emitted = true;
145163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (idx < 0) {
145263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be >= 0",
145363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name);
145427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    error_emitted = true;
145527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
1456b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
145763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_array()) {
1458a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    /* If the array is a variable dereference, it dereferences the
1459a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * whole array, by definition.  Use this to get the variable.
1460a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     *
1461a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: Should some methods for getting / setting / testing
1462a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: array access limits be added to ir_dereference?
1463a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     */
1464a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    ir_variable *const v = array->whole_variable_referenced();
146563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((v != NULL) && (unsigned(idx) > v->max_array_access))
146663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       v->max_array_access = idx;
146763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
14682b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke      } else if (array->type->array_size() == 0) {
14692b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
1470a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt      } else {
1471a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 if (array->type->is_array()) {
14725226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	    /* whole_variable_referenced can return NULL if the array is a
14735226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * member of a structure.  In this case it is safe to not update
14745226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * the max_array_access field because it is never used for fields
14755226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * of structures.
14765226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     */
1477a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	    ir_variable *v = array->whole_variable_referenced();
14785226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	    if (v != NULL)
14795226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	       v->max_array_access = array->type->array_size();
1480a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 }
148127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
148227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
148327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
148427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 result->type = glsl_type::error_type;
148527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
148627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      type = result->type;
1487a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
148827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   }
1489a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1490a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_function_call:
14917cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      /* Should *NEVER* get here.  ast_function_call should always be handled
14927cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick       * by ast_function_expression::hir.
1493a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
14947cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      assert(0);
1495a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1496a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1497a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_identifier: {
1498a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* ast_identifier can appear several places in a full abstract syntax
1499a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * tree.  This particular use must be at location specified in the grammar
1500a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * as 'variable_identifier'.
1501a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
15028bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      ir_variable *var =
15038bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick	 state->symbols->get_variable(this->primary_expression.identifier);
1504a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
15051660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_variable(var);
1506a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1507a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (var != NULL) {
1508a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 type = result->type;
1509a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
151071d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
151118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->primary_expression.identifier);
1512a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1513a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 error_emitted = true;
1514a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1515a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1516a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1517a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1518a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_int_constant:
15190471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::int_type;
15201660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.int_constant);
1521a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1522a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1523a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_uint_constant:
15240471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::uint_type;
15251660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1526a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1527a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1528a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_float_constant:
15290471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::float_type;
15301660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.float_constant);
1531a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1532a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1533a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bool_constant:
15340471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::bool_type;
15351660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1536a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1537a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1538a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sequence: {
1539a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* It should not be possible to generate a sequence in the AST without
1540a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * any expressions in it.
1541a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1542304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      assert(!this->expressions.is_empty());
1543a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1544a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The r-value of a sequence is the last expression in the sequence.  If
1545a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the other expressions in the sequence do not have side-effects (and
1546a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * therefore add instructions to the instruction list), they get dropped
1547a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * on the floor.
1548a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
15492b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_node, ast, link, &this->expressions)
1550304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick	 result = ast->hir(instructions, state);
1551a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1552a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      type = result->type;
1553a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1554a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Any errors should have already been emitted in the loop above.
1555a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1556a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      error_emitted = true;
1557a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1558a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1559a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1560a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1561cef3baecf636a30b62cd7a1e8de57c7650f7003eIan Romanick   if (type->is_error() && !error_emitted)
156271d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick      _mesa_glsl_error(& loc, state, "type mismatch");
1563a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1564a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
1565a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1566a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1567a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1568fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
15690044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions,
157018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1571a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1572a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* It is possible to have expression statements that don't have an
1573a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * expression.  This is the solitary semicolon:
1574a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1575a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for (i = 0; i < 5; i++)
1576a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     ;
1577a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1578a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * In this case the expression will be NULL.  Test for NULL and don't do
1579a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * anything in that case.
1580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
158118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (expression != NULL)
158218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      expression->hir(instructions, state);
1583a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1584a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Statements do not have r-values.
1585a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1586a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1587a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1588a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1589a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1590fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
15910044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions,
159218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			    struct _mesa_glsl_parse_state *state)
1593a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
159418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
15958bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->push_scope();
1596a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
15972b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, &this->statements)
1598304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
1599a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
160018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
16018bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->pop_scope();
1602a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1603a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Compound statements do not have r-values.
1604a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1605a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1606a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1607a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1608a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
160928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type *
1610d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunkeprocess_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
161128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick		   struct _mesa_glsl_parse_state *state)
161228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{
161328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   unsigned length = 0;
161428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
161528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   /* FINISHME: Reject delcarations of multidimensional arrays. */
161628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
161728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   if (array_size != NULL) {
161828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      exec_list dummy_instructions;
161928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
162028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      YYLTYPE loc = array_size->get_location();
162128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
162228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      /* FINISHME: Verify that the grammar forbids side-effects in array
162328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       * FINISHME: sizes.   i.e., 'vec4 [x = 12] data'
162428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick       */
162528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      assert(dummy_instructions.is_empty());
162628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
162728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      if (ir != NULL) {
162828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 if (!ir->type->is_integer()) {
162928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be integer type");
163028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else if (!ir->type->is_scalar()) {
163128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be scalar type");
163228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else {
163328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    ir_constant *const size = ir->constant_expression_value();
163428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
163528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    if (size == NULL) {
163628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be a "
163728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick				"constant valued expression");
163828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else if (size->value.i[0] <= 0) {
163928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > 0");
164028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else {
164128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       assert(size->type == ir->type);
164228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       length = size->value.u[0];
164328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    }
164428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 }
164528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
1646d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke   } else if (state->es_shader) {
1647d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke      /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
1648d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke       * array declarations have been removed from the language.
1649d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke       */
1650d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke      _mesa_glsl_error(loc, state, "unsized array declarations are not "
1651d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke		       "allowed in GLSL ES 1.00.");
165228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   }
165328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1654f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick   return glsl_type::get_array_instance(base, length);
165528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick}
165628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
165728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1658d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type *
1659d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name,
1660d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick			      struct _mesa_glsl_parse_state *state) const
1661a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1662d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   const struct glsl_type *type;
1663a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1664ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke   type = state->symbols->get_type(this->type_name);
1665ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke   *name = this->type_name;
1666a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1667ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke   if (this->is_array) {
1668ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke      YYLTYPE loc = this->get_location();
1669ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke      type = process_array_type(&loc, type, this->array_size, state);
1670a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1671a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1672a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
1673a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1674a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1675a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1676a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1677a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1678768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick				 ir_variable *var,
16792e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 struct _mesa_glsl_parse_state *state,
16802e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 YYLTYPE *loc)
1681a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1682e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.invariant)
1683a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->invariant = 1;
1684a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1685a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* FINISHME: Mark 'in' variables at global scope as read-only. */
1686e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.constant || qual->flags.q.attribute
1687e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick       || qual->flags.q.uniform
1688e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick       || (qual->flags.q.varying && (state->target == fragment_shader)))
1689a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->read_only = 1;
1690a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1691e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.centroid)
1692a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->centroid = 1;
1693a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1694e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.attribute && state->target != vertex_shader) {
16952e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      var->type = glsl_type::error_type;
16962e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      _mesa_glsl_error(loc, state,
16972e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "`attribute' variables may not be declared in the "
1698ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       "%s shader",
1699ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       _mesa_glsl_shader_target_name(state->target));
17002e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   }
17012e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
170290b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
170390b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *
170490b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     "The varying qualifier can be used only with the data types
170590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
170690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     these."
170790b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    */
1708e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.varying) {
17090ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      const glsl_type *non_array_type;
17100ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
17110ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (var->type && var->type->is_array())
17120ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type->fields.array;
17130ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      else
17140ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type;
17150ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
17160ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
17170ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 var->type = glsl_type::error_type;
17180ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 _mesa_glsl_error(loc, state,
17190ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt			  "varying variables must be of base type float");
17200ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      }
172190b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   }
172290b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt
17237e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   /* If there is no qualifier that changes the mode of the variable, leave
17247e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    * the setting alone.
17257e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    */
1726e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.in && qual->flags.q.out)
1727a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_inout;
1728e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.attribute || qual->flags.q.in
1729e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	    || (qual->flags.q.varying && (state->target == fragment_shader)))
1730a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_in;
1731e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.out
1732e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	    || (qual->flags.q.varying && (state->target == vertex_shader)))
1733a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_out;
1734e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.uniform)
1735a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_uniform;
1736a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1737e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.flat)
1738a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_flat;
1739e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.noperspective)
1740a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_noperspective;
1741a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   else
1742a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->interpolation = ir_var_smooth;
17439d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick
1744e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   var->pixel_center_integer = qual->flags.q.pixel_center_integer;
1745e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   var->origin_upper_left = qual->flags.q.origin_upper_left;
1746e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
17478d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick       && (strcmp(var->name, "gl_FragCoord") != 0)) {
1748e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick      const char *const qual_string = (qual->flags.q.origin_upper_left)
17498d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick	 ? "origin_upper_left" : "pixel_center_integer";
17508d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
17518d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick      _mesa_glsl_error(loc, state,
17528d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "layout qualifier `%s' can only be applied to "
17538d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "fragment shader input `gl_FragCoord'",
17548d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       qual_string);
17558d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick   }
17568d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
1757eee68d3631813580a14fa51fda6f0c959279256cIan Romanick   if (qual->flags.q.explicit_location) {
1758eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      const bool global_scope = (state->current_function == NULL);
1759eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      bool fail = false;
1760eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      const char *string = "";
1761eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
1762eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      /* In the vertex shader only shader inputs can be given explicit
1763eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       * locations.
1764eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       *
1765eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       * In the fragment shader only shader outputs can be given explicit
1766eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       * locations.
1767eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       */
1768eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      switch (state->target) {
1769eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      case vertex_shader:
1770eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 if (!global_scope || (var->mode != ir_var_in)) {
1771eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    fail = true;
1772eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    string = "input";
1773eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 }
1774eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 break;
1775eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
1776eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      case geometry_shader:
1777eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 _mesa_glsl_error(loc, state,
1778eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "geometry shader variables cannot be given "
1779eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "explicit locations\n");
1780eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 break;
1781eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
1782eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      case fragment_shader:
1783eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 if (!global_scope || (var->mode != ir_var_in)) {
1784eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    fail = true;
1785eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    string = "output";
1786eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 }
1787eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 break;
1788a75da2c0e85eb6b8279ec895c3f74cc4aefc0257Kenneth Graunke      };
1789eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
1790eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      if (fail) {
1791eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 _mesa_glsl_error(loc, state,
1792eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "only %s shader %s variables can be given an "
1793eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "explicit location\n",
1794eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  _mesa_glsl_shader_target_name(state->target),
1795eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  string);
1796eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      } else {
1797eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 var->explicit_location = true;
179868a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick
179968a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 /* This bit of silliness is needed because invalid explicit locations
180068a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * are supposed to be flagged during linking.  Small negative values
180168a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
180268a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
180368a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * The linker needs to be able to differentiate these cases.  This
180468a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * ensures that negative values stay negative.
180568a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  */
180668a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 if (qual->location >= 0) {
180768a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	    var->location = (state->target == vertex_shader)
180868a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	       ? (qual->location + VERT_ATTRIB_GENERIC0)
180968a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	       : (qual->location + FRAG_RESULT_DATA0);
181068a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 } else {
181168a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	    var->location = qual->location;
181268a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 }
1813eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      }
1814eee68d3631813580a14fa51fda6f0c959279256cIan Romanick   }
1815eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
181610eaa8bcbce1cd6d2e120e913f7abafde9675215Kenneth Graunke   if (var->type->is_array() && state->language_version != 110) {
18179d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick      var->array_lvalue = true;
18189d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick   }
1819a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1820a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1821a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1822fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
18230044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions,
182418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			 struct _mesa_glsl_parse_state *state)
1825a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1826953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
1827a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *decl_type;
1828a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *type_name = NULL;
18298558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   ir_rvalue *result = NULL;
1830c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   YYLTYPE loc = this->get_location();
1831a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18326f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
18336f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
18346f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     "To ensure that a particular output variable is invariant, it is
18356f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     necessary to use the invariant qualifier. It can either be used to
18366f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     qualify a previously declared variable as being invariant
18376f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
18386f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *         invariant gl_Position; // make existing gl_Position be invariant"
18396f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
18406f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * In these cases the parser will set the 'invariant' flag in the declarator
18416f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * list, and the type will be NULL.
18426f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    */
18436f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   if (this->invariant) {
18446f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      assert(this->type == NULL);
18456f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
18466f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (state->current_function != NULL) {
18476f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 _mesa_glsl_error(& loc, state,
18486f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "All uses of `invariant' keyword must be at global "
18496f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "scope\n");
18506f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
18516f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
18526f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
18536f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(!decl->is_array);
18546f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->array_size == NULL);
18556f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->initializer == NULL);
18566f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
18576f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 ir_variable *const earlier =
18586f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    state->symbols->get_variable(decl->identifier);
18596f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 if (earlier == NULL) {
18606f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
18616f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "Undeclared variable `%s' cannot be marked "
18626f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "invariant\n", decl->identifier);
18636f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == vertex_shader)
18646f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_out)) {
18656f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
18666f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
18676f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", decl->identifier);
18686f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == fragment_shader)
18696f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_in)) {
18706f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
18716f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
18726f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", decl->identifier);
18736f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else {
18746f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    earlier->invariant = true;
18756f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
18766f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
18776f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
18786f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      /* Invariant redeclarations do not have r-values.
18796f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       */
18806f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      return NULL;
18816f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   }
18826f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
18836f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(this->type != NULL);
18846f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(!this->invariant);
18856f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
18863455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* The type specifier may contain a structure definition.  Process that
18873455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * before any of the variable declarations.
18883455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
18893455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   (void) this->type->specifier->hir(instructions, state);
18903455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
1891d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   decl_type = this->type->specifier->glsl_type(& type_name, state);
1892304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick   if (this->declarations.is_empty()) {
18936f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      /* The only valid case where the declaration list can be empty is when
18946f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       * the declaration is setting the default precision of a built-in type
18956f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       * (e.g., 'precision highp vec4;').
1896c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick       */
1897c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick
18986f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (decl_type != NULL) {
1899c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      } else {
1900c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick	    _mesa_glsl_error(& loc, state, "incomplete declaration");
1901c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      }
1902c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   }
1903a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
19042b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
1905a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      const struct glsl_type *var_type;
1906768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_variable *var;
1907a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1908a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Emit a warning if a variable declaration shadows a
1909a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: declaration at a higher scope.
1910a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1911a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1912cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick      if ((decl_type == NULL) || decl_type->is_void()) {
1913a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (type_name != NULL) {
1914a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1915a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type `%s' in declaration of `%s'",
1916a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     type_name, decl->identifier);
1917a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 } else {
1918a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
1919a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type in declaration of `%s'",
1920a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     decl->identifier);
1921a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
1922a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
1923a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1924a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1925a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (decl->is_array) {
1926d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 var_type = process_array_type(&loc, decl_type, decl->array_size,
1927d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke				       state);
1928a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
1929a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 var_type = decl_type;
1930a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1931a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
19327e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick      var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
1933a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
19343f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
19353f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
19363f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     "Global variables can only use the qualifiers const,
19373f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     attribute, uni form, or varying. Only one may be
19383f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     specified.
19393f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
19403f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     Local variables can only use the qualifier const."
19413f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
19423f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       * This is relaxed in GLSL 1.30.
19433f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       */
19443f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      if (state->language_version < 120) {
1945e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.out) {
19463f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
19473f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`out' qualifier in declaration of `%s' "
19483f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "only valid for function parameters in GLSL 1.10.",
19493f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
19503f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
1951e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.in) {
19523f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
19533f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`in' qualifier in declaration of `%s' "
19543f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "only valid for function parameters in GLSL 1.10.",
19553f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     decl->identifier);
19563f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
19573f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 /* FINISHME: Test for other invalid qualifiers. */
19583f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      }
19593f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt
19602e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
19612e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				       & loc);
1962a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1963e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick      if (this->type->qualifier.flags.q.invariant) {
1964046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
1965046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt						   var->mode == ir_var_inout)) {
1966046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
1967046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature outval
1968046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
19696f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
19706f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
19716f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", var->name);
1972046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 } else if ((state->target == fragment_shader) &&
1973046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt		    !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
1974046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
1975046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature inval
1976046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
19776f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
19786f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
19796f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", var->name);
19806f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
19816f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
19826f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
1983e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      if (state->current_function != NULL) {
1984b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 const char *mode = NULL;
1985e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 const char *extra = "";
1986b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
1987e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 /* There is no need to check for 'inout' here because the parser will
1988e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	  * only allow that in function parameter lists.
1989e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	  */
1990e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.attribute) {
1991b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "attribute";
1992e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.uniform) {
1993b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "uniform";
1994e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.varying) {
1995b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "varying";
1996e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.in) {
1997e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "in";
1998e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
1999e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.out) {
2000e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "out";
2001e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
2002b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 }
2003b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
2004b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 if (mode) {
2005e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	    _mesa_glsl_error(& loc, state,
2006b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick			     "%s variable `%s' must be declared at "
2007e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     "global scope%s",
2008e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     mode, var->name, extra);
2009e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 }
2010e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      } else if (var->mode == ir_var_in) {
2011fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 if (state->target == vertex_shader) {
2012fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    bool error_emitted = false;
2013fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
2014fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2015fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
2016fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
2017fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
2018fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors. Vertex shader inputs can also form arrays of these
2019fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    types, but not structures."
2020fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
20212d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
20222d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
20232d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
20242d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
20252d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors. They cannot be arrays or structures."
20262d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
2027fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
2028fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
2029fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "The attribute qualifier can be used only with float,
2030fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    floating-point vectors, and matrices. Attribute variables
2031fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    cannot be declared as arrays or structures."
2032fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     */
2033fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    const glsl_type *check_type = var->type->is_array()
2034fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       ? var->type->fields.array : var->type;
2035fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
2036fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    switch (check_type->base_type) {
2037fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_FLOAT:
2038fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       break;
2039fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_UINT:
2040fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_INT:
2041fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       if (state->language_version > 120)
2042fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		  break;
2043fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       /* FALLTHROUGH */
2044fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    default:
2045fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
2046fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
2047fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"type %s`%s'",
2048fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				var->type->is_array() ? "array of " : "",
2049fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				check_type->name);
2050fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
2051fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
2052fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
20532d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    if (!error_emitted && (state->language_version <= 130)
2054fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		&& var->type->is_array()) {
2055fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
2056fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
2057fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"array type");
2058fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
2059fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
2060fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 }
2061fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick      }
2062fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
2063e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick      /* Process the initializer and add its instructions to a temporary
2064e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * list.  This list will be added to the instruction stream (below) after
2065e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * the declaration is added.  This is done because in some cases (such as
2066e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * redeclarations) the declaration may not actually be added to the
2067e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * instruction stream.
2068e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       */
2069fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      exec_list initializer_instructions;
207066faec4895b7bb59a614087a200c05157191b4aeIan Romanick      if (decl->initializer != NULL) {
207143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 YYLTYPE initializer_loc = decl->initializer->get_location();
207243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
207366faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
207466faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *
207566faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    "All uniform variables are read-only and are initialized either
207666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    directly by an application via API commands, or indirectly by
207766faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  *    OpenGL."
207866faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
207966faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 if ((state->language_version <= 110)
208066faec4895b7bb59a614087a200c05157191b4aeIan Romanick	     && (var->mode == ir_var_uniform)) {
208143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
208243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize uniforms in GLSL 1.10");
208343de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
208443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick
208543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if (var->type->is_sampler()) {
208643de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
208743de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize samplers");
208843de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 }
208919360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
209043de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
209143de17282017bdf187d6e646de3262cc64b7f46bIan Romanick	    _mesa_glsl_error(& initializer_loc, state,
209243de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     "cannot initialize %s shader input / %s",
2093ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick			     _mesa_glsl_shader_target_name(state->target),
209443de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     (state->target == vertex_shader)
209543de17282017bdf187d6e646de3262cc64b7f46bIan Romanick			     ? "attribute" : "varying");
209666faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
209766faec4895b7bb59a614087a200c05157191b4aeIan Romanick
20981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
2099fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt	 ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions,
2100e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick						 state);
210119360152f5bd8cff93359dbfe5a50a90b699c118Ian Romanick
2102ce030884064046925a655413097dd8257e9392ddIan Romanick	 /* Calculate the constant value if this is a const or uniform
2103307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	  * declaration.
210466faec4895b7bb59a614087a200c05157191b4aeIan Romanick	  */
2105e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.constant
2106e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	     || this->type->qualifier.flags.q.uniform) {
2107e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs);
2108e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    if (new_rhs != NULL) {
2109e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	       rhs = new_rhs;
2110e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt
2111e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       ir_constant *constant_value = rhs->constant_expression_value();
2112e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       if (!constant_value) {
2113e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  _mesa_glsl_error(& initializer_loc, state,
2114e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   "initializer of %s variable `%s' must be a "
2115e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   "constant expression",
2116e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick				   (this->type->qualifier.flags.q.constant)
2117e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   ? "const" : "uniform",
2118e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt				   decl->identifier);
2119e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  if (var->type->is_numeric()) {
2120e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		     /* Reduce cascading errors. */
2121e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		     var->constant_value = ir_constant::zero(ctx, var->type);
2122e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  }
2123e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       } else {
2124e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  rhs = constant_value;
2125e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  var->constant_value = constant_value;
2126e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       }
2127e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	    } else {
2128e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke	       _mesa_glsl_error(&initializer_loc, state,
2129e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke			        "initializer of type %s cannot be assigned to "
2130e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke				"variable of type %s",
2131e1d71850faba23d1bea3858a8c2e05a45fd21143Kenneth Graunke				rhs->type->name, var->type->name);
2132e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       if (var->type->is_numeric()) {
2133e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  /* Reduce cascading errors. */
2134e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt		  var->constant_value = ir_constant::zero(ctx, var->type);
2135e11757bb896e3dadc54fb3d18adf4b71e3e883b3Eric Anholt	       }
2136307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	    }
2137307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 }
213866faec4895b7bb59a614087a200c05157191b4aeIan Romanick
2139307c71bf24a3c99409ccf4b8b10f161e4b032cbaEric Anholt	 if (rhs && !rhs->type->is_error()) {
2140ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    bool temp = var->read_only;
2141e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	    if (this->type->qualifier.flags.q.constant)
2142ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	       var->read_only = false;
2143ce030884064046925a655413097dd8257e9392ddIan Romanick
2144ce030884064046925a655413097dd8257e9392ddIan Romanick	    /* Never emit code to initialize a uniform.
2145ce030884064046925a655413097dd8257e9392ddIan Romanick	     */
2146e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	    if (!this->type->qualifier.flags.q.uniform)
2147fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt	       result = do_assignment(&initializer_instructions, state,
2148fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt				      lhs, rhs,
2149ce030884064046925a655413097dd8257e9392ddIan Romanick				      this->get_location());
2150ac3af37d27c49704dd3b2d303b4497b08f8b47fdEric Anholt	    var->read_only = temp;
215166faec4895b7bb59a614087a200c05157191b4aeIan Romanick	 }
215266faec4895b7bb59a614087a200c05157191b4aeIan Romanick      }
215317d86f4371da413176ba365ca26a58bac172d365Ian Romanick
21540ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
21550ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *
21560ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *     "It is an error to write to a const variable outside of
21570ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      its declaration, so they must be initialized when
21580ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      declared."
21590ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       */
2160e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick      if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
21610ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	 _mesa_glsl_error(& loc, state,
21620ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt			  "const declaration of `%s' must be initialized");
21630ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      }
21640ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt
21655d25746640ee27882b69a962459727cf924443dbKenneth Graunke      /* Check if this declaration is actually a re-declaration, either to
21665d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * resize an array or add qualifiers to an existing variable.
21675466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *
2168a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke       * This is allowed for variables in the current scope, or when at
2169a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke       * global scope (for built-ins in the implicit outer scope).
21705466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       */
21715d25746640ee27882b69a962459727cf924443dbKenneth Graunke      ir_variable *earlier = state->symbols->get_variable(decl->identifier);
2172a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke      if (earlier != NULL && (state->current_function == NULL ||
2173a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke	  state->symbols->name_declared_this_scope(decl->identifier))) {
21745466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
21755d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
21765d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  *
21775d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  * "It is legal to declare an array without a size and then
21785d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  *  later re-declare the same name as an array of the same
21795d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  *  type and specify a size."
21805d25746640ee27882b69a962459727cf924443dbKenneth Graunke	  */
21815d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 if ((earlier->type->array_size() == 0)
21825466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     && var->type->is_array()
21835466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     && (var->type->element_type() == earlier->type->element_type())) {
21845466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    /* FINISHME: This doesn't match the qualifiers on the two
21855466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     * FINISHME: declarations.  It's not 100% clear whether this is
21865466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     * FINISHME: required or not.
21875466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	     */
21885466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
2189cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	    /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
2190cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     *
2191cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     *     "The size [of gl_TexCoord] can be at most
2192cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     *     gl_MaxTextureCoords."
2193cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	     */
2194127308b4be077e5bdf60f76320307550921e86bbIan Romanick	    const unsigned size = unsigned(var->type->array_size());
2195cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	    if ((strcmp("gl_TexCoord", var->name) == 0)
2196127308b4be077e5bdf60f76320307550921e86bbIan Romanick		&& (size > state->Const.MaxTextureCoords)) {
2197cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	       YYLTYPE loc = this->get_location();
2198cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick
2199cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick	       _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot "
2200cd00d5b88caa41ebf4b407126f314832f9fdae54Ian Romanick				"be larger than gl_MaxTextureCoords (%u)\n",
2201127308b4be077e5bdf60f76320307550921e86bbIan Romanick				state->Const.MaxTextureCoords);
220212873fa4e332959295154edfe957c0af79af5e74Ian Romanick	    } else if ((size > 0) && (size <= earlier->max_array_access)) {
22035466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	       YYLTYPE loc = this->get_location();
22045466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
22055466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > %u due to "
22065466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick				"previous access",
22075466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick				earlier->max_array_access);
22085466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    }
22095466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
22105466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    earlier->type = var->type;
22115466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    delete var;
22125466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    var = NULL;
22136e006273840282e06a08655553821ef8176b2d9cChad Versace	 } else if (state->ARB_fragment_coord_conventions_enable
22145d25746640ee27882b69a962459727cf924443dbKenneth Graunke		    && strcmp(var->name, "gl_FragCoord") == 0
22155d25746640ee27882b69a962459727cf924443dbKenneth Graunke		    && earlier->type == var->type
22165d25746640ee27882b69a962459727cf924443dbKenneth Graunke		    && earlier->mode == var->mode) {
22174a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	    /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
22184a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	     * qualifiers.
22194a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	     */
22204a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	    earlier->origin_upper_left = var->origin_upper_left;
22214a962170d7cf4243d6ae156fca20a6167388925dEric Anholt	    earlier->pixel_center_integer = var->pixel_center_integer;
22225466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 } else {
22235466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	    YYLTYPE loc = this->get_location();
22245d25746640ee27882b69a962459727cf924443dbKenneth Graunke	    _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
22255466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 }
22265466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
22275466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 continue;
22285466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick      }
22295466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
22305d25746640ee27882b69a962459727cf924443dbKenneth Graunke      /* By now, we know it's a new variable declaration (we didn't hit the
22315d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * above "continue").
22325d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *
22335d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
22345466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *
22355466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *   "Identifiers starting with "gl_" are reserved for use by
22365466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *   OpenGL, and may not be declared in a shader as either a
22375466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       *   variable or a function."
22385466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       */
2239de3b40d8cdc42cc1cd71dd65c90d6d569d922fc6Ian Romanick      if (strncmp(decl->identifier, "gl_", 3) == 0)
22405466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick	 _mesa_glsl_error(& loc, state,
22415466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick			  "identifier `%s' uses reserved `gl_' prefix",
22425466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick			  decl->identifier);
22435466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
22445d25746640ee27882b69a962459727cf924443dbKenneth Graunke      /* Add the variable to the symbol table.  Note that the initializer's
22455d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * IR was already processed earlier (though it hasn't been emitted yet),
22465d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * without the variable in scope.
22475d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *
22485d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * This differs from most C-like languages, but it follows the GLSL
22495d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
22505d25746640ee27882b69a962459727cf924443dbKenneth Graunke       * spec:
22515d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *
22525d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *     "Within a declaration, the scope of a name starts immediately
22535d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *     after the initializer if present or immediately after the name
22545d25746640ee27882b69a962459727cf924443dbKenneth Graunke       *     being declared if not."
22555d25746640ee27882b69a962459727cf924443dbKenneth Graunke       */
2256001eee52d461233b1e1d6ed3577965e9bcb209e8Eric Anholt      if (!state->symbols->add_variable(var)) {
22575d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 YYLTYPE loc = this->get_location();
22585d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
22595d25746640ee27882b69a962459727cf924443dbKenneth Graunke			  "current scope", decl->identifier);
22605d25746640ee27882b69a962459727cf924443dbKenneth Graunke	 continue;
22615d25746640ee27882b69a962459727cf924443dbKenneth Graunke      }
22625d25746640ee27882b69a962459727cf924443dbKenneth Graunke
22638048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt      /* Push the variable declaration to the top.  It means that all
22648048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * the variable declarations will appear in a funny
22658048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * last-to-first order, but otherwise we run into trouble if a
22668048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * function is prototyped, a global var is decled, then the
22678048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * function is defined with usage of the global var.  See
22688048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       * glslparsertest's CorrectModule.frag.
22698048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt       */
22708048226b7b1bbe8fd89f9c32fa4fadca4b8760c4Eric Anholt      instructions->push_head(var);
2271fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      instructions->append_list(&initializer_instructions);
2272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
22748558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt
22758558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   /* Generally, variable declarations do not have r-values.  However,
22768558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * one is used for the declaration in
22778558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
22788558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * while (bool b = some_condition()) {
22798558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *   ...
22808558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * }
22818558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
22828558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * so we return the rvalue from the last seen declaration here.
2283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
22848558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   return result;
2285a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2286a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2287a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2288fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
22890044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions,
229018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
2291a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2292953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
2293a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *type;
2294a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *name = NULL;
22952e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   YYLTYPE loc = this->get_location();
2296a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2297d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   type = this->type->specifier->glsl_type(& name, state);
2298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type == NULL) {
2300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (name != NULL) {
2301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2302a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type `%s' in declaration of `%s'",
230318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  name, this->identifier);
2304a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
2305a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type in declaration of `%s'",
230718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->identifier);
2308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2309a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
23100471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::error_type;
2311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2312a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2313068c80cfe0a280490353b6b007165d717c672eedEric Anholt   /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2314068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
2315068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    "Functions that accept no input arguments need not use void in the
2316068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    argument list because prototypes (or definitions) are required and
2317068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    therefore there is no ambiguity when an empty argument list "( )" is
2318068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    declared. The idiom "(void)" as a parameter list is provided for
2319068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    convenience."
2320068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
2321068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * Placing this check here prevents a void parameter being set up
2322068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * for a function, which avoids tripping up checks for main taking
2323068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * parameters and lookups of an unnamed symbol.
2324068c80cfe0a280490353b6b007165d717c672eedEric Anholt    */
2325cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if (type->is_void()) {
2326cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (this->identifier != NULL)
2327cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 _mesa_glsl_error(& loc, state,
2328cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick			  "named parameter cannot have type `void'");
2329cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2330cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      is_void = true;
2331068c80cfe0a280490353b6b007165d717c672eedEric Anholt      return NULL;
2332cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
2333068c80cfe0a280490353b6b007165d717c672eedEric Anholt
233445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   if (formal_parameter && (this->identifier == NULL)) {
233545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
233645d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      return NULL;
233745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   }
233845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick
2339e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   /* This only handles "vec4 foo[..]".  The earlier specifier->glsl_type(...)
2340e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    * call already handled the "vec4[..] foo" case.
2341e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    */
2342e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   if (this->is_array) {
2343d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke      type = process_array_type(&loc, type, this->array_size, state);
2344e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
2345e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
2346e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   if (type->array_size() == 0) {
2347e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
2348e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke		       "a declared size.");
2349e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      type = glsl_type::error_type;
2350e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
2351e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
2352cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   is_void = false;
23537e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
2354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2355cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   /* Apply any specified qualifiers to the parameter declaration.  Note that
2356cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    * for function parameters the default mode is 'in'.
2357cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    */
23582e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
2359a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
23600044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(var);
2361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Parameter declarations do not have r-values.
2363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
2365a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2366a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
236845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanickvoid
2369304ea90233baeac6801a98e981658cb7a2d2501cIan Romanickast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
237045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    bool formal,
237145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    exec_list *ir_parameters,
237245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    _mesa_glsl_parse_state *state)
2373a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2374cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   ast_parameter_declarator *void_param = NULL;
2375cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   unsigned count = 0;
2376a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
23772b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
237845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      param->formal_parameter = formal;
2379068c80cfe0a280490353b6b007165d717c672eedEric Anholt      param->hir(ir_parameters, state);
2380cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2381cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (param->is_void)
2382cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 void_param = param;
2383cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2384cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      count++;
2385cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
2386cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2387cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if ((void_param != NULL) && (count > 1)) {
2388cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      YYLTYPE loc = void_param->get_location();
2389cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2390cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      _mesa_glsl_error(& loc, state,
2391cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick		       "`void' parameter must be only parameter");
2392a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2393a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2394a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2395a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2396fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
239792318a947958892497722772b03c643ebc943294Ian Romanickast_function::hir(exec_list *instructions,
239892318a947958892497722772b03c643ebc943294Ian Romanick		  struct _mesa_glsl_parse_state *state)
2399a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2400953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
240118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_function *f = NULL;
240292318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *sig = NULL;
240392318a947958892497722772b03c643ebc943294Ian Romanick   exec_list hir_parameters;
2404a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2405ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   const char *const name = identifier;
2406a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
240763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
240863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
240963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   "Function declarations (prototypes) cannot occur inside of functions;
241063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   they must be at global scope, or for the built-in functions, outside
241163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   the global scope."
241263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
241363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
241463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
241563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   "User defined functions may only be defined within the global scope."
241663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
241763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    * Note that this language does not appear in GLSL 1.10.
241863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    */
241963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   if ((state->current_function != NULL) && (state->language_version != 110)) {
242063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      YYLTYPE loc = this->get_location();
242163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      _mesa_glsl_error(&loc, state,
242263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick		       "declaration of function `%s' not allowed within "
242363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick		       "function body", name);
242463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   }
242563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick
2426edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
2427edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *
2428edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   "Identifiers starting with "gl_" are reserved for use by
2429edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   OpenGL, and may not be declared in a shader as either a
2430edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   variable or a function."
2431edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    */
2432edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   if (strncmp(name, "gl_", 3) == 0) {
2433edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      YYLTYPE loc = this->get_location();
2434edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      _mesa_glsl_error(&loc, state,
2435edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke		       "identifier `%s' uses reserved `gl_' prefix", name);
2436edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   }
2437edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke
2438a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the list of function parameters to HIR now so that they can be
2439a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * used below to compare this function's signature with previously seen
2440a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * signatures for functions with the same name.
2441a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
244245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   ast_parameter_declarator::parameters_to_hir(& this->parameters,
244345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       is_definition,
244445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       & hir_parameters, state);
2445a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2446e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const char *return_type_name;
2447e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const glsl_type *return_type =
244892318a947958892497722772b03c643ebc943294Ian Romanick      this->return_type->specifier->glsl_type(& return_type_name, state);
2449e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
245076e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   if (!return_type) {
245176e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      YYLTYPE loc = this->get_location();
245276e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      _mesa_glsl_error(&loc, state,
245376e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       "function `%s' has undeclared return type `%s'",
245476e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       name, return_type_name);
245576e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      return_type = glsl_type::error_type;
245676e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   }
2457e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
2458ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
2459ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    * "No qualifier is allowed on the return type of a function."
2460ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    */
2461ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   if (this->return_type->has_qualifiers()) {
2462ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      YYLTYPE loc = this->get_location();
2463ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      _mesa_glsl_error(& loc, state,
2464ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke		       "function `%s' return type has qualifiers", name);
2465ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   }
2466ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke
2467a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Verify that this function's signature either doesn't match a previously
2468a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * seen signature for a function with the same name, or, if a match is found,
2469a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * that the previously seen signature does not have an associated definition.
2470a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2471e466b182bbf21f62fe6542091f4af3275555db80Ian Romanick   f = state->symbols->get_function(name);
247281f03393982c29f8f4165b5629c8e8fb708b97a3Kenneth Graunke   if (f != NULL && (state->es_shader || f->has_user_signature())) {
2473202604e8160157e4e80b3458175e0170d168e557Ian Romanick      sig = f->exact_matching_signature(&hir_parameters);
24740d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke      if (sig != NULL) {
24750d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 const char *badvar = sig->qualifiers_match(&hir_parameters);
24760d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (badvar != NULL) {
24770d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
2478abd40b15210c17b2a3ba8fcffc868fda203efa01Kenneth Graunke
24790d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
24800d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "qualifiers don't match prototype", name, badvar);
24810d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
24821e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
24830d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (sig->return_type != return_type) {
24840d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
248560be7626b829af7e1d07330b9a88468924ba350eEric Anholt
24860d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
24870d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "match prototype", name);
24880d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
2489a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
24900d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (is_definition && sig->is_defined) {
24910d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
2492a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
24930d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
2494a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
2495a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2496a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
24971660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      f = new(ctx) ir_function(name);
2498e8f5ebf313da3ce33ccbbcf9b72946853035fbddEric Anholt      if (!state->symbols->add_function(f)) {
2499e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 /* This function name shadows a non-function use of the same name. */
2500e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 YYLTYPE loc = this->get_location();
2501e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke
2502e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
2503e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke			  "non-function", name);
2504e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 return NULL;
2505e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke      }
25069fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke
25079fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke      /* Emit the new function header */
250863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      if (state->current_function == NULL)
250963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	 instructions->push_tail(f);
251063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      else {
251163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	 /* IR invariants disallow function declarations or definitions nested
251263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * within other function definitions.  Insert the new ir_function
251363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * block in the instruction sequence before the ir_function block
251463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * containing the current ir_function_signature.
251563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  *
251663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * This can only happen in a GLSL 1.10 shader.  In all other GLSL
251763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * versions this nesting is disallowed.  There is a check for this at
251863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  * the top of this function.
251963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	  */
252063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	 ir_function *const curr =
252163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	    const_cast<ir_function *>(state->current_function->function());
252263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick
252363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick	 curr->insert_before(f);
252463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      }
2525a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2526a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2527ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   /* Verify the return type of main() */
2528ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   if (strcmp(name, "main") == 0) {
252925711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick      if (! return_type->is_void()) {
2530ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 YYLTYPE loc = this->get_location();
2531ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
2532ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must return void");
2533ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt      }
2534174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
253592318a947958892497722772b03c643ebc943294Ian Romanick      if (!hir_parameters.is_empty()) {
2536174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 YYLTYPE loc = this->get_location();
2537174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
2538174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
2539174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt      }
2540ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   }
2541a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2542a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Finish storing the information about this new function in its signature.
2543a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
254492318a947958892497722772b03c643ebc943294Ian Romanick   if (sig == NULL) {
25451660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      sig = new(ctx) ir_function_signature(return_type);
254692318a947958892497722772b03c643ebc943294Ian Romanick      f->add_signature(sig);
2547a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2548a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2549bff6013d469b3d4e54cdc5731801c56994a523ecKenneth Graunke   sig->replace_parameters(&hir_parameters);
255092318a947958892497722772b03c643ebc943294Ian Romanick   signature = sig;
255192318a947958892497722772b03c643ebc943294Ian Romanick
255292318a947958892497722772b03c643ebc943294Ian Romanick   /* Function declarations (prototypes) do not have r-values.
255392318a947958892497722772b03c643ebc943294Ian Romanick    */
255492318a947958892497722772b03c643ebc943294Ian Romanick   return NULL;
255592318a947958892497722772b03c643ebc943294Ian Romanick}
255692318a947958892497722772b03c643ebc943294Ian Romanick
255792318a947958892497722772b03c643ebc943294Ian Romanick
255892318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue *
255992318a947958892497722772b03c643ebc943294Ian Romanickast_function_definition::hir(exec_list *instructions,
256092318a947958892497722772b03c643ebc943294Ian Romanick			     struct _mesa_glsl_parse_state *state)
256192318a947958892497722772b03c643ebc943294Ian Romanick{
256292318a947958892497722772b03c643ebc943294Ian Romanick   prototype->is_definition = true;
256392318a947958892497722772b03c643ebc943294Ian Romanick   prototype->hir(instructions, state);
2564e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
256592318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *signature = prototype->signature;
2566826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke   if (signature == NULL)
2567826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke      return NULL;
2568a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
256941ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == NULL);
257041ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = signature;
25716de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   state->found_return = false;
257241ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
2573e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   /* Duplicate parameters declared in the prototype as concrete variables.
2574e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick    * Add these to the symbol table.
2575a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
25768bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->push_scope();
2577e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   foreach_iter(exec_list_iterator, iter, signature->parameters) {
2578fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
2579e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
2580fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      assert(var != NULL);
2581a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
25823359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* The only way a parameter would "exist" is if two parameters have
25833359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       * the same name.
25843359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
25853359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(var->name)) {
25863359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 YYLTYPE loc = this->get_location();
25873359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
25883359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
25893359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      } else {
2590001eee52d461233b1e1d6ed3577965e9bcb209e8Eric Anholt	 state->symbols->add_variable(var);
25913359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      }
2592a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2593a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
25949fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   /* Convert the body of the function to HIR. */
2595894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt   this->body->hir(&signature->body, state);
25969fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   signature->is_defined = true;
2597a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
25988bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->pop_scope();
2599a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
260041ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == signature);
260141ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
2602a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
26036de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   if (!signature->return_type->is_void() && !state->found_return) {
26046de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      YYLTYPE loc = this->get_location();
26056de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
26066de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       "%s, but no return statement",
26076de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->function_name(),
26086de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->return_type->name);
26096de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   }
26106de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke
2611a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Function definitions do not have r-values.
2612a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
2613a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
2614a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
261516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
261616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2617fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
261816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions,
261916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			struct _mesa_glsl_parse_state *state)
262016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{
2621953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
262216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2623c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   switch (mode) {
2624c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_return: {
262516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      ir_return *inst;
2626aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt      assert(state->current_function);
262716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
262816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      if (opt_return_value) {
2629ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 if (state->current_function->return_type->base_type ==
2630ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	     GLSL_TYPE_VOID) {
2631ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    YYLTYPE loc = this->get_location();
2632ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt
2633ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	    _mesa_glsl_error(& loc, state,
2634ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "`return` with a value, in function `%s' "
2635ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt			     "returning void",
2636f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
2637ab79d4ec6e7c5639084f71f93857f39239e8b848Eric Anholt	 }
263816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2639b4cdba687c098eea2ecc61349a4ea02a8769909eChad Versace	 ir_rvalue *const ret = opt_return_value->hir(instructions, state);
264016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick	 assert(ret != NULL);
264116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
264218707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 /* Implicit conversions are not allowed for return values. */
264318707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 if (state->current_function->return_type != ret->type) {
264418707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    YYLTYPE loc = this->get_location();
264518707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke
264618707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    _mesa_glsl_error(& loc, state,
264718707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "`return' with wrong type %s, in function `%s' "
264818707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "returning %s",
264918707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     ret->type->name,
265018707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->function_name(),
265118707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->return_type->name);
265218707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 }
265316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
26541660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return(ret);
265516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      } else {
2656aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 if (state->current_function->return_type->base_type !=
2657aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	     GLSL_TYPE_VOID) {
2658aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    YYLTYPE loc = this->get_location();
2659aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt
2660aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    _mesa_glsl_error(& loc, state,
2661aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "`return' with no value, in function %s returning "
2662aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "non-void",
2663f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
2664aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 }
26651660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return;
266616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      }
266716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
26686de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      state->found_return = true;
266916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      instructions->push_tail(inst);
2670c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
267116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   }
267216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
2673c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_discard:
2674b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      if (state->target != fragment_shader) {
2675b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 YYLTYPE loc = this->get_location();
2676b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
2677b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 _mesa_glsl_error(& loc, state,
2678b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt			  "`discard' may only appear in a fragment shader");
2679b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      }
268077049a702ad54e09c4102fe8c964e069944f83e5Kenneth Graunke      instructions->push_tail(new(ctx) ir_discard);
2681c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
2682c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick
2683c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_break:
2684c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_continue:
26854cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      /* FINISHME: Handle switch-statements.  They cannot contain 'continue',
26864cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: and they use a different IR instruction for 'break'.
26874cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       */
26884cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      /* FINISHME: Correctly handle the nesting.  If a switch-statement is
26894cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: inside a loop, a 'continue' is valid and will bind to the
26904cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       * FINISHME: loop.
26914cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick       */
26924cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      if (state->loop_or_switch_nesting == NULL) {
26934cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 YYLTYPE loc = this->get_location();
26944cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
26954cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 _mesa_glsl_error(& loc, state,
26964cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick			  "`%s' may only appear in a loop",
26974cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick			  (mode == ast_break) ? "break" : "continue");
26984cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      } else {
26994cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
27004cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
27012d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 /* Inline the for loop expression again, since we don't know
27022d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  * where near the end of the loop body the normal copy of it
27032d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  * is going to be placed.
27042d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  */
27052d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 if (mode == ast_continue &&
27062d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	     state->loop_or_switch_nesting_ast->rest_expression) {
27072d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	    state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
27082d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt								    state);
27092d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 }
27102d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt
27114cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 if (loop != NULL) {
27124cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    ir_loop_jump *const jump =
27131660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	       new(ctx) ir_loop_jump((mode == ast_break)
27141660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     ? ir_loop_jump::jump_break
27151660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     : ir_loop_jump::jump_continue);
27164cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    instructions->push_tail(jump);
27174cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 }
27184cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      }
27194cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
2720c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
2721b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   }
2722b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
272316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   /* Jump instructions do not have r-values.
272416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
272516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
272616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
27273c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
27283c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
27293c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue *
27303c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions,
27313c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			     struct _mesa_glsl_parse_state *state)
27323c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
2733953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
27341660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
27353c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_rvalue *const condition = this->condition->hir(instructions, state);
27363c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
27373c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
27383c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
27393c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    "Any expression whose type evaluates to a Boolean can be used as the
27403c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    conditional expression bool-expression. Vector types are not accepted
27413c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    as the expression to if."
27423c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
27433c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * The checks are separated so that higher quality diagnostics can be
27443c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * generated for cases where both rules are violated.
27453c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
27463c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
27473c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      YYLTYPE loc = this->condition->get_location();
27483c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
27493c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
27503c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick		       "boolean");
27513c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
27523c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
27531660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_if *const stmt = new(ctx) ir_if(condition);
27543c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
2755665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (then_statement != NULL) {
2756665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
27574f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      then_statement->hir(& stmt->then_instructions, state);
2758665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
2759665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
27603c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
2761665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (else_statement != NULL) {
2762665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
27634f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      else_statement->hir(& stmt->else_instructions, state);
2764665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
2765665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
27663c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
27673c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   instructions->push_tail(stmt);
27683c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
27693c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* if-statements do not have r-values.
27703c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
27713c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   return NULL;
27723c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick}
27739e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
27749e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
27758c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickvoid
27768c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::condition_to_hir(ir_loop *stmt,
27778c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick					  struct _mesa_glsl_parse_state *state)
27789e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick{
2779953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
27801660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
27819e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (condition != NULL) {
27829e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      ir_rvalue *const cond =
27839e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 condition->hir(& stmt->body_instructions, state);
27849e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
27859e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      if ((cond == NULL)
27869e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  || !cond->type->is_boolean() || !cond->type->is_scalar()) {
27879e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 YYLTYPE loc = condition->get_location();
27889e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
27899e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 _mesa_glsl_error(& loc, state,
27909e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick			  "loop condition must be scalar boolean");
27919e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      } else {
27929e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 /* As the first code in the loop body, generate a block that looks
27939e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  * like 'if (!condition) break;' as the loop termination condition.
27949e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	  */
27959e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_rvalue *const not_cond =
27961660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
27971660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				   NULL);
27989e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
27991660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
28009e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
28019e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 ir_jump *const break_stmt =
28021660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
28039e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
28049e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 if_stmt->then_instructions.push_tail(break_stmt);
28059e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick	 stmt->body_instructions.push_tail(if_stmt);
28069e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      }
28079e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   }
28088c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick}
28098c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
28108c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
28118c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickir_rvalue *
28128c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanickast_iteration_statement::hir(exec_list *instructions,
28138c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick			     struct _mesa_glsl_parse_state *state)
28148c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick{
2815953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
28161660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
2817484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   /* For-loops and while-loops start a new scope, but do-while loops do not.
28188c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
2819484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   if (mode != ast_do_while)
28208c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      state->symbols->push_scope();
28218c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
28228c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (init_statement != NULL)
28238c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      init_statement->hir(instructions, state);
28248c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
28251660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_loop *const stmt = new(ctx) ir_loop();
28268c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   instructions->push_tail(stmt);
28278c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
28288c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   /* Track the current loop and / or switch-statement nesting.
28298c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick    */
28308c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   ir_instruction *const nesting = state->loop_or_switch_nesting;
28312d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt   ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
28322d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt
28338c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   state->loop_or_switch_nesting = stmt;
28342d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt   state->loop_or_switch_nesting_ast = this;
28358c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
28368c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode != ast_do_while)
28378c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      condition_to_hir(stmt, state);
28389e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
28394f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick   if (body != NULL)
28404f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      body->hir(& stmt->body_instructions, state);
28419e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
28429e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   if (rest_expression != NULL)
28439e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      rest_expression->hir(& stmt->body_instructions, state);
28449e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
28458c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick   if (mode == ast_do_while)
28468c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick      condition_to_hir(stmt, state);
28478c46ed24906ee10dd2f2cfaf4cf9803eca1ba523Ian Romanick
2848484606610e36ec7598f7733e9787d888b6a63d64Ian Romanick   if (mode != ast_do_while)
28499e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick      state->symbols->pop_scope();
28509e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
2851e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   /* Restore previous nesting before returning.
2852e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick    */
2853e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick   state->loop_or_switch_nesting = nesting;
28542d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt   state->loop_or_switch_nesting_ast = nesting_ast;
2855e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick
28569e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   /* Loops do not have r-values.
28579e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
28589e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   return NULL;
28599e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick}
28603455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
28613455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
28623455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
28633455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_type_specifier::hir(exec_list *instructions,
28643455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
28653455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
28663455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   if (this->structure != NULL)
28673455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      return this->structure->hir(instructions, state);
286885ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick
286985ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick   return NULL;
28703455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
28713455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
28723455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
28733455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
28743455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_struct_specifier::hir(exec_list *instructions,
28753455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
28763455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
28773455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned decl_count = 0;
28783455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
28793455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Make an initial pass over the list of structure fields to determine how
28803455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * many there are.  Each element in this list is an ast_declarator_list.
28813455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * This means that we actually need to count the number of elements in the
28823455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * 'declarations' list in each of the elements.
28833455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
28842b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
28852b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
2886304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      foreach_list_const (decl_ptr, & decl_list->declarations) {
28873455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_count++;
28883455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
28893455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
28903455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
28913455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Allocate storage for the structure fields and process the field
28923455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * declarations.  As the declarations are processed, try to also convert
28933455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * the types to HIR.  This ensures that structure definitions embedded in
28943455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * other structure definitions are processed.
28953455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
289621b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt   glsl_struct_field *const fields = talloc_array(state, glsl_struct_field,
289721b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt						  decl_count);
28983455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
28993455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned i = 0;
29002b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
29012b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
29023455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const char *type_name;
29033455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
29043455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      decl_list->type->specifier->hir(instructions, state);
29053455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
2906c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke      /* Section 10.9 of the GLSL ES 1.00 specification states that
2907c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke       * embedded structure definitions have been removed from the language.
2908c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke       */
2909c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke      if (state->es_shader && decl_list->type->specifier->structure != NULL) {
2910c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke	 YYLTYPE loc = this->get_location();
2911c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke	 _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
2912c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke			  "not allowed in GLSL ES 1.00.");
2913c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke      }
2914c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke
29153455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const glsl_type *decl_type =
29163455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_list->type->specifier->glsl_type(& type_name, state);
29173455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
29182b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_declaration, decl, link,
29192b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick			  &decl_list->declarations) {
2920d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 const struct glsl_type *field_type = decl_type;
2921d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 if (decl->is_array) {
2922d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	    YYLTYPE loc = decl->get_location();
2923d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	    field_type = process_array_type(&loc, decl_type, decl->array_size,
2924d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke					    state);
2925d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 }
292673986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	 fields[i].type = (field_type != NULL)
292773986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	    ? field_type : glsl_type::error_type;
29283455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 fields[i].name = decl->identifier;
29293455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 i++;
29303455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
29313455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
29323455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
29333455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   assert(i == decl_count);
29343455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
293549e3577b91f44013746f7a3db411e7041b7d899eIan Romanick   const glsl_type *t =
2936ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke      glsl_type::get_record_instance(fields, decl_count, this->name);
29371d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
2938ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   YYLTYPE loc = this->get_location();
2939a789ca649cb143c0c5bf3209ff1bde398fbd777eIan Romanick   if (!state->symbols->add_type(name, t)) {
2940ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
2941ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   } else {
2942a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick
2943a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      const glsl_type **s = (const glsl_type **)
2944a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 realloc(state->user_structures,
2945a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick		 sizeof(state->user_structures[0]) *
2946a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick		 (state->num_user_structures + 1));
2947a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      if (s != NULL) {
2948a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 s[state->num_user_structures] = t;
2949a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->user_structures = s;
2950a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->num_user_structures++;
2951a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      }
2952ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   }
29533455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
29543455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Structure type definitions do not have r-values.
29553455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
29563455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   return NULL;
29573455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
2958