ast_to_hir.cpp revision ead3589aa2810b66164178a1d55d2063cfa3b041
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"
57140632190cf41e6a035ca199b181091d4ed46986Eric Anholt#include "program/hash_table.h"
58a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick#include "ir.h"
59a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
60d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanickvoid
61d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
62d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick{
63adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick   _mesa_glsl_initialize_variables(instructions, state);
64adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick
65814c89abdbcd5b841b98746af921796df0362238Kenneth Graunke   state->symbols->language_version = state->language_version;
66814c89abdbcd5b841b98746af921796df0362238Kenneth Graunke
6741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
6841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
690d81b0e18494a80c4326fbc98837842959675869Paul Berry   state->toplevel_ir = instructions;
700d81b0e18494a80c4326fbc98837842959675869Paul Berry
71a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke   /* Section 4.2 of the GLSL 1.20 specification states:
72a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * "The built-in functions are scoped in a scope outside the global scope
73a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  users declare global variables in.  That is, a shader's global scope,
74a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  available for user-defined functions and global variables, is nested
75a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  inside the scope containing the built-in functions."
76a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *
77a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * Since built-in functions like ftransform() access built-in variables,
78a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * it follows that those must be in the outer scope as well.
79a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *
80a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * We push scope here to create this nesting effect...but don't pop.
81a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * This way, a shader's globals are still in the symbol table for use
82a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * by the linker.
83a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    */
84a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke   state->symbols->push_scope();
85a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke
862b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, & state->translation_unit)
87304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
8802c5ae1b3fef75d5c0a715313a69e6b95ebd5b95Ian Romanick
8902c5ae1b3fef75d5c0a715313a69e6b95ebd5b95Ian Romanick   detect_recursion_unlinked(state, instructions);
900d81b0e18494a80c4326fbc98837842959675869Paul Berry
910d81b0e18494a80c4326fbc98837842959675869Paul Berry   state->toplevel_ir = NULL;
92d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick}
93d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
94d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
950104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick/**
960104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is available, convert one operand to a different type
970104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
980104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * The \c from \c ir_rvalue is converted "in place".
990104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
1000104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param to     Type that the operand it to be converted to
1010104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param from   Operand that is being converted
1020104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param state  GLSL compiler state
1030104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
1040104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \return
1050104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is possible (or unnecessary), \c true is returned.
1060104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * Otherwise \c false is returned.
1070104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */
108f32d3df8ab2b7c6c746f46870edc4b284cea50caKenneth Graunkebool
109bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
1100104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick			  struct _mesa_glsl_parse_state *state)
1110104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick{
112953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
113bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (to->base_type == from->type->base_type)
1140104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return true;
1150104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1160104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* This conversion was added in GLSL 1.20.  If the compilation mode is
1170104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * GLSL 1.10, the conversion is skipped.
1180104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1190104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (state->language_version < 120)
1200104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1210104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1220104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
1230104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *
1240104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    "There are no implicit array or structure conversions. For
1250104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    example, an array of int cannot be implicitly converted to an
1260104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    array of float. There are no implicit conversions between
1270104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    signed and unsigned integers."
1280104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1290104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* FINISHME: The above comment is partially a lie.  There is int/uint
1300104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * FINISHME: conversion for immediate constants.
1310104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
132bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (!to->is_float() || !from->type->is_numeric())
1330104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1340104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
135506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke   /* Convert to a floating point type with the same number of components
136506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke    * as the original type - i.e. int to float, not int to vec4.
137506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke    */
138506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke   to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
139506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke			        from->type->matrix_columns);
140506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke
141bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   switch (from->type->base_type) {
1420104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_INT:
1431660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
1440104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1450104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_UINT:
1461660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
1470104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1480104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_BOOL:
1491660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
150dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt      break;
1510104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   default:
1520104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      assert(0);
1530104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   }
1540104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1550104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   return true;
1560104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick}
1570104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1580104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
159a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
160bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
161a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       bool multiply,
162a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
163a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
164336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
165336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
1660104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
167a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
169a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic binary operators add (+), subtract (-),
170a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    multiply (*), and divide (/) operate on integer and
171a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    floating-point scalars, vectors, and matrices."
172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
17360b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   if (!type_a->is_numeric() || !type_b->is_numeric()) {
174a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
175a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Operands to arithmetic operators must be numeric");
1760471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
177a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
178a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
179a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
180a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If one operand is floating-point based and the other is
181a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    not, then the conversions from Section 4.1.10 "Implicit
182a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Conversions" are applied to the non-floating-point-based operand."
183a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1840104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
1850104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
186a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
187a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Could not implicitly convert operands to "
188a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "arithmetic operator");
1890104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return glsl_type::error_type;
190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
191336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
192336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
193336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
194a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If the operands are integer types, they must both be signed or
195a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    both be unsigned."
196a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
197a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * From this rule and the preceeding conversion it can be inferred that
198a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
19960b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * The is_numeric check above already filtered out the case where either
20060b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * type is not one of these, so now the base types need only be tested for
20160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * equality.
202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type_a->base_type != type_b->base_type) {
204a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
205a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "base type mismatch for arithmetic operator");
2060471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
207a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
208a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
209a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All arithmetic binary operators result in the same fundamental type
210a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (signed integer, unsigned integer, or floating-point) as the
211a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operands they operate on, after operand type conversion. After
212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    conversion, the following cases are valid
213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The two operands are scalars. In this case the operation is
215a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      applied, resulting in a scalar."
216a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
217cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar() && type_b->is_scalar())
218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
219a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
220a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* One operand is a scalar, and the other is a vector or matrix.
221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      In this case, the scalar operation is applied independently to each
222a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      component of the vector or matrix, resulting in the same size
223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector or matrix."
224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
225cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar()) {
226cb36f8aaeeb09660843316270a781948f773d90bIan Romanick      if (!type_b->is_scalar())
227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_b;
228cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   } else if (type_b->is_scalar()) {
229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
230a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
231a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * handled.
235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
23660b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_a->is_scalar());
23760b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_b->is_scalar());
238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
239a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The two operands are vectors of the same size. In this case, the
240a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operation is done component-wise resulting in the same size
241a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector."
242a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
243a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector() && type_b->is_vector()) {
244a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b) {
245a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
246a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      } else {
247a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 _mesa_glsl_error(loc, state,
248a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt			  "vector size mismatch for arithmetic operator");
249a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return glsl_type::error_type;
250a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      }
251a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
252a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
253a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
254a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
255a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <vector, vector> have been handled.  At least one of the operands must
256a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * be matrix.  Further, since there are no integer matrix types, the base
257a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * type of both operands must be float.
258a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
25960b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(type_a->is_matrix() || type_b->is_matrix());
260a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_a->base_type == GLSL_TYPE_FLOAT);
261a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_b->base_type == GLSL_TYPE_FLOAT);
262a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
263a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The operator is add (+), subtract (-), or divide (/), and the
264a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operands are matrices with the same number of rows and the same
265a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      number of columns. In this case, the operation is done component-
266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      wise resulting in the same size matrix."
267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The operator is multiply (*), where both operands are matrices or
268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      one operand is a vector and the other a matrix. A right vector
269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand is treated as a column vector and a left vector operand as a
270a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      row vector. In all these cases, it is required that the number of
271a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      columns of the left operand is equal to the number of rows of the
272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      right operand. Then, the multiply (*) operation does a linear
273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      algebraic multiply, yielding an object that has the same number of
274a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      rows as the left operand and the same number of columns as the right
275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand. Section 5.10 "Vector and Matrix Operations" explains in
276a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      more detail how vectors and matrices are operated on."
277a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
278a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (! multiply) {
279a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b)
280a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
282fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      if (type_a->is_matrix() && type_b->is_matrix()) {
283c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* Matrix multiply.  The columns of A must match the rows of B.  Given
284c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the other previously tested constraints, this means the vector type
285c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of a row from A must be the same as the vector type of a column from
286c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
287c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  */
288c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b->column_type()) {
289c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	    /* The resulting matrix has the number of columns of matrix B and
290c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * the number of rows of matrix A.  We get the row count of A by
291c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * looking at the size of a vector that makes up a column.  The
292c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * transpose (size of a row) is done for B.
293c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     */
294a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    const glsl_type *const type =
295c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	       glsl_type::get_instance(type_a->base_type,
296c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_a->column_type()->vector_elements,
297c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_b->row_type()->vector_elements);
298a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    assert(type != glsl_type::error_type);
299a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
300a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    return type;
301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
302fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      } else if (type_a->is_matrix()) {
303a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* A is a matrix and B is a column vector.  Columns of A must match
304c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * rows of B.  Given the other previously tested constraints, this
305c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * means the vector type of a row from A must be the same as the
306c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * vector the type of B.
307a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
30847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 if (type_a->row_type() == type_b) {
30947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    /* The resulting vector has a number of elements equal to
31047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	     * the number of rows of matrix A. */
31147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    const glsl_type *const type =
31247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	       glsl_type::get_instance(type_a->base_type,
31347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       type_a->column_type()->vector_elements,
31447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       1);
31547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    assert(type != glsl_type::error_type);
31647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth
31747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    return type;
31847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 }
319a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
320fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick	 assert(type_b->is_matrix());
321a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
322c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* A is a row vector and B is a matrix.  Columns of A must match rows
323c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of B.  Given the other previously tested constraints, this means
324c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the type of A must be the same as the vector type of a column from
325c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
326a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
32747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 if (type_a == type_b->column_type()) {
32847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    /* The resulting vector has a number of elements equal to
32947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	     * the number of columns of matrix B. */
33047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    const glsl_type *const type =
33147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	       glsl_type::get_instance(type_a->base_type,
33247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       type_b->row_type()->vector_elements,
33347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       1);
33447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    assert(type != glsl_type::error_type);
33547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth
33647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    return type;
33747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 }
338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
339a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
340a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
341a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      return glsl_type::error_type;
342a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
344a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All other cases are illegal."
346a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
347a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3480471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
349a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
350a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
35365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholtunary_arithmetic_result_type(const struct glsl_type *type,
35465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt			     struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 57:
357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
358a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic unary operators negate (-), post- and pre-increment
359a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     and decrement (-- and ++) operate on integer or floating-point
360a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     values (including vectors and matrices). All unary operators work
361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     component-wise on their operands. These result with the same type
362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     they operated on."
363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
36465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (!type->is_numeric()) {
36565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
36665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to arithmetic operators must be numeric");
3670471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
36865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
369a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
370a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
372a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
373cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace/**
374cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \brief Return the result type of a bit-logic operation.
375cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace *
376cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * If the given types to the bit-logic operator are invalid, return
377cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * glsl_type::error_type.
378cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace *
379cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \param type_a Type of LHS of bit-logic op
380cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \param type_b Type of RHS of bit-logic op
381cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace */
382cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versacestatic const struct glsl_type *
383cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versacebit_logic_result_type(const struct glsl_type *type_a,
384cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                      const struct glsl_type *type_b,
385cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                      ast_operators op,
386cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                      struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
387cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace{
388cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (state->language_version < 130) {
389cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
390cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
391cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
392cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace
393cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
394cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *
395cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     "The bitwise operators and (&), exclusive-or (^), and inclusive-or
396cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     (|). The operands must be of type signed or unsigned integers or
397cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     integer vectors."
398cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     */
399cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (!type_a->is_integer()) {
400cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
401cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                         ast_expression::operator_string(op));
402cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
403cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
404cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (!type_b->is_integer()) {
405cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
406cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                        ast_expression::operator_string(op));
407cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
408cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
409cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace
410cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    /*     "The fundamental types of the operands (signed or unsigned) must
411cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     match,"
412cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     */
413cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (type_a->base_type != type_b->base_type) {
414cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
415cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                        "base type", ast_expression::operator_string(op));
416cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
417cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
418cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace
419cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    /*     "The operands cannot be vectors of differing size." */
420cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (type_a->is_vector() &&
421cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace        type_b->is_vector() &&
422cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace        type_a->vector_elements != type_b->vector_elements) {
423cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
424cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                        "different sizes", ast_expression::operator_string(op));
425cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
426cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
427cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace
428cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    /*     "If one operand is a scalar and the other a vector, the scalar is
429cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     applied component-wise to the vector, resulting in the same type as
430cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     the vector. The fundamental types of the operands [...] will be the
431cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     resulting fundamental type."
432cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     */
433cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (type_a->is_scalar())
434cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace        return type_b;
435cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    else
436cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace        return type_a;
437cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace}
438a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
439a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
440a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickmodulus_result_type(const struct glsl_type *type_a,
44165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    const struct glsl_type *type_b,
44265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
443a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
44482f994f3860ca05ff5550f32844b0f523d40b9efChad Versace   if (state->language_version < 130) {
44582f994f3860ca05ff5550f32844b0f523d40b9efChad Versace      _mesa_glsl_error(loc, state,
44682f994f3860ca05ff5550f32844b0f523d40b9efChad Versace                       "operator '%%' is reserved in %s",
44782f994f3860ca05ff5550f32844b0f523d40b9efChad Versace                       state->version_string);
44882f994f3860ca05ff5550f32844b0f523d40b9efChad Versace      return glsl_type::error_type;
44982f994f3860ca05ff5550f32844b0f523d40b9efChad Versace   }
45082f994f3860ca05ff5550f32844b0f523d40b9efChad Versace
451a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
452a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The operator modulus (%) operates on signed or unsigned integers or
453a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    integer vectors. The operand types must both be signed or both be
454a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    unsigned."
455a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4568eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke   if (!type_a->is_integer()) {
4578eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke      _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer.");
4588eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke      return glsl_type::error_type;
4598eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke   }
4608eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke   if (!type_b->is_integer()) {
4618eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke      _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer.");
4628eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke      return glsl_type::error_type;
4638eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke   }
4648eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke   if (type_a->base_type != type_b->base_type) {
4658eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke      _mesa_glsl_error(loc, state,
4668eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke		       "operands of %% must have the same base type");
4670471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
468a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
469a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
470a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operands cannot be vectors of differing size. If one operand is
471a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    a scalar and the other vector, then the scalar is applied component-
472a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    wise to the vector, resulting in the same type as the vector. If both
473a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    are vectors of the same size, the result is computed component-wise."
474a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
475a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector()) {
476a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick      if (!type_b->is_vector()
477a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  || (type_a->vector_elements == type_b->vector_elements))
478a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_a;
479a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else
480a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_b;
481a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
482a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operator modulus (%) is not defined for any other data types
483a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (non-integer types)."
484a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
48565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
4860471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
487a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
488a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
489a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
490a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
491bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
49265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
493a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
494336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
495336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
4960150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick
497a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
498a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The relational operators greater than (>), less than (<), greater
499a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    than or equal (>=), and less than or equal (<=) operate only on
500a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    scalar integer and scalar floating-point expressions."
501a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
502a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick   if (!type_a->is_numeric()
503a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick       || !type_b->is_numeric()
504cb36f8aaeeb09660843316270a781948f773d90bIan Romanick       || !type_a->is_scalar()
50565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt       || !type_b->is_scalar()) {
50665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
50765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to relational operators must be scalar and "
50865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "numeric");
5090471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
51065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
511a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
512a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "Either the operands' types must match, or the conversions from
513a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
514a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operand, after which the types must match."
515a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
5160150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
5170150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
51865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
51965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Could not implicitly convert operands to "
52065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "relational operator");
5210150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick      return glsl_type::error_type;
522a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
523336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
524336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
525a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
52665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (type_a->base_type != type_b->base_type) {
52765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "base type mismatch");
5280471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
52965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
530a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
531a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The result is scalar Boolean."
532a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
5330471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::bool_type;
534a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
535a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
536c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace/**
537c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \brief Return the result type of a bit-shift operation.
538c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace *
539c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * If the given types to the bit-shift operator are invalid, return
540c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * glsl_type::error_type.
541c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace *
542c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \param type_a Type of LHS of bit-shift op
543c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \param type_b Type of RHS of bit-shift op
544c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */
545c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versacestatic const struct glsl_type *
546c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versaceshift_result_type(const struct glsl_type *type_a,
547c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                  const struct glsl_type *type_b,
548c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                  ast_operators op,
549c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                  struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
550c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace{
551c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (state->language_version < 130) {
552c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
553c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      return glsl_type::error_type;
554c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
555c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
556c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
557c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *
558c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     "The shift operators (<<) and (>>). For both operators, the operands
559c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     must be signed or unsigned integers or integer vectors. One operand
560c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     can be signed while the other is unsigned."
561c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    */
562c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (!type_a->is_integer()) {
563c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
564c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              "integer vector", ast_expression::operator_string(op));
565c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace     return glsl_type::error_type;
566c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
567c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
568c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (!type_b->is_integer()) {
569c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
570c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              "integer vector", ast_expression::operator_string(op));
571c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace     return glsl_type::error_type;
572c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
573c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
574c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   /*     "If the first operand is a scalar, the second operand has to be
575c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     a scalar as well."
576c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    */
577c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (type_a->is_scalar() && !type_b->is_scalar()) {
578c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the "
579c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              "second must be scalar as well",
580c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              ast_expression::operator_string(op));
581c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace     return glsl_type::error_type;
582c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
583c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
584c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   /* If both operands are vectors, check that they have same number of
585c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    * elements.
586c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    */
587c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (type_a->is_vector() &&
588c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      type_b->is_vector() &&
589c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      type_a->vector_elements != type_b->vector_elements) {
590c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "Vector operands to operator %s must "
591c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              "have same number of elements",
592c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              ast_expression::operator_string(op));
593c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace     return glsl_type::error_type;
594c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
595c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
596c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   /*     "In all cases, the resulting type will be the same type as the left
597c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     operand."
598c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    */
599c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   return type_a;
600c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace}
601a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
6020bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/**
6030bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type
6040bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
6050bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location.  If the types are
6060bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be
6070bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted.
6080bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
6090bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return
6100bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
6110bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned.  This may be
6120bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion.
6130bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
6140bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note
6150bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to
6160bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values.
6170bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */
618fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
619336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholtvalidate_assignment(struct _mesa_glsl_parse_state *state,
62085caea29c18fad89050ac366c558afef568dcb3fIan Romanick		    const glsl_type *lhs_type, ir_rvalue *rhs,
62185caea29c18fad89050ac366c558afef568dcb3fIan Romanick		    bool is_initializer)
6220bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{
6230bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If there is already some error in the RHS, just return it.  Anything
6240bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    * else will lead to an avalanche of error message back to the user.
6250bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
626ec53010c4d02e11171d3c782a41b70cad76788e8Ian Romanick   if (rhs->type->is_error())
6270bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
6280bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
6290bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If the types are identical, the assignment can trivially proceed.
6300bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
631ec53010c4d02e11171d3c782a41b70cad76788e8Ian Romanick   if (rhs->type == lhs_type)
6320bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
6330bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
6340157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   /* If the array element types are the same and the size of the LHS is zero,
63585caea29c18fad89050ac366c558afef568dcb3fIan Romanick    * the assignment is okay for initializers embedded in variable
63685caea29c18fad89050ac366c558afef568dcb3fIan Romanick    * declarations.
6370157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    *
6380157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
6390157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * is handled by ir_dereference::is_lvalue.
6400157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    */
64185caea29c18fad89050ac366c558afef568dcb3fIan Romanick   if (is_initializer && lhs_type->is_array() && rhs->type->is_array()
6420157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->element_type() == rhs->type->element_type())
6430157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->array_size() == 0)) {
6440157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      return rhs;
6450157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   }
6460157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
647336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   /* Check for implicit conversion in GLSL 1.20 */
648336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   if (apply_implicit_conversion(lhs_type, rhs, state)) {
649ec53010c4d02e11171d3c782a41b70cad76788e8Ian Romanick      if (rhs->type == lhs_type)
650336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt	 return rhs;
651336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   }
652336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
6530bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   return NULL;
6540bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick}
6550bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
656a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholtstatic void
657a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholtmark_whole_array_access(ir_rvalue *access)
658a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt{
659a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt   ir_dereference_variable *deref = access->as_dereference_variable();
660a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt
661a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt   if (deref && deref->var) {
662a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt      deref->var->max_array_access = deref->type->length - 1;
663a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt   }
664a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt}
665a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt
66610a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue *
66710a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
668e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick	      const char *non_lvalue_description,
66985caea29c18fad89050ac366c558afef568dcb3fIan Romanick	      ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer,
67010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      YYLTYPE lhs_loc)
67110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{
672953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
67310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
67410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
67510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (!error_emitted) {
676e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      if (non_lvalue_description != NULL) {
677e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick         _mesa_glsl_error(&lhs_loc, state,
678e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick                          "assignment to %s",
679e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			  non_lvalue_description);
680e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick	 error_emitted = true;
681e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      } else if (lhs->variable_referenced() != NULL
682e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick		 && lhs->variable_referenced()->read_only) {
683b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace         _mesa_glsl_error(&lhs_loc, state,
684b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace                          "assignment to read-only variable '%s'",
685b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace                          lhs->variable_referenced()->name);
686b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace         error_emitted = true;
687b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace
688525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt      } else if (state->language_version <= 110 && lhs->type->is_array()) {
689525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
690525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	  *
691525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	  *    "Other binary or unary expressions, non-dereferenced
692525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	  *     arrays, function names, swizzles with repeated fields,
693525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	  *     and constants cannot be l-values."
694525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	  */
695525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	 _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not "
696525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt			  "allowed in GLSL 1.10 or GLSL ES 1.00.");
697525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	 error_emitted = true;
698b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace      } else if (!lhs->is_lvalue()) {
69910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
70010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 error_emitted = true;
70110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      }
70210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
70310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
70485caea29c18fad89050ac366c558afef568dcb3fIan Romanick   ir_rvalue *new_rhs =
70585caea29c18fad89050ac366c558afef568dcb3fIan Romanick      validate_assignment(state, lhs->type, rhs, is_initializer);
70610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (new_rhs == NULL) {
70710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      _mesa_glsl_error(& lhs_loc, state, "type mismatch");
70810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   } else {
70910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      rhs = new_rhs;
7100157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
7110157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      /* If the LHS array was not declared with a size, it takes it size from
7120157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * the RHS.  If the LHS is an l-value and a whole array, it must be a
7130157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * dereference of a variable.  Any other case would require that the LHS
7140157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * is either not an l-value or not a whole array.
7150157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       */
7160157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      if (lhs->type->array_size() == 0) {
7170157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 ir_dereference *const d = lhs->as_dereference();
7180157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
7190157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(d != NULL);
7200157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
72136ea28646c666ac2af9b43c47e65f9f53ffcc390Ian Romanick	 ir_variable *const var = d->variable_referenced();
7220157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
7230157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(var != NULL);
7240157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
72563f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
72663f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    /* FINISHME: This should actually log the location of the RHS. */
72763f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
72863f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     "previous access",
72963f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     var->max_array_access);
73063f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 }
73163f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick
732f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick	 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
7330157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick						   rhs->type->array_size());
7349703ed05e684f4269cd8af27c94e9b6bf8781d85Eric Anholt	 d->type = var->type;
7350157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      }
736407a1001aefcb15e8d066031417d91ea22f1daf1Eric Anholt      mark_whole_array_access(rhs);
737a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt      mark_whole_array_access(lhs);
73810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
73910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
7402731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt   /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
7412731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * but not post_inc) need the converted assigned value as an rvalue
7422731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * to handle things like:
7432731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
7442731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * i = j += 1;
7452731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
7462731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * So we always just store the computed value being assigned to a
7472731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * temporary and return a deref of that temporary.  If the rvalue
7482731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * ends up not being used, the temp will get copy-propagated out.
7492731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    */
7507e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
7517e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick					   ir_var_temporary);
752e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
753ae805922b7e3cdaf3aee26c3b799fe3608669bbaEric Anholt   instructions->push_tail(var);
754e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   instructions->push_tail(new(ctx) ir_assignment(deref_var,
7551660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  rhs,
7561660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  NULL));
757e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   deref_var = new(ctx) ir_dereference_variable(var);
7582731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt
7598e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick   if (!error_emitted)
7608e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick      instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
76110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
7621660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
76310a685218610e737e23d2d8a243ed6ff6613becdEric Anholt}
7640bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
765de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue *
766959a9ecdd8fbc3375e4149f2b44d253622ff12eeEric Anholtget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
767de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{
768d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8Kenneth Graunke   void *ctx = ralloc_parent(lvalue);
769de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_variable *var;
770de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
7717e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
7727e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick			      ir_var_temporary);
77343b5b03d67ce890e867c81d4a5cfc4871d711d43Eric Anholt   instructions->push_tail(var);
774de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->mode = ir_var_auto;
775de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
7761660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
7771660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  lvalue, NULL));
778de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
7791660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
780de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt}
781de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
782de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
783fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
7840044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions,
78518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	      struct _mesa_glsl_parse_state *state)
78618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{
78718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) instructions;
78818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) state;
78918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
79018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   return NULL;
79118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
79218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
793ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholtstatic ir_rvalue *
794ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholtdo_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
795ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt{
796ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   int join_op;
7976d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick   ir_rvalue *cmp = NULL;
798ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
799ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   if (operation == ir_binop_all_equal)
800ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      join_op = ir_binop_logic_and;
801ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   else
802ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      join_op = ir_binop_logic_or;
803ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
804ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   switch (op0->type->base_type) {
805ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_FLOAT:
806ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_UINT:
807ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_INT:
808ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_BOOL:
809ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      return new(mem_ctx) ir_expression(operation, op0, op1);
810ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
811ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_ARRAY: {
812ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      for (unsigned int i = 0; i < op0->type->length; i++) {
813ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 ir_rvalue *e0, *e1, *result;
814ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
815ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
816ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt						new(mem_ctx) ir_constant(i));
817ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
818ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt						new(mem_ctx) ir_constant(i));
819ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 result = do_comparison(mem_ctx, operation, e0, e1);
820ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
8216d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	 if (cmp) {
8226d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	    cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
823ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 } else {
8246d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	    cmp = result;
825ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 }
826ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      }
827b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt
828b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt      mark_whole_array_access(op0);
829b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt      mark_whole_array_access(op1);
8306d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      break;
831ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   }
832ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
833ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_STRUCT: {
834ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      for (unsigned int i = 0; i < op0->type->length; i++) {
835ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 ir_rvalue *e0, *e1, *result;
836ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 const char *field_name = op0->type->fields.structure[i].name;
837ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
838ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
839ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt						 field_name);
840ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
841ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt						 field_name);
842ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 result = do_comparison(mem_ctx, operation, e0, e1);
843ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
8446d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	 if (cmp) {
8456d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	    cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
846ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 } else {
8476d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	    cmp = result;
848ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 }
849ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      }
8506d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      break;
851ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   }
852ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
853ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_ERROR:
854ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_VOID:
855ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_SAMPLER:
856ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      /* I assume a comparison of a struct containing a sampler just
857ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt       * ignores the sampler present in the type.
858ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt       */
8596d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      break;
8606d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick
8616d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick   default:
8626d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      assert(!"Should not get here.");
8636d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      break;
864ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   }
865d56c97413ee65e40e3544b89ffca450df9ba1c06Eric Anholt
8666d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick   if (cmp == NULL)
8676d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      cmp = new(mem_ctx) ir_constant(true);
8686d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick
8696d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick   return cmp;
870ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt}
87118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
87201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt/* For logical operations, we want to ensure that the operands are
87301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt * scalar booleans.  If it isn't, emit an error and return a constant
87401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt * boolean to avoid triggering cascading error messages.
87501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt */
87601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholtir_rvalue *
87701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholtget_scalar_boolean_operand(exec_list *instructions,
87801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   struct _mesa_glsl_parse_state *state,
87901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   ast_expression *parent_expr,
88001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   int operand,
88101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   const char *operand_name,
88201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   bool *error_emitted)
88301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt{
88401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   ast_expression *expr = parent_expr->subexpressions[operand];
88501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   void *ctx = state;
88601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   ir_rvalue *val = expr->hir(instructions, state);
88701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt
88801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   if (val->type->is_boolean() && val->type->is_scalar())
88901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      return val;
89001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt
89101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   if (!*error_emitted) {
89201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      YYLTYPE loc = expr->get_location();
89301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
89401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt		       operand_name,
89501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt		       parent_expr->operator_string(parent_expr->oper));
89601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      *error_emitted = true;
89701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   }
89801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt
89901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   return new(ctx) ir_constant(true);
90001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt}
90101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt
90293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry/**
90393b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry * If name refers to a builtin array whose maximum allowed size is less than
90493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry * size, report an error and return true.  Otherwise return false.
90593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry */
90693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berrystatic bool
90793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berrycheck_builtin_array_max_size(const char *name, unsigned size,
90893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                             YYLTYPE loc, struct _mesa_glsl_parse_state *state)
90993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry{
91093b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry   if ((strcmp("gl_TexCoord", name) == 0)
91193b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       && (size > state->Const.MaxTextureCoords)) {
91293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
91393b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       *
91493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       *     "The size [of gl_TexCoord] can be at most
91593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       *     gl_MaxTextureCoords."
91693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       */
91793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
91893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                       "be larger than gl_MaxTextureCoords (%u)\n",
91993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                       state->Const.MaxTextureCoords);
92093b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      return true;
92137bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry   } else if (strcmp("gl_ClipDistance", name) == 0
92237bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry              && size > state->Const.MaxClipPlanes) {
92337bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry      /* From section 7.1 (Vertex Shader Special Variables) of the
92437bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       * GLSL 1.30 spec:
92537bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *
92637bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   "The gl_ClipDistance array is predeclared as unsized and
92737bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   must be sized by the shader either redeclaring it with a
92837bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   size or indexing it only with integral constant
92937bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   expressions. ... The size can be at most
93037bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   gl_MaxClipDistances."
93137bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       */
93237bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry      _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
93337bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry                       "be larger than gl_MaxClipDistances (%u)\n",
93437bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry                       state->Const.MaxClipPlanes);
93537bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry      return true;
93693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry   }
93793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry   return false;
93893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry}
93993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry
9409abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry/**
9419abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * Create the constant 1, of a which is appropriate for incrementing and
9429abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * decrementing values of the given GLSL type.  For example, if type is vec4,
9439abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * this creates a constant value of 1.0 having type float.
9449abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry *
9459abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * If the given type is invalid for increment and decrement operators, return
9469abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * a floating point 1--the error will be detected later.
9479abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry */
9489abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berrystatic ir_rvalue *
9499abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berryconstant_one_for_inc_dec(void *ctx, const glsl_type *type)
9509abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry{
9519abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   switch (type->base_type) {
9529abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   case GLSL_TYPE_UINT:
9539abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      return new(ctx) ir_constant((unsigned) 1);
9549abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   case GLSL_TYPE_INT:
9559abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      return new(ctx) ir_constant(1);
9569abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   default:
9579abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   case GLSL_TYPE_FLOAT:
9589abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      return new(ctx) ir_constant(1.0f);
9599abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   }
9609abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry}
9619abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry
962fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
9630044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions,
96418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick		    struct _mesa_glsl_parse_state *state)
965a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
966953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
967a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   static const int operations[AST_NUM_OPERATORS] = {
968a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_assign doesn't convert to ir_expression. */
969a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_plus doesn't convert to ir_expression. */
970a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_neg,
971a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,
972a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,
973a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,
974a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,
975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,
976a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,
977a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,
978a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_less,
979a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_greater,
980a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lequal,
981a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_gequal,
9824dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri      ir_binop_all_equal,
9834dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri      ir_binop_any_nequal,
984a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and,
985a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor,
986a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,
987a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_bit_not,
988a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_and,
989a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_xor,
990a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_or,
991a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_logic_not,
992a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
993a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Note: The following block of expression types actually convert
994a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * to multiple IR instructions.
995a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
996a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,     /* ast_mul_assign */
997a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,     /* ast_div_assign */
998a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,     /* ast_mod_assign */
999a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,     /* ast_add_assign */
1000a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,     /* ast_sub_assign */
1001a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,  /* ast_ls_assign */
1002a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,  /* ast_rs_assign */
1003a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and, /* ast_and_assign */
1004a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor, /* ast_xor_assign */
1005a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,  /* ast_or_assign */
1006a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1007a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_conditional doesn't convert to ir_expression. */
1008de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_pre_inc. */
1009de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_pre_dec. */
1010de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_post_inc. */
1011de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_post_dec. */
1012a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_field_selection doesn't conv to ir_expression. */
1013a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_array_index doesn't convert to ir_expression. */
1014a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_function_call doesn't conv to ir_expression. */
1015a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_identifier doesn't convert to ir_expression. */
1016a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_int_constant doesn't convert to ir_expression. */
1017a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
1018a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_float_constant doesn't conv to ir_expression. */
1019a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
1020a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_sequence doesn't convert to ir_expression. */
1021a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   };
1022fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *result = NULL;
10231c325af4d6b907e0a47ab7f868a2a78f054f153fAras Pranckevicius   ir_rvalue *op[3];
102408ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke   const struct glsl_type *type; /* a temporary variable for switch cases */
1025a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   bool error_emitted = false;
1026a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc;
1027a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
102818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   loc = this->get_location();
1029a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
103018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   switch (this->oper) {
10316652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   case ast_assign: {
103218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
103318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1034a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1035e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      result = do_assignment(instructions, state,
1036e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
1037e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     op[0], op[1], false,
103810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
103910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = result->type->is_error();
1040a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
10416652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   }
1042a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1043a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_plus:
104418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
1045a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1046c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1047c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth
1048c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      error_emitted = type->is_error();
1049a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1050a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = op[0];
1051a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1052a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1053a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_neg:
105418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
1055a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
105665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1057a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
105865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
1059a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10601660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
10611660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
1062a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1063a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1064a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add:
1065a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub:
1066a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul:
1067a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div:
106818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
106918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1070a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1071bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
107218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul),
1073a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
1074a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      error_emitted = type->is_error();
1075a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10761660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
10771660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
1078a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1079a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1080a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mod:
108118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
108218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1083a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
108465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
1085a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
108618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(operations[this->oper] == ir_binop_mod);
1087a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10881660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
10891660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
109065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
1091a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1092a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1093a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lshift:
1094a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rshift:
10955c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       if (state->language_version < 130) {
10965c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace          _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30",
10975c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace              operator_string(this->oper));
10985c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace          error_emitted = true;
10995c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       }
11005c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace
11015c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       op[0] = this->subexpressions[0]->hir(instructions, state);
11025c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       op[1] = this->subexpressions[1]->hir(instructions, state);
1103c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace       type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1104c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                                &loc);
11055c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       result = new(ctx) ir_expression(operations[this->oper], type,
11065c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace                                       op[0], op[1]);
11075c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
11085c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       break;
1109a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1110a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_less:
1111a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_greater:
1112a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lequal:
1113a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_gequal:
111418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
111518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1116a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
111765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = relational_result_type(op[0], op[1], state, & loc);
1118a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1119a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The relational operators must either generate an error or result
1120a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
1121a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1122a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      assert(type->is_error()
1123a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     || ((type->base_type == GLSL_TYPE_BOOL)
1124cb36f8aaeeb09660843316270a781948f773d90bIan Romanick		 && type->is_scalar()));
1125a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
11261660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
11271660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
112865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
1129a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1130a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1131a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_nequal:
1132a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_equal:
11336e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
11346e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
11356e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
11366e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
11376e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *
11386e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    "The equality operators equal (==), and not equal (!=)
11396e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    operate on all types. They result in a scalar Boolean. If
11406e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    the operand types do not match, then there must be a
11416e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    conversion from Section 4.1.10 "Implicit Conversions"
11426e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    applied to one operand that can make them match, in which
11436e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    case this conversion is done."
11446e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       */
1145bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1146bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[1]->type, op[0], state))
1147212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick	  || (op[0]->type != op[1]->type)) {
11486e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
11496e659caaa946339a2de3890a8bed091ccb65102aIan Romanick			  "type", (this->oper == ast_equal) ? "==" : "!=");
11506e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 error_emitted = true;
1151a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick      } else if ((state->language_version <= 110)
1152a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick		 && (op[0]->type->is_array() || op[1]->type->is_array())) {
1153a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
1154a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick			  "GLSL 1.10");
1155a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 error_emitted = true;
11566e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      }
11576e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
1158175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt      if (error_emitted) {
1159175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt	 result = new(ctx) ir_constant(false);
1160175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt      } else {
1161175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt	 result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
1162175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt	 assert(result->type == glsl_type::bool_type);
1163175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt      }
1164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1165a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_and:
1167a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_xor:
1168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_or:
11691eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[0] = this->subexpressions[0]->hir(instructions, state);
11701eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[1] = this->subexpressions[1]->hir(instructions, state);
1171cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace      type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1172cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                                   state, &loc);
11731eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      result = new(ctx) ir_expression(operations[this->oper], type,
11741eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke				      op[0], op[1]);
11751eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
11761eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      break;
11771eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
1178a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_not:
11791eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[0] = this->subexpressions[0]->hir(instructions, state);
11801eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
11811eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (state->language_version < 130) {
11821eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
11831eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
11841eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
11851eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
11861eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (!op[0]->type->is_integer()) {
11871eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
11881eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
11891eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
11901eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
119139464489510270bbe472d11f7614c04ce1b6ae33Ian Romanick      type = error_emitted ? glsl_type::error_type : op[0]->type;
11921eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
1193a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1194a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
11954950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_and: {
11967ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt      exec_list rhs_instructions;
119701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
119801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt					 "LHS", &error_emitted);
11997ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt      op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
12007ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt					 "RHS", &error_emitted);
1201b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
1202ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt      if (rhs_instructions.is_empty()) {
1203ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt	 result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
1204ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt	 type = result->type;
120544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
120681d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
12077e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "and_tmp",
12087e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
120981d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(tmp);
121081d664f099a5fd5fac777480532fb4307d591451Ian Romanick
12111660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
121244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 instructions->push_tail(stmt);
12134950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12147ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt	 stmt->then_instructions.append_list(&rhs_instructions);
12151660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
121644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
12171660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
121844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
12194950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12201660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
122144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
12221660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
122344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
12244950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12251660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
122644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
122744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
12284950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
12294950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
12304950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12314950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_or: {
12329e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt      exec_list rhs_instructions;
123301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
123401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt					 "LHS", &error_emitted);
12359e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt      op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
12369e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt					 "RHS", &error_emitted);
12374950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
1238ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt      if (rhs_instructions.is_empty()) {
1239ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt	 result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
1240ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt	 type = result->type;
124144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
1242dfd30ca6a95a7d95835dad78ffe1fba4d1f4ef69Kenneth Graunke	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
12437e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "or_tmp",
12447e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
12450b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
12464950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
124781d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_if *const stmt = new(ctx) ir_if(op[0]);
124881d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(stmt);
124981d664f099a5fd5fac777480532fb4307d591451Ian Romanick
12501660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
125144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
12521660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
125344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
12544950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12559e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt	 stmt->else_instructions.append_list(&rhs_instructions);
12561660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
125744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
12581660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[1], NULL);
125944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
12604950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12611660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
126244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
126344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
12644950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
12654950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
12664950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12674950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_xor:
1268756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt      /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1269756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       *
1270756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       *    "The logical binary operators and (&&), or ( | | ), and
1271756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       *     exclusive or (^^). They operate only on two Boolean
1272756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       *     expressions and result in a Boolean expression."
1273756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       */
1274756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
1275756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt					 &error_emitted);
1276756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt      op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
1277756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt					 &error_emitted);
12784950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12791660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
12801660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
1281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1283a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt   case ast_logic_not:
128401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
128501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt					 "operand", &error_emitted);
1286a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
12871660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
12881660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
1289a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      break;
1290a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
1291a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul_assign:
1292a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div_assign:
1293a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add_assign:
1294a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub_assign: {
129518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
129618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1298bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
129918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul_assign),
1300a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
1301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
13021660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
13031660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						   op[0], op[1]);
1304a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
13053e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
1306e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
130785caea29c18fad89050ac366c558afef568dcb3fIan Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
130810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
130910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = (op[0]->type->is_error());
1310a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
1312a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * explicitly test for this because none of the binary expression
1313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * operators allow array operands either.
1314a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1316a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1317a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1318a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
131948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   case ast_mod_assign: {
132048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
132148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
132248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
132365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
132448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
132548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      assert(operations[this->oper] == ir_binop_mod);
132648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
1327768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
13281660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
13291660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
133048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
13313e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
1332e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
133385caea29c18fad89050ac366c558afef568dcb3fIan Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
133448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt			     this->subexpressions[0]->get_location());
133565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
133648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      break;
133748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   }
1338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1339a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_ls_assign:
1340338ed6ec297d76746b6466c26c307722af965e60Chad Versace   case ast_rs_assign: {
1341338ed6ec297d76746b6466c26c307722af965e60Chad Versace      op[0] = this->subexpressions[0]->hir(instructions, state);
1342338ed6ec297d76746b6466c26c307722af965e60Chad Versace      op[1] = this->subexpressions[1]->hir(instructions, state);
1343338ed6ec297d76746b6466c26c307722af965e60Chad Versace      type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1344338ed6ec297d76746b6466c26c307722af965e60Chad Versace                               &loc);
1345338ed6ec297d76746b6466c26c307722af965e60Chad Versace      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1346338ed6ec297d76746b6466c26c307722af965e60Chad Versace                                                   type, op[0], op[1]);
1347e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      result = do_assignment(instructions, state,
1348e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
1349e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
1350338ed6ec297d76746b6466c26c307722af965e60Chad Versace                             this->subexpressions[0]->get_location());
1351338ed6ec297d76746b6466c26c307722af965e60Chad Versace      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1352251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1353338ed6ec297d76746b6466c26c307722af965e60Chad Versace   }
1354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_and_assign:
1356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_xor_assign:
1357d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace   case ast_or_assign: {
1358d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      op[0] = this->subexpressions[0]->hir(instructions, state);
1359d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      op[1] = this->subexpressions[1]->hir(instructions, state);
1360d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1361d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                                   state, &loc);
1362d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1363d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                                                   type, op[0], op[1]);
1364e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      result = do_assignment(instructions, state,
1365e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
1366e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
1367d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                             this->subexpressions[0]->get_location());
1368d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1369251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1370d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace   }
1371a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
137296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   case ast_conditional: {
137396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
137496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
137596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    "The ternary selection operator (?:). It operates on three
137696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
137796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    first expression, which must result in a scalar Boolean."
137896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
137901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
138001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt					 "condition", &error_emitted);
138196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
138296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* The :? operator is implemented by generating an anonymous temporary
138396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * followed by an if-statement.  The last instruction in each branch of
138496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * the if-statement assigns a value to the anonymous temporary.  This
138596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * temporary is the r-value of the expression.
138696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
13870ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list then_instructions;
13880ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list else_instructions;
138996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
13900ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[1] = this->subexpressions[1]->hir(&then_instructions, state);
13910ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[2] = this->subexpressions[2]->hir(&else_instructions, state);
139296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
139396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
139496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
139596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     "The second and third expressions can be any type, as
139696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     long their types match, or there is a conversion in
139796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     Section 4.1.10 "Implicit Conversions" that can be applied
139896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     to one of the expressions to make their types match. This
139996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     resulting matching type is the type of the entire
140096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     expression."
140196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
1402bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1403bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[2]->type, op[1], state))
1404db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	  || (op[1]->type != op[2]->type)) {
140596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[1]->get_location();
140696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
140796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
140896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick			  "operator must have matching types.");
140996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
14100ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = glsl_type::error_type;
1411db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick      } else {
14120ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = op[1]->type;
141396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
141496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
1415f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1416f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *
1417f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *    "The second and third expressions must be the same type, but can
1418f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *    be of any type other than an array."
1419f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       */
1420f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      if ((state->language_version <= 110) && type->is_array()) {
1421f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1422f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick			  "operator must not be arrays.");
1423f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick	 error_emitted = true;
1424f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      }
1425f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick
14267825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *cond_val = op[0]->constant_expression_value();
14277825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *then_val = op[1]->constant_expression_value();
14287825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *else_val = op[2]->constant_expression_value();
14297825d3d15710fdfcfc503754862963aac8065480Ian Romanick
14307825d3d15710fdfcfc503754862963aac8065480Ian Romanick      if (then_instructions.is_empty()
14317825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && else_instructions.is_empty()
14327825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
14337825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 result = (cond_val->value.b[0]) ? then_val : else_val;
14347825d3d15710fdfcfc503754862963aac8065480Ian Romanick      } else {
14357e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	 ir_variable *const tmp =
14367e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	    new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
14370b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
14380ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
14391660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
14407825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 instructions->push_tail(stmt);
14410ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
14427825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 then_instructions.move_nodes_to(& stmt->then_instructions);
14431660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref =
14441660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
14457825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const then_assign =
14461660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
14477825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->then_instructions.push_tail(then_assign);
14480ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
14497825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 else_instructions.move_nodes_to(& stmt->else_instructions);
14501660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref =
14511660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
14527825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const else_assign =
14531660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[2], NULL);
14547825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->else_instructions.push_tail(else_assign);
14550ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
14561660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
14577825d3d15710fdfcfc503754862963aac8065480Ian Romanick      }
1458251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
145996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   }
1460a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1461a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_pre_inc:
146276ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   case ast_pre_dec: {
1463fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick      this->non_lvalue_description = (this->oper == ast_pre_inc)
1464fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick	 ? "pre-increment operation" : "pre-decrement operation";
1465fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick
146676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
14679abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
146876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1469a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
147076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1471768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
14721660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
14731660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
147476ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
14753e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
1476e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
147785caea29c18fad89050ac366c558afef568dcb3fIan Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
147876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt			     this->subexpressions[0]->get_location());
147976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      error_emitted = op[0]->type->is_error();
148076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      break;
148176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   }
1482a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1483a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_post_inc:
1484de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   case ast_post_dec: {
1485fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick      this->non_lvalue_description = (this->oper == ast_post_inc)
1486fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick	 ? "post-increment operation" : "post-decrement operation";
1487de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
14889abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1489de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1490de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1491de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1492a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1493de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1494768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
14951660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
14961660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
1497de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1498de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      /* Get a temporary of a copy of the lvalue before it's modified.
1499de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       * This may get thrown away later.
1500de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       */
15018273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1502de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
15033e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      (void)do_assignment(instructions, state,
1504e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			  this->subexpressions[0]->non_lvalue_description,
150585caea29c18fad89050ac366c558afef568dcb3fIan Romanick			  op[0]->clone(ctx, NULL), temp_rhs, false,
1506de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			  this->subexpressions[0]->get_location());
1507de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1508de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error();
1509a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1510de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   }
1511a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1512a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_field_selection:
151318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1514a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1515a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
151627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   case ast_array_index: {
151727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      YYLTYPE index_loc = subexpressions[1]->get_location();
151827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
151927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[0] = subexpressions[0]->hir(instructions, state);
152027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[1] = subexpressions[1]->hir(instructions, state);
152127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
152227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
152327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1524a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick      ir_rvalue *const array = op[0];
1525b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
15261660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_array(op[0], op[1]);
1527b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1528b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      /* Do not use op[0] after this point.  Use array.
1529b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       */
1530b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      op[0] = NULL;
1531b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
153227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
153327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
153427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 break;
153527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
153663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick      if (!array->type->is_array()
153763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_matrix()
153863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_vector()) {
153927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
154063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "cannot dereference non-array / non-matrix / "
154163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "non-vector");
154227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
154327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
154427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
154527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (!op[1]->type->is_integer()) {
154627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
154727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be integer type");
154827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
154927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      } else if (!op[1]->type->is_scalar()) {
155027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
155127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be scalar");
155227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
155327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
155427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
155527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      /* If the array index is a constant expression and the array has a
155627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size, ensure that the access is in-bounds.  If the array
155727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * index is not a constant expression, ensure that the array has a
155827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size.
155927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       */
156027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      ir_constant *const const_index = op[1]->constant_expression_value();
156127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (const_index != NULL) {
156227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 const int idx = const_index->value.i[0];
156363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 const char *type_name;
156463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 unsigned bound = 0;
156563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick
156663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
156763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "matrix";
156863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
156963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "vector";
157063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
157163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "array";
157263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
157327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
157427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
157527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *
157627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    "It is illegal to declare an array with a size, and then
157727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    later (in the same shader) index the same array with an
157827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    integral constant expression greater than or equal to the
157927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    declared size. It is also illegal to index an array with a
158027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    negative constant expression."
158127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  */
158263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
158363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->row_type()->vector_elements <= idx) {
158463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->row_type()->vector_elements;
158563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
158663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
158763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->vector_elements <= idx) {
158863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->vector_elements;
158963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
159063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
159163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((array->type->array_size() > 0)
159263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick		&& (array->type->array_size() <= idx)) {
159363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->array_size();
159463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
159527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
159627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
159763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (bound > 0) {
159863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be < %u",
159963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name, bound);
160063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    error_emitted = true;
160163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (idx < 0) {
160263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be >= 0",
160363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name);
160427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    error_emitted = true;
160527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
1606b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
160763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_array()) {
1608a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    /* If the array is a variable dereference, it dereferences the
1609a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * whole array, by definition.  Use this to get the variable.
1610a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     *
1611a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: Should some methods for getting / setting / testing
1612a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: array access limits be added to ir_dereference?
1613a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     */
1614a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    ir_variable *const v = array->whole_variable_referenced();
161593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry	    if ((v != NULL) && (unsigned(idx) > v->max_array_access)) {
161663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       v->max_array_access = idx;
161793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry
161893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry               /* Check whether this access will, as a side effect, implicitly
161993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                * cause the size of a built-in array to be too large.
162093b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                */
162193b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry               if (check_builtin_array_max_size(v->name, idx+1, loc, state))
162293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                  error_emitted = true;
162393b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry            }
162463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
16252b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke      } else if (array->type->array_size() == 0) {
16262b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
1627a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt      } else {
1628a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 if (array->type->is_array()) {
16295226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	    /* whole_variable_referenced can return NULL if the array is a
16305226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * member of a structure.  In this case it is safe to not update
16315226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * the max_array_access field because it is never used for fields
16325226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * of structures.
16335226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     */
1634a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	    ir_variable *v = array->whole_variable_referenced();
16355226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	    if (v != NULL)
16360d9d036004f135c38990c60f46074b70cff6e663Ian Romanick	       v->max_array_access = array->type->array_size() - 1;
1637a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 }
163827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
163927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1640e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick      /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
1641e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       *
1642f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace       *    "Samplers aggregated into arrays within a shader (using square
1643f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace       *    brackets [ ]) can only be indexed with integral constant
1644f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace       *    expressions [...]."
1645e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       *
1646e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * This restriction was added in GLSL 1.30.  Shaders using earlier version
1647e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * of the language should not be rejected by the compiler front-end for
1648e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * using this construct.  This allows useful things such as using a loop
1649e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * counter as the index to an array of samplers.  If the loop in unrolled,
1650e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * the code should compile correctly.  Instead, emit a warning.
1651f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace       */
1652f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace      if (array->type->is_array() &&
1653f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace          array->type->element_type()->is_sampler() &&
1654f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace          const_index == NULL) {
1655f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace
1656e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	 if (state->language_version == 100) {
1657e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	    _mesa_glsl_warning(&loc, state,
1658e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "sampler arrays indexed with non-constant "
1659e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "expressions is optional in GLSL ES 1.00");
1660e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	 } else if (state->language_version < 130) {
1661e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	    _mesa_glsl_warning(&loc, state,
1662e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "sampler arrays indexed with non-constant "
1663e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "expressions is forbidden in GLSL 1.30 and "
1664e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "later");
1665e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	 } else {
1666e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	    _mesa_glsl_error(&loc, state,
1667e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			     "sampler arrays indexed with non-constant "
1668e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			     "expressions is forbidden in GLSL 1.30 and "
1669e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			     "later");
1670e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	    error_emitted = true;
1671e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	 }
1672f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace      }
1673f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace
167427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
167527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 result->type = glsl_type::error_type;
167627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1677a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
167827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   }
1679a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1680a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_function_call:
16817cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      /* Should *NEVER* get here.  ast_function_call should always be handled
16827cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick       * by ast_function_expression::hir.
1683a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
16847cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      assert(0);
1685a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1686a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1687a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_identifier: {
1688a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* ast_identifier can appear several places in a full abstract syntax
1689a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * tree.  This particular use must be at location specified in the grammar
1690a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * as 'variable_identifier'.
1691a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
16928bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      ir_variable *var =
16938bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick	 state->symbols->get_variable(this->primary_expression.identifier);
1694a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
16951660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_variable(var);
1696a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1697a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (var != NULL) {
1698bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	 var->used = true;
1699a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
170071d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
170118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->primary_expression.identifier);
1702a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1703a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 error_emitted = true;
1704a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1705a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1706a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1707a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1708a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_int_constant:
17091660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.int_constant);
1710a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1711a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1712a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_uint_constant:
17131660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1715a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1716a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_float_constant:
17171660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.float_constant);
1718a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1719a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1720a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bool_constant:
17211660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1722a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1724a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sequence: {
1725a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* It should not be possible to generate a sequence in the AST without
1726a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * any expressions in it.
1727a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1728304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      assert(!this->expressions.is_empty());
1729a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1730a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The r-value of a sequence is the last expression in the sequence.  If
1731a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the other expressions in the sequence do not have side-effects (and
1732a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * therefore add instructions to the instruction list), they get dropped
1733a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * on the floor.
1734a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
17353d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick      exec_node *previous_tail_pred = NULL;
17363d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick      YYLTYPE previous_operand_loc = loc;
17373d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick
17383d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick      foreach_list_typed (ast_node, ast, link, &this->expressions) {
17393d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 /* If one of the operands of comma operator does not generate any
17403d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * code, we want to emit a warning.  At each pass through the loop
17413d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * previous_tail_pred will point to the last instruction in the
17423d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * stream *before* processing the previous operand.  Naturally,
17433d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * instructions->tail_pred will point to the last instruction in the
17443d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * stream *after* processing the previous operand.  If the two
17453d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * pointers match, then the previous operand had no effect.
17463d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  *
17473d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * The warning behavior here differs slightly from GCC.  GCC will
17483d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * only emit a warning if none of the left-hand operands have an
17493d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * effect.  However, it will emit a warning for each.  I believe that
17503d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * there are some cases in C (especially with GCC extensions) where
17513d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * it is useful to have an intermediate step in a sequence have no
17523d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * effect, but I don't think these cases exist in GLSL.  Either way,
17533d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * it would be a giant hassle to replicate that behavior.
17543d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  */
17553d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 if (previous_tail_pred == instructions->tail_pred) {
17563d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	    _mesa_glsl_warning(&previous_operand_loc, state,
17573d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick			       "left-hand operand of comma expression has "
17583d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick			       "no effect");
17593d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 }
17603d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick
17613d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 /* tail_pred is directly accessed instead of using the get_tail()
17623d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * method for performance reasons.  get_tail() has extra code to
17633d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * return NULL when the list is empty.  We don't care about that
17643d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * here, so using tail_pred directly is fine.
17653d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  */
17663d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 previous_tail_pred = instructions->tail_pred;
17673d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 previous_operand_loc = ast->get_location();
17683d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick
1769304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick	 result = ast->hir(instructions, state);
17703d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick      }
1771a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1772a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Any errors should have already been emitted in the loop above.
1773a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1774a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      error_emitted = true;
1775a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1776a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1777a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
177808ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke   type = NULL; /* use result->type, not type. */
177908ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke   assert(result != NULL);
1780a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
178108ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke   if (result->type->is_error() && !error_emitted)
178271d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick      _mesa_glsl_error(& loc, state, "type mismatch");
1783a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1784a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
1785a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1786a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1787a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1788fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
17890044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions,
179018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1791a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1792a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* It is possible to have expression statements that don't have an
1793a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * expression.  This is the solitary semicolon:
1794a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1795a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for (i = 0; i < 5; i++)
1796a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     ;
1797a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1798a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * In this case the expression will be NULL.  Test for NULL and don't do
1799a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * anything in that case.
1800a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
180118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (expression != NULL)
180218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      expression->hir(instructions, state);
1803a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1804a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Statements do not have r-values.
1805a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1806a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1807a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1808a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1809a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1810fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
18110044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions,
181218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			    struct _mesa_glsl_parse_state *state)
1813a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
181418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
18158bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->push_scope();
1816a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18172b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, &this->statements)
1818304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
1819a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
182018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
18218bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->pop_scope();
1822a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1823a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Compound statements do not have r-values.
1824a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1825a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1826a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1827a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1828a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
182928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type *
1830d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunkeprocess_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
183128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick		   struct _mesa_glsl_parse_state *state)
183228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{
183328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   unsigned length = 0;
183428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1835a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick   /* From page 19 (page 25) of the GLSL 1.20 spec:
1836a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick    *
1837a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick    *     "Only one-dimensional arrays may be declared."
1838a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick    */
1839a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick   if (base->is_array()) {
1840a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick      _mesa_glsl_error(loc, state,
1841a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick		       "invalid array of `%s' (only one-dimensional arrays "
1842a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick		       "may be declared)",
1843a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick		       base->name);
1844a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick      return glsl_type::error_type;
1845a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick   }
184628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
184728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   if (array_size != NULL) {
184828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      exec_list dummy_instructions;
184928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
185028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      YYLTYPE loc = array_size->get_location();
185128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
185228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      if (ir != NULL) {
185328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 if (!ir->type->is_integer()) {
185428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be integer type");
185528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else if (!ir->type->is_scalar()) {
185628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be scalar type");
185728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else {
185828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    ir_constant *const size = ir->constant_expression_value();
185928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
186028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    if (size == NULL) {
186128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be a "
186228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick				"constant valued expression");
186328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else if (size->value.i[0] <= 0) {
186428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > 0");
186528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else {
186628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       assert(size->type == ir->type);
186728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       length = size->value.u[0];
1868d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry
1869d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry               /* If the array size is const (and we've verified that
1870d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                * it is) then no instructions should have been emitted
1871d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                * when we converted it to HIR.  If they were emitted,
1872d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                * then either the array size isn't const after all, or
1873d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                * we are emitting unnecessary instructions.
1874d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                */
1875d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry               assert(dummy_instructions.is_empty());
187628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    }
187728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 }
187828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
1879d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke   } else if (state->es_shader) {
1880d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke      /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
1881d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke       * array declarations have been removed from the language.
1882d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke       */
1883d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke      _mesa_glsl_error(loc, state, "unsized array declarations are not "
1884d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke		       "allowed in GLSL ES 1.00.");
188528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   }
188628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1887f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick   return glsl_type::get_array_instance(base, length);
188828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick}
188928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
189028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1891d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type *
1892d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name,
1893d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick			      struct _mesa_glsl_parse_state *state) const
1894a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1895d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   const struct glsl_type *type;
1896a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1897ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke   type = state->symbols->get_type(this->type_name);
1898ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke   *name = this->type_name;
1899a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1900ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke   if (this->is_array) {
1901ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke      YYLTYPE loc = this->get_location();
1902ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke      type = process_array_type(&loc, type, this->array_size, state);
1903a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1904a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1905a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
1906a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1907a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1908a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1909a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1910a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1911768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick				 ir_variable *var,
19122e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 struct _mesa_glsl_parse_state *state,
19132e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 YYLTYPE *loc)
1914a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1915bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick   if (qual->flags.q.invariant) {
1916bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick      if (var->used) {
1917bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	 _mesa_glsl_error(loc, state,
1918bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			  "variable `%s' may not be redeclared "
1919bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			  "`invariant' after being used",
1920bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			  var->name);
1921bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick      } else {
1922bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	 var->invariant = 1;
1923bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick      }
1924bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick   }
1925a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1926e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.constant || qual->flags.q.attribute
1927e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick       || qual->flags.q.uniform
1928e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick       || (qual->flags.q.varying && (state->target == fragment_shader)))
1929a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->read_only = 1;
1930a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1931e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.centroid)
1932a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->centroid = 1;
1933a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1934e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.attribute && state->target != vertex_shader) {
19352e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      var->type = glsl_type::error_type;
19362e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      _mesa_glsl_error(loc, state,
19372e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "`attribute' variables may not be declared in the "
1938ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       "%s shader",
1939ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       _mesa_glsl_shader_target_name(state->target));
19402e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   }
19412e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
194290b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
194390b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *
194490b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     "The varying qualifier can be used only with the data types
194590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
194690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     these."
194790b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    */
1948e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.varying) {
19490ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      const glsl_type *non_array_type;
19500ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
19510ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (var->type && var->type->is_array())
19520ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type->fields.array;
19530ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      else
19540ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type;
19550ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
19560ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
19570ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 var->type = glsl_type::error_type;
19580ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 _mesa_glsl_error(loc, state,
19590ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt			  "varying variables must be of base type float");
19600ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      }
196190b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   }
196290b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt
19637e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   /* If there is no qualifier that changes the mode of the variable, leave
19647e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    * the setting alone.
19657e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    */
1966e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.in && qual->flags.q.out)
1967a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_inout;
1968e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.attribute || qual->flags.q.in
1969e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	    || (qual->flags.q.varying && (state->target == fragment_shader)))
1970a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_in;
1971e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.out
1972e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	    || (qual->flags.q.varying && (state->target == vertex_shader)))
1973a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_out;
1974e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.uniform)
1975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_uniform;
1976a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
197786b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick   if (state->all_invariant && (state->current_function == NULL)) {
197886b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      switch (state->target) {
197986b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      case vertex_shader:
198086b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 if (var->mode == ir_var_out)
198186b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	    var->invariant = true;
198286b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 break;
198386b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      case geometry_shader:
198486b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 if ((var->mode == ir_var_in) || (var->mode == ir_var_out))
198586b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	    var->invariant = true;
198686b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 break;
198786b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      case fragment_shader:
198886b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 if (var->mode == ir_var_in)
198986b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	    var->invariant = true;
199086b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 break;
199186b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      }
199286b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick   }
199386b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick
1994e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.flat)
1995cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry      var->interpolation = INTERP_QUALIFIER_FLAT;
1996e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.noperspective)
1997cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry      var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
1998c488150dea083a9677429b4185c6b20d7facd52bPaul Berry   else if (qual->flags.q.smooth)
1999cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry      var->interpolation = INTERP_QUALIFIER_SMOOTH;
2000c488150dea083a9677429b4185c6b20d7facd52bPaul Berry   else
2001c488150dea083a9677429b4185c6b20d7facd52bPaul Berry      var->interpolation = INTERP_QUALIFIER_NONE;
20029d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick
2003916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt   if (var->interpolation != INTERP_QUALIFIER_NONE &&
2004916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt       !(state->target == vertex_shader && var->mode == ir_var_out) &&
2005916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt       !(state->target == fragment_shader && var->mode == ir_var_in)) {
2006916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      const char *qual_string = NULL;
2007916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      switch (var->interpolation) {
2008916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      case INTERP_QUALIFIER_FLAT:
2009916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt	 qual_string = "flat";
2010916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt	 break;
2011916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      case INTERP_QUALIFIER_NOPERSPECTIVE:
2012916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt	 qual_string = "noperspective";
2013916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt	 break;
2014916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      case INTERP_QUALIFIER_SMOOTH:
2015916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt	 qual_string = "smooth";
2016916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt	 break;
2017916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      }
2018916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt
2019916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      _mesa_glsl_error(loc, state,
2020916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt		       "interpolation qualifier `%s' can only be applied to "
2021916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt		       "vertex shader outputs and fragment shader inputs.",
2022916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt		       qual_string);
2023916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt
2024916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt   }
2025916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt
2026e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   var->pixel_center_integer = qual->flags.q.pixel_center_integer;
2027e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   var->origin_upper_left = qual->flags.q.origin_upper_left;
2028e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
20298d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick       && (strcmp(var->name, "gl_FragCoord") != 0)) {
2030e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick      const char *const qual_string = (qual->flags.q.origin_upper_left)
20318d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick	 ? "origin_upper_left" : "pixel_center_integer";
20328d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
20338d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick      _mesa_glsl_error(loc, state,
20348d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "layout qualifier `%s' can only be applied to "
20358d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "fragment shader input `gl_FragCoord'",
20368d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       qual_string);
20378d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick   }
20388d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
2039eee68d3631813580a14fa51fda6f0c959279256cIan Romanick   if (qual->flags.q.explicit_location) {
2040eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      const bool global_scope = (state->current_function == NULL);
2041eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      bool fail = false;
2042eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      const char *string = "";
2043eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
2044eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      /* In the vertex shader only shader inputs can be given explicit
2045eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       * locations.
2046eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       *
2047eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       * In the fragment shader only shader outputs can be given explicit
2048eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       * locations.
2049eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       */
2050eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      switch (state->target) {
2051eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      case vertex_shader:
2052eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 if (!global_scope || (var->mode != ir_var_in)) {
2053eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    fail = true;
2054eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    string = "input";
2055eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 }
2056eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 break;
2057eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
2058eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      case geometry_shader:
2059eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 _mesa_glsl_error(loc, state,
2060eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "geometry shader variables cannot be given "
2061eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "explicit locations\n");
2062eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 break;
2063eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
2064eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      case fragment_shader:
2065b078aad8ab22d840456688480a8c27d4664297cePaul Berry	 if (!global_scope || (var->mode != ir_var_out)) {
2066eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    fail = true;
2067eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    string = "output";
2068eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 }
2069eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 break;
2070a75da2c0e85eb6b8279ec895c3f74cc4aefc0257Kenneth Graunke      };
2071eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
2072eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      if (fail) {
2073eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 _mesa_glsl_error(loc, state,
2074eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "only %s shader %s variables can be given an "
2075eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "explicit location\n",
2076eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  _mesa_glsl_shader_target_name(state->target),
2077eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  string);
2078eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      } else {
2079eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 var->explicit_location = true;
208068a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick
208168a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 /* This bit of silliness is needed because invalid explicit locations
208268a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * are supposed to be flagged during linking.  Small negative values
208368a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
208468a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
208568a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * The linker needs to be able to differentiate these cases.  This
208668a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * ensures that negative values stay negative.
208768a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  */
208868a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 if (qual->location >= 0) {
208968a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	    var->location = (state->target == vertex_shader)
209068a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	       ? (qual->location + VERT_ATTRIB_GENERIC0)
209168a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	       : (qual->location + FRAG_RESULT_DATA0);
209268a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 } else {
209368a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	    var->location = qual->location;
209468a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 }
2095eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      }
2096eee68d3631813580a14fa51fda6f0c959279256cIan Romanick   }
2097eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
20984bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   /* Does the declaration use the 'layout' keyword?
20994bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    */
21004bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   const bool uses_layout = qual->flags.q.pixel_center_integer
21014bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      || qual->flags.q.origin_upper_left
21024bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      || qual->flags.q.explicit_location;
21034bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick
21044bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   /* Does the declaration use the deprecated 'attribute' or 'varying'
21054bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * keywords?
21064bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    */
21074bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   const bool uses_deprecated_qualifier = qual->flags.q.attribute
21084bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      || qual->flags.q.varying;
21094bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick
21104bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   /* Is the 'layout' keyword used with parameters that allow relaxed checking.
21114bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
21124bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
21134bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * allowed the layout qualifier to be used with 'varying' and 'attribute'.
21144bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * These extensions and all following extensions that add the 'layout'
21154bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * keyword have been modified to require the use of 'in' or 'out'.
21164bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *
21174bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * The following extension do not allow the deprecated keywords:
21184bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *
21194bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *    GL_AMD_conservative_depth
21206b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák    *    GL_ARB_conservative_depth
21214bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *    GL_ARB_gpu_shader5
21224bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *    GL_ARB_separate_shader_objects
21234bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *    GL_ARB_tesselation_shader
21244bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *    GL_ARB_transform_feedback3
21254bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *    GL_ARB_uniform_buffer_object
21264bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *
21274bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
21284bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * allow layout with the deprecated keywords.
21294bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    */
21304bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   const bool relaxed_layout_qualifier_checking =
21314bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      state->ARB_fragment_coord_conventions_enable;
21324bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick
21334bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   if (uses_layout && uses_deprecated_qualifier) {
21344bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      if (relaxed_layout_qualifier_checking) {
21354bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick	 _mesa_glsl_warning(loc, state,
21364bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick			    "`layout' qualifier may not be used with "
21374bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick			    "`attribute' or `varying'");
21384bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      } else {
21394bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick	 _mesa_glsl_error(loc, state,
21404bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick			  "`layout' qualifier may not be used with "
21414bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick			  "`attribute' or `varying'");
21424bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      }
21434bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   }
21444bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick
2145bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2146bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace    * AMD_conservative_depth.
2147bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace    */
2148bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   int depth_layout_count = qual->flags.q.depth_any
2149bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      + qual->flags.q.depth_greater
2150bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      + qual->flags.q.depth_less
2151bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      + qual->flags.q.depth_unchanged;
2152bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   if (depth_layout_count > 0
21536b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák       && !state->AMD_conservative_depth_enable
21546b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák       && !state->ARB_conservative_depth_enable) {
2155bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace       _mesa_glsl_error(loc, state,
21566b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák                        "extension GL_AMD_conservative_depth or "
21576b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák                        "GL_ARB_conservative_depth must be enabled "
2158bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace			"to use depth layout qualifiers");
2159bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   } else if (depth_layout_count > 0
2160bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace              && strcmp(var->name, "gl_FragDepth") != 0) {
2161bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace       _mesa_glsl_error(loc, state,
2162bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace                        "depth layout qualifiers can be applied only to "
2163bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace                        "gl_FragDepth");
2164bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   } else if (depth_layout_count > 1
2165bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace              && strcmp(var->name, "gl_FragDepth") == 0) {
2166bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      _mesa_glsl_error(loc, state,
2167bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace                       "at most one depth layout qualifier can be applied to "
2168bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace                       "gl_FragDepth");
2169bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   }
2170bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   if (qual->flags.q.depth_any)
2171bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      var->depth_layout = ir_depth_layout_any;
2172bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   else if (qual->flags.q.depth_greater)
2173bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      var->depth_layout = ir_depth_layout_greater;
2174bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   else if (qual->flags.q.depth_less)
2175bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      var->depth_layout = ir_depth_layout_less;
2176bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   else if (qual->flags.q.depth_unchanged)
2177bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace       var->depth_layout = ir_depth_layout_unchanged;
2178bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   else
2179bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace       var->depth_layout = ir_depth_layout_none;
2180a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2181a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
21828e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick/**
21838e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * Get the variable that is being redeclared by this declaration
21848e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick *
21858e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * Semantic checks to verify the validity of the redeclaration are also
21868e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * performed.  If semantic checks fail, compilation error will be emitted via
21878e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
21888e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick *
21898e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * \returns
21908e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * A pointer to an existing variable in the current scope if the declaration
21918e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * is a redeclaration, \c NULL otherwise.
21928e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick */
21938e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanickir_variable *
21948e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanickget_variable_being_redeclared(ir_variable *var, ast_declaration *decl,
21958e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			      struct _mesa_glsl_parse_state *state)
21968e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick{
21978e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   /* Check if this declaration is actually a re-declaration, either to
21988e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    * resize an array or add qualifiers to an existing variable.
21998e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    *
22008e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    * This is allowed for variables in the current scope, or when at
22018e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    * global scope (for built-ins in the implicit outer scope).
22028e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    */
22038e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   ir_variable *earlier = state->symbols->get_variable(decl->identifier);
22048e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   if (earlier == NULL ||
22058e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       (state->current_function != NULL &&
22068e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	!state->symbols->name_declared_this_scope(decl->identifier))) {
22078e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      return NULL;
22088e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   }
22098e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22108e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22118e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   YYLTYPE loc = decl->get_location();
22128e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22138e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
22148e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    *
22158e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    * "It is legal to declare an array without a size and then
22168e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    *  later re-declare the same name as an array of the same
22178e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    *  type and specify a size."
22188e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    */
22198e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   if ((earlier->type->array_size() == 0)
22208e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       && var->type->is_array()
22218e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       && (var->type->element_type() == earlier->type->element_type())) {
22228e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* FINISHME: This doesn't match the qualifiers on the two
22238e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * FINISHME: declarations.  It's not 100% clear whether this is
22248e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * FINISHME: required or not.
22258e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       */
22268e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22278e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      const unsigned size = unsigned(var->type->array_size());
222893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      check_builtin_array_max_size(var->name, size, loc, state);
222993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      if ((size > 0) && (size <= earlier->max_array_access)) {
22308e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
22318e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "previous access",
22328e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  earlier->max_array_access);
22338e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      }
22348e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22358e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->type = var->type;
22368e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      delete var;
22378e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      var = NULL;
22388e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   } else if (state->ARB_fragment_coord_conventions_enable
22398e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && strcmp(var->name, "gl_FragCoord") == 0
22408e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->type == var->type
22418e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->mode == var->mode) {
22428e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
22438e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * qualifiers.
22448e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       */
22458e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->origin_upper_left = var->origin_upper_left;
22468e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->pixel_center_integer = var->pixel_center_integer;
22478e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22488e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* According to section 4.3.7 of the GLSL 1.30 spec,
22498e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * the following built-in varaibles can be redeclared with an
22508e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * interpolation qualifier:
22518e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_FrontColor
22528e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_BackColor
22538e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_FrontSecondaryColor
22548e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_BackSecondaryColor
22558e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_Color
22568e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_SecondaryColor
22578e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       */
22588e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   } else if (state->language_version >= 130
22598e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && (strcmp(var->name, "gl_FrontColor") == 0
22608e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_BackColor") == 0
22618e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_FrontSecondaryColor") == 0
22628e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_BackSecondaryColor") == 0
22638e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_Color") == 0
22648e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_SecondaryColor") == 0)
22658e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->type == var->type
22668e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->mode == var->mode) {
22678e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->interpolation = var->interpolation;
22688e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22698e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* Layout qualifiers for gl_FragDepth. */
22706b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák   } else if ((state->AMD_conservative_depth_enable ||
22716b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák               state->ARB_conservative_depth_enable)
22728e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && strcmp(var->name, "gl_FragDepth") == 0
22738e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->type == var->type
22748e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->mode == var->mode) {
22758e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22768e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /** From the AMD_conservative_depth spec:
22778e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *     Within any shader, the first redeclarations of gl_FragDepth
22788e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *     must appear before any use of gl_FragDepth.
22798e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       */
22808e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      if (earlier->used) {
22818e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	 _mesa_glsl_error(&loc, state,
22828e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "the first redeclaration of gl_FragDepth "
22838e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "must appear before any use of gl_FragDepth");
22848e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      }
22858e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22868e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* Prevent inconsistent redeclaration of depth layout qualifier. */
22878e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      if (earlier->depth_layout != ir_depth_layout_none
22888e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	  && earlier->depth_layout != var->depth_layout) {
22898e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	 _mesa_glsl_error(&loc, state,
22908e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "gl_FragDepth: depth layout is declared here "
22918e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "as '%s, but it was previously declared as "
22928e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "'%s'",
22938e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  depth_layout_string(var->depth_layout),
22948e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  depth_layout_string(earlier->depth_layout));
22958e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      }
22968e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22978e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->depth_layout = var->depth_layout;
22988e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22998e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   } else {
23008e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
23018e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   }
23028e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
23038e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   return earlier;
23048e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick}
2305a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
23060292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick/**
23070292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * Generate the IR for an initializer in a variable declaration
23080292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick */
23090292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanickir_rvalue *
23100292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanickprocess_initializer(ir_variable *var, ast_declaration *decl,
23110292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		    ast_fully_specified_type *type,
23120292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		    exec_list *initializer_instructions,
23130292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		    struct _mesa_glsl_parse_state *state)
23140292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick{
23150292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   ir_rvalue *result = NULL;
23160292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23170292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   YYLTYPE initializer_loc = decl->initializer->get_location();
23180292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23190292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
23200292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    *
23210292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    *    "All uniform variables are read-only and are initialized either
23220292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    *    directly by an application via API commands, or indirectly by
23230292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    *    OpenGL."
23240292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    */
23250292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if ((state->language_version <= 110)
23260292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       && (var->mode == ir_var_uniform)) {
23270292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      _mesa_glsl_error(& initializer_loc, state,
23280292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       "cannot initialize uniforms in GLSL 1.10");
23290292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
23300292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23310292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if (var->type->is_sampler()) {
23320292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      _mesa_glsl_error(& initializer_loc, state,
23330292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       "cannot initialize samplers");
23340292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
23350292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23360292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
23370292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      _mesa_glsl_error(& initializer_loc, state,
23380292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       "cannot initialize %s shader input / %s",
23390292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       _mesa_glsl_shader_target_name(state->target),
23400292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       (state->target == vertex_shader)
23410292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       ? "attribute" : "varying");
23420292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
23430292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23440292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   ir_dereference *const lhs = new(state) ir_dereference_variable(var);
23450292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   ir_rvalue *rhs = decl->initializer->hir(initializer_instructions,
23460292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick					   state);
23470292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23480292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   /* Calculate the constant value if this is a const or uniform
23490292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    * declaration.
23500292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    */
23510292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if (type->qualifier.flags.q.constant
23520292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       || type->qualifier.flags.q.uniform) {
235385caea29c18fad89050ac366c558afef568dcb3fIan Romanick      ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs, true);
23540292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      if (new_rhs != NULL) {
23550292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 rhs = new_rhs;
23560292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23570292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 ir_constant *constant_value = rhs->constant_expression_value();
23580292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 if (!constant_value) {
23590292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    _mesa_glsl_error(& initializer_loc, state,
23600292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     "initializer of %s variable `%s' must be a "
23610292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     "constant expression",
23620292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     (type->qualifier.flags.q.constant)
23630292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     ? "const" : "uniform",
23640292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     decl->identifier);
23650292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    if (var->type->is_numeric()) {
23660292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	       /* Reduce cascading errors. */
23670292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	       var->constant_value = ir_constant::zero(state, var->type);
23680292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    }
23690292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 } else {
23700292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    rhs = constant_value;
23710292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    var->constant_value = constant_value;
23720292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 }
23730292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      } else {
23740292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 _mesa_glsl_error(&initializer_loc, state,
23750292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			  "initializer of type %s cannot be assigned to "
23760292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			  "variable of type %s",
23770292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			  rhs->type->name, var->type->name);
23780292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 if (var->type->is_numeric()) {
23790292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    /* Reduce cascading errors. */
23800292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    var->constant_value = ir_constant::zero(state, var->type);
23810292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 }
23820292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      }
23830292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
23840292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23850292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if (rhs && !rhs->type->is_error()) {
23860292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      bool temp = var->read_only;
23870292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      if (type->qualifier.flags.q.constant)
23880292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 var->read_only = false;
23890292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23900292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      /* Never emit code to initialize a uniform.
23910292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       */
23920292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      const glsl_type *initializer_type;
23930292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      if (!type->qualifier.flags.q.uniform) {
23940292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 result = do_assignment(initializer_instructions, state,
2395e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick				NULL,
239685caea29c18fad89050ac366c558afef568dcb3fIan Romanick				lhs, rhs, true,
23970292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick				type->get_location());
23980292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 initializer_type = result->type;
23990292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      } else
24000292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 initializer_type = rhs->type;
24010292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
2402f37b1ad937dd2c420f4c9fd9aa5887942bd31f3fIan Romanick      var->constant_initializer = rhs->constant_expression_value();
2403f37b1ad937dd2c420f4c9fd9aa5887942bd31f3fIan Romanick      var->has_initializer = true;
2404f37b1ad937dd2c420f4c9fd9aa5887942bd31f3fIan Romanick
24050292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      /* If the declared variable is an unsized array, it must inherrit
24060292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * its full type from the initializer.  A declaration such as
24070292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24080292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *     uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
24090292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24100292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * becomes
24110292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24120292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *     uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
24130292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24140292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * The assignment generated in the if-statement (below) will also
24150292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * automatically handle this case for non-uniforms.
24160292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24170292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * If the declared variable is not an array, the types must
24180292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * already match exactly.  As a result, the type assignment
24190292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * here can be done unconditionally.  For non-uniforms the call
24200292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * to do_assignment can change the type of the initializer (via
24210292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * the implicit conversion rules).  For uniforms the initializer
24220292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * must be a constant expression, and the type of that expression
24230292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * was validated above.
24240292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       */
24250292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      var->type = initializer_type;
24260292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
24270292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      var->read_only = temp;
24280292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
24290292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
24300292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   return result;
24310292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick}
24320292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
2433fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
24340044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions,
243518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			 struct _mesa_glsl_parse_state *state)
2436a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2437953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
2438a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *decl_type;
2439a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *type_name = NULL;
24408558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   ir_rvalue *result = NULL;
2441c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   YYLTYPE loc = this->get_location();
2442a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
24436f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
24446f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
24456f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     "To ensure that a particular output variable is invariant, it is
24466f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     necessary to use the invariant qualifier. It can either be used to
24476f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     qualify a previously declared variable as being invariant
24486f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
24496f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *         invariant gl_Position; // make existing gl_Position be invariant"
24506f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
24516f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * In these cases the parser will set the 'invariant' flag in the declarator
24526f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * list, and the type will be NULL.
24536f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    */
24546f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   if (this->invariant) {
24556f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      assert(this->type == NULL);
24566f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
24576f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (state->current_function != NULL) {
24586f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 _mesa_glsl_error(& loc, state,
24596f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "All uses of `invariant' keyword must be at global "
24606f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "scope\n");
24616f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
24626f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
24636f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
24646f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(!decl->is_array);
24656f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->array_size == NULL);
24666f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->initializer == NULL);
24676f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
24686f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 ir_variable *const earlier =
24696f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    state->symbols->get_variable(decl->identifier);
24706f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 if (earlier == NULL) {
24716f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
24726f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "Undeclared variable `%s' cannot be marked "
24736f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "invariant\n", decl->identifier);
24746f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == vertex_shader)
24756f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_out)) {
24766f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
24776f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
24786f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", decl->identifier);
24796f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == fragment_shader)
24806f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_in)) {
24816f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
24826f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
24836f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", decl->identifier);
2484bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	 } else if (earlier->used) {
2485bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	    _mesa_glsl_error(& loc, state,
2486bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			     "variable `%s' may not be redeclared "
2487bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			     "`invariant' after being used",
2488bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			     earlier->name);
24896f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else {
24906f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    earlier->invariant = true;
24916f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
24926f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
24936f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
24946f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      /* Invariant redeclarations do not have r-values.
24956f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       */
24966f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      return NULL;
24976f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   }
24986f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
24996f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(this->type != NULL);
25006f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(!this->invariant);
25016f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
25023455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* The type specifier may contain a structure definition.  Process that
25033455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * before any of the variable declarations.
25043455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
25053455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   (void) this->type->specifier->hir(instructions, state);
25063455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
2507d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   decl_type = this->type->specifier->glsl_type(& type_name, state);
2508304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick   if (this->declarations.is_empty()) {
2509f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick      /* If there is no structure involved in the program text, there are two
2510f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * possible scenarios:
2511f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *
2512f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * - The program text contained something like 'vec4;'.  This is an
2513f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *   empty declaration.  It is valid but weird.  Emit a warning.
2514f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *
2515f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * - The program text contained something like 'S;' and 'S' is not the
2516f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *   name of a known structure type.  This is both invalid and weird.
2517f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *   Emit an error.
2518f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *
2519f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * Note that if decl_type is NULL and there is a structure involved,
2520f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * there must have been some sort of error with the structure.  In this
2521f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * case we assume that an error was already generated on this line of
2522f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * code for the structure.  There is no need to generate an additional,
2523f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * confusing error.
2524f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       */
2525f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick      assert(this->type->specifier->structure == NULL || decl_type != NULL
2526f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick	     || state->error);
2527f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick      if (this->type->specifier->structure == NULL) {
2528f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick	 if (decl_type != NULL) {
2529547212d963c70161915c46d64e8020617199fb8dChia-I Wu	    _mesa_glsl_warning(&loc, state, "empty declaration");
2530f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick	 } else {
2531f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick	    _mesa_glsl_error(&loc, state,
2532f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick			     "invalid type `%s' in empty declaration",
2533f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick			     type_name);
2534547212d963c70161915c46d64e8020617199fb8dChia-I Wu	 }
2535c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      }
2536c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   }
2537a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
25382b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
2539a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      const struct glsl_type *var_type;
2540768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_variable *var;
2541a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2542a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Emit a warning if a variable declaration shadows a
2543a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: declaration at a higher scope.
2544a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
2545a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2546cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick      if ((decl_type == NULL) || decl_type->is_void()) {
2547a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (type_name != NULL) {
2548a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
2549a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type `%s' in declaration of `%s'",
2550a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     type_name, decl->identifier);
2551a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 } else {
2552a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
2553a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type in declaration of `%s'",
2554a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     decl->identifier);
2555a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
2556a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
2557a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2558a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2559a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (decl->is_array) {
2560d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 var_type = process_array_type(&loc, decl_type, decl->array_size,
2561d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke				       state);
2562a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick	 if (var_type->is_error())
2563a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick	    continue;
2564a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
2565a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 var_type = decl_type;
2566a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2567a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
25687e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick      var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
2569a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
25703f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
25713f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
25723f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     "Global variables can only use the qualifiers const,
25733f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     attribute, uni form, or varying. Only one may be
25743f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     specified.
25753f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
25763f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     Local variables can only use the qualifier const."
25773f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
257882c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick       * This is relaxed in GLSL 1.30.  It is also relaxed by any extension
257982c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick       * that adds the 'layout' keyword.
25803f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       */
258182c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick      if ((state->language_version < 130)
258282c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick	  && !state->ARB_explicit_attrib_location_enable
258382c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick	  && !state->ARB_fragment_coord_conventions_enable) {
2584e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.out) {
25853f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
25863f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`out' qualifier in declaration of `%s' "
2587469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick			     "only valid for function parameters in %s.",
2588469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick			     decl->identifier, state->version_string);
25893f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
2590e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.in) {
25913f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
25923f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`in' qualifier in declaration of `%s' "
2593469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick			     "only valid for function parameters in %s.",
2594469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick			     decl->identifier, state->version_string);
25953f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
25963f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 /* FINISHME: Test for other invalid qualifiers. */
25973f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      }
25983f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt
25992e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
26002e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				       & loc);
2601a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2602e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick      if (this->type->qualifier.flags.q.invariant) {
2603046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
2604046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt						   var->mode == ir_var_inout)) {
2605046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
2606046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature outval
2607046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
26086f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
26096f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
26106f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", var->name);
2611046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 } else if ((state->target == fragment_shader) &&
2612046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt		    !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
2613046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
2614046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature inval
2615046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
26166f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
26176f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
26186f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", var->name);
26196f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
26206f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
26216f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
2622e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      if (state->current_function != NULL) {
2623b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 const char *mode = NULL;
2624e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 const char *extra = "";
2625b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
2626e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 /* There is no need to check for 'inout' here because the parser will
2627e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	  * only allow that in function parameter lists.
2628e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	  */
2629e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.attribute) {
2630b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "attribute";
2631e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.uniform) {
2632b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "uniform";
2633e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.varying) {
2634b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "varying";
2635e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.in) {
2636e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "in";
2637e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
2638e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.out) {
2639e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "out";
2640e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
2641b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 }
2642b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
2643b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 if (mode) {
2644e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	    _mesa_glsl_error(& loc, state,
2645b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick			     "%s variable `%s' must be declared at "
2646e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     "global scope%s",
2647e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     mode, var->name, extra);
2648e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 }
2649e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      } else if (var->mode == ir_var_in) {
265001a584d09350d2c726312e2c9e88c5dbc54bdb70Chad Versace         var->read_only = true;
265101a584d09350d2c726312e2c9e88c5dbc54bdb70Chad Versace
2652fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 if (state->target == vertex_shader) {
2653fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    bool error_emitted = false;
2654fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
2655fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2656fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
2657fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
2658fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
2659fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors. Vertex shader inputs can also form arrays of these
2660fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    types, but not structures."
2661fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
26622d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
26632d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
26642d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
26652d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
26662d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors. They cannot be arrays or structures."
26672d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
2668fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
2669fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
2670fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "The attribute qualifier can be used only with float,
2671fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    floating-point vectors, and matrices. Attribute variables
2672fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    cannot be declared as arrays or structures."
2673fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     */
2674fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    const glsl_type *check_type = var->type->is_array()
2675fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       ? var->type->fields.array : var->type;
2676fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
2677fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    switch (check_type->base_type) {
2678fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_FLOAT:
2679fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       break;
2680fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_UINT:
2681fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_INT:
2682fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       if (state->language_version > 120)
2683fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		  break;
2684fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       /* FALLTHROUGH */
2685fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    default:
2686fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
2687fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
2688fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"type %s`%s'",
2689fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				var->type->is_array() ? "array of " : "",
2690fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				check_type->name);
2691fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
2692fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
2693fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
26942d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    if (!error_emitted && (state->language_version <= 130)
2695fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		&& var->type->is_array()) {
2696fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
2697fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
2698fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"array type");
2699fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
2700fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
2701fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 }
2702fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick      }
2703fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
270468d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace      /* Integer vertex outputs must be qualified with 'flat'.
270568d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       *
270668d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       * From section 4.3.6 of the GLSL 1.30 spec:
270768d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       *    "If a vertex output is a signed or unsigned integer or integer
270868d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       *    vector, then it must be qualified with the interpolation qualifier
270968d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       *    flat."
271068d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       */
271168d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace      if (state->language_version >= 130
271268d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace          && state->target == vertex_shader
271368d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace          && state->current_function == NULL
271468d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace          && var->type->is_integer()
271568d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace          && var->mode == ir_var_out
2716cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry          && var->interpolation != INTERP_QUALIFIER_FLAT) {
271768d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace
271868d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace         _mesa_glsl_error(&loc, state, "If a vertex output is an integer, "
271968d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace                          "then it must be qualified with 'flat'");
272068d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace      }
272168d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace
272268d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace
2723605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace      /* Interpolation qualifiers cannot be applied to 'centroid' and
2724605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       * 'centroid varying'.
2725605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       *
2726605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
2727605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       *    "interpolation qualifiers may only precede the qualifiers in,
2728605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       *    centroid in, out, or centroid out in a declaration. They do not apply
2729605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       *    to the deprecated storage qualifiers varying or centroid varying."
2730605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       */
2731605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace      if (state->language_version >= 130
2732605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace          && this->type->qualifier.has_interpolation()
2733605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace          && this->type->qualifier.flags.q.varying) {
2734605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace
2735605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         const char *i = this->type->qualifier.interpolation_string();
2736605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         assert(i != NULL);
2737605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         const char *s;
2738605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         if (this->type->qualifier.flags.q.centroid)
2739605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace            s = "centroid varying";
2740605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         else
2741605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace            s = "varying";
2742605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace
2743605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         _mesa_glsl_error(&loc, state,
2744605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace                          "qualifier '%s' cannot be applied to the "
2745605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace                          "deprecated storage qualifier '%s'", i, s);
2746605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace      }
2747605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace
2748605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace
27498faaa4a672c1062e486eda2525287715b554342dChad Versace      /* Interpolation qualifiers can only apply to vertex shader outputs and
27508faaa4a672c1062e486eda2525287715b554342dChad Versace       * fragment shader inputs.
27518faaa4a672c1062e486eda2525287715b554342dChad Versace       *
27528faaa4a672c1062e486eda2525287715b554342dChad Versace       * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
27538faaa4a672c1062e486eda2525287715b554342dChad Versace       *    "Outputs from a vertex shader (out) and inputs to a fragment
27548faaa4a672c1062e486eda2525287715b554342dChad Versace       *    shader (in) can be further qualified with one or more of these
27558faaa4a672c1062e486eda2525287715b554342dChad Versace       *    interpolation qualifiers"
27568faaa4a672c1062e486eda2525287715b554342dChad Versace       */
27578faaa4a672c1062e486eda2525287715b554342dChad Versace      if (state->language_version >= 130
27588faaa4a672c1062e486eda2525287715b554342dChad Versace          && this->type->qualifier.has_interpolation()) {
27598faaa4a672c1062e486eda2525287715b554342dChad Versace
27608faaa4a672c1062e486eda2525287715b554342dChad Versace         const char *i = this->type->qualifier.interpolation_string();
27618faaa4a672c1062e486eda2525287715b554342dChad Versace         assert(i != NULL);
27628faaa4a672c1062e486eda2525287715b554342dChad Versace
27638faaa4a672c1062e486eda2525287715b554342dChad Versace         switch (state->target) {
27648faaa4a672c1062e486eda2525287715b554342dChad Versace         case vertex_shader:
27658faaa4a672c1062e486eda2525287715b554342dChad Versace            if (this->type->qualifier.flags.q.in) {
27668faaa4a672c1062e486eda2525287715b554342dChad Versace               _mesa_glsl_error(&loc, state,
27678faaa4a672c1062e486eda2525287715b554342dChad Versace                                "qualifier '%s' cannot be applied to vertex "
27688faaa4a672c1062e486eda2525287715b554342dChad Versace                                "shader inputs", i);
27698faaa4a672c1062e486eda2525287715b554342dChad Versace            }
27708faaa4a672c1062e486eda2525287715b554342dChad Versace            break;
27718faaa4a672c1062e486eda2525287715b554342dChad Versace         case fragment_shader:
27728faaa4a672c1062e486eda2525287715b554342dChad Versace            if (this->type->qualifier.flags.q.out) {
27738faaa4a672c1062e486eda2525287715b554342dChad Versace               _mesa_glsl_error(&loc, state,
27748faaa4a672c1062e486eda2525287715b554342dChad Versace                                "qualifier '%s' cannot be applied to fragment "
27758faaa4a672c1062e486eda2525287715b554342dChad Versace                                "shader outputs", i);
27768faaa4a672c1062e486eda2525287715b554342dChad Versace            }
27778faaa4a672c1062e486eda2525287715b554342dChad Versace            break;
27788faaa4a672c1062e486eda2525287715b554342dChad Versace         default:
27798faaa4a672c1062e486eda2525287715b554342dChad Versace            assert(0);
27808faaa4a672c1062e486eda2525287715b554342dChad Versace         }
27818faaa4a672c1062e486eda2525287715b554342dChad Versace      }
27828faaa4a672c1062e486eda2525287715b554342dChad Versace
27838faaa4a672c1062e486eda2525287715b554342dChad Versace
27841eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace      /* From section 4.3.4 of the GLSL 1.30 spec:
27851eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace       *    "It is an error to use centroid in in a vertex shader."
27861eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace       */
27871eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace      if (state->language_version >= 130
27881eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace          && this->type->qualifier.flags.q.centroid
27891eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace          && this->type->qualifier.flags.q.in
27901eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace          && state->target == vertex_shader) {
27911eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace
27921eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace         _mesa_glsl_error(&loc, state,
27931eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace                          "'centroid in' cannot be used in a vertex shader");
27941eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace      }
27951eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace
27961eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace
2797889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
2798889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       */
2799889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      if (this->type->specifier->precision != ast_precision_none
2800889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace          && state->language_version != 100
2801889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace          && state->language_version < 130) {
2802889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
2803889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace         _mesa_glsl_error(&loc, state,
2804889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace                          "precision qualifiers are supported only in GLSL ES "
2805889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace                          "1.00, and GLSL 1.30 and later");
2806889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      }
2807889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
2808889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
280945e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace      /* Precision qualifiers only apply to floating point and integer types.
2810889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *
2811889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       * From section 4.5.2 of the GLSL 1.30 spec:
2812889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *    "Any floating point or any integer declaration can have the type
2813889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *    preceded by one of these precision qualifiers [...] Literal
2814889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *    constants do not have precision qualifiers. Neither do Boolean
2815889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *    variables.
28168752824f27c979986ae855667337e89637b005fbKenneth Graunke       *
28178752824f27c979986ae855667337e89637b005fbKenneth Graunke       * In GLSL ES, sampler types are also allowed.
28188752824f27c979986ae855667337e89637b005fbKenneth Graunke       *
28198752824f27c979986ae855667337e89637b005fbKenneth Graunke       * From page 87 of the GLSL ES spec:
28208752824f27c979986ae855667337e89637b005fbKenneth Graunke       *    "RESOLUTION: Allow sampler types to take a precision qualifier."
2821889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       */
2822889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      if (this->type->specifier->precision != ast_precision_none
282345e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace          && !var->type->is_float()
282445e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace          && !var->type->is_integer()
28258752824f27c979986ae855667337e89637b005fbKenneth Graunke          && !(var->type->is_sampler() && state->es_shader)
282645e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace          && !(var->type->is_array()
282745e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace               && (var->type->fields.array->is_float()
282845e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace                   || var->type->fields.array->is_integer()))) {
2829889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
2830889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace         _mesa_glsl_error(&loc, state,
28318752824f27c979986ae855667337e89637b005fbKenneth Graunke                          "precision qualifiers apply only to floating point"
28328752824f27c979986ae855667337e89637b005fbKenneth Graunke                          "%s types", state->es_shader ? ", integer, and sampler"
28338752824f27c979986ae855667337e89637b005fbKenneth Graunke						       : "and integer");
2834889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      }
2835889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
2836f07221056e1822187546b76387714b3172f9b2c5Paul Berry      /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
2837f07221056e1822187546b76387714b3172f9b2c5Paul Berry       *
2838f07221056e1822187546b76387714b3172f9b2c5Paul Berry       *    "[Sampler types] can only be declared as function
2839f07221056e1822187546b76387714b3172f9b2c5Paul Berry       *    parameters or uniform variables (see Section 4.3.5
2840f07221056e1822187546b76387714b3172f9b2c5Paul Berry       *    "Uniform")".
2841f07221056e1822187546b76387714b3172f9b2c5Paul Berry       */
2842f07221056e1822187546b76387714b3172f9b2c5Paul Berry      if (var_type->contains_sampler() &&
2843f07221056e1822187546b76387714b3172f9b2c5Paul Berry          !this->type->qualifier.flags.q.uniform) {
2844f07221056e1822187546b76387714b3172f9b2c5Paul Berry         _mesa_glsl_error(&loc, state, "samplers must be declared uniform");
2845f07221056e1822187546b76387714b3172f9b2c5Paul Berry      }
2846f07221056e1822187546b76387714b3172f9b2c5Paul Berry
2847e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick      /* Process the initializer and add its instructions to a temporary
2848e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * list.  This list will be added to the instruction stream (below) after
2849e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * the declaration is added.  This is done because in some cases (such as
2850e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * redeclarations) the declaration may not actually be added to the
2851e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * instruction stream.
2852e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       */
2853fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      exec_list initializer_instructions;
285409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick      ir_variable *earlier = get_variable_being_redeclared(var, decl, state);
285509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick
285666faec4895b7bb59a614087a200c05157191b4aeIan Romanick      if (decl->initializer != NULL) {
285709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 result = process_initializer((earlier == NULL) ? var : earlier,
285809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick				      decl, this->type,
28590292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick				      &initializer_instructions, state);
286066faec4895b7bb59a614087a200c05157191b4aeIan Romanick      }
286117d86f4371da413176ba365ca26a58bac172d365Ian Romanick
28620ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
28630ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *
28640ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *     "It is an error to write to a const variable outside of
28650ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      its declaration, so they must be initialized when
28660ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      declared."
28670ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       */
2868e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick      if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
28690ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	 _mesa_glsl_error(& loc, state,
287046f7105df487c91569f7e4a8da74d673c12e5619Chad Versace			  "const declaration of `%s' must be initialized",
287146f7105df487c91569f7e4a8da74d673c12e5619Chad Versace			  decl->identifier);
28720ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      }
28730ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt
287409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick      /* If the declaration is not a redeclaration, there are a few additional
287509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick       * semantic checks that must be applied.  In addition, variable that was
287609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick       * created for the declaration should be added to the IR stream.
28775466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       */
287809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick      if (earlier == NULL) {
287909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
288009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *
288109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *   "Identifiers starting with "gl_" are reserved for use by
288209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *   OpenGL, and may not be declared in a shader as either a
288309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *   variable or a function."
288409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  */
288509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 if (strncmp(decl->identifier, "gl_", 3) == 0)
288609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	    _mesa_glsl_error(& loc, state,
288709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick			     "identifier `%s' uses reserved `gl_' prefix",
288809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick			     decl->identifier);
2889c475a54578bf5473c6c62bc5468ef4fe555164d7Jason Wood	 else if (strstr(decl->identifier, "__")) {
2890684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	    /* From page 14 (page 20 of the PDF) of the GLSL 1.10
2891684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     * spec:
2892684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     *
2893684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     *     "In addition, all identifiers containing two
2894684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     *      consecutive underscores (__) are reserved as
2895684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     *      possible future keywords."
2896684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     */
2897684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	    _mesa_glsl_error(& loc, state,
2898684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt			     "identifier `%s' uses reserved `__' string",
2899684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt			     decl->identifier);
2900684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	 }
29015466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
290209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 /* Add the variable to the symbol table.  Note that the initializer's
290309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * IR was already processed earlier (though it hasn't been emitted
290409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * yet), without the variable in scope.
290509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *
290609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * This differs from most C-like languages, but it follows the GLSL
290709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
290809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * spec:
290909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *
291009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *     "Within a declaration, the scope of a name starts immediately
291109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *     after the initializer if present or immediately after the name
291209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *     being declared if not."
291309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  */
291409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 if (!state->symbols->add_variable(var)) {
291509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	    YYLTYPE loc = this->get_location();
291609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	    _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
291709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick			     "current scope", decl->identifier);
291809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	    continue;
291909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 }
292009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick
292109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 /* Push the variable declaration to the top.  It means that all the
292209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * variable declarations will appear in a funny last-to-first order,
292309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * but otherwise we run into trouble if a function is prototyped, a
292409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * global var is decled, then the function is defined with usage of
292509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * the global var.  See glslparsertest's CorrectModule.frag.
292609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  */
292709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 instructions->push_head(var);
29285d25746640ee27882b69a962459727cf924443dbKenneth Graunke      }
29295d25746640ee27882b69a962459727cf924443dbKenneth Graunke
2930fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      instructions->append_list(&initializer_instructions);
2931a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2932a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
29338558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt
29348558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   /* Generally, variable declarations do not have r-values.  However,
29358558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * one is used for the declaration in
29368558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
29378558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * while (bool b = some_condition()) {
29388558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *   ...
29398558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * }
29408558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
29418558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * so we return the rvalue from the last seen declaration here.
2942a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
29438558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   return result;
2944a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2945a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2946a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2947fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
29480044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions,
294918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
2950a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2951953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
2952a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *type;
2953a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *name = NULL;
29542e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   YYLTYPE loc = this->get_location();
2955a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2956d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   type = this->type->specifier->glsl_type(& name, state);
2957a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2958a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type == NULL) {
2959a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (name != NULL) {
2960a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2961a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type `%s' in declaration of `%s'",
296218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  name, this->identifier);
2963a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
2964a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2965a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type in declaration of `%s'",
296618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->identifier);
2967a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2968a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
29690471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::error_type;
2970a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2971a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2972068c80cfe0a280490353b6b007165d717c672eedEric Anholt   /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2973068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
2974068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    "Functions that accept no input arguments need not use void in the
2975068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    argument list because prototypes (or definitions) are required and
2976068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    therefore there is no ambiguity when an empty argument list "( )" is
2977068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    declared. The idiom "(void)" as a parameter list is provided for
2978068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    convenience."
2979068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
2980068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * Placing this check here prevents a void parameter being set up
2981068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * for a function, which avoids tripping up checks for main taking
2982068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * parameters and lookups of an unnamed symbol.
2983068c80cfe0a280490353b6b007165d717c672eedEric Anholt    */
2984cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if (type->is_void()) {
2985cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (this->identifier != NULL)
2986cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 _mesa_glsl_error(& loc, state,
2987cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick			  "named parameter cannot have type `void'");
2988cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
2989cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      is_void = true;
2990068c80cfe0a280490353b6b007165d717c672eedEric Anholt      return NULL;
2991cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
2992068c80cfe0a280490353b6b007165d717c672eedEric Anholt
299345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   if (formal_parameter && (this->identifier == NULL)) {
299445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
299545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      return NULL;
299645d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   }
299745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick
2998e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   /* This only handles "vec4 foo[..]".  The earlier specifier->glsl_type(...)
2999e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    * call already handled the "vec4[..] foo" case.
3000e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    */
3001e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   if (this->is_array) {
3002d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke      type = process_array_type(&loc, type, this->array_size, state);
3003e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
3004e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
3005a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick   if (!type->is_error() && type->array_size() == 0) {
3006e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
3007e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke		       "a declared size.");
3008e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      type = glsl_type::error_type;
3009e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
3010e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
3011cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   is_void = false;
30127e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
3013a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3014cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   /* Apply any specified qualifiers to the parameter declaration.  Note that
3015cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    * for function parameters the default mode is 'in'.
3016cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    */
30172e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
3018a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3019f07221056e1822187546b76387714b3172f9b2c5Paul Berry   /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3020f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *
3021f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    "Samplers cannot be treated as l-values; hence cannot be used
3022f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    as out or inout function parameters, nor can they be assigned
3023f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    into."
3024f07221056e1822187546b76387714b3172f9b2c5Paul Berry    */
3025f07221056e1822187546b76387714b3172f9b2c5Paul Berry   if ((var->mode == ir_var_inout || var->mode == ir_var_out)
3026f07221056e1822187546b76387714b3172f9b2c5Paul Berry       && type->contains_sampler()) {
3027f07221056e1822187546b76387714b3172f9b2c5Paul Berry      _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers");
3028f07221056e1822187546b76387714b3172f9b2c5Paul Berry      type = glsl_type::error_type;
3029f07221056e1822187546b76387714b3172f9b2c5Paul Berry   }
3030f07221056e1822187546b76387714b3172f9b2c5Paul Berry
303100792e3586746c833ffc9bb65712e38038916e06Paul Berry   /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
303200792e3586746c833ffc9bb65712e38038916e06Paul Berry    *
303300792e3586746c833ffc9bb65712e38038916e06Paul Berry    *    "When calling a function, expressions that do not evaluate to
303400792e3586746c833ffc9bb65712e38038916e06Paul Berry    *     l-values cannot be passed to parameters declared as out or inout."
303500792e3586746c833ffc9bb65712e38038916e06Paul Berry    *
303600792e3586746c833ffc9bb65712e38038916e06Paul Berry    * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
303700792e3586746c833ffc9bb65712e38038916e06Paul Berry    *
303800792e3586746c833ffc9bb65712e38038916e06Paul Berry    *    "Other binary or unary expressions, non-dereferenced arrays,
303900792e3586746c833ffc9bb65712e38038916e06Paul Berry    *     function names, swizzles with repeated fields, and constants
304000792e3586746c833ffc9bb65712e38038916e06Paul Berry    *     cannot be l-values."
304100792e3586746c833ffc9bb65712e38038916e06Paul Berry    *
304200792e3586746c833ffc9bb65712e38038916e06Paul Berry    * So for GLSL 1.10, passing an array as an out or inout parameter is not
304300792e3586746c833ffc9bb65712e38038916e06Paul Berry    * allowed.  This restriction is removed in GLSL 1.20, and in GLSL ES.
304400792e3586746c833ffc9bb65712e38038916e06Paul Berry    */
304500792e3586746c833ffc9bb65712e38038916e06Paul Berry   if ((var->mode == ir_var_inout || var->mode == ir_var_out)
304600792e3586746c833ffc9bb65712e38038916e06Paul Berry       && type->is_array() && state->language_version == 110) {
304700792e3586746c833ffc9bb65712e38038916e06Paul Berry      _mesa_glsl_error(&loc, state, "Arrays cannot be out or inout parameters in GLSL 1.10");
304800792e3586746c833ffc9bb65712e38038916e06Paul Berry      type = glsl_type::error_type;
304900792e3586746c833ffc9bb65712e38038916e06Paul Berry   }
305000792e3586746c833ffc9bb65712e38038916e06Paul Berry
30510044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(var);
3052a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3053a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Parameter declarations do not have r-values.
3054a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3055a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
3056a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
3057a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3058a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
305945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanickvoid
3060304ea90233baeac6801a98e981658cb7a2d2501cIan Romanickast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
306145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    bool formal,
306245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    exec_list *ir_parameters,
306345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    _mesa_glsl_parse_state *state)
3064a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
3065cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   ast_parameter_declarator *void_param = NULL;
3066cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   unsigned count = 0;
3067a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
30682b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
306945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      param->formal_parameter = formal;
3070068c80cfe0a280490353b6b007165d717c672eedEric Anholt      param->hir(ir_parameters, state);
3071cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3072cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (param->is_void)
3073cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 void_param = param;
3074cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3075cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      count++;
3076cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
3077cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3078cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if ((void_param != NULL) && (count > 1)) {
3079cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      YYLTYPE loc = void_param->get_location();
3080cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3081cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      _mesa_glsl_error(& loc, state,
3082cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick		       "`void' parameter must be only parameter");
3083a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3084a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
3085a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3086a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
30876fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunkevoid
30880d81b0e18494a80c4326fbc98837842959675869Paul Berryemit_function(_mesa_glsl_parse_state *state, ir_function *f)
30896fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke{
30900d81b0e18494a80c4326fbc98837842959675869Paul Berry   /* IR invariants disallow function declarations or definitions
30910d81b0e18494a80c4326fbc98837842959675869Paul Berry    * nested within other function definitions.  But there is no
30920d81b0e18494a80c4326fbc98837842959675869Paul Berry    * requirement about the relative order of function declarations
30930d81b0e18494a80c4326fbc98837842959675869Paul Berry    * and definitions with respect to one another.  So simply insert
30940d81b0e18494a80c4326fbc98837842959675869Paul Berry    * the new ir_function block at the end of the toplevel instruction
30950d81b0e18494a80c4326fbc98837842959675869Paul Berry    * list.
30960d81b0e18494a80c4326fbc98837842959675869Paul Berry    */
30970d81b0e18494a80c4326fbc98837842959675869Paul Berry   state->toplevel_ir->push_tail(f);
30986fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke}
30996fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke
31006fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke
3101fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
310292318a947958892497722772b03c643ebc943294Ian Romanickast_function::hir(exec_list *instructions,
310392318a947958892497722772b03c643ebc943294Ian Romanick		  struct _mesa_glsl_parse_state *state)
3104a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
3105953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
310618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_function *f = NULL;
310792318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *sig = NULL;
310892318a947958892497722772b03c643ebc943294Ian Romanick   exec_list hir_parameters;
3109a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3110ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   const char *const name = identifier;
3111a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
31129a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick   /* New functions are always added to the top-level IR instruction stream,
31139a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick    * so this instruction list pointer is ignored.  See also emit_function
31149a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick    * (called below).
31159a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick    */
31169a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick   (void) instructions;
31179a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick
311863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
311963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
312063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   "Function declarations (prototypes) cannot occur inside of functions;
312163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   they must be at global scope, or for the built-in functions, outside
312263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   the global scope."
312363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
312463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
312563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
312663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   "User defined functions may only be defined within the global scope."
312763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
312863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    * Note that this language does not appear in GLSL 1.10.
312963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    */
313063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   if ((state->current_function != NULL) && (state->language_version != 110)) {
313163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      YYLTYPE loc = this->get_location();
313263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      _mesa_glsl_error(&loc, state,
313363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick		       "declaration of function `%s' not allowed within "
313463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick		       "function body", name);
313563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   }
313663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick
3137edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3138edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *
3139edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   "Identifiers starting with "gl_" are reserved for use by
3140edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   OpenGL, and may not be declared in a shader as either a
3141edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   variable or a function."
3142edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    */
3143edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   if (strncmp(name, "gl_", 3) == 0) {
3144edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      YYLTYPE loc = this->get_location();
3145edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      _mesa_glsl_error(&loc, state,
3146edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke		       "identifier `%s' uses reserved `gl_' prefix", name);
3147edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   }
3148edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke
3149a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the list of function parameters to HIR now so that they can be
3150a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * used below to compare this function's signature with previously seen
3151a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * signatures for functions with the same name.
3152a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
315345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   ast_parameter_declarator::parameters_to_hir(& this->parameters,
315445d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       is_definition,
315545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       & hir_parameters, state);
3156a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3157e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const char *return_type_name;
3158e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const glsl_type *return_type =
315992318a947958892497722772b03c643ebc943294Ian Romanick      this->return_type->specifier->glsl_type(& return_type_name, state);
3160e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
316176e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   if (!return_type) {
316276e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      YYLTYPE loc = this->get_location();
316376e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      _mesa_glsl_error(&loc, state,
316476e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       "function `%s' has undeclared return type `%s'",
316576e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       name, return_type_name);
316676e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      return_type = glsl_type::error_type;
316776e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   }
3168e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
3169ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3170ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    * "No qualifier is allowed on the return type of a function."
3171ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    */
3172ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   if (this->return_type->has_qualifiers()) {
3173ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      YYLTYPE loc = this->get_location();
3174ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      _mesa_glsl_error(& loc, state,
3175ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke		       "function `%s' return type has qualifiers", name);
3176ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   }
3177ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke
3178f07221056e1822187546b76387714b3172f9b2c5Paul Berry   /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3179f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *
3180f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    "[Sampler types] can only be declared as function parameters
3181f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    or uniform variables (see Section 4.3.5 "Uniform")".
3182f07221056e1822187546b76387714b3172f9b2c5Paul Berry    */
3183f07221056e1822187546b76387714b3172f9b2c5Paul Berry   if (return_type->contains_sampler()) {
3184f07221056e1822187546b76387714b3172f9b2c5Paul Berry      YYLTYPE loc = this->get_location();
3185f07221056e1822187546b76387714b3172f9b2c5Paul Berry      _mesa_glsl_error(&loc, state,
3186f07221056e1822187546b76387714b3172f9b2c5Paul Berry                       "function `%s' return type can't contain a sampler",
3187f07221056e1822187546b76387714b3172f9b2c5Paul Berry                       name);
3188f07221056e1822187546b76387714b3172f9b2c5Paul Berry   }
3189f07221056e1822187546b76387714b3172f9b2c5Paul Berry
3190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Verify that this function's signature either doesn't match a previously
3191a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * seen signature for a function with the same name, or, if a match is found,
3192a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * that the previously seen signature does not have an associated definition.
3193a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3194e466b182bbf21f62fe6542091f4af3275555db80Ian Romanick   f = state->symbols->get_function(name);
319581f03393982c29f8f4165b5629c8e8fb708b97a3Kenneth Graunke   if (f != NULL && (state->es_shader || f->has_user_signature())) {
3196202604e8160157e4e80b3458175e0170d168e557Ian Romanick      sig = f->exact_matching_signature(&hir_parameters);
31970d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke      if (sig != NULL) {
31980d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 const char *badvar = sig->qualifiers_match(&hir_parameters);
31990d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (badvar != NULL) {
32000d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
3201abd40b15210c17b2a3ba8fcffc868fda203efa01Kenneth Graunke
32020d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
32030d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "qualifiers don't match prototype", name, badvar);
32040d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
32051e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
32060d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (sig->return_type != return_type) {
32070d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
320860be7626b829af7e1d07330b9a88468924ba350eEric Anholt
32090d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
32100d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "match prototype", name);
32110d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
3212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
32130d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (is_definition && sig->is_defined) {
32140d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
3215a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
32160d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
3217a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
3218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
3219a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
32201660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      f = new(ctx) ir_function(name);
3221e8f5ebf313da3ce33ccbbcf9b72946853035fbddEric Anholt      if (!state->symbols->add_function(f)) {
3222e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 /* This function name shadows a non-function use of the same name. */
3223e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 YYLTYPE loc = this->get_location();
3224e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke
3225e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
3226e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke			  "non-function", name);
3227e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 return NULL;
3228e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke      }
32299fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke
32300d81b0e18494a80c4326fbc98837842959675869Paul Berry      emit_function(state, f);
3231a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3233ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   /* Verify the return type of main() */
3234ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   if (strcmp(name, "main") == 0) {
323525711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick      if (! return_type->is_void()) {
3236ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 YYLTYPE loc = this->get_location();
3237ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
3238ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must return void");
3239ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt      }
3240174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
324192318a947958892497722772b03c643ebc943294Ian Romanick      if (!hir_parameters.is_empty()) {
3242174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 YYLTYPE loc = this->get_location();
3243174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
3244174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
3245174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt      }
3246ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   }
3247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Finish storing the information about this new function in its signature.
3249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
325092318a947958892497722772b03c643ebc943294Ian Romanick   if (sig == NULL) {
32511660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      sig = new(ctx) ir_function_signature(return_type);
325292318a947958892497722772b03c643ebc943294Ian Romanick      f->add_signature(sig);
3253a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3254a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3255bff6013d469b3d4e54cdc5731801c56994a523ecKenneth Graunke   sig->replace_parameters(&hir_parameters);
325692318a947958892497722772b03c643ebc943294Ian Romanick   signature = sig;
325792318a947958892497722772b03c643ebc943294Ian Romanick
325892318a947958892497722772b03c643ebc943294Ian Romanick   /* Function declarations (prototypes) do not have r-values.
325992318a947958892497722772b03c643ebc943294Ian Romanick    */
326092318a947958892497722772b03c643ebc943294Ian Romanick   return NULL;
326192318a947958892497722772b03c643ebc943294Ian Romanick}
326292318a947958892497722772b03c643ebc943294Ian Romanick
326392318a947958892497722772b03c643ebc943294Ian Romanick
326492318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue *
326592318a947958892497722772b03c643ebc943294Ian Romanickast_function_definition::hir(exec_list *instructions,
326692318a947958892497722772b03c643ebc943294Ian Romanick			     struct _mesa_glsl_parse_state *state)
326792318a947958892497722772b03c643ebc943294Ian Romanick{
326892318a947958892497722772b03c643ebc943294Ian Romanick   prototype->is_definition = true;
326992318a947958892497722772b03c643ebc943294Ian Romanick   prototype->hir(instructions, state);
3270e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
327192318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *signature = prototype->signature;
3272826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke   if (signature == NULL)
3273826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke      return NULL;
3274a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
327541ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == NULL);
327641ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = signature;
32776de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   state->found_return = false;
327841ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
3279e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   /* Duplicate parameters declared in the prototype as concrete variables.
3280e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick    * Add these to the symbol table.
3281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
32828bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->push_scope();
3283e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   foreach_iter(exec_list_iterator, iter, signature->parameters) {
3284fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
3285e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
3286fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      assert(var != NULL);
3287a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
32883359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* The only way a parameter would "exist" is if two parameters have
32893359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       * the same name.
32903359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
32913359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(var->name)) {
32923359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 YYLTYPE loc = this->get_location();
32933359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
32943359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
32953359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      } else {
3296001eee52d461233b1e1d6ed3577965e9bcb209e8Eric Anholt	 state->symbols->add_variable(var);
32973359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      }
3298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
33009fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   /* Convert the body of the function to HIR. */
3301894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt   this->body->hir(&signature->body, state);
33029fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   signature->is_defined = true;
3303a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
33048bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->pop_scope();
3305a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
330641ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == signature);
330741ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
3308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
33096de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   if (!signature->return_type->is_void() && !state->found_return) {
33106de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      YYLTYPE loc = this->get_location();
33116de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
33126de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       "%s, but no return statement",
33136de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->function_name(),
33146de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->return_type->name);
33156de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   }
33166de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke
3317a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Function definitions do not have r-values.
3318a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3319a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
3320a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
332116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
332216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
3323fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
332416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions,
332516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			struct _mesa_glsl_parse_state *state)
332616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{
3327953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
332816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
3329c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   switch (mode) {
3330c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_return: {
333116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      ir_return *inst;
3332aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt      assert(state->current_function);
333316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
333416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      if (opt_return_value) {
3335b4cdba687c098eea2ecc61349a4ea02a8769909eChad Versace	 ir_rvalue *const ret = opt_return_value->hir(instructions, state);
33362db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick
33372db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	 /* The value of the return type can be NULL if the shader says
33382db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  * 'return foo();' and foo() is a function that returns void.
33392db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  *
33402db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  * NOTE: The GLSL spec doesn't say that this is an error.  The type
33412db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  * of the return value is void.  If the return type of the function is
33422db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  * also void, then this should compile without error.  Seriously.
33432db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  */
33442db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	 const glsl_type *const ret_type =
33452db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	    (ret == NULL) ? glsl_type::void_type : ret->type;
334616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
334718707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 /* Implicit conversions are not allowed for return values. */
33482db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	 if (state->current_function->return_type != ret_type) {
334918707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    YYLTYPE loc = this->get_location();
335018707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke
335118707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    _mesa_glsl_error(& loc, state,
335218707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "`return' with wrong type %s, in function `%s' "
335318707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "returning %s",
33542db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick			     ret_type->name,
335518707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->function_name(),
335618707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->return_type->name);
335718707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 }
335816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
33591660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return(ret);
336016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      } else {
3361aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 if (state->current_function->return_type->base_type !=
3362aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	     GLSL_TYPE_VOID) {
3363aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    YYLTYPE loc = this->get_location();
3364aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt
3365aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    _mesa_glsl_error(& loc, state,
3366aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "`return' with no value, in function %s returning "
3367aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "non-void",
3368f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
3369aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 }
33701660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return;
337116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      }
337216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
33736de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      state->found_return = true;
337416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      instructions->push_tail(inst);
3375c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
337616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   }
337716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
3378c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_discard:
3379b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      if (state->target != fragment_shader) {
3380b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 YYLTYPE loc = this->get_location();
3381b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
3382b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 _mesa_glsl_error(& loc, state,
3383b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt			  "`discard' may only appear in a fragment shader");
3384b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      }
338577049a702ad54e09c4102fe8c964e069944f83e5Kenneth Graunke      instructions->push_tail(new(ctx) ir_discard);
3386c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
3387c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick
3388c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_break:
3389c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_continue:
33905c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      if (mode == ast_continue &&
33915c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	  state->loop_nesting_ast == NULL) {
33924cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 YYLTYPE loc = this->get_location();
33934cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
33944cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 _mesa_glsl_error(& loc, state,
33955c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe			  "continue may only appear in a loop");
33965c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      } else if (mode == ast_break &&
33975c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe		 state->loop_nesting_ast == NULL &&
339822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt		 state->switch_state.switch_nesting_ast == NULL) {
33995c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 YYLTYPE loc = this->get_location();
34004cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
34015c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 _mesa_glsl_error(& loc, state,
34025c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe			  "break may only appear in a loop or a switch");
34035c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      } else {
34045c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 /* For a loop, inline the for loop expression again,
34055c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	  * since we don't know where near the end of
34065c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	  * the loop body the normal copy of it
34072d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  * is going to be placed.
34082d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  */
34095c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 if (state->loop_nesting_ast != NULL &&
34105c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	     mode == ast_continue &&
34115c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	     state->loop_nesting_ast->rest_expression) {
34125c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    state->loop_nesting_ast->rest_expression->hir(instructions,
34135c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe							  state);
34142d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 }
34152d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt
341622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	 if (state->switch_state.is_switch_innermost &&
34175c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	     mode == ast_break) {
34185c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    /* Force break out of switch by setting is_break switch state.
34195c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	     */
342022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	    ir_variable *const is_break_var = state->switch_state.is_break_var;
34215c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    ir_dereference_variable *const deref_is_break_var =
34225c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	       new(ctx) ir_dereference_variable(is_break_var);
34235c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    ir_constant *const true_val = new(ctx) ir_constant(true);
34245c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    ir_assignment *const set_break_var =
34255c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	       new(ctx) ir_assignment(deref_is_break_var,
34265c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe				      true_val,
34275c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe				      NULL);
34285c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
34295c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    instructions->push_tail(set_break_var);
34305c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 }
34315c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 else {
34325c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    ir_loop_jump *const jump =
34331660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	       new(ctx) ir_loop_jump((mode == ast_break)
34341660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     ? ir_loop_jump::jump_break
34351660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     : ir_loop_jump::jump_continue);
34364cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    instructions->push_tail(jump);
34374cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 }
34384cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      }
34394cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
3440c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
3441b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   }
3442b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
344316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   /* Jump instructions do not have r-values.
344416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
344516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
344616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
34473c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34483c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34493c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue *
34503c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions,
34513c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			     struct _mesa_glsl_parse_state *state)
34523c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
3453953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
34541660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
34553c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_rvalue *const condition = this->condition->hir(instructions, state);
34563c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34573c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
34583c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
34593c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    "Any expression whose type evaluates to a Boolean can be used as the
34603c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    conditional expression bool-expression. Vector types are not accepted
34613c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    as the expression to if."
34623c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
34633c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * The checks are separated so that higher quality diagnostics can be
34643c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * generated for cases where both rules are violated.
34653c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
34663c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
34673c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      YYLTYPE loc = this->condition->get_location();
34683c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34693c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
34703c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick		       "boolean");
34713c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
34723c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34731660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_if *const stmt = new(ctx) ir_if(condition);
34743c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
3475665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (then_statement != NULL) {
3476665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
34774f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      then_statement->hir(& stmt->then_instructions, state);
3478665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
3479665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
34803c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
3481665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (else_statement != NULL) {
3482665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
34834f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      else_statement->hir(& stmt->else_instructions, state);
3484665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
3485665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
34863c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34873c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   instructions->push_tail(stmt);
34883c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34893c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* if-statements do not have r-values.
34903c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
34913c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   return NULL;
34923c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick}
34939e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
34949e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
349585beb39e14556cf02f58116fd287120cd1defbd5Dan McCabeir_rvalue *
349685beb39e14556cf02f58116fd287120cd1defbd5Dan McCabeast_switch_statement::hir(exec_list *instructions,
349785beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe			  struct _mesa_glsl_parse_state *state)
349885beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe{
34995c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   void *ctx = state;
35005c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35015c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_rvalue *const test_expression =
35025c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      this->test_expression->hir(instructions, state);
35035c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35045c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
35055c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    *
35065c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    *    "The type of init-expression in a switch statement must be a
35075c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    *     scalar integer."
35085c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    *
35095c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    * The checks are separated so that higher quality diagnostics can be
35105c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    * generated for cases where the rule is violated.
35115c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35125c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   if (!test_expression->type->is_integer()) {
35135c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      YYLTYPE loc = this->test_expression->get_location();
35145c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35155c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      _mesa_glsl_error(& loc,
35165c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe		       state,
35175c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe		       "switch-statement expression must be scalar "
35185c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe		       "integer");
35195c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   }
35205c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35215c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Track the switch-statement nesting in a stack-like manner.
35225c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
352322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   struct glsl_switch_state saved = state->switch_state;
35245c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
352522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state.is_switch_innermost = true;
352622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state.switch_nesting_ast = this;
3527140632190cf41e6a035ca199b181091d4ed46986Eric Anholt   state->switch_state.labels_ht = hash_table_ctor(0, hash_table_pointer_hash,
3528140632190cf41e6a035ca199b181091d4ed46986Eric Anholt						   hash_table_pointer_compare);
352957e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt   state->switch_state.previous_default = NULL;
35305c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35315c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Initalize is_fallthru state to false.
35325c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35335c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
353422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state.is_fallthru_var =
353522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      new(ctx) ir_variable(glsl_type::bool_type,
353622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt			   "switch_is_fallthru_tmp",
353722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt			   ir_var_temporary);
353822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   instructions->push_tail(state->switch_state.is_fallthru_var);
35395c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35405c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_dereference_variable *deref_is_fallthru_var =
354122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
35425c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
35435c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe						  is_fallthru_val,
35445c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe						  NULL));
35455c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35465c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Initalize is_break state to false.
35475c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35485c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_rvalue *const is_break_val = new (ctx) ir_constant(false);
354922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state.is_break_var = new(ctx) ir_variable(glsl_type::bool_type,
355022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt							   "switch_is_break_tmp",
355122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt							   ir_var_temporary);
355222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   instructions->push_tail(state->switch_state.is_break_var);
35535c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35545c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_dereference_variable *deref_is_break_var =
355522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      new(ctx) ir_dereference_variable(state->switch_state.is_break_var);
35565c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var,
35575c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe						  is_break_val,
35585c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe						  NULL));
35595c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35605c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Cache test expression.
35615c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35625c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   test_to_hir(instructions, state);
35635c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35645c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Emit code for body of switch stmt.
35655c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35665c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   body->hir(instructions, state);
35675c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
3568140632190cf41e6a035ca199b181091d4ed46986Eric Anholt   hash_table_dtor(state->switch_state.labels_ht);
3569140632190cf41e6a035ca199b181091d4ed46986Eric Anholt
357022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state = saved;
357185beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe
357222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* Switch statements do not have r-values.
357322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
357422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     return NULL;
357522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  }
35765c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35775c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
357822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  void
357922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ast_switch_statement::test_to_hir(exec_list *instructions,
358022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt				    struct _mesa_glsl_parse_state *state)
358122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  {
358222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     void *ctx = state;
35835c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
358422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* Cache value of test expression.
358522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
358622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     ir_rvalue *const test_val =
358722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	test_expression->hir(instructions,
358822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt			     state);
35895c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
359022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     state->switch_state.test_var = new(ctx) ir_variable(glsl_type::int_type,
359122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt							 "switch_test_tmp",
359222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt							 ir_var_temporary);
359322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     ir_dereference_variable *deref_test_var =
359422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	new(ctx) ir_dereference_variable(state->switch_state.test_var);
35955c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
359622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     instructions->push_tail(state->switch_state.test_var);
359722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     instructions->push_tail(new(ctx) ir_assignment(deref_test_var,
359822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt						    test_val,
359922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt						    NULL));
360022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  }
360185beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe
360285beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe
360322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ir_rvalue *
360422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ast_switch_body::hir(exec_list *instructions,
360522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt		       struct _mesa_glsl_parse_state *state)
360622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  {
360722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     if (stmts != NULL)
360822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	stmts->hir(instructions, state);
36091660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
361022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* Switch bodies do not have r-values.
361122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
361222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     return NULL;
361322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  }
36149e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
36159e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
361622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ir_rvalue *
361722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ast_case_statement_list::hir(exec_list *instructions,
361822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt			       struct _mesa_glsl_parse_state *state)
361922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  {
362022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases)
362122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	case_stmt->hir(instructions, state);
36229e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
362322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* Case statements do not have r-values.
362422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
362522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     return NULL;
362622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  }
36279e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
36289e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
362922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ir_rvalue *
363022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ast_case_statement::hir(exec_list *instructions,
363122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt			  struct _mesa_glsl_parse_state *state)
363222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  {
363322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     labels->hir(instructions, state);
363422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
363522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* Conditionally set fallthru state based on break state.
363622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
363722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     ir_constant *const false_val = new(state) ir_constant(false);
363822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     ir_dereference_variable *const deref_is_fallthru_var =
363922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
364022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     ir_dereference_variable *const deref_is_break_var =
364122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	new(state) ir_dereference_variable(state->switch_state.is_break_var);
364222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     ir_assignment *const reset_fallthru_on_break =
364322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	new(state) ir_assignment(deref_is_fallthru_var,
364422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt				 false_val,
364522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt				 deref_is_break_var);
364622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     instructions->push_tail(reset_fallthru_on_break);
364722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
364822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* Guard case statements depending on fallthru state.
364922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
365022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     ir_dereference_variable *const deref_fallthru_guard =
365122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
365222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
365322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
365422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     foreach_list_typed (ast_node, stmt, link, & this->stmts)
365522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	stmt->hir(& test_fallthru->then_instructions, state);
365622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
365722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     instructions->push_tail(test_fallthru);
365822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
365922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* Case statements do not have r-values.
366022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
366122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     return NULL;
366222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  }
366322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
366422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
366522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ir_rvalue *
366622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ast_case_label_list::hir(exec_list *instructions,
366722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt			   struct _mesa_glsl_parse_state *state)
366822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  {
366922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     foreach_list_typed (ast_case_label, label, link, & this->labels)
367022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	label->hir(instructions, state);
367122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
367222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* Case labels do not have r-values.
367322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
367422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     return NULL;
367522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  }
367622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
367722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
367822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ir_rvalue *
367922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ast_case_label::hir(exec_list *instructions,
368022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt		      struct _mesa_glsl_parse_state *state)
368122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  {
368222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     void *ctx = state;
368322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
368422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     ir_dereference_variable *deref_fallthru_var =
368522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
368622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
368722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     ir_rvalue *const true_val = new(ctx) ir_constant(true);
368822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
368922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* If not default case, ...
369022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
369122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     if (this->test_value != NULL) {
369222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	/* Conditionally set fallthru state based on
369322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	 * comparison of cached test expression value to case label.
369422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	 */
36952c3e10e71935506798c413363df27afc4348fb53Eric Anholt	ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
36962c3e10e71935506798c413363df27afc4348fb53Eric Anholt	ir_constant *label_const = label_rval->constant_expression_value();
36972c3e10e71935506798c413363df27afc4348fb53Eric Anholt
36982c3e10e71935506798c413363df27afc4348fb53Eric Anholt	if (!label_const) {
36992c3e10e71935506798c413363df27afc4348fb53Eric Anholt	   YYLTYPE loc = this->test_value->get_location();
37002c3e10e71935506798c413363df27afc4348fb53Eric Anholt
37012c3e10e71935506798c413363df27afc4348fb53Eric Anholt	   _mesa_glsl_error(& loc, state,
37022c3e10e71935506798c413363df27afc4348fb53Eric Anholt			    "switch statement case label must be a "
37032c3e10e71935506798c413363df27afc4348fb53Eric Anholt			    "constant expression");
37042c3e10e71935506798c413363df27afc4348fb53Eric Anholt
37052c3e10e71935506798c413363df27afc4348fb53Eric Anholt	   /* Stuff a dummy value in to allow processing to continue. */
37062c3e10e71935506798c413363df27afc4348fb53Eric Anholt	   label_const = new(ctx) ir_constant(0);
3707140632190cf41e6a035ca199b181091d4ed46986Eric Anholt	} else {
3708140632190cf41e6a035ca199b181091d4ed46986Eric Anholt	   ast_expression *previous_label = (ast_expression *)
3709140632190cf41e6a035ca199b181091d4ed46986Eric Anholt	      hash_table_find(state->switch_state.labels_ht,
3710140632190cf41e6a035ca199b181091d4ed46986Eric Anholt			      (void *)(uintptr_t)label_const->value.u[0]);
3711140632190cf41e6a035ca199b181091d4ed46986Eric Anholt
3712140632190cf41e6a035ca199b181091d4ed46986Eric Anholt	   if (previous_label) {
3713140632190cf41e6a035ca199b181091d4ed46986Eric Anholt	      YYLTYPE loc = this->test_value->get_location();
3714140632190cf41e6a035ca199b181091d4ed46986Eric Anholt	      _mesa_glsl_error(& loc, state,
3715140632190cf41e6a035ca199b181091d4ed46986Eric Anholt			       "duplicate case value");
3716140632190cf41e6a035ca199b181091d4ed46986Eric Anholt
3717140632190cf41e6a035ca199b181091d4ed46986Eric Anholt	      loc = previous_label->get_location();
3718140632190cf41e6a035ca199b181091d4ed46986Eric Anholt	      _mesa_glsl_error(& loc, state,
3719140632190cf41e6a035ca199b181091d4ed46986Eric Anholt			       "this is the previous case label");
3720140632190cf41e6a035ca199b181091d4ed46986Eric Anholt	   } else {
3721140632190cf41e6a035ca199b181091d4ed46986Eric Anholt	      hash_table_insert(state->switch_state.labels_ht,
3722140632190cf41e6a035ca199b181091d4ed46986Eric Anholt				this->test_value,
3723140632190cf41e6a035ca199b181091d4ed46986Eric Anholt				(void *)(uintptr_t)label_const->value.u[0]);
3724140632190cf41e6a035ca199b181091d4ed46986Eric Anholt	   }
37252c3e10e71935506798c413363df27afc4348fb53Eric Anholt	}
372622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
372722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	ir_dereference_variable *deref_test_var =
372822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	   new(ctx) ir_dereference_variable(state->switch_state.test_var);
372922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
373022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	ir_rvalue *const test_cond = new(ctx) ir_expression(ir_binop_all_equal,
373122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt							    glsl_type::bool_type,
37322c3e10e71935506798c413363df27afc4348fb53Eric Anholt							    label_const,
373322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt							    deref_test_var);
373422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
373522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	ir_assignment *set_fallthru_on_test =
373622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	   new(ctx) ir_assignment(deref_fallthru_var,
373722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt				  true_val,
373822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt				  test_cond);
373922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
374022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	instructions->push_tail(set_fallthru_on_test);
374122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     } else { /* default case */
374257e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt	if (state->switch_state.previous_default) {
374357e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt	   printf("a\n");
374457e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt	   YYLTYPE loc = this->get_location();
374557e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt	   _mesa_glsl_error(& loc, state,
374657e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt			       "multiple default labels in one switch");
374757e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt
374857e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt	   printf("b\n");
374957e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt
375057e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt	   loc = state->switch_state.previous_default->get_location();
375157e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt	   _mesa_glsl_error(& loc, state,
375257e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt			    "this is the first default label");
375357e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt	}
375457e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt	state->switch_state.previous_default = this;
375557e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt
375622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	/* Set falltrhu state.
375722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	 */
375822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	ir_assignment *set_fallthru =
375922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	   new(ctx) ir_assignment(deref_fallthru_var,
376022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt				  true_val,
376122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt				  NULL);
376222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
376322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	instructions->push_tail(set_fallthru);
376422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     }
376522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
376622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* Case statements do not have r-values.
376722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
376822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     return NULL;
376922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  }
377022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
377122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
377222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  void
377322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ast_iteration_statement::condition_to_hir(ir_loop *stmt,
377422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt					    struct _mesa_glsl_parse_state *state)
377522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  {
377622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     void *ctx = state;
377722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
377822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     if (condition != NULL) {
377922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	ir_rvalue *const cond =
378022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	   condition->hir(& stmt->body_instructions, state);
378122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
378222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	if ((cond == NULL)
378322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	    || !cond->type->is_boolean() || !cond->type->is_scalar()) {
378422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	   YYLTYPE loc = condition->get_location();
378522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
378622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	   _mesa_glsl_error(& loc, state,
378722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt			    "loop condition must be scalar boolean");
378822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	} else {
378922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	   /* As the first code in the loop body, generate a block that looks
379022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	    * like 'if (!condition) break;' as the loop termination condition.
379122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	    */
379222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	   ir_rvalue *const not_cond =
379322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	      new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
379422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt				     NULL);
379522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
379622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	   ir_if *const if_stmt = new(ctx) ir_if(not_cond);
37979e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
379822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	   ir_jump *const break_stmt =
379922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	      new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
380022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
380122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	   if_stmt->then_instructions.push_tail(break_stmt);
380222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	   stmt->body_instructions.push_tail(if_stmt);
380322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	}
380422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     }
380522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  }
380622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
380722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
380822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ir_rvalue *
380922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  ast_iteration_statement::hir(exec_list *instructions,
381022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt			       struct _mesa_glsl_parse_state *state)
381122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt  {
381222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     void *ctx = state;
381322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
381422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* For-loops and while-loops start a new scope, but do-while loops do not.
381522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
381622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     if (mode != ast_do_while)
381722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	state->symbols->push_scope();
381822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
381922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     if (init_statement != NULL)
382022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	init_statement->hir(instructions, state);
382122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
382222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     ir_loop *const stmt = new(ctx) ir_loop();
382322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     instructions->push_tail(stmt);
382422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
382522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* Track the current loop nesting.
382622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
382722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
382822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
382922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     state->loop_nesting_ast = this;
383022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
383122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* Likewise, indicate that following code is closest to a loop,
383222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      * NOT closest to a switch.
383322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
383422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
383522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     state->switch_state.is_switch_innermost = false;
383622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
383722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     if (mode != ast_do_while)
383822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	condition_to_hir(stmt, state);
383922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
384022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     if (body != NULL)
384122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	body->hir(& stmt->body_instructions, state);
384222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
384322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     if (rest_expression != NULL)
384422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	rest_expression->hir(& stmt->body_instructions, state);
384522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
384622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     if (mode == ast_do_while)
384722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	condition_to_hir(stmt, state);
384822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
384922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     if (mode != ast_do_while)
385022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	state->symbols->pop_scope();
385122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
385222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     /* Restore previous nesting before returning.
385322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      */
385422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     state->loop_nesting_ast = nesting_ast;
385522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt     state->switch_state.is_switch_innermost = saved_is_switch_innermost;
3856e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick
38579e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   /* Loops do not have r-values.
38589e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
38599e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   return NULL;
38609e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick}
38613455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
38623455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
38633455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
38643455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_type_specifier::hir(exec_list *instructions,
38653455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
38663455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
386708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   if (!this->is_precision_statement && this->structure == NULL)
386808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      return NULL;
386908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
387008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   YYLTYPE loc = this->get_location();
387108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
387208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   if (this->precision != ast_precision_none
387308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace       && state->language_version != 100
387408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace       && state->language_version < 130) {
387508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      _mesa_glsl_error(&loc, state,
387608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                       "precision qualifiers exist only in "
387708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                       "GLSL ES 1.00, and GLSL 1.30 and later");
387808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      return NULL;
387908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   }
388008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   if (this->precision != ast_precision_none
388108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace       && this->structure != NULL) {
388208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      _mesa_glsl_error(&loc, state,
388308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                       "precision qualifiers do not apply to structures");
388408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      return NULL;
388508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   }
388608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
388708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   /* If this is a precision statement, check that the type to which it is
388808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    * applied is either float or int.
388908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *
389008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    * From section 4.5.3 of the GLSL 1.30 spec:
389108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *    "The precision statement
389208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *       precision precision-qualifier type;
389308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *    can be used to establish a default precision qualifier. The type
389408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *    field can be either int or float [...].  Any other types or
389508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *    qualifiers will result in an error.
389608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    */
389708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   if (this->is_precision_statement) {
389808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      assert(this->precision != ast_precision_none);
389908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      assert(this->structure == NULL); /* The check for structures was
390008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                                        * performed above. */
390108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      if (this->is_array) {
390208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace         _mesa_glsl_error(&loc, state,
390308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                          "default precision statements do not apply to "
390408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                          "arrays");
390508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace         return NULL;
390608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      }
390708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      if (this->type_specifier != ast_float
390808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace          && this->type_specifier != ast_int) {
390908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace         _mesa_glsl_error(&loc, state,
391008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                          "default precision statements apply only to types "
391108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                          "float and int");
391208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace         return NULL;
391308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      }
391408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
391508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      /* FINISHME: Translate precision statements into IR. */
391608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      return NULL;
391708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   }
391808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
39193455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   if (this->structure != NULL)
39203455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      return this->structure->hir(instructions, state);
392185ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick
392285ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick   return NULL;
39233455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
39243455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39253455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39263455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
39273455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_struct_specifier::hir(exec_list *instructions,
39283455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
39293455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
39303455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned decl_count = 0;
39313455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39323455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Make an initial pass over the list of structure fields to determine how
39333455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * many there are.  Each element in this list is an ast_declarator_list.
39343455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * This means that we actually need to count the number of elements in the
39353455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * 'declarations' list in each of the elements.
39363455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
39372b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
39382b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
3939304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      foreach_list_const (decl_ptr, & decl_list->declarations) {
39403455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_count++;
39413455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
39423455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
39433455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39443455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Allocate storage for the structure fields and process the field
39453455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * declarations.  As the declarations are processed, try to also convert
39463455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * the types to HIR.  This ensures that structure definitions embedded in
39473455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * other structure definitions are processed.
39483455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
3949d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8Kenneth Graunke   glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field,
395021b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt						  decl_count);
39513455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39523455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned i = 0;
39532b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
39542b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
39553455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const char *type_name;
39563455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39573455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      decl_list->type->specifier->hir(instructions, state);
39583455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
3959c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke      /* Section 10.9 of the GLSL ES 1.00 specification states that
3960c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke       * embedded structure definitions have been removed from the language.
3961c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke       */
3962c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke      if (state->es_shader && decl_list->type->specifier->structure != NULL) {
3963c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke	 YYLTYPE loc = this->get_location();
3964c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke	 _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
3965c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke			  "not allowed in GLSL ES 1.00.");
3966c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke      }
3967c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke
39683455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const glsl_type *decl_type =
39693455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_list->type->specifier->glsl_type(& type_name, state);
39703455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39712b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_declaration, decl, link,
39722b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick			  &decl_list->declarations) {
3973d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 const struct glsl_type *field_type = decl_type;
3974d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 if (decl->is_array) {
3975d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	    YYLTYPE loc = decl->get_location();
3976d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	    field_type = process_array_type(&loc, decl_type, decl->array_size,
3977d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke					    state);
3978d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 }
397973986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	 fields[i].type = (field_type != NULL)
398073986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	    ? field_type : glsl_type::error_type;
39813455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 fields[i].name = decl->identifier;
39823455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 i++;
39833455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
39843455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
39853455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39863455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   assert(i == decl_count);
39873455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
398849e3577b91f44013746f7a3db411e7041b7d899eIan Romanick   const glsl_type *t =
3989ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke      glsl_type::get_record_instance(fields, decl_count, this->name);
39901d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
3991ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   YYLTYPE loc = this->get_location();
3992a789ca649cb143c0c5bf3209ff1bde398fbd777eIan Romanick   if (!state->symbols->add_type(name, t)) {
3993ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
3994ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   } else {
3995eb639349e289a6b8be06a54f5e9e0ce18c71d511Kenneth Graunke      const glsl_type **s = reralloc(state, state->user_structures,
3996eb639349e289a6b8be06a54f5e9e0ce18c71d511Kenneth Graunke				     const glsl_type *,
3997eb639349e289a6b8be06a54f5e9e0ce18c71d511Kenneth Graunke				     state->num_user_structures + 1);
3998a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      if (s != NULL) {
3999a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 s[state->num_user_structures] = t;
4000a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->user_structures = s;
4001a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->num_user_structures++;
4002a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      }
4003ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   }
40043455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
40053455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Structure type definitions do not have r-values.
40063455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
40073455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   return NULL;
40083455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
4009