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
604b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholtstatic void
614b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholtdetect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
624b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt			       exec_list *instructions);
634b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
64d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanickvoid
65d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
66d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick{
67adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick   _mesa_glsl_initialize_variables(instructions, state);
68adfb0cd7401251bef0c854ac945fce78f0ed11dbIan Romanick
69814c89abdbcd5b841b98746af921796df0362238Kenneth Graunke   state->symbols->language_version = state->language_version;
70814c89abdbcd5b841b98746af921796df0362238Kenneth Graunke
7141ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
7241ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
730d81b0e18494a80c4326fbc98837842959675869Paul Berry   state->toplevel_ir = instructions;
740d81b0e18494a80c4326fbc98837842959675869Paul Berry
75a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke   /* Section 4.2 of the GLSL 1.20 specification states:
76a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * "The built-in functions are scoped in a scope outside the global scope
77a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  users declare global variables in.  That is, a shader's global scope,
78a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  available for user-defined functions and global variables, is nested
79a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *  inside the scope containing the built-in functions."
80a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *
81a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * Since built-in functions like ftransform() access built-in variables,
82a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * it follows that those must be in the outer scope as well.
83a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    *
84a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * We push scope here to create this nesting effect...but don't pop.
85a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * This way, a shader's globals are still in the symbol table for use
86a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    * by the linker.
87a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke    */
88a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke   state->symbols->push_scope();
89a044285e25615f2d97636fe3ba47d580c3537bc4Kenneth Graunke
902b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, & state->translation_unit)
91304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
9202c5ae1b3fef75d5c0a715313a69e6b95ebd5b95Ian Romanick
9302c5ae1b3fef75d5c0a715313a69e6b95ebd5b95Ian Romanick   detect_recursion_unlinked(state, instructions);
944b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   detect_conflicting_assignments(state, instructions);
950d81b0e18494a80c4326fbc98837842959675869Paul Berry
960d81b0e18494a80c4326fbc98837842959675869Paul Berry   state->toplevel_ir = NULL;
97d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick}
98d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
99d949a9afb0a01e9678a4343f66b056b41a2e48a9Ian Romanick
1000104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick/**
1010104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is available, convert one operand to a different type
1020104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
1030104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * The \c from \c ir_rvalue is converted "in place".
1040104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
1050104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param to     Type that the operand it to be converted to
1060104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param from   Operand that is being converted
1070104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \param state  GLSL compiler state
1080104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick *
1090104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * \return
1100104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * If a conversion is possible (or unnecessary), \c true is returned.
1110104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick * Otherwise \c false is returned.
1120104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick */
113f32d3df8ab2b7c6c746f46870edc4b284cea50caKenneth Graunkebool
114bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
1150104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick			  struct _mesa_glsl_parse_state *state)
1160104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick{
117953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
118bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (to->base_type == from->type->base_type)
1190104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return true;
1200104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1210104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* This conversion was added in GLSL 1.20.  If the compilation mode is
1220104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * GLSL 1.10, the conversion is skipped.
1230104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1240104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (state->language_version < 120)
1250104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1260104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1270104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
1280104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *
1290104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    "There are no implicit array or structure conversions. For
1300104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    example, an array of int cannot be implicitly converted to an
1310104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    array of float. There are no implicit conversions between
1320104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    *    signed and unsigned integers."
1330104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
1340104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   /* FINISHME: The above comment is partially a lie.  There is int/uint
1350104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    * FINISHME: conversion for immediate constants.
1360104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick    */
137bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   if (!to->is_float() || !from->type->is_numeric())
1380104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return false;
1390104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
140506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke   /* Convert to a floating point type with the same number of components
141506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke    * as the original type - i.e. int to float, not int to vec4.
142506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke    */
143506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke   to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
144506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke			        from->type->matrix_columns);
145506199b852390e14a1d78392285bee8f06b6ede7Kenneth Graunke
146bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick   switch (from->type->base_type) {
1470104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_INT:
1481660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
1490104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1500104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_UINT:
1511660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
1520104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      break;
1530104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   case GLSL_TYPE_BOOL:
1541660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
155dc58b3f8ccd817fdee390a3df5b8e0fb29d5397cEric Anholt      break;
1560104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   default:
1570104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      assert(0);
1580104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   }
1590104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1600104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   return true;
1610104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick}
1620104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
1630104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
164a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
165bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick		       bool multiply,
167a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
169336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
170336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
1710104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick
172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic binary operators add (+), subtract (-),
175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    multiply (*), and divide (/) operate on integer and
176a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    floating-point scalars, vectors, and matrices."
177a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
17860b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   if (!type_a->is_numeric() || !type_b->is_numeric()) {
179a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
180a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Operands to arithmetic operators must be numeric");
1810471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
182a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
183a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
185a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If one operand is floating-point based and the other is
186a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    not, then the conversions from Section 4.1.10 "Implicit
187a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Conversions" are applied to the non-floating-point-based operand."
188a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1890104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
1900104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
191a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
192a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "Could not implicitly convert operands to "
193a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "arithmetic operator");
1940104536568ed031654c1c3c957b0216bbca4a1d6Ian Romanick      return glsl_type::error_type;
195a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
196336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
197336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
198336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
199a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "If the operands are integer types, they must both be signed or
200a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    both be unsigned."
201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * From this rule and the preceeding conversion it can be inferred that
203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
20460b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * The is_numeric check above already filtered out the case where either
20560b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * type is not one of these, so now the base types need only be tested for
20660b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick    * equality.
207a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
208a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type_a->base_type != type_b->base_type) {
209a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state,
210a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt		       "base type mismatch for arithmetic operator");
2110471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
212a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All arithmetic binary operators result in the same fundamental type
215a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (signed integer, unsigned integer, or floating-point) as the
216a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operands they operate on, after operand type conversion. After
217a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    conversion, the following cases are valid
218a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
219a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The two operands are scalars. In this case the operation is
220a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      applied, resulting in a scalar."
221a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
222cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar() && type_b->is_scalar())
223a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* One operand is a scalar, and the other is a vector or matrix.
226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      In this case, the scalar operation is applied independently to each
227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      component of the vector or matrix, resulting in the same size
228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector or matrix."
229a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
230cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   if (type_a->is_scalar()) {
231cb36f8aaeeb09660843316270a781948f773d90bIan Romanick      if (!type_b->is_scalar())
232a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_b;
233cb36f8aaeeb09660843316270a781948f773d90bIan Romanick   } else if (type_b->is_scalar()) {
234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_a;
235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
236a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
237a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
238a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
239a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * handled.
240a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
24160b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_a->is_scalar());
24260b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(!type_b->is_scalar());
243a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
244a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The two operands are vectors of the same size. In this case, the
245a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operation is done component-wise resulting in the same size
246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      vector."
247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
248a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector() && type_b->is_vector()) {
249a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b) {
250a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
251a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      } else {
252a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 _mesa_glsl_error(loc, state,
253a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt			  "vector size mismatch for arithmetic operator");
254a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return glsl_type::error_type;
255a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      }
256a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
257a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
258a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
259a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
260a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * <vector, vector> have been handled.  At least one of the operands must
261a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * be matrix.  Further, since there are no integer matrix types, the base
262a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * type of both operands must be float.
263a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
26460b54d977a7b3df9612eb9232f6b5d6c3f393e2fIan Romanick   assert(type_a->is_matrix() || type_b->is_matrix());
265a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_a->base_type == GLSL_TYPE_FLOAT);
266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   assert(type_b->base_type == GLSL_TYPE_FLOAT);
267a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
268a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*   "* The operator is add (+), subtract (-), or divide (/), and the
269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operands are matrices with the same number of rows and the same
270a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      number of columns. In this case, the operation is done component-
271a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      wise resulting in the same size matrix."
272a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    * The operator is multiply (*), where both operands are matrices or
273a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      one operand is a vector and the other a matrix. A right vector
274a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand is treated as a column vector and a left vector operand as a
275a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      row vector. In all these cases, it is required that the number of
276a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      columns of the left operand is equal to the number of rows of the
277a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      right operand. Then, the multiply (*) operation does a linear
278a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      algebraic multiply, yielding an object that has the same number of
279a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      rows as the left operand and the same number of columns as the right
280a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      operand. Section 5.10 "Vector and Matrix Operations" explains in
281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *      more detail how vectors and matrices are operated on."
282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (! multiply) {
284a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      if (type_a == type_b)
285a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	 return type_a;
286a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
287fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      if (type_a->is_matrix() && type_b->is_matrix()) {
288c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* Matrix multiply.  The columns of A must match the rows of B.  Given
289c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the other previously tested constraints, this means the vector type
290c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of a row from A must be the same as the vector type of a column from
291c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
292c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  */
293c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 if (type_a->row_type() == type_b->column_type()) {
294c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	    /* The resulting matrix has the number of columns of matrix B and
295c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * the number of rows of matrix A.  We get the row count of A by
296c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * looking at the size of a vector that makes up a column.  The
297c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     * transpose (size of a row) is done for B.
298c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	     */
299a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    const glsl_type *const type =
300c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	       glsl_type::get_instance(type_a->base_type,
301c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_a->column_type()->vector_elements,
302c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick				       type_b->row_type()->vector_elements);
303a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    assert(type != glsl_type::error_type);
304a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
305a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt	    return type;
306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
307fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick      } else if (type_a->is_matrix()) {
308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 /* A is a matrix and B is a column vector.  Columns of A must match
309c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * rows of B.  Given the other previously tested constraints, this
310c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * means the vector type of a row from A must be the same as the
311c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * vector the type of B.
312a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
31347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 if (type_a->row_type() == type_b) {
31447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    /* The resulting vector has a number of elements equal to
31547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	     * the number of rows of matrix A. */
31647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    const glsl_type *const type =
31747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	       glsl_type::get_instance(type_a->base_type,
31847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       type_a->column_type()->vector_elements,
31947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       1);
32047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    assert(type != glsl_type::error_type);
32147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth
32247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    return type;
32347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 }
324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
325fce1150156edc8b51f5cf077679c0fdb5d582abaIan Romanick	 assert(type_b->is_matrix());
326a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
327c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	 /* A is a row vector and B is a matrix.  Columns of A must match rows
328c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * of B.  Given the other previously tested constraints, this means
329c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * the type of A must be the same as the vector type of a column from
330c1bd3a1a61364d8450629a935b4611184eb99654Ian Romanick	  * B.
331a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  */
33247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 if (type_a == type_b->column_type()) {
33347c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    /* The resulting vector has a number of elements equal to
33447c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	     * the number of columns of matrix B. */
33547c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    const glsl_type *const type =
33647c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	       glsl_type::get_instance(type_a->base_type,
33747c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       type_b->row_type()->vector_elements,
33847c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth				       1);
33947c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    assert(type != glsl_type::error_type);
34047c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth
34147c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	    return type;
34247c90b144729e3edf3b5cbf5b260c1c46e429879Carl Worth	 }
343a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
344a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt
345a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
346a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      return glsl_type::error_type;
347a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
348a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
349a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
350a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "All other cases are illegal."
351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
352a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
3530471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
355a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
356a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
357a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
35865e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholtunary_arithmetic_result_type(const struct glsl_type *type,
35965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt			     struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
360a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 57:
362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The arithmetic unary operators negate (-), post- and pre-increment
364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     and decrement (-- and ++) operate on integer or floating-point
365a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     values (including vectors and matrices). All unary operators work
366a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     component-wise on their operands. These result with the same type
367a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     they operated on."
368a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
36965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (!type->is_numeric()) {
37065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
37165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to arithmetic operators must be numeric");
3720471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
37365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
374a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
375a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
376a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
377a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
378cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace/**
379cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \brief Return the result type of a bit-logic operation.
380cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace *
381cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * If the given types to the bit-logic operator are invalid, return
382cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * glsl_type::error_type.
383cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace *
384cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \param type_a Type of LHS of bit-logic op
385cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace * \param type_b Type of RHS of bit-logic op
386cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace */
387cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versacestatic const struct glsl_type *
388cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versacebit_logic_result_type(const struct glsl_type *type_a,
389cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                      const struct glsl_type *type_b,
390cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                      ast_operators op,
391cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                      struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
392cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace{
393cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (state->language_version < 130) {
394cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
395cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
396cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
397cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace
398cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
399cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *
400cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     "The bitwise operators and (&), exclusive-or (^), and inclusive-or
401cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     (|). The operands must be of type signed or unsigned integers or
402cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     integer vectors."
403cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     */
404cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (!type_a->is_integer()) {
405cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
406cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                         ast_expression::operator_string(op));
407cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
408cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
409cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (!type_b->is_integer()) {
410cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
411cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                        ast_expression::operator_string(op));
412cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
413cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
414cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace
415cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    /*     "The fundamental types of the operands (signed or unsigned) must
416cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     match,"
417cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     */
418cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (type_a->base_type != type_b->base_type) {
419cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
420cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                        "base type", ast_expression::operator_string(op));
421cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
422cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
423cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace
424cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    /*     "The operands cannot be vectors of differing size." */
425cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (type_a->is_vector() &&
426cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace        type_b->is_vector() &&
427cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace        type_a->vector_elements != type_b->vector_elements) {
428cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
429cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                        "different sizes", ast_expression::operator_string(op));
430cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace       return glsl_type::error_type;
431cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    }
432cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace
433cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    /*     "If one operand is a scalar and the other a vector, the scalar is
434cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     applied component-wise to the vector, resulting in the same type as
435cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     the vector. The fundamental types of the operands [...] will be the
436cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     *     resulting fundamental type."
437cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace     */
438cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    if (type_a->is_scalar())
439cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace        return type_b;
440cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace    else
441cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace        return type_a;
442cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace}
443a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
444a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
445a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickmodulus_result_type(const struct glsl_type *type_a,
44665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    const struct glsl_type *type_b,
44765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		    struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
448a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
44982f994f3860ca05ff5550f32844b0f523d40b9efChad Versace   if (state->language_version < 130) {
45082f994f3860ca05ff5550f32844b0f523d40b9efChad Versace      _mesa_glsl_error(loc, state,
45182f994f3860ca05ff5550f32844b0f523d40b9efChad Versace                       "operator '%%' is reserved in %s",
45282f994f3860ca05ff5550f32844b0f523d40b9efChad Versace                       state->version_string);
45382f994f3860ca05ff5550f32844b0f523d40b9efChad Versace      return glsl_type::error_type;
45482f994f3860ca05ff5550f32844b0f523d40b9efChad Versace   }
45582f994f3860ca05ff5550f32844b0f523d40b9efChad Versace
456a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
457a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The operator modulus (%) operates on signed or unsigned integers or
458a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    integer vectors. The operand types must both be signed or both be
459a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    unsigned."
460a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
4618eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke   if (!type_a->is_integer()) {
4628eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke      _mesa_glsl_error(loc, state, "LHS of operator %% must be an integer.");
4638eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke      return glsl_type::error_type;
4648eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke   }
4658eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke   if (!type_b->is_integer()) {
4668eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke      _mesa_glsl_error(loc, state, "RHS of operator %% must be an integer.");
4678eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke      return glsl_type::error_type;
4688eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke   }
4698eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke   if (type_a->base_type != type_b->base_type) {
4708eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke      _mesa_glsl_error(loc, state,
4718eb975394478a5c1ebd2bd8a12b5eb61cef808a7Kenneth Graunke		       "operands of %% must have the same base type");
4720471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
473a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
474a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
475a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operands cannot be vectors of differing size. If one operand is
476a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    a scalar and the other vector, then the scalar is applied component-
477a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    wise to the vector, resulting in the same type as the vector. If both
478a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    are vectors of the same size, the result is computed component-wise."
479a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
480a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick   if (type_a->is_vector()) {
481a2dd22fb194bdffa14a2466ae5667f3be63430d3Ian Romanick      if (!type_b->is_vector()
482a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	  || (type_a->vector_elements == type_b->vector_elements))
483a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 return type_a;
484a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else
485a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      return type_b;
486a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
487a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The operator modulus (%) is not defined for any other data types
488a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    (non-integer types)."
489a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
49065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   _mesa_glsl_error(loc, state, "type mismatch");
4910471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::error_type;
492a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
493a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
494a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
495a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic const struct glsl_type *
496bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanickrelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
49765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
498a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
499336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_a = value_a->type;
500336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   const glsl_type *type_b = value_b->type;
5010150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick
502a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* From GLSL 1.50 spec, page 56:
503a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    "The relational operators greater than (>), less than (<), greater
504a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    than or equal (>=), and less than or equal (<=) operate only on
505a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    scalar integer and scalar floating-point expressions."
506a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
507a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick   if (!type_a->is_numeric()
508a6d653dcbbee3158f9ea7b284bdeb1a8432f0fcbIan Romanick       || !type_b->is_numeric()
509cb36f8aaeeb09660843316270a781948f773d90bIan Romanick       || !type_a->is_scalar()
51065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt       || !type_b->is_scalar()) {
51165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
51265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Operands to relational operators must be scalar and "
51365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "numeric");
5140471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
51565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
516a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
517a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "Either the operands' types must match, or the conversions from
518a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
519a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *    operand, after which the types must match."
520a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
5210150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick   if (!apply_implicit_conversion(type_a, value_b, state)
5220150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick       && !apply_implicit_conversion(type_b, value_a, state)) {
52365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state,
52465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "Could not implicitly convert operands to "
52565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt		       "relational operator");
5260150f5f20edaef96520af5d1bbed0e62e24918e5Ian Romanick      return glsl_type::error_type;
527a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
528336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_a = value_a->type;
529336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   type_b = value_b->type;
530a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
53165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   if (type_a->base_type != type_b->base_type) {
53265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      _mesa_glsl_error(loc, state, "base type mismatch");
5330471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      return glsl_type::error_type;
53465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt   }
535a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
536a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /*    "The result is scalar Boolean."
537a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
5380471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick   return glsl_type::bool_type;
539a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
540a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
541c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace/**
542c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \brief Return the result type of a bit-shift operation.
543c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace *
544c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * If the given types to the bit-shift operator are invalid, return
545c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * glsl_type::error_type.
546c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace *
547c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \param type_a Type of LHS of bit-shift op
548c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace * \param type_b Type of RHS of bit-shift op
549c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace */
550c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versacestatic const struct glsl_type *
551c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versaceshift_result_type(const struct glsl_type *type_a,
552c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                  const struct glsl_type *type_b,
553c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                  ast_operators op,
554c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                  struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
555c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace{
556c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (state->language_version < 130) {
557c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
558c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      return glsl_type::error_type;
559c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
560c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
561c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
562c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *
563c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     "The shift operators (<<) and (>>). For both operators, the operands
564c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     must be signed or unsigned integers or integer vectors. One operand
565c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     can be signed while the other is unsigned."
566c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    */
567c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (!type_a->is_integer()) {
568c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
569c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              "integer vector", ast_expression::operator_string(op));
570c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace     return glsl_type::error_type;
571c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
572c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
573c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (!type_b->is_integer()) {
574c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
575c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              "integer vector", ast_expression::operator_string(op));
576c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace     return glsl_type::error_type;
577c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
578c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
579c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   /*     "If the first operand is a scalar, the second operand has to be
580c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     a scalar as well."
581c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    */
582c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (type_a->is_scalar() && !type_b->is_scalar()) {
583c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the "
584c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              "second must be scalar as well",
585c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              ast_expression::operator_string(op));
586c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace     return glsl_type::error_type;
587c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
588c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
589c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   /* If both operands are vectors, check that they have same number of
590c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    * elements.
591c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    */
592c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   if (type_a->is_vector() &&
593c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      type_b->is_vector() &&
594c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      type_a->vector_elements != type_b->vector_elements) {
595c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace      _mesa_glsl_error(loc, state, "Vector operands to operator %s must "
596c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              "have same number of elements",
597c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace              ast_expression::operator_string(op));
598c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace     return glsl_type::error_type;
599c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   }
600c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace
601c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   /*     "In all cases, the resulting type will be the same type as the left
602c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    *     operand."
603c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace    */
604c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace   return type_a;
605c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace}
606a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
6070bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick/**
6080bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that a value can be assigned to a location with a specified type
6090bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
6100bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Validates that \c rhs can be assigned to some location.  If the types are
6110bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * not an exact match but an automatic conversion is possible, \c rhs will be
6120bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * converted.
6130bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
6140bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \return
6150bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
6160bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * Otherwise the actual RHS to be assigned will be returned.  This may be
6170bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \c rhs, or it may be \c rhs after some type conversion.
6180bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick *
6190bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * \note
6200bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * In addition to being used for assignments, this function is used to
6210bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick * type-check return values.
6220bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick */
623fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
624336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholtvalidate_assignment(struct _mesa_glsl_parse_state *state,
62585caea29c18fad89050ac366c558afef568dcb3fIan Romanick		    const glsl_type *lhs_type, ir_rvalue *rhs,
62685caea29c18fad89050ac366c558afef568dcb3fIan Romanick		    bool is_initializer)
6270bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick{
6280bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If there is already some error in the RHS, just return it.  Anything
6290bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    * else will lead to an avalanche of error message back to the user.
6300bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
631ec53010c4d02e11171d3c782a41b70cad76788e8Ian Romanick   if (rhs->type->is_error())
6320bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
6330bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
6340bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   /* If the types are identical, the assignment can trivially proceed.
6350bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick    */
636ec53010c4d02e11171d3c782a41b70cad76788e8Ian Romanick   if (rhs->type == lhs_type)
6370bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick      return rhs;
6380bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
6390157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   /* If the array element types are the same and the size of the LHS is zero,
64085caea29c18fad89050ac366c558afef568dcb3fIan Romanick    * the assignment is okay for initializers embedded in variable
64185caea29c18fad89050ac366c558afef568dcb3fIan Romanick    * declarations.
6420157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    *
6430157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
6440157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    * is handled by ir_dereference::is_lvalue.
6450157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick    */
64685caea29c18fad89050ac366c558afef568dcb3fIan Romanick   if (is_initializer && lhs_type->is_array() && rhs->type->is_array()
6470157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->element_type() == rhs->type->element_type())
6480157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       && (lhs_type->array_size() == 0)) {
6490157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      return rhs;
6500157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick   }
6510157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
652336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   /* Check for implicit conversion in GLSL 1.20 */
653336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   if (apply_implicit_conversion(lhs_type, rhs, state)) {
654ec53010c4d02e11171d3c782a41b70cad76788e8Ian Romanick      if (rhs->type == lhs_type)
655336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt	 return rhs;
656336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt   }
657336b4ad8c76a525a0df2f4b0fc1d67e86bc5db3fEric Anholt
6580bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick   return NULL;
6590bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick}
6600bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
661a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholtstatic void
662a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholtmark_whole_array_access(ir_rvalue *access)
663a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt{
664a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt   ir_dereference_variable *deref = access->as_dereference_variable();
665a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt
666a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt   if (deref && deref->var) {
667a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt      deref->var->max_array_access = deref->type->length - 1;
668a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt   }
669a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt}
670a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt
67110a685218610e737e23d2d8a243ed6ff6613becdEric Anholtir_rvalue *
67210a685218610e737e23d2d8a243ed6ff6613becdEric Anholtdo_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
673e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick	      const char *non_lvalue_description,
67485caea29c18fad89050ac366c558afef568dcb3fIan Romanick	      ir_rvalue *lhs, ir_rvalue *rhs, bool is_initializer,
67510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	      YYLTYPE lhs_loc)
67610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt{
677953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
67810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
67910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
680f2475ca424f7e001be50f64dafa5700f6603d684Eric Anholt   ir_variable *lhs_var = lhs->variable_referenced();
681f2475ca424f7e001be50f64dafa5700f6603d684Eric Anholt   if (lhs_var)
682f2475ca424f7e001be50f64dafa5700f6603d684Eric Anholt      lhs_var->assigned = true;
683f2475ca424f7e001be50f64dafa5700f6603d684Eric Anholt
68410a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (!error_emitted) {
685e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      if (non_lvalue_description != NULL) {
686e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick         _mesa_glsl_error(&lhs_loc, state,
687e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick                          "assignment to %s",
688e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			  non_lvalue_description);
689e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick	 error_emitted = true;
690e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      } else if (lhs->variable_referenced() != NULL
691e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick		 && lhs->variable_referenced()->read_only) {
692b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace         _mesa_glsl_error(&lhs_loc, state,
693b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace                          "assignment to read-only variable '%s'",
694b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace                          lhs->variable_referenced()->name);
695b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace         error_emitted = true;
696b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace
697525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt      } else if (state->language_version <= 110 && lhs->type->is_array()) {
698525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	 /* From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
699525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	  *
700525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	  *    "Other binary or unary expressions, non-dereferenced
701525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	  *     arrays, function names, swizzles with repeated fields,
702525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	  *     and constants cannot be l-values."
703525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	  */
704525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	 _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not "
705525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt			  "allowed in GLSL 1.10 or GLSL ES 1.00.");
706525cec98a5c65c27c62fed0cff706bca50bf8c6eEric Anholt	 error_emitted = true;
707b66be7518ad57368b31b5d70a2bb4c0fe66aa988Chad Versace      } else if (!lhs->is_lvalue()) {
70810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
70910a685218610e737e23d2d8a243ed6ff6613becdEric Anholt	 error_emitted = true;
71010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      }
71110a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
71210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
71385caea29c18fad89050ac366c558afef568dcb3fIan Romanick   ir_rvalue *new_rhs =
71485caea29c18fad89050ac366c558afef568dcb3fIan Romanick      validate_assignment(state, lhs->type, rhs, is_initializer);
71510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   if (new_rhs == NULL) {
71610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      _mesa_glsl_error(& lhs_loc, state, "type mismatch");
71710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   } else {
71810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      rhs = new_rhs;
7190157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
7200157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      /* If the LHS array was not declared with a size, it takes it size from
7210157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * the RHS.  If the LHS is an l-value and a whole array, it must be a
7220157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * dereference of a variable.  Any other case would require that the LHS
7230157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       * is either not an l-value or not a whole array.
7240157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick       */
7250157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      if (lhs->type->array_size() == 0) {
7260157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 ir_dereference *const d = lhs->as_dereference();
7270157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
7280157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(d != NULL);
7290157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
73036ea28646c666ac2af9b43c47e65f9f53ffcc390Ian Romanick	 ir_variable *const var = d->variable_referenced();
7310157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
7320157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick	 assert(var != NULL);
7330157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick
73463f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
73563f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    /* FINISHME: This should actually log the location of the RHS. */
73663f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	    _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
73763f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     "previous access",
73863f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick			     var->max_array_access);
73963f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick	 }
74063f394203a8be9b87f8617cd7a56a0806c0870b3Ian Romanick
741f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick	 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
7420157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick						   rhs->type->array_size());
7439703ed05e684f4269cd8af27c94e9b6bf8781d85Eric Anholt	 d->type = var->type;
7440157f41e5e644632393edf903f3c1adb1cf782cdIan Romanick      }
745407a1001aefcb15e8d066031417d91ea22f1daf1Eric Anholt      mark_whole_array_access(rhs);
746a313c29c777de0bc74c252f1d35eef7d3d907e1fEric Anholt      mark_whole_array_access(lhs);
74710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt   }
74810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
7492731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt   /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
7502731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * but not post_inc) need the converted assigned value as an rvalue
7512731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * to handle things like:
7522731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
7532731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * i = j += 1;
7542731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    *
7552731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * So we always just store the computed value being assigned to a
7562731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * temporary and return a deref of that temporary.  If the rvalue
7572731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    * ends up not being used, the temp will get copy-propagated out.
7582731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt    */
7597e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
7607e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick					   ir_var_temporary);
761e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
762ae805922b7e3cdaf3aee26c3b799fe3608669bbaEric Anholt   instructions->push_tail(var);
763aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt   instructions->push_tail(new(ctx) ir_assignment(deref_var, rhs));
764e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   deref_var = new(ctx) ir_dereference_variable(var);
7652731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt
7668e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick   if (!error_emitted)
767aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt      instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var));
76810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
7691660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
77010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt}
7710bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
772de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue *
773959a9ecdd8fbc3375e4149f2b44d253622ff12eeEric Anholtget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
774de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{
775d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8Kenneth Graunke   void *ctx = ralloc_parent(lvalue);
776de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_variable *var;
777de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
7787e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
7797e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick			      ir_var_temporary);
78043b5b03d67ce890e867c81d4a5cfc4871d711d43Eric Anholt   instructions->push_tail(var);
781de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->mode = ir_var_auto;
782de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
7831660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
784aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt						  lvalue));
785de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
7861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
787de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt}
788de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
789de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
790fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
7910044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions,
79218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	      struct _mesa_glsl_parse_state *state)
79318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{
79418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) instructions;
79518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) state;
79618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
79718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   return NULL;
79818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
79918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
800ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholtstatic ir_rvalue *
801ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholtdo_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
802ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt{
803ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   int join_op;
8046d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick   ir_rvalue *cmp = NULL;
805ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
806ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   if (operation == ir_binop_all_equal)
807ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      join_op = ir_binop_logic_and;
808ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   else
809ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      join_op = ir_binop_logic_or;
810ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
811ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   switch (op0->type->base_type) {
812ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_FLOAT:
813ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_UINT:
814ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_INT:
815ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_BOOL:
816ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      return new(mem_ctx) ir_expression(operation, op0, op1);
817ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
818ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_ARRAY: {
819ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      for (unsigned int i = 0; i < op0->type->length; i++) {
820ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 ir_rvalue *e0, *e1, *result;
821ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
822ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
823ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt						new(mem_ctx) ir_constant(i));
824ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
825ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt						new(mem_ctx) ir_constant(i));
826ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 result = do_comparison(mem_ctx, operation, e0, e1);
827ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
8286d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	 if (cmp) {
8296d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	    cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
830ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 } else {
8316d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	    cmp = result;
832ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 }
833ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      }
834b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt
835b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt      mark_whole_array_access(op0);
836b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt      mark_whole_array_access(op1);
8376d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      break;
838ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   }
839ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
840ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_STRUCT: {
841ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      for (unsigned int i = 0; i < op0->type->length; i++) {
842ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 ir_rvalue *e0, *e1, *result;
843ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 const char *field_name = op0->type->fields.structure[i].name;
844ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
845ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
846ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt						 field_name);
847ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
848ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt						 field_name);
849ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 result = do_comparison(mem_ctx, operation, e0, e1);
850ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
8516d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	 if (cmp) {
8526d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	    cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
853ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 } else {
8546d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	    cmp = result;
855ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 }
856ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      }
8576d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      break;
858ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   }
859ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
860ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_ERROR:
861ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_VOID:
862ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_SAMPLER:
863ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      /* I assume a comparison of a struct containing a sampler just
864ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt       * ignores the sampler present in the type.
865ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt       */
8666d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      break;
8676d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick
8686d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick   default:
8696d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      assert(!"Should not get here.");
8706d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      break;
871ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   }
872d56c97413ee65e40e3544b89ffca450df9ba1c06Eric Anholt
8736d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick   if (cmp == NULL)
8746d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      cmp = new(mem_ctx) ir_constant(true);
8756d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick
8766d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick   return cmp;
877ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt}
87818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
87901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt/* For logical operations, we want to ensure that the operands are
88001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt * scalar booleans.  If it isn't, emit an error and return a constant
88101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt * boolean to avoid triggering cascading error messages.
88201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt */
88301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholtir_rvalue *
88401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholtget_scalar_boolean_operand(exec_list *instructions,
88501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   struct _mesa_glsl_parse_state *state,
88601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   ast_expression *parent_expr,
88701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   int operand,
88801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   const char *operand_name,
88901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   bool *error_emitted)
89001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt{
89101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   ast_expression *expr = parent_expr->subexpressions[operand];
89201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   void *ctx = state;
89301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   ir_rvalue *val = expr->hir(instructions, state);
89401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt
89501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   if (val->type->is_boolean() && val->type->is_scalar())
89601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      return val;
89701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt
89801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   if (!*error_emitted) {
89901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      YYLTYPE loc = expr->get_location();
90001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
90101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt		       operand_name,
90201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt		       parent_expr->operator_string(parent_expr->oper));
90301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      *error_emitted = true;
90401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   }
90501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt
90601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   return new(ctx) ir_constant(true);
90701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt}
90801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt
90993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry/**
91093b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry * If name refers to a builtin array whose maximum allowed size is less than
91193b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry * size, report an error and return true.  Otherwise return false.
91293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry */
91393b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berrystatic bool
91493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berrycheck_builtin_array_max_size(const char *name, unsigned size,
91593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                             YYLTYPE loc, struct _mesa_glsl_parse_state *state)
91693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry{
91793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry   if ((strcmp("gl_TexCoord", name) == 0)
91893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       && (size > state->Const.MaxTextureCoords)) {
91993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
92093b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       *
92193b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       *     "The size [of gl_TexCoord] can be at most
92293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       *     gl_MaxTextureCoords."
92393b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       */
92493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
92593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                       "be larger than gl_MaxTextureCoords (%u)\n",
92693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                       state->Const.MaxTextureCoords);
92793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      return true;
92837bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry   } else if (strcmp("gl_ClipDistance", name) == 0
92937bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry              && size > state->Const.MaxClipPlanes) {
93037bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry      /* From section 7.1 (Vertex Shader Special Variables) of the
93137bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       * GLSL 1.30 spec:
93237bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *
93337bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   "The gl_ClipDistance array is predeclared as unsized and
93437bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   must be sized by the shader either redeclaring it with a
93537bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   size or indexing it only with integral constant
93637bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   expressions. ... The size can be at most
93737bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   gl_MaxClipDistances."
93837bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       */
93937bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry      _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
94037bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry                       "be larger than gl_MaxClipDistances (%u)\n",
94137bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry                       state->Const.MaxClipPlanes);
94237bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry      return true;
94393b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry   }
94493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry   return false;
94593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry}
94693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry
9479abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry/**
9489abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * Create the constant 1, of a which is appropriate for incrementing and
9499abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * decrementing values of the given GLSL type.  For example, if type is vec4,
9509abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * this creates a constant value of 1.0 having type float.
9519abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry *
9529abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * If the given type is invalid for increment and decrement operators, return
9539abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * a floating point 1--the error will be detected later.
9549abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry */
9559abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berrystatic ir_rvalue *
9569abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berryconstant_one_for_inc_dec(void *ctx, const glsl_type *type)
9579abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry{
9589abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   switch (type->base_type) {
9599abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   case GLSL_TYPE_UINT:
9609abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      return new(ctx) ir_constant((unsigned) 1);
9619abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   case GLSL_TYPE_INT:
9629abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      return new(ctx) ir_constant(1);
9639abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   default:
9649abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   case GLSL_TYPE_FLOAT:
9659abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      return new(ctx) ir_constant(1.0f);
9669abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   }
9679abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry}
9689abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry
969fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
9700044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions,
97118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick		    struct _mesa_glsl_parse_state *state)
972a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
973953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
974a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   static const int operations[AST_NUM_OPERATORS] = {
975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_assign doesn't convert to ir_expression. */
976a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_plus doesn't convert to ir_expression. */
977a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_neg,
978a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,
979a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,
980a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,
981a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,
982a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,
983a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,
984a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,
985a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_less,
986a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_greater,
987a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lequal,
988a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_gequal,
9894dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri      ir_binop_all_equal,
9904dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri      ir_binop_any_nequal,
991a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and,
992a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor,
993a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,
994a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_bit_not,
995a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_and,
996a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_xor,
997a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_or,
998a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_logic_not,
999a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1000a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Note: The following block of expression types actually convert
1001a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * to multiple IR instructions.
1002a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1003a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,     /* ast_mul_assign */
1004a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,     /* ast_div_assign */
1005a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,     /* ast_mod_assign */
1006a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,     /* ast_add_assign */
1007a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,     /* ast_sub_assign */
1008a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,  /* ast_ls_assign */
1009a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,  /* ast_rs_assign */
1010a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and, /* ast_and_assign */
1011a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor, /* ast_xor_assign */
1012a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,  /* ast_or_assign */
1013a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1014a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_conditional doesn't convert to ir_expression. */
1015de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_pre_inc. */
1016de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_pre_dec. */
1017de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_post_inc. */
1018de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_post_dec. */
1019a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_field_selection doesn't conv to ir_expression. */
1020a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_array_index doesn't convert to ir_expression. */
1021a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_function_call doesn't conv to ir_expression. */
1022a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_identifier doesn't convert to ir_expression. */
1023a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_int_constant doesn't convert to ir_expression. */
1024a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
1025a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_float_constant doesn't conv to ir_expression. */
1026a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
1027a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_sequence doesn't convert to ir_expression. */
1028a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   };
1029fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *result = NULL;
10301c325af4d6b907e0a47ab7f868a2a78f054f153fAras Pranckevicius   ir_rvalue *op[3];
103108ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke   const struct glsl_type *type; /* a temporary variable for switch cases */
1032a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   bool error_emitted = false;
1033a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc;
1034a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
103518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   loc = this->get_location();
1036a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
103718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   switch (this->oper) {
10386652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   case ast_assign: {
103918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
104018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1041a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1042e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      result = do_assignment(instructions, state,
1043e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
1044e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     op[0], op[1], false,
104510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
104610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = result->type->is_error();
1047a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
10486652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   }
1049a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1050a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_plus:
105118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
1052a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1053c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1054c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth
1055c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      error_emitted = type->is_error();
1056a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1057a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = op[0];
1058a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1059a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1060a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_neg:
106118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
1062a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
106365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1064a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
106565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
1066a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10671660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
10681660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
1069a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1070a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1071a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add:
1072a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub:
1073a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul:
1074a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div:
107518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
107618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1077a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1078bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
107918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul),
1080a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
1081a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      error_emitted = type->is_error();
1082a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10831660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
10841660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
1085a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1086a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1087a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mod:
108818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
108918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1090a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
109165e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
1092a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
109318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(operations[this->oper] == ir_binop_mod);
1094a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10951660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
10961660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
109765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
1098a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1099a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lshift:
1101a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rshift:
11025c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       if (state->language_version < 130) {
11035c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace          _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30",
11045c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace              operator_string(this->oper));
11055c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace          error_emitted = true;
11065c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       }
11075c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace
11085c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       op[0] = this->subexpressions[0]->hir(instructions, state);
11095c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       op[1] = this->subexpressions[1]->hir(instructions, state);
1110c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace       type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1111c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                                &loc);
11125c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       result = new(ctx) ir_expression(operations[this->oper], type,
11135c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace                                       op[0], op[1]);
11145c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
11155c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       break;
1116a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1117a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_less:
1118a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_greater:
1119a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lequal:
1120a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_gequal:
112118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
112218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1123a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
112465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = relational_result_type(op[0], op[1], state, & loc);
1125a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1126a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The relational operators must either generate an error or result
1127a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
1128a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1129a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      assert(type->is_error()
1130a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     || ((type->base_type == GLSL_TYPE_BOOL)
1131cb36f8aaeeb09660843316270a781948f773d90bIan Romanick		 && type->is_scalar()));
1132a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
11331660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
11341660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
113565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
1136a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1137a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1138a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_nequal:
1139a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_equal:
11406e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
11416e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
11426e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
11436e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
11446e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *
11456e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    "The equality operators equal (==), and not equal (!=)
11466e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    operate on all types. They result in a scalar Boolean. If
11476e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    the operand types do not match, then there must be a
11486e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    conversion from Section 4.1.10 "Implicit Conversions"
11496e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    applied to one operand that can make them match, in which
11506e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    case this conversion is done."
11516e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       */
1152bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1153bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[1]->type, op[0], state))
1154212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick	  || (op[0]->type != op[1]->type)) {
11556e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
11566e659caaa946339a2de3890a8bed091ccb65102aIan Romanick			  "type", (this->oper == ast_equal) ? "==" : "!=");
11576e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 error_emitted = true;
1158a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick      } else if ((state->language_version <= 110)
1159a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick		 && (op[0]->type->is_array() || op[1]->type->is_array())) {
1160a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
1161a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick			  "GLSL 1.10");
1162a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 error_emitted = true;
11636e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      }
11646e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
1165175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt      if (error_emitted) {
1166175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt	 result = new(ctx) ir_constant(false);
1167175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt      } else {
1168175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt	 result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
1169175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt	 assert(result->type == glsl_type::bool_type);
1170175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt      }
1171a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_and:
1174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_xor:
1175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_or:
11761eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[0] = this->subexpressions[0]->hir(instructions, state);
11771eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[1] = this->subexpressions[1]->hir(instructions, state);
1178cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace      type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1179cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                                   state, &loc);
11801eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      result = new(ctx) ir_expression(operations[this->oper], type,
11811eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke				      op[0], op[1]);
11821eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
11831eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      break;
11841eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
1185a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_not:
11861eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[0] = this->subexpressions[0]->hir(instructions, state);
11871eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
11881eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (state->language_version < 130) {
11891eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
11901eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
11911eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
11921eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
11931eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (!op[0]->type->is_integer()) {
11941eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
11951eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
11961eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
11971eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
119839464489510270bbe472d11f7614c04ce1b6ae33Ian Romanick      type = error_emitted ? glsl_type::error_type : op[0]->type;
11991eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
1200a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1201a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
12024950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_and: {
12037ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt      exec_list rhs_instructions;
120401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
120501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt					 "LHS", &error_emitted);
12067ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt      op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
12077ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt					 "RHS", &error_emitted);
1208b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
1209ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt      if (rhs_instructions.is_empty()) {
1210ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt	 result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
1211ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt	 type = result->type;
121244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
121381d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
12147e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "and_tmp",
12157e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
121681d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(tmp);
121781d664f099a5fd5fac777480532fb4307d591451Ian Romanick
12181660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
121944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 instructions->push_tail(stmt);
12204950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12217ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt	 stmt->then_instructions.append_list(&rhs_instructions);
12221660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
122344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
1224aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt	    new(ctx) ir_assignment(then_deref, op[1]);
122544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
12264950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12271660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
122844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
1229aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt	    new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false));
123044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
12314950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12321660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
123344b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
123444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
12354950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
12364950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
12374950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12384950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_or: {
12399e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt      exec_list rhs_instructions;
124001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
124101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt					 "LHS", &error_emitted);
12429e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt      op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
12439e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt					 "RHS", &error_emitted);
12444950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
1245ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt      if (rhs_instructions.is_empty()) {
1246ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt	 result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
1247ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt	 type = result->type;
124844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
1249dfd30ca6a95a7d95835dad78ffe1fba4d1f4ef69Kenneth Graunke	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
12507e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "or_tmp",
12517e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
12520b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
12534950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
125481d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_if *const stmt = new(ctx) ir_if(op[0]);
125581d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(stmt);
125681d664f099a5fd5fac777480532fb4307d591451Ian Romanick
12571660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
125844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
1259aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt	    new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true));
126044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
12614950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12629e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt	 stmt->else_instructions.append_list(&rhs_instructions);
12631660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
126444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
1265aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt	    new(ctx) ir_assignment(else_deref, op[1]);
126644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
12674950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12681660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
126944b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
127044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
12714950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
12724950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
12734950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12744950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_xor:
1275756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt      /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1276756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       *
1277756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       *    "The logical binary operators and (&&), or ( | | ), and
1278756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       *     exclusive or (^^). They operate only on two Boolean
1279756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       *     expressions and result in a Boolean expression."
1280756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       */
1281756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
1282756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt					 &error_emitted);
1283756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt      op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
1284756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt					 &error_emitted);
12854950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
12871660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
1288a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1289a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1290a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt   case ast_logic_not:
129101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
129201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt					 "operand", &error_emitted);
1293a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
12941660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
12951660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
1296a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      break;
1297a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
1298a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul_assign:
1299a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div_assign:
1300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add_assign:
1301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub_assign: {
130218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
130318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1304a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1305bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
130618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul_assign),
1307a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
1308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
13091660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
13101660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						   op[0], op[1]);
1311a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
13123e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
1313e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
131485caea29c18fad89050ac366c558afef568dcb3fIan Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
131510a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
131610a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = (op[0]->type->is_error());
1317a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1318a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
1319a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * explicitly test for this because none of the binary expression
1320a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * operators allow array operands either.
1321a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1322a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1323a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1325a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
132648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   case ast_mod_assign: {
132748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
132848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
132948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
133065e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
133148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
133248a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      assert(operations[this->oper] == ir_binop_mod);
133348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
1334768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
13351660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
13361660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
133748a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
13383e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
1339e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
134085caea29c18fad89050ac366c558afef568dcb3fIan Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
134148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt			     this->subexpressions[0]->get_location());
134265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
134348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      break;
134448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   }
1345a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1346a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_ls_assign:
1347338ed6ec297d76746b6466c26c307722af965e60Chad Versace   case ast_rs_assign: {
1348338ed6ec297d76746b6466c26c307722af965e60Chad Versace      op[0] = this->subexpressions[0]->hir(instructions, state);
1349338ed6ec297d76746b6466c26c307722af965e60Chad Versace      op[1] = this->subexpressions[1]->hir(instructions, state);
1350338ed6ec297d76746b6466c26c307722af965e60Chad Versace      type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1351338ed6ec297d76746b6466c26c307722af965e60Chad Versace                               &loc);
1352338ed6ec297d76746b6466c26c307722af965e60Chad Versace      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1353338ed6ec297d76746b6466c26c307722af965e60Chad Versace                                                   type, op[0], op[1]);
1354e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      result = do_assignment(instructions, state,
1355e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
1356e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
1357338ed6ec297d76746b6466c26c307722af965e60Chad Versace                             this->subexpressions[0]->get_location());
1358338ed6ec297d76746b6466c26c307722af965e60Chad Versace      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1359251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1360338ed6ec297d76746b6466c26c307722af965e60Chad Versace   }
1361a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1362a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_and_assign:
1363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_xor_assign:
1364d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace   case ast_or_assign: {
1365d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      op[0] = this->subexpressions[0]->hir(instructions, state);
1366d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      op[1] = this->subexpressions[1]->hir(instructions, state);
1367d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1368d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                                   state, &loc);
1369d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1370d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                                                   type, op[0], op[1]);
1371e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      result = do_assignment(instructions, state,
1372e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
1373e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
1374d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                             this->subexpressions[0]->get_location());
1375d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1376251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1377d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace   }
1378a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
137996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   case ast_conditional: {
138096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
138196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
138296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    "The ternary selection operator (?:). It operates on three
138396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
138496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    first expression, which must result in a scalar Boolean."
138596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
138601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
138701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt					 "condition", &error_emitted);
138896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
138996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* The :? operator is implemented by generating an anonymous temporary
139096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * followed by an if-statement.  The last instruction in each branch of
139196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * the if-statement assigns a value to the anonymous temporary.  This
139296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * temporary is the r-value of the expression.
139396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
13940ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list then_instructions;
13950ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list else_instructions;
139696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
13970ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[1] = this->subexpressions[1]->hir(&then_instructions, state);
13980ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[2] = this->subexpressions[2]->hir(&else_instructions, state);
139996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
140096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
140196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
140296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     "The second and third expressions can be any type, as
140396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     long their types match, or there is a conversion in
140496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     Section 4.1.10 "Implicit Conversions" that can be applied
140596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     to one of the expressions to make their types match. This
140696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     resulting matching type is the type of the entire
140796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     expression."
140896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
1409bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1410bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[2]->type, op[1], state))
1411db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	  || (op[1]->type != op[2]->type)) {
141296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[1]->get_location();
141396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
141496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
141596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick			  "operator must have matching types.");
141696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
14170ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = glsl_type::error_type;
1418db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick      } else {
14190ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = op[1]->type;
142096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
142196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
1422f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1423f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *
1424f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *    "The second and third expressions must be the same type, but can
1425f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *    be of any type other than an array."
1426f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       */
1427f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      if ((state->language_version <= 110) && type->is_array()) {
1428f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1429f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick			  "operator must not be arrays.");
1430f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick	 error_emitted = true;
1431f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      }
1432f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick
14337825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *cond_val = op[0]->constant_expression_value();
14347825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *then_val = op[1]->constant_expression_value();
14357825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *else_val = op[2]->constant_expression_value();
14367825d3d15710fdfcfc503754862963aac8065480Ian Romanick
14377825d3d15710fdfcfc503754862963aac8065480Ian Romanick      if (then_instructions.is_empty()
14387825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && else_instructions.is_empty()
14397825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
14407825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 result = (cond_val->value.b[0]) ? then_val : else_val;
14417825d3d15710fdfcfc503754862963aac8065480Ian Romanick      } else {
14427e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	 ir_variable *const tmp =
14437e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	    new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
14440b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
14450ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
14461660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
14477825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 instructions->push_tail(stmt);
14480ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
14497825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 then_instructions.move_nodes_to(& stmt->then_instructions);
14501660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref =
14511660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
14527825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const then_assign =
1453aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt	    new(ctx) ir_assignment(then_deref, op[1]);
14547825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->then_instructions.push_tail(then_assign);
14550ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
14567825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 else_instructions.move_nodes_to(& stmt->else_instructions);
14571660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref =
14581660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
14597825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const else_assign =
1460aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt	    new(ctx) ir_assignment(else_deref, op[2]);
14617825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->else_instructions.push_tail(else_assign);
14620ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
14631660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
14647825d3d15710fdfcfc503754862963aac8065480Ian Romanick      }
1465251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
146696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   }
1467a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1468a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_pre_inc:
146976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   case ast_pre_dec: {
1470fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick      this->non_lvalue_description = (this->oper == ast_pre_inc)
1471fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick	 ? "pre-increment operation" : "pre-decrement operation";
1472fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick
147376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
14749abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
147576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1476a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
147776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1478768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
14791660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
14801660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
148176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
14823e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
1483e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
148485caea29c18fad89050ac366c558afef568dcb3fIan Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
148576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt			     this->subexpressions[0]->get_location());
148676ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      error_emitted = op[0]->type->is_error();
148776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      break;
148876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   }
1489a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1490a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_post_inc:
1491de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   case ast_post_dec: {
1492fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick      this->non_lvalue_description = (this->oper == ast_post_inc)
1493fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick	 ? "post-increment operation" : "post-decrement operation";
1494de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
14959abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1496de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1497de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1498de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1499a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1500de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1501768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
15021660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
15031660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
1504de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1505de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      /* Get a temporary of a copy of the lvalue before it's modified.
1506de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       * This may get thrown away later.
1507de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       */
15088273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1509de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
15103e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      (void)do_assignment(instructions, state,
1511e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			  this->subexpressions[0]->non_lvalue_description,
151285caea29c18fad89050ac366c558afef568dcb3fIan Romanick			  op[0]->clone(ctx, NULL), temp_rhs, false,
1513de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			  this->subexpressions[0]->get_location());
1514de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1515de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error();
1516a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1517de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   }
1518a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1519a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_field_selection:
152018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1521a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1522a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
152327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   case ast_array_index: {
152427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      YYLTYPE index_loc = subexpressions[1]->get_location();
152527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
152627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[0] = subexpressions[0]->hir(instructions, state);
152727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[1] = subexpressions[1]->hir(instructions, state);
152827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
152927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
153027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1531a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick      ir_rvalue *const array = op[0];
1532b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
15331660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_array(op[0], op[1]);
1534b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1535b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      /* Do not use op[0] after this point.  Use array.
1536b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       */
1537b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      op[0] = NULL;
1538b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
153927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
154027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
154127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 break;
154227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
154363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick      if (!array->type->is_array()
154463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_matrix()
154563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_vector()) {
154627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
154763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "cannot dereference non-array / non-matrix / "
154863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "non-vector");
154927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
155027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
155127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
155227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (!op[1]->type->is_integer()) {
155327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
155427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be integer type");
155527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
155627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      } else if (!op[1]->type->is_scalar()) {
155727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
155827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be scalar");
155927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
156027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
156127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
156227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      /* If the array index is a constant expression and the array has a
156327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size, ensure that the access is in-bounds.  If the array
156427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * index is not a constant expression, ensure that the array has a
156527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size.
156627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       */
156727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      ir_constant *const const_index = op[1]->constant_expression_value();
156827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (const_index != NULL) {
156927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 const int idx = const_index->value.i[0];
157063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 const char *type_name;
157163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 unsigned bound = 0;
157263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick
157363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
157463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "matrix";
157563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
157663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "vector";
157763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
157863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "array";
157963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
158027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
158127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
158227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *
158327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    "It is illegal to declare an array with a size, and then
158427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    later (in the same shader) index the same array with an
158527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    integral constant expression greater than or equal to the
158627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    declared size. It is also illegal to index an array with a
158727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    negative constant expression."
158827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  */
158963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
159063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->row_type()->vector_elements <= idx) {
159163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->row_type()->vector_elements;
159263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
159363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
159463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->vector_elements <= idx) {
159563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->vector_elements;
159663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
159763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
159863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((array->type->array_size() > 0)
159963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick		&& (array->type->array_size() <= idx)) {
160063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->array_size();
160163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
160227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
160327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
160463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (bound > 0) {
160563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be < %u",
160663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name, bound);
160763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    error_emitted = true;
160863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (idx < 0) {
160963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be >= 0",
161063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name);
161127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    error_emitted = true;
161227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
1613b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
161463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_array()) {
1615a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    /* If the array is a variable dereference, it dereferences the
1616a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * whole array, by definition.  Use this to get the variable.
1617a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     *
1618a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: Should some methods for getting / setting / testing
1619a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: array access limits be added to ir_dereference?
1620a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     */
1621a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    ir_variable *const v = array->whole_variable_referenced();
162293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry	    if ((v != NULL) && (unsigned(idx) > v->max_array_access)) {
162363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       v->max_array_access = idx;
162493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry
162593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry               /* Check whether this access will, as a side effect, implicitly
162693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                * cause the size of a built-in array to be too large.
162793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                */
162893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry               if (check_builtin_array_max_size(v->name, idx+1, loc, state))
162993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                  error_emitted = true;
163093b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry            }
163163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
16322b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke      } else if (array->type->array_size() == 0) {
16332b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
1634a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt      } else {
1635a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 if (array->type->is_array()) {
16365226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	    /* whole_variable_referenced can return NULL if the array is a
16375226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * member of a structure.  In this case it is safe to not update
16385226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * the max_array_access field because it is never used for fields
16395226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * of structures.
16405226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     */
1641a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	    ir_variable *v = array->whole_variable_referenced();
16425226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	    if (v != NULL)
16430d9d036004f135c38990c60f46074b70cff6e663Ian Romanick	       v->max_array_access = array->type->array_size() - 1;
1644a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 }
164527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
164627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1647e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick      /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
1648e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       *
1649f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace       *    "Samplers aggregated into arrays within a shader (using square
1650f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace       *    brackets [ ]) can only be indexed with integral constant
1651f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace       *    expressions [...]."
1652e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       *
1653e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * This restriction was added in GLSL 1.30.  Shaders using earlier version
1654e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * of the language should not be rejected by the compiler front-end for
1655e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * using this construct.  This allows useful things such as using a loop
1656e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * counter as the index to an array of samplers.  If the loop in unrolled,
1657e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * the code should compile correctly.  Instead, emit a warning.
1658f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace       */
1659f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace      if (array->type->is_array() &&
1660f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace          array->type->element_type()->is_sampler() &&
1661f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace          const_index == NULL) {
1662f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace
1663e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	 if (state->language_version == 100) {
1664e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	    _mesa_glsl_warning(&loc, state,
1665e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "sampler arrays indexed with non-constant "
1666e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "expressions is optional in GLSL ES 1.00");
1667e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	 } else if (state->language_version < 130) {
1668e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	    _mesa_glsl_warning(&loc, state,
1669e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "sampler arrays indexed with non-constant "
1670e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "expressions is forbidden in GLSL 1.30 and "
1671e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "later");
1672e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	 } else {
1673e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	    _mesa_glsl_error(&loc, state,
1674e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			     "sampler arrays indexed with non-constant "
1675e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			     "expressions is forbidden in GLSL 1.30 and "
1676e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			     "later");
1677e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	    error_emitted = true;
1678e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	 }
1679f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace      }
1680f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace
168127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
168227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 result->type = glsl_type::error_type;
168327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1684a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
168527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   }
1686a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1687a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_function_call:
16887cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      /* Should *NEVER* get here.  ast_function_call should always be handled
16897cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick       * by ast_function_expression::hir.
1690a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
16917cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      assert(0);
1692a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1693a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1694a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_identifier: {
1695a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* ast_identifier can appear several places in a full abstract syntax
1696a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * tree.  This particular use must be at location specified in the grammar
1697a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * as 'variable_identifier'.
1698a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
16998bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      ir_variable *var =
17008bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick	 state->symbols->get_variable(this->primary_expression.identifier);
1701a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1702a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (var != NULL) {
1703bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	 var->used = true;
1704dca19a771156685895892740f687cee7cf84a8c9Kenneth Graunke	 result = new(ctx) ir_dereference_variable(var);
1705a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
170671d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
170718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->primary_expression.identifier);
1708a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1709807e967c615dc80a264af5a89af7649f95481744Kenneth Graunke	 result = ir_rvalue::error_value(ctx);
1710a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 error_emitted = true;
1711a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1712a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1713a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1715a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_int_constant:
17161660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.int_constant);
1717a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1718a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1719a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_uint_constant:
17201660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1721a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1722a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_float_constant:
17241660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.float_constant);
1725a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1726a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1727a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bool_constant:
17281660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1729a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1730a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1731a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sequence: {
1732a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* It should not be possible to generate a sequence in the AST without
1733a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * any expressions in it.
1734a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1735304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      assert(!this->expressions.is_empty());
1736a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1737a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The r-value of a sequence is the last expression in the sequence.  If
1738a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the other expressions in the sequence do not have side-effects (and
1739a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * therefore add instructions to the instruction list), they get dropped
1740a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * on the floor.
1741a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
17423d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick      exec_node *previous_tail_pred = NULL;
17433d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick      YYLTYPE previous_operand_loc = loc;
17443d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick
17453d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick      foreach_list_typed (ast_node, ast, link, &this->expressions) {
17463d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 /* If one of the operands of comma operator does not generate any
17473d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * code, we want to emit a warning.  At each pass through the loop
17483d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * previous_tail_pred will point to the last instruction in the
17493d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * stream *before* processing the previous operand.  Naturally,
17503d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * instructions->tail_pred will point to the last instruction in the
17513d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * stream *after* processing the previous operand.  If the two
17523d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * pointers match, then the previous operand had no effect.
17533d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  *
17543d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * The warning behavior here differs slightly from GCC.  GCC will
17553d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * only emit a warning if none of the left-hand operands have an
17563d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * effect.  However, it will emit a warning for each.  I believe that
17573d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * there are some cases in C (especially with GCC extensions) where
17583d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * it is useful to have an intermediate step in a sequence have no
17593d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * effect, but I don't think these cases exist in GLSL.  Either way,
17603d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * it would be a giant hassle to replicate that behavior.
17613d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  */
17623d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 if (previous_tail_pred == instructions->tail_pred) {
17633d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	    _mesa_glsl_warning(&previous_operand_loc, state,
17643d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick			       "left-hand operand of comma expression has "
17653d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick			       "no effect");
17663d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 }
17673d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick
17683d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 /* tail_pred is directly accessed instead of using the get_tail()
17693d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * method for performance reasons.  get_tail() has extra code to
17703d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * return NULL when the list is empty.  We don't care about that
17713d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * here, so using tail_pred directly is fine.
17723d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  */
17733d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 previous_tail_pred = instructions->tail_pred;
17743d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 previous_operand_loc = ast->get_location();
17753d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick
1776304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick	 result = ast->hir(instructions, state);
17773d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick      }
1778a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1779a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Any errors should have already been emitted in the loop above.
1780a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1781a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      error_emitted = true;
1782a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1783a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1784a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
178508ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke   type = NULL; /* use result->type, not type. */
178608ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke   assert(result != NULL);
1787a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
178808ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke   if (result->type->is_error() && !error_emitted)
178971d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick      _mesa_glsl_error(& loc, state, "type mismatch");
1790a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1791a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
1792a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1793a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1794a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1795fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
17960044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions,
179718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1798a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1799a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* It is possible to have expression statements that don't have an
1800a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * expression.  This is the solitary semicolon:
1801a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1802a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for (i = 0; i < 5; i++)
1803a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     ;
1804a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1805a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * In this case the expression will be NULL.  Test for NULL and don't do
1806a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * anything in that case.
1807a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
180818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (expression != NULL)
180918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      expression->hir(instructions, state);
1810a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1811a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Statements do not have r-values.
1812a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1813a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1814a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1815a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1816a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1817fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
18180044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions,
181918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			    struct _mesa_glsl_parse_state *state)
1820a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
182118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
18228bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->push_scope();
1823a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18242b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, &this->statements)
1825304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
1826a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
182718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
18288bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->pop_scope();
1829a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1830a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Compound statements do not have r-values.
1831a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1832a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1833a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1834a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1835a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
183628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type *
1837d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunkeprocess_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
183828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick		   struct _mesa_glsl_parse_state *state)
183928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{
184028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   unsigned length = 0;
184128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1842a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick   /* From page 19 (page 25) of the GLSL 1.20 spec:
1843a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick    *
1844a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick    *     "Only one-dimensional arrays may be declared."
1845a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick    */
1846a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick   if (base->is_array()) {
1847a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick      _mesa_glsl_error(loc, state,
1848a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick		       "invalid array of `%s' (only one-dimensional arrays "
1849a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick		       "may be declared)",
1850a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick		       base->name);
1851a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick      return glsl_type::error_type;
1852a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick   }
185328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
185428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   if (array_size != NULL) {
185528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      exec_list dummy_instructions;
185628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
185728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      YYLTYPE loc = array_size->get_location();
185828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
185928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      if (ir != NULL) {
186028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 if (!ir->type->is_integer()) {
186128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be integer type");
186228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else if (!ir->type->is_scalar()) {
186328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be scalar type");
186428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else {
186528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    ir_constant *const size = ir->constant_expression_value();
186628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
186728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    if (size == NULL) {
186828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be a "
186928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick				"constant valued expression");
187028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else if (size->value.i[0] <= 0) {
187128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > 0");
187228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else {
187328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       assert(size->type == ir->type);
187428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       length = size->value.u[0];
1875d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry
1876d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry               /* If the array size is const (and we've verified that
1877d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                * it is) then no instructions should have been emitted
1878d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                * when we converted it to HIR.  If they were emitted,
1879d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                * then either the array size isn't const after all, or
1880d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                * we are emitting unnecessary instructions.
1881d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                */
1882d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry               assert(dummy_instructions.is_empty());
188328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    }
188428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 }
188528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
1886d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke   } else if (state->es_shader) {
1887d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke      /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
1888d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke       * array declarations have been removed from the language.
1889d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke       */
1890d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke      _mesa_glsl_error(loc, state, "unsized array declarations are not "
1891d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke		       "allowed in GLSL ES 1.00.");
189228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   }
189328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1894f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick   return glsl_type::get_array_instance(base, length);
189528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick}
189628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
189728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1898d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type *
1899d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name,
1900d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick			      struct _mesa_glsl_parse_state *state) const
1901a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1902d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   const struct glsl_type *type;
1903a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1904ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke   type = state->symbols->get_type(this->type_name);
1905ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke   *name = this->type_name;
1906a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1907ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke   if (this->is_array) {
1908ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke      YYLTYPE loc = this->get_location();
1909ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke      type = process_array_type(&loc, type, this->array_size, state);
1910a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1911a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1912a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
1913a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1914a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1915a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1916a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1917a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1918768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick				 ir_variable *var,
19192e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 struct _mesa_glsl_parse_state *state,
1920f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt				 YYLTYPE *loc,
1921f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt				 bool ubo_qualifiers_valid)
1922a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1923bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick   if (qual->flags.q.invariant) {
1924bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick      if (var->used) {
1925bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	 _mesa_glsl_error(loc, state,
1926bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			  "variable `%s' may not be redeclared "
1927bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			  "`invariant' after being used",
1928bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			  var->name);
1929bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick      } else {
1930bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	 var->invariant = 1;
1931bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick      }
1932bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick   }
1933a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1934e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.constant || qual->flags.q.attribute
1935e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick       || qual->flags.q.uniform
1936e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick       || (qual->flags.q.varying && (state->target == fragment_shader)))
1937a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->read_only = 1;
1938a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1939e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.centroid)
1940a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->centroid = 1;
1941a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1942e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.attribute && state->target != vertex_shader) {
19432e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      var->type = glsl_type::error_type;
19442e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      _mesa_glsl_error(loc, state,
19452e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "`attribute' variables may not be declared in the "
1946ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       "%s shader",
1947ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       _mesa_glsl_shader_target_name(state->target));
19482e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   }
19492e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
195090b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
195190b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *
195290b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     "The varying qualifier can be used only with the data types
195390b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
195490b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     these."
195590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    */
1956e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.varying) {
19570ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      const glsl_type *non_array_type;
19580ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
19590ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (var->type && var->type->is_array())
19600ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type->fields.array;
19610ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      else
19620ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type;
19630ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
19640ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
19650ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 var->type = glsl_type::error_type;
19660ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 _mesa_glsl_error(loc, state,
19670ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt			  "varying variables must be of base type float");
19680ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      }
196990b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   }
197090b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt
19717e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   /* If there is no qualifier that changes the mode of the variable, leave
19727e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    * the setting alone.
19737e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    */
1974e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.in && qual->flags.q.out)
1975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_inout;
1976e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.attribute || qual->flags.q.in
1977e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	    || (qual->flags.q.varying && (state->target == fragment_shader)))
1978a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_in;
1979e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.out
1980e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	    || (qual->flags.q.varying && (state->target == vertex_shader)))
1981a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_out;
1982e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.uniform)
1983a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_uniform;
1984a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
198586b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick   if (state->all_invariant && (state->current_function == NULL)) {
198686b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      switch (state->target) {
198786b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      case vertex_shader:
198886b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 if (var->mode == ir_var_out)
198986b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	    var->invariant = true;
199086b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 break;
199186b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      case geometry_shader:
199286b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 if ((var->mode == ir_var_in) || (var->mode == ir_var_out))
199386b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	    var->invariant = true;
199486b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 break;
199586b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      case fragment_shader:
199686b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 if (var->mode == ir_var_in)
199786b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	    var->invariant = true;
199886b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 break;
199986b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      }
200086b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick   }
200186b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick
2002e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.flat)
2003cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry      var->interpolation = INTERP_QUALIFIER_FLAT;
2004e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.noperspective)
2005cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry      var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
2006c488150dea083a9677429b4185c6b20d7facd52bPaul Berry   else if (qual->flags.q.smooth)
2007cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry      var->interpolation = INTERP_QUALIFIER_SMOOTH;
2008c488150dea083a9677429b4185c6b20d7facd52bPaul Berry   else
2009c488150dea083a9677429b4185c6b20d7facd52bPaul Berry      var->interpolation = INTERP_QUALIFIER_NONE;
20109d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick
2011916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt   if (var->interpolation != INTERP_QUALIFIER_NONE &&
2012916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt       !(state->target == vertex_shader && var->mode == ir_var_out) &&
2013916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt       !(state->target == fragment_shader && var->mode == ir_var_in)) {
2014916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      _mesa_glsl_error(loc, state,
2015916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt		       "interpolation qualifier `%s' can only be applied to "
2016916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt		       "vertex shader outputs and fragment shader inputs.",
2017d43f4181e1633d2677a8d828abd4ffc1a7c1e493Kenneth Graunke		       var->interpolation_string());
2018916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt   }
2019916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt
2020e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   var->pixel_center_integer = qual->flags.q.pixel_center_integer;
2021e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   var->origin_upper_left = qual->flags.q.origin_upper_left;
2022e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
20238d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick       && (strcmp(var->name, "gl_FragCoord") != 0)) {
2024e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick      const char *const qual_string = (qual->flags.q.origin_upper_left)
20258d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick	 ? "origin_upper_left" : "pixel_center_integer";
20268d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
20278d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick      _mesa_glsl_error(loc, state,
20288d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "layout qualifier `%s' can only be applied to "
20298d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "fragment shader input `gl_FragCoord'",
20308d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       qual_string);
20318d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick   }
20328d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
2033eee68d3631813580a14fa51fda6f0c959279256cIan Romanick   if (qual->flags.q.explicit_location) {
2034eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      const bool global_scope = (state->current_function == NULL);
2035eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      bool fail = false;
2036eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      const char *string = "";
2037eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
2038eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      /* In the vertex shader only shader inputs can be given explicit
2039eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       * locations.
2040eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       *
2041eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       * In the fragment shader only shader outputs can be given explicit
2042eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       * locations.
2043eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       */
2044eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      switch (state->target) {
2045eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      case vertex_shader:
2046eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 if (!global_scope || (var->mode != ir_var_in)) {
2047eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    fail = true;
2048eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    string = "input";
2049eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 }
2050eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 break;
2051eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
2052eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      case geometry_shader:
2053eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 _mesa_glsl_error(loc, state,
2054eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "geometry shader variables cannot be given "
2055eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "explicit locations\n");
2056eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 break;
2057eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
2058eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      case fragment_shader:
2059b078aad8ab22d840456688480a8c27d4664297cePaul Berry	 if (!global_scope || (var->mode != ir_var_out)) {
2060eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    fail = true;
2061eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    string = "output";
2062eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 }
2063eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 break;
2064a75da2c0e85eb6b8279ec895c3f74cc4aefc0257Kenneth Graunke      };
2065eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
2066eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      if (fail) {
2067eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 _mesa_glsl_error(loc, state,
2068eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "only %s shader %s variables can be given an "
2069eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "explicit location\n",
2070eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  _mesa_glsl_shader_target_name(state->target),
2071eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  string);
2072eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      } else {
2073eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 var->explicit_location = true;
207468a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick
207568a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 /* This bit of silliness is needed because invalid explicit locations
207668a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * are supposed to be flagged during linking.  Small negative values
207768a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
207868a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
207968a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * The linker needs to be able to differentiate these cases.  This
208068a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * ensures that negative values stay negative.
208168a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  */
208268a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 if (qual->location >= 0) {
208368a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	    var->location = (state->target == vertex_shader)
208468a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	       ? (qual->location + VERT_ATTRIB_GENERIC0)
208568a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	       : (qual->location + FRAG_RESULT_DATA0);
208668a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 } else {
208768a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	    var->location = qual->location;
208868a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 }
208942ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke
20901256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie	 if (qual->flags.q.explicit_index) {
209142ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke            /* From the GLSL 4.30 specification, section 4.4.2 (Output
209242ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke             * Layout Qualifiers):
209342ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke             *
209442ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke             * "It is also a compile-time error if a fragment shader
209542ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke             *  sets a layout index to less than 0 or greater than 1."
209642ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke             *
209742ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke             * Older specifications don't mandate a behavior; we take
209842ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke             * this as a clarification and always generate the error.
209942ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke             */
210042ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke            if (qual->index < 0 || qual->index > 1) {
210142ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke               _mesa_glsl_error(loc, state,
210242ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke                                "explicit index may only be 0 or 1\n");
210342ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke            } else {
210442ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke               var->explicit_index = true;
210542ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke               var->index = qual->index;
210642ef3f68c9621eb713f154955dccabacd6e1e0efKenneth Graunke            }
21071256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie	 }
2108eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      }
21091256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie   } else if (qual->flags.q.explicit_index) {
21101256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie	 _mesa_glsl_error(loc, state,
21111256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie			  "explicit index requires explicit location\n");
2112eee68d3631813580a14fa51fda6f0c959279256cIan Romanick   }
2113eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
21144bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   /* Does the declaration use the 'layout' keyword?
21154bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    */
21164bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   const bool uses_layout = qual->flags.q.pixel_center_integer
21174bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      || qual->flags.q.origin_upper_left
21181256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie      || qual->flags.q.explicit_location; /* no need for index since it relies on location */
21194bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick
21204bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   /* Does the declaration use the deprecated 'attribute' or 'varying'
21214bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * keywords?
21224bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    */
21234bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   const bool uses_deprecated_qualifier = qual->flags.q.attribute
21244bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      || qual->flags.q.varying;
21254bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick
21264bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   /* Is the 'layout' keyword used with parameters that allow relaxed checking.
21274bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
21284bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
21294bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * allowed the layout qualifier to be used with 'varying' and 'attribute'.
21304bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * These extensions and all following extensions that add the 'layout'
21314bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * keyword have been modified to require the use of 'in' or 'out'.
21324bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *
21334bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * The following extension do not allow the deprecated keywords:
21344bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *
21354bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *    GL_AMD_conservative_depth
21366b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák    *    GL_ARB_conservative_depth
21374bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *    GL_ARB_gpu_shader5
21384bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *    GL_ARB_separate_shader_objects
21394bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *    GL_ARB_tesselation_shader
21404bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *    GL_ARB_transform_feedback3
21414bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *    GL_ARB_uniform_buffer_object
21424bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    *
21434bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
21444bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    * allow layout with the deprecated keywords.
21454bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick    */
21464bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   const bool relaxed_layout_qualifier_checking =
21474bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      state->ARB_fragment_coord_conventions_enable;
21484bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick
21494bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   if (uses_layout && uses_deprecated_qualifier) {
21504bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      if (relaxed_layout_qualifier_checking) {
21514bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick	 _mesa_glsl_warning(loc, state,
21524bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick			    "`layout' qualifier may not be used with "
21534bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick			    "`attribute' or `varying'");
21544bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      } else {
21554bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick	 _mesa_glsl_error(loc, state,
21564bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick			  "`layout' qualifier may not be used with "
21574bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick			  "`attribute' or `varying'");
21584bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick      }
21594bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick   }
21604bcff0c19091c7df2b2e0bafe58addb5bae28f1aIan Romanick
2161bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   /* Layout qualifiers for gl_FragDepth, which are enabled by extension
2162bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace    * AMD_conservative_depth.
2163bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace    */
2164bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   int depth_layout_count = qual->flags.q.depth_any
2165bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      + qual->flags.q.depth_greater
2166bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      + qual->flags.q.depth_less
2167bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      + qual->flags.q.depth_unchanged;
2168bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   if (depth_layout_count > 0
21696b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák       && !state->AMD_conservative_depth_enable
21706b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák       && !state->ARB_conservative_depth_enable) {
2171bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace       _mesa_glsl_error(loc, state,
21726b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák                        "extension GL_AMD_conservative_depth or "
21736b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák                        "GL_ARB_conservative_depth must be enabled "
2174bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace			"to use depth layout qualifiers");
2175bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   } else if (depth_layout_count > 0
2176bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace              && strcmp(var->name, "gl_FragDepth") != 0) {
2177bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace       _mesa_glsl_error(loc, state,
2178bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace                        "depth layout qualifiers can be applied only to "
2179bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace                        "gl_FragDepth");
2180bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   } else if (depth_layout_count > 1
2181bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace              && strcmp(var->name, "gl_FragDepth") == 0) {
2182bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      _mesa_glsl_error(loc, state,
2183bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace                       "at most one depth layout qualifier can be applied to "
2184bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace                       "gl_FragDepth");
2185bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   }
2186bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   if (qual->flags.q.depth_any)
2187bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      var->depth_layout = ir_depth_layout_any;
2188bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   else if (qual->flags.q.depth_greater)
2189bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      var->depth_layout = ir_depth_layout_greater;
2190bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   else if (qual->flags.q.depth_less)
2191bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace      var->depth_layout = ir_depth_layout_less;
2192bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   else if (qual->flags.q.depth_unchanged)
2193bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace       var->depth_layout = ir_depth_layout_unchanged;
2194bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace   else
2195bc04d244f5a86fd7085e3d648949413e2d2ec797Chad Versace       var->depth_layout = ir_depth_layout_none;
2196f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt
2197f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt   if (qual->flags.q.std140 ||
2198f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt       qual->flags.q.packed ||
2199f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt       qual->flags.q.shared) {
2200f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt      _mesa_glsl_error(loc, state,
2201f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt                       "uniform block layout qualifiers std140, packed, and "
2202f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt		       "shared can only be applied to uniform blocks, not "
2203f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt		       "members");
2204f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt   }
2205f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt
2206f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt   if (!ubo_qualifiers_valid &&
2207f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt       (qual->flags.q.row_major || qual->flags.q.column_major)) {
2208f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt      _mesa_glsl_error(loc, state,
2209f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt                       "uniform block layout qualifiers row_major and "
2210f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt		       "column_major can only be applied to uniform block "
2211f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt		       "members");
2212f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt   }
2213a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2214a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
22158e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick/**
22168e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * Get the variable that is being redeclared by this declaration
22178e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick *
22188e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * Semantic checks to verify the validity of the redeclaration are also
22198e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * performed.  If semantic checks fail, compilation error will be emitted via
22208e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
22218e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick *
22228e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * \returns
22238e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * A pointer to an existing variable in the current scope if the declaration
22248e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * is a redeclaration, \c NULL otherwise.
22258e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick */
22268e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanickir_variable *
22278e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanickget_variable_being_redeclared(ir_variable *var, ast_declaration *decl,
22288e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			      struct _mesa_glsl_parse_state *state)
22298e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick{
22308e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   /* Check if this declaration is actually a re-declaration, either to
22318e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    * resize an array or add qualifiers to an existing variable.
22328e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    *
22338e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    * This is allowed for variables in the current scope, or when at
22348e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    * global scope (for built-ins in the implicit outer scope).
22358e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    */
22368e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   ir_variable *earlier = state->symbols->get_variable(decl->identifier);
22378e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   if (earlier == NULL ||
22388e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       (state->current_function != NULL &&
22398e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	!state->symbols->name_declared_this_scope(decl->identifier))) {
22408e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      return NULL;
22418e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   }
22428e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22438e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22448e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   YYLTYPE loc = decl->get_location();
22458e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22468e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
22478e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    *
22488e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    * "It is legal to declare an array without a size and then
22498e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    *  later re-declare the same name as an array of the same
22508e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    *  type and specify a size."
22518e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    */
22528e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   if ((earlier->type->array_size() == 0)
22538e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       && var->type->is_array()
22548e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       && (var->type->element_type() == earlier->type->element_type())) {
22558e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* FINISHME: This doesn't match the qualifiers on the two
22568e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * FINISHME: declarations.  It's not 100% clear whether this is
22578e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * FINISHME: required or not.
22588e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       */
22598e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22608e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      const unsigned size = unsigned(var->type->array_size());
226193b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      check_builtin_array_max_size(var->name, size, loc, state);
226293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      if ((size > 0) && (size <= earlier->max_array_access)) {
22638e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
22648e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "previous access",
22658e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  earlier->max_array_access);
22668e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      }
22678e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22688e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->type = var->type;
22698e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      delete var;
22708e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      var = NULL;
22718e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   } else if (state->ARB_fragment_coord_conventions_enable
22728e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && strcmp(var->name, "gl_FragCoord") == 0
22738e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->type == var->type
22748e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->mode == var->mode) {
22758e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
22768e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * qualifiers.
22778e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       */
22788e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->origin_upper_left = var->origin_upper_left;
22798e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->pixel_center_integer = var->pixel_center_integer;
22808e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22818e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* According to section 4.3.7 of the GLSL 1.30 spec,
22828e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * the following built-in varaibles can be redeclared with an
22838e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * interpolation qualifier:
22848e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_FrontColor
22858e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_BackColor
22868e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_FrontSecondaryColor
22878e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_BackSecondaryColor
22888e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_Color
22898e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_SecondaryColor
22908e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       */
22918e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   } else if (state->language_version >= 130
22928e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && (strcmp(var->name, "gl_FrontColor") == 0
22938e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_BackColor") == 0
22948e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_FrontSecondaryColor") == 0
22958e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_BackSecondaryColor") == 0
22968e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_Color") == 0
22978e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_SecondaryColor") == 0)
22988e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->type == var->type
22998e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->mode == var->mode) {
23008e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->interpolation = var->interpolation;
23018e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
23028e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* Layout qualifiers for gl_FragDepth. */
23036b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák   } else if ((state->AMD_conservative_depth_enable ||
23046b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák               state->ARB_conservative_depth_enable)
23058e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && strcmp(var->name, "gl_FragDepth") == 0
23068e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->type == var->type
23078e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->mode == var->mode) {
23088e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
23098e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /** From the AMD_conservative_depth spec:
23108e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *     Within any shader, the first redeclarations of gl_FragDepth
23118e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *     must appear before any use of gl_FragDepth.
23128e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       */
23138e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      if (earlier->used) {
23148e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	 _mesa_glsl_error(&loc, state,
23158e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "the first redeclaration of gl_FragDepth "
23168e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "must appear before any use of gl_FragDepth");
23178e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      }
23188e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
23198e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* Prevent inconsistent redeclaration of depth layout qualifier. */
23208e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      if (earlier->depth_layout != ir_depth_layout_none
23218e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	  && earlier->depth_layout != var->depth_layout) {
23228e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	 _mesa_glsl_error(&loc, state,
23238e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "gl_FragDepth: depth layout is declared here "
23248e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "as '%s, but it was previously declared as "
23258e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "'%s'",
23268e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  depth_layout_string(var->depth_layout),
23278e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  depth_layout_string(earlier->depth_layout));
23288e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      }
23298e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
23308e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->depth_layout = var->depth_layout;
23318e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
23328e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   } else {
23338e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
23348e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   }
23358e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
23368e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   return earlier;
23378e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick}
2338a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
23390292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick/**
23400292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * Generate the IR for an initializer in a variable declaration
23410292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick */
23420292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanickir_rvalue *
23430292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanickprocess_initializer(ir_variable *var, ast_declaration *decl,
23440292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		    ast_fully_specified_type *type,
23450292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		    exec_list *initializer_instructions,
23460292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		    struct _mesa_glsl_parse_state *state)
23470292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick{
23480292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   ir_rvalue *result = NULL;
23490292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23500292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   YYLTYPE initializer_loc = decl->initializer->get_location();
23510292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23520292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
23530292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    *
23540292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    *    "All uniform variables are read-only and are initialized either
23550292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    *    directly by an application via API commands, or indirectly by
23560292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    *    OpenGL."
23570292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    */
23580292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if ((state->language_version <= 110)
23590292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       && (var->mode == ir_var_uniform)) {
23600292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      _mesa_glsl_error(& initializer_loc, state,
23610292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       "cannot initialize uniforms in GLSL 1.10");
23620292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
23630292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23640292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if (var->type->is_sampler()) {
23650292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      _mesa_glsl_error(& initializer_loc, state,
23660292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       "cannot initialize samplers");
23670292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
23680292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23690292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
23700292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      _mesa_glsl_error(& initializer_loc, state,
23710292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       "cannot initialize %s shader input / %s",
23720292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       _mesa_glsl_shader_target_name(state->target),
23730292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       (state->target == vertex_shader)
23740292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       ? "attribute" : "varying");
23750292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
23760292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23770292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   ir_dereference *const lhs = new(state) ir_dereference_variable(var);
23780292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   ir_rvalue *rhs = decl->initializer->hir(initializer_instructions,
23790292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick					   state);
23800292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23810292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   /* Calculate the constant value if this is a const or uniform
23820292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    * declaration.
23830292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    */
23840292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if (type->qualifier.flags.q.constant
23850292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       || type->qualifier.flags.q.uniform) {
238685caea29c18fad89050ac366c558afef568dcb3fIan Romanick      ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs, true);
23870292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      if (new_rhs != NULL) {
23880292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 rhs = new_rhs;
23890292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23900292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 ir_constant *constant_value = rhs->constant_expression_value();
23910292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 if (!constant_value) {
23920292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    _mesa_glsl_error(& initializer_loc, state,
23930292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     "initializer of %s variable `%s' must be a "
23940292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     "constant expression",
23950292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     (type->qualifier.flags.q.constant)
23960292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     ? "const" : "uniform",
23970292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     decl->identifier);
23980292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    if (var->type->is_numeric()) {
23990292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	       /* Reduce cascading errors. */
24000292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	       var->constant_value = ir_constant::zero(state, var->type);
24010292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    }
24020292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 } else {
24030292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    rhs = constant_value;
24040292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    var->constant_value = constant_value;
24050292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 }
24060292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      } else {
24070292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 _mesa_glsl_error(&initializer_loc, state,
24080292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			  "initializer of type %s cannot be assigned to "
24090292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			  "variable of type %s",
24100292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			  rhs->type->name, var->type->name);
24110292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 if (var->type->is_numeric()) {
24120292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    /* Reduce cascading errors. */
24130292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    var->constant_value = ir_constant::zero(state, var->type);
24140292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 }
24150292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      }
24160292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
24170292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
24180292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if (rhs && !rhs->type->is_error()) {
24190292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      bool temp = var->read_only;
24200292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      if (type->qualifier.flags.q.constant)
24210292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 var->read_only = false;
24220292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
24230292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      /* Never emit code to initialize a uniform.
24240292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       */
24250292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      const glsl_type *initializer_type;
24260292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      if (!type->qualifier.flags.q.uniform) {
24270292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 result = do_assignment(initializer_instructions, state,
2428e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick				NULL,
242985caea29c18fad89050ac366c558afef568dcb3fIan Romanick				lhs, rhs, true,
24300292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick				type->get_location());
24310292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 initializer_type = result->type;
24320292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      } else
24330292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 initializer_type = rhs->type;
24340292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
2435f37b1ad937dd2c420f4c9fd9aa5887942bd31f3fIan Romanick      var->constant_initializer = rhs->constant_expression_value();
2436f37b1ad937dd2c420f4c9fd9aa5887942bd31f3fIan Romanick      var->has_initializer = true;
2437f37b1ad937dd2c420f4c9fd9aa5887942bd31f3fIan Romanick
24380292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      /* If the declared variable is an unsized array, it must inherrit
24390292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * its full type from the initializer.  A declaration such as
24400292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24410292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *     uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
24420292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24430292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * becomes
24440292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24450292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *     uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
24460292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24470292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * The assignment generated in the if-statement (below) will also
24480292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * automatically handle this case for non-uniforms.
24490292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24500292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * If the declared variable is not an array, the types must
24510292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * already match exactly.  As a result, the type assignment
24520292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * here can be done unconditionally.  For non-uniforms the call
24530292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * to do_assignment can change the type of the initializer (via
24540292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * the implicit conversion rules).  For uniforms the initializer
24550292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * must be a constant expression, and the type of that expression
24560292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * was validated above.
24570292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       */
24580292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      var->type = initializer_type;
24590292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
24600292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      var->read_only = temp;
24610292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
24620292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
24630292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   return result;
24640292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick}
24650292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
2466fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
24670044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions,
246818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			 struct _mesa_glsl_parse_state *state)
2469a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2470953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
2471a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *decl_type;
2472a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *type_name = NULL;
24738558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   ir_rvalue *result = NULL;
2474c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   YYLTYPE loc = this->get_location();
2475a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
24766f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
24776f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
24786f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     "To ensure that a particular output variable is invariant, it is
24796f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     necessary to use the invariant qualifier. It can either be used to
24806f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     qualify a previously declared variable as being invariant
24816f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
24826f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *         invariant gl_Position; // make existing gl_Position be invariant"
24836f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
24846f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * In these cases the parser will set the 'invariant' flag in the declarator
24856f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * list, and the type will be NULL.
24866f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    */
24876f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   if (this->invariant) {
24886f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      assert(this->type == NULL);
24896f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
24906f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (state->current_function != NULL) {
24916f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 _mesa_glsl_error(& loc, state,
24926f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "All uses of `invariant' keyword must be at global "
24936f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "scope\n");
24946f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
24956f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
24966f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
24976f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(!decl->is_array);
24986f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->array_size == NULL);
24996f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->initializer == NULL);
25006f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
25016f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 ir_variable *const earlier =
25026f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    state->symbols->get_variable(decl->identifier);
25036f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 if (earlier == NULL) {
25046f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
25056f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "Undeclared variable `%s' cannot be marked "
25066f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "invariant\n", decl->identifier);
25076f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == vertex_shader)
25086f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_out)) {
25096f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
25106f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
25116f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", decl->identifier);
25126f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == fragment_shader)
25136f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_in)) {
25146f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
25156f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
25166f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", decl->identifier);
2517bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	 } else if (earlier->used) {
2518bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	    _mesa_glsl_error(& loc, state,
2519bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			     "variable `%s' may not be redeclared "
2520bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			     "`invariant' after being used",
2521bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			     earlier->name);
25226f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else {
25236f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    earlier->invariant = true;
25246f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
25256f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
25266f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
25276f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      /* Invariant redeclarations do not have r-values.
25286f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       */
25296f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      return NULL;
25306f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   }
25316f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
25326f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(this->type != NULL);
25336f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(!this->invariant);
25346f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
25353455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* The type specifier may contain a structure definition.  Process that
25363455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * before any of the variable declarations.
25373455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
25383455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   (void) this->type->specifier->hir(instructions, state);
25393455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
2540d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   decl_type = this->type->specifier->glsl_type(& type_name, state);
2541304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick   if (this->declarations.is_empty()) {
2542f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick      /* If there is no structure involved in the program text, there are two
2543f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * possible scenarios:
2544f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *
2545f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * - The program text contained something like 'vec4;'.  This is an
2546f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *   empty declaration.  It is valid but weird.  Emit a warning.
2547f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *
2548f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * - The program text contained something like 'S;' and 'S' is not the
2549f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *   name of a known structure type.  This is both invalid and weird.
2550f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *   Emit an error.
2551f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *
2552f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * Note that if decl_type is NULL and there is a structure involved,
2553f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * there must have been some sort of error with the structure.  In this
2554f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * case we assume that an error was already generated on this line of
2555f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * code for the structure.  There is no need to generate an additional,
2556f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * confusing error.
2557f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       */
2558f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick      assert(this->type->specifier->structure == NULL || decl_type != NULL
2559f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick	     || state->error);
2560f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick      if (this->type->specifier->structure == NULL) {
2561f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick	 if (decl_type != NULL) {
2562547212d963c70161915c46d64e8020617199fb8dChia-I Wu	    _mesa_glsl_warning(&loc, state, "empty declaration");
2563f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick	 } else {
2564f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick	    _mesa_glsl_error(&loc, state,
2565f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick			     "invalid type `%s' in empty declaration",
2566f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick			     type_name);
2567547212d963c70161915c46d64e8020617199fb8dChia-I Wu	 }
2568c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      }
2569c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   }
2570a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
25712b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
2572a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      const struct glsl_type *var_type;
2573768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_variable *var;
2574a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2575a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Emit a warning if a variable declaration shadows a
2576a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: declaration at a higher scope.
2577a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
2578a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2579cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick      if ((decl_type == NULL) || decl_type->is_void()) {
2580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (type_name != NULL) {
2581a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
2582a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type `%s' in declaration of `%s'",
2583a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     type_name, decl->identifier);
2584a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 } else {
2585a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
2586a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type in declaration of `%s'",
2587a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     decl->identifier);
2588a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
2589a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
2590a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2591a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2592a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (decl->is_array) {
2593d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 var_type = process_array_type(&loc, decl_type, decl->array_size,
2594d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke				       state);
2595a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick	 if (var_type->is_error())
2596a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick	    continue;
2597a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
2598a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 var_type = decl_type;
2599a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2600a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
26017e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick      var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
2602a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
26033f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
26043f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
26053f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     "Global variables can only use the qualifiers const,
26063f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     attribute, uni form, or varying. Only one may be
26073f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     specified.
26083f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
26093f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     Local variables can only use the qualifier const."
26103f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
261182c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick       * This is relaxed in GLSL 1.30.  It is also relaxed by any extension
261282c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick       * that adds the 'layout' keyword.
26133f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       */
261482c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick      if ((state->language_version < 130)
261582c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick	  && !state->ARB_explicit_attrib_location_enable
261682c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick	  && !state->ARB_fragment_coord_conventions_enable) {
2617e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.out) {
26183f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
26193f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`out' qualifier in declaration of `%s' "
2620469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick			     "only valid for function parameters in %s.",
2621469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick			     decl->identifier, state->version_string);
26223f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
2623e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.in) {
26243f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
26253f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`in' qualifier in declaration of `%s' "
2626469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick			     "only valid for function parameters in %s.",
2627469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick			     decl->identifier, state->version_string);
26283f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
26293f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 /* FINISHME: Test for other invalid qualifiers. */
26303f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      }
26313f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt
26322e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
2633f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt				       & loc, this->ubo_qualifiers_valid);
2634a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2635e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick      if (this->type->qualifier.flags.q.invariant) {
2636046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
2637046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt						   var->mode == ir_var_inout)) {
2638046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
2639046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature outval
2640046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
26416f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
26426f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
26436f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", var->name);
2644046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 } else if ((state->target == fragment_shader) &&
2645046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt		    !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
2646046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
2647046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature inval
2648046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
26496f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
26506f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
26516f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", var->name);
26526f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
26536f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
26546f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
2655e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      if (state->current_function != NULL) {
2656b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 const char *mode = NULL;
2657e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 const char *extra = "";
2658b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
2659e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 /* There is no need to check for 'inout' here because the parser will
2660e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	  * only allow that in function parameter lists.
2661e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	  */
2662e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.attribute) {
2663b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "attribute";
2664e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.uniform) {
2665b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "uniform";
2666e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.varying) {
2667b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "varying";
2668e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.in) {
2669e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "in";
2670e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
2671e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.out) {
2672e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "out";
2673e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
2674b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 }
2675b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
2676b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 if (mode) {
2677e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	    _mesa_glsl_error(& loc, state,
2678b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick			     "%s variable `%s' must be declared at "
2679e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     "global scope%s",
2680e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     mode, var->name, extra);
2681e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 }
2682e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      } else if (var->mode == ir_var_in) {
268301a584d09350d2c726312e2c9e88c5dbc54bdb70Chad Versace         var->read_only = true;
268401a584d09350d2c726312e2c9e88c5dbc54bdb70Chad Versace
2685fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 if (state->target == vertex_shader) {
2686fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    bool error_emitted = false;
2687fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
2688fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2689fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
2690fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
2691fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
2692fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors. Vertex shader inputs can also form arrays of these
2693fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    types, but not structures."
2694fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
26952d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
26962d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
26972d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
26982d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
26992d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors. They cannot be arrays or structures."
27002d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
2701fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
2702fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
2703fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "The attribute qualifier can be used only with float,
2704fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    floating-point vectors, and matrices. Attribute variables
2705fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    cannot be declared as arrays or structures."
2706fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     */
2707fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    const glsl_type *check_type = var->type->is_array()
2708fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       ? var->type->fields.array : var->type;
2709fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
2710fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    switch (check_type->base_type) {
2711fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_FLOAT:
2712fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       break;
2713fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_UINT:
2714fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_INT:
2715fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       if (state->language_version > 120)
2716fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		  break;
2717fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       /* FALLTHROUGH */
2718fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    default:
2719fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
2720fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
2721fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"type %s`%s'",
2722fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				var->type->is_array() ? "array of " : "",
2723fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				check_type->name);
2724fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
2725fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
2726fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
27272d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    if (!error_emitted && (state->language_version <= 130)
2728fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		&& var->type->is_array()) {
2729fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
2730fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
2731fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"array type");
2732fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
2733fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
2734fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 }
2735fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick      }
2736fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
273768d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace      /* Integer vertex outputs must be qualified with 'flat'.
273868d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       *
273968d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       * From section 4.3.6 of the GLSL 1.30 spec:
274068d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       *    "If a vertex output is a signed or unsigned integer or integer
274168d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       *    vector, then it must be qualified with the interpolation qualifier
274268d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       *    flat."
274368d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       */
274468d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace      if (state->language_version >= 130
274568d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace          && state->target == vertex_shader
274668d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace          && state->current_function == NULL
274768d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace          && var->type->is_integer()
274868d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace          && var->mode == ir_var_out
2749cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry          && var->interpolation != INTERP_QUALIFIER_FLAT) {
275068d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace
275168d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace         _mesa_glsl_error(&loc, state, "If a vertex output is an integer, "
275268d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace                          "then it must be qualified with 'flat'");
275368d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace      }
275468d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace
275568d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace
2756605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace      /* Interpolation qualifiers cannot be applied to 'centroid' and
2757605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       * 'centroid varying'.
2758605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       *
2759605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
2760605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       *    "interpolation qualifiers may only precede the qualifiers in,
2761605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       *    centroid in, out, or centroid out in a declaration. They do not apply
2762605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       *    to the deprecated storage qualifiers varying or centroid varying."
2763605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       */
2764605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace      if (state->language_version >= 130
2765605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace          && this->type->qualifier.has_interpolation()
2766605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace          && this->type->qualifier.flags.q.varying) {
2767605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace
2768605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         const char *i = this->type->qualifier.interpolation_string();
2769605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         assert(i != NULL);
2770605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         const char *s;
2771605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         if (this->type->qualifier.flags.q.centroid)
2772605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace            s = "centroid varying";
2773605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         else
2774605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace            s = "varying";
2775605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace
2776605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         _mesa_glsl_error(&loc, state,
2777605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace                          "qualifier '%s' cannot be applied to the "
2778605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace                          "deprecated storage qualifier '%s'", i, s);
2779605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace      }
2780605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace
2781605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace
27828faaa4a672c1062e486eda2525287715b554342dChad Versace      /* Interpolation qualifiers can only apply to vertex shader outputs and
27838faaa4a672c1062e486eda2525287715b554342dChad Versace       * fragment shader inputs.
27848faaa4a672c1062e486eda2525287715b554342dChad Versace       *
27858faaa4a672c1062e486eda2525287715b554342dChad Versace       * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
27868faaa4a672c1062e486eda2525287715b554342dChad Versace       *    "Outputs from a vertex shader (out) and inputs to a fragment
27878faaa4a672c1062e486eda2525287715b554342dChad Versace       *    shader (in) can be further qualified with one or more of these
27888faaa4a672c1062e486eda2525287715b554342dChad Versace       *    interpolation qualifiers"
27898faaa4a672c1062e486eda2525287715b554342dChad Versace       */
27908faaa4a672c1062e486eda2525287715b554342dChad Versace      if (state->language_version >= 130
27918faaa4a672c1062e486eda2525287715b554342dChad Versace          && this->type->qualifier.has_interpolation()) {
27928faaa4a672c1062e486eda2525287715b554342dChad Versace
27938faaa4a672c1062e486eda2525287715b554342dChad Versace         const char *i = this->type->qualifier.interpolation_string();
27948faaa4a672c1062e486eda2525287715b554342dChad Versace         assert(i != NULL);
27958faaa4a672c1062e486eda2525287715b554342dChad Versace
27968faaa4a672c1062e486eda2525287715b554342dChad Versace         switch (state->target) {
27978faaa4a672c1062e486eda2525287715b554342dChad Versace         case vertex_shader:
27988faaa4a672c1062e486eda2525287715b554342dChad Versace            if (this->type->qualifier.flags.q.in) {
27998faaa4a672c1062e486eda2525287715b554342dChad Versace               _mesa_glsl_error(&loc, state,
28008faaa4a672c1062e486eda2525287715b554342dChad Versace                                "qualifier '%s' cannot be applied to vertex "
28018faaa4a672c1062e486eda2525287715b554342dChad Versace                                "shader inputs", i);
28028faaa4a672c1062e486eda2525287715b554342dChad Versace            }
28038faaa4a672c1062e486eda2525287715b554342dChad Versace            break;
28048faaa4a672c1062e486eda2525287715b554342dChad Versace         case fragment_shader:
28058faaa4a672c1062e486eda2525287715b554342dChad Versace            if (this->type->qualifier.flags.q.out) {
28068faaa4a672c1062e486eda2525287715b554342dChad Versace               _mesa_glsl_error(&loc, state,
28078faaa4a672c1062e486eda2525287715b554342dChad Versace                                "qualifier '%s' cannot be applied to fragment "
28088faaa4a672c1062e486eda2525287715b554342dChad Versace                                "shader outputs", i);
28098faaa4a672c1062e486eda2525287715b554342dChad Versace            }
28108faaa4a672c1062e486eda2525287715b554342dChad Versace            break;
28118faaa4a672c1062e486eda2525287715b554342dChad Versace         default:
28128faaa4a672c1062e486eda2525287715b554342dChad Versace            assert(0);
28138faaa4a672c1062e486eda2525287715b554342dChad Versace         }
28148faaa4a672c1062e486eda2525287715b554342dChad Versace      }
28158faaa4a672c1062e486eda2525287715b554342dChad Versace
28168faaa4a672c1062e486eda2525287715b554342dChad Versace
28171eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace      /* From section 4.3.4 of the GLSL 1.30 spec:
28181eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace       *    "It is an error to use centroid in in a vertex shader."
28191eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace       */
28201eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace      if (state->language_version >= 130
28211eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace          && this->type->qualifier.flags.q.centroid
28221eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace          && this->type->qualifier.flags.q.in
28231eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace          && state->target == vertex_shader) {
28241eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace
28251eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace         _mesa_glsl_error(&loc, state,
28261eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace                          "'centroid in' cannot be used in a vertex shader");
28271eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace      }
28281eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace
28291eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace
2830889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
2831889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       */
2832889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      if (this->type->specifier->precision != ast_precision_none
2833889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace          && state->language_version != 100
2834889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace          && state->language_version < 130) {
2835889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
2836889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace         _mesa_glsl_error(&loc, state,
2837889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace                          "precision qualifiers are supported only in GLSL ES "
2838889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace                          "1.00, and GLSL 1.30 and later");
2839889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      }
2840889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
2841889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
284245e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace      /* Precision qualifiers only apply to floating point and integer types.
2843889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *
2844889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       * From section 4.5.2 of the GLSL 1.30 spec:
2845889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *    "Any floating point or any integer declaration can have the type
2846889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *    preceded by one of these precision qualifiers [...] Literal
2847889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *    constants do not have precision qualifiers. Neither do Boolean
2848889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *    variables.
28498752824f27c979986ae855667337e89637b005fbKenneth Graunke       *
28508752824f27c979986ae855667337e89637b005fbKenneth Graunke       * In GLSL ES, sampler types are also allowed.
28518752824f27c979986ae855667337e89637b005fbKenneth Graunke       *
28528752824f27c979986ae855667337e89637b005fbKenneth Graunke       * From page 87 of the GLSL ES spec:
28538752824f27c979986ae855667337e89637b005fbKenneth Graunke       *    "RESOLUTION: Allow sampler types to take a precision qualifier."
2854889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       */
2855889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      if (this->type->specifier->precision != ast_precision_none
285645e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace          && !var->type->is_float()
285745e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace          && !var->type->is_integer()
28588752824f27c979986ae855667337e89637b005fbKenneth Graunke          && !(var->type->is_sampler() && state->es_shader)
285945e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace          && !(var->type->is_array()
286045e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace               && (var->type->fields.array->is_float()
286145e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace                   || var->type->fields.array->is_integer()))) {
2862889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
2863889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace         _mesa_glsl_error(&loc, state,
28648752824f27c979986ae855667337e89637b005fbKenneth Graunke                          "precision qualifiers apply only to floating point"
28658752824f27c979986ae855667337e89637b005fbKenneth Graunke                          "%s types", state->es_shader ? ", integer, and sampler"
28668752824f27c979986ae855667337e89637b005fbKenneth Graunke						       : "and integer");
2867889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      }
2868889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
2869f07221056e1822187546b76387714b3172f9b2c5Paul Berry      /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
2870f07221056e1822187546b76387714b3172f9b2c5Paul Berry       *
2871f07221056e1822187546b76387714b3172f9b2c5Paul Berry       *    "[Sampler types] can only be declared as function
2872f07221056e1822187546b76387714b3172f9b2c5Paul Berry       *    parameters or uniform variables (see Section 4.3.5
2873f07221056e1822187546b76387714b3172f9b2c5Paul Berry       *    "Uniform")".
2874f07221056e1822187546b76387714b3172f9b2c5Paul Berry       */
2875f07221056e1822187546b76387714b3172f9b2c5Paul Berry      if (var_type->contains_sampler() &&
2876f07221056e1822187546b76387714b3172f9b2c5Paul Berry          !this->type->qualifier.flags.q.uniform) {
2877f07221056e1822187546b76387714b3172f9b2c5Paul Berry         _mesa_glsl_error(&loc, state, "samplers must be declared uniform");
2878f07221056e1822187546b76387714b3172f9b2c5Paul Berry      }
2879f07221056e1822187546b76387714b3172f9b2c5Paul Berry
2880e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick      /* Process the initializer and add its instructions to a temporary
2881e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * list.  This list will be added to the instruction stream (below) after
2882e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * the declaration is added.  This is done because in some cases (such as
2883e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * redeclarations) the declaration may not actually be added to the
2884e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * instruction stream.
2885e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       */
2886fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      exec_list initializer_instructions;
288709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick      ir_variable *earlier = get_variable_being_redeclared(var, decl, state);
288809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick
288966faec4895b7bb59a614087a200c05157191b4aeIan Romanick      if (decl->initializer != NULL) {
289009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 result = process_initializer((earlier == NULL) ? var : earlier,
289109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick				      decl, this->type,
28920292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick				      &initializer_instructions, state);
289366faec4895b7bb59a614087a200c05157191b4aeIan Romanick      }
289417d86f4371da413176ba365ca26a58bac172d365Ian Romanick
28950ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
28960ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *
28970ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *     "It is an error to write to a const variable outside of
28980ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      its declaration, so they must be initialized when
28990ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      declared."
29000ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       */
2901e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick      if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
29020ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	 _mesa_glsl_error(& loc, state,
290346f7105df487c91569f7e4a8da74d673c12e5619Chad Versace			  "const declaration of `%s' must be initialized",
290446f7105df487c91569f7e4a8da74d673c12e5619Chad Versace			  decl->identifier);
29050ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      }
29060ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt
290709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick      /* If the declaration is not a redeclaration, there are a few additional
290809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick       * semantic checks that must be applied.  In addition, variable that was
290909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick       * created for the declaration should be added to the IR stream.
29105466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       */
291109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick      if (earlier == NULL) {
291209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
291309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *
291409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *   "Identifiers starting with "gl_" are reserved for use by
291509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *   OpenGL, and may not be declared in a shader as either a
291609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *   variable or a function."
291709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  */
291809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 if (strncmp(decl->identifier, "gl_", 3) == 0)
291909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	    _mesa_glsl_error(& loc, state,
292009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick			     "identifier `%s' uses reserved `gl_' prefix",
292109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick			     decl->identifier);
2922c475a54578bf5473c6c62bc5468ef4fe555164d7Jason Wood	 else if (strstr(decl->identifier, "__")) {
2923684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	    /* From page 14 (page 20 of the PDF) of the GLSL 1.10
2924684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     * spec:
2925684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     *
2926684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     *     "In addition, all identifiers containing two
2927684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     *      consecutive underscores (__) are reserved as
2928684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     *      possible future keywords."
2929684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     */
2930684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	    _mesa_glsl_error(& loc, state,
2931684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt			     "identifier `%s' uses reserved `__' string",
2932684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt			     decl->identifier);
2933684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	 }
29345466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
293509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 /* Add the variable to the symbol table.  Note that the initializer's
293609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * IR was already processed earlier (though it hasn't been emitted
293709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * yet), without the variable in scope.
293809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *
293909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * This differs from most C-like languages, but it follows the GLSL
294009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
294109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * spec:
294209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *
294309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *     "Within a declaration, the scope of a name starts immediately
294409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *     after the initializer if present or immediately after the name
294509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *     being declared if not."
294609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  */
294709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 if (!state->symbols->add_variable(var)) {
294809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	    YYLTYPE loc = this->get_location();
294909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	    _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
295009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick			     "current scope", decl->identifier);
295109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	    continue;
295209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 }
295309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick
295409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 /* Push the variable declaration to the top.  It means that all the
295509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * variable declarations will appear in a funny last-to-first order,
295609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * but otherwise we run into trouble if a function is prototyped, a
295709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * global var is decled, then the function is defined with usage of
295809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * the global var.  See glslparsertest's CorrectModule.frag.
295909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  */
296009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 instructions->push_head(var);
29615d25746640ee27882b69a962459727cf924443dbKenneth Graunke      }
29625d25746640ee27882b69a962459727cf924443dbKenneth Graunke
2963fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      instructions->append_list(&initializer_instructions);
2964a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2965a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
29668558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt
29678558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   /* Generally, variable declarations do not have r-values.  However,
29688558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * one is used for the declaration in
29698558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
29708558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * while (bool b = some_condition()) {
29718558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *   ...
29728558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * }
29738558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
29748558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * so we return the rvalue from the last seen declaration here.
2975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
29768558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   return result;
2977a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2978a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2979a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2980fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
29810044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions,
298218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
2983a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2984953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
2985a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *type;
2986a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *name = NULL;
29872e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   YYLTYPE loc = this->get_location();
2988a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2989d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   type = this->type->specifier->glsl_type(& name, state);
2990a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2991a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type == NULL) {
2992a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (name != NULL) {
2993a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2994a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type `%s' in declaration of `%s'",
299518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  name, this->identifier);
2996a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
2997a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2998a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type in declaration of `%s'",
299918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->identifier);
3000a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
3001a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
30020471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::error_type;
3003a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3004a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3005068c80cfe0a280490353b6b007165d717c672eedEric Anholt   /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
3006068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
3007068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    "Functions that accept no input arguments need not use void in the
3008068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    argument list because prototypes (or definitions) are required and
3009068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    therefore there is no ambiguity when an empty argument list "( )" is
3010068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    declared. The idiom "(void)" as a parameter list is provided for
3011068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    convenience."
3012068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
3013068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * Placing this check here prevents a void parameter being set up
3014068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * for a function, which avoids tripping up checks for main taking
3015068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * parameters and lookups of an unnamed symbol.
3016068c80cfe0a280490353b6b007165d717c672eedEric Anholt    */
3017cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if (type->is_void()) {
3018cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (this->identifier != NULL)
3019cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 _mesa_glsl_error(& loc, state,
3020cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick			  "named parameter cannot have type `void'");
3021cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3022cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      is_void = true;
3023068c80cfe0a280490353b6b007165d717c672eedEric Anholt      return NULL;
3024cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
3025068c80cfe0a280490353b6b007165d717c672eedEric Anholt
302645d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   if (formal_parameter && (this->identifier == NULL)) {
302745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
302845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      return NULL;
302945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   }
303045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick
3031e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   /* This only handles "vec4 foo[..]".  The earlier specifier->glsl_type(...)
3032e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    * call already handled the "vec4[..] foo" case.
3033e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    */
3034e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   if (this->is_array) {
3035d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke      type = process_array_type(&loc, type, this->array_size, state);
3036e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
3037e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
3038a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick   if (!type->is_error() && type->array_size() == 0) {
3039e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
3040e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke		       "a declared size.");
3041e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      type = glsl_type::error_type;
3042e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
3043e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
3044cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   is_void = false;
30457e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
3046a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3047cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   /* Apply any specified qualifiers to the parameter declaration.  Note that
3048cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    * for function parameters the default mode is 'in'.
3049cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    */
3050f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc,
3051f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt				    false);
3052a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3053f07221056e1822187546b76387714b3172f9b2c5Paul Berry   /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3054f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *
3055f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    "Samplers cannot be treated as l-values; hence cannot be used
3056f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    as out or inout function parameters, nor can they be assigned
3057f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    into."
3058f07221056e1822187546b76387714b3172f9b2c5Paul Berry    */
3059f07221056e1822187546b76387714b3172f9b2c5Paul Berry   if ((var->mode == ir_var_inout || var->mode == ir_var_out)
3060f07221056e1822187546b76387714b3172f9b2c5Paul Berry       && type->contains_sampler()) {
3061f07221056e1822187546b76387714b3172f9b2c5Paul Berry      _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers");
3062f07221056e1822187546b76387714b3172f9b2c5Paul Berry      type = glsl_type::error_type;
3063f07221056e1822187546b76387714b3172f9b2c5Paul Berry   }
3064f07221056e1822187546b76387714b3172f9b2c5Paul Berry
306500792e3586746c833ffc9bb65712e38038916e06Paul Berry   /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
306600792e3586746c833ffc9bb65712e38038916e06Paul Berry    *
306700792e3586746c833ffc9bb65712e38038916e06Paul Berry    *    "When calling a function, expressions that do not evaluate to
306800792e3586746c833ffc9bb65712e38038916e06Paul Berry    *     l-values cannot be passed to parameters declared as out or inout."
306900792e3586746c833ffc9bb65712e38038916e06Paul Berry    *
307000792e3586746c833ffc9bb65712e38038916e06Paul Berry    * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
307100792e3586746c833ffc9bb65712e38038916e06Paul Berry    *
307200792e3586746c833ffc9bb65712e38038916e06Paul Berry    *    "Other binary or unary expressions, non-dereferenced arrays,
307300792e3586746c833ffc9bb65712e38038916e06Paul Berry    *     function names, swizzles with repeated fields, and constants
307400792e3586746c833ffc9bb65712e38038916e06Paul Berry    *     cannot be l-values."
307500792e3586746c833ffc9bb65712e38038916e06Paul Berry    *
307600792e3586746c833ffc9bb65712e38038916e06Paul Berry    * So for GLSL 1.10, passing an array as an out or inout parameter is not
307700792e3586746c833ffc9bb65712e38038916e06Paul Berry    * allowed.  This restriction is removed in GLSL 1.20, and in GLSL ES.
307800792e3586746c833ffc9bb65712e38038916e06Paul Berry    */
307900792e3586746c833ffc9bb65712e38038916e06Paul Berry   if ((var->mode == ir_var_inout || var->mode == ir_var_out)
308000792e3586746c833ffc9bb65712e38038916e06Paul Berry       && type->is_array() && state->language_version == 110) {
308100792e3586746c833ffc9bb65712e38038916e06Paul Berry      _mesa_glsl_error(&loc, state, "Arrays cannot be out or inout parameters in GLSL 1.10");
308200792e3586746c833ffc9bb65712e38038916e06Paul Berry      type = glsl_type::error_type;
308300792e3586746c833ffc9bb65712e38038916e06Paul Berry   }
308400792e3586746c833ffc9bb65712e38038916e06Paul Berry
30850044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(var);
3086a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3087a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Parameter declarations do not have r-values.
3088a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3089a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
3090a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
3091a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3092a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
309345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanickvoid
3094304ea90233baeac6801a98e981658cb7a2d2501cIan Romanickast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
309545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    bool formal,
309645d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    exec_list *ir_parameters,
309745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    _mesa_glsl_parse_state *state)
3098a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
3099cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   ast_parameter_declarator *void_param = NULL;
3100cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   unsigned count = 0;
3101a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
31022b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
310345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      param->formal_parameter = formal;
3104068c80cfe0a280490353b6b007165d717c672eedEric Anholt      param->hir(ir_parameters, state);
3105cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3106cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (param->is_void)
3107cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 void_param = param;
3108cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3109cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      count++;
3110cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
3111cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3112cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if ((void_param != NULL) && (count > 1)) {
3113cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      YYLTYPE loc = void_param->get_location();
3114cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3115cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      _mesa_glsl_error(& loc, state,
3116cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick		       "`void' parameter must be only parameter");
3117a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3118a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
3119a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3120a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
31216fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunkevoid
31220d81b0e18494a80c4326fbc98837842959675869Paul Berryemit_function(_mesa_glsl_parse_state *state, ir_function *f)
31236fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke{
31240d81b0e18494a80c4326fbc98837842959675869Paul Berry   /* IR invariants disallow function declarations or definitions
31250d81b0e18494a80c4326fbc98837842959675869Paul Berry    * nested within other function definitions.  But there is no
31260d81b0e18494a80c4326fbc98837842959675869Paul Berry    * requirement about the relative order of function declarations
31270d81b0e18494a80c4326fbc98837842959675869Paul Berry    * and definitions with respect to one another.  So simply insert
31280d81b0e18494a80c4326fbc98837842959675869Paul Berry    * the new ir_function block at the end of the toplevel instruction
31290d81b0e18494a80c4326fbc98837842959675869Paul Berry    * list.
31300d81b0e18494a80c4326fbc98837842959675869Paul Berry    */
31310d81b0e18494a80c4326fbc98837842959675869Paul Berry   state->toplevel_ir->push_tail(f);
31326fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke}
31336fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke
31346fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke
3135fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
313692318a947958892497722772b03c643ebc943294Ian Romanickast_function::hir(exec_list *instructions,
313792318a947958892497722772b03c643ebc943294Ian Romanick		  struct _mesa_glsl_parse_state *state)
3138a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
3139953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
314018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_function *f = NULL;
314192318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *sig = NULL;
314292318a947958892497722772b03c643ebc943294Ian Romanick   exec_list hir_parameters;
3143a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3144ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   const char *const name = identifier;
3145a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
31469a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick   /* New functions are always added to the top-level IR instruction stream,
31479a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick    * so this instruction list pointer is ignored.  See also emit_function
31489a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick    * (called below).
31499a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick    */
31509a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick   (void) instructions;
31519a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick
315263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
315363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
315463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   "Function declarations (prototypes) cannot occur inside of functions;
315563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   they must be at global scope, or for the built-in functions, outside
315663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   the global scope."
315763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
315863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
315963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
316063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   "User defined functions may only be defined within the global scope."
316163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
316263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    * Note that this language does not appear in GLSL 1.10.
316363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    */
316463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   if ((state->current_function != NULL) && (state->language_version != 110)) {
316563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      YYLTYPE loc = this->get_location();
316663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      _mesa_glsl_error(&loc, state,
316763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick		       "declaration of function `%s' not allowed within "
316863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick		       "function body", name);
316963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   }
317063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick
3171edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3172edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *
3173edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   "Identifiers starting with "gl_" are reserved for use by
3174edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   OpenGL, and may not be declared in a shader as either a
3175edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   variable or a function."
3176edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    */
3177edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   if (strncmp(name, "gl_", 3) == 0) {
3178edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      YYLTYPE loc = this->get_location();
3179edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      _mesa_glsl_error(&loc, state,
3180edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke		       "identifier `%s' uses reserved `gl_' prefix", name);
3181edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   }
3182edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke
3183a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the list of function parameters to HIR now so that they can be
3184a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * used below to compare this function's signature with previously seen
3185a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * signatures for functions with the same name.
3186a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
318745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   ast_parameter_declarator::parameters_to_hir(& this->parameters,
318845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       is_definition,
318945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       & hir_parameters, state);
3190a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3191e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const char *return_type_name;
3192e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const glsl_type *return_type =
319392318a947958892497722772b03c643ebc943294Ian Romanick      this->return_type->specifier->glsl_type(& return_type_name, state);
3194e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
319576e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   if (!return_type) {
319676e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      YYLTYPE loc = this->get_location();
319776e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      _mesa_glsl_error(&loc, state,
319876e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       "function `%s' has undeclared return type `%s'",
319976e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       name, return_type_name);
320076e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      return_type = glsl_type::error_type;
320176e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   }
3202e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
3203ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3204ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    * "No qualifier is allowed on the return type of a function."
3205ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    */
3206ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   if (this->return_type->has_qualifiers()) {
3207ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      YYLTYPE loc = this->get_location();
3208ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      _mesa_glsl_error(& loc, state,
3209ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke		       "function `%s' return type has qualifiers", name);
3210ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   }
3211ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke
3212f07221056e1822187546b76387714b3172f9b2c5Paul Berry   /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3213f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *
3214f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    "[Sampler types] can only be declared as function parameters
3215f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    or uniform variables (see Section 4.3.5 "Uniform")".
3216f07221056e1822187546b76387714b3172f9b2c5Paul Berry    */
3217f07221056e1822187546b76387714b3172f9b2c5Paul Berry   if (return_type->contains_sampler()) {
3218f07221056e1822187546b76387714b3172f9b2c5Paul Berry      YYLTYPE loc = this->get_location();
3219f07221056e1822187546b76387714b3172f9b2c5Paul Berry      _mesa_glsl_error(&loc, state,
3220f07221056e1822187546b76387714b3172f9b2c5Paul Berry                       "function `%s' return type can't contain a sampler",
3221f07221056e1822187546b76387714b3172f9b2c5Paul Berry                       name);
3222f07221056e1822187546b76387714b3172f9b2c5Paul Berry   }
3223f07221056e1822187546b76387714b3172f9b2c5Paul Berry
3224a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Verify that this function's signature either doesn't match a previously
3225a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * seen signature for a function with the same name, or, if a match is found,
3226a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * that the previously seen signature does not have an associated definition.
3227a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3228e466b182bbf21f62fe6542091f4af3275555db80Ian Romanick   f = state->symbols->get_function(name);
322981f03393982c29f8f4165b5629c8e8fb708b97a3Kenneth Graunke   if (f != NULL && (state->es_shader || f->has_user_signature())) {
3230202604e8160157e4e80b3458175e0170d168e557Ian Romanick      sig = f->exact_matching_signature(&hir_parameters);
32310d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke      if (sig != NULL) {
32320d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 const char *badvar = sig->qualifiers_match(&hir_parameters);
32330d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (badvar != NULL) {
32340d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
3235abd40b15210c17b2a3ba8fcffc868fda203efa01Kenneth Graunke
32360d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
32370d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "qualifiers don't match prototype", name, badvar);
32380d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
32391e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
32400d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (sig->return_type != return_type) {
32410d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
324260be7626b829af7e1d07330b9a88468924ba350eEric Anholt
32430d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
32440d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "match prototype", name);
32450d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
3246a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
32470d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (is_definition && sig->is_defined) {
32480d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
3249a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
32500d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
3251a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
3252a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
3253a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
32541660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      f = new(ctx) ir_function(name);
3255e8f5ebf313da3ce33ccbbcf9b72946853035fbddEric Anholt      if (!state->symbols->add_function(f)) {
3256e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 /* This function name shadows a non-function use of the same name. */
3257e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 YYLTYPE loc = this->get_location();
3258e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke
3259e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
3260e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke			  "non-function", name);
3261e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 return NULL;
3262e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke      }
32639fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke
32640d81b0e18494a80c4326fbc98837842959675869Paul Berry      emit_function(state, f);
3265a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3266a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3267ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   /* Verify the return type of main() */
3268ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   if (strcmp(name, "main") == 0) {
326925711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick      if (! return_type->is_void()) {
3270ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 YYLTYPE loc = this->get_location();
3271ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
3272ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must return void");
3273ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt      }
3274174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
327592318a947958892497722772b03c643ebc943294Ian Romanick      if (!hir_parameters.is_empty()) {
3276174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 YYLTYPE loc = this->get_location();
3277174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
3278174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
3279174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt      }
3280ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   }
3281a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3282a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Finish storing the information about this new function in its signature.
3283a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
328492318a947958892497722772b03c643ebc943294Ian Romanick   if (sig == NULL) {
32851660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      sig = new(ctx) ir_function_signature(return_type);
328692318a947958892497722772b03c643ebc943294Ian Romanick      f->add_signature(sig);
3287a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3288a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3289bff6013d469b3d4e54cdc5731801c56994a523ecKenneth Graunke   sig->replace_parameters(&hir_parameters);
329092318a947958892497722772b03c643ebc943294Ian Romanick   signature = sig;
329192318a947958892497722772b03c643ebc943294Ian Romanick
329292318a947958892497722772b03c643ebc943294Ian Romanick   /* Function declarations (prototypes) do not have r-values.
329392318a947958892497722772b03c643ebc943294Ian Romanick    */
329492318a947958892497722772b03c643ebc943294Ian Romanick   return NULL;
329592318a947958892497722772b03c643ebc943294Ian Romanick}
329692318a947958892497722772b03c643ebc943294Ian Romanick
329792318a947958892497722772b03c643ebc943294Ian Romanick
329892318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue *
329992318a947958892497722772b03c643ebc943294Ian Romanickast_function_definition::hir(exec_list *instructions,
330092318a947958892497722772b03c643ebc943294Ian Romanick			     struct _mesa_glsl_parse_state *state)
330192318a947958892497722772b03c643ebc943294Ian Romanick{
330292318a947958892497722772b03c643ebc943294Ian Romanick   prototype->is_definition = true;
330392318a947958892497722772b03c643ebc943294Ian Romanick   prototype->hir(instructions, state);
3304e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
330592318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *signature = prototype->signature;
3306826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke   if (signature == NULL)
3307826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke      return NULL;
3308a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
330941ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == NULL);
331041ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = signature;
33116de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   state->found_return = false;
331241ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
3313e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   /* Duplicate parameters declared in the prototype as concrete variables.
3314e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick    * Add these to the symbol table.
3315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
33168bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->push_scope();
3317e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   foreach_iter(exec_list_iterator, iter, signature->parameters) {
3318fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
3319e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
3320fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      assert(var != NULL);
3321a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
33223359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* The only way a parameter would "exist" is if two parameters have
33233359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       * the same name.
33243359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
33253359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(var->name)) {
33263359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 YYLTYPE loc = this->get_location();
33273359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
33283359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
33293359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      } else {
3330001eee52d461233b1e1d6ed3577965e9bcb209e8Eric Anholt	 state->symbols->add_variable(var);
33313359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      }
3332a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3333a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
33349fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   /* Convert the body of the function to HIR. */
3335894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt   this->body->hir(&signature->body, state);
33369fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   signature->is_defined = true;
3337a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
33388bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->pop_scope();
3339a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
334041ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == signature);
334141ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
3342a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
33436de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   if (!signature->return_type->is_void() && !state->found_return) {
33446de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      YYLTYPE loc = this->get_location();
33456de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
33466de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       "%s, but no return statement",
33476de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->function_name(),
33486de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->return_type->name);
33496de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   }
33506de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke
3351a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Function definitions do not have r-values.
3352a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3353a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
3354a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
335516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
335616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
3357fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
335816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions,
335916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			struct _mesa_glsl_parse_state *state)
336016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{
3361953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
336216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
3363c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   switch (mode) {
3364c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_return: {
336516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      ir_return *inst;
3366aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt      assert(state->current_function);
336716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
336816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      if (opt_return_value) {
3369b4cdba687c098eea2ecc61349a4ea02a8769909eChad Versace	 ir_rvalue *const ret = opt_return_value->hir(instructions, state);
33702db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick
33712db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	 /* The value of the return type can be NULL if the shader says
33722db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  * 'return foo();' and foo() is a function that returns void.
33732db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  *
33742db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  * NOTE: The GLSL spec doesn't say that this is an error.  The type
33752db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  * of the return value is void.  If the return type of the function is
33762db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  * also void, then this should compile without error.  Seriously.
33772db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  */
33782db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	 const glsl_type *const ret_type =
33792db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	    (ret == NULL) ? glsl_type::void_type : ret->type;
338016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
338118707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 /* Implicit conversions are not allowed for return values. */
33822db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	 if (state->current_function->return_type != ret_type) {
338318707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    YYLTYPE loc = this->get_location();
338418707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke
338518707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    _mesa_glsl_error(& loc, state,
338618707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "`return' with wrong type %s, in function `%s' "
338718707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "returning %s",
33882db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick			     ret_type->name,
338918707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->function_name(),
339018707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->return_type->name);
339118707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 }
339216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
33931660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return(ret);
339416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      } else {
3395aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 if (state->current_function->return_type->base_type !=
3396aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	     GLSL_TYPE_VOID) {
3397aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    YYLTYPE loc = this->get_location();
3398aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt
3399aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    _mesa_glsl_error(& loc, state,
3400aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "`return' with no value, in function %s returning "
3401aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "non-void",
3402f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
3403aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 }
34041660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return;
340516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      }
340616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
34076de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      state->found_return = true;
340816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      instructions->push_tail(inst);
3409c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
341016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   }
341116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
3412c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_discard:
3413b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      if (state->target != fragment_shader) {
3414b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 YYLTYPE loc = this->get_location();
3415b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
3416b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 _mesa_glsl_error(& loc, state,
3417b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt			  "`discard' may only appear in a fragment shader");
3418b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      }
341977049a702ad54e09c4102fe8c964e069944f83e5Kenneth Graunke      instructions->push_tail(new(ctx) ir_discard);
3420c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
3421c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick
3422c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_break:
3423c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_continue:
34245c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      if (mode == ast_continue &&
34255c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	  state->loop_nesting_ast == NULL) {
34264cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 YYLTYPE loc = this->get_location();
34274cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
34284cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 _mesa_glsl_error(& loc, state,
34295c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe			  "continue may only appear in a loop");
34305c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      } else if (mode == ast_break &&
34315c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe		 state->loop_nesting_ast == NULL &&
343222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt		 state->switch_state.switch_nesting_ast == NULL) {
34335c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 YYLTYPE loc = this->get_location();
34344cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
34355c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 _mesa_glsl_error(& loc, state,
34365c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe			  "break may only appear in a loop or a switch");
34375c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      } else {
34385c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 /* For a loop, inline the for loop expression again,
34395c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	  * since we don't know where near the end of
34405c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	  * the loop body the normal copy of it
34412d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  * is going to be placed.
34422d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  */
34435c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 if (state->loop_nesting_ast != NULL &&
34445c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	     mode == ast_continue &&
34455c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	     state->loop_nesting_ast->rest_expression) {
34465c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    state->loop_nesting_ast->rest_expression->hir(instructions,
34475c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe							  state);
34482d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 }
34492d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt
345022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	 if (state->switch_state.is_switch_innermost &&
34515c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	     mode == ast_break) {
34525c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    /* Force break out of switch by setting is_break switch state.
34535c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	     */
345422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	    ir_variable *const is_break_var = state->switch_state.is_break_var;
34555c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    ir_dereference_variable *const deref_is_break_var =
34565c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	       new(ctx) ir_dereference_variable(is_break_var);
34575c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    ir_constant *const true_val = new(ctx) ir_constant(true);
34585c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    ir_assignment *const set_break_var =
3459aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt	       new(ctx) ir_assignment(deref_is_break_var, true_val);
34605c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
34615c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    instructions->push_tail(set_break_var);
34625c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 }
34635c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 else {
34645c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    ir_loop_jump *const jump =
34651660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	       new(ctx) ir_loop_jump((mode == ast_break)
34661660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     ? ir_loop_jump::jump_break
34671660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     : ir_loop_jump::jump_continue);
34684cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    instructions->push_tail(jump);
34694cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 }
34704cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      }
34714cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
3472c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
3473b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   }
3474b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
347516a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   /* Jump instructions do not have r-values.
347616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
347716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
347816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
34793c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34803c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34813c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue *
34823c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions,
34833c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			     struct _mesa_glsl_parse_state *state)
34843c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
3485953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
34861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
34873c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_rvalue *const condition = this->condition->hir(instructions, state);
34883c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34893c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
34903c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
34913c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    "Any expression whose type evaluates to a Boolean can be used as the
34923c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    conditional expression bool-expression. Vector types are not accepted
34933c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    as the expression to if."
34943c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
34953c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * The checks are separated so that higher quality diagnostics can be
34963c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * generated for cases where both rules are violated.
34973c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
34983c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
34993c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      YYLTYPE loc = this->condition->get_location();
35003c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
35013c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
35023c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick		       "boolean");
35033c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
35043c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
35051660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_if *const stmt = new(ctx) ir_if(condition);
35063c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
3507665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (then_statement != NULL) {
3508665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
35094f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      then_statement->hir(& stmt->then_instructions, state);
3510665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
3511665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
35123c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
3513665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (else_statement != NULL) {
3514665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
35154f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      else_statement->hir(& stmt->else_instructions, state);
3516665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
3517665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
35183c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
35193c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   instructions->push_tail(stmt);
35203c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
35213c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* if-statements do not have r-values.
35223c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
35233c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   return NULL;
35243c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick}
35259e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
35269e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
352785beb39e14556cf02f58116fd287120cd1defbd5Dan McCabeir_rvalue *
352885beb39e14556cf02f58116fd287120cd1defbd5Dan McCabeast_switch_statement::hir(exec_list *instructions,
352985beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe			  struct _mesa_glsl_parse_state *state)
353085beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe{
35315c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   void *ctx = state;
35325c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35335c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_rvalue *const test_expression =
35345c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      this->test_expression->hir(instructions, state);
35355c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35365c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
35375c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    *
35385c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    *    "The type of init-expression in a switch statement must be a
35395c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    *     scalar integer."
35405c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
3541bbbc7c7d566905920967f56648fc26abcb37f4a1Eric Anholt   if (!test_expression->type->is_scalar() ||
3542bbbc7c7d566905920967f56648fc26abcb37f4a1Eric Anholt       !test_expression->type->is_integer()) {
35435c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      YYLTYPE loc = this->test_expression->get_location();
35445c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35455c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      _mesa_glsl_error(& loc,
35465c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe		       state,
35475c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe		       "switch-statement expression must be scalar "
35485c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe		       "integer");
35495c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   }
35505c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35515c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Track the switch-statement nesting in a stack-like manner.
35525c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
355322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   struct glsl_switch_state saved = state->switch_state;
35545c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
355522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state.is_switch_innermost = true;
355622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state.switch_nesting_ast = this;
3557140632190cf41e6a035ca199b181091d4ed46986Eric Anholt   state->switch_state.labels_ht = hash_table_ctor(0, hash_table_pointer_hash,
3558140632190cf41e6a035ca199b181091d4ed46986Eric Anholt						   hash_table_pointer_compare);
355957e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt   state->switch_state.previous_default = NULL;
35605c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35615c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Initalize is_fallthru state to false.
35625c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35635c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
356422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state.is_fallthru_var =
356522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      new(ctx) ir_variable(glsl_type::bool_type,
356622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt			   "switch_is_fallthru_tmp",
356722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt			   ir_var_temporary);
356822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   instructions->push_tail(state->switch_state.is_fallthru_var);
35695c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35705c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_dereference_variable *deref_is_fallthru_var =
357122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
35725c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
3573aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt						  is_fallthru_val));
35745c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35755c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Initalize is_break state to false.
35765c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35775c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_rvalue *const is_break_val = new (ctx) ir_constant(false);
357822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state.is_break_var = new(ctx) ir_variable(glsl_type::bool_type,
357922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt							   "switch_is_break_tmp",
358022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt							   ir_var_temporary);
358122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   instructions->push_tail(state->switch_state.is_break_var);
35825c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35835c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_dereference_variable *deref_is_break_var =
358422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      new(ctx) ir_dereference_variable(state->switch_state.is_break_var);
35855c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var,
3586aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt						  is_break_val));
35875c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35885c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Cache test expression.
35895c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35905c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   test_to_hir(instructions, state);
35915462f3679ab7217d3a3be48365750801c7771237Eric Anholt
35925c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Emit code for body of switch stmt.
35935c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35945c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   body->hir(instructions, state);
35955c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
3596140632190cf41e6a035ca199b181091d4ed46986Eric Anholt   hash_table_dtor(state->switch_state.labels_ht);
3597140632190cf41e6a035ca199b181091d4ed46986Eric Anholt
359822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state = saved;
359985beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe
36005462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Switch statements do not have r-values. */
36015462f3679ab7217d3a3be48365750801c7771237Eric Anholt   return NULL;
36025462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
36035462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36045462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36055462f3679ab7217d3a3be48365750801c7771237Eric Anholtvoid
36065462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_switch_statement::test_to_hir(exec_list *instructions,
36075462f3679ab7217d3a3be48365750801c7771237Eric Anholt				  struct _mesa_glsl_parse_state *state)
36085462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
36095462f3679ab7217d3a3be48365750801c7771237Eric Anholt   void *ctx = state;
36105462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36115462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Cache value of test expression. */
36125462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_rvalue *const test_val =
36135462f3679ab7217d3a3be48365750801c7771237Eric Anholt      test_expression->hir(instructions,
36145462f3679ab7217d3a3be48365750801c7771237Eric Anholt			   state);
36155462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36169c4e9ce051bb43861adb4f8cd8e88a733c2f3ed1Eric Anholt   state->switch_state.test_var = new(ctx) ir_variable(test_val->type,
36175462f3679ab7217d3a3be48365750801c7771237Eric Anholt						       "switch_test_tmp",
36185462f3679ab7217d3a3be48365750801c7771237Eric Anholt						       ir_var_temporary);
36195462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_dereference_variable *deref_test_var =
36205462f3679ab7217d3a3be48365750801c7771237Eric Anholt      new(ctx) ir_dereference_variable(state->switch_state.test_var);
36215462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36225462f3679ab7217d3a3be48365750801c7771237Eric Anholt   instructions->push_tail(state->switch_state.test_var);
3623aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt   instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val));
36245462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
36255462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36265462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36275462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue *
36285462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_switch_body::hir(exec_list *instructions,
36295462f3679ab7217d3a3be48365750801c7771237Eric Anholt		     struct _mesa_glsl_parse_state *state)
36305462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
36315462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (stmts != NULL)
36325462f3679ab7217d3a3be48365750801c7771237Eric Anholt      stmts->hir(instructions, state);
36335462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36345462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Switch bodies do not have r-values. */
36355462f3679ab7217d3a3be48365750801c7771237Eric Anholt   return NULL;
36365462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
36375462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36385462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue *
36395462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_case_statement_list::hir(exec_list *instructions,
36405462f3679ab7217d3a3be48365750801c7771237Eric Anholt			     struct _mesa_glsl_parse_state *state)
36415462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
36425462f3679ab7217d3a3be48365750801c7771237Eric Anholt   foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases)
36435462f3679ab7217d3a3be48365750801c7771237Eric Anholt      case_stmt->hir(instructions, state);
36445462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36455462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Case statements do not have r-values. */
36465462f3679ab7217d3a3be48365750801c7771237Eric Anholt   return NULL;
36475462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
36485462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36495462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue *
36505462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_case_statement::hir(exec_list *instructions,
36515462f3679ab7217d3a3be48365750801c7771237Eric Anholt			struct _mesa_glsl_parse_state *state)
36525462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
36535462f3679ab7217d3a3be48365750801c7771237Eric Anholt   labels->hir(instructions, state);
36545462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36555462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Conditionally set fallthru state based on break state. */
36565462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_constant *const false_val = new(state) ir_constant(false);
36575462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_dereference_variable *const deref_is_fallthru_var =
36585462f3679ab7217d3a3be48365750801c7771237Eric Anholt      new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
36595462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_dereference_variable *const deref_is_break_var =
36605462f3679ab7217d3a3be48365750801c7771237Eric Anholt      new(state) ir_dereference_variable(state->switch_state.is_break_var);
36615462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_assignment *const reset_fallthru_on_break =
36625462f3679ab7217d3a3be48365750801c7771237Eric Anholt      new(state) ir_assignment(deref_is_fallthru_var,
36635462f3679ab7217d3a3be48365750801c7771237Eric Anholt			       false_val,
36645462f3679ab7217d3a3be48365750801c7771237Eric Anholt			       deref_is_break_var);
36655462f3679ab7217d3a3be48365750801c7771237Eric Anholt   instructions->push_tail(reset_fallthru_on_break);
36665462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36675462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Guard case statements depending on fallthru state. */
36685462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_dereference_variable *const deref_fallthru_guard =
36695462f3679ab7217d3a3be48365750801c7771237Eric Anholt      new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
36705462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
36715462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36725462f3679ab7217d3a3be48365750801c7771237Eric Anholt   foreach_list_typed (ast_node, stmt, link, & this->stmts)
36735462f3679ab7217d3a3be48365750801c7771237Eric Anholt      stmt->hir(& test_fallthru->then_instructions, state);
36745462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36755462f3679ab7217d3a3be48365750801c7771237Eric Anholt   instructions->push_tail(test_fallthru);
36765462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36775462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Case statements do not have r-values. */
36785462f3679ab7217d3a3be48365750801c7771237Eric Anholt   return NULL;
36795462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
36805462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36815462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36825462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue *
36835462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_case_label_list::hir(exec_list *instructions,
36845462f3679ab7217d3a3be48365750801c7771237Eric Anholt			 struct _mesa_glsl_parse_state *state)
36855462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
36865462f3679ab7217d3a3be48365750801c7771237Eric Anholt   foreach_list_typed (ast_case_label, label, link, & this->labels)
36875462f3679ab7217d3a3be48365750801c7771237Eric Anholt      label->hir(instructions, state);
36885462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36895462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Case labels do not have r-values. */
36905462f3679ab7217d3a3be48365750801c7771237Eric Anholt   return NULL;
36915462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
36925462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36935462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue *
36945462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_case_label::hir(exec_list *instructions,
36955462f3679ab7217d3a3be48365750801c7771237Eric Anholt		    struct _mesa_glsl_parse_state *state)
36965462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
36975462f3679ab7217d3a3be48365750801c7771237Eric Anholt   void *ctx = state;
36985462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36995462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_dereference_variable *deref_fallthru_var =
37005462f3679ab7217d3a3be48365750801c7771237Eric Anholt      new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
37015462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37025462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_rvalue *const true_val = new(ctx) ir_constant(true);
37035462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37045462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* If not default case, ... */
37055462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (this->test_value != NULL) {
37065462f3679ab7217d3a3be48365750801c7771237Eric Anholt      /* Conditionally set fallthru state based on
37075462f3679ab7217d3a3be48365750801c7771237Eric Anholt       * comparison of cached test expression value to case label.
37085462f3679ab7217d3a3be48365750801c7771237Eric Anholt       */
37095462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
37105462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_constant *label_const = label_rval->constant_expression_value();
37115462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37125462f3679ab7217d3a3be48365750801c7771237Eric Anholt      if (!label_const) {
37135462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 YYLTYPE loc = this->test_value->get_location();
37145462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37155462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 _mesa_glsl_error(& loc, state,
37165462f3679ab7217d3a3be48365750801c7771237Eric Anholt			  "switch statement case label must be a "
37175462f3679ab7217d3a3be48365750801c7771237Eric Anholt			  "constant expression");
37185462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37195462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 /* Stuff a dummy value in to allow processing to continue. */
37205462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 label_const = new(ctx) ir_constant(0);
37215462f3679ab7217d3a3be48365750801c7771237Eric Anholt      } else {
37225462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 ast_expression *previous_label = (ast_expression *)
37235462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    hash_table_find(state->switch_state.labels_ht,
37245462f3679ab7217d3a3be48365750801c7771237Eric Anholt			    (void *)(uintptr_t)label_const->value.u[0]);
37255462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37265462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 if (previous_label) {
37275462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    YYLTYPE loc = this->test_value->get_location();
37285462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    _mesa_glsl_error(& loc, state,
37295462f3679ab7217d3a3be48365750801c7771237Eric Anholt			     "duplicate case value");
37305462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37315462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    loc = previous_label->get_location();
37325462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    _mesa_glsl_error(& loc, state,
37335462f3679ab7217d3a3be48365750801c7771237Eric Anholt			     "this is the previous case label");
37345462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 } else {
37355462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    hash_table_insert(state->switch_state.labels_ht,
37365462f3679ab7217d3a3be48365750801c7771237Eric Anholt			      this->test_value,
3737140632190cf41e6a035ca199b181091d4ed46986Eric Anholt			      (void *)(uintptr_t)label_const->value.u[0]);
37385462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 }
37395462f3679ab7217d3a3be48365750801c7771237Eric Anholt      }
37405462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37415462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_dereference_variable *deref_test_var =
37425462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 new(ctx) ir_dereference_variable(state->switch_state.test_var);
37435462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37445462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_rvalue *const test_cond = new(ctx) ir_expression(ir_binop_all_equal,
37455462f3679ab7217d3a3be48365750801c7771237Eric Anholt							  label_const,
37465462f3679ab7217d3a3be48365750801c7771237Eric Anholt							  deref_test_var);
37475462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37485462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_assignment *set_fallthru_on_test =
37495462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 new(ctx) ir_assignment(deref_fallthru_var,
37505462f3679ab7217d3a3be48365750801c7771237Eric Anholt				true_val,
37515462f3679ab7217d3a3be48365750801c7771237Eric Anholt				test_cond);
37525462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37535462f3679ab7217d3a3be48365750801c7771237Eric Anholt      instructions->push_tail(set_fallthru_on_test);
37545462f3679ab7217d3a3be48365750801c7771237Eric Anholt   } else { /* default case */
37555462f3679ab7217d3a3be48365750801c7771237Eric Anholt      if (state->switch_state.previous_default) {
37565462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 YYLTYPE loc = this->get_location();
37575462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 _mesa_glsl_error(& loc, state,
37585462f3679ab7217d3a3be48365750801c7771237Eric Anholt			  "multiple default labels in one switch");
37595462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37605462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 loc = state->switch_state.previous_default->get_location();
37615462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 _mesa_glsl_error(& loc, state,
37625462f3679ab7217d3a3be48365750801c7771237Eric Anholt			  "this is the first default label");
37635462f3679ab7217d3a3be48365750801c7771237Eric Anholt      }
37645462f3679ab7217d3a3be48365750801c7771237Eric Anholt      state->switch_state.previous_default = this;
37655462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37665462f3679ab7217d3a3be48365750801c7771237Eric Anholt      /* Set falltrhu state. */
37675462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_assignment *set_fallthru =
3768aa5ec137757323b424d0f2638c50c93b9b06ffffEric Anholt	 new(ctx) ir_assignment(deref_fallthru_var, true_val);
37695462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37705462f3679ab7217d3a3be48365750801c7771237Eric Anholt      instructions->push_tail(set_fallthru);
37715462f3679ab7217d3a3be48365750801c7771237Eric Anholt   }
3772140632190cf41e6a035ca199b181091d4ed46986Eric Anholt
37735462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Case statements do not have r-values. */
37745462f3679ab7217d3a3be48365750801c7771237Eric Anholt   return NULL;
37755462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
37765462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37775462f3679ab7217d3a3be48365750801c7771237Eric Anholtvoid
37785462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_iteration_statement::condition_to_hir(ir_loop *stmt,
37795462f3679ab7217d3a3be48365750801c7771237Eric Anholt					  struct _mesa_glsl_parse_state *state)
37805462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
37815462f3679ab7217d3a3be48365750801c7771237Eric Anholt   void *ctx = state;
37825462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37835462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (condition != NULL) {
37845462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_rvalue *const cond =
37855462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 condition->hir(& stmt->body_instructions, state);
37865462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37875462f3679ab7217d3a3be48365750801c7771237Eric Anholt      if ((cond == NULL)
37885462f3679ab7217d3a3be48365750801c7771237Eric Anholt	  || !cond->type->is_boolean() || !cond->type->is_scalar()) {
37895462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 YYLTYPE loc = condition->get_location();
37905462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37915462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 _mesa_glsl_error(& loc, state,
37925462f3679ab7217d3a3be48365750801c7771237Eric Anholt			  "loop condition must be scalar boolean");
37935462f3679ab7217d3a3be48365750801c7771237Eric Anholt      } else {
37945462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 /* As the first code in the loop body, generate a block that looks
37955462f3679ab7217d3a3be48365750801c7771237Eric Anholt	  * like 'if (!condition) break;' as the loop termination condition.
37965462f3679ab7217d3a3be48365750801c7771237Eric Anholt	  */
37975462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 ir_rvalue *const not_cond =
37985d6ea16dfe99e1aba61c25a897b66951faab1a39Eric Anholt	    new(ctx) ir_expression(ir_unop_logic_not, cond);
37995462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38005462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
38015462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38025462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 ir_jump *const break_stmt =
38035462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
38045462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38055462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 if_stmt->then_instructions.push_tail(break_stmt);
38065462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 stmt->body_instructions.push_tail(if_stmt);
38075462f3679ab7217d3a3be48365750801c7771237Eric Anholt      }
38085462f3679ab7217d3a3be48365750801c7771237Eric Anholt   }
38095462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
38105462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38115462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38125462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue *
38135462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_iteration_statement::hir(exec_list *instructions,
38145462f3679ab7217d3a3be48365750801c7771237Eric Anholt			     struct _mesa_glsl_parse_state *state)
38155462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
38165462f3679ab7217d3a3be48365750801c7771237Eric Anholt   void *ctx = state;
38175462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38185462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* For-loops and while-loops start a new scope, but do-while loops do not.
38195462f3679ab7217d3a3be48365750801c7771237Eric Anholt    */
38205462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (mode != ast_do_while)
38215462f3679ab7217d3a3be48365750801c7771237Eric Anholt      state->symbols->push_scope();
38225462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38235462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (init_statement != NULL)
38245462f3679ab7217d3a3be48365750801c7771237Eric Anholt      init_statement->hir(instructions, state);
38255462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38265462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_loop *const stmt = new(ctx) ir_loop();
38275462f3679ab7217d3a3be48365750801c7771237Eric Anholt   instructions->push_tail(stmt);
38285462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38295462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Track the current loop nesting. */
38305462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
38315462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38325462f3679ab7217d3a3be48365750801c7771237Eric Anholt   state->loop_nesting_ast = this;
38335462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38345462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Likewise, indicate that following code is closest to a loop,
38355462f3679ab7217d3a3be48365750801c7771237Eric Anholt    * NOT closest to a switch.
38365462f3679ab7217d3a3be48365750801c7771237Eric Anholt    */
38375462f3679ab7217d3a3be48365750801c7771237Eric Anholt   bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
38385462f3679ab7217d3a3be48365750801c7771237Eric Anholt   state->switch_state.is_switch_innermost = false;
38395462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38405462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (mode != ast_do_while)
38415462f3679ab7217d3a3be48365750801c7771237Eric Anholt      condition_to_hir(stmt, state);
38425462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38435462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (body != NULL)
38445462f3679ab7217d3a3be48365750801c7771237Eric Anholt      body->hir(& stmt->body_instructions, state);
38455462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38465462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (rest_expression != NULL)
38475462f3679ab7217d3a3be48365750801c7771237Eric Anholt      rest_expression->hir(& stmt->body_instructions, state);
38485462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38495462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (mode == ast_do_while)
38505462f3679ab7217d3a3be48365750801c7771237Eric Anholt      condition_to_hir(stmt, state);
38515462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38525462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (mode != ast_do_while)
38535462f3679ab7217d3a3be48365750801c7771237Eric Anholt      state->symbols->pop_scope();
385422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
38555462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Restore previous nesting before returning. */
38565462f3679ab7217d3a3be48365750801c7771237Eric Anholt   state->loop_nesting_ast = nesting_ast;
38575462f3679ab7217d3a3be48365750801c7771237Eric Anholt   state->switch_state.is_switch_innermost = saved_is_switch_innermost;
3858e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick
38599e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   /* Loops do not have r-values.
38609e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
38619e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   return NULL;
38629e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick}
38633455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
38643455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
38653455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
38663455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_type_specifier::hir(exec_list *instructions,
38673455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
38683455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
386908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   if (!this->is_precision_statement && this->structure == NULL)
387008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      return NULL;
387108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
387208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   YYLTYPE loc = this->get_location();
387308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
387408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   if (this->precision != ast_precision_none
387508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace       && state->language_version != 100
387608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace       && state->language_version < 130) {
387708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      _mesa_glsl_error(&loc, state,
387808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                       "precision qualifiers exist only in "
387908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                       "GLSL ES 1.00, and GLSL 1.30 and later");
388008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      return NULL;
388108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   }
388208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   if (this->precision != ast_precision_none
388308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace       && this->structure != NULL) {
388408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      _mesa_glsl_error(&loc, state,
388508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                       "precision qualifiers do not apply to structures");
388608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      return NULL;
388708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   }
388808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
388908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   /* If this is a precision statement, check that the type to which it is
389008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    * applied is either float or int.
389108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *
389208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    * From section 4.5.3 of the GLSL 1.30 spec:
389308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *    "The precision statement
389408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *       precision precision-qualifier type;
389508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *    can be used to establish a default precision qualifier. The type
389608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *    field can be either int or float [...].  Any other types or
389708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *    qualifiers will result in an error.
389808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    */
389908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   if (this->is_precision_statement) {
390008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      assert(this->precision != ast_precision_none);
390108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      assert(this->structure == NULL); /* The check for structures was
390208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                                        * performed above. */
390308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      if (this->is_array) {
390408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace         _mesa_glsl_error(&loc, state,
390508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                          "default precision statements do not apply to "
390608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                          "arrays");
390708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace         return NULL;
390808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      }
3909eb7a71dea78152142b456f29e4881c4d3aeb56b6Eric Anholt      if (strcmp(this->type_name, "float") != 0 &&
3910eb7a71dea78152142b456f29e4881c4d3aeb56b6Eric Anholt	  strcmp(this->type_name, "int") != 0) {
391108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace         _mesa_glsl_error(&loc, state,
391208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                          "default precision statements apply only to types "
391308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                          "float and int");
391408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace         return NULL;
391508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      }
391608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
391708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      /* FINISHME: Translate precision statements into IR. */
391808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      return NULL;
391908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   }
392008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
39213455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   if (this->structure != NULL)
39223455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      return this->structure->hir(instructions, state);
392385ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick
392485ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick   return NULL;
39253455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
39263455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39273455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39283455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
39293455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_struct_specifier::hir(exec_list *instructions,
39303455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
39313455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
39323455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned decl_count = 0;
39333455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39343455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Make an initial pass over the list of structure fields to determine how
39353455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * many there are.  Each element in this list is an ast_declarator_list.
39363455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * This means that we actually need to count the number of elements in the
39373455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * 'declarations' list in each of the elements.
39383455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
39392b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
39402b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
3941304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      foreach_list_const (decl_ptr, & decl_list->declarations) {
39423455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_count++;
39433455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
39443455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
39453455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39463455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Allocate storage for the structure fields and process the field
39473455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * declarations.  As the declarations are processed, try to also convert
39483455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * the types to HIR.  This ensures that structure definitions embedded in
39493455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * other structure definitions are processed.
39503455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
3951d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8Kenneth Graunke   glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field,
395221b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt						  decl_count);
39533455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39543455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned i = 0;
39552b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
39562b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
39573455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const char *type_name;
39583455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39593455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      decl_list->type->specifier->hir(instructions, state);
39603455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
3961c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke      /* Section 10.9 of the GLSL ES 1.00 specification states that
3962c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke       * embedded structure definitions have been removed from the language.
3963c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke       */
3964c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke      if (state->es_shader && decl_list->type->specifier->structure != NULL) {
3965c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke	 YYLTYPE loc = this->get_location();
3966c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke	 _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
3967c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke			  "not allowed in GLSL ES 1.00.");
3968c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke      }
3969c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke
39703455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const glsl_type *decl_type =
39713455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_list->type->specifier->glsl_type(& type_name, state);
39723455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39732b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_declaration, decl, link,
39742b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick			  &decl_list->declarations) {
3975d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 const struct glsl_type *field_type = decl_type;
3976d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 if (decl->is_array) {
3977d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	    YYLTYPE loc = decl->get_location();
3978d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	    field_type = process_array_type(&loc, decl_type, decl->array_size,
3979d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke					    state);
3980d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 }
398173986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	 fields[i].type = (field_type != NULL)
398273986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	    ? field_type : glsl_type::error_type;
39833455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 fields[i].name = decl->identifier;
39843455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 i++;
39853455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
39863455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
39873455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39883455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   assert(i == decl_count);
39893455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
399049e3577b91f44013746f7a3db411e7041b7d899eIan Romanick   const glsl_type *t =
3991ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke      glsl_type::get_record_instance(fields, decl_count, this->name);
39921d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
3993ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   YYLTYPE loc = this->get_location();
3994a789ca649cb143c0c5bf3209ff1bde398fbd777eIan Romanick   if (!state->symbols->add_type(name, t)) {
3995ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
3996ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   } else {
3997eb639349e289a6b8be06a54f5e9e0ce18c71d511Kenneth Graunke      const glsl_type **s = reralloc(state, state->user_structures,
3998eb639349e289a6b8be06a54f5e9e0ce18c71d511Kenneth Graunke				     const glsl_type *,
3999eb639349e289a6b8be06a54f5e9e0ce18c71d511Kenneth Graunke				     state->num_user_structures + 1);
4000a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      if (s != NULL) {
4001a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 s[state->num_user_structures] = t;
4002a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->user_structures = s;
4003a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->num_user_structures++;
4004a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      }
4005ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   }
40063455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
40073455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Structure type definitions do not have r-values.
40083455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
40093455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   return NULL;
40103455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
40114b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
4012b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholtstatic struct gl_uniform_block *
4013b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholtget_next_uniform_block(struct _mesa_glsl_parse_state *state)
4014b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt{
4015b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt   if (state->num_uniform_blocks >= state->uniform_block_array_size) {
4016b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt      state->uniform_block_array_size *= 2;
4017b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt      if (state->uniform_block_array_size <= 4)
4018b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	 state->uniform_block_array_size = 4;
4019b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt
4020b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt      state->uniform_blocks = reralloc(state,
4021b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt				       state->uniform_blocks,
4022b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt				       struct gl_uniform_block,
4023b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt				       state->uniform_block_array_size);
4024b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt   }
4025b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt
4026b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt   memset(&state->uniform_blocks[state->num_uniform_blocks],
4027b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	  0, sizeof(*state->uniform_blocks));
4028b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt   return &state->uniform_blocks[state->num_uniform_blocks++];
4029b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt}
4030b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt
40312d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholtir_rvalue *
40322d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholtast_uniform_block::hir(exec_list *instructions,
40332d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt		       struct _mesa_glsl_parse_state *state)
40342d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt{
40352d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt   /* The ast_uniform_block has a list of ast_declarator_lists.  We
40362d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt    * need to turn those into ir_variables with an association
40372d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt    * with this uniform block.
40382d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt    */
4039b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt   struct gl_uniform_block *ubo = get_next_uniform_block(state);
4040b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt   ubo->Name = ralloc_strdup(state->uniform_blocks, this->block_name);
4041b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt
4042765221c0d5fdb0357bf6fae3220c7d57d93f938dKenneth Graunke   if (!state->symbols->add_uniform_block(ubo)) {
4043765221c0d5fdb0357bf6fae3220c7d57d93f938dKenneth Graunke      YYLTYPE loc = this->get_location();
4044765221c0d5fdb0357bf6fae3220c7d57d93f938dKenneth Graunke      _mesa_glsl_error(&loc, state, "Uniform block name `%s' already taken in "
4045765221c0d5fdb0357bf6fae3220c7d57d93f938dKenneth Graunke                       "the current scope.\n", ubo->Name);
4046765221c0d5fdb0357bf6fae3220c7d57d93f938dKenneth Graunke   }
4047765221c0d5fdb0357bf6fae3220c7d57d93f938dKenneth Graunke
4048b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt   unsigned int num_variables = 0;
4049b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt   foreach_list_typed(ast_declarator_list, decl_list, link, &declarations) {
4050b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt      foreach_list_const(node, &decl_list->declarations) {
4051b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	 num_variables++;
4052b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt      }
4053b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt   }
4054b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt
4055b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt   bool block_row_major = this->layout.flags.q.row_major;
4056b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt
4057b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt   ubo->Uniforms = rzalloc_array(state->uniform_blocks,
4058b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt				 struct gl_uniform_buffer_variable,
4059b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt				 num_variables);
4060b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt
4061f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt   foreach_list_typed(ast_declarator_list, decl_list, link, &declarations) {
4062f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt      exec_list declared_variables;
4063f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt
4064f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt      decl_list->hir(&declared_variables, state);
4065f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt
4066b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt      foreach_list_const(node, &declared_variables) {
4067b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	 struct ir_variable *var = (ir_variable *)node;
4068b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt
4069b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	 struct gl_uniform_buffer_variable *ubo_var =
4070b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	    &ubo->Uniforms[ubo->NumUniforms++];
4071b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt
4072b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	 var->uniform_block = ubo - state->uniform_blocks;
4073b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt
4074b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	 ubo_var->Name = ralloc_strdup(state->uniform_blocks, var->name);
4075b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	 ubo_var->Type = var->type;
4076b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	 ubo_var->Buffer = ubo - state->uniform_blocks;
4077b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	 ubo_var->Offset = 0; /* Assigned at link time. */
407886e0045578cd8d8be7736a8f56e2b51d61bda32aEric Anholt
407986e0045578cd8d8be7736a8f56e2b51d61bda32aEric Anholt	 if (var->type->is_matrix() ||
408086e0045578cd8d8be7736a8f56e2b51d61bda32aEric Anholt	     (var->type->is_array() && var->type->fields.array->is_matrix())) {
408186e0045578cd8d8be7736a8f56e2b51d61bda32aEric Anholt	    ubo_var->RowMajor = block_row_major;
408286e0045578cd8d8be7736a8f56e2b51d61bda32aEric Anholt	    if (decl_list->type->qualifier.flags.q.row_major)
408386e0045578cd8d8be7736a8f56e2b51d61bda32aEric Anholt	       ubo_var->RowMajor = true;
408486e0045578cd8d8be7736a8f56e2b51d61bda32aEric Anholt	    else if (decl_list->type->qualifier.flags.q.column_major)
408586e0045578cd8d8be7736a8f56e2b51d61bda32aEric Anholt	       ubo_var->RowMajor = false;
408686e0045578cd8d8be7736a8f56e2b51d61bda32aEric Anholt	 }
4087b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt
4088b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	 /* From the GL_ARB_uniform_buffer_object spec:
4089b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	  *
4090b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	  *     "Sampler types are not allowed inside of uniform
4091b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	  *      blocks. All other types, arrays, and structures
4092b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	  *      allowed for uniforms are allowed within a uniform
4093b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	  *      block."
4094b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	  */
4095b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	 if (var->type->contains_sampler()) {
4096b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	    YYLTYPE loc = decl_list->get_location();
4097b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	    _mesa_glsl_error(&loc, state,
4098b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt			     "Uniform in non-default uniform block contains sampler\n");
4099b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt	 }
4100b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt      }
4101b3c093c79c2ec49c36af37aa290d5ae452149f6eEric Anholt
4102f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt      instructions->append_list(&declared_variables);
4103f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt   }
4104f7561e8ecd80e915150ca63c0c79a5f9839c8e12Eric Anholt
41052d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt   return NULL;
41062d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt}
41072d03f48a65a666ecdcfaffa3d39ad1b77f2b25b6Eric Anholt
41084b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholtstatic void
41094b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholtdetect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
41104b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt			       exec_list *instructions)
41114b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt{
41124b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   bool gl_FragColor_assigned = false;
41134b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   bool gl_FragData_assigned = false;
41144b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   bool user_defined_fs_output_assigned = false;
41154b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   ir_variable *user_defined_fs_output = NULL;
41164b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
41174b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   /* It would be nice to have proper location information. */
41184b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   YYLTYPE loc;
41194b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   memset(&loc, 0, sizeof(loc));
41204b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
41214b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   foreach_list(node, instructions) {
41224b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      ir_variable *var = ((ir_instruction *)node)->as_variable();
41234b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
4124b2ee5a08bae6cdbbdafc1f1d9d6f3afbad2f7944Eric Anholt      if (!var || !var->assigned)
41254b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt	 continue;
41264b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
41274b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      if (strcmp(var->name, "gl_FragColor") == 0)
4128b2ee5a08bae6cdbbdafc1f1d9d6f3afbad2f7944Eric Anholt	 gl_FragColor_assigned = true;
41294b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      else if (strcmp(var->name, "gl_FragData") == 0)
4130b2ee5a08bae6cdbbdafc1f1d9d6f3afbad2f7944Eric Anholt	 gl_FragData_assigned = true;
41314b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      else if (strncmp(var->name, "gl_", 3) != 0) {
41324b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt	 if (state->target == fragment_shader &&
41334b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt	     (var->mode == ir_var_out || var->mode == ir_var_inout)) {
41344b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt	    user_defined_fs_output_assigned = true;
41354b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt	    user_defined_fs_output = var;
41364b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt	 }
41374b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      }
41384b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   }
41394b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
41404b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   /* From the GLSL 1.30 spec:
41414b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *
41424b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *     "If a shader statically assigns a value to gl_FragColor, it
41434b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      may not assign a value to any element of gl_FragData. If a
41444b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      shader statically writes a value to any element of
41454b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      gl_FragData, it may not assign a value to
41464b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      gl_FragColor. That is, a shader may assign values to either
41474b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      gl_FragColor or gl_FragData, but not both. Multiple shaders
41484b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      linked together must also consistently write just one of
41494b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      these variables.  Similarly, if user declared output
41504b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      variables are in use (statically assigned to), then the
41514b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      built-in variables gl_FragColor and gl_FragData may not be
41524b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      assigned to. These incorrect usages all generate compile
41534b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      time errors."
41544b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    */
41554b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   if (gl_FragColor_assigned && gl_FragData_assigned) {
41564b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      _mesa_glsl_error(&loc, state, "fragment shader writes to both "
41574b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt		       "`gl_FragColor' and `gl_FragData'\n");
41584b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
41594b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      _mesa_glsl_error(&loc, state, "fragment shader writes to both "
41604b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt		       "`gl_FragColor' and `%s'\n",
41614b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt		       user_defined_fs_output->name);
41624b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
41634b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      _mesa_glsl_error(&loc, state, "fragment shader writes to both "
41644b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt		       "`gl_FragData' and `%s'\n",
41654b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt		       user_defined_fs_output->name);
41664b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   }
41674b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt}
4168