11591693c7b415e9869157c711fe11263c95d74eDavid Li/*
21591693c7b415e9869157c711fe11263c95d74eDavid Li * Copyright © 2010 Intel Corporation
31591693c7b415e9869157c711fe11263c95d74eDavid Li *
41591693c7b415e9869157c711fe11263c95d74eDavid Li * Permission is hereby granted, free of charge, to any person obtaining a
51591693c7b415e9869157c711fe11263c95d74eDavid Li * copy of this software and associated documentation files (the "Software"),
61591693c7b415e9869157c711fe11263c95d74eDavid Li * to deal in the Software without restriction, including without limitation
71591693c7b415e9869157c711fe11263c95d74eDavid Li * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81591693c7b415e9869157c711fe11263c95d74eDavid Li * and/or sell copies of the Software, and to permit persons to whom the
91591693c7b415e9869157c711fe11263c95d74eDavid Li * Software is furnished to do so, subject to the following conditions:
101591693c7b415e9869157c711fe11263c95d74eDavid Li *
111591693c7b415e9869157c711fe11263c95d74eDavid Li * The above copyright notice and this permission notice (including the next
121591693c7b415e9869157c711fe11263c95d74eDavid Li * paragraph) shall be included in all copies or substantial portions of the
131591693c7b415e9869157c711fe11263c95d74eDavid Li * Software.
141591693c7b415e9869157c711fe11263c95d74eDavid Li *
151591693c7b415e9869157c711fe11263c95d74eDavid Li * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161591693c7b415e9869157c711fe11263c95d74eDavid Li * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171591693c7b415e9869157c711fe11263c95d74eDavid Li * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181591693c7b415e9869157c711fe11263c95d74eDavid Li * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191591693c7b415e9869157c711fe11263c95d74eDavid Li * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201591693c7b415e9869157c711fe11263c95d74eDavid Li * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
211591693c7b415e9869157c711fe11263c95d74eDavid Li * DEALINGS IN THE SOFTWARE.
221591693c7b415e9869157c711fe11263c95d74eDavid Li */
231591693c7b415e9869157c711fe11263c95d74eDavid Li
241591693c7b415e9869157c711fe11263c95d74eDavid Li/**
251591693c7b415e9869157c711fe11263c95d74eDavid Li * \file ast_to_hir.c
261591693c7b415e9869157c711fe11263c95d74eDavid Li * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
271591693c7b415e9869157c711fe11263c95d74eDavid Li *
281591693c7b415e9869157c711fe11263c95d74eDavid Li * During the conversion to HIR, the majority of the symantic checking is
291591693c7b415e9869157c711fe11263c95d74eDavid Li * preformed on the program.  This includes:
301591693c7b415e9869157c711fe11263c95d74eDavid Li *
311591693c7b415e9869157c711fe11263c95d74eDavid Li *    * Symbol table management
321591693c7b415e9869157c711fe11263c95d74eDavid Li *    * Type checking
331591693c7b415e9869157c711fe11263c95d74eDavid Li *    * Function binding
341591693c7b415e9869157c711fe11263c95d74eDavid Li *
351591693c7b415e9869157c711fe11263c95d74eDavid Li * The majority of this work could be done during parsing, and the parser could
361591693c7b415e9869157c711fe11263c95d74eDavid Li * probably generate HIR directly.  However, this results in frequent changes
371591693c7b415e9869157c711fe11263c95d74eDavid Li * to the parser code.  Since we do not assume that every system this complier
381591693c7b415e9869157c711fe11263c95d74eDavid Li * is built on will have Flex and Bison installed, we have to store the code
391591693c7b415e9869157c711fe11263c95d74eDavid Li * generated by these tools in our version control system.  In other parts of
401591693c7b415e9869157c711fe11263c95d74eDavid Li * the system we've seen problems where a parser was changed but the generated
411591693c7b415e9869157c711fe11263c95d74eDavid Li * code was not committed, merge conflicts where created because two developers
421591693c7b415e9869157c711fe11263c95d74eDavid Li * had slightly different versions of Bison installed, etc.
431591693c7b415e9869157c711fe11263c95d74eDavid Li *
441591693c7b415e9869157c711fe11263c95d74eDavid Li * I have also noticed that running Bison generated parsers in GDB is very
451591693c7b415e9869157c711fe11263c95d74eDavid Li * irritating.  When you get a segfault on '$$ = $1->foo', you can't very
461591693c7b415e9869157c711fe11263c95d74eDavid Li * well 'print $1' in GDB.
471591693c7b415e9869157c711fe11263c95d74eDavid Li *
481591693c7b415e9869157c711fe11263c95d74eDavid Li * As a result, my preference is to put as little C code as possible in the
491591693c7b415e9869157c711fe11263c95d74eDavid Li * parser (and lexer) sources.
501591693c7b415e9869157c711fe11263c95d74eDavid Li */
511591693c7b415e9869157c711fe11263c95d74eDavid Li
521591693c7b415e9869157c711fe11263c95d74eDavid Li#include "main/core.h" /* for struct gl_extensions */
531591693c7b415e9869157c711fe11263c95d74eDavid Li#include "glsl_symbol_table.h"
541591693c7b415e9869157c711fe11263c95d74eDavid Li#include "glsl_parser_extras.h"
551591693c7b415e9869157c711fe11263c95d74eDavid Li#include "ast.h"
561591693c7b415e9869157c711fe11263c95d74eDavid Li#include "glsl_types.h"
571591693c7b415e9869157c711fe11263c95d74eDavid Li#include "ir.h"
581591693c7b415e9869157c711fe11263c95d74eDavid Li
591591693c7b415e9869157c711fe11263c95d74eDavid Livoid
601591693c7b415e9869157c711fe11263c95d74eDavid Li_mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
611591693c7b415e9869157c711fe11263c95d74eDavid Li{
621591693c7b415e9869157c711fe11263c95d74eDavid Li   _mesa_glsl_initialize_variables(instructions, state);
631591693c7b415e9869157c711fe11263c95d74eDavid Li   _mesa_glsl_initialize_functions(instructions, state);
641591693c7b415e9869157c711fe11263c95d74eDavid Li
651591693c7b415e9869157c711fe11263c95d74eDavid Li   state->symbols->language_version = state->language_version;
661591693c7b415e9869157c711fe11263c95d74eDavid Li
671591693c7b415e9869157c711fe11263c95d74eDavid Li   state->current_function = NULL;
681591693c7b415e9869157c711fe11263c95d74eDavid Li
691591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Section 4.2 of the GLSL 1.20 specification states:
701591693c7b415e9869157c711fe11263c95d74eDavid Li    * "The built-in functions are scoped in a scope outside the global scope
711591693c7b415e9869157c711fe11263c95d74eDavid Li    *  users declare global variables in.  That is, a shader's global scope,
721591693c7b415e9869157c711fe11263c95d74eDavid Li    *  available for user-defined functions and global variables, is nested
731591693c7b415e9869157c711fe11263c95d74eDavid Li    *  inside the scope containing the built-in functions."
741591693c7b415e9869157c711fe11263c95d74eDavid Li    *
751591693c7b415e9869157c711fe11263c95d74eDavid Li    * Since built-in functions like ftransform() access built-in variables,
761591693c7b415e9869157c711fe11263c95d74eDavid Li    * it follows that those must be in the outer scope as well.
771591693c7b415e9869157c711fe11263c95d74eDavid Li    *
781591693c7b415e9869157c711fe11263c95d74eDavid Li    * We push scope here to create this nesting effect...but don't pop.
791591693c7b415e9869157c711fe11263c95d74eDavid Li    * This way, a shader's globals are still in the symbol table for use
801591693c7b415e9869157c711fe11263c95d74eDavid Li    * by the linker.
811591693c7b415e9869157c711fe11263c95d74eDavid Li    */
821591693c7b415e9869157c711fe11263c95d74eDavid Li   state->symbols->push_scope();
831591693c7b415e9869157c711fe11263c95d74eDavid Li
841591693c7b415e9869157c711fe11263c95d74eDavid Li   foreach_list_typed (ast_node, ast, link, & state->translation_unit)
851591693c7b415e9869157c711fe11263c95d74eDavid Li      ast->hir(instructions, state);
861591693c7b415e9869157c711fe11263c95d74eDavid Li}
871591693c7b415e9869157c711fe11263c95d74eDavid Li
881591693c7b415e9869157c711fe11263c95d74eDavid Li
891591693c7b415e9869157c711fe11263c95d74eDavid Li/**
901591693c7b415e9869157c711fe11263c95d74eDavid Li * If a conversion is available, convert one operand to a different type
911591693c7b415e9869157c711fe11263c95d74eDavid Li *
921591693c7b415e9869157c711fe11263c95d74eDavid Li * The \c from \c ir_rvalue is converted "in place".
931591693c7b415e9869157c711fe11263c95d74eDavid Li *
941591693c7b415e9869157c711fe11263c95d74eDavid Li * \param to     Type that the operand it to be converted to
951591693c7b415e9869157c711fe11263c95d74eDavid Li * \param from   Operand that is being converted
961591693c7b415e9869157c711fe11263c95d74eDavid Li * \param state  GLSL compiler state
971591693c7b415e9869157c711fe11263c95d74eDavid Li *
981591693c7b415e9869157c711fe11263c95d74eDavid Li * \return
991591693c7b415e9869157c711fe11263c95d74eDavid Li * If a conversion is possible (or unnecessary), \c true is returned.
1001591693c7b415e9869157c711fe11263c95d74eDavid Li * Otherwise \c false is returned.
1011591693c7b415e9869157c711fe11263c95d74eDavid Li */
1021591693c7b415e9869157c711fe11263c95d74eDavid Libool
1031591693c7b415e9869157c711fe11263c95d74eDavid Liapply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
1041591693c7b415e9869157c711fe11263c95d74eDavid Li			  struct _mesa_glsl_parse_state *state)
1051591693c7b415e9869157c711fe11263c95d74eDavid Li{
1061591693c7b415e9869157c711fe11263c95d74eDavid Li   void *ctx = state;
1071591693c7b415e9869157c711fe11263c95d74eDavid Li   if (to->base_type == from->type->base_type)
1081591693c7b415e9869157c711fe11263c95d74eDavid Li      return true;
1091591693c7b415e9869157c711fe11263c95d74eDavid Li
1101591693c7b415e9869157c711fe11263c95d74eDavid Li   /* This conversion was added in GLSL 1.20.  If the compilation mode is
1111591693c7b415e9869157c711fe11263c95d74eDavid Li    * GLSL 1.10, the conversion is skipped.
1121591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1131591693c7b415e9869157c711fe11263c95d74eDavid Li   if (state->language_version < 120)
1141591693c7b415e9869157c711fe11263c95d74eDavid Li      return false;
1151591693c7b415e9869157c711fe11263c95d74eDavid Li
1161591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
1171591693c7b415e9869157c711fe11263c95d74eDavid Li    *
1181591693c7b415e9869157c711fe11263c95d74eDavid Li    *    "There are no implicit array or structure conversions. For
1191591693c7b415e9869157c711fe11263c95d74eDavid Li    *    example, an array of int cannot be implicitly converted to an
1201591693c7b415e9869157c711fe11263c95d74eDavid Li    *    array of float. There are no implicit conversions between
1211591693c7b415e9869157c711fe11263c95d74eDavid Li    *    signed and unsigned integers."
1221591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1231591693c7b415e9869157c711fe11263c95d74eDavid Li   /* FINISHME: The above comment is partially a lie.  There is int/uint
1241591693c7b415e9869157c711fe11263c95d74eDavid Li    * FINISHME: conversion for immediate constants.
1251591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1261591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!to->is_float() || !from->type->is_numeric())
1271591693c7b415e9869157c711fe11263c95d74eDavid Li      return false;
1281591693c7b415e9869157c711fe11263c95d74eDavid Li
1291591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Convert to a floating point type with the same number of components
1301591693c7b415e9869157c711fe11263c95d74eDavid Li    * as the original type - i.e. int to float, not int to vec4.
1311591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1321591693c7b415e9869157c711fe11263c95d74eDavid Li   to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
1331591693c7b415e9869157c711fe11263c95d74eDavid Li			        from->type->matrix_columns);
1341591693c7b415e9869157c711fe11263c95d74eDavid Li
1351591693c7b415e9869157c711fe11263c95d74eDavid Li   switch (from->type->base_type) {
1361591693c7b415e9869157c711fe11263c95d74eDavid Li   case GLSL_TYPE_INT:
1371591693c7b415e9869157c711fe11263c95d74eDavid Li      from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
1381591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
1391591693c7b415e9869157c711fe11263c95d74eDavid Li   case GLSL_TYPE_UINT:
1401591693c7b415e9869157c711fe11263c95d74eDavid Li      from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
1411591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
1421591693c7b415e9869157c711fe11263c95d74eDavid Li   case GLSL_TYPE_BOOL:
1431591693c7b415e9869157c711fe11263c95d74eDavid Li      from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
1441591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
1451591693c7b415e9869157c711fe11263c95d74eDavid Li   default:
1461591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(0);
1471591693c7b415e9869157c711fe11263c95d74eDavid Li   }
1481591693c7b415e9869157c711fe11263c95d74eDavid Li
1491591693c7b415e9869157c711fe11263c95d74eDavid Li   return true;
1501591693c7b415e9869157c711fe11263c95d74eDavid Li}
1511591693c7b415e9869157c711fe11263c95d74eDavid Li
1521591693c7b415e9869157c711fe11263c95d74eDavid Li
1531591693c7b415e9869157c711fe11263c95d74eDavid Listatic const struct glsl_type *
1541591693c7b415e9869157c711fe11263c95d74eDavid Liarithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
1551591693c7b415e9869157c711fe11263c95d74eDavid Li		       bool multiply,
1561591693c7b415e9869157c711fe11263c95d74eDavid Li		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
1571591693c7b415e9869157c711fe11263c95d74eDavid Li{
1581591693c7b415e9869157c711fe11263c95d74eDavid Li   const glsl_type *type_a = value_a->type;
1591591693c7b415e9869157c711fe11263c95d74eDavid Li   const glsl_type *type_b = value_b->type;
1601591693c7b415e9869157c711fe11263c95d74eDavid Li
1611591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From GLSL 1.50 spec, page 56:
1621591693c7b415e9869157c711fe11263c95d74eDavid Li    *
1631591693c7b415e9869157c711fe11263c95d74eDavid Li    *    "The arithmetic binary operators add (+), subtract (-),
1641591693c7b415e9869157c711fe11263c95d74eDavid Li    *    multiply (*), and divide (/) operate on integer and
1651591693c7b415e9869157c711fe11263c95d74eDavid Li    *    floating-point scalars, vectors, and matrices."
1661591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1671591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!type_a->is_numeric() || !type_b->is_numeric()) {
1681591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state,
1691591693c7b415e9869157c711fe11263c95d74eDavid Li		       "Operands to arithmetic operators must be numeric");
1701591693c7b415e9869157c711fe11263c95d74eDavid Li      return glsl_type::error_type;
1711591693c7b415e9869157c711fe11263c95d74eDavid Li   }
1721591693c7b415e9869157c711fe11263c95d74eDavid Li
1731591693c7b415e9869157c711fe11263c95d74eDavid Li
1741591693c7b415e9869157c711fe11263c95d74eDavid Li   /*    "If one operand is floating-point based and the other is
1751591693c7b415e9869157c711fe11263c95d74eDavid Li    *    not, then the conversions from Section 4.1.10 "Implicit
1761591693c7b415e9869157c711fe11263c95d74eDavid Li    *    Conversions" are applied to the non-floating-point-based operand."
1771591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1781591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!apply_implicit_conversion(type_a, value_b, state)
1791591693c7b415e9869157c711fe11263c95d74eDavid Li       && !apply_implicit_conversion(type_b, value_a, state)) {
1801591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state,
1811591693c7b415e9869157c711fe11263c95d74eDavid Li		       "Could not implicitly convert operands to "
1821591693c7b415e9869157c711fe11263c95d74eDavid Li		       "arithmetic operator");
1831591693c7b415e9869157c711fe11263c95d74eDavid Li      return glsl_type::error_type;
1841591693c7b415e9869157c711fe11263c95d74eDavid Li   }
1851591693c7b415e9869157c711fe11263c95d74eDavid Li   type_a = value_a->type;
1861591693c7b415e9869157c711fe11263c95d74eDavid Li   type_b = value_b->type;
1871591693c7b415e9869157c711fe11263c95d74eDavid Li
1881591693c7b415e9869157c711fe11263c95d74eDavid Li   /*    "If the operands are integer types, they must both be signed or
1891591693c7b415e9869157c711fe11263c95d74eDavid Li    *    both be unsigned."
1901591693c7b415e9869157c711fe11263c95d74eDavid Li    *
1911591693c7b415e9869157c711fe11263c95d74eDavid Li    * From this rule and the preceeding conversion it can be inferred that
1921591693c7b415e9869157c711fe11263c95d74eDavid Li    * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
1931591693c7b415e9869157c711fe11263c95d74eDavid Li    * The is_numeric check above already filtered out the case where either
1941591693c7b415e9869157c711fe11263c95d74eDavid Li    * type is not one of these, so now the base types need only be tested for
1951591693c7b415e9869157c711fe11263c95d74eDavid Li    * equality.
1961591693c7b415e9869157c711fe11263c95d74eDavid Li    */
1971591693c7b415e9869157c711fe11263c95d74eDavid Li   if (type_a->base_type != type_b->base_type) {
1981591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state,
1991591693c7b415e9869157c711fe11263c95d74eDavid Li		       "base type mismatch for arithmetic operator");
2001591693c7b415e9869157c711fe11263c95d74eDavid Li      return glsl_type::error_type;
2011591693c7b415e9869157c711fe11263c95d74eDavid Li   }
2021591693c7b415e9869157c711fe11263c95d74eDavid Li
2031591693c7b415e9869157c711fe11263c95d74eDavid Li   /*    "All arithmetic binary operators result in the same fundamental type
2041591693c7b415e9869157c711fe11263c95d74eDavid Li    *    (signed integer, unsigned integer, or floating-point) as the
2051591693c7b415e9869157c711fe11263c95d74eDavid Li    *    operands they operate on, after operand type conversion. After
2061591693c7b415e9869157c711fe11263c95d74eDavid Li    *    conversion, the following cases are valid
2071591693c7b415e9869157c711fe11263c95d74eDavid Li    *
2081591693c7b415e9869157c711fe11263c95d74eDavid Li    *    * The two operands are scalars. In this case the operation is
2091591693c7b415e9869157c711fe11263c95d74eDavid Li    *      applied, resulting in a scalar."
2101591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2111591693c7b415e9869157c711fe11263c95d74eDavid Li   if (type_a->is_scalar() && type_b->is_scalar())
2121591693c7b415e9869157c711fe11263c95d74eDavid Li      return type_a;
2131591693c7b415e9869157c711fe11263c95d74eDavid Li
2141591693c7b415e9869157c711fe11263c95d74eDavid Li   /*   "* One operand is a scalar, and the other is a vector or matrix.
2151591693c7b415e9869157c711fe11263c95d74eDavid Li    *      In this case, the scalar operation is applied independently to each
2161591693c7b415e9869157c711fe11263c95d74eDavid Li    *      component of the vector or matrix, resulting in the same size
2171591693c7b415e9869157c711fe11263c95d74eDavid Li    *      vector or matrix."
2181591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2191591693c7b415e9869157c711fe11263c95d74eDavid Li   if (type_a->is_scalar()) {
2201591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!type_b->is_scalar())
2211591693c7b415e9869157c711fe11263c95d74eDavid Li	 return type_b;
2221591693c7b415e9869157c711fe11263c95d74eDavid Li   } else if (type_b->is_scalar()) {
2231591693c7b415e9869157c711fe11263c95d74eDavid Li      return type_a;
2241591693c7b415e9869157c711fe11263c95d74eDavid Li   }
2251591693c7b415e9869157c711fe11263c95d74eDavid Li
2261591693c7b415e9869157c711fe11263c95d74eDavid Li   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
2271591693c7b415e9869157c711fe11263c95d74eDavid Li    * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
2281591693c7b415e9869157c711fe11263c95d74eDavid Li    * handled.
2291591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2301591693c7b415e9869157c711fe11263c95d74eDavid Li   assert(!type_a->is_scalar());
2311591693c7b415e9869157c711fe11263c95d74eDavid Li   assert(!type_b->is_scalar());
2321591693c7b415e9869157c711fe11263c95d74eDavid Li
2331591693c7b415e9869157c711fe11263c95d74eDavid Li   /*   "* The two operands are vectors of the same size. In this case, the
2341591693c7b415e9869157c711fe11263c95d74eDavid Li    *      operation is done component-wise resulting in the same size
2351591693c7b415e9869157c711fe11263c95d74eDavid Li    *      vector."
2361591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2371591693c7b415e9869157c711fe11263c95d74eDavid Li   if (type_a->is_vector() && type_b->is_vector()) {
2381591693c7b415e9869157c711fe11263c95d74eDavid Li      if (type_a == type_b) {
2391591693c7b415e9869157c711fe11263c95d74eDavid Li	 return type_a;
2401591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
2411591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(loc, state,
2421591693c7b415e9869157c711fe11263c95d74eDavid Li			  "vector size mismatch for arithmetic operator");
2431591693c7b415e9869157c711fe11263c95d74eDavid Li	 return glsl_type::error_type;
2441591693c7b415e9869157c711fe11263c95d74eDavid Li      }
2451591693c7b415e9869157c711fe11263c95d74eDavid Li   }
2461591693c7b415e9869157c711fe11263c95d74eDavid Li
2471591693c7b415e9869157c711fe11263c95d74eDavid Li   /* All of the combinations of <scalar, scalar>, <vector, scalar>,
2481591693c7b415e9869157c711fe11263c95d74eDavid Li    * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
2491591693c7b415e9869157c711fe11263c95d74eDavid Li    * <vector, vector> have been handled.  At least one of the operands must
2501591693c7b415e9869157c711fe11263c95d74eDavid Li    * be matrix.  Further, since there are no integer matrix types, the base
2511591693c7b415e9869157c711fe11263c95d74eDavid Li    * type of both operands must be float.
2521591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2531591693c7b415e9869157c711fe11263c95d74eDavid Li   assert(type_a->is_matrix() || type_b->is_matrix());
2541591693c7b415e9869157c711fe11263c95d74eDavid Li   assert(type_a->base_type == GLSL_TYPE_FLOAT);
2551591693c7b415e9869157c711fe11263c95d74eDavid Li   assert(type_b->base_type == GLSL_TYPE_FLOAT);
2561591693c7b415e9869157c711fe11263c95d74eDavid Li
2571591693c7b415e9869157c711fe11263c95d74eDavid Li   /*   "* The operator is add (+), subtract (-), or divide (/), and the
2581591693c7b415e9869157c711fe11263c95d74eDavid Li    *      operands are matrices with the same number of rows and the same
2591591693c7b415e9869157c711fe11263c95d74eDavid Li    *      number of columns. In this case, the operation is done component-
2601591693c7b415e9869157c711fe11263c95d74eDavid Li    *      wise resulting in the same size matrix."
2611591693c7b415e9869157c711fe11263c95d74eDavid Li    *    * The operator is multiply (*), where both operands are matrices or
2621591693c7b415e9869157c711fe11263c95d74eDavid Li    *      one operand is a vector and the other a matrix. A right vector
2631591693c7b415e9869157c711fe11263c95d74eDavid Li    *      operand is treated as a column vector and a left vector operand as a
2641591693c7b415e9869157c711fe11263c95d74eDavid Li    *      row vector. In all these cases, it is required that the number of
2651591693c7b415e9869157c711fe11263c95d74eDavid Li    *      columns of the left operand is equal to the number of rows of the
2661591693c7b415e9869157c711fe11263c95d74eDavid Li    *      right operand. Then, the multiply (*) operation does a linear
2671591693c7b415e9869157c711fe11263c95d74eDavid Li    *      algebraic multiply, yielding an object that has the same number of
2681591693c7b415e9869157c711fe11263c95d74eDavid Li    *      rows as the left operand and the same number of columns as the right
2691591693c7b415e9869157c711fe11263c95d74eDavid Li    *      operand. Section 5.10 "Vector and Matrix Operations" explains in
2701591693c7b415e9869157c711fe11263c95d74eDavid Li    *      more detail how vectors and matrices are operated on."
2711591693c7b415e9869157c711fe11263c95d74eDavid Li    */
2721591693c7b415e9869157c711fe11263c95d74eDavid Li   if (! multiply) {
2731591693c7b415e9869157c711fe11263c95d74eDavid Li      if (type_a == type_b)
2741591693c7b415e9869157c711fe11263c95d74eDavid Li	 return type_a;
2751591693c7b415e9869157c711fe11263c95d74eDavid Li   } else {
2761591693c7b415e9869157c711fe11263c95d74eDavid Li      if (type_a->is_matrix() && type_b->is_matrix()) {
2771591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* Matrix multiply.  The columns of A must match the rows of B.  Given
2781591693c7b415e9869157c711fe11263c95d74eDavid Li	  * the other previously tested constraints, this means the vector type
2791591693c7b415e9869157c711fe11263c95d74eDavid Li	  * of a row from A must be the same as the vector type of a column from
2801591693c7b415e9869157c711fe11263c95d74eDavid Li	  * B.
2811591693c7b415e9869157c711fe11263c95d74eDavid Li	  */
2821591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (type_a->row_type() == type_b->column_type()) {
2831591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* The resulting matrix has the number of columns of matrix B and
2841591693c7b415e9869157c711fe11263c95d74eDavid Li	     * the number of rows of matrix A.  We get the row count of A by
2851591693c7b415e9869157c711fe11263c95d74eDavid Li	     * looking at the size of a vector that makes up a column.  The
2861591693c7b415e9869157c711fe11263c95d74eDavid Li	     * transpose (size of a row) is done for B.
2871591693c7b415e9869157c711fe11263c95d74eDavid Li	     */
2881591693c7b415e9869157c711fe11263c95d74eDavid Li	    const glsl_type *const type =
2891591693c7b415e9869157c711fe11263c95d74eDavid Li	       glsl_type::get_instance(type_a->base_type,
2901591693c7b415e9869157c711fe11263c95d74eDavid Li				       type_a->column_type()->vector_elements,
2911591693c7b415e9869157c711fe11263c95d74eDavid Li				       type_b->row_type()->vector_elements);
2921591693c7b415e9869157c711fe11263c95d74eDavid Li	    assert(type != glsl_type::error_type);
2931591693c7b415e9869157c711fe11263c95d74eDavid Li
2941591693c7b415e9869157c711fe11263c95d74eDavid Li	    return type;
2951591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
2961591693c7b415e9869157c711fe11263c95d74eDavid Li      } else if (type_a->is_matrix()) {
2971591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* A is a matrix and B is a column vector.  Columns of A must match
2981591693c7b415e9869157c711fe11263c95d74eDavid Li	  * rows of B.  Given the other previously tested constraints, this
2991591693c7b415e9869157c711fe11263c95d74eDavid Li	  * means the vector type of a row from A must be the same as the
3001591693c7b415e9869157c711fe11263c95d74eDavid Li	  * vector the type of B.
3011591693c7b415e9869157c711fe11263c95d74eDavid Li	  */
3021591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (type_a->row_type() == type_b) {
3031591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* The resulting vector has a number of elements equal to
3041591693c7b415e9869157c711fe11263c95d74eDavid Li	     * the number of rows of matrix A. */
3051591693c7b415e9869157c711fe11263c95d74eDavid Li	    const glsl_type *const type =
3061591693c7b415e9869157c711fe11263c95d74eDavid Li	       glsl_type::get_instance(type_a->base_type,
3071591693c7b415e9869157c711fe11263c95d74eDavid Li				       type_a->column_type()->vector_elements,
3081591693c7b415e9869157c711fe11263c95d74eDavid Li				       1);
3091591693c7b415e9869157c711fe11263c95d74eDavid Li	    assert(type != glsl_type::error_type);
3101591693c7b415e9869157c711fe11263c95d74eDavid Li
3111591693c7b415e9869157c711fe11263c95d74eDavid Li	    return type;
3121591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
3131591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
3141591693c7b415e9869157c711fe11263c95d74eDavid Li	 assert(type_b->is_matrix());
3151591693c7b415e9869157c711fe11263c95d74eDavid Li
3161591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* A is a row vector and B is a matrix.  Columns of A must match rows
3171591693c7b415e9869157c711fe11263c95d74eDavid Li	  * of B.  Given the other previously tested constraints, this means
3181591693c7b415e9869157c711fe11263c95d74eDavid Li	  * the type of A must be the same as the vector type of a column from
3191591693c7b415e9869157c711fe11263c95d74eDavid Li	  * B.
3201591693c7b415e9869157c711fe11263c95d74eDavid Li	  */
3211591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (type_a == type_b->column_type()) {
3221591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* The resulting vector has a number of elements equal to
3231591693c7b415e9869157c711fe11263c95d74eDavid Li	     * the number of columns of matrix B. */
3241591693c7b415e9869157c711fe11263c95d74eDavid Li	    const glsl_type *const type =
3251591693c7b415e9869157c711fe11263c95d74eDavid Li	       glsl_type::get_instance(type_a->base_type,
3261591693c7b415e9869157c711fe11263c95d74eDavid Li				       type_b->row_type()->vector_elements,
3271591693c7b415e9869157c711fe11263c95d74eDavid Li				       1);
3281591693c7b415e9869157c711fe11263c95d74eDavid Li	    assert(type != glsl_type::error_type);
3291591693c7b415e9869157c711fe11263c95d74eDavid Li
3301591693c7b415e9869157c711fe11263c95d74eDavid Li	    return type;
3311591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
3321591693c7b415e9869157c711fe11263c95d74eDavid Li      }
3331591693c7b415e9869157c711fe11263c95d74eDavid Li
3341591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
3351591693c7b415e9869157c711fe11263c95d74eDavid Li      return glsl_type::error_type;
3361591693c7b415e9869157c711fe11263c95d74eDavid Li   }
3371591693c7b415e9869157c711fe11263c95d74eDavid Li
3381591693c7b415e9869157c711fe11263c95d74eDavid Li
3391591693c7b415e9869157c711fe11263c95d74eDavid Li   /*    "All other cases are illegal."
3401591693c7b415e9869157c711fe11263c95d74eDavid Li    */
3411591693c7b415e9869157c711fe11263c95d74eDavid Li   _mesa_glsl_error(loc, state, "type mismatch");
3421591693c7b415e9869157c711fe11263c95d74eDavid Li   return glsl_type::error_type;
3431591693c7b415e9869157c711fe11263c95d74eDavid Li}
3441591693c7b415e9869157c711fe11263c95d74eDavid Li
3451591693c7b415e9869157c711fe11263c95d74eDavid Li
3461591693c7b415e9869157c711fe11263c95d74eDavid Listatic const struct glsl_type *
3471591693c7b415e9869157c711fe11263c95d74eDavid Liunary_arithmetic_result_type(const struct glsl_type *type,
3481591693c7b415e9869157c711fe11263c95d74eDavid Li			     struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
3491591693c7b415e9869157c711fe11263c95d74eDavid Li{
3501591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From GLSL 1.50 spec, page 57:
3511591693c7b415e9869157c711fe11263c95d74eDavid Li    *
3521591693c7b415e9869157c711fe11263c95d74eDavid Li    *    "The arithmetic unary operators negate (-), post- and pre-increment
3531591693c7b415e9869157c711fe11263c95d74eDavid Li    *     and decrement (-- and ++) operate on integer or floating-point
3541591693c7b415e9869157c711fe11263c95d74eDavid Li    *     values (including vectors and matrices). All unary operators work
3551591693c7b415e9869157c711fe11263c95d74eDavid Li    *     component-wise on their operands. These result with the same type
3561591693c7b415e9869157c711fe11263c95d74eDavid Li    *     they operated on."
3571591693c7b415e9869157c711fe11263c95d74eDavid Li    */
3581591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!type->is_numeric()) {
3591591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state,
3601591693c7b415e9869157c711fe11263c95d74eDavid Li		       "Operands to arithmetic operators must be numeric");
3611591693c7b415e9869157c711fe11263c95d74eDavid Li      return glsl_type::error_type;
3621591693c7b415e9869157c711fe11263c95d74eDavid Li   }
3631591693c7b415e9869157c711fe11263c95d74eDavid Li
3641591693c7b415e9869157c711fe11263c95d74eDavid Li   return type;
3651591693c7b415e9869157c711fe11263c95d74eDavid Li}
3661591693c7b415e9869157c711fe11263c95d74eDavid Li
3671591693c7b415e9869157c711fe11263c95d74eDavid Li/**
3681591693c7b415e9869157c711fe11263c95d74eDavid Li * \brief Return the result type of a bit-logic operation.
3691591693c7b415e9869157c711fe11263c95d74eDavid Li *
3701591693c7b415e9869157c711fe11263c95d74eDavid Li * If the given types to the bit-logic operator are invalid, return
3711591693c7b415e9869157c711fe11263c95d74eDavid Li * glsl_type::error_type.
3721591693c7b415e9869157c711fe11263c95d74eDavid Li *
3731591693c7b415e9869157c711fe11263c95d74eDavid Li * \param type_a Type of LHS of bit-logic op
3741591693c7b415e9869157c711fe11263c95d74eDavid Li * \param type_b Type of RHS of bit-logic op
3751591693c7b415e9869157c711fe11263c95d74eDavid Li */
3761591693c7b415e9869157c711fe11263c95d74eDavid Listatic const struct glsl_type *
3771591693c7b415e9869157c711fe11263c95d74eDavid Libit_logic_result_type(const struct glsl_type *type_a,
3781591693c7b415e9869157c711fe11263c95d74eDavid Li                      const struct glsl_type *type_b,
3791591693c7b415e9869157c711fe11263c95d74eDavid Li                      ast_operators op,
3801591693c7b415e9869157c711fe11263c95d74eDavid Li                      struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
3811591693c7b415e9869157c711fe11263c95d74eDavid Li{
3821591693c7b415e9869157c711fe11263c95d74eDavid Li    if (state->language_version < 130) {
3831591693c7b415e9869157c711fe11263c95d74eDavid Li       _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
3841591693c7b415e9869157c711fe11263c95d74eDavid Li       return glsl_type::error_type;
3851591693c7b415e9869157c711fe11263c95d74eDavid Li    }
3861591693c7b415e9869157c711fe11263c95d74eDavid Li
3871591693c7b415e9869157c711fe11263c95d74eDavid Li    /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
3881591693c7b415e9869157c711fe11263c95d74eDavid Li     *
3891591693c7b415e9869157c711fe11263c95d74eDavid Li     *     "The bitwise operators and (&), exclusive-or (^), and inclusive-or
3901591693c7b415e9869157c711fe11263c95d74eDavid Li     *     (|). The operands must be of type signed or unsigned integers or
3911591693c7b415e9869157c711fe11263c95d74eDavid Li     *     integer vectors."
3921591693c7b415e9869157c711fe11263c95d74eDavid Li     */
3931591693c7b415e9869157c711fe11263c95d74eDavid Li    if (!type_a->is_integer()) {
3941591693c7b415e9869157c711fe11263c95d74eDavid Li       _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
3951591693c7b415e9869157c711fe11263c95d74eDavid Li                         ast_expression::operator_string(op));
3961591693c7b415e9869157c711fe11263c95d74eDavid Li       return glsl_type::error_type;
3971591693c7b415e9869157c711fe11263c95d74eDavid Li    }
3981591693c7b415e9869157c711fe11263c95d74eDavid Li    if (!type_b->is_integer()) {
3991591693c7b415e9869157c711fe11263c95d74eDavid Li       _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
4001591693c7b415e9869157c711fe11263c95d74eDavid Li                        ast_expression::operator_string(op));
4011591693c7b415e9869157c711fe11263c95d74eDavid Li       return glsl_type::error_type;
4021591693c7b415e9869157c711fe11263c95d74eDavid Li    }
4031591693c7b415e9869157c711fe11263c95d74eDavid Li
4041591693c7b415e9869157c711fe11263c95d74eDavid Li    /*     "The fundamental types of the operands (signed or unsigned) must
4051591693c7b415e9869157c711fe11263c95d74eDavid Li     *     match,"
4061591693c7b415e9869157c711fe11263c95d74eDavid Li     */
4071591693c7b415e9869157c711fe11263c95d74eDavid Li    if (type_a->base_type != type_b->base_type) {
4081591693c7b415e9869157c711fe11263c95d74eDavid Li       _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
4091591693c7b415e9869157c711fe11263c95d74eDavid Li                        "base type", ast_expression::operator_string(op));
4101591693c7b415e9869157c711fe11263c95d74eDavid Li       return glsl_type::error_type;
4111591693c7b415e9869157c711fe11263c95d74eDavid Li    }
4121591693c7b415e9869157c711fe11263c95d74eDavid Li
4131591693c7b415e9869157c711fe11263c95d74eDavid Li    /*     "The operands cannot be vectors of differing size." */
4141591693c7b415e9869157c711fe11263c95d74eDavid Li    if (type_a->is_vector() &&
4151591693c7b415e9869157c711fe11263c95d74eDavid Li        type_b->is_vector() &&
4161591693c7b415e9869157c711fe11263c95d74eDavid Li        type_a->vector_elements != type_b->vector_elements) {
4171591693c7b415e9869157c711fe11263c95d74eDavid Li       _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
4181591693c7b415e9869157c711fe11263c95d74eDavid Li                        "different sizes", ast_expression::operator_string(op));
4191591693c7b415e9869157c711fe11263c95d74eDavid Li       return glsl_type::error_type;
4201591693c7b415e9869157c711fe11263c95d74eDavid Li    }
4211591693c7b415e9869157c711fe11263c95d74eDavid Li
4221591693c7b415e9869157c711fe11263c95d74eDavid Li    /*     "If one operand is a scalar and the other a vector, the scalar is
4231591693c7b415e9869157c711fe11263c95d74eDavid Li     *     applied component-wise to the vector, resulting in the same type as
4241591693c7b415e9869157c711fe11263c95d74eDavid Li     *     the vector. The fundamental types of the operands [...] will be the
4251591693c7b415e9869157c711fe11263c95d74eDavid Li     *     resulting fundamental type."
4261591693c7b415e9869157c711fe11263c95d74eDavid Li     */
4271591693c7b415e9869157c711fe11263c95d74eDavid Li    if (type_a->is_scalar())
4281591693c7b415e9869157c711fe11263c95d74eDavid Li        return type_b;
4291591693c7b415e9869157c711fe11263c95d74eDavid Li    else
4301591693c7b415e9869157c711fe11263c95d74eDavid Li        return type_a;
4311591693c7b415e9869157c711fe11263c95d74eDavid Li}
4321591693c7b415e9869157c711fe11263c95d74eDavid Li
4331591693c7b415e9869157c711fe11263c95d74eDavid Listatic const struct glsl_type *
4341591693c7b415e9869157c711fe11263c95d74eDavid Limodulus_result_type(const struct glsl_type *type_a,
4351591693c7b415e9869157c711fe11263c95d74eDavid Li		    const struct glsl_type *type_b,
4361591693c7b415e9869157c711fe11263c95d74eDavid Li		    struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
4371591693c7b415e9869157c711fe11263c95d74eDavid Li{
4381591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From GLSL 1.50 spec, page 56:
4391591693c7b415e9869157c711fe11263c95d74eDavid Li    *    "The operator modulus (%) operates on signed or unsigned integers or
4401591693c7b415e9869157c711fe11263c95d74eDavid Li    *    integer vectors. The operand types must both be signed or both be
4411591693c7b415e9869157c711fe11263c95d74eDavid Li    *    unsigned."
4421591693c7b415e9869157c711fe11263c95d74eDavid Li    */
4431591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!type_a->is_integer() || !type_b->is_integer()
4441591693c7b415e9869157c711fe11263c95d74eDavid Li       || (type_a->base_type != type_b->base_type)) {
4451591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state, "type mismatch");
4461591693c7b415e9869157c711fe11263c95d74eDavid Li      return glsl_type::error_type;
4471591693c7b415e9869157c711fe11263c95d74eDavid Li   }
4481591693c7b415e9869157c711fe11263c95d74eDavid Li
4491591693c7b415e9869157c711fe11263c95d74eDavid Li   /*    "The operands cannot be vectors of differing size. If one operand is
4501591693c7b415e9869157c711fe11263c95d74eDavid Li    *    a scalar and the other vector, then the scalar is applied component-
4511591693c7b415e9869157c711fe11263c95d74eDavid Li    *    wise to the vector, resulting in the same type as the vector. If both
4521591693c7b415e9869157c711fe11263c95d74eDavid Li    *    are vectors of the same size, the result is computed component-wise."
4531591693c7b415e9869157c711fe11263c95d74eDavid Li    */
4541591693c7b415e9869157c711fe11263c95d74eDavid Li   if (type_a->is_vector()) {
4551591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!type_b->is_vector()
4561591693c7b415e9869157c711fe11263c95d74eDavid Li	  || (type_a->vector_elements == type_b->vector_elements))
4571591693c7b415e9869157c711fe11263c95d74eDavid Li	 return type_a;
4581591693c7b415e9869157c711fe11263c95d74eDavid Li   } else
4591591693c7b415e9869157c711fe11263c95d74eDavid Li      return type_b;
4601591693c7b415e9869157c711fe11263c95d74eDavid Li
4611591693c7b415e9869157c711fe11263c95d74eDavid Li   /*    "The operator modulus (%) is not defined for any other data types
4621591693c7b415e9869157c711fe11263c95d74eDavid Li    *    (non-integer types)."
4631591693c7b415e9869157c711fe11263c95d74eDavid Li    */
4641591693c7b415e9869157c711fe11263c95d74eDavid Li   _mesa_glsl_error(loc, state, "type mismatch");
4651591693c7b415e9869157c711fe11263c95d74eDavid Li   return glsl_type::error_type;
4661591693c7b415e9869157c711fe11263c95d74eDavid Li}
4671591693c7b415e9869157c711fe11263c95d74eDavid Li
4681591693c7b415e9869157c711fe11263c95d74eDavid Li
4691591693c7b415e9869157c711fe11263c95d74eDavid Listatic const struct glsl_type *
4701591693c7b415e9869157c711fe11263c95d74eDavid Lirelational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
4711591693c7b415e9869157c711fe11263c95d74eDavid Li		       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
4721591693c7b415e9869157c711fe11263c95d74eDavid Li{
4731591693c7b415e9869157c711fe11263c95d74eDavid Li   const glsl_type *type_a = value_a->type;
4741591693c7b415e9869157c711fe11263c95d74eDavid Li   const glsl_type *type_b = value_b->type;
4751591693c7b415e9869157c711fe11263c95d74eDavid Li
4761591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From GLSL 1.50 spec, page 56:
4771591693c7b415e9869157c711fe11263c95d74eDavid Li    *    "The relational operators greater than (>), less than (<), greater
4781591693c7b415e9869157c711fe11263c95d74eDavid Li    *    than or equal (>=), and less than or equal (<=) operate only on
4791591693c7b415e9869157c711fe11263c95d74eDavid Li    *    scalar integer and scalar floating-point expressions."
4801591693c7b415e9869157c711fe11263c95d74eDavid Li    */
4811591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!type_a->is_numeric()
4821591693c7b415e9869157c711fe11263c95d74eDavid Li       || !type_b->is_numeric()
4831591693c7b415e9869157c711fe11263c95d74eDavid Li       || !type_a->is_scalar()
4841591693c7b415e9869157c711fe11263c95d74eDavid Li       || !type_b->is_scalar()) {
4851591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state,
4861591693c7b415e9869157c711fe11263c95d74eDavid Li		       "Operands to relational operators must be scalar and "
4871591693c7b415e9869157c711fe11263c95d74eDavid Li		       "numeric");
4881591693c7b415e9869157c711fe11263c95d74eDavid Li      return glsl_type::error_type;
4891591693c7b415e9869157c711fe11263c95d74eDavid Li   }
4901591693c7b415e9869157c711fe11263c95d74eDavid Li
4911591693c7b415e9869157c711fe11263c95d74eDavid Li   /*    "Either the operands' types must match, or the conversions from
4921591693c7b415e9869157c711fe11263c95d74eDavid Li    *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
4931591693c7b415e9869157c711fe11263c95d74eDavid Li    *    operand, after which the types must match."
4941591693c7b415e9869157c711fe11263c95d74eDavid Li    */
4951591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!apply_implicit_conversion(type_a, value_b, state)
4961591693c7b415e9869157c711fe11263c95d74eDavid Li       && !apply_implicit_conversion(type_b, value_a, state)) {
4971591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state,
4981591693c7b415e9869157c711fe11263c95d74eDavid Li		       "Could not implicitly convert operands to "
4991591693c7b415e9869157c711fe11263c95d74eDavid Li		       "relational operator");
5001591693c7b415e9869157c711fe11263c95d74eDavid Li      return glsl_type::error_type;
5011591693c7b415e9869157c711fe11263c95d74eDavid Li   }
5021591693c7b415e9869157c711fe11263c95d74eDavid Li   type_a = value_a->type;
5031591693c7b415e9869157c711fe11263c95d74eDavid Li   type_b = value_b->type;
5041591693c7b415e9869157c711fe11263c95d74eDavid Li
5051591693c7b415e9869157c711fe11263c95d74eDavid Li   if (type_a->base_type != type_b->base_type) {
5061591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state, "base type mismatch");
5071591693c7b415e9869157c711fe11263c95d74eDavid Li      return glsl_type::error_type;
5081591693c7b415e9869157c711fe11263c95d74eDavid Li   }
5091591693c7b415e9869157c711fe11263c95d74eDavid Li
5101591693c7b415e9869157c711fe11263c95d74eDavid Li   /*    "The result is scalar Boolean."
5111591693c7b415e9869157c711fe11263c95d74eDavid Li    */
5121591693c7b415e9869157c711fe11263c95d74eDavid Li   return glsl_type::bool_type;
5131591693c7b415e9869157c711fe11263c95d74eDavid Li}
5141591693c7b415e9869157c711fe11263c95d74eDavid Li
5151591693c7b415e9869157c711fe11263c95d74eDavid Li/**
5161591693c7b415e9869157c711fe11263c95d74eDavid Li * \brief Return the result type of a bit-shift operation.
5171591693c7b415e9869157c711fe11263c95d74eDavid Li *
5181591693c7b415e9869157c711fe11263c95d74eDavid Li * If the given types to the bit-shift operator are invalid, return
5191591693c7b415e9869157c711fe11263c95d74eDavid Li * glsl_type::error_type.
5201591693c7b415e9869157c711fe11263c95d74eDavid Li *
5211591693c7b415e9869157c711fe11263c95d74eDavid Li * \param type_a Type of LHS of bit-shift op
5221591693c7b415e9869157c711fe11263c95d74eDavid Li * \param type_b Type of RHS of bit-shift op
5231591693c7b415e9869157c711fe11263c95d74eDavid Li */
5241591693c7b415e9869157c711fe11263c95d74eDavid Listatic const struct glsl_type *
5251591693c7b415e9869157c711fe11263c95d74eDavid Lishift_result_type(const struct glsl_type *type_a,
5261591693c7b415e9869157c711fe11263c95d74eDavid Li                  const struct glsl_type *type_b,
5271591693c7b415e9869157c711fe11263c95d74eDavid Li                  ast_operators op,
5281591693c7b415e9869157c711fe11263c95d74eDavid Li                  struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
5291591693c7b415e9869157c711fe11263c95d74eDavid Li{
5301591693c7b415e9869157c711fe11263c95d74eDavid Li   if (state->language_version < 130) {
5311591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
5321591693c7b415e9869157c711fe11263c95d74eDavid Li      return glsl_type::error_type;
5331591693c7b415e9869157c711fe11263c95d74eDavid Li   }
5341591693c7b415e9869157c711fe11263c95d74eDavid Li
5351591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
5361591693c7b415e9869157c711fe11263c95d74eDavid Li    *
5371591693c7b415e9869157c711fe11263c95d74eDavid Li    *     "The shift operators (<<) and (>>). For both operators, the operands
5381591693c7b415e9869157c711fe11263c95d74eDavid Li    *     must be signed or unsigned integers or integer vectors. One operand
5391591693c7b415e9869157c711fe11263c95d74eDavid Li    *     can be signed while the other is unsigned."
5401591693c7b415e9869157c711fe11263c95d74eDavid Li    */
5411591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!type_a->is_integer()) {
5421591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
5431591693c7b415e9869157c711fe11263c95d74eDavid Li              "integer vector", ast_expression::operator_string(op));
5441591693c7b415e9869157c711fe11263c95d74eDavid Li     return glsl_type::error_type;
5451591693c7b415e9869157c711fe11263c95d74eDavid Li
5461591693c7b415e9869157c711fe11263c95d74eDavid Li   }
5471591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!type_b->is_integer()) {
5481591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
5491591693c7b415e9869157c711fe11263c95d74eDavid Li              "integer vector", ast_expression::operator_string(op));
5501591693c7b415e9869157c711fe11263c95d74eDavid Li     return glsl_type::error_type;
5511591693c7b415e9869157c711fe11263c95d74eDavid Li   }
5521591693c7b415e9869157c711fe11263c95d74eDavid Li
5531591693c7b415e9869157c711fe11263c95d74eDavid Li   /*     "If the first operand is a scalar, the second operand has to be
5541591693c7b415e9869157c711fe11263c95d74eDavid Li    *     a scalar as well."
5551591693c7b415e9869157c711fe11263c95d74eDavid Li    */
5561591693c7b415e9869157c711fe11263c95d74eDavid Li   if (type_a->is_scalar() && !type_b->is_scalar()) {
5571591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the "
5581591693c7b415e9869157c711fe11263c95d74eDavid Li              "second must be scalar as well",
5591591693c7b415e9869157c711fe11263c95d74eDavid Li              ast_expression::operator_string(op));
5601591693c7b415e9869157c711fe11263c95d74eDavid Li     return glsl_type::error_type;
5611591693c7b415e9869157c711fe11263c95d74eDavid Li   }
5621591693c7b415e9869157c711fe11263c95d74eDavid Li
5631591693c7b415e9869157c711fe11263c95d74eDavid Li   /* If both operands are vectors, check that they have same number of
5641591693c7b415e9869157c711fe11263c95d74eDavid Li    * elements.
5651591693c7b415e9869157c711fe11263c95d74eDavid Li    */
5661591693c7b415e9869157c711fe11263c95d74eDavid Li   if (type_a->is_vector() &&
5671591693c7b415e9869157c711fe11263c95d74eDavid Li      type_b->is_vector() &&
5681591693c7b415e9869157c711fe11263c95d74eDavid Li      type_a->vector_elements != type_b->vector_elements) {
5691591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state, "Vector operands to operator %s must "
5701591693c7b415e9869157c711fe11263c95d74eDavid Li              "have same number of elements",
5711591693c7b415e9869157c711fe11263c95d74eDavid Li              ast_expression::operator_string(op));
5721591693c7b415e9869157c711fe11263c95d74eDavid Li     return glsl_type::error_type;
5731591693c7b415e9869157c711fe11263c95d74eDavid Li   }
5741591693c7b415e9869157c711fe11263c95d74eDavid Li
5751591693c7b415e9869157c711fe11263c95d74eDavid Li   /*     "In all cases, the resulting type will be the same type as the left
5761591693c7b415e9869157c711fe11263c95d74eDavid Li    *     operand."
5771591693c7b415e9869157c711fe11263c95d74eDavid Li    */
5781591693c7b415e9869157c711fe11263c95d74eDavid Li   return type_a;
5791591693c7b415e9869157c711fe11263c95d74eDavid Li}
5801591693c7b415e9869157c711fe11263c95d74eDavid Li
5811591693c7b415e9869157c711fe11263c95d74eDavid Li/**
5821591693c7b415e9869157c711fe11263c95d74eDavid Li * Validates that a value can be assigned to a location with a specified type
5831591693c7b415e9869157c711fe11263c95d74eDavid Li *
5841591693c7b415e9869157c711fe11263c95d74eDavid Li * Validates that \c rhs can be assigned to some location.  If the types are
5851591693c7b415e9869157c711fe11263c95d74eDavid Li * not an exact match but an automatic conversion is possible, \c rhs will be
5861591693c7b415e9869157c711fe11263c95d74eDavid Li * converted.
5871591693c7b415e9869157c711fe11263c95d74eDavid Li *
5881591693c7b415e9869157c711fe11263c95d74eDavid Li * \return
5891591693c7b415e9869157c711fe11263c95d74eDavid Li * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
5901591693c7b415e9869157c711fe11263c95d74eDavid Li * Otherwise the actual RHS to be assigned will be returned.  This may be
5911591693c7b415e9869157c711fe11263c95d74eDavid Li * \c rhs, or it may be \c rhs after some type conversion.
5921591693c7b415e9869157c711fe11263c95d74eDavid Li *
5931591693c7b415e9869157c711fe11263c95d74eDavid Li * \note
5941591693c7b415e9869157c711fe11263c95d74eDavid Li * In addition to being used for assignments, this function is used to
5951591693c7b415e9869157c711fe11263c95d74eDavid Li * type-check return values.
5961591693c7b415e9869157c711fe11263c95d74eDavid Li */
5971591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
5981591693c7b415e9869157c711fe11263c95d74eDavid Livalidate_assignment(struct _mesa_glsl_parse_state *state,
5991591693c7b415e9869157c711fe11263c95d74eDavid Li		    const glsl_type *lhs_type, ir_rvalue *rhs)
6001591693c7b415e9869157c711fe11263c95d74eDavid Li{
6011591693c7b415e9869157c711fe11263c95d74eDavid Li   const glsl_type *rhs_type = rhs->type;
6021591693c7b415e9869157c711fe11263c95d74eDavid Li
6031591693c7b415e9869157c711fe11263c95d74eDavid Li   /* If there is already some error in the RHS, just return it.  Anything
6041591693c7b415e9869157c711fe11263c95d74eDavid Li    * else will lead to an avalanche of error message back to the user.
6051591693c7b415e9869157c711fe11263c95d74eDavid Li    */
6061591693c7b415e9869157c711fe11263c95d74eDavid Li   if (rhs_type->is_error())
6071591693c7b415e9869157c711fe11263c95d74eDavid Li      return rhs;
6081591693c7b415e9869157c711fe11263c95d74eDavid Li
6091591693c7b415e9869157c711fe11263c95d74eDavid Li   /* If the types are identical, the assignment can trivially proceed.
6101591693c7b415e9869157c711fe11263c95d74eDavid Li    */
6111591693c7b415e9869157c711fe11263c95d74eDavid Li   if (rhs_type == lhs_type)
6121591693c7b415e9869157c711fe11263c95d74eDavid Li      return rhs;
6131591693c7b415e9869157c711fe11263c95d74eDavid Li
6141591693c7b415e9869157c711fe11263c95d74eDavid Li   /* If the array element types are the same and the size of the LHS is zero,
6151591693c7b415e9869157c711fe11263c95d74eDavid Li    * the assignment is okay.
6161591693c7b415e9869157c711fe11263c95d74eDavid Li    *
6171591693c7b415e9869157c711fe11263c95d74eDavid Li    * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
6181591693c7b415e9869157c711fe11263c95d74eDavid Li    * is handled by ir_dereference::is_lvalue.
6191591693c7b415e9869157c711fe11263c95d74eDavid Li    */
6201591693c7b415e9869157c711fe11263c95d74eDavid Li   if (lhs_type->is_array() && rhs->type->is_array()
6211591693c7b415e9869157c711fe11263c95d74eDavid Li       && (lhs_type->element_type() == rhs->type->element_type())
6221591693c7b415e9869157c711fe11263c95d74eDavid Li       && (lhs_type->array_size() == 0)) {
6231591693c7b415e9869157c711fe11263c95d74eDavid Li      return rhs;
6241591693c7b415e9869157c711fe11263c95d74eDavid Li   }
6251591693c7b415e9869157c711fe11263c95d74eDavid Li
6261591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Check for implicit conversion in GLSL 1.20 */
6271591693c7b415e9869157c711fe11263c95d74eDavid Li   if (apply_implicit_conversion(lhs_type, rhs, state)) {
6281591693c7b415e9869157c711fe11263c95d74eDavid Li      rhs_type = rhs->type;
6291591693c7b415e9869157c711fe11263c95d74eDavid Li      if (rhs_type == lhs_type)
6301591693c7b415e9869157c711fe11263c95d74eDavid Li	 return rhs;
6311591693c7b415e9869157c711fe11263c95d74eDavid Li   }
6321591693c7b415e9869157c711fe11263c95d74eDavid Li
6331591693c7b415e9869157c711fe11263c95d74eDavid Li   return NULL;
6341591693c7b415e9869157c711fe11263c95d74eDavid Li}
6351591693c7b415e9869157c711fe11263c95d74eDavid Li
6361591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
6371591693c7b415e9869157c711fe11263c95d74eDavid Lido_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
6381591693c7b415e9869157c711fe11263c95d74eDavid Li	      ir_rvalue *lhs, ir_rvalue *rhs,
6391591693c7b415e9869157c711fe11263c95d74eDavid Li	      YYLTYPE lhs_loc)
6401591693c7b415e9869157c711fe11263c95d74eDavid Li{
6411591693c7b415e9869157c711fe11263c95d74eDavid Li   void *ctx = state;
6421591693c7b415e9869157c711fe11263c95d74eDavid Li   bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
6431591693c7b415e9869157c711fe11263c95d74eDavid Li
6441591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!error_emitted) {
6451591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!lhs->is_lvalue()) {
6461591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
6471591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
6481591693c7b415e9869157c711fe11263c95d74eDavid Li      }
6491591693c7b415e9869157c711fe11263c95d74eDavid Li
6501591693c7b415e9869157c711fe11263c95d74eDavid Li      if (state->es_shader && lhs->type->is_array()) {
6511591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not "
6521591693c7b415e9869157c711fe11263c95d74eDavid Li			  "allowed in GLSL ES 1.00.");
6531591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
6541591693c7b415e9869157c711fe11263c95d74eDavid Li      }
6551591693c7b415e9869157c711fe11263c95d74eDavid Li   }
6561591693c7b415e9869157c711fe11263c95d74eDavid Li
6571591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs);
6581591693c7b415e9869157c711fe11263c95d74eDavid Li   if (new_rhs == NULL) {
6591591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(& lhs_loc, state, "type mismatch");
6601591693c7b415e9869157c711fe11263c95d74eDavid Li   } else {
6611591693c7b415e9869157c711fe11263c95d74eDavid Li      rhs = new_rhs;
6621591693c7b415e9869157c711fe11263c95d74eDavid Li
6631591693c7b415e9869157c711fe11263c95d74eDavid Li      /* If the LHS array was not declared with a size, it takes it size from
6641591693c7b415e9869157c711fe11263c95d74eDavid Li       * the RHS.  If the LHS is an l-value and a whole array, it must be a
6651591693c7b415e9869157c711fe11263c95d74eDavid Li       * dereference of a variable.  Any other case would require that the LHS
6661591693c7b415e9869157c711fe11263c95d74eDavid Li       * is either not an l-value or not a whole array.
6671591693c7b415e9869157c711fe11263c95d74eDavid Li       */
6681591693c7b415e9869157c711fe11263c95d74eDavid Li      if (lhs->type->array_size() == 0) {
6691591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_dereference *const d = lhs->as_dereference();
6701591693c7b415e9869157c711fe11263c95d74eDavid Li
6711591693c7b415e9869157c711fe11263c95d74eDavid Li	 assert(d != NULL);
6721591693c7b415e9869157c711fe11263c95d74eDavid Li
6731591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_variable *const var = d->variable_referenced();
6741591693c7b415e9869157c711fe11263c95d74eDavid Li
6751591693c7b415e9869157c711fe11263c95d74eDavid Li	 assert(var != NULL);
6761591693c7b415e9869157c711fe11263c95d74eDavid Li
6771591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (var->max_array_access >= unsigned(rhs->type->array_size())) {
6781591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* FINISHME: This should actually log the location of the RHS. */
6791591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
6801591693c7b415e9869157c711fe11263c95d74eDavid Li			     "previous access",
6811591693c7b415e9869157c711fe11263c95d74eDavid Li			     var->max_array_access);
6821591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
6831591693c7b415e9869157c711fe11263c95d74eDavid Li
6841591693c7b415e9869157c711fe11263c95d74eDavid Li	 var->type = glsl_type::get_array_instance(lhs->type->element_type(),
6851591693c7b415e9869157c711fe11263c95d74eDavid Li						   rhs->type->array_size());
6861591693c7b415e9869157c711fe11263c95d74eDavid Li	 d->type = var->type;
6871591693c7b415e9869157c711fe11263c95d74eDavid Li      }
6881591693c7b415e9869157c711fe11263c95d74eDavid Li   }
6891591693c7b415e9869157c711fe11263c95d74eDavid Li
6901591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
6911591693c7b415e9869157c711fe11263c95d74eDavid Li    * but not post_inc) need the converted assigned value as an rvalue
6921591693c7b415e9869157c711fe11263c95d74eDavid Li    * to handle things like:
6931591693c7b415e9869157c711fe11263c95d74eDavid Li    *
6941591693c7b415e9869157c711fe11263c95d74eDavid Li    * i = j += 1;
6951591693c7b415e9869157c711fe11263c95d74eDavid Li    *
6961591693c7b415e9869157c711fe11263c95d74eDavid Li    * So we always just store the computed value being assigned to a
6971591693c7b415e9869157c711fe11263c95d74eDavid Li    * temporary and return a deref of that temporary.  If the rvalue
6981591693c7b415e9869157c711fe11263c95d74eDavid Li    * ends up not being used, the temp will get copy-propagated out.
6991591693c7b415e9869157c711fe11263c95d74eDavid Li    */
7001591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
7011591693c7b415e9869157c711fe11263c95d74eDavid Li					   ir_var_temporary);
7021591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
7031591693c7b415e9869157c711fe11263c95d74eDavid Li   instructions->push_tail(var);
7041591693c7b415e9869157c711fe11263c95d74eDavid Li   instructions->push_tail(new(ctx) ir_assignment(deref_var,
7051591693c7b415e9869157c711fe11263c95d74eDavid Li						  rhs,
7061591693c7b415e9869157c711fe11263c95d74eDavid Li						  NULL));
7071591693c7b415e9869157c711fe11263c95d74eDavid Li   deref_var = new(ctx) ir_dereference_variable(var);
7081591693c7b415e9869157c711fe11263c95d74eDavid Li
7091591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!error_emitted)
7101591693c7b415e9869157c711fe11263c95d74eDavid Li      instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
7111591693c7b415e9869157c711fe11263c95d74eDavid Li
7121591693c7b415e9869157c711fe11263c95d74eDavid Li   return new(ctx) ir_dereference_variable(var);
7131591693c7b415e9869157c711fe11263c95d74eDavid Li}
7141591693c7b415e9869157c711fe11263c95d74eDavid Li
7151591693c7b415e9869157c711fe11263c95d74eDavid Listatic ir_rvalue *
7161591693c7b415e9869157c711fe11263c95d74eDavid Liget_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
7171591693c7b415e9869157c711fe11263c95d74eDavid Li{
718d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li   void *ctx = hieralloc_parent(lvalue);
7191591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_variable *var;
7201591693c7b415e9869157c711fe11263c95d74eDavid Li
7211591693c7b415e9869157c711fe11263c95d74eDavid Li   var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
7221591693c7b415e9869157c711fe11263c95d74eDavid Li			      ir_var_temporary);
7231591693c7b415e9869157c711fe11263c95d74eDavid Li   instructions->push_tail(var);
7241591693c7b415e9869157c711fe11263c95d74eDavid Li   var->mode = ir_var_auto;
7251591693c7b415e9869157c711fe11263c95d74eDavid Li
7261591693c7b415e9869157c711fe11263c95d74eDavid Li   instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
7271591693c7b415e9869157c711fe11263c95d74eDavid Li						  lvalue, NULL));
7281591693c7b415e9869157c711fe11263c95d74eDavid Li
7291591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Once we've created this temporary, mark it read only so it's no
7301591693c7b415e9869157c711fe11263c95d74eDavid Li    * longer considered an lvalue.
7311591693c7b415e9869157c711fe11263c95d74eDavid Li    */
7321591693c7b415e9869157c711fe11263c95d74eDavid Li   var->read_only = true;
7331591693c7b415e9869157c711fe11263c95d74eDavid Li
7341591693c7b415e9869157c711fe11263c95d74eDavid Li   return new(ctx) ir_dereference_variable(var);
7351591693c7b415e9869157c711fe11263c95d74eDavid Li}
7361591693c7b415e9869157c711fe11263c95d74eDavid Li
7371591693c7b415e9869157c711fe11263c95d74eDavid Li
7381591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
7391591693c7b415e9869157c711fe11263c95d74eDavid Liast_node::hir(exec_list *instructions,
7401591693c7b415e9869157c711fe11263c95d74eDavid Li	      struct _mesa_glsl_parse_state *state)
7411591693c7b415e9869157c711fe11263c95d74eDavid Li{
7421591693c7b415e9869157c711fe11263c95d74eDavid Li   (void) instructions;
7431591693c7b415e9869157c711fe11263c95d74eDavid Li   (void) state;
7441591693c7b415e9869157c711fe11263c95d74eDavid Li
7451591693c7b415e9869157c711fe11263c95d74eDavid Li   return NULL;
7461591693c7b415e9869157c711fe11263c95d74eDavid Li}
7471591693c7b415e9869157c711fe11263c95d74eDavid Li
7481591693c7b415e9869157c711fe11263c95d74eDavid Listatic void
7491591693c7b415e9869157c711fe11263c95d74eDavid Limark_whole_array_access(ir_rvalue *access)
7501591693c7b415e9869157c711fe11263c95d74eDavid Li{
7511591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_dereference_variable *deref = access->as_dereference_variable();
7521591693c7b415e9869157c711fe11263c95d74eDavid Li
7531591693c7b415e9869157c711fe11263c95d74eDavid Li   if (deref) {
7541591693c7b415e9869157c711fe11263c95d74eDavid Li      deref->var->max_array_access = deref->type->length - 1;
7551591693c7b415e9869157c711fe11263c95d74eDavid Li   }
7561591693c7b415e9869157c711fe11263c95d74eDavid Li}
7571591693c7b415e9869157c711fe11263c95d74eDavid Li
7581591693c7b415e9869157c711fe11263c95d74eDavid Listatic ir_rvalue *
7591591693c7b415e9869157c711fe11263c95d74eDavid Lido_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
7601591693c7b415e9869157c711fe11263c95d74eDavid Li{
7611591693c7b415e9869157c711fe11263c95d74eDavid Li   int join_op;
7621591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_rvalue *cmp = NULL;
7631591693c7b415e9869157c711fe11263c95d74eDavid Li
7641591693c7b415e9869157c711fe11263c95d74eDavid Li   if (operation == ir_binop_all_equal)
7651591693c7b415e9869157c711fe11263c95d74eDavid Li      join_op = ir_binop_logic_and;
7661591693c7b415e9869157c711fe11263c95d74eDavid Li   else
7671591693c7b415e9869157c711fe11263c95d74eDavid Li      join_op = ir_binop_logic_or;
7681591693c7b415e9869157c711fe11263c95d74eDavid Li
7691591693c7b415e9869157c711fe11263c95d74eDavid Li   switch (op0->type->base_type) {
7701591693c7b415e9869157c711fe11263c95d74eDavid Li   case GLSL_TYPE_FLOAT:
7711591693c7b415e9869157c711fe11263c95d74eDavid Li   case GLSL_TYPE_UINT:
7721591693c7b415e9869157c711fe11263c95d74eDavid Li   case GLSL_TYPE_INT:
7731591693c7b415e9869157c711fe11263c95d74eDavid Li   case GLSL_TYPE_BOOL:
7741591693c7b415e9869157c711fe11263c95d74eDavid Li      return new(mem_ctx) ir_expression(operation, op0, op1);
7751591693c7b415e9869157c711fe11263c95d74eDavid Li
7761591693c7b415e9869157c711fe11263c95d74eDavid Li   case GLSL_TYPE_ARRAY: {
7771591693c7b415e9869157c711fe11263c95d74eDavid Li      for (unsigned int i = 0; i < op0->type->length; i++) {
7781591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_rvalue *e0, *e1, *result;
7791591693c7b415e9869157c711fe11263c95d74eDavid Li
7801591693c7b415e9869157c711fe11263c95d74eDavid Li	 e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
7811591693c7b415e9869157c711fe11263c95d74eDavid Li						new(mem_ctx) ir_constant(i));
7821591693c7b415e9869157c711fe11263c95d74eDavid Li	 e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
7831591693c7b415e9869157c711fe11263c95d74eDavid Li						new(mem_ctx) ir_constant(i));
7841591693c7b415e9869157c711fe11263c95d74eDavid Li	 result = do_comparison(mem_ctx, operation, e0, e1);
7851591693c7b415e9869157c711fe11263c95d74eDavid Li
7861591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (cmp) {
7871591693c7b415e9869157c711fe11263c95d74eDavid Li	    cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
7881591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else {
7891591693c7b415e9869157c711fe11263c95d74eDavid Li	    cmp = result;
7901591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
7911591693c7b415e9869157c711fe11263c95d74eDavid Li      }
7921591693c7b415e9869157c711fe11263c95d74eDavid Li
7931591693c7b415e9869157c711fe11263c95d74eDavid Li      mark_whole_array_access(op0);
7941591693c7b415e9869157c711fe11263c95d74eDavid Li      mark_whole_array_access(op1);
7951591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
7961591693c7b415e9869157c711fe11263c95d74eDavid Li   }
7971591693c7b415e9869157c711fe11263c95d74eDavid Li
7981591693c7b415e9869157c711fe11263c95d74eDavid Li   case GLSL_TYPE_STRUCT: {
7991591693c7b415e9869157c711fe11263c95d74eDavid Li      for (unsigned int i = 0; i < op0->type->length; i++) {
8001591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_rvalue *e0, *e1, *result;
8011591693c7b415e9869157c711fe11263c95d74eDavid Li	 const char *field_name = op0->type->fields.structure[i].name;
8021591693c7b415e9869157c711fe11263c95d74eDavid Li
8031591693c7b415e9869157c711fe11263c95d74eDavid Li	 e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
8041591693c7b415e9869157c711fe11263c95d74eDavid Li						 field_name);
8051591693c7b415e9869157c711fe11263c95d74eDavid Li	 e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
8061591693c7b415e9869157c711fe11263c95d74eDavid Li						 field_name);
8071591693c7b415e9869157c711fe11263c95d74eDavid Li	 result = do_comparison(mem_ctx, operation, e0, e1);
8081591693c7b415e9869157c711fe11263c95d74eDavid Li
8091591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (cmp) {
8101591693c7b415e9869157c711fe11263c95d74eDavid Li	    cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
8111591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else {
8121591693c7b415e9869157c711fe11263c95d74eDavid Li	    cmp = result;
8131591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
8141591693c7b415e9869157c711fe11263c95d74eDavid Li      }
8151591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
8161591693c7b415e9869157c711fe11263c95d74eDavid Li   }
8171591693c7b415e9869157c711fe11263c95d74eDavid Li
8181591693c7b415e9869157c711fe11263c95d74eDavid Li   case GLSL_TYPE_ERROR:
8191591693c7b415e9869157c711fe11263c95d74eDavid Li   case GLSL_TYPE_VOID:
8201591693c7b415e9869157c711fe11263c95d74eDavid Li   case GLSL_TYPE_SAMPLER:
8211591693c7b415e9869157c711fe11263c95d74eDavid Li      /* I assume a comparison of a struct containing a sampler just
8221591693c7b415e9869157c711fe11263c95d74eDavid Li       * ignores the sampler present in the type.
8231591693c7b415e9869157c711fe11263c95d74eDavid Li       */
8241591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
8251591693c7b415e9869157c711fe11263c95d74eDavid Li
8261591693c7b415e9869157c711fe11263c95d74eDavid Li   default:
8271591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(!"Should not get here.");
8281591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
8291591693c7b415e9869157c711fe11263c95d74eDavid Li   }
8301591693c7b415e9869157c711fe11263c95d74eDavid Li
8311591693c7b415e9869157c711fe11263c95d74eDavid Li   if (cmp == NULL)
8321591693c7b415e9869157c711fe11263c95d74eDavid Li      cmp = new(mem_ctx) ir_constant(true);
8331591693c7b415e9869157c711fe11263c95d74eDavid Li
8341591693c7b415e9869157c711fe11263c95d74eDavid Li   return cmp;
8351591693c7b415e9869157c711fe11263c95d74eDavid Li}
8361591693c7b415e9869157c711fe11263c95d74eDavid Li
8371591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
8381591693c7b415e9869157c711fe11263c95d74eDavid Liast_expression::hir(exec_list *instructions,
8391591693c7b415e9869157c711fe11263c95d74eDavid Li		    struct _mesa_glsl_parse_state *state)
8401591693c7b415e9869157c711fe11263c95d74eDavid Li{
8411591693c7b415e9869157c711fe11263c95d74eDavid Li   void *ctx = state;
8421591693c7b415e9869157c711fe11263c95d74eDavid Li   static const int operations[AST_NUM_OPERATORS] = {
8431591693c7b415e9869157c711fe11263c95d74eDavid Li      -1,               /* ast_assign doesn't convert to ir_expression. */
8441591693c7b415e9869157c711fe11263c95d74eDavid Li      -1,               /* ast_plus doesn't convert to ir_expression. */
8451591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_unop_neg,
8461591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_add,
8471591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_sub,
8481591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_mul,
8491591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_div,
8501591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_mod,
8511591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_lshift,
8521591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_rshift,
8531591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_less,
8541591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_greater,
8551591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_lequal,
8561591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_gequal,
8571591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_all_equal,
8581591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_any_nequal,
8591591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_bit_and,
8601591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_bit_xor,
8611591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_bit_or,
8621591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_unop_bit_not,
8631591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_logic_and,
8641591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_logic_xor,
8651591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_logic_or,
8661591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_unop_logic_not,
8671591693c7b415e9869157c711fe11263c95d74eDavid Li
8681591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Note: The following block of expression types actually convert
8691591693c7b415e9869157c711fe11263c95d74eDavid Li       * to multiple IR instructions.
8701591693c7b415e9869157c711fe11263c95d74eDavid Li       */
8711591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_mul,     /* ast_mul_assign */
8721591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_div,     /* ast_div_assign */
8731591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_mod,     /* ast_mod_assign */
8741591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_add,     /* ast_add_assign */
8751591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_sub,     /* ast_sub_assign */
8761591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_lshift,  /* ast_ls_assign */
8771591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_rshift,  /* ast_rs_assign */
8781591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_bit_and, /* ast_and_assign */
8791591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_bit_xor, /* ast_xor_assign */
8801591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_bit_or,  /* ast_or_assign */
8811591693c7b415e9869157c711fe11263c95d74eDavid Li
8821591693c7b415e9869157c711fe11263c95d74eDavid Li      -1,               /* ast_conditional doesn't convert to ir_expression. */
8831591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_add,     /* ast_pre_inc. */
8841591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_sub,     /* ast_pre_dec. */
8851591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_add,     /* ast_post_inc. */
8861591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_binop_sub,     /* ast_post_dec. */
8871591693c7b415e9869157c711fe11263c95d74eDavid Li      -1,               /* ast_field_selection doesn't conv to ir_expression. */
8881591693c7b415e9869157c711fe11263c95d74eDavid Li      -1,               /* ast_array_index doesn't convert to ir_expression. */
8891591693c7b415e9869157c711fe11263c95d74eDavid Li      -1,               /* ast_function_call doesn't conv to ir_expression. */
8901591693c7b415e9869157c711fe11263c95d74eDavid Li      -1,               /* ast_identifier doesn't convert to ir_expression. */
8911591693c7b415e9869157c711fe11263c95d74eDavid Li      -1,               /* ast_int_constant doesn't convert to ir_expression. */
8921591693c7b415e9869157c711fe11263c95d74eDavid Li      -1,               /* ast_uint_constant doesn't conv to ir_expression. */
8931591693c7b415e9869157c711fe11263c95d74eDavid Li      -1,               /* ast_float_constant doesn't conv to ir_expression. */
8941591693c7b415e9869157c711fe11263c95d74eDavid Li      -1,               /* ast_bool_constant doesn't conv to ir_expression. */
8951591693c7b415e9869157c711fe11263c95d74eDavid Li      -1,               /* ast_sequence doesn't convert to ir_expression. */
8961591693c7b415e9869157c711fe11263c95d74eDavid Li   };
8971591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_rvalue *result = NULL;
8981591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_rvalue *op[3];
8991591693c7b415e9869157c711fe11263c95d74eDavid Li   const struct glsl_type *type = glsl_type::error_type;
9001591693c7b415e9869157c711fe11263c95d74eDavid Li   bool error_emitted = false;
9011591693c7b415e9869157c711fe11263c95d74eDavid Li   YYLTYPE loc;
9021591693c7b415e9869157c711fe11263c95d74eDavid Li
9031591693c7b415e9869157c711fe11263c95d74eDavid Li   loc = this->get_location();
9041591693c7b415e9869157c711fe11263c95d74eDavid Li
9051591693c7b415e9869157c711fe11263c95d74eDavid Li   switch (this->oper) {
9061591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_assign: {
9071591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
9081591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = this->subexpressions[1]->hir(instructions, state);
9091591693c7b415e9869157c711fe11263c95d74eDavid Li
9101591693c7b415e9869157c711fe11263c95d74eDavid Li      result = do_assignment(instructions, state, op[0], op[1],
9111591693c7b415e9869157c711fe11263c95d74eDavid Li			     this->subexpressions[0]->get_location());
9121591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = result->type->is_error();
9131591693c7b415e9869157c711fe11263c95d74eDavid Li      type = result->type;
9141591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
9151591693c7b415e9869157c711fe11263c95d74eDavid Li   }
9161591693c7b415e9869157c711fe11263c95d74eDavid Li
9171591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_plus:
9181591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
9191591693c7b415e9869157c711fe11263c95d74eDavid Li
9201591693c7b415e9869157c711fe11263c95d74eDavid Li      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
9211591693c7b415e9869157c711fe11263c95d74eDavid Li
9221591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = type->is_error();
9231591693c7b415e9869157c711fe11263c95d74eDavid Li
9241591693c7b415e9869157c711fe11263c95d74eDavid Li      result = op[0];
9251591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
9261591693c7b415e9869157c711fe11263c95d74eDavid Li
9271591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_neg:
9281591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
9291591693c7b415e9869157c711fe11263c95d74eDavid Li
9301591693c7b415e9869157c711fe11263c95d74eDavid Li      type = unary_arithmetic_result_type(op[0]->type, state, & loc);
9311591693c7b415e9869157c711fe11263c95d74eDavid Li
9321591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = type->is_error();
9331591693c7b415e9869157c711fe11263c95d74eDavid Li
9341591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_expression(operations[this->oper], type,
9351591693c7b415e9869157c711fe11263c95d74eDavid Li				      op[0], NULL);
9361591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
9371591693c7b415e9869157c711fe11263c95d74eDavid Li
9381591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_add:
9391591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_sub:
9401591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_mul:
9411591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_div:
9421591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
9431591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = this->subexpressions[1]->hir(instructions, state);
9441591693c7b415e9869157c711fe11263c95d74eDavid Li
9451591693c7b415e9869157c711fe11263c95d74eDavid Li      type = arithmetic_result_type(op[0], op[1],
9461591693c7b415e9869157c711fe11263c95d74eDavid Li				    (this->oper == ast_mul),
9471591693c7b415e9869157c711fe11263c95d74eDavid Li				    state, & loc);
9481591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = type->is_error();
9491591693c7b415e9869157c711fe11263c95d74eDavid Li
9501591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_expression(operations[this->oper], type,
9511591693c7b415e9869157c711fe11263c95d74eDavid Li				      op[0], op[1]);
9521591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
9531591693c7b415e9869157c711fe11263c95d74eDavid Li
9541591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_mod:
9551591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
9561591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = this->subexpressions[1]->hir(instructions, state);
9571591693c7b415e9869157c711fe11263c95d74eDavid Li
9581591693c7b415e9869157c711fe11263c95d74eDavid Li      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
9591591693c7b415e9869157c711fe11263c95d74eDavid Li
9601591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(operations[this->oper] == ir_binop_mod);
9611591693c7b415e9869157c711fe11263c95d74eDavid Li
9621591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_expression(operations[this->oper], type,
9631591693c7b415e9869157c711fe11263c95d74eDavid Li				      op[0], op[1]);
9641591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = type->is_error();
9651591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
9661591693c7b415e9869157c711fe11263c95d74eDavid Li
9671591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_lshift:
9681591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_rshift:
9691591693c7b415e9869157c711fe11263c95d74eDavid Li       if (state->language_version < 130) {
9701591693c7b415e9869157c711fe11263c95d74eDavid Li          _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30",
9711591693c7b415e9869157c711fe11263c95d74eDavid Li              operator_string(this->oper));
9721591693c7b415e9869157c711fe11263c95d74eDavid Li          error_emitted = true;
9731591693c7b415e9869157c711fe11263c95d74eDavid Li       }
9741591693c7b415e9869157c711fe11263c95d74eDavid Li
9751591693c7b415e9869157c711fe11263c95d74eDavid Li       op[0] = this->subexpressions[0]->hir(instructions, state);
9761591693c7b415e9869157c711fe11263c95d74eDavid Li       op[1] = this->subexpressions[1]->hir(instructions, state);
9771591693c7b415e9869157c711fe11263c95d74eDavid Li       type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
9781591693c7b415e9869157c711fe11263c95d74eDavid Li                                &loc);
9791591693c7b415e9869157c711fe11263c95d74eDavid Li       result = new(ctx) ir_expression(operations[this->oper], type,
9801591693c7b415e9869157c711fe11263c95d74eDavid Li                                       op[0], op[1]);
9811591693c7b415e9869157c711fe11263c95d74eDavid Li       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
9821591693c7b415e9869157c711fe11263c95d74eDavid Li       break;
9831591693c7b415e9869157c711fe11263c95d74eDavid Li
9841591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_less:
9851591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_greater:
9861591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_lequal:
9871591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_gequal:
9881591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
9891591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = this->subexpressions[1]->hir(instructions, state);
9901591693c7b415e9869157c711fe11263c95d74eDavid Li
9911591693c7b415e9869157c711fe11263c95d74eDavid Li      type = relational_result_type(op[0], op[1], state, & loc);
9921591693c7b415e9869157c711fe11263c95d74eDavid Li
9931591693c7b415e9869157c711fe11263c95d74eDavid Li      /* The relational operators must either generate an error or result
9941591693c7b415e9869157c711fe11263c95d74eDavid Li       * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
9951591693c7b415e9869157c711fe11263c95d74eDavid Li       */
9961591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(type->is_error()
9971591693c7b415e9869157c711fe11263c95d74eDavid Li	     || ((type->base_type == GLSL_TYPE_BOOL)
9981591693c7b415e9869157c711fe11263c95d74eDavid Li		 && type->is_scalar()));
9991591693c7b415e9869157c711fe11263c95d74eDavid Li
10001591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_expression(operations[this->oper], type,
10011591693c7b415e9869157c711fe11263c95d74eDavid Li				      op[0], op[1]);
10021591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = type->is_error();
10031591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
10041591693c7b415e9869157c711fe11263c95d74eDavid Li
10051591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_nequal:
10061591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_equal:
10071591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
10081591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = this->subexpressions[1]->hir(instructions, state);
10091591693c7b415e9869157c711fe11263c95d74eDavid Li
10101591693c7b415e9869157c711fe11263c95d74eDavid Li      /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
10111591693c7b415e9869157c711fe11263c95d74eDavid Li       *
10121591693c7b415e9869157c711fe11263c95d74eDavid Li       *    "The equality operators equal (==), and not equal (!=)
10131591693c7b415e9869157c711fe11263c95d74eDavid Li       *    operate on all types. They result in a scalar Boolean. If
10141591693c7b415e9869157c711fe11263c95d74eDavid Li       *    the operand types do not match, then there must be a
10151591693c7b415e9869157c711fe11263c95d74eDavid Li       *    conversion from Section 4.1.10 "Implicit Conversions"
10161591693c7b415e9869157c711fe11263c95d74eDavid Li       *    applied to one operand that can make them match, in which
10171591693c7b415e9869157c711fe11263c95d74eDavid Li       *    case this conversion is done."
10181591693c7b415e9869157c711fe11263c95d74eDavid Li       */
10191591693c7b415e9869157c711fe11263c95d74eDavid Li      if ((!apply_implicit_conversion(op[0]->type, op[1], state)
10201591693c7b415e9869157c711fe11263c95d74eDavid Li	   && !apply_implicit_conversion(op[1]->type, op[0], state))
10211591693c7b415e9869157c711fe11263c95d74eDavid Li	  || (op[0]->type != op[1]->type)) {
10221591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
10231591693c7b415e9869157c711fe11263c95d74eDavid Li			  "type", (this->oper == ast_equal) ? "==" : "!=");
10241591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
10251591693c7b415e9869157c711fe11263c95d74eDavid Li      } else if ((state->language_version <= 110)
10261591693c7b415e9869157c711fe11263c95d74eDavid Li		 && (op[0]->type->is_array() || op[1]->type->is_array())) {
10271591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
10281591693c7b415e9869157c711fe11263c95d74eDavid Li			  "GLSL 1.10");
10291591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
10301591693c7b415e9869157c711fe11263c95d74eDavid Li      }
10311591693c7b415e9869157c711fe11263c95d74eDavid Li
10321591693c7b415e9869157c711fe11263c95d74eDavid Li      result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
10331591693c7b415e9869157c711fe11263c95d74eDavid Li      type = glsl_type::bool_type;
10341591693c7b415e9869157c711fe11263c95d74eDavid Li
10351591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(error_emitted || (result->type == glsl_type::bool_type));
10361591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
10371591693c7b415e9869157c711fe11263c95d74eDavid Li
10381591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_bit_and:
10391591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_bit_xor:
10401591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_bit_or:
10411591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
10421591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = this->subexpressions[1]->hir(instructions, state);
10431591693c7b415e9869157c711fe11263c95d74eDavid Li      type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
10441591693c7b415e9869157c711fe11263c95d74eDavid Li                                   state, &loc);
10451591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_expression(operations[this->oper], type,
10461591693c7b415e9869157c711fe11263c95d74eDavid Li				      op[0], op[1]);
10471591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
10481591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
10491591693c7b415e9869157c711fe11263c95d74eDavid Li
10501591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_bit_not:
10511591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
10521591693c7b415e9869157c711fe11263c95d74eDavid Li
10531591693c7b415e9869157c711fe11263c95d74eDavid Li      if (state->language_version < 130) {
10541591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
10551591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
10561591693c7b415e9869157c711fe11263c95d74eDavid Li      }
10571591693c7b415e9869157c711fe11263c95d74eDavid Li
10581591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!op[0]->type->is_integer()) {
10591591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
10601591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
10611591693c7b415e9869157c711fe11263c95d74eDavid Li      }
10621591693c7b415e9869157c711fe11263c95d74eDavid Li
10631591693c7b415e9869157c711fe11263c95d74eDavid Li      type = op[0]->type;
10641591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
10651591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
10661591693c7b415e9869157c711fe11263c95d74eDavid Li
10671591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_logic_and: {
10681591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
10691591693c7b415e9869157c711fe11263c95d74eDavid Li
10701591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
10711591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->subexpressions[0]->get_location();
10721591693c7b415e9869157c711fe11263c95d74eDavid Li
10731591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
10741591693c7b415e9869157c711fe11263c95d74eDavid Li			  operator_string(this->oper));
10751591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
10761591693c7b415e9869157c711fe11263c95d74eDavid Li      }
10771591693c7b415e9869157c711fe11263c95d74eDavid Li
10781591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_constant *op0_const = op[0]->constant_expression_value();
10791591693c7b415e9869157c711fe11263c95d74eDavid Li      if (op0_const) {
10801591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (op0_const->value.b[0]) {
10811591693c7b415e9869157c711fe11263c95d74eDavid Li	    op[1] = this->subexpressions[1]->hir(instructions, state);
10821591693c7b415e9869157c711fe11263c95d74eDavid Li
10831591693c7b415e9869157c711fe11263c95d74eDavid Li	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
10841591693c7b415e9869157c711fe11263c95d74eDavid Li	       YYLTYPE loc = this->subexpressions[1]->get_location();
10851591693c7b415e9869157c711fe11263c95d74eDavid Li
10861591693c7b415e9869157c711fe11263c95d74eDavid Li	       _mesa_glsl_error(& loc, state,
10871591693c7b415e9869157c711fe11263c95d74eDavid Li				"RHS of `%s' must be scalar boolean",
10881591693c7b415e9869157c711fe11263c95d74eDavid Li				operator_string(this->oper));
10891591693c7b415e9869157c711fe11263c95d74eDavid Li	       error_emitted = true;
10901591693c7b415e9869157c711fe11263c95d74eDavid Li	    }
10911591693c7b415e9869157c711fe11263c95d74eDavid Li	    result = op[1];
10921591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else {
10931591693c7b415e9869157c711fe11263c95d74eDavid Li	    result = op0_const;
10941591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
10951591693c7b415e9869157c711fe11263c95d74eDavid Li	 type = glsl_type::bool_type;
10961591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
10971591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
10981591693c7b415e9869157c711fe11263c95d74eDavid Li						       "and_tmp",
10991591693c7b415e9869157c711fe11263c95d74eDavid Li						       ir_var_temporary);
11001591693c7b415e9869157c711fe11263c95d74eDavid Li	 instructions->push_tail(tmp);
11011591693c7b415e9869157c711fe11263c95d74eDavid Li
11021591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_if *const stmt = new(ctx) ir_if(op[0]);
11031591693c7b415e9869157c711fe11263c95d74eDavid Li	 instructions->push_tail(stmt);
11041591693c7b415e9869157c711fe11263c95d74eDavid Li
11051591693c7b415e9869157c711fe11263c95d74eDavid Li	 op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
11061591693c7b415e9869157c711fe11263c95d74eDavid Li
11071591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
11081591693c7b415e9869157c711fe11263c95d74eDavid Li	    YYLTYPE loc = this->subexpressions[1]->get_location();
11091591693c7b415e9869157c711fe11263c95d74eDavid Li
11101591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
11111591693c7b415e9869157c711fe11263c95d74eDavid Li			     "RHS of `%s' must be scalar boolean",
11121591693c7b415e9869157c711fe11263c95d74eDavid Li			     operator_string(this->oper));
11131591693c7b415e9869157c711fe11263c95d74eDavid Li	    error_emitted = true;
11141591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
11151591693c7b415e9869157c711fe11263c95d74eDavid Li
11161591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
11171591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_assignment *const then_assign =
11181591693c7b415e9869157c711fe11263c95d74eDavid Li	    new(ctx) ir_assignment(then_deref, op[1], NULL);
11191591693c7b415e9869157c711fe11263c95d74eDavid Li	 stmt->then_instructions.push_tail(then_assign);
11201591693c7b415e9869157c711fe11263c95d74eDavid Li
11211591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
11221591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_assignment *const else_assign =
11231591693c7b415e9869157c711fe11263c95d74eDavid Li	    new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
11241591693c7b415e9869157c711fe11263c95d74eDavid Li	 stmt->else_instructions.push_tail(else_assign);
11251591693c7b415e9869157c711fe11263c95d74eDavid Li
11261591693c7b415e9869157c711fe11263c95d74eDavid Li	 result = new(ctx) ir_dereference_variable(tmp);
11271591693c7b415e9869157c711fe11263c95d74eDavid Li	 type = tmp->type;
11281591693c7b415e9869157c711fe11263c95d74eDavid Li      }
11291591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
11301591693c7b415e9869157c711fe11263c95d74eDavid Li   }
11311591693c7b415e9869157c711fe11263c95d74eDavid Li
11321591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_logic_or: {
11331591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
11341591693c7b415e9869157c711fe11263c95d74eDavid Li
11351591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
11361591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->subexpressions[0]->get_location();
11371591693c7b415e9869157c711fe11263c95d74eDavid Li
11381591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
11391591693c7b415e9869157c711fe11263c95d74eDavid Li			  operator_string(this->oper));
11401591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
11411591693c7b415e9869157c711fe11263c95d74eDavid Li      }
11421591693c7b415e9869157c711fe11263c95d74eDavid Li
11431591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_constant *op0_const = op[0]->constant_expression_value();
11441591693c7b415e9869157c711fe11263c95d74eDavid Li      if (op0_const) {
11451591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (op0_const->value.b[0]) {
11461591693c7b415e9869157c711fe11263c95d74eDavid Li	    result = op0_const;
11471591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else {
11481591693c7b415e9869157c711fe11263c95d74eDavid Li	    op[1] = this->subexpressions[1]->hir(instructions, state);
11491591693c7b415e9869157c711fe11263c95d74eDavid Li
11501591693c7b415e9869157c711fe11263c95d74eDavid Li	    if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
11511591693c7b415e9869157c711fe11263c95d74eDavid Li	       YYLTYPE loc = this->subexpressions[1]->get_location();
11521591693c7b415e9869157c711fe11263c95d74eDavid Li
11531591693c7b415e9869157c711fe11263c95d74eDavid Li	       _mesa_glsl_error(& loc, state,
11541591693c7b415e9869157c711fe11263c95d74eDavid Li				"RHS of `%s' must be scalar boolean",
11551591693c7b415e9869157c711fe11263c95d74eDavid Li				operator_string(this->oper));
11561591693c7b415e9869157c711fe11263c95d74eDavid Li	       error_emitted = true;
11571591693c7b415e9869157c711fe11263c95d74eDavid Li	    }
11581591693c7b415e9869157c711fe11263c95d74eDavid Li	    result = op[1];
11591591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
11601591693c7b415e9869157c711fe11263c95d74eDavid Li	 type = glsl_type::bool_type;
11611591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
11621591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
11631591693c7b415e9869157c711fe11263c95d74eDavid Li						       "or_tmp",
11641591693c7b415e9869157c711fe11263c95d74eDavid Li						       ir_var_temporary);
11651591693c7b415e9869157c711fe11263c95d74eDavid Li	 instructions->push_tail(tmp);
11661591693c7b415e9869157c711fe11263c95d74eDavid Li
11671591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_if *const stmt = new(ctx) ir_if(op[0]);
11681591693c7b415e9869157c711fe11263c95d74eDavid Li	 instructions->push_tail(stmt);
11691591693c7b415e9869157c711fe11263c95d74eDavid Li
11701591693c7b415e9869157c711fe11263c95d74eDavid Li	 op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state);
11711591693c7b415e9869157c711fe11263c95d74eDavid Li
11721591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
11731591693c7b415e9869157c711fe11263c95d74eDavid Li	    YYLTYPE loc = this->subexpressions[1]->get_location();
11741591693c7b415e9869157c711fe11263c95d74eDavid Li
11751591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
11761591693c7b415e9869157c711fe11263c95d74eDavid Li			     operator_string(this->oper));
11771591693c7b415e9869157c711fe11263c95d74eDavid Li	    error_emitted = true;
11781591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
11791591693c7b415e9869157c711fe11263c95d74eDavid Li
11801591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
11811591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_assignment *const then_assign =
11821591693c7b415e9869157c711fe11263c95d74eDavid Li	    new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
11831591693c7b415e9869157c711fe11263c95d74eDavid Li	 stmt->then_instructions.push_tail(then_assign);
11841591693c7b415e9869157c711fe11263c95d74eDavid Li
11851591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
11861591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_assignment *const else_assign =
11871591693c7b415e9869157c711fe11263c95d74eDavid Li	    new(ctx) ir_assignment(else_deref, op[1], NULL);
11881591693c7b415e9869157c711fe11263c95d74eDavid Li	 stmt->else_instructions.push_tail(else_assign);
11891591693c7b415e9869157c711fe11263c95d74eDavid Li
11901591693c7b415e9869157c711fe11263c95d74eDavid Li	 result = new(ctx) ir_dereference_variable(tmp);
11911591693c7b415e9869157c711fe11263c95d74eDavid Li	 type = tmp->type;
11921591693c7b415e9869157c711fe11263c95d74eDavid Li      }
11931591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
11941591693c7b415e9869157c711fe11263c95d74eDavid Li   }
11951591693c7b415e9869157c711fe11263c95d74eDavid Li
11961591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_logic_xor:
11971591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
11981591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = this->subexpressions[1]->hir(instructions, state);
11991591693c7b415e9869157c711fe11263c95d74eDavid Li
12001591693c7b415e9869157c711fe11263c95d74eDavid Li
12011591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
12021591693c7b415e9869157c711fe11263c95d74eDavid Li				      op[0], op[1]);
12031591693c7b415e9869157c711fe11263c95d74eDavid Li      type = glsl_type::bool_type;
12041591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
12051591693c7b415e9869157c711fe11263c95d74eDavid Li
12061591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_logic_not:
12071591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
12081591693c7b415e9869157c711fe11263c95d74eDavid Li
12091591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
12101591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->subexpressions[0]->get_location();
12111591693c7b415e9869157c711fe11263c95d74eDavid Li
12121591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state,
12131591693c7b415e9869157c711fe11263c95d74eDavid Li			  "operand of `!' must be scalar boolean");
12141591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
12151591693c7b415e9869157c711fe11263c95d74eDavid Li      }
12161591693c7b415e9869157c711fe11263c95d74eDavid Li
12171591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
12181591693c7b415e9869157c711fe11263c95d74eDavid Li				      op[0], NULL);
12191591693c7b415e9869157c711fe11263c95d74eDavid Li      type = glsl_type::bool_type;
12201591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
12211591693c7b415e9869157c711fe11263c95d74eDavid Li
12221591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_mul_assign:
12231591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_div_assign:
12241591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_add_assign:
12251591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_sub_assign: {
12261591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
12271591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = this->subexpressions[1]->hir(instructions, state);
12281591693c7b415e9869157c711fe11263c95d74eDavid Li
12291591693c7b415e9869157c711fe11263c95d74eDavid Li      type = arithmetic_result_type(op[0], op[1],
12301591693c7b415e9869157c711fe11263c95d74eDavid Li				    (this->oper == ast_mul_assign),
12311591693c7b415e9869157c711fe11263c95d74eDavid Li				    state, & loc);
12321591693c7b415e9869157c711fe11263c95d74eDavid Li
12331591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
12341591693c7b415e9869157c711fe11263c95d74eDavid Li						   op[0], op[1]);
12351591693c7b415e9869157c711fe11263c95d74eDavid Li
12361591693c7b415e9869157c711fe11263c95d74eDavid Li      result = do_assignment(instructions, state,
12371591693c7b415e9869157c711fe11263c95d74eDavid Li			     op[0]->clone(ctx, NULL), temp_rhs,
12381591693c7b415e9869157c711fe11263c95d74eDavid Li			     this->subexpressions[0]->get_location());
12391591693c7b415e9869157c711fe11263c95d74eDavid Li      type = result->type;
12401591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = (op[0]->type->is_error());
12411591693c7b415e9869157c711fe11263c95d74eDavid Li
12421591693c7b415e9869157c711fe11263c95d74eDavid Li      /* GLSL 1.10 does not allow array assignment.  However, we don't have to
12431591693c7b415e9869157c711fe11263c95d74eDavid Li       * explicitly test for this because none of the binary expression
12441591693c7b415e9869157c711fe11263c95d74eDavid Li       * operators allow array operands either.
12451591693c7b415e9869157c711fe11263c95d74eDavid Li       */
12461591693c7b415e9869157c711fe11263c95d74eDavid Li
12471591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
12481591693c7b415e9869157c711fe11263c95d74eDavid Li   }
12491591693c7b415e9869157c711fe11263c95d74eDavid Li
12501591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_mod_assign: {
12511591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
12521591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = this->subexpressions[1]->hir(instructions, state);
12531591693c7b415e9869157c711fe11263c95d74eDavid Li
12541591693c7b415e9869157c711fe11263c95d74eDavid Li      type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
12551591693c7b415e9869157c711fe11263c95d74eDavid Li
12561591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(operations[this->oper] == ir_binop_mod);
12571591693c7b415e9869157c711fe11263c95d74eDavid Li
12581591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_rvalue *temp_rhs;
12591591693c7b415e9869157c711fe11263c95d74eDavid Li      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
12601591693c7b415e9869157c711fe11263c95d74eDavid Li					op[0], op[1]);
12611591693c7b415e9869157c711fe11263c95d74eDavid Li
12621591693c7b415e9869157c711fe11263c95d74eDavid Li      result = do_assignment(instructions, state,
12631591693c7b415e9869157c711fe11263c95d74eDavid Li			     op[0]->clone(ctx, NULL), temp_rhs,
12641591693c7b415e9869157c711fe11263c95d74eDavid Li			     this->subexpressions[0]->get_location());
12651591693c7b415e9869157c711fe11263c95d74eDavid Li      type = result->type;
12661591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = type->is_error();
12671591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
12681591693c7b415e9869157c711fe11263c95d74eDavid Li   }
12691591693c7b415e9869157c711fe11263c95d74eDavid Li
12701591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_ls_assign:
12711591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_rs_assign: {
12721591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
12731591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = this->subexpressions[1]->hir(instructions, state);
12741591693c7b415e9869157c711fe11263c95d74eDavid Li      type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
12751591693c7b415e9869157c711fe11263c95d74eDavid Li                               &loc);
12761591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
12771591693c7b415e9869157c711fe11263c95d74eDavid Li                                                   type, op[0], op[1]);
12781591693c7b415e9869157c711fe11263c95d74eDavid Li      result = do_assignment(instructions, state, op[0]->clone(ctx, NULL),
12791591693c7b415e9869157c711fe11263c95d74eDavid Li                             temp_rhs,
12801591693c7b415e9869157c711fe11263c95d74eDavid Li                             this->subexpressions[0]->get_location());
12811591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
12821591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
12831591693c7b415e9869157c711fe11263c95d74eDavid Li   }
12841591693c7b415e9869157c711fe11263c95d74eDavid Li
12851591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_and_assign:
12861591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_xor_assign:
12871591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_or_assign: {
12881591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
12891591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = this->subexpressions[1]->hir(instructions, state);
12901591693c7b415e9869157c711fe11263c95d74eDavid Li      type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
12911591693c7b415e9869157c711fe11263c95d74eDavid Li                                   state, &loc);
12921591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
12931591693c7b415e9869157c711fe11263c95d74eDavid Li                                                   type, op[0], op[1]);
12941591693c7b415e9869157c711fe11263c95d74eDavid Li      result = do_assignment(instructions, state, op[0]->clone(ctx, NULL),
12951591693c7b415e9869157c711fe11263c95d74eDavid Li                             temp_rhs,
12961591693c7b415e9869157c711fe11263c95d74eDavid Li                             this->subexpressions[0]->get_location());
12971591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
12981591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
12991591693c7b415e9869157c711fe11263c95d74eDavid Li   }
13001591693c7b415e9869157c711fe11263c95d74eDavid Li
13011591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_conditional: {
13021591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
13031591693c7b415e9869157c711fe11263c95d74eDavid Li
13041591693c7b415e9869157c711fe11263c95d74eDavid Li      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
13051591693c7b415e9869157c711fe11263c95d74eDavid Li       *
13061591693c7b415e9869157c711fe11263c95d74eDavid Li       *    "The ternary selection operator (?:). It operates on three
13071591693c7b415e9869157c711fe11263c95d74eDavid Li       *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
13081591693c7b415e9869157c711fe11263c95d74eDavid Li       *    first expression, which must result in a scalar Boolean."
13091591693c7b415e9869157c711fe11263c95d74eDavid Li       */
13101591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
13111591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->subexpressions[0]->get_location();
13121591693c7b415e9869157c711fe11263c95d74eDavid Li
13131591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
13141591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
13151591693c7b415e9869157c711fe11263c95d74eDavid Li      }
13161591693c7b415e9869157c711fe11263c95d74eDavid Li
13171591693c7b415e9869157c711fe11263c95d74eDavid Li      /* The :? operator is implemented by generating an anonymous temporary
13181591693c7b415e9869157c711fe11263c95d74eDavid Li       * followed by an if-statement.  The last instruction in each branch of
13191591693c7b415e9869157c711fe11263c95d74eDavid Li       * the if-statement assigns a value to the anonymous temporary.  This
13201591693c7b415e9869157c711fe11263c95d74eDavid Li       * temporary is the r-value of the expression.
13211591693c7b415e9869157c711fe11263c95d74eDavid Li       */
13221591693c7b415e9869157c711fe11263c95d74eDavid Li      exec_list then_instructions;
13231591693c7b415e9869157c711fe11263c95d74eDavid Li      exec_list else_instructions;
13241591693c7b415e9869157c711fe11263c95d74eDavid Li
13251591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = this->subexpressions[1]->hir(&then_instructions, state);
13261591693c7b415e9869157c711fe11263c95d74eDavid Li      op[2] = this->subexpressions[2]->hir(&else_instructions, state);
13271591693c7b415e9869157c711fe11263c95d74eDavid Li
13281591693c7b415e9869157c711fe11263c95d74eDavid Li      /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
13291591693c7b415e9869157c711fe11263c95d74eDavid Li       *
13301591693c7b415e9869157c711fe11263c95d74eDavid Li       *     "The second and third expressions can be any type, as
13311591693c7b415e9869157c711fe11263c95d74eDavid Li       *     long their types match, or there is a conversion in
13321591693c7b415e9869157c711fe11263c95d74eDavid Li       *     Section 4.1.10 "Implicit Conversions" that can be applied
13331591693c7b415e9869157c711fe11263c95d74eDavid Li       *     to one of the expressions to make their types match. This
13341591693c7b415e9869157c711fe11263c95d74eDavid Li       *     resulting matching type is the type of the entire
13351591693c7b415e9869157c711fe11263c95d74eDavid Li       *     expression."
13361591693c7b415e9869157c711fe11263c95d74eDavid Li       */
13371591693c7b415e9869157c711fe11263c95d74eDavid Li      if ((!apply_implicit_conversion(op[1]->type, op[2], state)
13381591693c7b415e9869157c711fe11263c95d74eDavid Li	   && !apply_implicit_conversion(op[2]->type, op[1], state))
13391591693c7b415e9869157c711fe11263c95d74eDavid Li	  || (op[1]->type != op[2]->type)) {
13401591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->subexpressions[1]->get_location();
13411591693c7b415e9869157c711fe11263c95d74eDavid Li
13421591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
13431591693c7b415e9869157c711fe11263c95d74eDavid Li			  "operator must have matching types.");
13441591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
13451591693c7b415e9869157c711fe11263c95d74eDavid Li	 type = glsl_type::error_type;
13461591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
13471591693c7b415e9869157c711fe11263c95d74eDavid Li	 type = op[1]->type;
13481591693c7b415e9869157c711fe11263c95d74eDavid Li      }
13491591693c7b415e9869157c711fe11263c95d74eDavid Li
13501591693c7b415e9869157c711fe11263c95d74eDavid Li      /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
13511591693c7b415e9869157c711fe11263c95d74eDavid Li       *
13521591693c7b415e9869157c711fe11263c95d74eDavid Li       *    "The second and third expressions must be the same type, but can
13531591693c7b415e9869157c711fe11263c95d74eDavid Li       *    be of any type other than an array."
13541591693c7b415e9869157c711fe11263c95d74eDavid Li       */
13551591693c7b415e9869157c711fe11263c95d74eDavid Li      if ((state->language_version <= 110) && type->is_array()) {
13561591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
13571591693c7b415e9869157c711fe11263c95d74eDavid Li			  "operator must not be arrays.");
13581591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
13591591693c7b415e9869157c711fe11263c95d74eDavid Li      }
13601591693c7b415e9869157c711fe11263c95d74eDavid Li
13611591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_constant *cond_val = op[0]->constant_expression_value();
13621591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_constant *then_val = op[1]->constant_expression_value();
13631591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_constant *else_val = op[2]->constant_expression_value();
13641591693c7b415e9869157c711fe11263c95d74eDavid Li
13651591693c7b415e9869157c711fe11263c95d74eDavid Li      if (then_instructions.is_empty()
13661591693c7b415e9869157c711fe11263c95d74eDavid Li	  && else_instructions.is_empty()
13671591693c7b415e9869157c711fe11263c95d74eDavid Li	  && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
13681591693c7b415e9869157c711fe11263c95d74eDavid Li	 result = (cond_val->value.b[0]) ? then_val : else_val;
13691591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
13701591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_variable *const tmp =
13711591693c7b415e9869157c711fe11263c95d74eDavid Li	    new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
13721591693c7b415e9869157c711fe11263c95d74eDavid Li	 instructions->push_tail(tmp);
13731591693c7b415e9869157c711fe11263c95d74eDavid Li
13741591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_if *const stmt = new(ctx) ir_if(op[0]);
13751591693c7b415e9869157c711fe11263c95d74eDavid Li	 instructions->push_tail(stmt);
13761591693c7b415e9869157c711fe11263c95d74eDavid Li
13771591693c7b415e9869157c711fe11263c95d74eDavid Li	 then_instructions.move_nodes_to(& stmt->then_instructions);
13781591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_dereference *const then_deref =
13791591693c7b415e9869157c711fe11263c95d74eDavid Li	    new(ctx) ir_dereference_variable(tmp);
13801591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_assignment *const then_assign =
13811591693c7b415e9869157c711fe11263c95d74eDavid Li	    new(ctx) ir_assignment(then_deref, op[1], NULL);
13821591693c7b415e9869157c711fe11263c95d74eDavid Li	 stmt->then_instructions.push_tail(then_assign);
13831591693c7b415e9869157c711fe11263c95d74eDavid Li
13841591693c7b415e9869157c711fe11263c95d74eDavid Li	 else_instructions.move_nodes_to(& stmt->else_instructions);
13851591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_dereference *const else_deref =
13861591693c7b415e9869157c711fe11263c95d74eDavid Li	    new(ctx) ir_dereference_variable(tmp);
13871591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_assignment *const else_assign =
13881591693c7b415e9869157c711fe11263c95d74eDavid Li	    new(ctx) ir_assignment(else_deref, op[2], NULL);
13891591693c7b415e9869157c711fe11263c95d74eDavid Li	 stmt->else_instructions.push_tail(else_assign);
13901591693c7b415e9869157c711fe11263c95d74eDavid Li
13911591693c7b415e9869157c711fe11263c95d74eDavid Li	 result = new(ctx) ir_dereference_variable(tmp);
13921591693c7b415e9869157c711fe11263c95d74eDavid Li      }
13931591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
13941591693c7b415e9869157c711fe11263c95d74eDavid Li   }
13951591693c7b415e9869157c711fe11263c95d74eDavid Li
13961591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_pre_inc:
13971591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_pre_dec: {
13981591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
13991591693c7b415e9869157c711fe11263c95d74eDavid Li      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
14001591693c7b415e9869157c711fe11263c95d74eDavid Li	 op[1] = new(ctx) ir_constant(1.0f);
14011591693c7b415e9869157c711fe11263c95d74eDavid Li      else
14021591693c7b415e9869157c711fe11263c95d74eDavid Li	 op[1] = new(ctx) ir_constant(1);
14031591693c7b415e9869157c711fe11263c95d74eDavid Li
14041591693c7b415e9869157c711fe11263c95d74eDavid Li      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
14051591693c7b415e9869157c711fe11263c95d74eDavid Li
14061591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_rvalue *temp_rhs;
14071591693c7b415e9869157c711fe11263c95d74eDavid Li      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
14081591693c7b415e9869157c711fe11263c95d74eDavid Li					op[0], op[1]);
14091591693c7b415e9869157c711fe11263c95d74eDavid Li
14101591693c7b415e9869157c711fe11263c95d74eDavid Li      result = do_assignment(instructions, state,
14111591693c7b415e9869157c711fe11263c95d74eDavid Li			     op[0]->clone(ctx, NULL), temp_rhs,
14121591693c7b415e9869157c711fe11263c95d74eDavid Li			     this->subexpressions[0]->get_location());
14131591693c7b415e9869157c711fe11263c95d74eDavid Li      type = result->type;
14141591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = op[0]->type->is_error();
14151591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
14161591693c7b415e9869157c711fe11263c95d74eDavid Li   }
14171591693c7b415e9869157c711fe11263c95d74eDavid Li
14181591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_post_inc:
14191591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_post_dec: {
14201591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = this->subexpressions[0]->hir(instructions, state);
14211591693c7b415e9869157c711fe11263c95d74eDavid Li      if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
14221591693c7b415e9869157c711fe11263c95d74eDavid Li	 op[1] = new(ctx) ir_constant(1.0f);
14231591693c7b415e9869157c711fe11263c95d74eDavid Li      else
14241591693c7b415e9869157c711fe11263c95d74eDavid Li	 op[1] = new(ctx) ir_constant(1);
14251591693c7b415e9869157c711fe11263c95d74eDavid Li
14261591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
14271591693c7b415e9869157c711fe11263c95d74eDavid Li
14281591693c7b415e9869157c711fe11263c95d74eDavid Li      type = arithmetic_result_type(op[0], op[1], false, state, & loc);
14291591693c7b415e9869157c711fe11263c95d74eDavid Li
14301591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_rvalue *temp_rhs;
14311591693c7b415e9869157c711fe11263c95d74eDavid Li      temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
14321591693c7b415e9869157c711fe11263c95d74eDavid Li					op[0], op[1]);
14331591693c7b415e9869157c711fe11263c95d74eDavid Li
14341591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Get a temporary of a copy of the lvalue before it's modified.
14351591693c7b415e9869157c711fe11263c95d74eDavid Li       * This may get thrown away later.
14361591693c7b415e9869157c711fe11263c95d74eDavid Li       */
14371591693c7b415e9869157c711fe11263c95d74eDavid Li      result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
14381591693c7b415e9869157c711fe11263c95d74eDavid Li
14391591693c7b415e9869157c711fe11263c95d74eDavid Li      (void)do_assignment(instructions, state,
14401591693c7b415e9869157c711fe11263c95d74eDavid Li			  op[0]->clone(ctx, NULL), temp_rhs,
14411591693c7b415e9869157c711fe11263c95d74eDavid Li			  this->subexpressions[0]->get_location());
14421591693c7b415e9869157c711fe11263c95d74eDavid Li
14431591693c7b415e9869157c711fe11263c95d74eDavid Li      type = result->type;
14441591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = op[0]->type->is_error();
14451591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
14461591693c7b415e9869157c711fe11263c95d74eDavid Li   }
14471591693c7b415e9869157c711fe11263c95d74eDavid Li
14481591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_field_selection:
14491591693c7b415e9869157c711fe11263c95d74eDavid Li      result = _mesa_ast_field_selection_to_hir(this, instructions, state);
14501591693c7b415e9869157c711fe11263c95d74eDavid Li      type = result->type;
14511591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
14521591693c7b415e9869157c711fe11263c95d74eDavid Li
14531591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_array_index: {
14541591693c7b415e9869157c711fe11263c95d74eDavid Li      YYLTYPE index_loc = subexpressions[1]->get_location();
14551591693c7b415e9869157c711fe11263c95d74eDavid Li
14561591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = subexpressions[0]->hir(instructions, state);
14571591693c7b415e9869157c711fe11263c95d74eDavid Li      op[1] = subexpressions[1]->hir(instructions, state);
14581591693c7b415e9869157c711fe11263c95d74eDavid Li
14591591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
14601591693c7b415e9869157c711fe11263c95d74eDavid Li
14611591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_rvalue *const array = op[0];
14621591693c7b415e9869157c711fe11263c95d74eDavid Li
14631591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_dereference_array(op[0], op[1]);
14641591693c7b415e9869157c711fe11263c95d74eDavid Li
14651591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Do not use op[0] after this point.  Use array.
14661591693c7b415e9869157c711fe11263c95d74eDavid Li       */
14671591693c7b415e9869157c711fe11263c95d74eDavid Li      op[0] = NULL;
14681591693c7b415e9869157c711fe11263c95d74eDavid Li
14691591693c7b415e9869157c711fe11263c95d74eDavid Li
14701591693c7b415e9869157c711fe11263c95d74eDavid Li      if (error_emitted)
14711591693c7b415e9869157c711fe11263c95d74eDavid Li	 break;
14721591693c7b415e9869157c711fe11263c95d74eDavid Li
14731591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!array->type->is_array()
14741591693c7b415e9869157c711fe11263c95d74eDavid Li	  && !array->type->is_matrix()
14751591693c7b415e9869157c711fe11263c95d74eDavid Li	  && !array->type->is_vector()) {
14761591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& index_loc, state,
14771591693c7b415e9869157c711fe11263c95d74eDavid Li			  "cannot dereference non-array / non-matrix / "
14781591693c7b415e9869157c711fe11263c95d74eDavid Li			  "non-vector");
14791591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
14801591693c7b415e9869157c711fe11263c95d74eDavid Li      }
14811591693c7b415e9869157c711fe11263c95d74eDavid Li
14821591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!op[1]->type->is_integer()) {
14831591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& index_loc, state,
14841591693c7b415e9869157c711fe11263c95d74eDavid Li			  "array index must be integer type");
14851591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
14861591693c7b415e9869157c711fe11263c95d74eDavid Li      } else if (!op[1]->type->is_scalar()) {
14871591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& index_loc, state,
14881591693c7b415e9869157c711fe11263c95d74eDavid Li			  "array index must be scalar");
14891591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
14901591693c7b415e9869157c711fe11263c95d74eDavid Li      }
14911591693c7b415e9869157c711fe11263c95d74eDavid Li
14921591693c7b415e9869157c711fe11263c95d74eDavid Li      /* If the array index is a constant expression and the array has a
14931591693c7b415e9869157c711fe11263c95d74eDavid Li       * declared size, ensure that the access is in-bounds.  If the array
14941591693c7b415e9869157c711fe11263c95d74eDavid Li       * index is not a constant expression, ensure that the array has a
14951591693c7b415e9869157c711fe11263c95d74eDavid Li       * declared size.
14961591693c7b415e9869157c711fe11263c95d74eDavid Li       */
14971591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_constant *const const_index = op[1]->constant_expression_value();
14981591693c7b415e9869157c711fe11263c95d74eDavid Li      if (const_index != NULL) {
14991591693c7b415e9869157c711fe11263c95d74eDavid Li	 const int idx = const_index->value.i[0];
15001591693c7b415e9869157c711fe11263c95d74eDavid Li	 const char *type_name;
15011591693c7b415e9869157c711fe11263c95d74eDavid Li	 unsigned bound = 0;
15021591693c7b415e9869157c711fe11263c95d74eDavid Li
15031591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (array->type->is_matrix()) {
15041591693c7b415e9869157c711fe11263c95d74eDavid Li	    type_name = "matrix";
15051591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if (array->type->is_vector()) {
15061591693c7b415e9869157c711fe11263c95d74eDavid Li	    type_name = "vector";
15071591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else {
15081591693c7b415e9869157c711fe11263c95d74eDavid Li	    type_name = "array";
15091591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
15101591693c7b415e9869157c711fe11263c95d74eDavid Li
15111591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
15121591693c7b415e9869157c711fe11263c95d74eDavid Li	  *
15131591693c7b415e9869157c711fe11263c95d74eDavid Li	  *    "It is illegal to declare an array with a size, and then
15141591693c7b415e9869157c711fe11263c95d74eDavid Li	  *    later (in the same shader) index the same array with an
15151591693c7b415e9869157c711fe11263c95d74eDavid Li	  *    integral constant expression greater than or equal to the
15161591693c7b415e9869157c711fe11263c95d74eDavid Li	  *    declared size. It is also illegal to index an array with a
15171591693c7b415e9869157c711fe11263c95d74eDavid Li	  *    negative constant expression."
15181591693c7b415e9869157c711fe11263c95d74eDavid Li	  */
15191591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (array->type->is_matrix()) {
15201591693c7b415e9869157c711fe11263c95d74eDavid Li	    if (array->type->row_type()->vector_elements <= idx) {
15211591693c7b415e9869157c711fe11263c95d74eDavid Li	       bound = array->type->row_type()->vector_elements;
15221591693c7b415e9869157c711fe11263c95d74eDavid Li	    }
15231591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if (array->type->is_vector()) {
15241591693c7b415e9869157c711fe11263c95d74eDavid Li	    if (array->type->vector_elements <= idx) {
15251591693c7b415e9869157c711fe11263c95d74eDavid Li	       bound = array->type->vector_elements;
15261591693c7b415e9869157c711fe11263c95d74eDavid Li	    }
15271591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else {
15281591693c7b415e9869157c711fe11263c95d74eDavid Li	    if ((array->type->array_size() > 0)
15291591693c7b415e9869157c711fe11263c95d74eDavid Li		&& (array->type->array_size() <= idx)) {
15301591693c7b415e9869157c711fe11263c95d74eDavid Li	       bound = array->type->array_size();
15311591693c7b415e9869157c711fe11263c95d74eDavid Li	    }
15321591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
15331591693c7b415e9869157c711fe11263c95d74eDavid Li
15341591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (bound > 0) {
15351591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state, "%s index must be < %u",
15361591693c7b415e9869157c711fe11263c95d74eDavid Li			     type_name, bound);
15371591693c7b415e9869157c711fe11263c95d74eDavid Li	    error_emitted = true;
15381591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if (idx < 0) {
15391591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state, "%s index must be >= 0",
15401591693c7b415e9869157c711fe11263c95d74eDavid Li			     type_name);
15411591693c7b415e9869157c711fe11263c95d74eDavid Li	    error_emitted = true;
15421591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
15431591693c7b415e9869157c711fe11263c95d74eDavid Li
15441591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (array->type->is_array()) {
15451591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* If the array is a variable dereference, it dereferences the
15461591693c7b415e9869157c711fe11263c95d74eDavid Li	     * whole array, by definition.  Use this to get the variable.
15471591693c7b415e9869157c711fe11263c95d74eDavid Li	     *
15481591693c7b415e9869157c711fe11263c95d74eDavid Li	     * FINISHME: Should some methods for getting / setting / testing
15491591693c7b415e9869157c711fe11263c95d74eDavid Li	     * FINISHME: array access limits be added to ir_dereference?
15501591693c7b415e9869157c711fe11263c95d74eDavid Li	     */
15511591693c7b415e9869157c711fe11263c95d74eDavid Li	    ir_variable *const v = array->whole_variable_referenced();
15521591693c7b415e9869157c711fe11263c95d74eDavid Li	    if ((v != NULL) && (unsigned(idx) > v->max_array_access))
15531591693c7b415e9869157c711fe11263c95d74eDavid Li	       v->max_array_access = idx;
15541591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
15551591693c7b415e9869157c711fe11263c95d74eDavid Li      } else if (array->type->array_size() == 0) {
15561591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(&loc, state, "unsized array index must be constant");
15571591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
15581591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (array->type->is_array()) {
15591591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* whole_variable_referenced can return NULL if the array is a
15601591693c7b415e9869157c711fe11263c95d74eDavid Li	     * member of a structure.  In this case it is safe to not update
15611591693c7b415e9869157c711fe11263c95d74eDavid Li	     * the max_array_access field because it is never used for fields
15621591693c7b415e9869157c711fe11263c95d74eDavid Li	     * of structures.
15631591693c7b415e9869157c711fe11263c95d74eDavid Li	     */
15641591693c7b415e9869157c711fe11263c95d74eDavid Li	    ir_variable *v = array->whole_variable_referenced();
15651591693c7b415e9869157c711fe11263c95d74eDavid Li	    if (v != NULL)
15661591693c7b415e9869157c711fe11263c95d74eDavid Li	       v->max_array_access = array->type->array_size();
156736ffccf19c02a54a6dc9952dc9c181d4fda2a020David Li          // TODO: should this be array->type->array_size() - 1
15681591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
15691591693c7b415e9869157c711fe11263c95d74eDavid Li      }
15701591693c7b415e9869157c711fe11263c95d74eDavid Li
15711591693c7b415e9869157c711fe11263c95d74eDavid Li      /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
15721591693c7b415e9869157c711fe11263c95d74eDavid Li       *
15731591693c7b415e9869157c711fe11263c95d74eDavid Li       *    "Samplers aggregated into arrays within a shader (using square
15741591693c7b415e9869157c711fe11263c95d74eDavid Li       *    brackets [ ]) can only be indexed with integral constant
15751591693c7b415e9869157c711fe11263c95d74eDavid Li       *    expressions [...]."
15761591693c7b415e9869157c711fe11263c95d74eDavid Li       *
15771591693c7b415e9869157c711fe11263c95d74eDavid Li       * This restriction was added in GLSL 1.30.  Shaders using earlier version
15781591693c7b415e9869157c711fe11263c95d74eDavid Li       * of the language should not be rejected by the compiler front-end for
15791591693c7b415e9869157c711fe11263c95d74eDavid Li       * using this construct.  This allows useful things such as using a loop
15801591693c7b415e9869157c711fe11263c95d74eDavid Li       * counter as the index to an array of samplers.  If the loop in unrolled,
15811591693c7b415e9869157c711fe11263c95d74eDavid Li       * the code should compile correctly.  Instead, emit a warning.
15821591693c7b415e9869157c711fe11263c95d74eDavid Li       */
15831591693c7b415e9869157c711fe11263c95d74eDavid Li      if (array->type->is_array() &&
15841591693c7b415e9869157c711fe11263c95d74eDavid Li          array->type->element_type()->is_sampler() &&
15851591693c7b415e9869157c711fe11263c95d74eDavid Li          const_index == NULL) {
15861591693c7b415e9869157c711fe11263c95d74eDavid Li
15871591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (state->language_version == 100) {
15881591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_warning(&loc, state,
15891591693c7b415e9869157c711fe11263c95d74eDavid Li			       "sampler arrays indexed with non-constant "
15901591693c7b415e9869157c711fe11263c95d74eDavid Li			       "expressions is optional in GLSL ES 1.00");
15911591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if (state->language_version < 130) {
15921591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_warning(&loc, state,
15931591693c7b415e9869157c711fe11263c95d74eDavid Li			       "sampler arrays indexed with non-constant "
15941591693c7b415e9869157c711fe11263c95d74eDavid Li			       "expressions is forbidden in GLSL 1.30 and "
15951591693c7b415e9869157c711fe11263c95d74eDavid Li			       "later");
15961591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else {
15971591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(&loc, state,
15981591693c7b415e9869157c711fe11263c95d74eDavid Li			     "sampler arrays indexed with non-constant "
15991591693c7b415e9869157c711fe11263c95d74eDavid Li			     "expressions is forbidden in GLSL 1.30 and "
16001591693c7b415e9869157c711fe11263c95d74eDavid Li			     "later");
16011591693c7b415e9869157c711fe11263c95d74eDavid Li	    error_emitted = true;
16021591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
16031591693c7b415e9869157c711fe11263c95d74eDavid Li      }
16041591693c7b415e9869157c711fe11263c95d74eDavid Li
16051591693c7b415e9869157c711fe11263c95d74eDavid Li      if (error_emitted)
16061591693c7b415e9869157c711fe11263c95d74eDavid Li	 result->type = glsl_type::error_type;
16071591693c7b415e9869157c711fe11263c95d74eDavid Li
16081591693c7b415e9869157c711fe11263c95d74eDavid Li      type = result->type;
16091591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
16101591693c7b415e9869157c711fe11263c95d74eDavid Li   }
16111591693c7b415e9869157c711fe11263c95d74eDavid Li
16121591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_function_call:
16131591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Should *NEVER* get here.  ast_function_call should always be handled
16141591693c7b415e9869157c711fe11263c95d74eDavid Li       * by ast_function_expression::hir.
16151591693c7b415e9869157c711fe11263c95d74eDavid Li       */
16161591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(0);
16171591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
16181591693c7b415e9869157c711fe11263c95d74eDavid Li
16191591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_identifier: {
16201591693c7b415e9869157c711fe11263c95d74eDavid Li      /* ast_identifier can appear several places in a full abstract syntax
16211591693c7b415e9869157c711fe11263c95d74eDavid Li       * tree.  This particular use must be at location specified in the grammar
16221591693c7b415e9869157c711fe11263c95d74eDavid Li       * as 'variable_identifier'.
16231591693c7b415e9869157c711fe11263c95d74eDavid Li       */
16241591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_variable *var =
16251591693c7b415e9869157c711fe11263c95d74eDavid Li	 state->symbols->get_variable(this->primary_expression.identifier);
16261591693c7b415e9869157c711fe11263c95d74eDavid Li
16271591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_dereference_variable(var);
16281591693c7b415e9869157c711fe11263c95d74eDavid Li
16291591693c7b415e9869157c711fe11263c95d74eDavid Li      if (var != NULL) {
16301591693c7b415e9869157c711fe11263c95d74eDavid Li	 type = result->type;
16311591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
16321591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state, "`%s' undeclared",
16331591693c7b415e9869157c711fe11263c95d74eDavid Li			  this->primary_expression.identifier);
16341591693c7b415e9869157c711fe11263c95d74eDavid Li
16351591693c7b415e9869157c711fe11263c95d74eDavid Li	 error_emitted = true;
16361591693c7b415e9869157c711fe11263c95d74eDavid Li      }
16371591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
16381591693c7b415e9869157c711fe11263c95d74eDavid Li   }
16391591693c7b415e9869157c711fe11263c95d74eDavid Li
16401591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_int_constant:
16411591693c7b415e9869157c711fe11263c95d74eDavid Li      type = glsl_type::int_type;
16421591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_constant(this->primary_expression.int_constant);
16431591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
16441591693c7b415e9869157c711fe11263c95d74eDavid Li
16451591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_uint_constant:
16461591693c7b415e9869157c711fe11263c95d74eDavid Li      type = glsl_type::uint_type;
16471591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_constant(this->primary_expression.uint_constant);
16481591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
16491591693c7b415e9869157c711fe11263c95d74eDavid Li
16501591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_float_constant:
16511591693c7b415e9869157c711fe11263c95d74eDavid Li      type = glsl_type::float_type;
16521591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_constant(this->primary_expression.float_constant);
16531591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
16541591693c7b415e9869157c711fe11263c95d74eDavid Li
16551591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_bool_constant:
16561591693c7b415e9869157c711fe11263c95d74eDavid Li      type = glsl_type::bool_type;
16571591693c7b415e9869157c711fe11263c95d74eDavid Li      result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
16581591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
16591591693c7b415e9869157c711fe11263c95d74eDavid Li
16601591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_sequence: {
16611591693c7b415e9869157c711fe11263c95d74eDavid Li      /* It should not be possible to generate a sequence in the AST without
16621591693c7b415e9869157c711fe11263c95d74eDavid Li       * any expressions in it.
16631591693c7b415e9869157c711fe11263c95d74eDavid Li       */
16641591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(!this->expressions.is_empty());
16651591693c7b415e9869157c711fe11263c95d74eDavid Li
16661591693c7b415e9869157c711fe11263c95d74eDavid Li      /* The r-value of a sequence is the last expression in the sequence.  If
16671591693c7b415e9869157c711fe11263c95d74eDavid Li       * the other expressions in the sequence do not have side-effects (and
16681591693c7b415e9869157c711fe11263c95d74eDavid Li       * therefore add instructions to the instruction list), they get dropped
16691591693c7b415e9869157c711fe11263c95d74eDavid Li       * on the floor.
16701591693c7b415e9869157c711fe11263c95d74eDavid Li       */
16711591693c7b415e9869157c711fe11263c95d74eDavid Li      foreach_list_typed (ast_node, ast, link, &this->expressions)
16721591693c7b415e9869157c711fe11263c95d74eDavid Li	 result = ast->hir(instructions, state);
16731591693c7b415e9869157c711fe11263c95d74eDavid Li
16741591693c7b415e9869157c711fe11263c95d74eDavid Li      type = result->type;
16751591693c7b415e9869157c711fe11263c95d74eDavid Li
16761591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Any errors should have already been emitted in the loop above.
16771591693c7b415e9869157c711fe11263c95d74eDavid Li       */
16781591693c7b415e9869157c711fe11263c95d74eDavid Li      error_emitted = true;
16791591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
16801591693c7b415e9869157c711fe11263c95d74eDavid Li   }
16811591693c7b415e9869157c711fe11263c95d74eDavid Li   }
16821591693c7b415e9869157c711fe11263c95d74eDavid Li
16831591693c7b415e9869157c711fe11263c95d74eDavid Li   if (type->is_error() && !error_emitted)
16841591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(& loc, state, "type mismatch");
16851591693c7b415e9869157c711fe11263c95d74eDavid Li
16861591693c7b415e9869157c711fe11263c95d74eDavid Li   return result;
16871591693c7b415e9869157c711fe11263c95d74eDavid Li}
16881591693c7b415e9869157c711fe11263c95d74eDavid Li
16891591693c7b415e9869157c711fe11263c95d74eDavid Li
16901591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
16911591693c7b415e9869157c711fe11263c95d74eDavid Liast_expression_statement::hir(exec_list *instructions,
16921591693c7b415e9869157c711fe11263c95d74eDavid Li			      struct _mesa_glsl_parse_state *state)
16931591693c7b415e9869157c711fe11263c95d74eDavid Li{
16941591693c7b415e9869157c711fe11263c95d74eDavid Li   /* It is possible to have expression statements that don't have an
16951591693c7b415e9869157c711fe11263c95d74eDavid Li    * expression.  This is the solitary semicolon:
16961591693c7b415e9869157c711fe11263c95d74eDavid Li    *
16971591693c7b415e9869157c711fe11263c95d74eDavid Li    * for (i = 0; i < 5; i++)
16981591693c7b415e9869157c711fe11263c95d74eDavid Li    *     ;
16991591693c7b415e9869157c711fe11263c95d74eDavid Li    *
17001591693c7b415e9869157c711fe11263c95d74eDavid Li    * In this case the expression will be NULL.  Test for NULL and don't do
17011591693c7b415e9869157c711fe11263c95d74eDavid Li    * anything in that case.
17021591693c7b415e9869157c711fe11263c95d74eDavid Li    */
17031591693c7b415e9869157c711fe11263c95d74eDavid Li   if (expression != NULL)
17041591693c7b415e9869157c711fe11263c95d74eDavid Li      expression->hir(instructions, state);
17051591693c7b415e9869157c711fe11263c95d74eDavid Li
17061591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Statements do not have r-values.
17071591693c7b415e9869157c711fe11263c95d74eDavid Li    */
17081591693c7b415e9869157c711fe11263c95d74eDavid Li   return NULL;
17091591693c7b415e9869157c711fe11263c95d74eDavid Li}
17101591693c7b415e9869157c711fe11263c95d74eDavid Li
17111591693c7b415e9869157c711fe11263c95d74eDavid Li
17121591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
17131591693c7b415e9869157c711fe11263c95d74eDavid Liast_compound_statement::hir(exec_list *instructions,
17141591693c7b415e9869157c711fe11263c95d74eDavid Li			    struct _mesa_glsl_parse_state *state)
17151591693c7b415e9869157c711fe11263c95d74eDavid Li{
17161591693c7b415e9869157c711fe11263c95d74eDavid Li   if (new_scope)
17171591693c7b415e9869157c711fe11263c95d74eDavid Li      state->symbols->push_scope();
17181591693c7b415e9869157c711fe11263c95d74eDavid Li
17191591693c7b415e9869157c711fe11263c95d74eDavid Li   foreach_list_typed (ast_node, ast, link, &this->statements)
17201591693c7b415e9869157c711fe11263c95d74eDavid Li      ast->hir(instructions, state);
17211591693c7b415e9869157c711fe11263c95d74eDavid Li
17221591693c7b415e9869157c711fe11263c95d74eDavid Li   if (new_scope)
17231591693c7b415e9869157c711fe11263c95d74eDavid Li      state->symbols->pop_scope();
17241591693c7b415e9869157c711fe11263c95d74eDavid Li
17251591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Compound statements do not have r-values.
17261591693c7b415e9869157c711fe11263c95d74eDavid Li    */
17271591693c7b415e9869157c711fe11263c95d74eDavid Li   return NULL;
17281591693c7b415e9869157c711fe11263c95d74eDavid Li}
17291591693c7b415e9869157c711fe11263c95d74eDavid Li
17301591693c7b415e9869157c711fe11263c95d74eDavid Li
17311591693c7b415e9869157c711fe11263c95d74eDavid Listatic const glsl_type *
17321591693c7b415e9869157c711fe11263c95d74eDavid Liprocess_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
17331591693c7b415e9869157c711fe11263c95d74eDavid Li		   struct _mesa_glsl_parse_state *state)
17341591693c7b415e9869157c711fe11263c95d74eDavid Li{
17351591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned length = 0;
17361591693c7b415e9869157c711fe11263c95d74eDavid Li
17371591693c7b415e9869157c711fe11263c95d74eDavid Li   /* FINISHME: Reject delcarations of multidimensional arrays. */
17381591693c7b415e9869157c711fe11263c95d74eDavid Li
17391591693c7b415e9869157c711fe11263c95d74eDavid Li   if (array_size != NULL) {
17401591693c7b415e9869157c711fe11263c95d74eDavid Li      exec_list dummy_instructions;
17411591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
17421591693c7b415e9869157c711fe11263c95d74eDavid Li      YYLTYPE loc = array_size->get_location();
17431591693c7b415e9869157c711fe11263c95d74eDavid Li
17441591693c7b415e9869157c711fe11263c95d74eDavid Li      /* FINISHME: Verify that the grammar forbids side-effects in array
17451591693c7b415e9869157c711fe11263c95d74eDavid Li       * FINISHME: sizes.   i.e., 'vec4 [x = 12] data'
17461591693c7b415e9869157c711fe11263c95d74eDavid Li       */
17471591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(dummy_instructions.is_empty());
17481591693c7b415e9869157c711fe11263c95d74eDavid Li
17491591693c7b415e9869157c711fe11263c95d74eDavid Li      if (ir != NULL) {
17501591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (!ir->type->is_integer()) {
17511591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state, "array size must be integer type");
17521591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if (!ir->type->is_scalar()) {
17531591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state, "array size must be scalar type");
17541591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else {
17551591693c7b415e9869157c711fe11263c95d74eDavid Li	    ir_constant *const size = ir->constant_expression_value();
17561591693c7b415e9869157c711fe11263c95d74eDavid Li
17571591693c7b415e9869157c711fe11263c95d74eDavid Li	    if (size == NULL) {
17581591693c7b415e9869157c711fe11263c95d74eDavid Li	       _mesa_glsl_error(& loc, state, "array size must be a "
17591591693c7b415e9869157c711fe11263c95d74eDavid Li				"constant valued expression");
17601591693c7b415e9869157c711fe11263c95d74eDavid Li	    } else if (size->value.i[0] <= 0) {
17611591693c7b415e9869157c711fe11263c95d74eDavid Li	       _mesa_glsl_error(& loc, state, "array size must be > 0");
17621591693c7b415e9869157c711fe11263c95d74eDavid Li	    } else {
17631591693c7b415e9869157c711fe11263c95d74eDavid Li	       assert(size->type == ir->type);
17641591693c7b415e9869157c711fe11263c95d74eDavid Li	       length = size->value.u[0];
17651591693c7b415e9869157c711fe11263c95d74eDavid Li	    }
17661591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
17671591693c7b415e9869157c711fe11263c95d74eDavid Li      }
17681591693c7b415e9869157c711fe11263c95d74eDavid Li   } else if (state->es_shader) {
17691591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
17701591693c7b415e9869157c711fe11263c95d74eDavid Li       * array declarations have been removed from the language.
17711591693c7b415e9869157c711fe11263c95d74eDavid Li       */
17721591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state, "unsized array declarations are not "
17731591693c7b415e9869157c711fe11263c95d74eDavid Li		       "allowed in GLSL ES 1.00.");
17741591693c7b415e9869157c711fe11263c95d74eDavid Li   }
17751591693c7b415e9869157c711fe11263c95d74eDavid Li
17761591693c7b415e9869157c711fe11263c95d74eDavid Li   return glsl_type::get_array_instance(base, length);
17771591693c7b415e9869157c711fe11263c95d74eDavid Li}
17781591693c7b415e9869157c711fe11263c95d74eDavid Li
17791591693c7b415e9869157c711fe11263c95d74eDavid Li
17801591693c7b415e9869157c711fe11263c95d74eDavid Liconst glsl_type *
17811591693c7b415e9869157c711fe11263c95d74eDavid Liast_type_specifier::glsl_type(const char **name,
17821591693c7b415e9869157c711fe11263c95d74eDavid Li			      struct _mesa_glsl_parse_state *state) const
17831591693c7b415e9869157c711fe11263c95d74eDavid Li{
17841591693c7b415e9869157c711fe11263c95d74eDavid Li   const struct glsl_type *type;
17851591693c7b415e9869157c711fe11263c95d74eDavid Li
17861591693c7b415e9869157c711fe11263c95d74eDavid Li   type = state->symbols->get_type(this->type_name);
17871591693c7b415e9869157c711fe11263c95d74eDavid Li   *name = this->type_name;
17881591693c7b415e9869157c711fe11263c95d74eDavid Li
17891591693c7b415e9869157c711fe11263c95d74eDavid Li   if (this->is_array) {
17901591693c7b415e9869157c711fe11263c95d74eDavid Li      YYLTYPE loc = this->get_location();
17911591693c7b415e9869157c711fe11263c95d74eDavid Li      type = process_array_type(&loc, type, this->array_size, state);
17921591693c7b415e9869157c711fe11263c95d74eDavid Li   }
17931591693c7b415e9869157c711fe11263c95d74eDavid Li
17941591693c7b415e9869157c711fe11263c95d74eDavid Li   return type;
17951591693c7b415e9869157c711fe11263c95d74eDavid Li}
17961591693c7b415e9869157c711fe11263c95d74eDavid Li
17971591693c7b415e9869157c711fe11263c95d74eDavid Li
17981591693c7b415e9869157c711fe11263c95d74eDavid Listatic void
17991591693c7b415e9869157c711fe11263c95d74eDavid Liapply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
18001591693c7b415e9869157c711fe11263c95d74eDavid Li				 ir_variable *var,
18011591693c7b415e9869157c711fe11263c95d74eDavid Li				 struct _mesa_glsl_parse_state *state,
18021591693c7b415e9869157c711fe11263c95d74eDavid Li				 YYLTYPE *loc)
18031591693c7b415e9869157c711fe11263c95d74eDavid Li{
18041591693c7b415e9869157c711fe11263c95d74eDavid Li   if (qual->flags.q.invariant)
18051591693c7b415e9869157c711fe11263c95d74eDavid Li      var->invariant = 1;
18061591693c7b415e9869157c711fe11263c95d74eDavid Li
18071591693c7b415e9869157c711fe11263c95d74eDavid Li   /* FINISHME: Mark 'in' variables at global scope as read-only. */
18081591693c7b415e9869157c711fe11263c95d74eDavid Li   if (qual->flags.q.constant || qual->flags.q.attribute
18091591693c7b415e9869157c711fe11263c95d74eDavid Li       || qual->flags.q.uniform
18101591693c7b415e9869157c711fe11263c95d74eDavid Li       || (qual->flags.q.varying && (state->target == fragment_shader)))
18111591693c7b415e9869157c711fe11263c95d74eDavid Li      var->read_only = 1;
18121591693c7b415e9869157c711fe11263c95d74eDavid Li
18131591693c7b415e9869157c711fe11263c95d74eDavid Li   if (qual->flags.q.centroid)
18141591693c7b415e9869157c711fe11263c95d74eDavid Li      var->centroid = 1;
18151591693c7b415e9869157c711fe11263c95d74eDavid Li
18161591693c7b415e9869157c711fe11263c95d74eDavid Li   if (qual->flags.q.attribute && state->target != vertex_shader) {
18171591693c7b415e9869157c711fe11263c95d74eDavid Li      var->type = glsl_type::error_type;
18181591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state,
18191591693c7b415e9869157c711fe11263c95d74eDavid Li		       "`attribute' variables may not be declared in the "
18201591693c7b415e9869157c711fe11263c95d74eDavid Li		       "%s shader",
18211591693c7b415e9869157c711fe11263c95d74eDavid Li		       _mesa_glsl_shader_target_name(state->target));
18221591693c7b415e9869157c711fe11263c95d74eDavid Li   }
18231591693c7b415e9869157c711fe11263c95d74eDavid Li
18241591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
18251591693c7b415e9869157c711fe11263c95d74eDavid Li    *
18261591693c7b415e9869157c711fe11263c95d74eDavid Li    *     "The varying qualifier can be used only with the data types
18271591693c7b415e9869157c711fe11263c95d74eDavid Li    *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
18281591693c7b415e9869157c711fe11263c95d74eDavid Li    *     these."
18291591693c7b415e9869157c711fe11263c95d74eDavid Li    */
18301591693c7b415e9869157c711fe11263c95d74eDavid Li   if (qual->flags.q.varying) {
18311591693c7b415e9869157c711fe11263c95d74eDavid Li      const glsl_type *non_array_type;
18321591693c7b415e9869157c711fe11263c95d74eDavid Li
18331591693c7b415e9869157c711fe11263c95d74eDavid Li      if (var->type && var->type->is_array())
18341591693c7b415e9869157c711fe11263c95d74eDavid Li	 non_array_type = var->type->fields.array;
18351591693c7b415e9869157c711fe11263c95d74eDavid Li      else
18361591693c7b415e9869157c711fe11263c95d74eDavid Li	 non_array_type = var->type;
18371591693c7b415e9869157c711fe11263c95d74eDavid Li
18381591693c7b415e9869157c711fe11263c95d74eDavid Li      if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
18391591693c7b415e9869157c711fe11263c95d74eDavid Li	 var->type = glsl_type::error_type;
18401591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(loc, state,
18411591693c7b415e9869157c711fe11263c95d74eDavid Li			  "varying variables must be of base type float");
18421591693c7b415e9869157c711fe11263c95d74eDavid Li      }
18431591693c7b415e9869157c711fe11263c95d74eDavid Li   }
18441591693c7b415e9869157c711fe11263c95d74eDavid Li
18451591693c7b415e9869157c711fe11263c95d74eDavid Li   /* If there is no qualifier that changes the mode of the variable, leave
18461591693c7b415e9869157c711fe11263c95d74eDavid Li    * the setting alone.
18471591693c7b415e9869157c711fe11263c95d74eDavid Li    */
18481591693c7b415e9869157c711fe11263c95d74eDavid Li   if (qual->flags.q.in && qual->flags.q.out)
18491591693c7b415e9869157c711fe11263c95d74eDavid Li      var->mode = ir_var_inout;
18501591693c7b415e9869157c711fe11263c95d74eDavid Li   else if (qual->flags.q.attribute || qual->flags.q.in
18511591693c7b415e9869157c711fe11263c95d74eDavid Li	    || (qual->flags.q.varying && (state->target == fragment_shader)))
18521591693c7b415e9869157c711fe11263c95d74eDavid Li      var->mode = ir_var_in;
18531591693c7b415e9869157c711fe11263c95d74eDavid Li   else if (qual->flags.q.out
18541591693c7b415e9869157c711fe11263c95d74eDavid Li	    || (qual->flags.q.varying && (state->target == vertex_shader)))
18551591693c7b415e9869157c711fe11263c95d74eDavid Li      var->mode = ir_var_out;
18561591693c7b415e9869157c711fe11263c95d74eDavid Li   else if (qual->flags.q.uniform)
18571591693c7b415e9869157c711fe11263c95d74eDavid Li      var->mode = ir_var_uniform;
18581591693c7b415e9869157c711fe11263c95d74eDavid Li
18591591693c7b415e9869157c711fe11263c95d74eDavid Li   if (qual->flags.q.flat)
18601591693c7b415e9869157c711fe11263c95d74eDavid Li      var->interpolation = ir_var_flat;
18611591693c7b415e9869157c711fe11263c95d74eDavid Li   else if (qual->flags.q.noperspective)
18621591693c7b415e9869157c711fe11263c95d74eDavid Li      var->interpolation = ir_var_noperspective;
18631591693c7b415e9869157c711fe11263c95d74eDavid Li   else
18641591693c7b415e9869157c711fe11263c95d74eDavid Li      var->interpolation = ir_var_smooth;
18651591693c7b415e9869157c711fe11263c95d74eDavid Li
18661591693c7b415e9869157c711fe11263c95d74eDavid Li   var->pixel_center_integer = qual->flags.q.pixel_center_integer;
18671591693c7b415e9869157c711fe11263c95d74eDavid Li   var->origin_upper_left = qual->flags.q.origin_upper_left;
18681591693c7b415e9869157c711fe11263c95d74eDavid Li   if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
18691591693c7b415e9869157c711fe11263c95d74eDavid Li       && (strcmp(var->name, "gl_FragCoord") != 0)) {
18701591693c7b415e9869157c711fe11263c95d74eDavid Li      const char *const qual_string = (qual->flags.q.origin_upper_left)
18711591693c7b415e9869157c711fe11263c95d74eDavid Li	 ? "origin_upper_left" : "pixel_center_integer";
18721591693c7b415e9869157c711fe11263c95d74eDavid Li
18731591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(loc, state,
18741591693c7b415e9869157c711fe11263c95d74eDavid Li		       "layout qualifier `%s' can only be applied to "
18751591693c7b415e9869157c711fe11263c95d74eDavid Li		       "fragment shader input `gl_FragCoord'",
18761591693c7b415e9869157c711fe11263c95d74eDavid Li		       qual_string);
18771591693c7b415e9869157c711fe11263c95d74eDavid Li   }
18781591693c7b415e9869157c711fe11263c95d74eDavid Li
18791591693c7b415e9869157c711fe11263c95d74eDavid Li   if (qual->flags.q.explicit_location) {
18801591693c7b415e9869157c711fe11263c95d74eDavid Li      const bool global_scope = (state->current_function == NULL);
18811591693c7b415e9869157c711fe11263c95d74eDavid Li      bool fail = false;
18821591693c7b415e9869157c711fe11263c95d74eDavid Li      const char *string = "";
18831591693c7b415e9869157c711fe11263c95d74eDavid Li
18841591693c7b415e9869157c711fe11263c95d74eDavid Li      /* In the vertex shader only shader inputs can be given explicit
18851591693c7b415e9869157c711fe11263c95d74eDavid Li       * locations.
18861591693c7b415e9869157c711fe11263c95d74eDavid Li       *
18871591693c7b415e9869157c711fe11263c95d74eDavid Li       * In the fragment shader only shader outputs can be given explicit
18881591693c7b415e9869157c711fe11263c95d74eDavid Li       * locations.
18891591693c7b415e9869157c711fe11263c95d74eDavid Li       */
18901591693c7b415e9869157c711fe11263c95d74eDavid Li      switch (state->target) {
18911591693c7b415e9869157c711fe11263c95d74eDavid Li      case vertex_shader:
18921591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (!global_scope || (var->mode != ir_var_in)) {
18931591693c7b415e9869157c711fe11263c95d74eDavid Li	    fail = true;
18941591693c7b415e9869157c711fe11263c95d74eDavid Li	    string = "input";
18951591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
18961591693c7b415e9869157c711fe11263c95d74eDavid Li	 break;
18971591693c7b415e9869157c711fe11263c95d74eDavid Li
18981591693c7b415e9869157c711fe11263c95d74eDavid Li      case geometry_shader:
18991591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(loc, state,
19001591693c7b415e9869157c711fe11263c95d74eDavid Li			  "geometry shader variables cannot be given "
19011591693c7b415e9869157c711fe11263c95d74eDavid Li			  "explicit locations\n");
19021591693c7b415e9869157c711fe11263c95d74eDavid Li	 break;
19031591693c7b415e9869157c711fe11263c95d74eDavid Li
19041591693c7b415e9869157c711fe11263c95d74eDavid Li      case fragment_shader:
19051591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (!global_scope || (var->mode != ir_var_in)) {
19061591693c7b415e9869157c711fe11263c95d74eDavid Li	    fail = true;
19071591693c7b415e9869157c711fe11263c95d74eDavid Li	    string = "output";
19081591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
19091591693c7b415e9869157c711fe11263c95d74eDavid Li	 break;
19101591693c7b415e9869157c711fe11263c95d74eDavid Li      };
19111591693c7b415e9869157c711fe11263c95d74eDavid Li
19121591693c7b415e9869157c711fe11263c95d74eDavid Li      if (fail) {
19131591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(loc, state,
19141591693c7b415e9869157c711fe11263c95d74eDavid Li			  "only %s shader %s variables can be given an "
19151591693c7b415e9869157c711fe11263c95d74eDavid Li			  "explicit location\n",
19161591693c7b415e9869157c711fe11263c95d74eDavid Li			  _mesa_glsl_shader_target_name(state->target),
19171591693c7b415e9869157c711fe11263c95d74eDavid Li			  string);
19181591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
19191591693c7b415e9869157c711fe11263c95d74eDavid Li	 var->explicit_location = true;
19201591693c7b415e9869157c711fe11263c95d74eDavid Li
19211591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* This bit of silliness is needed because invalid explicit locations
19221591693c7b415e9869157c711fe11263c95d74eDavid Li	  * are supposed to be flagged during linking.  Small negative values
19231591693c7b415e9869157c711fe11263c95d74eDavid Li	  * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
19241591693c7b415e9869157c711fe11263c95d74eDavid Li	  * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
19251591693c7b415e9869157c711fe11263c95d74eDavid Li	  * The linker needs to be able to differentiate these cases.  This
19261591693c7b415e9869157c711fe11263c95d74eDavid Li	  * ensures that negative values stay negative.
19271591693c7b415e9869157c711fe11263c95d74eDavid Li	  */
19281591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (qual->location >= 0) {
19291591693c7b415e9869157c711fe11263c95d74eDavid Li	    var->location = (state->target == vertex_shader)
19301591693c7b415e9869157c711fe11263c95d74eDavid Li	       ? (qual->location + VERT_ATTRIB_GENERIC0)
19311591693c7b415e9869157c711fe11263c95d74eDavid Li	       : (qual->location + FRAG_RESULT_DATA0);
19321591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else {
19331591693c7b415e9869157c711fe11263c95d74eDavid Li	    var->location = qual->location;
19341591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
19351591693c7b415e9869157c711fe11263c95d74eDavid Li      }
19361591693c7b415e9869157c711fe11263c95d74eDavid Li   }
19371591693c7b415e9869157c711fe11263c95d74eDavid Li
19381591693c7b415e9869157c711fe11263c95d74eDavid Li   if (var->type->is_array() && state->language_version != 110) {
19391591693c7b415e9869157c711fe11263c95d74eDavid Li      var->array_lvalue = true;
19401591693c7b415e9869157c711fe11263c95d74eDavid Li   }
19411591693c7b415e9869157c711fe11263c95d74eDavid Li}
19421591693c7b415e9869157c711fe11263c95d74eDavid Li
19431591693c7b415e9869157c711fe11263c95d74eDavid Li
19441591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
19451591693c7b415e9869157c711fe11263c95d74eDavid Liast_declarator_list::hir(exec_list *instructions,
19461591693c7b415e9869157c711fe11263c95d74eDavid Li			 struct _mesa_glsl_parse_state *state)
19471591693c7b415e9869157c711fe11263c95d74eDavid Li{
19481591693c7b415e9869157c711fe11263c95d74eDavid Li   void *ctx = state;
19491591693c7b415e9869157c711fe11263c95d74eDavid Li   const struct glsl_type *decl_type;
19501591693c7b415e9869157c711fe11263c95d74eDavid Li   const char *type_name = NULL;
19511591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_rvalue *result = NULL;
19521591693c7b415e9869157c711fe11263c95d74eDavid Li   YYLTYPE loc = this->get_location();
19531591693c7b415e9869157c711fe11263c95d74eDavid Li
19541591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
19551591693c7b415e9869157c711fe11263c95d74eDavid Li    *
19561591693c7b415e9869157c711fe11263c95d74eDavid Li    *     "To ensure that a particular output variable is invariant, it is
19571591693c7b415e9869157c711fe11263c95d74eDavid Li    *     necessary to use the invariant qualifier. It can either be used to
19581591693c7b415e9869157c711fe11263c95d74eDavid Li    *     qualify a previously declared variable as being invariant
19591591693c7b415e9869157c711fe11263c95d74eDavid Li    *
19601591693c7b415e9869157c711fe11263c95d74eDavid Li    *         invariant gl_Position; // make existing gl_Position be invariant"
19611591693c7b415e9869157c711fe11263c95d74eDavid Li    *
19621591693c7b415e9869157c711fe11263c95d74eDavid Li    * In these cases the parser will set the 'invariant' flag in the declarator
19631591693c7b415e9869157c711fe11263c95d74eDavid Li    * list, and the type will be NULL.
19641591693c7b415e9869157c711fe11263c95d74eDavid Li    */
19651591693c7b415e9869157c711fe11263c95d74eDavid Li   if (this->invariant) {
19661591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(this->type == NULL);
19671591693c7b415e9869157c711fe11263c95d74eDavid Li
19681591693c7b415e9869157c711fe11263c95d74eDavid Li      if (state->current_function != NULL) {
19691591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state,
19701591693c7b415e9869157c711fe11263c95d74eDavid Li			  "All uses of `invariant' keyword must be at global "
19711591693c7b415e9869157c711fe11263c95d74eDavid Li			  "scope\n");
19721591693c7b415e9869157c711fe11263c95d74eDavid Li      }
19731591693c7b415e9869157c711fe11263c95d74eDavid Li
19741591693c7b415e9869157c711fe11263c95d74eDavid Li      foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
19751591693c7b415e9869157c711fe11263c95d74eDavid Li	 assert(!decl->is_array);
19761591693c7b415e9869157c711fe11263c95d74eDavid Li	 assert(decl->array_size == NULL);
19771591693c7b415e9869157c711fe11263c95d74eDavid Li	 assert(decl->initializer == NULL);
19781591693c7b415e9869157c711fe11263c95d74eDavid Li
19791591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_variable *const earlier =
19801591693c7b415e9869157c711fe11263c95d74eDavid Li	    state->symbols->get_variable(decl->identifier);
19811591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (earlier == NULL) {
19821591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
19831591693c7b415e9869157c711fe11263c95d74eDavid Li			     "Undeclared variable `%s' cannot be marked "
19841591693c7b415e9869157c711fe11263c95d74eDavid Li			     "invariant\n", decl->identifier);
19851591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if ((state->target == vertex_shader)
19861591693c7b415e9869157c711fe11263c95d74eDavid Li	       && (earlier->mode != ir_var_out)) {
19871591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
19881591693c7b415e9869157c711fe11263c95d74eDavid Li			     "`%s' cannot be marked invariant, vertex shader "
19891591693c7b415e9869157c711fe11263c95d74eDavid Li			     "outputs only\n", decl->identifier);
19901591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if ((state->target == fragment_shader)
19911591693c7b415e9869157c711fe11263c95d74eDavid Li	       && (earlier->mode != ir_var_in)) {
19921591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
19931591693c7b415e9869157c711fe11263c95d74eDavid Li			     "`%s' cannot be marked invariant, fragment shader "
19941591693c7b415e9869157c711fe11263c95d74eDavid Li			     "inputs only\n", decl->identifier);
19951591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else {
19961591693c7b415e9869157c711fe11263c95d74eDavid Li	    earlier->invariant = true;
19971591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
19981591693c7b415e9869157c711fe11263c95d74eDavid Li      }
19991591693c7b415e9869157c711fe11263c95d74eDavid Li
20001591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Invariant redeclarations do not have r-values.
20011591693c7b415e9869157c711fe11263c95d74eDavid Li       */
20021591693c7b415e9869157c711fe11263c95d74eDavid Li      return NULL;
20031591693c7b415e9869157c711fe11263c95d74eDavid Li   }
20041591693c7b415e9869157c711fe11263c95d74eDavid Li
20051591693c7b415e9869157c711fe11263c95d74eDavid Li   assert(this->type != NULL);
20061591693c7b415e9869157c711fe11263c95d74eDavid Li   assert(!this->invariant);
20071591693c7b415e9869157c711fe11263c95d74eDavid Li
20081591693c7b415e9869157c711fe11263c95d74eDavid Li   /* The type specifier may contain a structure definition.  Process that
20091591693c7b415e9869157c711fe11263c95d74eDavid Li    * before any of the variable declarations.
20101591693c7b415e9869157c711fe11263c95d74eDavid Li    */
20111591693c7b415e9869157c711fe11263c95d74eDavid Li   (void) this->type->specifier->hir(instructions, state);
20121591693c7b415e9869157c711fe11263c95d74eDavid Li
20131591693c7b415e9869157c711fe11263c95d74eDavid Li   decl_type = this->type->specifier->glsl_type(& type_name, state);
20141591693c7b415e9869157c711fe11263c95d74eDavid Li   if (this->declarations.is_empty()) {
20151591693c7b415e9869157c711fe11263c95d74eDavid Li      /* The only valid case where the declaration list can be empty is when
20161591693c7b415e9869157c711fe11263c95d74eDavid Li       * the declaration is setting the default precision of a built-in type
20171591693c7b415e9869157c711fe11263c95d74eDavid Li       * (e.g., 'precision highp vec4;').
20181591693c7b415e9869157c711fe11263c95d74eDavid Li       */
20191591693c7b415e9869157c711fe11263c95d74eDavid Li
20201591693c7b415e9869157c711fe11263c95d74eDavid Li      if (decl_type != NULL) {
20211591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
20221591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state, "incomplete declaration");
20231591693c7b415e9869157c711fe11263c95d74eDavid Li      }
20241591693c7b415e9869157c711fe11263c95d74eDavid Li   }
20251591693c7b415e9869157c711fe11263c95d74eDavid Li
20261591693c7b415e9869157c711fe11263c95d74eDavid Li   foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
20271591693c7b415e9869157c711fe11263c95d74eDavid Li      const struct glsl_type *var_type;
20281591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_variable *var;
20291591693c7b415e9869157c711fe11263c95d74eDavid Li
20301591693c7b415e9869157c711fe11263c95d74eDavid Li      /* FINISHME: Emit a warning if a variable declaration shadows a
20311591693c7b415e9869157c711fe11263c95d74eDavid Li       * FINISHME: declaration at a higher scope.
20321591693c7b415e9869157c711fe11263c95d74eDavid Li       */
20331591693c7b415e9869157c711fe11263c95d74eDavid Li
20341591693c7b415e9869157c711fe11263c95d74eDavid Li      if ((decl_type == NULL) || decl_type->is_void()) {
20351591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (type_name != NULL) {
20361591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
20371591693c7b415e9869157c711fe11263c95d74eDavid Li			     "invalid type `%s' in declaration of `%s'",
20381591693c7b415e9869157c711fe11263c95d74eDavid Li			     type_name, decl->identifier);
20391591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else {
20401591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
20411591693c7b415e9869157c711fe11263c95d74eDavid Li			     "invalid type in declaration of `%s'",
20421591693c7b415e9869157c711fe11263c95d74eDavid Li			     decl->identifier);
20431591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
20441591693c7b415e9869157c711fe11263c95d74eDavid Li	 continue;
20451591693c7b415e9869157c711fe11263c95d74eDavid Li      }
20461591693c7b415e9869157c711fe11263c95d74eDavid Li
20471591693c7b415e9869157c711fe11263c95d74eDavid Li      if (decl->is_array) {
20481591693c7b415e9869157c711fe11263c95d74eDavid Li	 var_type = process_array_type(&loc, decl_type, decl->array_size,
20491591693c7b415e9869157c711fe11263c95d74eDavid Li				       state);
20501591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
20511591693c7b415e9869157c711fe11263c95d74eDavid Li	 var_type = decl_type;
20521591693c7b415e9869157c711fe11263c95d74eDavid Li      }
20531591693c7b415e9869157c711fe11263c95d74eDavid Li
20541591693c7b415e9869157c711fe11263c95d74eDavid Li      var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
20551591693c7b415e9869157c711fe11263c95d74eDavid Li
20561591693c7b415e9869157c711fe11263c95d74eDavid Li      /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
20571591693c7b415e9869157c711fe11263c95d74eDavid Li       *
20581591693c7b415e9869157c711fe11263c95d74eDavid Li       *     "Global variables can only use the qualifiers const,
20591591693c7b415e9869157c711fe11263c95d74eDavid Li       *     attribute, uni form, or varying. Only one may be
20601591693c7b415e9869157c711fe11263c95d74eDavid Li       *     specified.
20611591693c7b415e9869157c711fe11263c95d74eDavid Li       *
20621591693c7b415e9869157c711fe11263c95d74eDavid Li       *     Local variables can only use the qualifier const."
20631591693c7b415e9869157c711fe11263c95d74eDavid Li       *
20641591693c7b415e9869157c711fe11263c95d74eDavid Li       * This is relaxed in GLSL 1.30.
20651591693c7b415e9869157c711fe11263c95d74eDavid Li       */
20661591693c7b415e9869157c711fe11263c95d74eDavid Li      if (state->language_version < 120) {
20671591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (this->type->qualifier.flags.q.out) {
20681591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
20691591693c7b415e9869157c711fe11263c95d74eDavid Li			     "`out' qualifier in declaration of `%s' "
20701591693c7b415e9869157c711fe11263c95d74eDavid Li			     "only valid for function parameters in GLSL 1.10.",
20711591693c7b415e9869157c711fe11263c95d74eDavid Li			     decl->identifier);
20721591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
20731591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (this->type->qualifier.flags.q.in) {
20741591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
20751591693c7b415e9869157c711fe11263c95d74eDavid Li			     "`in' qualifier in declaration of `%s' "
20761591693c7b415e9869157c711fe11263c95d74eDavid Li			     "only valid for function parameters in GLSL 1.10.",
20771591693c7b415e9869157c711fe11263c95d74eDavid Li			     decl->identifier);
20781591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
20791591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* FINISHME: Test for other invalid qualifiers. */
20801591693c7b415e9869157c711fe11263c95d74eDavid Li      }
20811591693c7b415e9869157c711fe11263c95d74eDavid Li
20821591693c7b415e9869157c711fe11263c95d74eDavid Li      apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
20831591693c7b415e9869157c711fe11263c95d74eDavid Li				       & loc);
20841591693c7b415e9869157c711fe11263c95d74eDavid Li
20851591693c7b415e9869157c711fe11263c95d74eDavid Li      if (this->type->qualifier.flags.q.invariant) {
20861591693c7b415e9869157c711fe11263c95d74eDavid Li	 if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
20871591693c7b415e9869157c711fe11263c95d74eDavid Li						   var->mode == ir_var_inout)) {
20881591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* FINISHME: Note that this doesn't work for invariant on
20891591693c7b415e9869157c711fe11263c95d74eDavid Li	     * a function signature outval
20901591693c7b415e9869157c711fe11263c95d74eDavid Li	     */
20911591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
20921591693c7b415e9869157c711fe11263c95d74eDavid Li			     "`%s' cannot be marked invariant, vertex shader "
20931591693c7b415e9869157c711fe11263c95d74eDavid Li			     "outputs only\n", var->name);
20941591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if ((state->target == fragment_shader) &&
20951591693c7b415e9869157c711fe11263c95d74eDavid Li		    !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
20961591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* FINISHME: Note that this doesn't work for invariant on
20971591693c7b415e9869157c711fe11263c95d74eDavid Li	     * a function signature inval
20981591693c7b415e9869157c711fe11263c95d74eDavid Li	     */
20991591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
21001591693c7b415e9869157c711fe11263c95d74eDavid Li			     "`%s' cannot be marked invariant, fragment shader "
21011591693c7b415e9869157c711fe11263c95d74eDavid Li			     "inputs only\n", var->name);
21021591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
21031591693c7b415e9869157c711fe11263c95d74eDavid Li      }
21041591693c7b415e9869157c711fe11263c95d74eDavid Li
21051591693c7b415e9869157c711fe11263c95d74eDavid Li      if (state->current_function != NULL) {
21061591693c7b415e9869157c711fe11263c95d74eDavid Li	 const char *mode = NULL;
21071591693c7b415e9869157c711fe11263c95d74eDavid Li	 const char *extra = "";
21081591693c7b415e9869157c711fe11263c95d74eDavid Li
21091591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* There is no need to check for 'inout' here because the parser will
21101591693c7b415e9869157c711fe11263c95d74eDavid Li	  * only allow that in function parameter lists.
21111591693c7b415e9869157c711fe11263c95d74eDavid Li	  */
21121591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (this->type->qualifier.flags.q.attribute) {
21131591693c7b415e9869157c711fe11263c95d74eDavid Li	    mode = "attribute";
21141591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if (this->type->qualifier.flags.q.uniform) {
21151591693c7b415e9869157c711fe11263c95d74eDavid Li	    mode = "uniform";
21161591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if (this->type->qualifier.flags.q.varying) {
21171591693c7b415e9869157c711fe11263c95d74eDavid Li	    mode = "varying";
21181591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if (this->type->qualifier.flags.q.in) {
21191591693c7b415e9869157c711fe11263c95d74eDavid Li	    mode = "in";
21201591693c7b415e9869157c711fe11263c95d74eDavid Li	    extra = " or in function parameter list";
21211591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if (this->type->qualifier.flags.q.out) {
21221591693c7b415e9869157c711fe11263c95d74eDavid Li	    mode = "out";
21231591693c7b415e9869157c711fe11263c95d74eDavid Li	    extra = " or in function parameter list";
21241591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
21251591693c7b415e9869157c711fe11263c95d74eDavid Li
21261591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (mode) {
21271591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
21281591693c7b415e9869157c711fe11263c95d74eDavid Li			     "%s variable `%s' must be declared at "
21291591693c7b415e9869157c711fe11263c95d74eDavid Li			     "global scope%s",
21301591693c7b415e9869157c711fe11263c95d74eDavid Li			     mode, var->name, extra);
21311591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
21321591693c7b415e9869157c711fe11263c95d74eDavid Li      } else if (var->mode == ir_var_in) {
21331591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (state->target == vertex_shader) {
21341591693c7b415e9869157c711fe11263c95d74eDavid Li	    bool error_emitted = false;
21351591693c7b415e9869157c711fe11263c95d74eDavid Li
21361591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
21371591693c7b415e9869157c711fe11263c95d74eDavid Li	     *
21381591693c7b415e9869157c711fe11263c95d74eDavid Li	     *    "Vertex shader inputs can only be float, floating-point
21391591693c7b415e9869157c711fe11263c95d74eDavid Li	     *    vectors, matrices, signed and unsigned integers and integer
21401591693c7b415e9869157c711fe11263c95d74eDavid Li	     *    vectors. Vertex shader inputs can also form arrays of these
21411591693c7b415e9869157c711fe11263c95d74eDavid Li	     *    types, but not structures."
21421591693c7b415e9869157c711fe11263c95d74eDavid Li	     *
21431591693c7b415e9869157c711fe11263c95d74eDavid Li	     * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
21441591693c7b415e9869157c711fe11263c95d74eDavid Li	     *
21451591693c7b415e9869157c711fe11263c95d74eDavid Li	     *    "Vertex shader inputs can only be float, floating-point
21461591693c7b415e9869157c711fe11263c95d74eDavid Li	     *    vectors, matrices, signed and unsigned integers and integer
21471591693c7b415e9869157c711fe11263c95d74eDavid Li	     *    vectors. They cannot be arrays or structures."
21481591693c7b415e9869157c711fe11263c95d74eDavid Li	     *
21491591693c7b415e9869157c711fe11263c95d74eDavid Li	     * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
21501591693c7b415e9869157c711fe11263c95d74eDavid Li	     *
21511591693c7b415e9869157c711fe11263c95d74eDavid Li	     *    "The attribute qualifier can be used only with float,
21521591693c7b415e9869157c711fe11263c95d74eDavid Li	     *    floating-point vectors, and matrices. Attribute variables
21531591693c7b415e9869157c711fe11263c95d74eDavid Li	     *    cannot be declared as arrays or structures."
21541591693c7b415e9869157c711fe11263c95d74eDavid Li	     */
21551591693c7b415e9869157c711fe11263c95d74eDavid Li	    const glsl_type *check_type = var->type->is_array()
21561591693c7b415e9869157c711fe11263c95d74eDavid Li	       ? var->type->fields.array : var->type;
21571591693c7b415e9869157c711fe11263c95d74eDavid Li
21581591693c7b415e9869157c711fe11263c95d74eDavid Li	    switch (check_type->base_type) {
21591591693c7b415e9869157c711fe11263c95d74eDavid Li	    case GLSL_TYPE_FLOAT:
21601591693c7b415e9869157c711fe11263c95d74eDavid Li	       break;
21611591693c7b415e9869157c711fe11263c95d74eDavid Li	    case GLSL_TYPE_UINT:
21621591693c7b415e9869157c711fe11263c95d74eDavid Li	    case GLSL_TYPE_INT:
21631591693c7b415e9869157c711fe11263c95d74eDavid Li	       if (state->language_version > 120)
21641591693c7b415e9869157c711fe11263c95d74eDavid Li		  break;
21651591693c7b415e9869157c711fe11263c95d74eDavid Li	       /* FALLTHROUGH */
21661591693c7b415e9869157c711fe11263c95d74eDavid Li	    default:
21671591693c7b415e9869157c711fe11263c95d74eDavid Li	       _mesa_glsl_error(& loc, state,
21681591693c7b415e9869157c711fe11263c95d74eDavid Li				"vertex shader input / attribute cannot have "
21691591693c7b415e9869157c711fe11263c95d74eDavid Li				"type %s`%s'",
21701591693c7b415e9869157c711fe11263c95d74eDavid Li				var->type->is_array() ? "array of " : "",
21711591693c7b415e9869157c711fe11263c95d74eDavid Li				check_type->name);
21721591693c7b415e9869157c711fe11263c95d74eDavid Li	       error_emitted = true;
21731591693c7b415e9869157c711fe11263c95d74eDavid Li	    }
21741591693c7b415e9869157c711fe11263c95d74eDavid Li
21751591693c7b415e9869157c711fe11263c95d74eDavid Li	    if (!error_emitted && (state->language_version <= 130)
21761591693c7b415e9869157c711fe11263c95d74eDavid Li		&& var->type->is_array()) {
21771591693c7b415e9869157c711fe11263c95d74eDavid Li	       _mesa_glsl_error(& loc, state,
21781591693c7b415e9869157c711fe11263c95d74eDavid Li				"vertex shader input / attribute cannot have "
21791591693c7b415e9869157c711fe11263c95d74eDavid Li				"array type");
21801591693c7b415e9869157c711fe11263c95d74eDavid Li	       error_emitted = true;
21811591693c7b415e9869157c711fe11263c95d74eDavid Li	    }
21821591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
21831591693c7b415e9869157c711fe11263c95d74eDavid Li      }
21841591693c7b415e9869157c711fe11263c95d74eDavid Li
21851591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Process the initializer and add its instructions to a temporary
21861591693c7b415e9869157c711fe11263c95d74eDavid Li       * list.  This list will be added to the instruction stream (below) after
21871591693c7b415e9869157c711fe11263c95d74eDavid Li       * the declaration is added.  This is done because in some cases (such as
21881591693c7b415e9869157c711fe11263c95d74eDavid Li       * redeclarations) the declaration may not actually be added to the
21891591693c7b415e9869157c711fe11263c95d74eDavid Li       * instruction stream.
21901591693c7b415e9869157c711fe11263c95d74eDavid Li       */
21911591693c7b415e9869157c711fe11263c95d74eDavid Li      exec_list initializer_instructions;
21921591693c7b415e9869157c711fe11263c95d74eDavid Li      if (decl->initializer != NULL) {
21931591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE initializer_loc = decl->initializer->get_location();
21941591693c7b415e9869157c711fe11263c95d74eDavid Li
21951591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
21961591693c7b415e9869157c711fe11263c95d74eDavid Li	  *
21971591693c7b415e9869157c711fe11263c95d74eDavid Li	  *    "All uniform variables are read-only and are initialized either
21981591693c7b415e9869157c711fe11263c95d74eDavid Li	  *    directly by an application via API commands, or indirectly by
21991591693c7b415e9869157c711fe11263c95d74eDavid Li	  *    OpenGL."
22001591693c7b415e9869157c711fe11263c95d74eDavid Li	  */
22011591693c7b415e9869157c711fe11263c95d74eDavid Li	 if ((state->language_version <= 110)
22021591693c7b415e9869157c711fe11263c95d74eDavid Li	     && (var->mode == ir_var_uniform)) {
22031591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& initializer_loc, state,
22041591693c7b415e9869157c711fe11263c95d74eDavid Li			     "cannot initialize uniforms in GLSL 1.10");
22051591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
22061591693c7b415e9869157c711fe11263c95d74eDavid Li
22071591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (var->type->is_sampler()) {
22081591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& initializer_loc, state,
22091591693c7b415e9869157c711fe11263c95d74eDavid Li			     "cannot initialize samplers");
22101591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
22111591693c7b415e9869157c711fe11263c95d74eDavid Li
22121591693c7b415e9869157c711fe11263c95d74eDavid Li	 if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
22131591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& initializer_loc, state,
22141591693c7b415e9869157c711fe11263c95d74eDavid Li			     "cannot initialize %s shader input / %s",
22151591693c7b415e9869157c711fe11263c95d74eDavid Li			     _mesa_glsl_shader_target_name(state->target),
22161591693c7b415e9869157c711fe11263c95d74eDavid Li			     (state->target == vertex_shader)
22171591693c7b415e9869157c711fe11263c95d74eDavid Li			     ? "attribute" : "varying");
22181591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
22191591693c7b415e9869157c711fe11263c95d74eDavid Li
22201591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
22211591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions,
22221591693c7b415e9869157c711fe11263c95d74eDavid Li						 state);
22231591693c7b415e9869157c711fe11263c95d74eDavid Li
22241591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* Calculate the constant value if this is a const or uniform
22251591693c7b415e9869157c711fe11263c95d74eDavid Li	  * declaration.
22261591693c7b415e9869157c711fe11263c95d74eDavid Li	  */
22271591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (this->type->qualifier.flags.q.constant
22281591693c7b415e9869157c711fe11263c95d74eDavid Li	     || this->type->qualifier.flags.q.uniform) {
22291591693c7b415e9869157c711fe11263c95d74eDavid Li	    ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs);
22301591693c7b415e9869157c711fe11263c95d74eDavid Li	    if (new_rhs != NULL) {
22311591693c7b415e9869157c711fe11263c95d74eDavid Li	       rhs = new_rhs;
22321591693c7b415e9869157c711fe11263c95d74eDavid Li
22331591693c7b415e9869157c711fe11263c95d74eDavid Li	       ir_constant *constant_value = rhs->constant_expression_value();
22341591693c7b415e9869157c711fe11263c95d74eDavid Li	       if (!constant_value) {
22351591693c7b415e9869157c711fe11263c95d74eDavid Li		  _mesa_glsl_error(& initializer_loc, state,
22361591693c7b415e9869157c711fe11263c95d74eDavid Li				   "initializer of %s variable `%s' must be a "
22371591693c7b415e9869157c711fe11263c95d74eDavid Li				   "constant expression",
22381591693c7b415e9869157c711fe11263c95d74eDavid Li				   (this->type->qualifier.flags.q.constant)
22391591693c7b415e9869157c711fe11263c95d74eDavid Li				   ? "const" : "uniform",
22401591693c7b415e9869157c711fe11263c95d74eDavid Li				   decl->identifier);
22411591693c7b415e9869157c711fe11263c95d74eDavid Li		  if (var->type->is_numeric()) {
22421591693c7b415e9869157c711fe11263c95d74eDavid Li		     /* Reduce cascading errors. */
22431591693c7b415e9869157c711fe11263c95d74eDavid Li		     var->constant_value = ir_constant::zero(ctx, var->type);
22441591693c7b415e9869157c711fe11263c95d74eDavid Li		  }
22451591693c7b415e9869157c711fe11263c95d74eDavid Li	       } else {
22461591693c7b415e9869157c711fe11263c95d74eDavid Li		  rhs = constant_value;
22471591693c7b415e9869157c711fe11263c95d74eDavid Li		  var->constant_value = constant_value;
22481591693c7b415e9869157c711fe11263c95d74eDavid Li	       }
22491591693c7b415e9869157c711fe11263c95d74eDavid Li	    } else {
22501591693c7b415e9869157c711fe11263c95d74eDavid Li	       _mesa_glsl_error(&initializer_loc, state,
22511591693c7b415e9869157c711fe11263c95d74eDavid Li			        "initializer of type %s cannot be assigned to "
22521591693c7b415e9869157c711fe11263c95d74eDavid Li				"variable of type %s",
22531591693c7b415e9869157c711fe11263c95d74eDavid Li				rhs->type->name, var->type->name);
22541591693c7b415e9869157c711fe11263c95d74eDavid Li	       if (var->type->is_numeric()) {
22551591693c7b415e9869157c711fe11263c95d74eDavid Li		  /* Reduce cascading errors. */
22561591693c7b415e9869157c711fe11263c95d74eDavid Li		  var->constant_value = ir_constant::zero(ctx, var->type);
22571591693c7b415e9869157c711fe11263c95d74eDavid Li	       }
22581591693c7b415e9869157c711fe11263c95d74eDavid Li	    }
22591591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
22601591693c7b415e9869157c711fe11263c95d74eDavid Li
22611591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (rhs && !rhs->type->is_error()) {
22621591693c7b415e9869157c711fe11263c95d74eDavid Li	    bool temp = var->read_only;
22631591693c7b415e9869157c711fe11263c95d74eDavid Li	    if (this->type->qualifier.flags.q.constant)
22641591693c7b415e9869157c711fe11263c95d74eDavid Li	       var->read_only = false;
22651591693c7b415e9869157c711fe11263c95d74eDavid Li
22661591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* Never emit code to initialize a uniform.
22671591693c7b415e9869157c711fe11263c95d74eDavid Li	     */
22681591693c7b415e9869157c711fe11263c95d74eDavid Li	    const glsl_type *initializer_type;
22691591693c7b415e9869157c711fe11263c95d74eDavid Li	    if (!this->type->qualifier.flags.q.uniform) {
22701591693c7b415e9869157c711fe11263c95d74eDavid Li	       result = do_assignment(&initializer_instructions, state,
22711591693c7b415e9869157c711fe11263c95d74eDavid Li				      lhs, rhs,
22721591693c7b415e9869157c711fe11263c95d74eDavid Li				      this->get_location());
22731591693c7b415e9869157c711fe11263c95d74eDavid Li	       initializer_type = result->type;
22741591693c7b415e9869157c711fe11263c95d74eDavid Li	    } else
22751591693c7b415e9869157c711fe11263c95d74eDavid Li	       initializer_type = rhs->type;
22761591693c7b415e9869157c711fe11263c95d74eDavid Li
22771591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* If the declared variable is an unsized array, it must inherrit
22781591693c7b415e9869157c711fe11263c95d74eDavid Li	     * its full type from the initializer.  A declaration such as
22791591693c7b415e9869157c711fe11263c95d74eDavid Li	     *
22801591693c7b415e9869157c711fe11263c95d74eDavid Li	     *     uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
22811591693c7b415e9869157c711fe11263c95d74eDavid Li	     *
22821591693c7b415e9869157c711fe11263c95d74eDavid Li	     * becomes
22831591693c7b415e9869157c711fe11263c95d74eDavid Li	     *
22841591693c7b415e9869157c711fe11263c95d74eDavid Li	     *     uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
22851591693c7b415e9869157c711fe11263c95d74eDavid Li	     *
22861591693c7b415e9869157c711fe11263c95d74eDavid Li	     * The assignment generated in the if-statement (below) will also
22871591693c7b415e9869157c711fe11263c95d74eDavid Li	     * automatically handle this case for non-uniforms.
22881591693c7b415e9869157c711fe11263c95d74eDavid Li	     *
22891591693c7b415e9869157c711fe11263c95d74eDavid Li	     * If the declared variable is not an array, the types must
22901591693c7b415e9869157c711fe11263c95d74eDavid Li	     * already match exactly.  As a result, the type assignment
22911591693c7b415e9869157c711fe11263c95d74eDavid Li	     * here can be done unconditionally.  For non-uniforms the call
22921591693c7b415e9869157c711fe11263c95d74eDavid Li	     * to do_assignment can change the type of the initializer (via
22931591693c7b415e9869157c711fe11263c95d74eDavid Li	     * the implicit conversion rules).  For uniforms the initializer
22941591693c7b415e9869157c711fe11263c95d74eDavid Li	     * must be a constant expression, and the type of that expression
22951591693c7b415e9869157c711fe11263c95d74eDavid Li	     * was validated above.
22961591693c7b415e9869157c711fe11263c95d74eDavid Li	     */
22971591693c7b415e9869157c711fe11263c95d74eDavid Li	    var->type = initializer_type;
22981591693c7b415e9869157c711fe11263c95d74eDavid Li
22991591693c7b415e9869157c711fe11263c95d74eDavid Li	    var->read_only = temp;
23001591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
23011591693c7b415e9869157c711fe11263c95d74eDavid Li      }
23021591693c7b415e9869157c711fe11263c95d74eDavid Li
23031591693c7b415e9869157c711fe11263c95d74eDavid Li      /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
23041591693c7b415e9869157c711fe11263c95d74eDavid Li       *
23051591693c7b415e9869157c711fe11263c95d74eDavid Li       *     "It is an error to write to a const variable outside of
23061591693c7b415e9869157c711fe11263c95d74eDavid Li       *      its declaration, so they must be initialized when
23071591693c7b415e9869157c711fe11263c95d74eDavid Li       *      declared."
23081591693c7b415e9869157c711fe11263c95d74eDavid Li       */
23091591693c7b415e9869157c711fe11263c95d74eDavid Li      if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
23101591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state,
23111591693c7b415e9869157c711fe11263c95d74eDavid Li			  "const declaration of `%s' must be initialized");
23121591693c7b415e9869157c711fe11263c95d74eDavid Li      }
23131591693c7b415e9869157c711fe11263c95d74eDavid Li
23141591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Check if this declaration is actually a re-declaration, either to
23151591693c7b415e9869157c711fe11263c95d74eDavid Li       * resize an array or add qualifiers to an existing variable.
23161591693c7b415e9869157c711fe11263c95d74eDavid Li       *
23171591693c7b415e9869157c711fe11263c95d74eDavid Li       * This is allowed for variables in the current scope, or when at
23181591693c7b415e9869157c711fe11263c95d74eDavid Li       * global scope (for built-ins in the implicit outer scope).
23191591693c7b415e9869157c711fe11263c95d74eDavid Li       */
23201591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_variable *earlier = state->symbols->get_variable(decl->identifier);
23211591693c7b415e9869157c711fe11263c95d74eDavid Li      if (earlier != NULL && (state->current_function == NULL ||
23221591693c7b415e9869157c711fe11263c95d74eDavid Li	  state->symbols->name_declared_this_scope(decl->identifier))) {
23231591693c7b415e9869157c711fe11263c95d74eDavid Li
23241591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
23251591693c7b415e9869157c711fe11263c95d74eDavid Li	  *
23261591693c7b415e9869157c711fe11263c95d74eDavid Li	  * "It is legal to declare an array without a size and then
23271591693c7b415e9869157c711fe11263c95d74eDavid Li	  *  later re-declare the same name as an array of the same
23281591693c7b415e9869157c711fe11263c95d74eDavid Li	  *  type and specify a size."
23291591693c7b415e9869157c711fe11263c95d74eDavid Li	  */
23301591693c7b415e9869157c711fe11263c95d74eDavid Li	 if ((earlier->type->array_size() == 0)
23311591693c7b415e9869157c711fe11263c95d74eDavid Li	     && var->type->is_array()
23321591693c7b415e9869157c711fe11263c95d74eDavid Li	     && (var->type->element_type() == earlier->type->element_type())) {
23331591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* FINISHME: This doesn't match the qualifiers on the two
23341591693c7b415e9869157c711fe11263c95d74eDavid Li	     * FINISHME: declarations.  It's not 100% clear whether this is
23351591693c7b415e9869157c711fe11263c95d74eDavid Li	     * FINISHME: required or not.
23361591693c7b415e9869157c711fe11263c95d74eDavid Li	     */
23371591693c7b415e9869157c711fe11263c95d74eDavid Li
23381591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
23391591693c7b415e9869157c711fe11263c95d74eDavid Li	     *
23401591693c7b415e9869157c711fe11263c95d74eDavid Li	     *     "The size [of gl_TexCoord] can be at most
23411591693c7b415e9869157c711fe11263c95d74eDavid Li	     *     gl_MaxTextureCoords."
23421591693c7b415e9869157c711fe11263c95d74eDavid Li	     */
23431591693c7b415e9869157c711fe11263c95d74eDavid Li	    const unsigned size = unsigned(var->type->array_size());
23441591693c7b415e9869157c711fe11263c95d74eDavid Li	    if ((strcmp("gl_TexCoord", var->name) == 0)
23451591693c7b415e9869157c711fe11263c95d74eDavid Li		&& (size > state->Const.MaxTextureCoords)) {
23461591693c7b415e9869157c711fe11263c95d74eDavid Li	       YYLTYPE loc = this->get_location();
23471591693c7b415e9869157c711fe11263c95d74eDavid Li
23481591693c7b415e9869157c711fe11263c95d74eDavid Li	       _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot "
23491591693c7b415e9869157c711fe11263c95d74eDavid Li				"be larger than gl_MaxTextureCoords (%u)\n",
23501591693c7b415e9869157c711fe11263c95d74eDavid Li				state->Const.MaxTextureCoords);
23511591693c7b415e9869157c711fe11263c95d74eDavid Li	    } else if ((size > 0) && (size <= earlier->max_array_access)) {
23521591693c7b415e9869157c711fe11263c95d74eDavid Li	       YYLTYPE loc = this->get_location();
23531591693c7b415e9869157c711fe11263c95d74eDavid Li
23541591693c7b415e9869157c711fe11263c95d74eDavid Li	       _mesa_glsl_error(& loc, state, "array size must be > %u due to "
23551591693c7b415e9869157c711fe11263c95d74eDavid Li				"previous access",
23561591693c7b415e9869157c711fe11263c95d74eDavid Li				earlier->max_array_access);
23571591693c7b415e9869157c711fe11263c95d74eDavid Li	    }
23581591693c7b415e9869157c711fe11263c95d74eDavid Li
23591591693c7b415e9869157c711fe11263c95d74eDavid Li	    earlier->type = var->type;
23601591693c7b415e9869157c711fe11263c95d74eDavid Li	    delete var;
23611591693c7b415e9869157c711fe11263c95d74eDavid Li	    var = NULL;
23621591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else if (state->ARB_fragment_coord_conventions_enable
23631591693c7b415e9869157c711fe11263c95d74eDavid Li		    && strcmp(var->name, "gl_FragCoord") == 0
23641591693c7b415e9869157c711fe11263c95d74eDavid Li		    && earlier->type == var->type
23651591693c7b415e9869157c711fe11263c95d74eDavid Li		    && earlier->mode == var->mode) {
23661591693c7b415e9869157c711fe11263c95d74eDavid Li	    /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
23671591693c7b415e9869157c711fe11263c95d74eDavid Li	     * qualifiers.
23681591693c7b415e9869157c711fe11263c95d74eDavid Li	     */
23691591693c7b415e9869157c711fe11263c95d74eDavid Li	    earlier->origin_upper_left = var->origin_upper_left;
23701591693c7b415e9869157c711fe11263c95d74eDavid Li	    earlier->pixel_center_integer = var->pixel_center_integer;
23711591693c7b415e9869157c711fe11263c95d74eDavid Li	 } else {
23721591693c7b415e9869157c711fe11263c95d74eDavid Li	    YYLTYPE loc = this->get_location();
23731591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
23741591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
23751591693c7b415e9869157c711fe11263c95d74eDavid Li
23761591693c7b415e9869157c711fe11263c95d74eDavid Li	 continue;
23771591693c7b415e9869157c711fe11263c95d74eDavid Li      }
23781591693c7b415e9869157c711fe11263c95d74eDavid Li
23791591693c7b415e9869157c711fe11263c95d74eDavid Li      /* By now, we know it's a new variable declaration (we didn't hit the
23801591693c7b415e9869157c711fe11263c95d74eDavid Li       * above "continue").
23811591693c7b415e9869157c711fe11263c95d74eDavid Li       *
23821591693c7b415e9869157c711fe11263c95d74eDavid Li       * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
23831591693c7b415e9869157c711fe11263c95d74eDavid Li       *
23841591693c7b415e9869157c711fe11263c95d74eDavid Li       *   "Identifiers starting with "gl_" are reserved for use by
23851591693c7b415e9869157c711fe11263c95d74eDavid Li       *   OpenGL, and may not be declared in a shader as either a
23861591693c7b415e9869157c711fe11263c95d74eDavid Li       *   variable or a function."
23871591693c7b415e9869157c711fe11263c95d74eDavid Li       */
23881591693c7b415e9869157c711fe11263c95d74eDavid Li      if (strncmp(decl->identifier, "gl_", 3) == 0)
23891591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state,
23901591693c7b415e9869157c711fe11263c95d74eDavid Li			  "identifier `%s' uses reserved `gl_' prefix",
23911591693c7b415e9869157c711fe11263c95d74eDavid Li			  decl->identifier);
23921591693c7b415e9869157c711fe11263c95d74eDavid Li
23931591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Add the variable to the symbol table.  Note that the initializer's
23941591693c7b415e9869157c711fe11263c95d74eDavid Li       * IR was already processed earlier (though it hasn't been emitted yet),
23951591693c7b415e9869157c711fe11263c95d74eDavid Li       * without the variable in scope.
23961591693c7b415e9869157c711fe11263c95d74eDavid Li       *
23971591693c7b415e9869157c711fe11263c95d74eDavid Li       * This differs from most C-like languages, but it follows the GLSL
23981591693c7b415e9869157c711fe11263c95d74eDavid Li       * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
23991591693c7b415e9869157c711fe11263c95d74eDavid Li       * spec:
24001591693c7b415e9869157c711fe11263c95d74eDavid Li       *
24011591693c7b415e9869157c711fe11263c95d74eDavid Li       *     "Within a declaration, the scope of a name starts immediately
24021591693c7b415e9869157c711fe11263c95d74eDavid Li       *     after the initializer if present or immediately after the name
24031591693c7b415e9869157c711fe11263c95d74eDavid Li       *     being declared if not."
24041591693c7b415e9869157c711fe11263c95d74eDavid Li       */
24051591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!state->symbols->add_variable(var)) {
24061591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->get_location();
24071591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
24081591693c7b415e9869157c711fe11263c95d74eDavid Li			  "current scope", decl->identifier);
24091591693c7b415e9869157c711fe11263c95d74eDavid Li	 continue;
24101591693c7b415e9869157c711fe11263c95d74eDavid Li      }
24111591693c7b415e9869157c711fe11263c95d74eDavid Li
24121591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Push the variable declaration to the top.  It means that all
24131591693c7b415e9869157c711fe11263c95d74eDavid Li       * the variable declarations will appear in a funny
24141591693c7b415e9869157c711fe11263c95d74eDavid Li       * last-to-first order, but otherwise we run into trouble if a
24151591693c7b415e9869157c711fe11263c95d74eDavid Li       * function is prototyped, a global var is decled, then the
24161591693c7b415e9869157c711fe11263c95d74eDavid Li       * function is defined with usage of the global var.  See
24171591693c7b415e9869157c711fe11263c95d74eDavid Li       * glslparsertest's CorrectModule.frag.
24181591693c7b415e9869157c711fe11263c95d74eDavid Li       */
24191591693c7b415e9869157c711fe11263c95d74eDavid Li      instructions->push_head(var);
24201591693c7b415e9869157c711fe11263c95d74eDavid Li      instructions->append_list(&initializer_instructions);
24211591693c7b415e9869157c711fe11263c95d74eDavid Li   }
24221591693c7b415e9869157c711fe11263c95d74eDavid Li
24231591693c7b415e9869157c711fe11263c95d74eDavid Li
24241591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Generally, variable declarations do not have r-values.  However,
24251591693c7b415e9869157c711fe11263c95d74eDavid Li    * one is used for the declaration in
24261591693c7b415e9869157c711fe11263c95d74eDavid Li    *
24271591693c7b415e9869157c711fe11263c95d74eDavid Li    * while (bool b = some_condition()) {
24281591693c7b415e9869157c711fe11263c95d74eDavid Li    *   ...
24291591693c7b415e9869157c711fe11263c95d74eDavid Li    * }
24301591693c7b415e9869157c711fe11263c95d74eDavid Li    *
24311591693c7b415e9869157c711fe11263c95d74eDavid Li    * so we return the rvalue from the last seen declaration here.
24321591693c7b415e9869157c711fe11263c95d74eDavid Li    */
24331591693c7b415e9869157c711fe11263c95d74eDavid Li   return result;
24341591693c7b415e9869157c711fe11263c95d74eDavid Li}
24351591693c7b415e9869157c711fe11263c95d74eDavid Li
24361591693c7b415e9869157c711fe11263c95d74eDavid Li
24371591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
24381591693c7b415e9869157c711fe11263c95d74eDavid Liast_parameter_declarator::hir(exec_list *instructions,
24391591693c7b415e9869157c711fe11263c95d74eDavid Li			      struct _mesa_glsl_parse_state *state)
24401591693c7b415e9869157c711fe11263c95d74eDavid Li{
24411591693c7b415e9869157c711fe11263c95d74eDavid Li   void *ctx = state;
24421591693c7b415e9869157c711fe11263c95d74eDavid Li   const struct glsl_type *type;
24431591693c7b415e9869157c711fe11263c95d74eDavid Li   const char *name = NULL;
24441591693c7b415e9869157c711fe11263c95d74eDavid Li   YYLTYPE loc = this->get_location();
24451591693c7b415e9869157c711fe11263c95d74eDavid Li
24461591693c7b415e9869157c711fe11263c95d74eDavid Li   type = this->type->specifier->glsl_type(& name, state);
24471591693c7b415e9869157c711fe11263c95d74eDavid Li
24481591693c7b415e9869157c711fe11263c95d74eDavid Li   if (type == NULL) {
24491591693c7b415e9869157c711fe11263c95d74eDavid Li      if (name != NULL) {
24501591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state,
24511591693c7b415e9869157c711fe11263c95d74eDavid Li			  "invalid type `%s' in declaration of `%s'",
24521591693c7b415e9869157c711fe11263c95d74eDavid Li			  name, this->identifier);
24531591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
24541591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state,
24551591693c7b415e9869157c711fe11263c95d74eDavid Li			  "invalid type in declaration of `%s'",
24561591693c7b415e9869157c711fe11263c95d74eDavid Li			  this->identifier);
24571591693c7b415e9869157c711fe11263c95d74eDavid Li      }
24581591693c7b415e9869157c711fe11263c95d74eDavid Li
24591591693c7b415e9869157c711fe11263c95d74eDavid Li      type = glsl_type::error_type;
24601591693c7b415e9869157c711fe11263c95d74eDavid Li   }
24611591693c7b415e9869157c711fe11263c95d74eDavid Li
24621591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
24631591693c7b415e9869157c711fe11263c95d74eDavid Li    *
24641591693c7b415e9869157c711fe11263c95d74eDavid Li    *    "Functions that accept no input arguments need not use void in the
24651591693c7b415e9869157c711fe11263c95d74eDavid Li    *    argument list because prototypes (or definitions) are required and
24661591693c7b415e9869157c711fe11263c95d74eDavid Li    *    therefore there is no ambiguity when an empty argument list "( )" is
24671591693c7b415e9869157c711fe11263c95d74eDavid Li    *    declared. The idiom "(void)" as a parameter list is provided for
24681591693c7b415e9869157c711fe11263c95d74eDavid Li    *    convenience."
24691591693c7b415e9869157c711fe11263c95d74eDavid Li    *
24701591693c7b415e9869157c711fe11263c95d74eDavid Li    * Placing this check here prevents a void parameter being set up
24711591693c7b415e9869157c711fe11263c95d74eDavid Li    * for a function, which avoids tripping up checks for main taking
24721591693c7b415e9869157c711fe11263c95d74eDavid Li    * parameters and lookups of an unnamed symbol.
24731591693c7b415e9869157c711fe11263c95d74eDavid Li    */
24741591693c7b415e9869157c711fe11263c95d74eDavid Li   if (type->is_void()) {
24751591693c7b415e9869157c711fe11263c95d74eDavid Li      if (this->identifier != NULL)
24761591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state,
24771591693c7b415e9869157c711fe11263c95d74eDavid Li			  "named parameter cannot have type `void'");
24781591693c7b415e9869157c711fe11263c95d74eDavid Li
24791591693c7b415e9869157c711fe11263c95d74eDavid Li      is_void = true;
24801591693c7b415e9869157c711fe11263c95d74eDavid Li      return NULL;
24811591693c7b415e9869157c711fe11263c95d74eDavid Li   }
24821591693c7b415e9869157c711fe11263c95d74eDavid Li
24831591693c7b415e9869157c711fe11263c95d74eDavid Li   if (formal_parameter && (this->identifier == NULL)) {
24841591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
24851591693c7b415e9869157c711fe11263c95d74eDavid Li      return NULL;
24861591693c7b415e9869157c711fe11263c95d74eDavid Li   }
24871591693c7b415e9869157c711fe11263c95d74eDavid Li
24881591693c7b415e9869157c711fe11263c95d74eDavid Li   /* This only handles "vec4 foo[..]".  The earlier specifier->glsl_type(...)
24891591693c7b415e9869157c711fe11263c95d74eDavid Li    * call already handled the "vec4[..] foo" case.
24901591693c7b415e9869157c711fe11263c95d74eDavid Li    */
24911591693c7b415e9869157c711fe11263c95d74eDavid Li   if (this->is_array) {
24921591693c7b415e9869157c711fe11263c95d74eDavid Li      type = process_array_type(&loc, type, this->array_size, state);
24931591693c7b415e9869157c711fe11263c95d74eDavid Li   }
24941591693c7b415e9869157c711fe11263c95d74eDavid Li
24951591693c7b415e9869157c711fe11263c95d74eDavid Li   if (type->array_size() == 0) {
24961591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
24971591693c7b415e9869157c711fe11263c95d74eDavid Li		       "a declared size.");
24981591693c7b415e9869157c711fe11263c95d74eDavid Li      type = glsl_type::error_type;
24991591693c7b415e9869157c711fe11263c95d74eDavid Li   }
25001591693c7b415e9869157c711fe11263c95d74eDavid Li
25011591693c7b415e9869157c711fe11263c95d74eDavid Li   is_void = false;
25021591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
25031591693c7b415e9869157c711fe11263c95d74eDavid Li
25041591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Apply any specified qualifiers to the parameter declaration.  Note that
25051591693c7b415e9869157c711fe11263c95d74eDavid Li    * for function parameters the default mode is 'in'.
25061591693c7b415e9869157c711fe11263c95d74eDavid Li    */
25071591693c7b415e9869157c711fe11263c95d74eDavid Li   apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
25081591693c7b415e9869157c711fe11263c95d74eDavid Li
25091591693c7b415e9869157c711fe11263c95d74eDavid Li   instructions->push_tail(var);
25101591693c7b415e9869157c711fe11263c95d74eDavid Li
25111591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Parameter declarations do not have r-values.
25121591693c7b415e9869157c711fe11263c95d74eDavid Li    */
25131591693c7b415e9869157c711fe11263c95d74eDavid Li   return NULL;
25141591693c7b415e9869157c711fe11263c95d74eDavid Li}
25151591693c7b415e9869157c711fe11263c95d74eDavid Li
25161591693c7b415e9869157c711fe11263c95d74eDavid Li
25171591693c7b415e9869157c711fe11263c95d74eDavid Livoid
25181591693c7b415e9869157c711fe11263c95d74eDavid Liast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
25191591693c7b415e9869157c711fe11263c95d74eDavid Li					    bool formal,
25201591693c7b415e9869157c711fe11263c95d74eDavid Li					    exec_list *ir_parameters,
25211591693c7b415e9869157c711fe11263c95d74eDavid Li					    _mesa_glsl_parse_state *state)
25221591693c7b415e9869157c711fe11263c95d74eDavid Li{
25231591693c7b415e9869157c711fe11263c95d74eDavid Li   ast_parameter_declarator *void_param = NULL;
25241591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned count = 0;
25251591693c7b415e9869157c711fe11263c95d74eDavid Li
25261591693c7b415e9869157c711fe11263c95d74eDavid Li   foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
25271591693c7b415e9869157c711fe11263c95d74eDavid Li      param->formal_parameter = formal;
25281591693c7b415e9869157c711fe11263c95d74eDavid Li      param->hir(ir_parameters, state);
25291591693c7b415e9869157c711fe11263c95d74eDavid Li
25301591693c7b415e9869157c711fe11263c95d74eDavid Li      if (param->is_void)
25311591693c7b415e9869157c711fe11263c95d74eDavid Li	 void_param = param;
25321591693c7b415e9869157c711fe11263c95d74eDavid Li
25331591693c7b415e9869157c711fe11263c95d74eDavid Li      count++;
25341591693c7b415e9869157c711fe11263c95d74eDavid Li   }
25351591693c7b415e9869157c711fe11263c95d74eDavid Li
25361591693c7b415e9869157c711fe11263c95d74eDavid Li   if ((void_param != NULL) && (count > 1)) {
25371591693c7b415e9869157c711fe11263c95d74eDavid Li      YYLTYPE loc = void_param->get_location();
25381591693c7b415e9869157c711fe11263c95d74eDavid Li
25391591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(& loc, state,
25401591693c7b415e9869157c711fe11263c95d74eDavid Li		       "`void' parameter must be only parameter");
25411591693c7b415e9869157c711fe11263c95d74eDavid Li   }
25421591693c7b415e9869157c711fe11263c95d74eDavid Li}
25431591693c7b415e9869157c711fe11263c95d74eDavid Li
25441591693c7b415e9869157c711fe11263c95d74eDavid Li
25451591693c7b415e9869157c711fe11263c95d74eDavid Livoid
25461591693c7b415e9869157c711fe11263c95d74eDavid Liemit_function(_mesa_glsl_parse_state *state, exec_list *instructions,
25471591693c7b415e9869157c711fe11263c95d74eDavid Li	      ir_function *f)
25481591693c7b415e9869157c711fe11263c95d74eDavid Li{
25491591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Emit the new function header */
25501591693c7b415e9869157c711fe11263c95d74eDavid Li   if (state->current_function == NULL) {
25511591693c7b415e9869157c711fe11263c95d74eDavid Li      instructions->push_tail(f);
25521591693c7b415e9869157c711fe11263c95d74eDavid Li   } else {
25531591693c7b415e9869157c711fe11263c95d74eDavid Li      /* IR invariants disallow function declarations or definitions nested
25541591693c7b415e9869157c711fe11263c95d74eDavid Li       * within other function definitions.  Insert the new ir_function
25551591693c7b415e9869157c711fe11263c95d74eDavid Li       * block in the instruction sequence before the ir_function block
25561591693c7b415e9869157c711fe11263c95d74eDavid Li       * containing the current ir_function_signature.
25571591693c7b415e9869157c711fe11263c95d74eDavid Li       */
25581591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_function *const curr =
25591591693c7b415e9869157c711fe11263c95d74eDavid Li	 const_cast<ir_function *>(state->current_function->function());
25601591693c7b415e9869157c711fe11263c95d74eDavid Li
25611591693c7b415e9869157c711fe11263c95d74eDavid Li      curr->insert_before(f);
25621591693c7b415e9869157c711fe11263c95d74eDavid Li   }
25631591693c7b415e9869157c711fe11263c95d74eDavid Li}
25641591693c7b415e9869157c711fe11263c95d74eDavid Li
25651591693c7b415e9869157c711fe11263c95d74eDavid Li
25661591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
25671591693c7b415e9869157c711fe11263c95d74eDavid Liast_function::hir(exec_list *instructions,
25681591693c7b415e9869157c711fe11263c95d74eDavid Li		  struct _mesa_glsl_parse_state *state)
25691591693c7b415e9869157c711fe11263c95d74eDavid Li{
25701591693c7b415e9869157c711fe11263c95d74eDavid Li   void *ctx = state;
25711591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_function *f = NULL;
25721591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_function_signature *sig = NULL;
25731591693c7b415e9869157c711fe11263c95d74eDavid Li   exec_list hir_parameters;
25741591693c7b415e9869157c711fe11263c95d74eDavid Li
25751591693c7b415e9869157c711fe11263c95d74eDavid Li   const char *const name = identifier;
25761591693c7b415e9869157c711fe11263c95d74eDavid Li
25771591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
25781591693c7b415e9869157c711fe11263c95d74eDavid Li    *
25791591693c7b415e9869157c711fe11263c95d74eDavid Li    *   "Function declarations (prototypes) cannot occur inside of functions;
25801591693c7b415e9869157c711fe11263c95d74eDavid Li    *   they must be at global scope, or for the built-in functions, outside
25811591693c7b415e9869157c711fe11263c95d74eDavid Li    *   the global scope."
25821591693c7b415e9869157c711fe11263c95d74eDavid Li    *
25831591693c7b415e9869157c711fe11263c95d74eDavid Li    * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
25841591693c7b415e9869157c711fe11263c95d74eDavid Li    *
25851591693c7b415e9869157c711fe11263c95d74eDavid Li    *   "User defined functions may only be defined within the global scope."
25861591693c7b415e9869157c711fe11263c95d74eDavid Li    *
25871591693c7b415e9869157c711fe11263c95d74eDavid Li    * Note that this language does not appear in GLSL 1.10.
25881591693c7b415e9869157c711fe11263c95d74eDavid Li    */
25891591693c7b415e9869157c711fe11263c95d74eDavid Li   if ((state->current_function != NULL) && (state->language_version != 110)) {
25901591693c7b415e9869157c711fe11263c95d74eDavid Li      YYLTYPE loc = this->get_location();
25911591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(&loc, state,
25921591693c7b415e9869157c711fe11263c95d74eDavid Li		       "declaration of function `%s' not allowed within "
25931591693c7b415e9869157c711fe11263c95d74eDavid Li		       "function body", name);
25941591693c7b415e9869157c711fe11263c95d74eDavid Li   }
25951591693c7b415e9869157c711fe11263c95d74eDavid Li
25961591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
25971591693c7b415e9869157c711fe11263c95d74eDavid Li    *
25981591693c7b415e9869157c711fe11263c95d74eDavid Li    *   "Identifiers starting with "gl_" are reserved for use by
25991591693c7b415e9869157c711fe11263c95d74eDavid Li    *   OpenGL, and may not be declared in a shader as either a
26001591693c7b415e9869157c711fe11263c95d74eDavid Li    *   variable or a function."
26011591693c7b415e9869157c711fe11263c95d74eDavid Li    */
26021591693c7b415e9869157c711fe11263c95d74eDavid Li   if (strncmp(name, "gl_", 3) == 0) {
26031591693c7b415e9869157c711fe11263c95d74eDavid Li      YYLTYPE loc = this->get_location();
26041591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(&loc, state,
26051591693c7b415e9869157c711fe11263c95d74eDavid Li		       "identifier `%s' uses reserved `gl_' prefix", name);
26061591693c7b415e9869157c711fe11263c95d74eDavid Li   }
26071591693c7b415e9869157c711fe11263c95d74eDavid Li
26081591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Convert the list of function parameters to HIR now so that they can be
26091591693c7b415e9869157c711fe11263c95d74eDavid Li    * used below to compare this function's signature with previously seen
26101591693c7b415e9869157c711fe11263c95d74eDavid Li    * signatures for functions with the same name.
26111591693c7b415e9869157c711fe11263c95d74eDavid Li    */
26121591693c7b415e9869157c711fe11263c95d74eDavid Li   ast_parameter_declarator::parameters_to_hir(& this->parameters,
26131591693c7b415e9869157c711fe11263c95d74eDavid Li					       is_definition,
26141591693c7b415e9869157c711fe11263c95d74eDavid Li					       & hir_parameters, state);
26151591693c7b415e9869157c711fe11263c95d74eDavid Li
26161591693c7b415e9869157c711fe11263c95d74eDavid Li   const char *return_type_name;
26171591693c7b415e9869157c711fe11263c95d74eDavid Li   const glsl_type *return_type =
26181591693c7b415e9869157c711fe11263c95d74eDavid Li      this->return_type->specifier->glsl_type(& return_type_name, state);
26191591693c7b415e9869157c711fe11263c95d74eDavid Li
26201591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!return_type) {
26211591693c7b415e9869157c711fe11263c95d74eDavid Li      YYLTYPE loc = this->get_location();
26221591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(&loc, state,
26231591693c7b415e9869157c711fe11263c95d74eDavid Li		       "function `%s' has undeclared return type `%s'",
26241591693c7b415e9869157c711fe11263c95d74eDavid Li		       name, return_type_name);
26251591693c7b415e9869157c711fe11263c95d74eDavid Li      return_type = glsl_type::error_type;
26261591693c7b415e9869157c711fe11263c95d74eDavid Li   }
26271591693c7b415e9869157c711fe11263c95d74eDavid Li
26281591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
26291591693c7b415e9869157c711fe11263c95d74eDavid Li    * "No qualifier is allowed on the return type of a function."
26301591693c7b415e9869157c711fe11263c95d74eDavid Li    */
26311591693c7b415e9869157c711fe11263c95d74eDavid Li   if (this->return_type->has_qualifiers()) {
26321591693c7b415e9869157c711fe11263c95d74eDavid Li      YYLTYPE loc = this->get_location();
26331591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(& loc, state,
26341591693c7b415e9869157c711fe11263c95d74eDavid Li		       "function `%s' return type has qualifiers", name);
26351591693c7b415e9869157c711fe11263c95d74eDavid Li   }
26361591693c7b415e9869157c711fe11263c95d74eDavid Li
26371591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Verify that this function's signature either doesn't match a previously
26381591693c7b415e9869157c711fe11263c95d74eDavid Li    * seen signature for a function with the same name, or, if a match is found,
26391591693c7b415e9869157c711fe11263c95d74eDavid Li    * that the previously seen signature does not have an associated definition.
26401591693c7b415e9869157c711fe11263c95d74eDavid Li    */
26411591693c7b415e9869157c711fe11263c95d74eDavid Li   f = state->symbols->get_function(name);
26421591693c7b415e9869157c711fe11263c95d74eDavid Li   if (f != NULL && (state->es_shader || f->has_user_signature())) {
26431591693c7b415e9869157c711fe11263c95d74eDavid Li      sig = f->exact_matching_signature(&hir_parameters);
26441591693c7b415e9869157c711fe11263c95d74eDavid Li      if (sig != NULL) {
26451591693c7b415e9869157c711fe11263c95d74eDavid Li	 const char *badvar = sig->qualifiers_match(&hir_parameters);
26461591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (badvar != NULL) {
26471591693c7b415e9869157c711fe11263c95d74eDavid Li	    YYLTYPE loc = this->get_location();
26481591693c7b415e9869157c711fe11263c95d74eDavid Li
26491591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
26501591693c7b415e9869157c711fe11263c95d74eDavid Li			     "qualifiers don't match prototype", name, badvar);
26511591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
26521591693c7b415e9869157c711fe11263c95d74eDavid Li
26531591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (sig->return_type != return_type) {
26541591693c7b415e9869157c711fe11263c95d74eDavid Li	    YYLTYPE loc = this->get_location();
26551591693c7b415e9869157c711fe11263c95d74eDavid Li
26561591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
26571591693c7b415e9869157c711fe11263c95d74eDavid Li			     "match prototype", name);
26581591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
26591591693c7b415e9869157c711fe11263c95d74eDavid Li
26601591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (is_definition && sig->is_defined) {
26611591693c7b415e9869157c711fe11263c95d74eDavid Li	    YYLTYPE loc = this->get_location();
26621591693c7b415e9869157c711fe11263c95d74eDavid Li
26631591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
26641591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
26651591693c7b415e9869157c711fe11263c95d74eDavid Li      }
26661591693c7b415e9869157c711fe11263c95d74eDavid Li   } else {
26671591693c7b415e9869157c711fe11263c95d74eDavid Li      f = new(ctx) ir_function(name);
26681591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!state->symbols->add_function(f)) {
26691591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* This function name shadows a non-function use of the same name. */
26701591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->get_location();
26711591693c7b415e9869157c711fe11263c95d74eDavid Li
26721591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
26731591693c7b415e9869157c711fe11263c95d74eDavid Li			  "non-function", name);
26741591693c7b415e9869157c711fe11263c95d74eDavid Li	 return NULL;
26751591693c7b415e9869157c711fe11263c95d74eDavid Li      }
26761591693c7b415e9869157c711fe11263c95d74eDavid Li
26771591693c7b415e9869157c711fe11263c95d74eDavid Li      emit_function(state, instructions, f);
26781591693c7b415e9869157c711fe11263c95d74eDavid Li   }
26791591693c7b415e9869157c711fe11263c95d74eDavid Li
26801591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Verify the return type of main() */
26811591693c7b415e9869157c711fe11263c95d74eDavid Li   if (strcmp(name, "main") == 0) {
26821591693c7b415e9869157c711fe11263c95d74eDavid Li      if (! return_type->is_void()) {
26831591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->get_location();
26841591693c7b415e9869157c711fe11263c95d74eDavid Li
26851591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state, "main() must return void");
26861591693c7b415e9869157c711fe11263c95d74eDavid Li      }
26871591693c7b415e9869157c711fe11263c95d74eDavid Li
26881591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!hir_parameters.is_empty()) {
26891591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->get_location();
26901591693c7b415e9869157c711fe11263c95d74eDavid Li
26911591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state, "main() must not take any parameters");
26921591693c7b415e9869157c711fe11263c95d74eDavid Li      }
26931591693c7b415e9869157c711fe11263c95d74eDavid Li   }
26941591693c7b415e9869157c711fe11263c95d74eDavid Li
26951591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Finish storing the information about this new function in its signature.
26961591693c7b415e9869157c711fe11263c95d74eDavid Li    */
26971591693c7b415e9869157c711fe11263c95d74eDavid Li   if (sig == NULL) {
26981591693c7b415e9869157c711fe11263c95d74eDavid Li      sig = new(ctx) ir_function_signature(return_type);
26991591693c7b415e9869157c711fe11263c95d74eDavid Li      f->add_signature(sig);
27001591693c7b415e9869157c711fe11263c95d74eDavid Li   }
27011591693c7b415e9869157c711fe11263c95d74eDavid Li
27021591693c7b415e9869157c711fe11263c95d74eDavid Li   sig->replace_parameters(&hir_parameters);
27031591693c7b415e9869157c711fe11263c95d74eDavid Li   signature = sig;
27041591693c7b415e9869157c711fe11263c95d74eDavid Li
27051591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Function declarations (prototypes) do not have r-values.
27061591693c7b415e9869157c711fe11263c95d74eDavid Li    */
27071591693c7b415e9869157c711fe11263c95d74eDavid Li   return NULL;
27081591693c7b415e9869157c711fe11263c95d74eDavid Li}
27091591693c7b415e9869157c711fe11263c95d74eDavid Li
27101591693c7b415e9869157c711fe11263c95d74eDavid Li
27111591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
27121591693c7b415e9869157c711fe11263c95d74eDavid Liast_function_definition::hir(exec_list *instructions,
27131591693c7b415e9869157c711fe11263c95d74eDavid Li			     struct _mesa_glsl_parse_state *state)
27141591693c7b415e9869157c711fe11263c95d74eDavid Li{
27151591693c7b415e9869157c711fe11263c95d74eDavid Li   prototype->is_definition = true;
27161591693c7b415e9869157c711fe11263c95d74eDavid Li   prototype->hir(instructions, state);
27171591693c7b415e9869157c711fe11263c95d74eDavid Li
27181591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_function_signature *signature = prototype->signature;
27191591693c7b415e9869157c711fe11263c95d74eDavid Li   if (signature == NULL)
27201591693c7b415e9869157c711fe11263c95d74eDavid Li      return NULL;
27211591693c7b415e9869157c711fe11263c95d74eDavid Li
27221591693c7b415e9869157c711fe11263c95d74eDavid Li   assert(state->current_function == NULL);
27231591693c7b415e9869157c711fe11263c95d74eDavid Li   state->current_function = signature;
27241591693c7b415e9869157c711fe11263c95d74eDavid Li   state->found_return = false;
27251591693c7b415e9869157c711fe11263c95d74eDavid Li
27261591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Duplicate parameters declared in the prototype as concrete variables.
27271591693c7b415e9869157c711fe11263c95d74eDavid Li    * Add these to the symbol table.
27281591693c7b415e9869157c711fe11263c95d74eDavid Li    */
27291591693c7b415e9869157c711fe11263c95d74eDavid Li   state->symbols->push_scope();
27301591693c7b415e9869157c711fe11263c95d74eDavid Li   foreach_iter(exec_list_iterator, iter, signature->parameters) {
27311591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
27321591693c7b415e9869157c711fe11263c95d74eDavid Li
27331591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(var != NULL);
27341591693c7b415e9869157c711fe11263c95d74eDavid Li
27351591693c7b415e9869157c711fe11263c95d74eDavid Li      /* The only way a parameter would "exist" is if two parameters have
27361591693c7b415e9869157c711fe11263c95d74eDavid Li       * the same name.
27371591693c7b415e9869157c711fe11263c95d74eDavid Li       */
27381591693c7b415e9869157c711fe11263c95d74eDavid Li      if (state->symbols->name_declared_this_scope(var->name)) {
27391591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->get_location();
27401591693c7b415e9869157c711fe11263c95d74eDavid Li
27411591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
27421591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
27431591693c7b415e9869157c711fe11263c95d74eDavid Li	 state->symbols->add_variable(var);
27441591693c7b415e9869157c711fe11263c95d74eDavid Li      }
27451591693c7b415e9869157c711fe11263c95d74eDavid Li   }
27461591693c7b415e9869157c711fe11263c95d74eDavid Li
27471591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Convert the body of the function to HIR. */
27481591693c7b415e9869157c711fe11263c95d74eDavid Li   this->body->hir(&signature->body, state);
27491591693c7b415e9869157c711fe11263c95d74eDavid Li   signature->is_defined = true;
27501591693c7b415e9869157c711fe11263c95d74eDavid Li
27511591693c7b415e9869157c711fe11263c95d74eDavid Li   state->symbols->pop_scope();
27521591693c7b415e9869157c711fe11263c95d74eDavid Li
27531591693c7b415e9869157c711fe11263c95d74eDavid Li   assert(state->current_function == signature);
27541591693c7b415e9869157c711fe11263c95d74eDavid Li   state->current_function = NULL;
27551591693c7b415e9869157c711fe11263c95d74eDavid Li
27561591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!signature->return_type->is_void() && !state->found_return) {
27571591693c7b415e9869157c711fe11263c95d74eDavid Li      YYLTYPE loc = this->get_location();
27581591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
27591591693c7b415e9869157c711fe11263c95d74eDavid Li		       "%s, but no return statement",
27601591693c7b415e9869157c711fe11263c95d74eDavid Li		       signature->function_name(),
27611591693c7b415e9869157c711fe11263c95d74eDavid Li		       signature->return_type->name);
27621591693c7b415e9869157c711fe11263c95d74eDavid Li   }
27631591693c7b415e9869157c711fe11263c95d74eDavid Li
27641591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Function definitions do not have r-values.
27651591693c7b415e9869157c711fe11263c95d74eDavid Li    */
27661591693c7b415e9869157c711fe11263c95d74eDavid Li   return NULL;
27671591693c7b415e9869157c711fe11263c95d74eDavid Li}
27681591693c7b415e9869157c711fe11263c95d74eDavid Li
27691591693c7b415e9869157c711fe11263c95d74eDavid Li
27701591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
27711591693c7b415e9869157c711fe11263c95d74eDavid Liast_jump_statement::hir(exec_list *instructions,
27721591693c7b415e9869157c711fe11263c95d74eDavid Li			struct _mesa_glsl_parse_state *state)
27731591693c7b415e9869157c711fe11263c95d74eDavid Li{
27741591693c7b415e9869157c711fe11263c95d74eDavid Li   void *ctx = state;
27751591693c7b415e9869157c711fe11263c95d74eDavid Li
27761591693c7b415e9869157c711fe11263c95d74eDavid Li   switch (mode) {
27771591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_return: {
27781591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_return *inst;
27791591693c7b415e9869157c711fe11263c95d74eDavid Li      assert(state->current_function);
27801591693c7b415e9869157c711fe11263c95d74eDavid Li
27811591693c7b415e9869157c711fe11263c95d74eDavid Li      if (opt_return_value) {
27821591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (state->current_function->return_type->base_type ==
27831591693c7b415e9869157c711fe11263c95d74eDavid Li	     GLSL_TYPE_VOID) {
27841591693c7b415e9869157c711fe11263c95d74eDavid Li	    YYLTYPE loc = this->get_location();
27851591693c7b415e9869157c711fe11263c95d74eDavid Li
27861591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
27871591693c7b415e9869157c711fe11263c95d74eDavid Li			     "`return` with a value, in function `%s' "
27881591693c7b415e9869157c711fe11263c95d74eDavid Li			     "returning void",
27891591693c7b415e9869157c711fe11263c95d74eDavid Li			     state->current_function->function_name());
27901591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
27911591693c7b415e9869157c711fe11263c95d74eDavid Li
27921591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_rvalue *const ret = opt_return_value->hir(instructions, state);
27931591693c7b415e9869157c711fe11263c95d74eDavid Li	 assert(ret != NULL);
27941591693c7b415e9869157c711fe11263c95d74eDavid Li
27951591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* Implicit conversions are not allowed for return values. */
27961591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (state->current_function->return_type != ret->type) {
27971591693c7b415e9869157c711fe11263c95d74eDavid Li	    YYLTYPE loc = this->get_location();
27981591693c7b415e9869157c711fe11263c95d74eDavid Li
27991591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
28001591693c7b415e9869157c711fe11263c95d74eDavid Li			     "`return' with wrong type %s, in function `%s' "
28011591693c7b415e9869157c711fe11263c95d74eDavid Li			     "returning %s",
28021591693c7b415e9869157c711fe11263c95d74eDavid Li			     ret->type->name,
28031591693c7b415e9869157c711fe11263c95d74eDavid Li			     state->current_function->function_name(),
28041591693c7b415e9869157c711fe11263c95d74eDavid Li			     state->current_function->return_type->name);
28051591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
28061591693c7b415e9869157c711fe11263c95d74eDavid Li
28071591693c7b415e9869157c711fe11263c95d74eDavid Li	 inst = new(ctx) ir_return(ret);
28081591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
28091591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (state->current_function->return_type->base_type !=
28101591693c7b415e9869157c711fe11263c95d74eDavid Li	     GLSL_TYPE_VOID) {
28111591693c7b415e9869157c711fe11263c95d74eDavid Li	    YYLTYPE loc = this->get_location();
28121591693c7b415e9869157c711fe11263c95d74eDavid Li
28131591693c7b415e9869157c711fe11263c95d74eDavid Li	    _mesa_glsl_error(& loc, state,
28141591693c7b415e9869157c711fe11263c95d74eDavid Li			     "`return' with no value, in function %s returning "
28151591693c7b415e9869157c711fe11263c95d74eDavid Li			     "non-void",
28161591693c7b415e9869157c711fe11263c95d74eDavid Li			     state->current_function->function_name());
28171591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
28181591693c7b415e9869157c711fe11263c95d74eDavid Li	 inst = new(ctx) ir_return;
28191591693c7b415e9869157c711fe11263c95d74eDavid Li      }
28201591693c7b415e9869157c711fe11263c95d74eDavid Li
28211591693c7b415e9869157c711fe11263c95d74eDavid Li      state->found_return = true;
28221591693c7b415e9869157c711fe11263c95d74eDavid Li      instructions->push_tail(inst);
28231591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
28241591693c7b415e9869157c711fe11263c95d74eDavid Li   }
28251591693c7b415e9869157c711fe11263c95d74eDavid Li
28261591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_discard:
28271591693c7b415e9869157c711fe11263c95d74eDavid Li      if (state->target != fragment_shader) {
28281591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->get_location();
28291591693c7b415e9869157c711fe11263c95d74eDavid Li
28301591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state,
28311591693c7b415e9869157c711fe11263c95d74eDavid Li			  "`discard' may only appear in a fragment shader");
28321591693c7b415e9869157c711fe11263c95d74eDavid Li      }
28331591693c7b415e9869157c711fe11263c95d74eDavid Li      instructions->push_tail(new(ctx) ir_discard);
28341591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
28351591693c7b415e9869157c711fe11263c95d74eDavid Li
28361591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_break:
28371591693c7b415e9869157c711fe11263c95d74eDavid Li   case ast_continue:
28381591693c7b415e9869157c711fe11263c95d74eDavid Li      /* FINISHME: Handle switch-statements.  They cannot contain 'continue',
28391591693c7b415e9869157c711fe11263c95d74eDavid Li       * FINISHME: and they use a different IR instruction for 'break'.
28401591693c7b415e9869157c711fe11263c95d74eDavid Li       */
28411591693c7b415e9869157c711fe11263c95d74eDavid Li      /* FINISHME: Correctly handle the nesting.  If a switch-statement is
28421591693c7b415e9869157c711fe11263c95d74eDavid Li       * FINISHME: inside a loop, a 'continue' is valid and will bind to the
28431591693c7b415e9869157c711fe11263c95d74eDavid Li       * FINISHME: loop.
28441591693c7b415e9869157c711fe11263c95d74eDavid Li       */
28451591693c7b415e9869157c711fe11263c95d74eDavid Li      if (state->loop_or_switch_nesting == NULL) {
28461591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->get_location();
28471591693c7b415e9869157c711fe11263c95d74eDavid Li
28481591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state,
28491591693c7b415e9869157c711fe11263c95d74eDavid Li			  "`%s' may only appear in a loop",
28501591693c7b415e9869157c711fe11263c95d74eDavid Li			  (mode == ast_break) ? "break" : "continue");
28511591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
28521591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
28531591693c7b415e9869157c711fe11263c95d74eDavid Li
28541591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* Inline the for loop expression again, since we don't know
28551591693c7b415e9869157c711fe11263c95d74eDavid Li	  * where near the end of the loop body the normal copy of it
28561591693c7b415e9869157c711fe11263c95d74eDavid Li	  * is going to be placed.
28571591693c7b415e9869157c711fe11263c95d74eDavid Li	  */
28581591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (mode == ast_continue &&
28591591693c7b415e9869157c711fe11263c95d74eDavid Li	     state->loop_or_switch_nesting_ast->rest_expression) {
28601591693c7b415e9869157c711fe11263c95d74eDavid Li	    state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
28611591693c7b415e9869157c711fe11263c95d74eDavid Li								    state);
28621591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
28631591693c7b415e9869157c711fe11263c95d74eDavid Li
28641591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (loop != NULL) {
28651591693c7b415e9869157c711fe11263c95d74eDavid Li	    ir_loop_jump *const jump =
28661591693c7b415e9869157c711fe11263c95d74eDavid Li	       new(ctx) ir_loop_jump((mode == ast_break)
28671591693c7b415e9869157c711fe11263c95d74eDavid Li				     ? ir_loop_jump::jump_break
28681591693c7b415e9869157c711fe11263c95d74eDavid Li				     : ir_loop_jump::jump_continue);
28691591693c7b415e9869157c711fe11263c95d74eDavid Li	    instructions->push_tail(jump);
28701591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
28711591693c7b415e9869157c711fe11263c95d74eDavid Li      }
28721591693c7b415e9869157c711fe11263c95d74eDavid Li
28731591693c7b415e9869157c711fe11263c95d74eDavid Li      break;
28741591693c7b415e9869157c711fe11263c95d74eDavid Li   }
28751591693c7b415e9869157c711fe11263c95d74eDavid Li
28761591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Jump instructions do not have r-values.
28771591693c7b415e9869157c711fe11263c95d74eDavid Li    */
28781591693c7b415e9869157c711fe11263c95d74eDavid Li   return NULL;
28791591693c7b415e9869157c711fe11263c95d74eDavid Li}
28801591693c7b415e9869157c711fe11263c95d74eDavid Li
28811591693c7b415e9869157c711fe11263c95d74eDavid Li
28821591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
28831591693c7b415e9869157c711fe11263c95d74eDavid Liast_selection_statement::hir(exec_list *instructions,
28841591693c7b415e9869157c711fe11263c95d74eDavid Li			     struct _mesa_glsl_parse_state *state)
28851591693c7b415e9869157c711fe11263c95d74eDavid Li{
28861591693c7b415e9869157c711fe11263c95d74eDavid Li   void *ctx = state;
28871591693c7b415e9869157c711fe11263c95d74eDavid Li
28881591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_rvalue *const condition = this->condition->hir(instructions, state);
28891591693c7b415e9869157c711fe11263c95d74eDavid Li
28901591693c7b415e9869157c711fe11263c95d74eDavid Li   /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
28911591693c7b415e9869157c711fe11263c95d74eDavid Li    *
28921591693c7b415e9869157c711fe11263c95d74eDavid Li    *    "Any expression whose type evaluates to a Boolean can be used as the
28931591693c7b415e9869157c711fe11263c95d74eDavid Li    *    conditional expression bool-expression. Vector types are not accepted
28941591693c7b415e9869157c711fe11263c95d74eDavid Li    *    as the expression to if."
28951591693c7b415e9869157c711fe11263c95d74eDavid Li    *
28961591693c7b415e9869157c711fe11263c95d74eDavid Li    * The checks are separated so that higher quality diagnostics can be
28971591693c7b415e9869157c711fe11263c95d74eDavid Li    * generated for cases where both rules are violated.
28981591693c7b415e9869157c711fe11263c95d74eDavid Li    */
28991591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
29001591693c7b415e9869157c711fe11263c95d74eDavid Li      YYLTYPE loc = this->condition->get_location();
29011591693c7b415e9869157c711fe11263c95d74eDavid Li
29021591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
29031591693c7b415e9869157c711fe11263c95d74eDavid Li		       "boolean");
29041591693c7b415e9869157c711fe11263c95d74eDavid Li   }
29051591693c7b415e9869157c711fe11263c95d74eDavid Li
29061591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_if *const stmt = new(ctx) ir_if(condition);
29071591693c7b415e9869157c711fe11263c95d74eDavid Li
29081591693c7b415e9869157c711fe11263c95d74eDavid Li   if (then_statement != NULL) {
29091591693c7b415e9869157c711fe11263c95d74eDavid Li      state->symbols->push_scope();
29101591693c7b415e9869157c711fe11263c95d74eDavid Li      then_statement->hir(& stmt->then_instructions, state);
29111591693c7b415e9869157c711fe11263c95d74eDavid Li      state->symbols->pop_scope();
29121591693c7b415e9869157c711fe11263c95d74eDavid Li   }
29131591693c7b415e9869157c711fe11263c95d74eDavid Li
29141591693c7b415e9869157c711fe11263c95d74eDavid Li   if (else_statement != NULL) {
29151591693c7b415e9869157c711fe11263c95d74eDavid Li      state->symbols->push_scope();
29161591693c7b415e9869157c711fe11263c95d74eDavid Li      else_statement->hir(& stmt->else_instructions, state);
29171591693c7b415e9869157c711fe11263c95d74eDavid Li      state->symbols->pop_scope();
29181591693c7b415e9869157c711fe11263c95d74eDavid Li   }
29191591693c7b415e9869157c711fe11263c95d74eDavid Li
29201591693c7b415e9869157c711fe11263c95d74eDavid Li   instructions->push_tail(stmt);
29211591693c7b415e9869157c711fe11263c95d74eDavid Li
29221591693c7b415e9869157c711fe11263c95d74eDavid Li   /* if-statements do not have r-values.
29231591693c7b415e9869157c711fe11263c95d74eDavid Li    */
29241591693c7b415e9869157c711fe11263c95d74eDavid Li   return NULL;
29251591693c7b415e9869157c711fe11263c95d74eDavid Li}
29261591693c7b415e9869157c711fe11263c95d74eDavid Li
29271591693c7b415e9869157c711fe11263c95d74eDavid Li
29281591693c7b415e9869157c711fe11263c95d74eDavid Livoid
29291591693c7b415e9869157c711fe11263c95d74eDavid Liast_iteration_statement::condition_to_hir(ir_loop *stmt,
29301591693c7b415e9869157c711fe11263c95d74eDavid Li					  struct _mesa_glsl_parse_state *state)
29311591693c7b415e9869157c711fe11263c95d74eDavid Li{
29321591693c7b415e9869157c711fe11263c95d74eDavid Li   void *ctx = state;
29331591693c7b415e9869157c711fe11263c95d74eDavid Li
29341591693c7b415e9869157c711fe11263c95d74eDavid Li   if (condition != NULL) {
29351591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_rvalue *const cond =
29361591693c7b415e9869157c711fe11263c95d74eDavid Li	 condition->hir(& stmt->body_instructions, state);
29371591693c7b415e9869157c711fe11263c95d74eDavid Li
29381591693c7b415e9869157c711fe11263c95d74eDavid Li      if ((cond == NULL)
29391591693c7b415e9869157c711fe11263c95d74eDavid Li	  || !cond->type->is_boolean() || !cond->type->is_scalar()) {
29401591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = condition->get_location();
29411591693c7b415e9869157c711fe11263c95d74eDavid Li
29421591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(& loc, state,
29431591693c7b415e9869157c711fe11263c95d74eDavid Li			  "loop condition must be scalar boolean");
29441591693c7b415e9869157c711fe11263c95d74eDavid Li      } else {
29451591693c7b415e9869157c711fe11263c95d74eDavid Li	 /* As the first code in the loop body, generate a block that looks
29461591693c7b415e9869157c711fe11263c95d74eDavid Li	  * like 'if (!condition) break;' as the loop termination condition.
29471591693c7b415e9869157c711fe11263c95d74eDavid Li	  */
29481591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_rvalue *const not_cond =
29491591693c7b415e9869157c711fe11263c95d74eDavid Li	    new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
29501591693c7b415e9869157c711fe11263c95d74eDavid Li				   NULL);
29511591693c7b415e9869157c711fe11263c95d74eDavid Li
29521591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_if *const if_stmt = new(ctx) ir_if(not_cond);
29531591693c7b415e9869157c711fe11263c95d74eDavid Li
29541591693c7b415e9869157c711fe11263c95d74eDavid Li	 ir_jump *const break_stmt =
29551591693c7b415e9869157c711fe11263c95d74eDavid Li	    new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
29561591693c7b415e9869157c711fe11263c95d74eDavid Li
29571591693c7b415e9869157c711fe11263c95d74eDavid Li	 if_stmt->then_instructions.push_tail(break_stmt);
29581591693c7b415e9869157c711fe11263c95d74eDavid Li	 stmt->body_instructions.push_tail(if_stmt);
29591591693c7b415e9869157c711fe11263c95d74eDavid Li      }
29601591693c7b415e9869157c711fe11263c95d74eDavid Li   }
29611591693c7b415e9869157c711fe11263c95d74eDavid Li}
29621591693c7b415e9869157c711fe11263c95d74eDavid Li
29631591693c7b415e9869157c711fe11263c95d74eDavid Li
29641591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
29651591693c7b415e9869157c711fe11263c95d74eDavid Liast_iteration_statement::hir(exec_list *instructions,
29661591693c7b415e9869157c711fe11263c95d74eDavid Li			     struct _mesa_glsl_parse_state *state)
29671591693c7b415e9869157c711fe11263c95d74eDavid Li{
29681591693c7b415e9869157c711fe11263c95d74eDavid Li   void *ctx = state;
29691591693c7b415e9869157c711fe11263c95d74eDavid Li
29701591693c7b415e9869157c711fe11263c95d74eDavid Li   /* For-loops and while-loops start a new scope, but do-while loops do not.
29711591693c7b415e9869157c711fe11263c95d74eDavid Li    */
29721591693c7b415e9869157c711fe11263c95d74eDavid Li   if (mode != ast_do_while)
29731591693c7b415e9869157c711fe11263c95d74eDavid Li      state->symbols->push_scope();
29741591693c7b415e9869157c711fe11263c95d74eDavid Li
29751591693c7b415e9869157c711fe11263c95d74eDavid Li   if (init_statement != NULL)
29761591693c7b415e9869157c711fe11263c95d74eDavid Li      init_statement->hir(instructions, state);
29771591693c7b415e9869157c711fe11263c95d74eDavid Li
29781591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_loop *const stmt = new(ctx) ir_loop();
29791591693c7b415e9869157c711fe11263c95d74eDavid Li   instructions->push_tail(stmt);
29801591693c7b415e9869157c711fe11263c95d74eDavid Li
29811591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Track the current loop and / or switch-statement nesting.
29821591693c7b415e9869157c711fe11263c95d74eDavid Li    */
29831591693c7b415e9869157c711fe11263c95d74eDavid Li   ir_instruction *const nesting = state->loop_or_switch_nesting;
29841591693c7b415e9869157c711fe11263c95d74eDavid Li   ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
29851591693c7b415e9869157c711fe11263c95d74eDavid Li
29861591693c7b415e9869157c711fe11263c95d74eDavid Li   state->loop_or_switch_nesting = stmt;
29871591693c7b415e9869157c711fe11263c95d74eDavid Li   state->loop_or_switch_nesting_ast = this;
29881591693c7b415e9869157c711fe11263c95d74eDavid Li
29891591693c7b415e9869157c711fe11263c95d74eDavid Li   if (mode != ast_do_while)
29901591693c7b415e9869157c711fe11263c95d74eDavid Li      condition_to_hir(stmt, state);
29911591693c7b415e9869157c711fe11263c95d74eDavid Li
29921591693c7b415e9869157c711fe11263c95d74eDavid Li   if (body != NULL)
29931591693c7b415e9869157c711fe11263c95d74eDavid Li      body->hir(& stmt->body_instructions, state);
29941591693c7b415e9869157c711fe11263c95d74eDavid Li
29951591693c7b415e9869157c711fe11263c95d74eDavid Li   if (rest_expression != NULL)
29961591693c7b415e9869157c711fe11263c95d74eDavid Li      rest_expression->hir(& stmt->body_instructions, state);
29971591693c7b415e9869157c711fe11263c95d74eDavid Li
29981591693c7b415e9869157c711fe11263c95d74eDavid Li   if (mode == ast_do_while)
29991591693c7b415e9869157c711fe11263c95d74eDavid Li      condition_to_hir(stmt, state);
30001591693c7b415e9869157c711fe11263c95d74eDavid Li
30011591693c7b415e9869157c711fe11263c95d74eDavid Li   if (mode != ast_do_while)
30021591693c7b415e9869157c711fe11263c95d74eDavid Li      state->symbols->pop_scope();
30031591693c7b415e9869157c711fe11263c95d74eDavid Li
30041591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Restore previous nesting before returning.
30051591693c7b415e9869157c711fe11263c95d74eDavid Li    */
30061591693c7b415e9869157c711fe11263c95d74eDavid Li   state->loop_or_switch_nesting = nesting;
30071591693c7b415e9869157c711fe11263c95d74eDavid Li   state->loop_or_switch_nesting_ast = nesting_ast;
30081591693c7b415e9869157c711fe11263c95d74eDavid Li
30091591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Loops do not have r-values.
30101591693c7b415e9869157c711fe11263c95d74eDavid Li    */
30111591693c7b415e9869157c711fe11263c95d74eDavid Li   return NULL;
30121591693c7b415e9869157c711fe11263c95d74eDavid Li}
30131591693c7b415e9869157c711fe11263c95d74eDavid Li
30141591693c7b415e9869157c711fe11263c95d74eDavid Li
30151591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
30161591693c7b415e9869157c711fe11263c95d74eDavid Liast_type_specifier::hir(exec_list *instructions,
30171591693c7b415e9869157c711fe11263c95d74eDavid Li			  struct _mesa_glsl_parse_state *state)
30181591693c7b415e9869157c711fe11263c95d74eDavid Li{
30191591693c7b415e9869157c711fe11263c95d74eDavid Li   if (this->structure != NULL)
30201591693c7b415e9869157c711fe11263c95d74eDavid Li      return this->structure->hir(instructions, state);
30211591693c7b415e9869157c711fe11263c95d74eDavid Li
30221591693c7b415e9869157c711fe11263c95d74eDavid Li   return NULL;
30231591693c7b415e9869157c711fe11263c95d74eDavid Li}
30241591693c7b415e9869157c711fe11263c95d74eDavid Li
30251591693c7b415e9869157c711fe11263c95d74eDavid Li
30261591693c7b415e9869157c711fe11263c95d74eDavid Liir_rvalue *
30271591693c7b415e9869157c711fe11263c95d74eDavid Liast_struct_specifier::hir(exec_list *instructions,
30281591693c7b415e9869157c711fe11263c95d74eDavid Li			  struct _mesa_glsl_parse_state *state)
30291591693c7b415e9869157c711fe11263c95d74eDavid Li{
30301591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned decl_count = 0;
30311591693c7b415e9869157c711fe11263c95d74eDavid Li
30321591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Make an initial pass over the list of structure fields to determine how
30331591693c7b415e9869157c711fe11263c95d74eDavid Li    * many there are.  Each element in this list is an ast_declarator_list.
30341591693c7b415e9869157c711fe11263c95d74eDavid Li    * This means that we actually need to count the number of elements in the
30351591693c7b415e9869157c711fe11263c95d74eDavid Li    * 'declarations' list in each of the elements.
30361591693c7b415e9869157c711fe11263c95d74eDavid Li    */
30371591693c7b415e9869157c711fe11263c95d74eDavid Li   foreach_list_typed (ast_declarator_list, decl_list, link,
30381591693c7b415e9869157c711fe11263c95d74eDavid Li		       &this->declarations) {
30391591693c7b415e9869157c711fe11263c95d74eDavid Li      foreach_list_const (decl_ptr, & decl_list->declarations) {
30401591693c7b415e9869157c711fe11263c95d74eDavid Li	 decl_count++;
30411591693c7b415e9869157c711fe11263c95d74eDavid Li      }
30421591693c7b415e9869157c711fe11263c95d74eDavid Li   }
30431591693c7b415e9869157c711fe11263c95d74eDavid Li
30441591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Allocate storage for the structure fields and process the field
30451591693c7b415e9869157c711fe11263c95d74eDavid Li    * declarations.  As the declarations are processed, try to also convert
30461591693c7b415e9869157c711fe11263c95d74eDavid Li    * the types to HIR.  This ensures that structure definitions embedded in
30471591693c7b415e9869157c711fe11263c95d74eDavid Li    * other structure definitions are processed.
30481591693c7b415e9869157c711fe11263c95d74eDavid Li    */
3049d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li   glsl_struct_field *const fields = hieralloc_array(state, glsl_struct_field,
30501591693c7b415e9869157c711fe11263c95d74eDavid Li						  decl_count);
30511591693c7b415e9869157c711fe11263c95d74eDavid Li
30521591693c7b415e9869157c711fe11263c95d74eDavid Li   unsigned i = 0;
30531591693c7b415e9869157c711fe11263c95d74eDavid Li   foreach_list_typed (ast_declarator_list, decl_list, link,
30541591693c7b415e9869157c711fe11263c95d74eDavid Li		       &this->declarations) {
30551591693c7b415e9869157c711fe11263c95d74eDavid Li      const char *type_name;
30561591693c7b415e9869157c711fe11263c95d74eDavid Li
30571591693c7b415e9869157c711fe11263c95d74eDavid Li      decl_list->type->specifier->hir(instructions, state);
30581591693c7b415e9869157c711fe11263c95d74eDavid Li
30591591693c7b415e9869157c711fe11263c95d74eDavid Li      /* Section 10.9 of the GLSL ES 1.00 specification states that
30601591693c7b415e9869157c711fe11263c95d74eDavid Li       * embedded structure definitions have been removed from the language.
30611591693c7b415e9869157c711fe11263c95d74eDavid Li       */
30621591693c7b415e9869157c711fe11263c95d74eDavid Li      if (state->es_shader && decl_list->type->specifier->structure != NULL) {
30631591693c7b415e9869157c711fe11263c95d74eDavid Li	 YYLTYPE loc = this->get_location();
30641591693c7b415e9869157c711fe11263c95d74eDavid Li	 _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
30651591693c7b415e9869157c711fe11263c95d74eDavid Li			  "not allowed in GLSL ES 1.00.");
30661591693c7b415e9869157c711fe11263c95d74eDavid Li      }
30671591693c7b415e9869157c711fe11263c95d74eDavid Li
30681591693c7b415e9869157c711fe11263c95d74eDavid Li      const glsl_type *decl_type =
30691591693c7b415e9869157c711fe11263c95d74eDavid Li	 decl_list->type->specifier->glsl_type(& type_name, state);
30701591693c7b415e9869157c711fe11263c95d74eDavid Li
30711591693c7b415e9869157c711fe11263c95d74eDavid Li      foreach_list_typed (ast_declaration, decl, link,
30721591693c7b415e9869157c711fe11263c95d74eDavid Li			  &decl_list->declarations) {
30731591693c7b415e9869157c711fe11263c95d74eDavid Li	 const struct glsl_type *field_type = decl_type;
30741591693c7b415e9869157c711fe11263c95d74eDavid Li	 if (decl->is_array) {
30751591693c7b415e9869157c711fe11263c95d74eDavid Li	    YYLTYPE loc = decl->get_location();
30761591693c7b415e9869157c711fe11263c95d74eDavid Li	    field_type = process_array_type(&loc, decl_type, decl->array_size,
30771591693c7b415e9869157c711fe11263c95d74eDavid Li					    state);
30781591693c7b415e9869157c711fe11263c95d74eDavid Li	 }
30791591693c7b415e9869157c711fe11263c95d74eDavid Li	 fields[i].type = (field_type != NULL)
30801591693c7b415e9869157c711fe11263c95d74eDavid Li	    ? field_type : glsl_type::error_type;
30811591693c7b415e9869157c711fe11263c95d74eDavid Li	 fields[i].name = decl->identifier;
30821591693c7b415e9869157c711fe11263c95d74eDavid Li	 i++;
30831591693c7b415e9869157c711fe11263c95d74eDavid Li      }
30841591693c7b415e9869157c711fe11263c95d74eDavid Li   }
30851591693c7b415e9869157c711fe11263c95d74eDavid Li
30861591693c7b415e9869157c711fe11263c95d74eDavid Li   assert(i == decl_count);
30871591693c7b415e9869157c711fe11263c95d74eDavid Li
30881591693c7b415e9869157c711fe11263c95d74eDavid Li   const glsl_type *t =
30891591693c7b415e9869157c711fe11263c95d74eDavid Li      glsl_type::get_record_instance(fields, decl_count, this->name);
30901591693c7b415e9869157c711fe11263c95d74eDavid Li
30911591693c7b415e9869157c711fe11263c95d74eDavid Li   YYLTYPE loc = this->get_location();
30921591693c7b415e9869157c711fe11263c95d74eDavid Li   if (!state->symbols->add_type(name, t)) {
30931591693c7b415e9869157c711fe11263c95d74eDavid Li      _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
30941591693c7b415e9869157c711fe11263c95d74eDavid Li   } else {
30951591693c7b415e9869157c711fe11263c95d74eDavid Li
30961591693c7b415e9869157c711fe11263c95d74eDavid Li      const glsl_type **s = (const glsl_type **)
30971591693c7b415e9869157c711fe11263c95d74eDavid Li	 realloc(state->user_structures,
30981591693c7b415e9869157c711fe11263c95d74eDavid Li		 sizeof(state->user_structures[0]) *
30991591693c7b415e9869157c711fe11263c95d74eDavid Li		 (state->num_user_structures + 1));
31001591693c7b415e9869157c711fe11263c95d74eDavid Li      if (s != NULL) {
31011591693c7b415e9869157c711fe11263c95d74eDavid Li	 s[state->num_user_structures] = t;
31021591693c7b415e9869157c711fe11263c95d74eDavid Li	 state->user_structures = s;
31031591693c7b415e9869157c711fe11263c95d74eDavid Li	 state->num_user_structures++;
31041591693c7b415e9869157c711fe11263c95d74eDavid Li      }
31051591693c7b415e9869157c711fe11263c95d74eDavid Li   }
31061591693c7b415e9869157c711fe11263c95d74eDavid Li
31071591693c7b415e9869157c711fe11263c95d74eDavid Li   /* Structure type definitions do not have r-values.
31081591693c7b415e9869157c711fe11263c95d74eDavid Li    */
31091591693c7b415e9869157c711fe11263c95d74eDavid Li   return NULL;
31101591693c7b415e9869157c711fe11263c95d74eDavid Li}
3111