ast_to_hir.cpp revision 5d6ea16dfe99e1aba61c25a897b66951faab1a39
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);
763e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   instructions->push_tail(new(ctx) ir_assignment(deref_var,
7641660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  rhs,
7651660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  NULL));
766e33c10328caec29616a5433b1d1df9088f3a84dfEric Anholt   deref_var = new(ctx) ir_dereference_variable(var);
7672731a739d047e4aadc1cab4bcf8c01c1cf8e86dbEric Anholt
7688e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick   if (!error_emitted)
7698e9ce2eb56a087c2544112700ae1abe3f96648ddIan Romanick      instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
77010a685218610e737e23d2d8a243ed6ff6613becdEric Anholt
7711660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
77210a685218610e737e23d2d8a243ed6ff6613becdEric Anholt}
7730bb1c3c1539fcadaa90d592a296c2ff1de3787a4Ian Romanick
774de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholtstatic ir_rvalue *
775959a9ecdd8fbc3375e4149f2b44d253622ff12eeEric Anholtget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
776de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt{
777d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8Kenneth Graunke   void *ctx = ralloc_parent(lvalue);
778de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   ir_variable *var;
779de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
7807e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
7817e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick			      ir_var_temporary);
78243b5b03d67ce890e867c81d4a5cfc4871d711d43Eric Anholt   instructions->push_tail(var);
783de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   var->mode = ir_var_auto;
784de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
7851660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
7861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						  lvalue, NULL));
787de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
7881660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_dereference_variable(var);
789de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt}
790de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
791de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
792fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
7930044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_node::hir(exec_list *instructions,
79418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick	      struct _mesa_glsl_parse_state *state)
79518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick{
79618238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) instructions;
79718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   (void) state;
79818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
79918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   return NULL;
80018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick}
80118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
802ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholtstatic ir_rvalue *
803ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholtdo_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
804ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt{
805ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   int join_op;
8066d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick   ir_rvalue *cmp = NULL;
807ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
808ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   if (operation == ir_binop_all_equal)
809ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      join_op = ir_binop_logic_and;
810ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   else
811ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      join_op = ir_binop_logic_or;
812ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
813ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   switch (op0->type->base_type) {
814ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_FLOAT:
815ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_UINT:
816ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_INT:
817ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_BOOL:
818ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      return new(mem_ctx) ir_expression(operation, op0, op1);
819ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
820ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_ARRAY: {
821ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      for (unsigned int i = 0; i < op0->type->length; i++) {
822ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 ir_rvalue *e0, *e1, *result;
823ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
824ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
825ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt						new(mem_ctx) ir_constant(i));
826ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
827ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt						new(mem_ctx) ir_constant(i));
828ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 result = do_comparison(mem_ctx, operation, e0, e1);
829ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
8306d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	 if (cmp) {
8316d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	    cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
832ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 } else {
8336d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	    cmp = result;
834ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 }
835ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      }
836b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt
837b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt      mark_whole_array_access(op0);
838b4f585665c31b1f80d909e38b3b2a9fab0c03076Eric Anholt      mark_whole_array_access(op1);
8396d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      break;
840ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   }
841ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
842ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_STRUCT: {
843ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      for (unsigned int i = 0; i < op0->type->length; i++) {
844ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 ir_rvalue *e0, *e1, *result;
845ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 const char *field_name = op0->type->fields.structure[i].name;
846ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
847ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
848ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt						 field_name);
849ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
850ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt						 field_name);
851ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 result = do_comparison(mem_ctx, operation, e0, e1);
852ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
8536d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	 if (cmp) {
8546d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	    cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
855ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 } else {
8566d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick	    cmp = result;
857ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt	 }
858ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      }
8596d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      break;
860ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   }
861ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt
862ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_ERROR:
863ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_VOID:
864ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   case GLSL_TYPE_SAMPLER:
865ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt      /* I assume a comparison of a struct containing a sampler just
866ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt       * ignores the sampler present in the type.
867ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt       */
8686d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      break;
8696d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick
8706d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick   default:
8716d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      assert(!"Should not get here.");
8726d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      break;
873ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt   }
874d56c97413ee65e40e3544b89ffca450df9ba1c06Eric Anholt
8756d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick   if (cmp == NULL)
8766d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick      cmp = new(mem_ctx) ir_constant(true);
8776d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick
8786d36be508ff0765beb6cf6bb95a323ff01e458ddIan Romanick   return cmp;
879ff79633d9f930e396933a0ad9564824ec73ea4dcEric Anholt}
88018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick
88101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt/* For logical operations, we want to ensure that the operands are
88201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt * scalar booleans.  If it isn't, emit an error and return a constant
88301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt * boolean to avoid triggering cascading error messages.
88401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt */
88501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholtir_rvalue *
88601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholtget_scalar_boolean_operand(exec_list *instructions,
88701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   struct _mesa_glsl_parse_state *state,
88801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   ast_expression *parent_expr,
88901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   int operand,
89001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   const char *operand_name,
89101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt			   bool *error_emitted)
89201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt{
89301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   ast_expression *expr = parent_expr->subexpressions[operand];
89401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   void *ctx = state;
89501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   ir_rvalue *val = expr->hir(instructions, state);
89601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt
89701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   if (val->type->is_boolean() && val->type->is_scalar())
89801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      return val;
89901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt
90001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   if (!*error_emitted) {
90101822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      YYLTYPE loc = expr->get_location();
90201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      _mesa_glsl_error(&loc, state, "%s of `%s' must be scalar boolean",
90301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt		       operand_name,
90401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt		       parent_expr->operator_string(parent_expr->oper));
90501822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      *error_emitted = true;
90601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   }
90701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt
90801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt   return new(ctx) ir_constant(true);
90901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt}
91001822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt
91193b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry/**
91293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry * If name refers to a builtin array whose maximum allowed size is less than
91393b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry * size, report an error and return true.  Otherwise return false.
91493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry */
91593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berrystatic bool
91693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berrycheck_builtin_array_max_size(const char *name, unsigned size,
91793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                             YYLTYPE loc, struct _mesa_glsl_parse_state *state)
91893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry{
91993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry   if ((strcmp("gl_TexCoord", name) == 0)
92093b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       && (size > state->Const.MaxTextureCoords)) {
92193b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
92293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       *
92393b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       *     "The size [of gl_TexCoord] can be at most
92493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       *     gl_MaxTextureCoords."
92593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry       */
92693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      _mesa_glsl_error(&loc, state, "`gl_TexCoord' array size cannot "
92793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                       "be larger than gl_MaxTextureCoords (%u)\n",
92893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                       state->Const.MaxTextureCoords);
92993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      return true;
93037bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry   } else if (strcmp("gl_ClipDistance", name) == 0
93137bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry              && size > state->Const.MaxClipPlanes) {
93237bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry      /* From section 7.1 (Vertex Shader Special Variables) of the
93337bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       * GLSL 1.30 spec:
93437bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *
93537bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   "The gl_ClipDistance array is predeclared as unsized and
93637bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   must be sized by the shader either redeclaring it with a
93737bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   size or indexing it only with integral constant
93837bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   expressions. ... The size can be at most
93937bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       *   gl_MaxClipDistances."
94037bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry       */
94137bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry      _mesa_glsl_error(&loc, state, "`gl_ClipDistance' array size cannot "
94237bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry                       "be larger than gl_MaxClipDistances (%u)\n",
94337bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry                       state->Const.MaxClipPlanes);
94437bb1c4de2eb2fa80d09b6e8dc8f39814d790e09Paul Berry      return true;
94593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry   }
94693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry   return false;
94793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry}
94893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry
9499abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry/**
9509abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * Create the constant 1, of a which is appropriate for incrementing and
9519abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * decrementing values of the given GLSL type.  For example, if type is vec4,
9529abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * this creates a constant value of 1.0 having type float.
9539abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry *
9549abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * If the given type is invalid for increment and decrement operators, return
9559abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry * a floating point 1--the error will be detected later.
9569abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry */
9579abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berrystatic ir_rvalue *
9589abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berryconstant_one_for_inc_dec(void *ctx, const glsl_type *type)
9599abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry{
9609abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   switch (type->base_type) {
9619abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   case GLSL_TYPE_UINT:
9629abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      return new(ctx) ir_constant((unsigned) 1);
9639abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   case GLSL_TYPE_INT:
9649abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      return new(ctx) ir_constant(1);
9659abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   default:
9669abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   case GLSL_TYPE_FLOAT:
9679abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      return new(ctx) ir_constant(1.0f);
9689abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry   }
9699abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry}
9709abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry
971fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
9720044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression::hir(exec_list *instructions,
97318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick		    struct _mesa_glsl_parse_state *state)
974a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
975953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
976a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   static const int operations[AST_NUM_OPERATORS] = {
977a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_assign doesn't convert to ir_expression. */
978a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_plus doesn't convert to ir_expression. */
979a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_neg,
980a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,
981a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,
982a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,
983a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,
984a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,
985a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,
986a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,
987a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_less,
988a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_greater,
989a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lequal,
990a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_gequal,
9914dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri      ir_binop_all_equal,
9924dfb89904c0a3d2166e9a3fc0253a254680e91bcLuca Barbieri      ir_binop_any_nequal,
993a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and,
994a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor,
995a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,
996a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_bit_not,
997a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_and,
998a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_xor,
999a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_logic_or,
1000a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_unop_logic_not,
1001a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1002a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Note: The following block of expression types actually convert
1003a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * to multiple IR instructions.
1004a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1005a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mul,     /* ast_mul_assign */
1006a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_div,     /* ast_div_assign */
1007a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_mod,     /* ast_mod_assign */
1008a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_add,     /* ast_add_assign */
1009a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_sub,     /* ast_sub_assign */
1010a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_lshift,  /* ast_ls_assign */
1011a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_rshift,  /* ast_rs_assign */
1012a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_and, /* ast_and_assign */
1013a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_xor, /* ast_xor_assign */
1014a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      ir_binop_bit_or,  /* ast_or_assign */
1015a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1016a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_conditional doesn't convert to ir_expression. */
1017de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_pre_inc. */
1018de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_pre_dec. */
1019de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_add,     /* ast_post_inc. */
1020de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      ir_binop_sub,     /* ast_post_dec. */
1021a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_field_selection doesn't conv to ir_expression. */
1022a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_array_index doesn't convert to ir_expression. */
1023a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_function_call doesn't conv to ir_expression. */
1024a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_identifier doesn't convert to ir_expression. */
1025a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_int_constant doesn't convert to ir_expression. */
1026a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
1027a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_float_constant doesn't conv to ir_expression. */
1028a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
1029a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      -1,               /* ast_sequence doesn't convert to ir_expression. */
1030a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   };
1031fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunke   ir_rvalue *result = NULL;
10321c325af4d6b907e0a47ab7f868a2a78f054f153fAras Pranckevicius   ir_rvalue *op[3];
103308ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke   const struct glsl_type *type; /* a temporary variable for switch cases */
1034a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   bool error_emitted = false;
1035a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   YYLTYPE loc;
1036a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
103718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   loc = this->get_location();
1038a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
103918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   switch (this->oper) {
10406652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   case ast_assign: {
104118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
104218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1043a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1044e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      result = do_assignment(instructions, state,
1045e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
1046e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     op[0], op[1], false,
104710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
104810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = result->type->is_error();
1049a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
10506652af36fe8994b1621d882fcc230d320908a2a3Ian Romanick   }
1051a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1052a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_plus:
105318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
1054a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1055c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1056c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth
1057c24bcad9f88379ffba9e2f0ff92f22cdf60c2927Carl Worth      error_emitted = type->is_error();
1058a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1059a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      result = op[0];
1060a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1061a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1062a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_neg:
106318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
1064a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
106565e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
1066a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
106765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
1068a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10691660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
10701660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
1071a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1072a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1073a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add:
1074a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub:
1075a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul:
1076a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div:
107718238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
107818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1079a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1080bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
108118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul),
1082a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
1083a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      error_emitted = type->is_error();
1084a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10851660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
10861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
1087a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1088a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1089a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mod:
109018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
109118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1092a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
109365e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
1094a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
109518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      assert(operations[this->oper] == ir_binop_mod);
1096a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
10971660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
10981660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
109965e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
1100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1101a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1102a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lshift:
1103a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_rshift:
11045c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       if (state->language_version < 130) {
11055c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace          _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30",
11065c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace              operator_string(this->oper));
11075c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace          error_emitted = true;
11085c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       }
11095c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace
11105c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       op[0] = this->subexpressions[0]->hir(instructions, state);
11115c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       op[1] = this->subexpressions[1]->hir(instructions, state);
1112c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace       type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1113c0197ab0af94ed0f3b2d453fb1ee6589f15d8e9cChad Versace                                &loc);
11145c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       result = new(ctx) ir_expression(operations[this->oper], type,
11155c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace                                       op[0], op[1]);
11165c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
11175c4c36f7f3842e287b303b1eca8d260c37e3580bChad Versace       break;
1118a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1119a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_less:
1120a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_greater:
1121a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_lequal:
1122a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_gequal:
112318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
112418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1125a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
112665e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = relational_result_type(op[0], op[1], state, & loc);
1127a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1128a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The relational operators must either generate an error or result
1129a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
1130a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1131a43817a483a8c4a480ef4e6dfda2cef899300eb0Ian Romanick      assert(type->is_error()
1132a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	     || ((type->base_type == GLSL_TYPE_BOOL)
1133cb36f8aaeeb09660843316270a781948f773d90bIan Romanick		 && type->is_scalar()));
1134a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
11351660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], type,
11361660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
113765e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
1138a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1139a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1140a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_nequal:
1141a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_equal:
11426e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
11436e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
11446e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
11456e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
11466e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *
11476e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    "The equality operators equal (==), and not equal (!=)
11486e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    operate on all types. They result in a scalar Boolean. If
11496e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    the operand types do not match, then there must be a
11506e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    conversion from Section 4.1.10 "Implicit Conversions"
11516e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    applied to one operand that can make them match, in which
11526e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       *    case this conversion is done."
11536e659caaa946339a2de3890a8bed091ccb65102aIan Romanick       */
1154bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[0]->type, op[1], state)
1155bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[1]->type, op[0], state))
1156212b0327b47033442842a7be3d7fb10e08e2bf66Ian Romanick	  || (op[0]->type != op[1]->type)) {
11576e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
11586e659caaa946339a2de3890a8bed091ccb65102aIan Romanick			  "type", (this->oper == ast_equal) ? "==" : "!=");
11596e659caaa946339a2de3890a8bed091ccb65102aIan Romanick	 error_emitted = true;
1160a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick      } else if ((state->language_version <= 110)
1161a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick		 && (op[0]->type->is_array() || op[1]->type->is_array())) {
1162a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
1163a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick			  "GLSL 1.10");
1164a80cbd6d82f6ec4c1d16129581d5fa893a6ba94fIan Romanick	 error_emitted = true;
11656e659caaa946339a2de3890a8bed091ccb65102aIan Romanick      }
11666e659caaa946339a2de3890a8bed091ccb65102aIan Romanick
1167175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt      if (error_emitted) {
1168175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt	 result = new(ctx) ir_constant(false);
1169175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt      } else {
1170175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt	 result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
1171175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt	 assert(result->type == glsl_type::bool_type);
1172175829f1a8ab0df7594131cc569462e45c1974ecEric Anholt      }
1173a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1174a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1175a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_and:
1176a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_xor:
1177a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_or:
11781eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[0] = this->subexpressions[0]->hir(instructions, state);
11791eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[1] = this->subexpressions[1]->hir(instructions, state);
1180cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace      type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1181cfdbf8bc8497b29fbdd9fa7bd00da554aecb5962Chad Versace                                   state, &loc);
11821eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      result = new(ctx) ir_expression(operations[this->oper], type,
11831eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke				      op[0], op[1]);
11841eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
11851eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      break;
11861eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
1187a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bit_not:
11881eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      op[0] = this->subexpressions[0]->hir(instructions, state);
11891eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
11901eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (state->language_version < 130) {
11911eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
11921eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
11931eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
11941eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
11951eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      if (!op[0]->type->is_integer()) {
11961eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
11971eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke	 error_emitted = true;
11981eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      }
11991eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke
120039464489510270bbe472d11f7614c04ce1b6ae33Ian Romanick      type = error_emitted ? glsl_type::error_type : op[0]->type;
12011eea96326fa652029e3898e104c715e5464f11e4Kenneth Graunke      result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
1202a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1203a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
12044950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_and: {
12057ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt      exec_list rhs_instructions;
120601822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
120701822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt					 "LHS", &error_emitted);
12087ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt      op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
12097ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt					 "RHS", &error_emitted);
1210b82c0c31aea2d02721f162b94b9f591242d9364eEric Anholt
1211ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt      if (rhs_instructions.is_empty()) {
1212ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt	 result = new(ctx) ir_expression(ir_binop_logic_and, op[0], op[1]);
1213ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt	 type = result->type;
121444b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
121581d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
12167e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "and_tmp",
12177e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
121881d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(tmp);
121981d664f099a5fd5fac777480532fb4307d591451Ian Romanick
12201660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
122144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 instructions->push_tail(stmt);
12224950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12237ec0c9789669ac88fcdd66c562e6d58281b477ceEric Anholt	 stmt->then_instructions.append_list(&rhs_instructions);
12241660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
122544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
12261660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
122744b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
12284950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12291660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
123044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
12311660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
123244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
12334950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12341660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
123544b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
123644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
12374950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
12384950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
12394950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12404950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_or: {
12419e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt      exec_list rhs_instructions;
124201822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
124301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt					 "LHS", &error_emitted);
12449e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt      op[1] = get_scalar_boolean_operand(&rhs_instructions, state, this, 1,
12459e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt					 "RHS", &error_emitted);
12464950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
1247ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt      if (rhs_instructions.is_empty()) {
1248ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt	 result = new(ctx) ir_expression(ir_binop_logic_or, op[0], op[1]);
1249ead3589aa2810b66164178a1d55d2063cfa3b041Eric Anholt	 type = result->type;
125044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      } else {
1251dfd30ca6a95a7d95835dad78ffe1fba4d1f4ef69Kenneth Graunke	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
12527e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       "or_tmp",
12537e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick						       ir_var_temporary);
12540b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
12554950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
125681d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 ir_if *const stmt = new(ctx) ir_if(op[0]);
125781d664f099a5fd5fac777480532fb4307d591451Ian Romanick	 instructions->push_tail(stmt);
125881d664f099a5fd5fac777480532fb4307d591451Ian Romanick
12591660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
126044b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const then_assign =
12611660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
126244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->then_instructions.push_tail(then_assign);
12634950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12649e04b190b5f59c5b375645f5756a6edd98a7f90cEric Anholt	 stmt->else_instructions.append_list(&rhs_instructions);
12651660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
126644b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 ir_assignment *const else_assign =
12671660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[1], NULL);
126844b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 stmt->else_instructions.push_tail(else_assign);
12694950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12701660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
127144b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt	 type = tmp->type;
127244b694e1f621730bca1cd03eabdfe5474d818f18Eric Anholt      }
12734950a68bf22ede6f4f368c9783e5401816159574Eric Anholt      break;
12744950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   }
12754950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12764950a68bf22ede6f4f368c9783e5401816159574Eric Anholt   case ast_logic_xor:
1277756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt      /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1278756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       *
1279756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       *    "The logical binary operators and (&&), or ( | | ), and
1280756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       *     exclusive or (^^). They operate only on two Boolean
1281756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       *     expressions and result in a Boolean expression."
1282756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt       */
1283756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0, "LHS",
1284756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt					 &error_emitted);
1285756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt      op[1] = get_scalar_boolean_operand(instructions, state, this, 1, "RHS",
1286756c262756b2434efeb2c2a33a180fda0757a6e5Eric Anholt					 &error_emitted);
12874950a68bf22ede6f4f368c9783e5401816159574Eric Anholt
12881660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
12891660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], op[1]);
1290a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1291a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1292a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt   case ast_logic_not:
129301822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
129401822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt					 "operand", &error_emitted);
1295a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
12961660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
12971660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				      op[0], NULL);
1298a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt      break;
1299a5827fe8d06a1161ef3b4e2b3296431a55d4ba2eEric Anholt
1300a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_mul_assign:
1301a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_div_assign:
1302a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_add_assign:
1303a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sub_assign: {
130418238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[0] = this->subexpressions[0]->hir(instructions, state);
130518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      op[1] = this->subexpressions[1]->hir(instructions, state);
1306a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1307bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      type = arithmetic_result_type(op[0], op[1],
130818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick				    (this->oper == ast_mul_assign),
1309a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt				    state, & loc);
1310a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
13111660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
13121660a2954797e056caba319c5d6c70b0d4be22feCarl Worth						   op[0], op[1]);
1313a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
13143e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
1315e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
131685caea29c18fad89050ac366c558afef568dcb3fIan Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
131710a685218610e737e23d2d8a243ed6ff6613becdEric Anholt			     this->subexpressions[0]->get_location());
131810a685218610e737e23d2d8a243ed6ff6613becdEric Anholt      error_emitted = (op[0]->type->is_error());
1319a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1320a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
1321a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * explicitly test for this because none of the binary expression
1322a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * operators allow array operands either.
1323a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1325a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1326a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1327a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
132848a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   case ast_mod_assign: {
132948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
133048a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      op[1] = this->subexpressions[1]->hir(instructions, state);
133148a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
133265e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
133348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
133448a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      assert(operations[this->oper] == ir_binop_mod);
133548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
1336768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
13371660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
13381660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
133948a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt
13403e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
1341e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
134285caea29c18fad89050ac366c558afef568dcb3fIan Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
134348a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt			     this->subexpressions[0]->get_location());
134465e1a7ac6a6735e135851ddb87e48361d4677000Eric Anholt      error_emitted = type->is_error();
134548a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt      break;
134648a0e64b7d6a4308f9c691e5844165ec97f8282eEric Anholt   }
1347a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1348a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_ls_assign:
1349338ed6ec297d76746b6466c26c307722af965e60Chad Versace   case ast_rs_assign: {
1350338ed6ec297d76746b6466c26c307722af965e60Chad Versace      op[0] = this->subexpressions[0]->hir(instructions, state);
1351338ed6ec297d76746b6466c26c307722af965e60Chad Versace      op[1] = this->subexpressions[1]->hir(instructions, state);
1352338ed6ec297d76746b6466c26c307722af965e60Chad Versace      type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
1353338ed6ec297d76746b6466c26c307722af965e60Chad Versace                               &loc);
1354338ed6ec297d76746b6466c26c307722af965e60Chad Versace      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1355338ed6ec297d76746b6466c26c307722af965e60Chad Versace                                                   type, op[0], op[1]);
1356e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      result = do_assignment(instructions, state,
1357e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
1358e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
1359338ed6ec297d76746b6466c26c307722af965e60Chad Versace                             this->subexpressions[0]->get_location());
1360338ed6ec297d76746b6466c26c307722af965e60Chad Versace      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1361251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1362338ed6ec297d76746b6466c26c307722af965e60Chad Versace   }
1363a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1364a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_and_assign:
1365a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_xor_assign:
1366d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace   case ast_or_assign: {
1367d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      op[0] = this->subexpressions[0]->hir(instructions, state);
1368d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      op[1] = this->subexpressions[1]->hir(instructions, state);
1369d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
1370d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                                   state, &loc);
1371d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
1372d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                                                   type, op[0], op[1]);
1373e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick      result = do_assignment(instructions, state,
1374e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
1375e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
1376d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace                             this->subexpressions[0]->get_location());
1377d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1378251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
1379d03ac0f8d81fd3032d271586d936f14b7d9201d5Chad Versace   }
1380a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
138196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   case ast_conditional: {
138296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
138396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
138496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    "The ternary selection operator (?:). It operates on three
138596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
138696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *    first expression, which must result in a scalar Boolean."
138796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
138801822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt      op[0] = get_scalar_boolean_operand(instructions, state, this, 0,
138901822706ec2c2501a2cd2431a90c56b334b79a5cEric Anholt					 "condition", &error_emitted);
139096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
139196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* The :? operator is implemented by generating an anonymous temporary
139296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * followed by an if-statement.  The last instruction in each branch of
139396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * the if-statement assigns a value to the anonymous temporary.  This
139496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       * temporary is the r-value of the expression.
139596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
13960ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list then_instructions;
13970ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      exec_list else_instructions;
139896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
13990ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[1] = this->subexpressions[1]->hir(&then_instructions, state);
14000ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick      op[2] = this->subexpressions[2]->hir(&else_instructions, state);
140196f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
140296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
140396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *
140496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     "The second and third expressions can be any type, as
140596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     long their types match, or there is a conversion in
140696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     Section 4.1.10 "Implicit Conversions" that can be applied
140796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     to one of the expressions to make their types match. This
140896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     resulting matching type is the type of the entire
140996f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       *     expression."
141096f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick       */
1411bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick      if ((!apply_implicit_conversion(op[1]->type, op[2], state)
1412bfb09c2a94414c1b40108c9c41eb0844d932e459Ian Romanick	   && !apply_implicit_conversion(op[2]->type, op[1], state))
1413db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick	  || (op[1]->type != op[2]->type)) {
141496f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 YYLTYPE loc = this->subexpressions[1]->get_location();
141596f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
141696f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
141796f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick			  "operator must have matching types.");
141896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick	 error_emitted = true;
14190ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = glsl_type::error_type;
1420db9be2e7aa3a56e43b725ad7725fe6b424e4933eIan Romanick      } else {
14210ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick	 type = op[1]->type;
142296f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick      }
142396f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick
1424f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
1425f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *
1426f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *    "The second and third expressions must be the same type, but can
1427f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       *    be of any type other than an array."
1428f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick       */
1429f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      if ((state->language_version <= 110) && type->is_array()) {
1430f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
1431f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick			  "operator must not be arrays.");
1432f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick	 error_emitted = true;
1433f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick      }
1434f09fabc448c0781f0cf9160565e1b0bab59a16e5Ian Romanick
14357825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *cond_val = op[0]->constant_expression_value();
14367825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *then_val = op[1]->constant_expression_value();
14377825d3d15710fdfcfc503754862963aac8065480Ian Romanick      ir_constant *else_val = op[2]->constant_expression_value();
14387825d3d15710fdfcfc503754862963aac8065480Ian Romanick
14397825d3d15710fdfcfc503754862963aac8065480Ian Romanick      if (then_instructions.is_empty()
14407825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && else_instructions.is_empty()
14417825d3d15710fdfcfc503754862963aac8065480Ian Romanick	  && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
14427825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 result = (cond_val->value.b[0]) ? then_val : else_val;
14437825d3d15710fdfcfc503754862963aac8065480Ian Romanick      } else {
14447e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	 ir_variable *const tmp =
14457e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick	    new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
14460b9ae3befb0bf80e000b159fd44c961a144f9c36Ian Romanick	 instructions->push_tail(tmp);
14470ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
14481660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_if *const stmt = new(ctx) ir_if(op[0]);
14497825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 instructions->push_tail(stmt);
14500ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
14517825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 then_instructions.move_nodes_to(& stmt->then_instructions);
14521660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const then_deref =
14531660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
14547825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const then_assign =
14551660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(then_deref, op[1], NULL);
14567825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->then_instructions.push_tail(then_assign);
14570ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
14587825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 else_instructions.move_nodes_to(& stmt->else_instructions);
14591660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 ir_dereference *const else_deref =
14601660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_dereference_variable(tmp);
14617825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 ir_assignment *const else_assign =
14621660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	    new(ctx) ir_assignment(else_deref, op[2], NULL);
14637825d3d15710fdfcfc503754862963aac8065480Ian Romanick	 stmt->else_instructions.push_tail(else_assign);
14640ad76c67675c35a65a79752058f53eee74947ba5Ian Romanick
14651660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 result = new(ctx) ir_dereference_variable(tmp);
14667825d3d15710fdfcfc503754862963aac8065480Ian Romanick      }
1467251eb753187fee83e6413f44f8b3cf0be1b4f4cbIan Romanick      break;
146896f9cea11606bb1bd8e07edc17032447424b8bffIan Romanick   }
1469a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1470a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_pre_inc:
147176ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   case ast_pre_dec: {
1472fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick      this->non_lvalue_description = (this->oper == ast_pre_inc)
1473fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick	 ? "pre-increment operation" : "pre-decrement operation";
1474fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick
147576ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
14769abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
147776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1478a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
147976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
1480768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
14811660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
14821660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
148376ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt
14843e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      result = do_assignment(instructions, state,
1485e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			     this->subexpressions[0]->non_lvalue_description,
148685caea29c18fad89050ac366c558afef568dcb3fIan Romanick			     op[0]->clone(ctx, NULL), temp_rhs, false,
148776ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt			     this->subexpressions[0]->get_location());
148876ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      error_emitted = op[0]->type->is_error();
148976ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt      break;
149076ea56c007263ec3b79234e7b775e3a7b519a54aEric Anholt   }
1491a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1492a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_post_inc:
1493de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   case ast_post_dec: {
1494fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick      this->non_lvalue_description = (this->oper == ast_post_inc)
1495fa0a9ac5cdf49865cfc289c4326c73c9dd4a61c5Ian Romanick	 ? "post-increment operation" : "post-decrement operation";
1496de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      op[0] = this->subexpressions[0]->hir(instructions, state);
14979abda92b270596fd3a5e2e8b74e3fc3255da70cePaul Berry      op[1] = constant_one_for_inc_dec(ctx, op[0]->type);
1498de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1499de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
1500de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1501a13bb1490c57ea958f2d1853d71c55d03263e9e4Eric Anholt      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
1502de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1503768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_rvalue *temp_rhs;
15041660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
15051660a2954797e056caba319c5d6c70b0d4be22feCarl Worth					op[0], op[1]);
1506de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1507de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      /* Get a temporary of a copy of the lvalue before it's modified.
1508de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       * This may get thrown away later.
1509de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt       */
15108273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
1511de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
15123e24ef68a9b22918c8b21b743d81bbf86f43c119Eric Anholt      (void)do_assignment(instructions, state,
1513e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick			  this->subexpressions[0]->non_lvalue_description,
151485caea29c18fad89050ac366c558afef568dcb3fIan Romanick			  op[0]->clone(ctx, NULL), temp_rhs, false,
1515de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt			  this->subexpressions[0]->get_location());
1516de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt
1517de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt      error_emitted = op[0]->type->is_error();
1518a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1519de38f0ed53c80f87478c3899d4ff1bc3a601ea6bEric Anholt   }
1520a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1521a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_field_selection:
152218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
1523a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1524a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
152527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   case ast_array_index: {
152627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      YYLTYPE index_loc = subexpressions[1]->get_location();
152727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
152827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[0] = subexpressions[0]->hir(instructions, state);
152927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      op[1] = subexpressions[1]->hir(instructions, state);
153027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
153127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
153227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1533a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick      ir_rvalue *const array = op[0];
1534b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
15351660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_dereference_array(op[0], op[1]);
1536b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
1537b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      /* Do not use op[0] after this point.  Use array.
1538b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick       */
1539b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick      op[0] = NULL;
1540b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
154127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
154227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
154327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 break;
154427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
154563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick      if (!array->type->is_array()
154663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_matrix()
154763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	  && !array->type->is_vector()) {
154827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
154963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "cannot dereference non-array / non-matrix / "
155063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			  "non-vector");
155127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
155227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
155327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
155427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (!op[1]->type->is_integer()) {
155527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
155627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be integer type");
155727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
155827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      } else if (!op[1]->type->is_scalar()) {
155927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 _mesa_glsl_error(& index_loc, state,
156027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick			  "array index must be scalar");
156127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 error_emitted = true;
156227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
156327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
156427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      /* If the array index is a constant expression and the array has a
156527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size, ensure that the access is in-bounds.  If the array
156627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * index is not a constant expression, ensure that the array has a
156727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       * declared size.
156827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick       */
156927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      ir_constant *const const_index = op[1]->constant_expression_value();
157027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (const_index != NULL) {
157127e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 const int idx = const_index->value.i[0];
157263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 const char *type_name;
157363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 unsigned bound = 0;
157463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick
157563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
157663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "matrix";
157763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
157863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "vector";
157963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
158063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    type_name = "array";
158163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
158227e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
158327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
158427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *
158527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    "It is illegal to declare an array with a size, and then
158627e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    later (in the same shader) index the same array with an
158727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    integral constant expression greater than or equal to the
158827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    declared size. It is also illegal to index an array with a
158927e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  *    negative constant expression."
159027e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	  */
159163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_matrix()) {
159263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->row_type()->vector_elements <= idx) {
159363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->row_type()->vector_elements;
159463038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
159563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (array->type->is_vector()) {
159663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if (array->type->vector_elements <= idx) {
159763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->vector_elements;
159863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
159963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else {
160063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    if ((array->type->array_size() > 0)
160163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick		&& (array->type->array_size() <= idx)) {
160263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       bound = array->type->array_size();
160363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    }
160427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
160527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
160663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (bound > 0) {
160763038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be < %u",
160863038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name, bound);
160963038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    error_emitted = true;
161063038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 } else if (idx < 0) {
161163038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	    _mesa_glsl_error(& loc, state, "%s index must be >= 0",
161263038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick			     type_name);
161327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	    error_emitted = true;
161427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 }
1615b8a21cc6df7e9da15a24ed3dbf60cd4aeb8effaaIan Romanick
161663038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 if (array->type->is_array()) {
1617a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    /* If the array is a variable dereference, it dereferences the
1618a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * whole array, by definition.  Use this to get the variable.
1619a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     *
1620a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: Should some methods for getting / setting / testing
1621a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     * FINISHME: array access limits be added to ir_dereference?
1622a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	     */
1623a9159f9e87b518ba0a4ad43db8fdd58a678b3a92Ian Romanick	    ir_variable *const v = array->whole_variable_referenced();
162493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry	    if ((v != NULL) && (unsigned(idx) > v->max_array_access)) {
162563038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	       v->max_array_access = idx;
162693b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry
162793b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry               /* Check whether this access will, as a side effect, implicitly
162893b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                * cause the size of a built-in array to be too large.
162993b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                */
163093b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry               if (check_builtin_array_max_size(v->name, idx+1, loc, state))
163193b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry                  error_emitted = true;
163293b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry            }
163363038e18ad44e4e021da9a6dbe7a075e57ff6415Ian Romanick	 }
16342b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke      } else if (array->type->array_size() == 0) {
16352b7c42b40ae459f7b290eb134d6dabd075aab9f0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
1636a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt      } else {
1637a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 if (array->type->is_array()) {
16385226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	    /* whole_variable_referenced can return NULL if the array is a
16395226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * member of a structure.  In this case it is safe to not update
16405226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * the max_array_access field because it is never used for fields
16415226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     * of structures.
16425226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	     */
1643a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	    ir_variable *v = array->whole_variable_referenced();
16445226f8c7b0025031e8540adc93ecfe0b36b8f90fAras Pranckevicius	    if (v != NULL)
16450d9d036004f135c38990c60f46074b70cff6e663Ian Romanick	       v->max_array_access = array->type->array_size() - 1;
1646a721abfbd1724e83381b46fc670bb38fbde76f69Eric Anholt	 }
164727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      }
164827e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1649e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick      /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
1650e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       *
1651f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace       *    "Samplers aggregated into arrays within a shader (using square
1652f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace       *    brackets [ ]) can only be indexed with integral constant
1653f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace       *    expressions [...]."
1654e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       *
1655e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * This restriction was added in GLSL 1.30.  Shaders using earlier version
1656e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * of the language should not be rejected by the compiler front-end for
1657e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * using this construct.  This allows useful things such as using a loop
1658e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * counter as the index to an array of samplers.  If the loop in unrolled,
1659e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick       * the code should compile correctly.  Instead, emit a warning.
1660f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace       */
1661f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace      if (array->type->is_array() &&
1662f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace          array->type->element_type()->is_sampler() &&
1663f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace          const_index == NULL) {
1664f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace
1665e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	 if (state->language_version == 100) {
1666e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	    _mesa_glsl_warning(&loc, state,
1667e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "sampler arrays indexed with non-constant "
1668e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "expressions is optional in GLSL ES 1.00");
1669e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	 } else if (state->language_version < 130) {
1670e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	    _mesa_glsl_warning(&loc, state,
1671e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "sampler arrays indexed with non-constant "
1672e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "expressions is forbidden in GLSL 1.30 and "
1673e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			       "later");
1674e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	 } else {
1675e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	    _mesa_glsl_error(&loc, state,
1676e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			     "sampler arrays indexed with non-constant "
1677e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			     "expressions is forbidden in GLSL 1.30 and "
1678e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick			     "later");
1679e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	    error_emitted = true;
1680e942f328365309a1d8240cfe8eb5d88391015f37Ian Romanick	 }
1681f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace      }
1682f0f2ec4d8a50c79c2943ac95eb790fb734d88980Chad Versace
168327e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick      if (error_emitted)
168427e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick	 result->type = glsl_type::error_type;
168527e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick
1686a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
168727e3cf8c0d8812f9be55ca6ceb52cf8232742d99Ian Romanick   }
1688a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1689a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_function_call:
16907cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      /* Should *NEVER* get here.  ast_function_call should always be handled
16917cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick       * by ast_function_expression::hir.
1692a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
16937cfddf19413ef61fcf1450bd61e9ece4cf1735a4Ian Romanick      assert(0);
1694a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1695a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1696a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_identifier: {
1697a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* ast_identifier can appear several places in a full abstract syntax
1698a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * tree.  This particular use must be at location specified in the grammar
1699a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * as 'variable_identifier'.
1700a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
17018bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      ir_variable *var =
17028bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick	 state->symbols->get_variable(this->primary_expression.identifier);
1703a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1704a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (var != NULL) {
1705bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	 var->used = true;
1706dca19a771156685895892740f687cee7cf84a8c9Kenneth Graunke	 result = new(ctx) ir_dereference_variable(var);
1707a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
170871d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
170918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->primary_expression.identifier);
1710a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1711807e967c615dc80a264af5a89af7649f95481744Kenneth Graunke	 result = ir_rvalue::error_value(ctx);
1712a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 error_emitted = true;
1713a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
1714a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1715a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1716a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1717a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_int_constant:
17181660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.int_constant);
1719a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1720a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1721a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_uint_constant:
17221660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.uint_constant);
1723a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1724a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1725a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_float_constant:
17261660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(this->primary_expression.float_constant);
1727a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1728a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1729a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_bool_constant:
17301660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
1731a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1732a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1733a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   case ast_sequence: {
1734a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* It should not be possible to generate a sequence in the AST without
1735a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * any expressions in it.
1736a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1737304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      assert(!this->expressions.is_empty());
1738a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1739a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* The r-value of a sequence is the last expression in the sequence.  If
1740a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * the other expressions in the sequence do not have side-effects (and
1741a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * therefore add instructions to the instruction list), they get dropped
1742a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * on the floor.
1743a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
17443d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick      exec_node *previous_tail_pred = NULL;
17453d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick      YYLTYPE previous_operand_loc = loc;
17463d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick
17473d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick      foreach_list_typed (ast_node, ast, link, &this->expressions) {
17483d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 /* If one of the operands of comma operator does not generate any
17493d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * code, we want to emit a warning.  At each pass through the loop
17503d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * previous_tail_pred will point to the last instruction in the
17513d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * stream *before* processing the previous operand.  Naturally,
17523d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * instructions->tail_pred will point to the last instruction in the
17533d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * stream *after* processing the previous operand.  If the two
17543d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * pointers match, then the previous operand had no effect.
17553d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  *
17563d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * The warning behavior here differs slightly from GCC.  GCC will
17573d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * only emit a warning if none of the left-hand operands have an
17583d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * effect.  However, it will emit a warning for each.  I believe that
17593d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * there are some cases in C (especially with GCC extensions) where
17603d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * it is useful to have an intermediate step in a sequence have no
17613d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * effect, but I don't think these cases exist in GLSL.  Either way,
17623d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * it would be a giant hassle to replicate that behavior.
17633d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  */
17643d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 if (previous_tail_pred == instructions->tail_pred) {
17653d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	    _mesa_glsl_warning(&previous_operand_loc, state,
17663d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick			       "left-hand operand of comma expression has "
17673d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick			       "no effect");
17683d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 }
17693d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick
17703d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 /* tail_pred is directly accessed instead of using the get_tail()
17713d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * method for performance reasons.  get_tail() has extra code to
17723d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * return NULL when the list is empty.  We don't care about that
17733d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  * here, so using tail_pred directly is fine.
17743d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	  */
17753d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 previous_tail_pred = instructions->tail_pred;
17763d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick	 previous_operand_loc = ast->get_location();
17773d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick
1778304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick	 result = ast->hir(instructions, state);
17793d5cfcfed16c5a79bdf67027afe4ea8058b899cbIan Romanick      }
1780a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1781a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* Any errors should have already been emitted in the loop above.
1782a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
1783a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      error_emitted = true;
1784a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      break;
1785a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1786a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
178708ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke   type = NULL; /* use result->type, not type. */
178808ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke   assert(result != NULL);
1789a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
179008ba9778dbf3326bdfc8ca8035b51412032748eaKenneth Graunke   if (result->type->is_error() && !error_emitted)
179171d0bbfcb2853f37b580ec7b705e55bb0eb426faIan Romanick      _mesa_glsl_error(& loc, state, "type mismatch");
1792a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1793a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return result;
1794a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1795a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1796a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1797fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
17980044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_expression_statement::hir(exec_list *instructions,
179918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
1800a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1801a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* It is possible to have expression statements that don't have an
1802a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * expression.  This is the solitary semicolon:
1803a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1804a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * for (i = 0; i < 5; i++)
1805a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *     ;
1806a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    *
1807a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * In this case the expression will be NULL.  Test for NULL and don't do
1808a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * anything in that case.
1809a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
181018238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (expression != NULL)
181118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick      expression->hir(instructions, state);
1812a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1813a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Statements do not have r-values.
1814a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1815a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1816a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1817a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1818a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1819fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
18200044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_compound_statement::hir(exec_list *instructions,
182118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			    struct _mesa_glsl_parse_state *state)
1822a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
182318238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
18248bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->push_scope();
1825a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
18262b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_node, ast, link, &this->statements)
1827304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      ast->hir(instructions, state);
1828a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
182918238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   if (new_scope)
18308bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick      state->symbols->pop_scope();
1831a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1832a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Compound statements do not have r-values.
1833a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
1834a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
1835a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1836a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1837a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
183828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanickstatic const glsl_type *
1839d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunkeprocess_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
184028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick		   struct _mesa_glsl_parse_state *state)
184128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick{
184228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   unsigned length = 0;
184328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1844a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick   /* From page 19 (page 25) of the GLSL 1.20 spec:
1845a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick    *
1846a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick    *     "Only one-dimensional arrays may be declared."
1847a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick    */
1848a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick   if (base->is_array()) {
1849a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick      _mesa_glsl_error(loc, state,
1850a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick		       "invalid array of `%s' (only one-dimensional arrays "
1851a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick		       "may be declared)",
1852a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick		       base->name);
1853a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick      return glsl_type::error_type;
1854a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick   }
185528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
185628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   if (array_size != NULL) {
185728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      exec_list dummy_instructions;
185828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
185928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      YYLTYPE loc = array_size->get_location();
186028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
186128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      if (ir != NULL) {
186228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 if (!ir->type->is_integer()) {
186328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be integer type");
186428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else if (!ir->type->is_scalar()) {
186528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    _mesa_glsl_error(& loc, state, "array size must be scalar type");
186628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 } else {
186728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    ir_constant *const size = ir->constant_expression_value();
186828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
186928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    if (size == NULL) {
187028009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be a "
187128009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick				"constant valued expression");
187228009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else if (size->value.i[0] <= 0) {
187328009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       _mesa_glsl_error(& loc, state, "array size must be > 0");
187428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    } else {
187528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       assert(size->type == ir->type);
187628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	       length = size->value.u[0];
1877d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry
1878d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry               /* If the array size is const (and we've verified that
1879d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                * it is) then no instructions should have been emitted
1880d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                * when we converted it to HIR.  If they were emitted,
1881d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                * then either the array size isn't const after all, or
1882d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                * we are emitting unnecessary instructions.
1883d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry                */
1884d4144a123b603d3c33cb356cf3c8e5ae4653594ePaul Berry               assert(dummy_instructions.is_empty());
188528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	    }
188628009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick	 }
188728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick      }
1888d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke   } else if (state->es_shader) {
1889d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke      /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
1890d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke       * array declarations have been removed from the language.
1891d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke       */
1892d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke      _mesa_glsl_error(loc, state, "unsized array declarations are not "
1893d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke		       "allowed in GLSL ES 1.00.");
189428009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick   }
189528009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1896f38d15b80d4e4c8ecb7a76087cdc49835f0aa271Ian Romanick   return glsl_type::get_array_instance(base, length);
189728009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick}
189828009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
189928009cd75cd3328774bd80a5b87a255ac881a710Ian Romanick
1900d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickconst glsl_type *
1901d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanickast_type_specifier::glsl_type(const char **name,
1902d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick			      struct _mesa_glsl_parse_state *state) const
1903a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1904d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   const struct glsl_type *type;
1905a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1906ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke   type = state->symbols->get_type(this->type_name);
1907ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke   *name = this->type_name;
1908a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1909ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke   if (this->is_array) {
1910ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke      YYLTYPE loc = this->get_location();
1911ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke      type = process_array_type(&loc, type, this->array_size, state);
1912a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
1913a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1914a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return type;
1915a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
1916a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1917a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1918a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickstatic void
1919a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanickapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
1920768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick				 ir_variable *var,
19212e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 struct _mesa_glsl_parse_state *state,
19222e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				 YYLTYPE *loc)
1923a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
1924bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick   if (qual->flags.q.invariant) {
1925bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick      if (var->used) {
1926bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	 _mesa_glsl_error(loc, state,
1927bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			  "variable `%s' may not be redeclared "
1928bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			  "`invariant' after being used",
1929bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			  var->name);
1930bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick      } else {
1931bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	 var->invariant = 1;
1932bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick      }
1933bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick   }
1934a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1935e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.constant || qual->flags.q.attribute
1936e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick       || qual->flags.q.uniform
1937e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick       || (qual->flags.q.varying && (state->target == fragment_shader)))
1938a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->read_only = 1;
1939a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1940e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.centroid)
1941a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->centroid = 1;
1942a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
1943e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.attribute && state->target != vertex_shader) {
19442e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      var->type = glsl_type::error_type;
19452e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      _mesa_glsl_error(loc, state,
19462e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt		       "`attribute' variables may not be declared in the "
1947ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       "%s shader",
1948ae4c4c07959f059e557ea44e98552ced1fec9f47Ian Romanick		       _mesa_glsl_shader_target_name(state->target));
19492e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   }
19502e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt
195190b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
195290b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *
195390b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     "The varying qualifier can be used only with the data types
195490b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
195590b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    *     these."
195690b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt    */
1957e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.varying) {
19580ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      const glsl_type *non_array_type;
19590ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
19600ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (var->type && var->type->is_array())
19610ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type->fields.array;
19620ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      else
19630ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 non_array_type = var->type;
19640ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt
19650ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
19660ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 var->type = glsl_type::error_type;
19670ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt	 _mesa_glsl_error(loc, state,
19680ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt			  "varying variables must be of base type float");
19690ca171908d04732176cbcaf2625fed8208a93dc9Eric Anholt      }
197090b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt   }
197190b7825b0e92375dbe721d2dca1a4a3f1093f4abEric Anholt
19727e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   /* If there is no qualifier that changes the mode of the variable, leave
19737e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    * the setting alone.
19747e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick    */
1975e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.in && qual->flags.q.out)
1976a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_inout;
1977e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.attribute || qual->flags.q.in
1978e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	    || (qual->flags.q.varying && (state->target == fragment_shader)))
1979a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_in;
1980e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.out
1981e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	    || (qual->flags.q.varying && (state->target == vertex_shader)))
1982a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_out;
1983e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.uniform)
1984a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      var->mode = ir_var_uniform;
1985a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
198686b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick   if (state->all_invariant && (state->current_function == NULL)) {
198786b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      switch (state->target) {
198886b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      case vertex_shader:
198986b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 if (var->mode == ir_var_out)
199086b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	    var->invariant = true;
199186b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 break;
199286b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      case geometry_shader:
199386b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 if ((var->mode == ir_var_in) || (var->mode == ir_var_out))
199486b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	    var->invariant = true;
199586b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 break;
199686b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      case fragment_shader:
199786b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 if (var->mode == ir_var_in)
199886b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	    var->invariant = true;
199986b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick	 break;
200086b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick      }
200186b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick   }
200286b4398cd158024f6be9fa830554a11c2a7ebe0cIan Romanick
2003e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if (qual->flags.q.flat)
2004cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry      var->interpolation = INTERP_QUALIFIER_FLAT;
2005e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   else if (qual->flags.q.noperspective)
2006cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry      var->interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
2007c488150dea083a9677429b4185c6b20d7facd52bPaul Berry   else if (qual->flags.q.smooth)
2008cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry      var->interpolation = INTERP_QUALIFIER_SMOOTH;
2009c488150dea083a9677429b4185c6b20d7facd52bPaul Berry   else
2010c488150dea083a9677429b4185c6b20d7facd52bPaul Berry      var->interpolation = INTERP_QUALIFIER_NONE;
20119d975377ca6dae7805804c0fbe625bb7c5f9e095Ian Romanick
2012916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt   if (var->interpolation != INTERP_QUALIFIER_NONE &&
2013916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt       !(state->target == vertex_shader && var->mode == ir_var_out) &&
2014916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt       !(state->target == fragment_shader && var->mode == ir_var_in)) {
2015916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      const char *qual_string = NULL;
2016916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      switch (var->interpolation) {
2017916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      case INTERP_QUALIFIER_FLAT:
2018916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt	 qual_string = "flat";
2019916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt	 break;
2020916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      case INTERP_QUALIFIER_NOPERSPECTIVE:
2021916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt	 qual_string = "noperspective";
2022916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt	 break;
2023916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      case INTERP_QUALIFIER_SMOOTH:
2024916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt	 qual_string = "smooth";
2025916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt	 break;
2026916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      }
2027916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt
2028916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt      _mesa_glsl_error(loc, state,
2029916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt		       "interpolation qualifier `%s' can only be applied to "
2030916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt		       "vertex shader outputs and fragment shader inputs.",
2031916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt		       qual_string);
2032916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt
2033916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt   }
2034916e206ef0cbcc5fa6c4026135e92079e1d73ec2Eric Anholt
2035e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   var->pixel_center_integer = qual->flags.q.pixel_center_integer;
2036e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   var->origin_upper_left = qual->flags.q.origin_upper_left;
2037e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick   if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
20388d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick       && (strcmp(var->name, "gl_FragCoord") != 0)) {
2039e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick      const char *const qual_string = (qual->flags.q.origin_upper_left)
20408d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick	 ? "origin_upper_left" : "pixel_center_integer";
20418d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
20428d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick      _mesa_glsl_error(loc, state,
20438d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "layout qualifier `%s' can only be applied to "
20448d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       "fragment shader input `gl_FragCoord'",
20458d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick		       qual_string);
20468d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick   }
20478d8469eb2ade4fd48188403351a38f740987fb80Ian Romanick
2048eee68d3631813580a14fa51fda6f0c959279256cIan Romanick   if (qual->flags.q.explicit_location) {
2049eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      const bool global_scope = (state->current_function == NULL);
2050eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      bool fail = false;
2051eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      const char *string = "";
2052eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
2053eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      /* In the vertex shader only shader inputs can be given explicit
2054eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       * locations.
2055eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       *
2056eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       * In the fragment shader only shader outputs can be given explicit
2057eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       * locations.
2058eee68d3631813580a14fa51fda6f0c959279256cIan Romanick       */
2059eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      switch (state->target) {
2060eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      case vertex_shader:
2061eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 if (!global_scope || (var->mode != ir_var_in)) {
2062eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    fail = true;
2063eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    string = "input";
2064eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 }
2065eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 break;
2066eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
2067eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      case geometry_shader:
2068eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 _mesa_glsl_error(loc, state,
2069eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "geometry shader variables cannot be given "
2070eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "explicit locations\n");
2071eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 break;
2072eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
2073eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      case fragment_shader:
2074b078aad8ab22d840456688480a8c27d4664297cePaul Berry	 if (!global_scope || (var->mode != ir_var_out)) {
2075eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    fail = true;
2076eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	    string = "output";
2077eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 }
2078eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 break;
2079a75da2c0e85eb6b8279ec895c3f74cc4aefc0257Kenneth Graunke      };
2080eee68d3631813580a14fa51fda6f0c959279256cIan Romanick
2081eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      if (fail) {
2082eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 _mesa_glsl_error(loc, state,
2083eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "only %s shader %s variables can be given an "
2084eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  "explicit location\n",
2085eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  _mesa_glsl_shader_target_name(state->target),
2086eee68d3631813580a14fa51fda6f0c959279256cIan Romanick			  string);
2087eee68d3631813580a14fa51fda6f0c959279256cIan Romanick      } else {
2088eee68d3631813580a14fa51fda6f0c959279256cIan Romanick	 var->explicit_location = true;
208968a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick
209068a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 /* This bit of silliness is needed because invalid explicit locations
209168a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * are supposed to be flagged during linking.  Small negative values
209268a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
209368a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
209468a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * The linker needs to be able to differentiate these cases.  This
209568a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  * ensures that negative values stay negative.
209668a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	  */
209768a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 if (qual->location >= 0) {
209868a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	    var->location = (state->target == vertex_shader)
209968a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	       ? (qual->location + VERT_ATTRIB_GENERIC0)
210068a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	       : (qual->location + FRAG_RESULT_DATA0);
210168a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 } else {
210268a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	    var->location = qual->location;
210368a4fc9d5a9dd3b61472451d659275531253b67dIan Romanick	 }
21041256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie	 if (qual->flags.q.explicit_index) {
21051256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie	    var->explicit_index = true;
21061256a5dcc86014d48bdc6fd10ea5a2fa11241667Dave Airlie	    var->index = qual->index;
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;
2196a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2197a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
21988e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick/**
21998e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * Get the variable that is being redeclared by this declaration
22008e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick *
22018e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * Semantic checks to verify the validity of the redeclaration are also
22028e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * performed.  If semantic checks fail, compilation error will be emitted via
22038e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * \c _mesa_glsl_error, but a non-\c NULL pointer will still be returned.
22048e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick *
22058e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * \returns
22068e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * A pointer to an existing variable in the current scope if the declaration
22078e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick * is a redeclaration, \c NULL otherwise.
22088e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick */
22098e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanickir_variable *
22108e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanickget_variable_being_redeclared(ir_variable *var, ast_declaration *decl,
22118e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			      struct _mesa_glsl_parse_state *state)
22128e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick{
22138e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   /* Check if this declaration is actually a re-declaration, either to
22148e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    * resize an array or add qualifiers to an existing variable.
22158e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    *
22168e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    * This is allowed for variables in the current scope, or when at
22178e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    * global scope (for built-ins in the implicit outer scope).
22188e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    */
22198e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   ir_variable *earlier = state->symbols->get_variable(decl->identifier);
22208e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   if (earlier == NULL ||
22218e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       (state->current_function != NULL &&
22228e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	!state->symbols->name_declared_this_scope(decl->identifier))) {
22238e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      return NULL;
22248e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   }
22258e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22268e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22278e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   YYLTYPE loc = decl->get_location();
22288e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22298e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
22308e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    *
22318e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    * "It is legal to declare an array without a size and then
22328e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    *  later re-declare the same name as an array of the same
22338e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    *  type and specify a size."
22348e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick    */
22358e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   if ((earlier->type->array_size() == 0)
22368e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       && var->type->is_array()
22378e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       && (var->type->element_type() == earlier->type->element_type())) {
22388e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* FINISHME: This doesn't match the qualifiers on the two
22398e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * FINISHME: declarations.  It's not 100% clear whether this is
22408e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * FINISHME: required or not.
22418e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       */
22428e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22438e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      const unsigned size = unsigned(var->type->array_size());
224493b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      check_builtin_array_max_size(var->name, size, loc, state);
224593b9758d01e2542ec3c2b8672cca0ae19b257aacPaul Berry      if ((size > 0) && (size <= earlier->max_array_access)) {
22468e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	 _mesa_glsl_error(& loc, state, "array size must be > %u due to "
22478e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "previous access",
22488e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  earlier->max_array_access);
22498e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      }
22508e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22518e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->type = var->type;
22528e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      delete var;
22538e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      var = NULL;
22548e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   } else if (state->ARB_fragment_coord_conventions_enable
22558e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && strcmp(var->name, "gl_FragCoord") == 0
22568e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->type == var->type
22578e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->mode == var->mode) {
22588e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
22598e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * qualifiers.
22608e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       */
22618e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->origin_upper_left = var->origin_upper_left;
22628e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->pixel_center_integer = var->pixel_center_integer;
22638e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22648e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* According to section 4.3.7 of the GLSL 1.30 spec,
22658e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * the following built-in varaibles can be redeclared with an
22668e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       * interpolation qualifier:
22678e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_FrontColor
22688e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_BackColor
22698e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_FrontSecondaryColor
22708e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_BackSecondaryColor
22718e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_Color
22728e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *    * gl_SecondaryColor
22738e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       */
22748e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   } else if (state->language_version >= 130
22758e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && (strcmp(var->name, "gl_FrontColor") == 0
22768e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_BackColor") == 0
22778e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_FrontSecondaryColor") == 0
22788e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_BackSecondaryColor") == 0
22798e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_Color") == 0
22808e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick		  || strcmp(var->name, "gl_SecondaryColor") == 0)
22818e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->type == var->type
22828e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->mode == var->mode) {
22838e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->interpolation = var->interpolation;
22848e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22858e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* Layout qualifiers for gl_FragDepth. */
22866b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák   } else if ((state->AMD_conservative_depth_enable ||
22876b43d6fdda13d73ca7b059f986ce2a2046bf03a0Marek Olšák               state->ARB_conservative_depth_enable)
22888e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && strcmp(var->name, "gl_FragDepth") == 0
22898e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->type == var->type
22908e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	      && earlier->mode == var->mode) {
22918e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
22928e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /** From the AMD_conservative_depth spec:
22938e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *     Within any shader, the first redeclarations of gl_FragDepth
22948e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       *     must appear before any use of gl_FragDepth.
22958e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick       */
22968e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      if (earlier->used) {
22978e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	 _mesa_glsl_error(&loc, state,
22988e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "the first redeclaration of gl_FragDepth "
22998e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "must appear before any use of gl_FragDepth");
23008e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      }
23018e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
23028e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      /* Prevent inconsistent redeclaration of depth layout qualifier. */
23038e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      if (earlier->depth_layout != ir_depth_layout_none
23048e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	  && earlier->depth_layout != var->depth_layout) {
23058e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick	 _mesa_glsl_error(&loc, state,
23068e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "gl_FragDepth: depth layout is declared here "
23078e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "as '%s, but it was previously declared as "
23088e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  "'%s'",
23098e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  depth_layout_string(var->depth_layout),
23108e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick			  depth_layout_string(earlier->depth_layout));
23118e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      }
23128e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
23138e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      earlier->depth_layout = var->depth_layout;
23148e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
23158e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   } else {
23168e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick      _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
23178e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   }
23188e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick
23198e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick   return earlier;
23208e6cb9fe51a2237e51b47198eb7d46b14ad288b5Ian Romanick}
2321a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
23220292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick/**
23230292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick * Generate the IR for an initializer in a variable declaration
23240292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick */
23250292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanickir_rvalue *
23260292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanickprocess_initializer(ir_variable *var, ast_declaration *decl,
23270292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		    ast_fully_specified_type *type,
23280292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		    exec_list *initializer_instructions,
23290292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		    struct _mesa_glsl_parse_state *state)
23300292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick{
23310292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   ir_rvalue *result = NULL;
23320292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23330292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   YYLTYPE initializer_loc = decl->initializer->get_location();
23340292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23350292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
23360292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    *
23370292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    *    "All uniform variables are read-only and are initialized either
23380292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    *    directly by an application via API commands, or indirectly by
23390292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    *    OpenGL."
23400292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    */
23410292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if ((state->language_version <= 110)
23420292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       && (var->mode == ir_var_uniform)) {
23430292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      _mesa_glsl_error(& initializer_loc, state,
23440292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       "cannot initialize uniforms in GLSL 1.10");
23450292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
23460292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23470292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if (var->type->is_sampler()) {
23480292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      _mesa_glsl_error(& initializer_loc, state,
23490292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       "cannot initialize samplers");
23500292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
23510292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23520292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
23530292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      _mesa_glsl_error(& initializer_loc, state,
23540292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       "cannot initialize %s shader input / %s",
23550292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       _mesa_glsl_shader_target_name(state->target),
23560292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       (state->target == vertex_shader)
23570292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick		       ? "attribute" : "varying");
23580292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
23590292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23600292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   ir_dereference *const lhs = new(state) ir_dereference_variable(var);
23610292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   ir_rvalue *rhs = decl->initializer->hir(initializer_instructions,
23620292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick					   state);
23630292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23640292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   /* Calculate the constant value if this is a const or uniform
23650292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    * declaration.
23660292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick    */
23670292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if (type->qualifier.flags.q.constant
23680292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       || type->qualifier.flags.q.uniform) {
236985caea29c18fad89050ac366c558afef568dcb3fIan Romanick      ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs, true);
23700292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      if (new_rhs != NULL) {
23710292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 rhs = new_rhs;
23720292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
23730292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 ir_constant *constant_value = rhs->constant_expression_value();
23740292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 if (!constant_value) {
23750292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    _mesa_glsl_error(& initializer_loc, state,
23760292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     "initializer of %s variable `%s' must be a "
23770292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     "constant expression",
23780292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     (type->qualifier.flags.q.constant)
23790292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     ? "const" : "uniform",
23800292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			     decl->identifier);
23810292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    if (var->type->is_numeric()) {
23820292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	       /* Reduce cascading errors. */
23830292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	       var->constant_value = ir_constant::zero(state, var->type);
23840292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    }
23850292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 } else {
23860292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    rhs = constant_value;
23870292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    var->constant_value = constant_value;
23880292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 }
23890292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      } else {
23900292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 _mesa_glsl_error(&initializer_loc, state,
23910292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			  "initializer of type %s cannot be assigned to "
23920292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			  "variable of type %s",
23930292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick			  rhs->type->name, var->type->name);
23940292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 if (var->type->is_numeric()) {
23950292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    /* Reduce cascading errors. */
23960292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	    var->constant_value = ir_constant::zero(state, var->type);
23970292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 }
23980292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      }
23990292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
24000292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
24010292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   if (rhs && !rhs->type->is_error()) {
24020292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      bool temp = var->read_only;
24030292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      if (type->qualifier.flags.q.constant)
24040292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 var->read_only = false;
24050292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
24060292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      /* Never emit code to initialize a uniform.
24070292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       */
24080292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      const glsl_type *initializer_type;
24090292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      if (!type->qualifier.flags.q.uniform) {
24100292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 result = do_assignment(initializer_instructions, state,
2411e9015e99d0d702570dbd3d7bcbafbf6c723cb7a7Ian Romanick				NULL,
241285caea29c18fad89050ac366c558afef568dcb3fIan Romanick				lhs, rhs, true,
24130292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick				type->get_location());
24140292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 initializer_type = result->type;
24150292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      } else
24160292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick	 initializer_type = rhs->type;
24170292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
2418f37b1ad937dd2c420f4c9fd9aa5887942bd31f3fIan Romanick      var->constant_initializer = rhs->constant_expression_value();
2419f37b1ad937dd2c420f4c9fd9aa5887942bd31f3fIan Romanick      var->has_initializer = true;
2420f37b1ad937dd2c420f4c9fd9aa5887942bd31f3fIan Romanick
24210292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      /* If the declared variable is an unsized array, it must inherrit
24220292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * its full type from the initializer.  A declaration such as
24230292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24240292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *     uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
24250292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24260292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * becomes
24270292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24280292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *     uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
24290292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24300292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * The assignment generated in the if-statement (below) will also
24310292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * automatically handle this case for non-uniforms.
24320292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       *
24330292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * If the declared variable is not an array, the types must
24340292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * already match exactly.  As a result, the type assignment
24350292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * here can be done unconditionally.  For non-uniforms the call
24360292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * to do_assignment can change the type of the initializer (via
24370292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * the implicit conversion rules).  For uniforms the initializer
24380292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * must be a constant expression, and the type of that expression
24390292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       * was validated above.
24400292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick       */
24410292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      var->type = initializer_type;
24420292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
24430292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick      var->read_only = temp;
24440292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   }
24450292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
24460292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick   return result;
24470292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick}
24480292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick
2449fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
24500044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_declarator_list::hir(exec_list *instructions,
245118238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			 struct _mesa_glsl_parse_state *state)
2452a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2453953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
2454a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *decl_type;
2455a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *type_name = NULL;
24568558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   ir_rvalue *result = NULL;
2457c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   YYLTYPE loc = this->get_location();
2458a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
24596f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
24606f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
24616f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     "To ensure that a particular output variable is invariant, it is
24626f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     necessary to use the invariant qualifier. It can either be used to
24636f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *     qualify a previously declared variable as being invariant
24646f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
24656f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *         invariant gl_Position; // make existing gl_Position be invariant"
24666f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    *
24676f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * In these cases the parser will set the 'invariant' flag in the declarator
24686f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    * list, and the type will be NULL.
24696f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick    */
24706f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   if (this->invariant) {
24716f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      assert(this->type == NULL);
24726f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
24736f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      if (state->current_function != NULL) {
24746f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 _mesa_glsl_error(& loc, state,
24756f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "All uses of `invariant' keyword must be at global "
24766f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			  "scope\n");
24776f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
24786f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
24796f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
24806f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(!decl->is_array);
24816f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->array_size == NULL);
24826f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 assert(decl->initializer == NULL);
24836f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
24846f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 ir_variable *const earlier =
24856f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    state->symbols->get_variable(decl->identifier);
24866f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 if (earlier == NULL) {
24876f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
24886f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "Undeclared variable `%s' cannot be marked "
24896f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "invariant\n", decl->identifier);
24906f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == vertex_shader)
24916f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_out)) {
24926f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
24936f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
24946f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", decl->identifier);
24956f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else if ((state->target == fragment_shader)
24966f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	       && (earlier->mode != ir_var_in)) {
24976f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
24986f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
24996f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", decl->identifier);
2500bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	 } else if (earlier->used) {
2501bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick	    _mesa_glsl_error(& loc, state,
2502bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			     "variable `%s' may not be redeclared "
2503bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			     "`invariant' after being used",
2504bd33055ef4b6dd18d6247ff7d9e47496ff4acc51Ian Romanick			     earlier->name);
25056f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 } else {
25066f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    earlier->invariant = true;
25076f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
25086f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
25096f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
25106f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      /* Invariant redeclarations do not have r-values.
25116f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick       */
25126f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      return NULL;
25136f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   }
25146f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
25156f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(this->type != NULL);
25166f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick   assert(!this->invariant);
25176f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
25183455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* The type specifier may contain a structure definition.  Process that
25193455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * before any of the variable declarations.
25203455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
25213455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   (void) this->type->specifier->hir(instructions, state);
25223455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
2523d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   decl_type = this->type->specifier->glsl_type(& type_name, state);
2524304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick   if (this->declarations.is_empty()) {
2525f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick      /* If there is no structure involved in the program text, there are two
2526f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * possible scenarios:
2527f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *
2528f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * - The program text contained something like 'vec4;'.  This is an
2529f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *   empty declaration.  It is valid but weird.  Emit a warning.
2530f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *
2531f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * - The program text contained something like 'S;' and 'S' is not the
2532f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *   name of a known structure type.  This is both invalid and weird.
2533f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *   Emit an error.
2534f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       *
2535f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * Note that if decl_type is NULL and there is a structure involved,
2536f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * there must have been some sort of error with the structure.  In this
2537f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * case we assume that an error was already generated on this line of
2538f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * code for the structure.  There is no need to generate an additional,
2539f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       * confusing error.
2540f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick       */
2541f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick      assert(this->type->specifier->structure == NULL || decl_type != NULL
2542f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick	     || state->error);
2543f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick      if (this->type->specifier->structure == NULL) {
2544f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick	 if (decl_type != NULL) {
2545547212d963c70161915c46d64e8020617199fb8dChia-I Wu	    _mesa_glsl_warning(&loc, state, "empty declaration");
2546f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick	 } else {
2547f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick	    _mesa_glsl_error(&loc, state,
2548f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick			     "invalid type `%s' in empty declaration",
2549f5ba4d055ee27bfd741ad019e3b3d4b1a1ce9bd9Ian Romanick			     type_name);
2550547212d963c70161915c46d64e8020617199fb8dChia-I Wu	 }
2551c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick      }
2552c824e35dd092a9cc0dbfd36d90fcdf1488c8942dIan Romanick   }
2553a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
25542b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
2555a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      const struct glsl_type *var_type;
2556768b55a5268572ff9fd03e57e92775882eb0a821Ian Romanick      ir_variable *var;
2557a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2558a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      /* FINISHME: Emit a warning if a variable declaration shadows a
2559a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       * FINISHME: declaration at a higher scope.
2560a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick       */
2561a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2562cec65a6b76290ee4da91691bd3ef01c3fb8a0c37Ian Romanick      if ((decl_type == NULL) || decl_type->is_void()) {
2563a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 if (type_name != NULL) {
2564a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
2565a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type `%s' in declaration of `%s'",
2566a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     type_name, decl->identifier);
2567a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 } else {
2568a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	    _mesa_glsl_error(& loc, state,
2569a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     "invalid type in declaration of `%s'",
2570a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			     decl->identifier);
2571a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
2572a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 continue;
2573a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2574a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2575a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (decl->is_array) {
2576d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 var_type = process_array_type(&loc, decl_type, decl->array_size,
2577d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke				       state);
2578a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick	 if (var_type->is_error())
2579a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick	    continue;
2580a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
2581a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 var_type = decl_type;
2582a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2583a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
25847e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick      var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
2585a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
25863f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
25873f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
25883f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     "Global variables can only use the qualifiers const,
25893f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     attribute, uni form, or varying. Only one may be
25903f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     specified.
25913f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
25923f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *     Local variables can only use the qualifier const."
25933f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       *
259482c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick       * This is relaxed in GLSL 1.30.  It is also relaxed by any extension
259582c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick       * that adds the 'layout' keyword.
25963f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt       */
259782c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick      if ((state->language_version < 130)
259882c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick	  && !state->ARB_explicit_attrib_location_enable
259982c4b4f88af97395a3d1b01e1998ec828cd5d305Ian Romanick	  && !state->ARB_fragment_coord_conventions_enable) {
2600e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.out) {
26013f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
26023f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`out' qualifier in declaration of `%s' "
2603469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick			     "only valid for function parameters in %s.",
2604469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick			     decl->identifier, state->version_string);
26053f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
2606e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.in) {
26073f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	    _mesa_glsl_error(& loc, state,
26083f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt			     "`in' qualifier in declaration of `%s' "
2609469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick			     "only valid for function parameters in %s.",
2610469ea695bbbc984ebec26b2413ab70d450a283caIan Romanick			     decl->identifier, state->version_string);
26113f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 }
26123f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt	 /* FINISHME: Test for other invalid qualifiers. */
26133f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt      }
26143f151509327629ce7d7cbfec42cae987ebf6639fEric Anholt
26152e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
26162e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt				       & loc);
2617a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2618e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick      if (this->type->qualifier.flags.q.invariant) {
2619046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
2620046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt						   var->mode == ir_var_inout)) {
2621046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
2622046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature outval
2623046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
26246f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
26256f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, vertex shader "
26266f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "outputs only\n", var->name);
2627046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	 } else if ((state->target == fragment_shader) &&
2628046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt		    !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
2629046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	    /* FINISHME: Note that this doesn't work for invariant on
2630046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     * a function signature inval
2631046bef235744e891e4a48076e1a3ff9a61a63092Eric Anholt	     */
26326f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	    _mesa_glsl_error(& loc, state,
26336f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "`%s' cannot be marked invariant, fragment shader "
26346f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick			     "inputs only\n", var->name);
26356f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick	 }
26366f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick      }
26376f0823da09384cc1b557385b9e19a9cc7e901ad7Ian Romanick
2638e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      if (state->current_function != NULL) {
2639b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 const char *mode = NULL;
2640e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 const char *extra = "";
2641b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
2642e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	 /* There is no need to check for 'inout' here because the parser will
2643e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	  * only allow that in function parameter lists.
2644e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	  */
2645e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 if (this->type->qualifier.flags.q.attribute) {
2646b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "attribute";
2647e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.uniform) {
2648b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "uniform";
2649e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.varying) {
2650b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	    mode = "varying";
2651e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.in) {
2652e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "in";
2653e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
2654e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick	 } else if (this->type->qualifier.flags.q.out) {
2655e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    mode = "out";
2656e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick	    extra = " or in function parameter list";
2657b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 }
2658b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick
2659b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick	 if (mode) {
2660e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	    _mesa_glsl_error(& loc, state,
2661b168e53452592ce7364a3ce46a6d30c5b746fc3bIan Romanick			     "%s variable `%s' must be declared at "
2662e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     "global scope%s",
2663e0800062daf237a9e4f893bc3db473da8f7472c0Ian Romanick			     mode, var->name, extra);
2664e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick	 }
2665e1c1a3f3bd139da47a1184a8c69af6239973a90cIan Romanick      } else if (var->mode == ir_var_in) {
266601a584d09350d2c726312e2c9e88c5dbc54bdb70Chad Versace         var->read_only = true;
266701a584d09350d2c726312e2c9e88c5dbc54bdb70Chad Versace
2668fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 if (state->target == vertex_shader) {
2669fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    bool error_emitted = false;
2670fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
2671fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2672fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
2673fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
2674fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
2675fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    vectors. Vertex shader inputs can also form arrays of these
2676fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    types, but not structures."
2677fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
26782d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
26792d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
26802d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    "Vertex shader inputs can only be float, floating-point
26812d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors, matrices, signed and unsigned integers and integer
26822d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *    vectors. They cannot be arrays or structures."
26832d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	     *
2684fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
2685fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *
2686fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    "The attribute qualifier can be used only with float,
2687fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    floating-point vectors, and matrices. Attribute variables
2688fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     *    cannot be declared as arrays or structures."
2689fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	     */
2690fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    const glsl_type *check_type = var->type->is_array()
2691fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       ? var->type->fields.array : var->type;
2692fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
2693fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    switch (check_type->base_type) {
2694fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_FLOAT:
2695fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       break;
2696fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_UINT:
2697fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    case GLSL_TYPE_INT:
2698fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       if (state->language_version > 120)
2699fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		  break;
2700fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       /* FALLTHROUGH */
2701fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    default:
2702fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
2703fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
2704fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"type %s`%s'",
2705fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				var->type->is_array() ? "array of " : "",
2706fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				check_type->name);
2707fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
2708fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
2709fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
27102d816204c875ace0fc363d3eeada2255a5009d5cIan Romanick	    if (!error_emitted && (state->language_version <= 130)
2711fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick		&& var->type->is_array()) {
2712fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       _mesa_glsl_error(& loc, state,
2713fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"vertex shader input / attribute cannot have "
2714fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick				"array type");
2715fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	       error_emitted = true;
2716fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	    }
2717fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick	 }
2718fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick      }
2719fb9f5b0675bd714fd6d6325479f62435aaabc2eeIan Romanick
272068d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace      /* Integer vertex outputs must be qualified with 'flat'.
272168d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       *
272268d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       * From section 4.3.6 of the GLSL 1.30 spec:
272368d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       *    "If a vertex output is a signed or unsigned integer or integer
272468d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       *    vector, then it must be qualified with the interpolation qualifier
272568d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       *    flat."
272668d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace       */
272768d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace      if (state->language_version >= 130
272868d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace          && state->target == vertex_shader
272968d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace          && state->current_function == NULL
273068d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace          && var->type->is_integer()
273168d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace          && var->mode == ir_var_out
2732cf45949d6a896651a5f3864d3b195e26d59eee74Paul Berry          && var->interpolation != INTERP_QUALIFIER_FLAT) {
273368d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace
273468d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace         _mesa_glsl_error(&loc, state, "If a vertex output is an integer, "
273568d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace                          "then it must be qualified with 'flat'");
273668d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace      }
273768d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace
273868d06b1454aea30c492c7318ab4e8514df8f38fdChad Versace
2739605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace      /* Interpolation qualifiers cannot be applied to 'centroid' and
2740605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       * 'centroid varying'.
2741605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       *
2742605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
2743605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       *    "interpolation qualifiers may only precede the qualifiers in,
2744605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       *    centroid in, out, or centroid out in a declaration. They do not apply
2745605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       *    to the deprecated storage qualifiers varying or centroid varying."
2746605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace       */
2747605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace      if (state->language_version >= 130
2748605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace          && this->type->qualifier.has_interpolation()
2749605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace          && this->type->qualifier.flags.q.varying) {
2750605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace
2751605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         const char *i = this->type->qualifier.interpolation_string();
2752605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         assert(i != NULL);
2753605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         const char *s;
2754605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         if (this->type->qualifier.flags.q.centroid)
2755605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace            s = "centroid varying";
2756605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         else
2757605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace            s = "varying";
2758605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace
2759605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace         _mesa_glsl_error(&loc, state,
2760605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace                          "qualifier '%s' cannot be applied to the "
2761605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace                          "deprecated storage qualifier '%s'", i, s);
2762605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace      }
2763605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace
2764605aacc67d73db0926e0046a90a07fcd93a2d613Chad Versace
27658faaa4a672c1062e486eda2525287715b554342dChad Versace      /* Interpolation qualifiers can only apply to vertex shader outputs and
27668faaa4a672c1062e486eda2525287715b554342dChad Versace       * fragment shader inputs.
27678faaa4a672c1062e486eda2525287715b554342dChad Versace       *
27688faaa4a672c1062e486eda2525287715b554342dChad Versace       * From page 29 (page 35 of the PDF) of the GLSL 1.30 spec:
27698faaa4a672c1062e486eda2525287715b554342dChad Versace       *    "Outputs from a vertex shader (out) and inputs to a fragment
27708faaa4a672c1062e486eda2525287715b554342dChad Versace       *    shader (in) can be further qualified with one or more of these
27718faaa4a672c1062e486eda2525287715b554342dChad Versace       *    interpolation qualifiers"
27728faaa4a672c1062e486eda2525287715b554342dChad Versace       */
27738faaa4a672c1062e486eda2525287715b554342dChad Versace      if (state->language_version >= 130
27748faaa4a672c1062e486eda2525287715b554342dChad Versace          && this->type->qualifier.has_interpolation()) {
27758faaa4a672c1062e486eda2525287715b554342dChad Versace
27768faaa4a672c1062e486eda2525287715b554342dChad Versace         const char *i = this->type->qualifier.interpolation_string();
27778faaa4a672c1062e486eda2525287715b554342dChad Versace         assert(i != NULL);
27788faaa4a672c1062e486eda2525287715b554342dChad Versace
27798faaa4a672c1062e486eda2525287715b554342dChad Versace         switch (state->target) {
27808faaa4a672c1062e486eda2525287715b554342dChad Versace         case vertex_shader:
27818faaa4a672c1062e486eda2525287715b554342dChad Versace            if (this->type->qualifier.flags.q.in) {
27828faaa4a672c1062e486eda2525287715b554342dChad Versace               _mesa_glsl_error(&loc, state,
27838faaa4a672c1062e486eda2525287715b554342dChad Versace                                "qualifier '%s' cannot be applied to vertex "
27848faaa4a672c1062e486eda2525287715b554342dChad Versace                                "shader inputs", i);
27858faaa4a672c1062e486eda2525287715b554342dChad Versace            }
27868faaa4a672c1062e486eda2525287715b554342dChad Versace            break;
27878faaa4a672c1062e486eda2525287715b554342dChad Versace         case fragment_shader:
27888faaa4a672c1062e486eda2525287715b554342dChad Versace            if (this->type->qualifier.flags.q.out) {
27898faaa4a672c1062e486eda2525287715b554342dChad Versace               _mesa_glsl_error(&loc, state,
27908faaa4a672c1062e486eda2525287715b554342dChad Versace                                "qualifier '%s' cannot be applied to fragment "
27918faaa4a672c1062e486eda2525287715b554342dChad Versace                                "shader outputs", i);
27928faaa4a672c1062e486eda2525287715b554342dChad Versace            }
27938faaa4a672c1062e486eda2525287715b554342dChad Versace            break;
27948faaa4a672c1062e486eda2525287715b554342dChad Versace         default:
27958faaa4a672c1062e486eda2525287715b554342dChad Versace            assert(0);
27968faaa4a672c1062e486eda2525287715b554342dChad Versace         }
27978faaa4a672c1062e486eda2525287715b554342dChad Versace      }
27988faaa4a672c1062e486eda2525287715b554342dChad Versace
27998faaa4a672c1062e486eda2525287715b554342dChad Versace
28001eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace      /* From section 4.3.4 of the GLSL 1.30 spec:
28011eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace       *    "It is an error to use centroid in in a vertex shader."
28021eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace       */
28031eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace      if (state->language_version >= 130
28041eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace          && this->type->qualifier.flags.q.centroid
28051eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace          && this->type->qualifier.flags.q.in
28061eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace          && state->target == vertex_shader) {
28071eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace
28081eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace         _mesa_glsl_error(&loc, state,
28091eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace                          "'centroid in' cannot be used in a vertex shader");
28101eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace      }
28111eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace
28121eb0f17fa4aa548779cb7d8ffbd86de3523d6796Chad Versace
2813889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
2814889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       */
2815889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      if (this->type->specifier->precision != ast_precision_none
2816889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace          && state->language_version != 100
2817889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace          && state->language_version < 130) {
2818889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
2819889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace         _mesa_glsl_error(&loc, state,
2820889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace                          "precision qualifiers are supported only in GLSL ES "
2821889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace                          "1.00, and GLSL 1.30 and later");
2822889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      }
2823889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
2824889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
282545e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace      /* Precision qualifiers only apply to floating point and integer types.
2826889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *
2827889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       * From section 4.5.2 of the GLSL 1.30 spec:
2828889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *    "Any floating point or any integer declaration can have the type
2829889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *    preceded by one of these precision qualifiers [...] Literal
2830889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *    constants do not have precision qualifiers. Neither do Boolean
2831889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       *    variables.
28328752824f27c979986ae855667337e89637b005fbKenneth Graunke       *
28338752824f27c979986ae855667337e89637b005fbKenneth Graunke       * In GLSL ES, sampler types are also allowed.
28348752824f27c979986ae855667337e89637b005fbKenneth Graunke       *
28358752824f27c979986ae855667337e89637b005fbKenneth Graunke       * From page 87 of the GLSL ES spec:
28368752824f27c979986ae855667337e89637b005fbKenneth Graunke       *    "RESOLUTION: Allow sampler types to take a precision qualifier."
2837889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace       */
2838889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      if (this->type->specifier->precision != ast_precision_none
283945e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace          && !var->type->is_float()
284045e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace          && !var->type->is_integer()
28418752824f27c979986ae855667337e89637b005fbKenneth Graunke          && !(var->type->is_sampler() && state->es_shader)
284245e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace          && !(var->type->is_array()
284345e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace               && (var->type->fields.array->is_float()
284445e8e6c6b1b7f3bc00a578fa6809c9bc719c171aChad Versace                   || var->type->fields.array->is_integer()))) {
2845889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
2846889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace         _mesa_glsl_error(&loc, state,
28478752824f27c979986ae855667337e89637b005fbKenneth Graunke                          "precision qualifiers apply only to floating point"
28488752824f27c979986ae855667337e89637b005fbKenneth Graunke                          "%s types", state->es_shader ? ", integer, and sampler"
28498752824f27c979986ae855667337e89637b005fbKenneth Graunke						       : "and integer");
2850889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace      }
2851889e1a5b6c6602198d649ea5881e0010dec575e9Chad Versace
2852f07221056e1822187546b76387714b3172f9b2c5Paul Berry      /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
2853f07221056e1822187546b76387714b3172f9b2c5Paul Berry       *
2854f07221056e1822187546b76387714b3172f9b2c5Paul Berry       *    "[Sampler types] can only be declared as function
2855f07221056e1822187546b76387714b3172f9b2c5Paul Berry       *    parameters or uniform variables (see Section 4.3.5
2856f07221056e1822187546b76387714b3172f9b2c5Paul Berry       *    "Uniform")".
2857f07221056e1822187546b76387714b3172f9b2c5Paul Berry       */
2858f07221056e1822187546b76387714b3172f9b2c5Paul Berry      if (var_type->contains_sampler() &&
2859f07221056e1822187546b76387714b3172f9b2c5Paul Berry          !this->type->qualifier.flags.q.uniform) {
2860f07221056e1822187546b76387714b3172f9b2c5Paul Berry         _mesa_glsl_error(&loc, state, "samplers must be declared uniform");
2861f07221056e1822187546b76387714b3172f9b2c5Paul Berry      }
2862f07221056e1822187546b76387714b3172f9b2c5Paul Berry
2863e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick      /* Process the initializer and add its instructions to a temporary
2864e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * list.  This list will be added to the instruction stream (below) after
2865e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * the declaration is added.  This is done because in some cases (such as
2866e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * redeclarations) the declaration may not actually be added to the
2867e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       * instruction stream.
2868e78e0fa42b49b50ed1150f7fdb74bf942ebd6bcfIan Romanick       */
2869fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      exec_list initializer_instructions;
287009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick      ir_variable *earlier = get_variable_being_redeclared(var, decl, state);
287109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick
287266faec4895b7bb59a614087a200c05157191b4aeIan Romanick      if (decl->initializer != NULL) {
287309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 result = process_initializer((earlier == NULL) ? var : earlier,
287409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick				      decl, this->type,
28750292ffb85c03e9fa15b9395e3875109dd8979292Ian Romanick				      &initializer_instructions, state);
287666faec4895b7bb59a614087a200c05157191b4aeIan Romanick      }
287717d86f4371da413176ba365ca26a58bac172d365Ian Romanick
28780ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
28790ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *
28800ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *     "It is an error to write to a const variable outside of
28810ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      its declaration, so they must be initialized when
28820ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       *      declared."
28830ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt       */
2884e24d35a5b59ca1e75b69a32db6294787378a963fIan Romanick      if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
28850ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt	 _mesa_glsl_error(& loc, state,
288646f7105df487c91569f7e4a8da74d673c12e5619Chad Versace			  "const declaration of `%s' must be initialized",
288746f7105df487c91569f7e4a8da74d673c12e5619Chad Versace			  decl->identifier);
28880ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt      }
28890ed61257253fc0df1dde9042cb0e7fe22d58077aEric Anholt
289009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick      /* If the declaration is not a redeclaration, there are a few additional
289109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick       * semantic checks that must be applied.  In addition, variable that was
289209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick       * created for the declaration should be added to the IR stream.
28935466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick       */
289409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick      if (earlier == NULL) {
289509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
289609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *
289709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *   "Identifiers starting with "gl_" are reserved for use by
289809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *   OpenGL, and may not be declared in a shader as either a
289909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *   variable or a function."
290009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  */
290109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 if (strncmp(decl->identifier, "gl_", 3) == 0)
290209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	    _mesa_glsl_error(& loc, state,
290309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick			     "identifier `%s' uses reserved `gl_' prefix",
290409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick			     decl->identifier);
2905c475a54578bf5473c6c62bc5468ef4fe555164d7Jason Wood	 else if (strstr(decl->identifier, "__")) {
2906684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	    /* From page 14 (page 20 of the PDF) of the GLSL 1.10
2907684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     * spec:
2908684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     *
2909684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     *     "In addition, all identifiers containing two
2910684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     *      consecutive underscores (__) are reserved as
2911684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     *      possible future keywords."
2912684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	     */
2913684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	    _mesa_glsl_error(& loc, state,
2914684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt			     "identifier `%s' uses reserved `__' string",
2915684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt			     decl->identifier);
2916684b701c124cf258ba9012adf51357e072ae61f9Eric Anholt	 }
29175466b63968b98c9627b8dd207ea2bebf838b5268Ian Romanick
291809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 /* Add the variable to the symbol table.  Note that the initializer's
291909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * IR was already processed earlier (though it hasn't been emitted
292009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * yet), without the variable in scope.
292109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *
292209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * This differs from most C-like languages, but it follows the GLSL
292309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
292409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * spec:
292509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *
292609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *     "Within a declaration, the scope of a name starts immediately
292709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *     after the initializer if present or immediately after the name
292809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  *     being declared if not."
292909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  */
293009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 if (!state->symbols->add_variable(var)) {
293109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	    YYLTYPE loc = this->get_location();
293209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	    _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
293309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick			     "current scope", decl->identifier);
293409a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	    continue;
293509a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 }
293609a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick
293709a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 /* Push the variable declaration to the top.  It means that all the
293809a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * variable declarations will appear in a funny last-to-first order,
293909a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * but otherwise we run into trouble if a function is prototyped, a
294009a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * global var is decled, then the function is defined with usage of
294109a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  * the global var.  See glslparsertest's CorrectModule.frag.
294209a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	  */
294309a4ba0fc31fa8fc193dfb7b4fd78e32722b8312Ian Romanick	 instructions->push_head(var);
29445d25746640ee27882b69a962459727cf924443dbKenneth Graunke      }
29455d25746640ee27882b69a962459727cf924443dbKenneth Graunke
2946fa33d0b85403da94e3f4a7e6c868af215c076b4bEric Anholt      instructions->append_list(&initializer_instructions);
2947a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2948a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
29498558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt
29508558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   /* Generally, variable declarations do not have r-values.  However,
29518558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * one is used for the declaration in
29528558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
29538558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * while (bool b = some_condition()) {
29548558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *   ...
29558558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * }
29568558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    *
29578558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt    * so we return the rvalue from the last seen declaration here.
2958a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
29598558459512594216c5aed0bb8d2b0efcbc8b921cEric Anholt   return result;
2960a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
2961a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2962a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2963fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
29640044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanickast_parameter_declarator::hir(exec_list *instructions,
296518238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			      struct _mesa_glsl_parse_state *state)
2966a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
2967953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
2968a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const struct glsl_type *type;
2969a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   const char *name = NULL;
29702e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   YYLTYPE loc = this->get_location();
2971a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2972d612a127ccf12c11204f7f72a332de12f58f85a2Ian Romanick   type = this->type->specifier->glsl_type(& name, state);
2973a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2974a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   if (type == NULL) {
2975a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      if (name != NULL) {
2976a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2977a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type `%s' in declaration of `%s'",
297818238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  name, this->identifier);
2979a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      } else {
2980a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 _mesa_glsl_error(& loc, state,
2981a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick			  "invalid type in declaration of `%s'",
298218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick			  this->identifier);
2983a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
2984a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
29850471e8b0896e05b3bc81ccad6184e6e35fb61425Ian Romanick      type = glsl_type::error_type;
2986a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
2987a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
2988068c80cfe0a280490353b6b007165d717c672eedEric Anholt   /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
2989068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
2990068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    "Functions that accept no input arguments need not use void in the
2991068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    argument list because prototypes (or definitions) are required and
2992068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    therefore there is no ambiguity when an empty argument list "( )" is
2993068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    declared. The idiom "(void)" as a parameter list is provided for
2994068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *    convenience."
2995068c80cfe0a280490353b6b007165d717c672eedEric Anholt    *
2996068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * Placing this check here prevents a void parameter being set up
2997068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * for a function, which avoids tripping up checks for main taking
2998068c80cfe0a280490353b6b007165d717c672eedEric Anholt    * parameters and lookups of an unnamed symbol.
2999068c80cfe0a280490353b6b007165d717c672eedEric Anholt    */
3000cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if (type->is_void()) {
3001cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (this->identifier != NULL)
3002cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 _mesa_glsl_error(& loc, state,
3003cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick			  "named parameter cannot have type `void'");
3004cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3005cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      is_void = true;
3006068c80cfe0a280490353b6b007165d717c672eedEric Anholt      return NULL;
3007cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
3008068c80cfe0a280490353b6b007165d717c672eedEric Anholt
300945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   if (formal_parameter && (this->identifier == NULL)) {
301045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
301145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      return NULL;
301245d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   }
301345d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick
3014e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   /* This only handles "vec4 foo[..]".  The earlier specifier->glsl_type(...)
3015e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    * call already handled the "vec4[..] foo" case.
3016e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke    */
3017e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   if (this->is_array) {
3018d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke      type = process_array_type(&loc, type, this->array_size, state);
3019e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
3020e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
3021a04211ecb8c907eaef69832abc2e16cd6f9887a0Ian Romanick   if (!type->is_error() && type->array_size() == 0) {
3022e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
3023e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke		       "a declared size.");
3024e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke      type = glsl_type::error_type;
3025e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke   }
3026e511a35fc53fb75a2401d8a94c0c35634175c575Kenneth Graunke
3027cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   is_void = false;
30287e2aa91507a5883e33473e0a94215ee3985baad1Ian Romanick   ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
3029a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3030cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick   /* Apply any specified qualifiers to the parameter declaration.  Note that
3031cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    * for function parameters the default mode is 'in'.
3032cdb8d54b6808b13092cb85e44cf02e4e91c3a669Ian Romanick    */
30332e063f1adf9e529697483eaabc7e015b4b740267Eric Anholt   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
3034a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3035f07221056e1822187546b76387714b3172f9b2c5Paul Berry   /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3036f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *
3037f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    "Samplers cannot be treated as l-values; hence cannot be used
3038f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    as out or inout function parameters, nor can they be assigned
3039f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    into."
3040f07221056e1822187546b76387714b3172f9b2c5Paul Berry    */
3041f07221056e1822187546b76387714b3172f9b2c5Paul Berry   if ((var->mode == ir_var_inout || var->mode == ir_var_out)
3042f07221056e1822187546b76387714b3172f9b2c5Paul Berry       && type->contains_sampler()) {
3043f07221056e1822187546b76387714b3172f9b2c5Paul Berry      _mesa_glsl_error(&loc, state, "out and inout parameters cannot contain samplers");
3044f07221056e1822187546b76387714b3172f9b2c5Paul Berry      type = glsl_type::error_type;
3045f07221056e1822187546b76387714b3172f9b2c5Paul Berry   }
3046f07221056e1822187546b76387714b3172f9b2c5Paul Berry
304700792e3586746c833ffc9bb65712e38038916e06Paul Berry   /* From page 39 (page 45 of the PDF) of the GLSL 1.10 spec:
304800792e3586746c833ffc9bb65712e38038916e06Paul Berry    *
304900792e3586746c833ffc9bb65712e38038916e06Paul Berry    *    "When calling a function, expressions that do not evaluate to
305000792e3586746c833ffc9bb65712e38038916e06Paul Berry    *     l-values cannot be passed to parameters declared as out or inout."
305100792e3586746c833ffc9bb65712e38038916e06Paul Berry    *
305200792e3586746c833ffc9bb65712e38038916e06Paul Berry    * From page 32 (page 38 of the PDF) of the GLSL 1.10 spec:
305300792e3586746c833ffc9bb65712e38038916e06Paul Berry    *
305400792e3586746c833ffc9bb65712e38038916e06Paul Berry    *    "Other binary or unary expressions, non-dereferenced arrays,
305500792e3586746c833ffc9bb65712e38038916e06Paul Berry    *     function names, swizzles with repeated fields, and constants
305600792e3586746c833ffc9bb65712e38038916e06Paul Berry    *     cannot be l-values."
305700792e3586746c833ffc9bb65712e38038916e06Paul Berry    *
305800792e3586746c833ffc9bb65712e38038916e06Paul Berry    * So for GLSL 1.10, passing an array as an out or inout parameter is not
305900792e3586746c833ffc9bb65712e38038916e06Paul Berry    * allowed.  This restriction is removed in GLSL 1.20, and in GLSL ES.
306000792e3586746c833ffc9bb65712e38038916e06Paul Berry    */
306100792e3586746c833ffc9bb65712e38038916e06Paul Berry   if ((var->mode == ir_var_inout || var->mode == ir_var_out)
306200792e3586746c833ffc9bb65712e38038916e06Paul Berry       && type->is_array() && state->language_version == 110) {
306300792e3586746c833ffc9bb65712e38038916e06Paul Berry      _mesa_glsl_error(&loc, state, "Arrays cannot be out or inout parameters in GLSL 1.10");
306400792e3586746c833ffc9bb65712e38038916e06Paul Berry      type = glsl_type::error_type;
306500792e3586746c833ffc9bb65712e38038916e06Paul Berry   }
306600792e3586746c833ffc9bb65712e38038916e06Paul Berry
30670044e7edcea22d2456c051a1c4b744a26960ad27Ian Romanick   instructions->push_tail(var);
3068a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3069a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Parameter declarations do not have r-values.
3070a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3071a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
3072a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
3073a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3074a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
307545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanickvoid
3076304ea90233baeac6801a98e981658cb7a2d2501cIan Romanickast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
307745d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    bool formal,
307845d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    exec_list *ir_parameters,
307945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					    _mesa_glsl_parse_state *state)
3080a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
3081cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   ast_parameter_declarator *void_param = NULL;
3082cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   unsigned count = 0;
3083a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
30842b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
308545d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick      param->formal_parameter = formal;
3086068c80cfe0a280490353b6b007165d717c672eedEric Anholt      param->hir(ir_parameters, state);
3087cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3088cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      if (param->is_void)
3089cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick	 void_param = param;
3090cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3091cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      count++;
3092cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   }
3093cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3094cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick   if ((void_param != NULL) && (count > 1)) {
3095cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      YYLTYPE loc = void_param->get_location();
3096cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick
3097cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick      _mesa_glsl_error(& loc, state,
3098cf37c9e8dad4349e45cb91d36957484fd76ce264Ian Romanick		       "`void' parameter must be only parameter");
3099a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3100a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
3101a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3102a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
31036fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunkevoid
31040d81b0e18494a80c4326fbc98837842959675869Paul Berryemit_function(_mesa_glsl_parse_state *state, ir_function *f)
31056fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke{
31060d81b0e18494a80c4326fbc98837842959675869Paul Berry   /* IR invariants disallow function declarations or definitions
31070d81b0e18494a80c4326fbc98837842959675869Paul Berry    * nested within other function definitions.  But there is no
31080d81b0e18494a80c4326fbc98837842959675869Paul Berry    * requirement about the relative order of function declarations
31090d81b0e18494a80c4326fbc98837842959675869Paul Berry    * and definitions with respect to one another.  So simply insert
31100d81b0e18494a80c4326fbc98837842959675869Paul Berry    * the new ir_function block at the end of the toplevel instruction
31110d81b0e18494a80c4326fbc98837842959675869Paul Berry    * list.
31120d81b0e18494a80c4326fbc98837842959675869Paul Berry    */
31130d81b0e18494a80c4326fbc98837842959675869Paul Berry   state->toplevel_ir->push_tail(f);
31146fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke}
31156fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke
31166fae1e4c4d33769e2f255d50907b5aa0ab80edd4Kenneth Graunke
3117fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
311892318a947958892497722772b03c643ebc943294Ian Romanickast_function::hir(exec_list *instructions,
311992318a947958892497722772b03c643ebc943294Ian Romanick		  struct _mesa_glsl_parse_state *state)
3120a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick{
3121953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
312218238de6c34a1a32c452f1006ed13d8adc1bc9d7Ian Romanick   ir_function *f = NULL;
312392318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *sig = NULL;
312492318a947958892497722772b03c643ebc943294Ian Romanick   exec_list hir_parameters;
3125a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3126ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   const char *const name = identifier;
3127a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
31289a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick   /* New functions are always added to the top-level IR instruction stream,
31299a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick    * so this instruction list pointer is ignored.  See also emit_function
31309a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick    * (called below).
31319a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick    */
31329a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick   (void) instructions;
31339a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick
313463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
313563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
313663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   "Function declarations (prototypes) cannot occur inside of functions;
313763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   they must be at global scope, or for the built-in functions, outside
313863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   the global scope."
313963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
314063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
314163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
314263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *   "User defined functions may only be defined within the global scope."
314363b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    *
314463b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    * Note that this language does not appear in GLSL 1.10.
314563b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick    */
314663b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   if ((state->current_function != NULL) && (state->language_version != 110)) {
314763b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      YYLTYPE loc = this->get_location();
314863b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick      _mesa_glsl_error(&loc, state,
314963b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick		       "declaration of function `%s' not allowed within "
315063b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick		       "function body", name);
315163b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick   }
315263b80f8cc181ded154668e60ac2cf0a6a82d118fIan Romanick
3153edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
3154edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *
3155edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   "Identifiers starting with "gl_" are reserved for use by
3156edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   OpenGL, and may not be declared in a shader as either a
3157edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    *   variable or a function."
3158edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke    */
3159edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   if (strncmp(name, "gl_", 3) == 0) {
3160edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      YYLTYPE loc = this->get_location();
3161edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke      _mesa_glsl_error(&loc, state,
3162edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke		       "identifier `%s' uses reserved `gl_' prefix", name);
3163edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke   }
3164edd180f03216d2fcb2771aeea34e7015fb2b83c3Kenneth Graunke
3165a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Convert the list of function parameters to HIR now so that they can be
3166a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * used below to compare this function's signature with previously seen
3167a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * signatures for functions with the same name.
3168a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
316945d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick   ast_parameter_declarator::parameters_to_hir(& this->parameters,
317045d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       is_definition,
317145d8a70c12ee6ea956baaf898324a828496382f6Ian Romanick					       & hir_parameters, state);
3172a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3173e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const char *return_type_name;
3174e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick   const glsl_type *return_type =
317592318a947958892497722772b03c643ebc943294Ian Romanick      this->return_type->specifier->glsl_type(& return_type_name, state);
3176e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
317776e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   if (!return_type) {
317876e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      YYLTYPE loc = this->get_location();
317976e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      _mesa_glsl_error(&loc, state,
318076e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       "function `%s' has undeclared return type `%s'",
318176e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt		       name, return_type_name);
318276e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt      return_type = glsl_type::error_type;
318376e96d74f49cc262ceaf2ed6c48d2f4ed21d219fEric Anholt   }
3184e39cc69fa3cb830b803fe0c4f6c30915aa886b5bIan Romanick
3185ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
3186ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    * "No qualifier is allowed on the return type of a function."
3187ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke    */
3188ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   if (this->return_type->has_qualifiers()) {
3189ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      YYLTYPE loc = this->get_location();
3190ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke      _mesa_glsl_error(& loc, state,
3191ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke		       "function `%s' return type has qualifiers", name);
3192ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke   }
3193ac04c257e31fe012dac750bcf5bf3134ba07ebdcKenneth Graunke
3194f07221056e1822187546b76387714b3172f9b2c5Paul Berry   /* From page 17 (page 23 of the PDF) of the GLSL 1.20 spec:
3195f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *
3196f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    "[Sampler types] can only be declared as function parameters
3197f07221056e1822187546b76387714b3172f9b2c5Paul Berry    *    or uniform variables (see Section 4.3.5 "Uniform")".
3198f07221056e1822187546b76387714b3172f9b2c5Paul Berry    */
3199f07221056e1822187546b76387714b3172f9b2c5Paul Berry   if (return_type->contains_sampler()) {
3200f07221056e1822187546b76387714b3172f9b2c5Paul Berry      YYLTYPE loc = this->get_location();
3201f07221056e1822187546b76387714b3172f9b2c5Paul Berry      _mesa_glsl_error(&loc, state,
3202f07221056e1822187546b76387714b3172f9b2c5Paul Berry                       "function `%s' return type can't contain a sampler",
3203f07221056e1822187546b76387714b3172f9b2c5Paul Berry                       name);
3204f07221056e1822187546b76387714b3172f9b2c5Paul Berry   }
3205f07221056e1822187546b76387714b3172f9b2c5Paul Berry
3206a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Verify that this function's signature either doesn't match a previously
3207a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * seen signature for a function with the same name, or, if a match is found,
3208a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    * that the previously seen signature does not have an associated definition.
3209a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3210e466b182bbf21f62fe6542091f4af3275555db80Ian Romanick   f = state->symbols->get_function(name);
321181f03393982c29f8f4165b5629c8e8fb708b97a3Kenneth Graunke   if (f != NULL && (state->es_shader || f->has_user_signature())) {
3212202604e8160157e4e80b3458175e0170d168e557Ian Romanick      sig = f->exact_matching_signature(&hir_parameters);
32130d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke      if (sig != NULL) {
32140d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 const char *badvar = sig->qualifiers_match(&hir_parameters);
32150d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (badvar != NULL) {
32160d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
3217abd40b15210c17b2a3ba8fcffc868fda203efa01Kenneth Graunke
32180d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
32190d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "qualifiers don't match prototype", name, badvar);
32200d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
32211e7ec3ce128a9d30d7d9e1707a22b270eb525075Eric Anholt
32220d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (sig->return_type != return_type) {
32230d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
322460be7626b829af7e1d07330b9a88468924ba350eEric Anholt
32250d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
32260d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke			     "match prototype", name);
32270d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 }
3228a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
32290d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	 if (is_definition && sig->is_defined) {
32300d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    YYLTYPE loc = this->get_location();
3231a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
32320d605cb97c9cd2f9a170e3aa15bdf4021a75fc14Kenneth Graunke	    _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
3233a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick	 }
3234a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick      }
3235a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   } else {
32361660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      f = new(ctx) ir_function(name);
3237e8f5ebf313da3ce33ccbbcf9b72946853035fbddEric Anholt      if (!state->symbols->add_function(f)) {
3238e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 /* This function name shadows a non-function use of the same name. */
3239e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 YYLTYPE loc = this->get_location();
3240e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke
3241e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
3242e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke			  "non-function", name);
3243e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke	 return NULL;
3244e09591317b2470fe9c104606577d4e10255727c0Kenneth Graunke      }
32459fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke
32460d81b0e18494a80c4326fbc98837842959675869Paul Berry      emit_function(state, f);
3247a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3248a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3249ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   /* Verify the return type of main() */
3250ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   if (strcmp(name, "main") == 0) {
325125711a85c22bed305c9b52b89feb9c600d1892dfIan Romanick      if (! return_type->is_void()) {
3252ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 YYLTYPE loc = this->get_location();
3253ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt
3254ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must return void");
3255ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt      }
3256174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
325792318a947958892497722772b03c643ebc943294Ian Romanick      if (!hir_parameters.is_empty()) {
3258174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 YYLTYPE loc = this->get_location();
3259174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt
3260174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt	 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
3261174cc03edcd6861ba416cc1afb19d4d43933ac84Eric Anholt      }
3262ab372dab2a013e5d0c8ee57bb799a76c9a78abf2Eric Anholt   }
3263a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3264a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Finish storing the information about this new function in its signature.
3265a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
326692318a947958892497722772b03c643ebc943294Ian Romanick   if (sig == NULL) {
32671660a2954797e056caba319c5d6c70b0d4be22feCarl Worth      sig = new(ctx) ir_function_signature(return_type);
326892318a947958892497722772b03c643ebc943294Ian Romanick      f->add_signature(sig);
3269a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3270a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
3271bff6013d469b3d4e54cdc5731801c56994a523ecKenneth Graunke   sig->replace_parameters(&hir_parameters);
327292318a947958892497722772b03c643ebc943294Ian Romanick   signature = sig;
327392318a947958892497722772b03c643ebc943294Ian Romanick
327492318a947958892497722772b03c643ebc943294Ian Romanick   /* Function declarations (prototypes) do not have r-values.
327592318a947958892497722772b03c643ebc943294Ian Romanick    */
327692318a947958892497722772b03c643ebc943294Ian Romanick   return NULL;
327792318a947958892497722772b03c643ebc943294Ian Romanick}
327892318a947958892497722772b03c643ebc943294Ian Romanick
327992318a947958892497722772b03c643ebc943294Ian Romanick
328092318a947958892497722772b03c643ebc943294Ian Romanickir_rvalue *
328192318a947958892497722772b03c643ebc943294Ian Romanickast_function_definition::hir(exec_list *instructions,
328292318a947958892497722772b03c643ebc943294Ian Romanick			     struct _mesa_glsl_parse_state *state)
328392318a947958892497722772b03c643ebc943294Ian Romanick{
328492318a947958892497722772b03c643ebc943294Ian Romanick   prototype->is_definition = true;
328592318a947958892497722772b03c643ebc943294Ian Romanick   prototype->hir(instructions, state);
3286e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
328792318a947958892497722772b03c643ebc943294Ian Romanick   ir_function_signature *signature = prototype->signature;
3288826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke   if (signature == NULL)
3289826a39cb14244820e8539a2328bb52447348f184Kenneth Graunke      return NULL;
3290a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
329141ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == NULL);
329241ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = signature;
32936de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   state->found_return = false;
329441ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick
3295e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   /* Duplicate parameters declared in the prototype as concrete variables.
3296e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick    * Add these to the symbol table.
3297a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
32988bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->push_scope();
3299e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick   foreach_iter(exec_list_iterator, iter, signature->parameters) {
3300fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
3301e29a5859891eb9e1587396dea0e8010f7d88f68cIan Romanick
3302fbc7c0b8f2e161bce1c048c63d2d5cfcdeb096f1Eric Anholt      assert(var != NULL);
3303a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
33043359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      /* The only way a parameter would "exist" is if two parameters have
33053359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       * the same name.
33063359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick       */
33073359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      if (state->symbols->name_declared_this_scope(var->name)) {
33083359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 YYLTYPE loc = this->get_location();
33093359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick
33103359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
33113359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      } else {
3312001eee52d461233b1e1d6ed3577965e9bcb209e8Eric Anholt	 state->symbols->add_variable(var);
33133359e58eac19dd7771a78310c8a0e3d3ded55063Ian Romanick      }
3314a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   }
3315a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
33169fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   /* Convert the body of the function to HIR. */
3317894ea972a4defdaafeaa3a248c113b06c7ae0c7eEric Anholt   this->body->hir(&signature->body, state);
33189fa99f3b6c84fe927ba97e6584cd919f097a6c9aKenneth Graunke   signature->is_defined = true;
3319a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
33208bde4cec6b189564b1f2d58514bd7e7a4b40f714Ian Romanick   state->symbols->pop_scope();
3321a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
332241ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   assert(state->current_function == signature);
332341ec6a47ab81620bab9182f987e4bc4780e3a6abIan Romanick   state->current_function = NULL;
3324a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick
33256de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   if (!signature->return_type->is_void() && !state->found_return) {
33266de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      YYLTYPE loc = this->get_location();
33276de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
33286de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       "%s, but no return statement",
33296de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->function_name(),
33306de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke		       signature->return_type->name);
33316de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke   }
33326de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke
3333a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   /* Function definitions do not have r-values.
3334a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick    */
3335a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick   return NULL;
3336a87ac255cf7ef0672b4de865d82e6a40c93b57dIan Romanick}
333716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
333816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
3339fb9fb5f51deca28ed1ec7b71759fb71fc26a0ab6Kenneth Graunkeir_rvalue *
334016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanickast_jump_statement::hir(exec_list *instructions,
334116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick			struct _mesa_glsl_parse_state *state)
334216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick{
3343953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
334416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
3345c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   switch (mode) {
3346c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_return: {
334716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      ir_return *inst;
3348aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt      assert(state->current_function);
334916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
335016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      if (opt_return_value) {
3351b4cdba687c098eea2ecc61349a4ea02a8769909eChad Versace	 ir_rvalue *const ret = opt_return_value->hir(instructions, state);
33522db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick
33532db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	 /* The value of the return type can be NULL if the shader says
33542db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  * 'return foo();' and foo() is a function that returns void.
33552db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  *
33562db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  * NOTE: The GLSL spec doesn't say that this is an error.  The type
33572db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  * of the return value is void.  If the return type of the function is
33582db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  * also void, then this should compile without error.  Seriously.
33592db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	  */
33602db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	 const glsl_type *const ret_type =
33612db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	    (ret == NULL) ? glsl_type::void_type : ret->type;
336216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
336318707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 /* Implicit conversions are not allowed for return values. */
33642db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick	 if (state->current_function->return_type != ret_type) {
336518707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    YYLTYPE loc = this->get_location();
336618707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke
336718707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	    _mesa_glsl_error(& loc, state,
336818707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "`return' with wrong type %s, in function `%s' "
336918707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     "returning %s",
33702db46fe5f0145a6afff5b8edc2f00b8c734bb640Ian Romanick			     ret_type->name,
337118707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->function_name(),
337218707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke			     state->current_function->return_type->name);
337318707eba1cd6c07fa8b63d0ba5b26f6433f1ae91Kenneth Graunke	 }
337416a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
33751660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return(ret);
337616a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      } else {
3377aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 if (state->current_function->return_type->base_type !=
3378aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	     GLSL_TYPE_VOID) {
3379aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    YYLTYPE loc = this->get_location();
3380aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt
3381aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	    _mesa_glsl_error(& loc, state,
3382aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "`return' with no value, in function %s returning "
3383aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt			     "non-void",
3384f96c52ba2e290e3ba5f14cd7f87ba5b4382a1785Kenneth Graunke			     state->current_function->function_name());
3385aad7c7793788f34e98fb7264dc2219fc73002877Eric Anholt	 }
33861660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	 inst = new(ctx) ir_return;
338716a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      }
338816a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
33896de825650560198eb97f19e72b2d56e68e3d7a63Kenneth Graunke      state->found_return = true;
339016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick      instructions->push_tail(inst);
3391c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
339216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   }
339316a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick
3394c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_discard:
3395b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      if (state->target != fragment_shader) {
3396b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 YYLTYPE loc = this->get_location();
3397b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
3398b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt	 _mesa_glsl_error(& loc, state,
3399b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt			  "`discard' may only appear in a fragment shader");
3400b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt      }
340177049a702ad54e09c4102fe8c964e069944f83e5Kenneth Graunke      instructions->push_tail(new(ctx) ir_discard);
3402c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
3403c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick
3404c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_break:
3405c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick   case ast_continue:
34065c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      if (mode == ast_continue &&
34075c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	  state->loop_nesting_ast == NULL) {
34084cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 YYLTYPE loc = this->get_location();
34094cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
34104cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 _mesa_glsl_error(& loc, state,
34115c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe			  "continue may only appear in a loop");
34125c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      } else if (mode == ast_break &&
34135c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe		 state->loop_nesting_ast == NULL &&
341422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt		 state->switch_state.switch_nesting_ast == NULL) {
34155c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 YYLTYPE loc = this->get_location();
34164cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
34175c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 _mesa_glsl_error(& loc, state,
34185c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe			  "break may only appear in a loop or a switch");
34195c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      } else {
34205c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 /* For a loop, inline the for loop expression again,
34215c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	  * since we don't know where near the end of
34225c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	  * the loop body the normal copy of it
34232d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  * is going to be placed.
34242d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	  */
34255c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 if (state->loop_nesting_ast != NULL &&
34265c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	     mode == ast_continue &&
34275c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	     state->loop_nesting_ast->rest_expression) {
34285c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    state->loop_nesting_ast->rest_expression->hir(instructions,
34295c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe							  state);
34302d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt	 }
34312d1ed7b1b112cf13dd7eda7f500691f4c98a1cccEric Anholt
343222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	 if (state->switch_state.is_switch_innermost &&
34335c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	     mode == ast_break) {
34345c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    /* Force break out of switch by setting is_break switch state.
34355c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	     */
343622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt	    ir_variable *const is_break_var = state->switch_state.is_break_var;
34375c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    ir_dereference_variable *const deref_is_break_var =
34385c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	       new(ctx) ir_dereference_variable(is_break_var);
34395c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    ir_constant *const true_val = new(ctx) ir_constant(true);
34405c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    ir_assignment *const set_break_var =
34415c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	       new(ctx) ir_assignment(deref_is_break_var,
34425c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe				      true_val,
34435c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe				      NULL);
34445c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
34455c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    instructions->push_tail(set_break_var);
34465c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 }
34475c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	 else {
34485c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe	    ir_loop_jump *const jump =
34491660a2954797e056caba319c5d6c70b0d4be22feCarl Worth	       new(ctx) ir_loop_jump((mode == ast_break)
34501660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     ? ir_loop_jump::jump_break
34511660a2954797e056caba319c5d6c70b0d4be22feCarl Worth				     : ir_loop_jump::jump_continue);
34524cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	    instructions->push_tail(jump);
34534cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick	 }
34544cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick      }
34554cf20cd37c12c6243a09d52739d3d47f030a1799Ian Romanick
3456c0e76d8352fbe96efb0338e9d98b08494671e504Ian Romanick      break;
3457b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt   }
3458b98020770730bd4a7e9bd5c2e4ab2ae8c1eb0376Eric Anholt
345916a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   /* Jump instructions do not have r-values.
346016a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick    */
346116a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick   return NULL;
346216a246c049fa3c8d7841f87c8defdd0f26f302eeIan Romanick}
34633c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34643c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34653c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickir_rvalue *
34663c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanickast_selection_statement::hir(exec_list *instructions,
34673c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick			     struct _mesa_glsl_parse_state *state)
34683c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick{
3469953ff1283d3d52e6a6b4850c2b0b574111625010Kenneth Graunke   void *ctx = state;
34701660a2954797e056caba319c5d6c70b0d4be22feCarl Worth
34713c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   ir_rvalue *const condition = this->condition->hir(instructions, state);
34723c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34733c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
34743c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
34753c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    "Any expression whose type evaluates to a Boolean can be used as the
34763c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    conditional expression bool-expression. Vector types are not accepted
34773c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *    as the expression to if."
34783c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    *
34793c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * The checks are separated so that higher quality diagnostics can be
34803c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    * generated for cases where both rules are violated.
34813c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
34823c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
34833c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      YYLTYPE loc = this->condition->get_location();
34843c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34853c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
34863c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick		       "boolean");
34873c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   }
34883c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
34891660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   ir_if *const stmt = new(ctx) ir_if(condition);
34903c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
3491665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (then_statement != NULL) {
3492665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
34934f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      then_statement->hir(& stmt->then_instructions, state);
3494665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
3495665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
34963c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
3497665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   if (else_statement != NULL) {
3498665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->push_scope();
34994f9d72fa9e2a4ff1a2aca6de8ee4fa93639c75f1Ian Romanick      else_statement->hir(& stmt->else_instructions, state);
3500665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke      state->symbols->pop_scope();
3501665d75cc5a23f8024034d0c4176fb281f94a30e9Kenneth Graunke   }
35023c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
35033c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   instructions->push_tail(stmt);
35043c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick
35053c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   /* if-statements do not have r-values.
35063c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick    */
35073c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick   return NULL;
35083c6fea3048a0d9add2fec621d30c32f3519d8868Ian Romanick}
35099e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
35109e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick
351185beb39e14556cf02f58116fd287120cd1defbd5Dan McCabeir_rvalue *
351285beb39e14556cf02f58116fd287120cd1defbd5Dan McCabeast_switch_statement::hir(exec_list *instructions,
351385beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe			  struct _mesa_glsl_parse_state *state)
351485beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe{
35155c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   void *ctx = state;
35165c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35175c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_rvalue *const test_expression =
35185c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      this->test_expression->hir(instructions, state);
35195c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35205c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* From page 66 (page 55 of the PDF) of the GLSL 1.50 spec:
35215c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    *
35225c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    *    "The type of init-expression in a switch statement must be a
35235c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    *     scalar integer."
35245c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    *
35255c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    * The checks are separated so that higher quality diagnostics can be
35265c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    * generated for cases where the rule is violated.
35275c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35285c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   if (!test_expression->type->is_integer()) {
35295c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      YYLTYPE loc = this->test_expression->get_location();
35305c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35315c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe      _mesa_glsl_error(& loc,
35325c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe		       state,
35335c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe		       "switch-statement expression must be scalar "
35345c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe		       "integer");
35355c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   }
35365c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35375c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Track the switch-statement nesting in a stack-like manner.
35385c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
353922d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   struct glsl_switch_state saved = state->switch_state;
35405c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
354122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state.is_switch_innermost = true;
354222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state.switch_nesting_ast = this;
3543140632190cf41e6a035ca199b181091d4ed46986Eric Anholt   state->switch_state.labels_ht = hash_table_ctor(0, hash_table_pointer_hash,
3544140632190cf41e6a035ca199b181091d4ed46986Eric Anholt						   hash_table_pointer_compare);
354557e44371a5b6aa8122b6a482ed6bd33e797ea1d2Eric Anholt   state->switch_state.previous_default = NULL;
35465c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35475c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Initalize is_fallthru state to false.
35485c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35495c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_rvalue *const is_fallthru_val = new (ctx) ir_constant(false);
355022d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state.is_fallthru_var =
355122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      new(ctx) ir_variable(glsl_type::bool_type,
355222d81f154fed9e004cca91807808ae3b81b01cedEric Anholt			   "switch_is_fallthru_tmp",
355322d81f154fed9e004cca91807808ae3b81b01cedEric Anholt			   ir_var_temporary);
355422d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   instructions->push_tail(state->switch_state.is_fallthru_var);
35555c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35565c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_dereference_variable *deref_is_fallthru_var =
355722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
35585c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   instructions->push_tail(new(ctx) ir_assignment(deref_is_fallthru_var,
35595c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe						  is_fallthru_val,
35605c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe						  NULL));
35615c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35625c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Initalize is_break state to false.
35635c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35645c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_rvalue *const is_break_val = new (ctx) ir_constant(false);
356522d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state.is_break_var = new(ctx) ir_variable(glsl_type::bool_type,
356622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt							   "switch_is_break_tmp",
356722d81f154fed9e004cca91807808ae3b81b01cedEric Anholt							   ir_var_temporary);
356822d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   instructions->push_tail(state->switch_state.is_break_var);
35695c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35705c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   ir_dereference_variable *deref_is_break_var =
357122d81f154fed9e004cca91807808ae3b81b01cedEric Anholt      new(ctx) ir_dereference_variable(state->switch_state.is_break_var);
35725c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   instructions->push_tail(new(ctx) ir_assignment(deref_is_break_var,
35735c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe						  is_break_val,
35745c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe						  NULL));
35755c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
35765c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Cache test expression.
35775c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35785c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   test_to_hir(instructions, state);
35795462f3679ab7217d3a3be48365750801c7771237Eric Anholt
35805c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   /* Emit code for body of switch stmt.
35815c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe    */
35825c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe   body->hir(instructions, state);
35835c02e2e2de75b9d18ca25b4f1cba38c4a89c5bd0Dan McCabe
3584140632190cf41e6a035ca199b181091d4ed46986Eric Anholt   hash_table_dtor(state->switch_state.labels_ht);
3585140632190cf41e6a035ca199b181091d4ed46986Eric Anholt
358622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt   state->switch_state = saved;
358785beb39e14556cf02f58116fd287120cd1defbd5Dan McCabe
35885462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Switch statements do not have r-values. */
35895462f3679ab7217d3a3be48365750801c7771237Eric Anholt   return NULL;
35905462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
35915462f3679ab7217d3a3be48365750801c7771237Eric Anholt
35925462f3679ab7217d3a3be48365750801c7771237Eric Anholt
35935462f3679ab7217d3a3be48365750801c7771237Eric Anholtvoid
35945462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_switch_statement::test_to_hir(exec_list *instructions,
35955462f3679ab7217d3a3be48365750801c7771237Eric Anholt				  struct _mesa_glsl_parse_state *state)
35965462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
35975462f3679ab7217d3a3be48365750801c7771237Eric Anholt   void *ctx = state;
35985462f3679ab7217d3a3be48365750801c7771237Eric Anholt
35995462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Cache value of test expression. */
36005462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_rvalue *const test_val =
36015462f3679ab7217d3a3be48365750801c7771237Eric Anholt      test_expression->hir(instructions,
36025462f3679ab7217d3a3be48365750801c7771237Eric Anholt			   state);
36035462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36045462f3679ab7217d3a3be48365750801c7771237Eric Anholt   state->switch_state.test_var = new(ctx) ir_variable(glsl_type::int_type,
36055462f3679ab7217d3a3be48365750801c7771237Eric Anholt						       "switch_test_tmp",
36065462f3679ab7217d3a3be48365750801c7771237Eric Anholt						       ir_var_temporary);
36075462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_dereference_variable *deref_test_var =
36085462f3679ab7217d3a3be48365750801c7771237Eric Anholt      new(ctx) ir_dereference_variable(state->switch_state.test_var);
36095462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36105462f3679ab7217d3a3be48365750801c7771237Eric Anholt   instructions->push_tail(state->switch_state.test_var);
36115462f3679ab7217d3a3be48365750801c7771237Eric Anholt   instructions->push_tail(new(ctx) ir_assignment(deref_test_var, test_val,
36125462f3679ab7217d3a3be48365750801c7771237Eric Anholt						  NULL));
36135462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
36145462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36155462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36165462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue *
36175462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_switch_body::hir(exec_list *instructions,
36185462f3679ab7217d3a3be48365750801c7771237Eric Anholt		     struct _mesa_glsl_parse_state *state)
36195462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
36205462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (stmts != NULL)
36215462f3679ab7217d3a3be48365750801c7771237Eric Anholt      stmts->hir(instructions, state);
36225462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36235462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Switch bodies do not have r-values. */
36245462f3679ab7217d3a3be48365750801c7771237Eric Anholt   return NULL;
36255462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
36265462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36275462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue *
36285462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_case_statement_list::hir(exec_list *instructions,
36295462f3679ab7217d3a3be48365750801c7771237Eric Anholt			     struct _mesa_glsl_parse_state *state)
36305462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
36315462f3679ab7217d3a3be48365750801c7771237Eric Anholt   foreach_list_typed (ast_case_statement, case_stmt, link, & this->cases)
36325462f3679ab7217d3a3be48365750801c7771237Eric Anholt      case_stmt->hir(instructions, state);
36335462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36345462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Case statements do not have r-values. */
36355462f3679ab7217d3a3be48365750801c7771237Eric Anholt   return NULL;
36365462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
36375462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36385462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue *
36395462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_case_statement::hir(exec_list *instructions,
36405462f3679ab7217d3a3be48365750801c7771237Eric Anholt			struct _mesa_glsl_parse_state *state)
36415462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
36425462f3679ab7217d3a3be48365750801c7771237Eric Anholt   labels->hir(instructions, state);
36435462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36445462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Conditionally set fallthru state based on break state. */
36455462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_constant *const false_val = new(state) ir_constant(false);
36465462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_dereference_variable *const deref_is_fallthru_var =
36475462f3679ab7217d3a3be48365750801c7771237Eric Anholt      new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
36485462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_dereference_variable *const deref_is_break_var =
36495462f3679ab7217d3a3be48365750801c7771237Eric Anholt      new(state) ir_dereference_variable(state->switch_state.is_break_var);
36505462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_assignment *const reset_fallthru_on_break =
36515462f3679ab7217d3a3be48365750801c7771237Eric Anholt      new(state) ir_assignment(deref_is_fallthru_var,
36525462f3679ab7217d3a3be48365750801c7771237Eric Anholt			       false_val,
36535462f3679ab7217d3a3be48365750801c7771237Eric Anholt			       deref_is_break_var);
36545462f3679ab7217d3a3be48365750801c7771237Eric Anholt   instructions->push_tail(reset_fallthru_on_break);
36555462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36565462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Guard case statements depending on fallthru state. */
36575462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_dereference_variable *const deref_fallthru_guard =
36585462f3679ab7217d3a3be48365750801c7771237Eric Anholt      new(state) ir_dereference_variable(state->switch_state.is_fallthru_var);
36595462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_if *const test_fallthru = new(state) ir_if(deref_fallthru_guard);
36605462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36615462f3679ab7217d3a3be48365750801c7771237Eric Anholt   foreach_list_typed (ast_node, stmt, link, & this->stmts)
36625462f3679ab7217d3a3be48365750801c7771237Eric Anholt      stmt->hir(& test_fallthru->then_instructions, state);
36635462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36645462f3679ab7217d3a3be48365750801c7771237Eric Anholt   instructions->push_tail(test_fallthru);
36655462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36665462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Case statements do not have r-values. */
36675462f3679ab7217d3a3be48365750801c7771237Eric Anholt   return NULL;
36685462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
36695462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36705462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36715462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue *
36725462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_case_label_list::hir(exec_list *instructions,
36735462f3679ab7217d3a3be48365750801c7771237Eric Anholt			 struct _mesa_glsl_parse_state *state)
36745462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
36755462f3679ab7217d3a3be48365750801c7771237Eric Anholt   foreach_list_typed (ast_case_label, label, link, & this->labels)
36765462f3679ab7217d3a3be48365750801c7771237Eric Anholt      label->hir(instructions, state);
36775462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36785462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Case labels do not have r-values. */
36795462f3679ab7217d3a3be48365750801c7771237Eric Anholt   return NULL;
36805462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
36815462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36825462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue *
36835462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_case_label::hir(exec_list *instructions,
36845462f3679ab7217d3a3be48365750801c7771237Eric Anholt		    struct _mesa_glsl_parse_state *state)
36855462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
36865462f3679ab7217d3a3be48365750801c7771237Eric Anholt   void *ctx = state;
36875462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36885462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_dereference_variable *deref_fallthru_var =
36895462f3679ab7217d3a3be48365750801c7771237Eric Anholt      new(ctx) ir_dereference_variable(state->switch_state.is_fallthru_var);
36905462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36915462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_rvalue *const true_val = new(ctx) ir_constant(true);
36925462f3679ab7217d3a3be48365750801c7771237Eric Anholt
36935462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* If not default case, ... */
36945462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (this->test_value != NULL) {
36955462f3679ab7217d3a3be48365750801c7771237Eric Anholt      /* Conditionally set fallthru state based on
36965462f3679ab7217d3a3be48365750801c7771237Eric Anholt       * comparison of cached test expression value to case label.
36975462f3679ab7217d3a3be48365750801c7771237Eric Anholt       */
36985462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_rvalue *const label_rval = this->test_value->hir(instructions, state);
36995462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_constant *label_const = label_rval->constant_expression_value();
37005462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37015462f3679ab7217d3a3be48365750801c7771237Eric Anholt      if (!label_const) {
37025462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 YYLTYPE loc = this->test_value->get_location();
37035462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37045462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 _mesa_glsl_error(& loc, state,
37055462f3679ab7217d3a3be48365750801c7771237Eric Anholt			  "switch statement case label must be a "
37065462f3679ab7217d3a3be48365750801c7771237Eric Anholt			  "constant expression");
37075462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37085462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 /* Stuff a dummy value in to allow processing to continue. */
37095462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 label_const = new(ctx) ir_constant(0);
37105462f3679ab7217d3a3be48365750801c7771237Eric Anholt      } else {
37115462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 ast_expression *previous_label = (ast_expression *)
37125462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    hash_table_find(state->switch_state.labels_ht,
37135462f3679ab7217d3a3be48365750801c7771237Eric Anholt			    (void *)(uintptr_t)label_const->value.u[0]);
37145462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37155462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 if (previous_label) {
37165462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    YYLTYPE loc = this->test_value->get_location();
37175462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    _mesa_glsl_error(& loc, state,
37185462f3679ab7217d3a3be48365750801c7771237Eric Anholt			     "duplicate case value");
37195462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37205462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    loc = previous_label->get_location();
37215462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    _mesa_glsl_error(& loc, state,
37225462f3679ab7217d3a3be48365750801c7771237Eric Anholt			     "this is the previous case label");
37235462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 } else {
37245462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    hash_table_insert(state->switch_state.labels_ht,
37255462f3679ab7217d3a3be48365750801c7771237Eric Anholt			      this->test_value,
3726140632190cf41e6a035ca199b181091d4ed46986Eric Anholt			      (void *)(uintptr_t)label_const->value.u[0]);
37275462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 }
37285462f3679ab7217d3a3be48365750801c7771237Eric Anholt      }
37295462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37305462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_dereference_variable *deref_test_var =
37315462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 new(ctx) ir_dereference_variable(state->switch_state.test_var);
37325462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37335462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_rvalue *const test_cond = new(ctx) ir_expression(ir_binop_all_equal,
37345462f3679ab7217d3a3be48365750801c7771237Eric Anholt							  label_const,
37355462f3679ab7217d3a3be48365750801c7771237Eric Anholt							  deref_test_var);
37365462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37375462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_assignment *set_fallthru_on_test =
37385462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 new(ctx) ir_assignment(deref_fallthru_var,
37395462f3679ab7217d3a3be48365750801c7771237Eric Anholt				true_val,
37405462f3679ab7217d3a3be48365750801c7771237Eric Anholt				test_cond);
37415462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37425462f3679ab7217d3a3be48365750801c7771237Eric Anholt      instructions->push_tail(set_fallthru_on_test);
37435462f3679ab7217d3a3be48365750801c7771237Eric Anholt   } else { /* default case */
37445462f3679ab7217d3a3be48365750801c7771237Eric Anholt      if (state->switch_state.previous_default) {
37455462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 printf("a\n");
37465462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 YYLTYPE loc = this->get_location();
37475462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 _mesa_glsl_error(& loc, state,
37485462f3679ab7217d3a3be48365750801c7771237Eric Anholt			  "multiple default labels in one switch");
37495462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37505462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 printf("b\n");
37515462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37525462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 loc = state->switch_state.previous_default->get_location();
37535462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 _mesa_glsl_error(& loc, state,
37545462f3679ab7217d3a3be48365750801c7771237Eric Anholt			  "this is the first default label");
37555462f3679ab7217d3a3be48365750801c7771237Eric Anholt      }
37565462f3679ab7217d3a3be48365750801c7771237Eric Anholt      state->switch_state.previous_default = this;
37575462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37585462f3679ab7217d3a3be48365750801c7771237Eric Anholt      /* Set falltrhu state. */
37595462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_assignment *set_fallthru =
37605462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 new(ctx) ir_assignment(deref_fallthru_var, true_val, NULL);
37615462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37625462f3679ab7217d3a3be48365750801c7771237Eric Anholt      instructions->push_tail(set_fallthru);
37635462f3679ab7217d3a3be48365750801c7771237Eric Anholt   }
3764140632190cf41e6a035ca199b181091d4ed46986Eric Anholt
37655462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Case statements do not have r-values. */
37665462f3679ab7217d3a3be48365750801c7771237Eric Anholt   return NULL;
37675462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
37685462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37695462f3679ab7217d3a3be48365750801c7771237Eric Anholtvoid
37705462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_iteration_statement::condition_to_hir(ir_loop *stmt,
37715462f3679ab7217d3a3be48365750801c7771237Eric Anholt					  struct _mesa_glsl_parse_state *state)
37725462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
37735462f3679ab7217d3a3be48365750801c7771237Eric Anholt   void *ctx = state;
37745462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37755462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (condition != NULL) {
37765462f3679ab7217d3a3be48365750801c7771237Eric Anholt      ir_rvalue *const cond =
37775462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 condition->hir(& stmt->body_instructions, state);
37785462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37795462f3679ab7217d3a3be48365750801c7771237Eric Anholt      if ((cond == NULL)
37805462f3679ab7217d3a3be48365750801c7771237Eric Anholt	  || !cond->type->is_boolean() || !cond->type->is_scalar()) {
37815462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 YYLTYPE loc = condition->get_location();
37825462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37835462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 _mesa_glsl_error(& loc, state,
37845462f3679ab7217d3a3be48365750801c7771237Eric Anholt			  "loop condition must be scalar boolean");
37855462f3679ab7217d3a3be48365750801c7771237Eric Anholt      } else {
37865462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 /* As the first code in the loop body, generate a block that looks
37875462f3679ab7217d3a3be48365750801c7771237Eric Anholt	  * like 'if (!condition) break;' as the loop termination condition.
37885462f3679ab7217d3a3be48365750801c7771237Eric Anholt	  */
37895462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 ir_rvalue *const not_cond =
37905d6ea16dfe99e1aba61c25a897b66951faab1a39Eric Anholt	    new(ctx) ir_expression(ir_unop_logic_not, cond);
37915462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37925462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
37935462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37945462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 ir_jump *const break_stmt =
37955462f3679ab7217d3a3be48365750801c7771237Eric Anholt	    new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
37965462f3679ab7217d3a3be48365750801c7771237Eric Anholt
37975462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 if_stmt->then_instructions.push_tail(break_stmt);
37985462f3679ab7217d3a3be48365750801c7771237Eric Anholt	 stmt->body_instructions.push_tail(if_stmt);
37995462f3679ab7217d3a3be48365750801c7771237Eric Anholt      }
38005462f3679ab7217d3a3be48365750801c7771237Eric Anholt   }
38015462f3679ab7217d3a3be48365750801c7771237Eric Anholt}
38025462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38035462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38045462f3679ab7217d3a3be48365750801c7771237Eric Anholtir_rvalue *
38055462f3679ab7217d3a3be48365750801c7771237Eric Anholtast_iteration_statement::hir(exec_list *instructions,
38065462f3679ab7217d3a3be48365750801c7771237Eric Anholt			     struct _mesa_glsl_parse_state *state)
38075462f3679ab7217d3a3be48365750801c7771237Eric Anholt{
38085462f3679ab7217d3a3be48365750801c7771237Eric Anholt   void *ctx = state;
38095462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38105462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* For-loops and while-loops start a new scope, but do-while loops do not.
38115462f3679ab7217d3a3be48365750801c7771237Eric Anholt    */
38125462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (mode != ast_do_while)
38135462f3679ab7217d3a3be48365750801c7771237Eric Anholt      state->symbols->push_scope();
38145462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38155462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (init_statement != NULL)
38165462f3679ab7217d3a3be48365750801c7771237Eric Anholt      init_statement->hir(instructions, state);
38175462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38185462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ir_loop *const stmt = new(ctx) ir_loop();
38195462f3679ab7217d3a3be48365750801c7771237Eric Anholt   instructions->push_tail(stmt);
38205462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38215462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Track the current loop nesting. */
38225462f3679ab7217d3a3be48365750801c7771237Eric Anholt   ast_iteration_statement *nesting_ast = state->loop_nesting_ast;
38235462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38245462f3679ab7217d3a3be48365750801c7771237Eric Anholt   state->loop_nesting_ast = this;
38255462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38265462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Likewise, indicate that following code is closest to a loop,
38275462f3679ab7217d3a3be48365750801c7771237Eric Anholt    * NOT closest to a switch.
38285462f3679ab7217d3a3be48365750801c7771237Eric Anholt    */
38295462f3679ab7217d3a3be48365750801c7771237Eric Anholt   bool saved_is_switch_innermost = state->switch_state.is_switch_innermost;
38305462f3679ab7217d3a3be48365750801c7771237Eric Anholt   state->switch_state.is_switch_innermost = false;
38315462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38325462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (mode != ast_do_while)
38335462f3679ab7217d3a3be48365750801c7771237Eric Anholt      condition_to_hir(stmt, state);
38345462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38355462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (body != NULL)
38365462f3679ab7217d3a3be48365750801c7771237Eric Anholt      body->hir(& stmt->body_instructions, state);
38375462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38385462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (rest_expression != NULL)
38395462f3679ab7217d3a3be48365750801c7771237Eric Anholt      rest_expression->hir(& stmt->body_instructions, state);
38405462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38415462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (mode == ast_do_while)
38425462f3679ab7217d3a3be48365750801c7771237Eric Anholt      condition_to_hir(stmt, state);
38435462f3679ab7217d3a3be48365750801c7771237Eric Anholt
38445462f3679ab7217d3a3be48365750801c7771237Eric Anholt   if (mode != ast_do_while)
38455462f3679ab7217d3a3be48365750801c7771237Eric Anholt      state->symbols->pop_scope();
384622d81f154fed9e004cca91807808ae3b81b01cedEric Anholt
38475462f3679ab7217d3a3be48365750801c7771237Eric Anholt   /* Restore previous nesting before returning. */
38485462f3679ab7217d3a3be48365750801c7771237Eric Anholt   state->loop_nesting_ast = nesting_ast;
38495462f3679ab7217d3a3be48365750801c7771237Eric Anholt   state->switch_state.is_switch_innermost = saved_is_switch_innermost;
3850e9d0f265aabb39928d4d8a527684bf3b9eebc21cIan Romanick
38519e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   /* Loops do not have r-values.
38529e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick    */
38539e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick   return NULL;
38549e7d010ee0ed89f1fe07a06a9d9bb22e15106ae2Ian Romanick}
38553455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
38563455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
38573455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
38583455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_type_specifier::hir(exec_list *instructions,
38593455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
38603455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
386108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   if (!this->is_precision_statement && this->structure == NULL)
386208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      return NULL;
386308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
386408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   YYLTYPE loc = this->get_location();
386508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
386608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   if (this->precision != ast_precision_none
386708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace       && state->language_version != 100
386808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace       && state->language_version < 130) {
386908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      _mesa_glsl_error(&loc, state,
387008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                       "precision qualifiers exist only in "
387108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                       "GLSL ES 1.00, and GLSL 1.30 and later");
387208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      return NULL;
387308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   }
387408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   if (this->precision != ast_precision_none
387508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace       && this->structure != NULL) {
387608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      _mesa_glsl_error(&loc, state,
387708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                       "precision qualifiers do not apply to structures");
387808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      return NULL;
387908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   }
388008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
388108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   /* If this is a precision statement, check that the type to which it is
388208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    * applied is either float or int.
388308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *
388408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    * From section 4.5.3 of the GLSL 1.30 spec:
388508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *    "The precision statement
388608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *       precision precision-qualifier type;
388708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *    can be used to establish a default precision qualifier. The type
388808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *    field can be either int or float [...].  Any other types or
388908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    *    qualifiers will result in an error.
389008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace    */
389108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   if (this->is_precision_statement) {
389208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      assert(this->precision != ast_precision_none);
389308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      assert(this->structure == NULL); /* The check for structures was
389408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                                        * performed above. */
389508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      if (this->is_array) {
389608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace         _mesa_glsl_error(&loc, state,
389708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                          "default precision statements do not apply to "
389808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                          "arrays");
389908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace         return NULL;
390008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      }
3901eb7a71dea78152142b456f29e4881c4d3aeb56b6Eric Anholt      if (strcmp(this->type_name, "float") != 0 &&
3902eb7a71dea78152142b456f29e4881c4d3aeb56b6Eric Anholt	  strcmp(this->type_name, "int") != 0) {
390308a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace         _mesa_glsl_error(&loc, state,
390408a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                          "default precision statements apply only to types "
390508a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace                          "float and int");
390608a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace         return NULL;
390708a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      }
390808a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
390908a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      /* FINISHME: Translate precision statements into IR. */
391008a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace      return NULL;
391108a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace   }
391208a286c9cc8fecb081057e0f551c88a446c47b6fChad Versace
39133455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   if (this->structure != NULL)
39143455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      return this->structure->hir(instructions, state);
391585ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick
391685ba37b97df8edd18b757bc475b12b66f4b117edIan Romanick   return NULL;
39173455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
39183455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39193455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39203455ce614424a5a23a23037e23d0454e476bceeaIan Romanickir_rvalue *
39213455ce614424a5a23a23037e23d0454e476bceeaIan Romanickast_struct_specifier::hir(exec_list *instructions,
39223455ce614424a5a23a23037e23d0454e476bceeaIan Romanick			  struct _mesa_glsl_parse_state *state)
39233455ce614424a5a23a23037e23d0454e476bceeaIan Romanick{
39243455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned decl_count = 0;
39253455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39263455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Make an initial pass over the list of structure fields to determine how
39273455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * many there are.  Each element in this list is an ast_declarator_list.
39283455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * This means that we actually need to count the number of elements in the
39293455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * 'declarations' list in each of the elements.
39303455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
39312b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
39322b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
3933304ea90233baeac6801a98e981658cb7a2d2501cIan Romanick      foreach_list_const (decl_ptr, & decl_list->declarations) {
39343455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_count++;
39353455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
39363455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
39373455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39383455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Allocate storage for the structure fields and process the field
39393455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * declarations.  As the declarations are processed, try to also convert
39403455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * the types to HIR.  This ensures that structure definitions embedded in
39413455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    * other structure definitions are processed.
39423455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
3943d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8Kenneth Graunke   glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field,
394421b0dbd79937e9d6787f045af7d60d4b6c649ec8Eric Anholt						  decl_count);
39453455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39463455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   unsigned i = 0;
39472b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick   foreach_list_typed (ast_declarator_list, decl_list, link,
39482b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick		       &this->declarations) {
39493455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const char *type_name;
39503455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39513455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      decl_list->type->specifier->hir(instructions, state);
39523455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
3953c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke      /* Section 10.9 of the GLSL ES 1.00 specification states that
3954c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke       * embedded structure definitions have been removed from the language.
3955c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke       */
3956c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke      if (state->es_shader && decl_list->type->specifier->structure != NULL) {
3957c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke	 YYLTYPE loc = this->get_location();
3958c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke	 _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
3959c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke			  "not allowed in GLSL ES 1.00.");
3960c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke      }
3961c98deb18d5836f75cf62562f9304e4d90e0ea920Kenneth Graunke
39623455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      const glsl_type *decl_type =
39633455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 decl_list->type->specifier->glsl_type(& type_name, state);
39643455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39652b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick      foreach_list_typed (ast_declaration, decl, link,
39662b97dc657a0e762bc67216405419cd348eb948c0Ian Romanick			  &decl_list->declarations) {
3967d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 const struct glsl_type *field_type = decl_type;
3968d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 if (decl->is_array) {
3969d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	    YYLTYPE loc = decl->get_location();
3970d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	    field_type = process_array_type(&loc, decl_type, decl->array_size,
3971d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke					    state);
3972d8e34e29eb58c38ef60226156aab8f4a93b397b7Kenneth Graunke	 }
397373986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	 fields[i].type = (field_type != NULL)
397473986a7a262807ab2cfd6d46ae17cfc7a30cdfecIan Romanick	    ? field_type : glsl_type::error_type;
39753455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 fields[i].name = decl->identifier;
39763455ce614424a5a23a23037e23d0454e476bceeaIan Romanick	 i++;
39773455ce614424a5a23a23037e23d0454e476bceeaIan Romanick      }
39783455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   }
39793455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39803455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   assert(i == decl_count);
39813455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
398249e3577b91f44013746f7a3db411e7041b7d899eIan Romanick   const glsl_type *t =
3983ca92ae2699c4aad21c0811b9a5562b9223816cafKenneth Graunke      glsl_type::get_record_instance(fields, decl_count, this->name);
39841d28b617ba66cfcb1641c9f516146d51aa82b118Ian Romanick
3985ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   YYLTYPE loc = this->get_location();
3986a789ca649cb143c0c5bf3209ff1bde398fbd777eIan Romanick   if (!state->symbols->add_type(name, t)) {
3987ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick      _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
3988ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   } else {
3989eb639349e289a6b8be06a54f5e9e0ce18c71d511Kenneth Graunke      const glsl_type **s = reralloc(state, state->user_structures,
3990eb639349e289a6b8be06a54f5e9e0ce18c71d511Kenneth Graunke				     const glsl_type *,
3991eb639349e289a6b8be06a54f5e9e0ce18c71d511Kenneth Graunke				     state->num_user_structures + 1);
3992a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      if (s != NULL) {
3993a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 s[state->num_user_structures] = t;
3994a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->user_structures = s;
3995a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick	 state->num_user_structures++;
3996a2c6df556655e3619f5a0cd82b0d11aac37c5692Ian Romanick      }
3997ab89927a91a0ea6ffdb56e5e75044472f7277f4aIan Romanick   }
39983455ce614424a5a23a23037e23d0454e476bceeaIan Romanick
39993455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   /* Structure type definitions do not have r-values.
40003455ce614424a5a23a23037e23d0454e476bceeaIan Romanick    */
40013455ce614424a5a23a23037e23d0454e476bceeaIan Romanick   return NULL;
40023455ce614424a5a23a23037e23d0454e476bceeaIan Romanick}
40034b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
40044b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholtstatic void
40054b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholtdetect_conflicting_assignments(struct _mesa_glsl_parse_state *state,
40064b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt			       exec_list *instructions)
40074b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt{
40084b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   bool gl_FragColor_assigned = false;
40094b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   bool gl_FragData_assigned = false;
40104b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   bool user_defined_fs_output_assigned = false;
40114b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   ir_variable *user_defined_fs_output = NULL;
40124b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
40134b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   /* It would be nice to have proper location information. */
40144b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   YYLTYPE loc;
40154b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   memset(&loc, 0, sizeof(loc));
40164b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
40174b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   foreach_list(node, instructions) {
40184b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      ir_variable *var = ((ir_instruction *)node)->as_variable();
40194b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
4020b2ee5a08bae6cdbbdafc1f1d9d6f3afbad2f7944Eric Anholt      if (!var || !var->assigned)
40214b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt	 continue;
40224b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
40234b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      if (strcmp(var->name, "gl_FragColor") == 0)
4024b2ee5a08bae6cdbbdafc1f1d9d6f3afbad2f7944Eric Anholt	 gl_FragColor_assigned = true;
40254b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      else if (strcmp(var->name, "gl_FragData") == 0)
4026b2ee5a08bae6cdbbdafc1f1d9d6f3afbad2f7944Eric Anholt	 gl_FragData_assigned = true;
40274b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      else if (strncmp(var->name, "gl_", 3) != 0) {
40284b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt	 if (state->target == fragment_shader &&
40294b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt	     (var->mode == ir_var_out || var->mode == ir_var_inout)) {
40304b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt	    user_defined_fs_output_assigned = true;
40314b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt	    user_defined_fs_output = var;
40324b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt	 }
40334b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      }
40344b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   }
40354b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt
40364b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   /* From the GLSL 1.30 spec:
40374b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *
40384b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *     "If a shader statically assigns a value to gl_FragColor, it
40394b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      may not assign a value to any element of gl_FragData. If a
40404b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      shader statically writes a value to any element of
40414b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      gl_FragData, it may not assign a value to
40424b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      gl_FragColor. That is, a shader may assign values to either
40434b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      gl_FragColor or gl_FragData, but not both. Multiple shaders
40444b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      linked together must also consistently write just one of
40454b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      these variables.  Similarly, if user declared output
40464b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      variables are in use (statically assigned to), then the
40474b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      built-in variables gl_FragColor and gl_FragData may not be
40484b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      assigned to. These incorrect usages all generate compile
40494b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    *      time errors."
40504b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt    */
40514b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   if (gl_FragColor_assigned && gl_FragData_assigned) {
40524b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      _mesa_glsl_error(&loc, state, "fragment shader writes to both "
40534b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt		       "`gl_FragColor' and `gl_FragData'\n");
40544b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   } else if (gl_FragColor_assigned && user_defined_fs_output_assigned) {
40554b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      _mesa_glsl_error(&loc, state, "fragment shader writes to both "
40564b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt		       "`gl_FragColor' and `%s'\n",
40574b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt		       user_defined_fs_output->name);
40584b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   } else if (gl_FragData_assigned && user_defined_fs_output_assigned) {
40594b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt      _mesa_glsl_error(&loc, state, "fragment shader writes to both "
40604b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt		       "`gl_FragData' and `%s'\n",
40614b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt		       user_defined_fs_output->name);
40624b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt   }
40634b2a4cb7c2131c3c31e3d44a0d8838ef45f270e7Eric Anholt}
4064