Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright © 2010 Intel Corporation
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21.  * DEALINGS IN THE SOFTWARE.
  22.  */
  23.  
  24. /**
  25.  * \file ast_to_hir.c
  26.  * Convert abstract syntax to to high-level intermediate reprensentation (HIR).
  27.  *
  28.  * During the conversion to HIR, the majority of the symantic checking is
  29.  * preformed on the program.  This includes:
  30.  *
  31.  *    * Symbol table management
  32.  *    * Type checking
  33.  *    * Function binding
  34.  *
  35.  * The majority of this work could be done during parsing, and the parser could
  36.  * probably generate HIR directly.  However, this results in frequent changes
  37.  * to the parser code.  Since we do not assume that every system this complier
  38.  * is built on will have Flex and Bison installed, we have to store the code
  39.  * generated by these tools in our version control system.  In other parts of
  40.  * the system we've seen problems where a parser was changed but the generated
  41.  * code was not committed, merge conflicts where created because two developers
  42.  * had slightly different versions of Bison installed, etc.
  43.  *
  44.  * I have also noticed that running Bison generated parsers in GDB is very
  45.  * irritating.  When you get a segfault on '$$ = $1->foo', you can't very
  46.  * well 'print $1' in GDB.
  47.  *
  48.  * As a result, my preference is to put as little C code as possible in the
  49.  * parser (and lexer) sources.
  50.  */
  51.  
  52. #include "main/core.h" /* for struct gl_extensions */
  53. #include "glsl_symbol_table.h"
  54. #include "glsl_parser_extras.h"
  55. #include "ast.h"
  56. #include "glsl_types.h"
  57. #include "ir.h"
  58.  
  59. void
  60. _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
  61. {
  62.    _mesa_glsl_initialize_variables(instructions, state);
  63.    _mesa_glsl_initialize_functions(instructions, state);
  64.  
  65.    state->symbols->language_version = state->language_version;
  66.  
  67.    state->current_function = NULL;
  68.  
  69.    /* Section 4.2 of the GLSL 1.20 specification states:
  70.     * "The built-in functions are scoped in a scope outside the global scope
  71.     *  users declare global variables in.  That is, a shader's global scope,
  72.     *  available for user-defined functions and global variables, is nested
  73.     *  inside the scope containing the built-in functions."
  74.     *
  75.     * Since built-in functions like ftransform() access built-in variables,
  76.     * it follows that those must be in the outer scope as well.
  77.     *
  78.     * We push scope here to create this nesting effect...but don't pop.
  79.     * This way, a shader's globals are still in the symbol table for use
  80.     * by the linker.
  81.     */
  82.    state->symbols->push_scope();
  83.  
  84.    foreach_list_typed (ast_node, ast, link, & state->translation_unit)
  85.       ast->hir(instructions, state);
  86. }
  87.  
  88.  
  89. /**
  90.  * If a conversion is available, convert one operand to a different type
  91.  *
  92.  * The \c from \c ir_rvalue is converted "in place".
  93.  *
  94.  * \param to     Type that the operand it to be converted to
  95.  * \param from   Operand that is being converted
  96.  * \param state  GLSL compiler state
  97.  *
  98.  * \return
  99.  * If a conversion is possible (or unnecessary), \c true is returned.
  100.  * Otherwise \c false is returned.
  101.  */
  102. bool
  103. apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
  104.                           struct _mesa_glsl_parse_state *state)
  105. {
  106.    void *ctx = state;
  107.    if (to->base_type == from->type->base_type)
  108.       return true;
  109.  
  110.    /* This conversion was added in GLSL 1.20.  If the compilation mode is
  111.     * GLSL 1.10, the conversion is skipped.
  112.     */
  113.    if (state->language_version < 120)
  114.       return false;
  115.  
  116.    /* From page 27 (page 33 of the PDF) of the GLSL 1.50 spec:
  117.     *
  118.     *    "There are no implicit array or structure conversions. For
  119.     *    example, an array of int cannot be implicitly converted to an
  120.     *    array of float. There are no implicit conversions between
  121.     *    signed and unsigned integers."
  122.     */
  123.    /* FINISHME: The above comment is partially a lie.  There is int/uint
  124.     * FINISHME: conversion for immediate constants.
  125.     */
  126.    if (!to->is_float() || !from->type->is_numeric())
  127.       return false;
  128.  
  129.    /* Convert to a floating point type with the same number of components
  130.     * as the original type - i.e. int to float, not int to vec4.
  131.     */
  132.    to = glsl_type::get_instance(GLSL_TYPE_FLOAT, from->type->vector_elements,
  133.                                 from->type->matrix_columns);
  134.  
  135.    switch (from->type->base_type) {
  136.    case GLSL_TYPE_INT:
  137.       from = new(ctx) ir_expression(ir_unop_i2f, to, from, NULL);
  138.       break;
  139.    case GLSL_TYPE_UINT:
  140.       from = new(ctx) ir_expression(ir_unop_u2f, to, from, NULL);
  141.       break;
  142.    case GLSL_TYPE_BOOL:
  143.       from = new(ctx) ir_expression(ir_unop_b2f, to, from, NULL);
  144.       break;
  145.    default:
  146.       assert(0);
  147.    }
  148.  
  149.    return true;
  150. }
  151.  
  152.  
  153. static const struct glsl_type *
  154. arithmetic_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
  155.                        bool multiply,
  156.                        struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  157. {
  158.    const glsl_type *type_a = value_a->type;
  159.    const glsl_type *type_b = value_b->type;
  160.  
  161.    /* From GLSL 1.50 spec, page 56:
  162.     *
  163.     *    "The arithmetic binary operators add (+), subtract (-),
  164.     *    multiply (*), and divide (/) operate on integer and
  165.     *    floating-point scalars, vectors, and matrices."
  166.     */
  167.    if (!type_a->is_numeric() || !type_b->is_numeric()) {
  168.       _mesa_glsl_error(loc, state,
  169.                        "Operands to arithmetic operators must be numeric");
  170.       return glsl_type::error_type;
  171.    }
  172.  
  173.  
  174.    /*    "If one operand is floating-point based and the other is
  175.     *    not, then the conversions from Section 4.1.10 "Implicit
  176.     *    Conversions" are applied to the non-floating-point-based operand."
  177.     */
  178.    if (!apply_implicit_conversion(type_a, value_b, state)
  179.        && !apply_implicit_conversion(type_b, value_a, state)) {
  180.       _mesa_glsl_error(loc, state,
  181.                        "Could not implicitly convert operands to "
  182.                        "arithmetic operator");
  183.       return glsl_type::error_type;
  184.    }
  185.    type_a = value_a->type;
  186.    type_b = value_b->type;
  187.  
  188.    /*    "If the operands are integer types, they must both be signed or
  189.     *    both be unsigned."
  190.     *
  191.     * From this rule and the preceeding conversion it can be inferred that
  192.     * both types must be GLSL_TYPE_FLOAT, or GLSL_TYPE_UINT, or GLSL_TYPE_INT.
  193.     * The is_numeric check above already filtered out the case where either
  194.     * type is not one of these, so now the base types need only be tested for
  195.     * equality.
  196.     */
  197.    if (type_a->base_type != type_b->base_type) {
  198.       _mesa_glsl_error(loc, state,
  199.                        "base type mismatch for arithmetic operator");
  200.       return glsl_type::error_type;
  201.    }
  202.  
  203.    /*    "All arithmetic binary operators result in the same fundamental type
  204.     *    (signed integer, unsigned integer, or floating-point) as the
  205.     *    operands they operate on, after operand type conversion. After
  206.     *    conversion, the following cases are valid
  207.     *
  208.     *    * The two operands are scalars. In this case the operation is
  209.     *      applied, resulting in a scalar."
  210.     */
  211.    if (type_a->is_scalar() && type_b->is_scalar())
  212.       return type_a;
  213.  
  214.    /*   "* One operand is a scalar, and the other is a vector or matrix.
  215.     *      In this case, the scalar operation is applied independently to each
  216.     *      component of the vector or matrix, resulting in the same size
  217.     *      vector or matrix."
  218.     */
  219.    if (type_a->is_scalar()) {
  220.       if (!type_b->is_scalar())
  221.          return type_b;
  222.    } else if (type_b->is_scalar()) {
  223.       return type_a;
  224.    }
  225.  
  226.    /* All of the combinations of <scalar, scalar>, <vector, scalar>,
  227.     * <scalar, vector>, <scalar, matrix>, and <matrix, scalar> have been
  228.     * handled.
  229.     */
  230.    assert(!type_a->is_scalar());
  231.    assert(!type_b->is_scalar());
  232.  
  233.    /*   "* The two operands are vectors of the same size. In this case, the
  234.     *      operation is done component-wise resulting in the same size
  235.     *      vector."
  236.     */
  237.    if (type_a->is_vector() && type_b->is_vector()) {
  238.       if (type_a == type_b) {
  239.          return type_a;
  240.       } else {
  241.          _mesa_glsl_error(loc, state,
  242.                           "vector size mismatch for arithmetic operator");
  243.          return glsl_type::error_type;
  244.       }
  245.    }
  246.  
  247.    /* All of the combinations of <scalar, scalar>, <vector, scalar>,
  248.     * <scalar, vector>, <scalar, matrix>, <matrix, scalar>, and
  249.     * <vector, vector> have been handled.  At least one of the operands must
  250.     * be matrix.  Further, since there are no integer matrix types, the base
  251.     * type of both operands must be float.
  252.     */
  253.    assert(type_a->is_matrix() || type_b->is_matrix());
  254.    assert(type_a->base_type == GLSL_TYPE_FLOAT);
  255.    assert(type_b->base_type == GLSL_TYPE_FLOAT);
  256.  
  257.    /*   "* The operator is add (+), subtract (-), or divide (/), and the
  258.     *      operands are matrices with the same number of rows and the same
  259.     *      number of columns. In this case, the operation is done component-
  260.     *      wise resulting in the same size matrix."
  261.     *    * The operator is multiply (*), where both operands are matrices or
  262.     *      one operand is a vector and the other a matrix. A right vector
  263.     *      operand is treated as a column vector and a left vector operand as a
  264.     *      row vector. In all these cases, it is required that the number of
  265.     *      columns of the left operand is equal to the number of rows of the
  266.     *      right operand. Then, the multiply (*) operation does a linear
  267.     *      algebraic multiply, yielding an object that has the same number of
  268.     *      rows as the left operand and the same number of columns as the right
  269.     *      operand. Section 5.10 "Vector and Matrix Operations" explains in
  270.     *      more detail how vectors and matrices are operated on."
  271.     */
  272.    if (! multiply) {
  273.       if (type_a == type_b)
  274.          return type_a;
  275.    } else {
  276.       if (type_a->is_matrix() && type_b->is_matrix()) {
  277.          /* Matrix multiply.  The columns of A must match the rows of B.  Given
  278.           * the other previously tested constraints, this means the vector type
  279.           * of a row from A must be the same as the vector type of a column from
  280.           * B.
  281.           */
  282.          if (type_a->row_type() == type_b->column_type()) {
  283.             /* The resulting matrix has the number of columns of matrix B and
  284.              * the number of rows of matrix A.  We get the row count of A by
  285.              * looking at the size of a vector that makes up a column.  The
  286.              * transpose (size of a row) is done for B.
  287.              */
  288.             const glsl_type *const type =
  289.                glsl_type::get_instance(type_a->base_type,
  290.                                        type_a->column_type()->vector_elements,
  291.                                        type_b->row_type()->vector_elements);
  292.             assert(type != glsl_type::error_type);
  293.  
  294.             return type;
  295.          }
  296.       } else if (type_a->is_matrix()) {
  297.          /* A is a matrix and B is a column vector.  Columns of A must match
  298.           * rows of B.  Given the other previously tested constraints, this
  299.           * means the vector type of a row from A must be the same as the
  300.           * vector the type of B.
  301.           */
  302.          if (type_a->row_type() == type_b) {
  303.             /* The resulting vector has a number of elements equal to
  304.              * the number of rows of matrix A. */
  305.             const glsl_type *const type =
  306.                glsl_type::get_instance(type_a->base_type,
  307.                                        type_a->column_type()->vector_elements,
  308.                                        1);
  309.             assert(type != glsl_type::error_type);
  310.  
  311.             return type;
  312.          }
  313.       } else {
  314.          assert(type_b->is_matrix());
  315.  
  316.          /* A is a row vector and B is a matrix.  Columns of A must match rows
  317.           * of B.  Given the other previously tested constraints, this means
  318.           * the type of A must be the same as the vector type of a column from
  319.           * B.
  320.           */
  321.          if (type_a == type_b->column_type()) {
  322.             /* The resulting vector has a number of elements equal to
  323.              * the number of columns of matrix B. */
  324.             const glsl_type *const type =
  325.                glsl_type::get_instance(type_a->base_type,
  326.                                        type_b->row_type()->vector_elements,
  327.                                        1);
  328.             assert(type != glsl_type::error_type);
  329.  
  330.             return type;
  331.          }
  332.       }
  333.  
  334.       _mesa_glsl_error(loc, state, "size mismatch for matrix multiplication");
  335.       return glsl_type::error_type;
  336.    }
  337.  
  338.  
  339.    /*    "All other cases are illegal."
  340.     */
  341.    _mesa_glsl_error(loc, state, "type mismatch");
  342.    return glsl_type::error_type;
  343. }
  344.  
  345.  
  346. static const struct glsl_type *
  347. unary_arithmetic_result_type(const struct glsl_type *type,
  348.                              struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  349. {
  350.    /* From GLSL 1.50 spec, page 57:
  351.     *
  352.     *    "The arithmetic unary operators negate (-), post- and pre-increment
  353.     *     and decrement (-- and ++) operate on integer or floating-point
  354.     *     values (including vectors and matrices). All unary operators work
  355.     *     component-wise on their operands. These result with the same type
  356.     *     they operated on."
  357.     */
  358.    if (!type->is_numeric()) {
  359.       _mesa_glsl_error(loc, state,
  360.                        "Operands to arithmetic operators must be numeric");
  361.       return glsl_type::error_type;
  362.    }
  363.  
  364.    return type;
  365. }
  366.  
  367. /**
  368.  * \brief Return the result type of a bit-logic operation.
  369.  *
  370.  * If the given types to the bit-logic operator are invalid, return
  371.  * glsl_type::error_type.
  372.  *
  373.  * \param type_a Type of LHS of bit-logic op
  374.  * \param type_b Type of RHS of bit-logic op
  375.  */
  376. static const struct glsl_type *
  377. bit_logic_result_type(const struct glsl_type *type_a,
  378.                       const struct glsl_type *type_b,
  379.                       ast_operators op,
  380.                       struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  381. {
  382.     if (state->language_version < 130) {
  383.        _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
  384.        return glsl_type::error_type;
  385.     }
  386.  
  387.     /* From page 50 (page 56 of PDF) of GLSL 1.30 spec:
  388.      *
  389.      *     "The bitwise operators and (&), exclusive-or (^), and inclusive-or
  390.      *     (|). The operands must be of type signed or unsigned integers or
  391.      *     integer vectors."
  392.      */
  393.     if (!type_a->is_integer()) {
  394.        _mesa_glsl_error(loc, state, "LHS of `%s' must be an integer",
  395.                          ast_expression::operator_string(op));
  396.        return glsl_type::error_type;
  397.     }
  398.     if (!type_b->is_integer()) {
  399.        _mesa_glsl_error(loc, state, "RHS of `%s' must be an integer",
  400.                         ast_expression::operator_string(op));
  401.        return glsl_type::error_type;
  402.     }
  403.  
  404.     /*     "The fundamental types of the operands (signed or unsigned) must
  405.      *     match,"
  406.      */
  407.     if (type_a->base_type != type_b->base_type) {
  408.        _mesa_glsl_error(loc, state, "operands of `%s' must have the same "
  409.                         "base type", ast_expression::operator_string(op));
  410.        return glsl_type::error_type;
  411.     }
  412.  
  413.     /*     "The operands cannot be vectors of differing size." */
  414.     if (type_a->is_vector() &&
  415.         type_b->is_vector() &&
  416.         type_a->vector_elements != type_b->vector_elements) {
  417.        _mesa_glsl_error(loc, state, "operands of `%s' cannot be vectors of "
  418.                         "different sizes", ast_expression::operator_string(op));
  419.        return glsl_type::error_type;
  420.     }
  421.  
  422.     /*     "If one operand is a scalar and the other a vector, the scalar is
  423.      *     applied component-wise to the vector, resulting in the same type as
  424.      *     the vector. The fundamental types of the operands [...] will be the
  425.      *     resulting fundamental type."
  426.      */
  427.     if (type_a->is_scalar())
  428.         return type_b;
  429.     else
  430.         return type_a;
  431. }
  432.  
  433. static const struct glsl_type *
  434. modulus_result_type(const struct glsl_type *type_a,
  435.                     const struct glsl_type *type_b,
  436.                     struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  437. {
  438.    if (state->language_version < 130) {
  439.       _mesa_glsl_error(loc, state,
  440.                        "operator '%%' is reserved in %s",
  441.                        state->version_string);
  442.       return glsl_type::error_type;
  443.    }
  444.  
  445.    /* From GLSL 1.50 spec, page 56:
  446.     *    "The operator modulus (%) operates on signed or unsigned integers or
  447.     *    integer vectors. The operand types must both be signed or both be
  448.     *    unsigned."
  449.     */
  450.    if (!type_a->is_integer() || !type_b->is_integer()
  451.        || (type_a->base_type != type_b->base_type)) {
  452.       _mesa_glsl_error(loc, state, "type mismatch");
  453.       return glsl_type::error_type;
  454.    }
  455.  
  456.    /*    "The operands cannot be vectors of differing size. If one operand is
  457.     *    a scalar and the other vector, then the scalar is applied component-
  458.     *    wise to the vector, resulting in the same type as the vector. If both
  459.     *    are vectors of the same size, the result is computed component-wise."
  460.     */
  461.    if (type_a->is_vector()) {
  462.       if (!type_b->is_vector()
  463.           || (type_a->vector_elements == type_b->vector_elements))
  464.          return type_a;
  465.    } else
  466.       return type_b;
  467.  
  468.    /*    "The operator modulus (%) is not defined for any other data types
  469.     *    (non-integer types)."
  470.     */
  471.    _mesa_glsl_error(loc, state, "type mismatch");
  472.    return glsl_type::error_type;
  473. }
  474.  
  475.  
  476. static const struct glsl_type *
  477. relational_result_type(ir_rvalue * &value_a, ir_rvalue * &value_b,
  478.                        struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  479. {
  480.    const glsl_type *type_a = value_a->type;
  481.    const glsl_type *type_b = value_b->type;
  482.  
  483.    /* From GLSL 1.50 spec, page 56:
  484.     *    "The relational operators greater than (>), less than (<), greater
  485.     *    than or equal (>=), and less than or equal (<=) operate only on
  486.     *    scalar integer and scalar floating-point expressions."
  487.     */
  488.    if (!type_a->is_numeric()
  489.        || !type_b->is_numeric()
  490.        || !type_a->is_scalar()
  491.        || !type_b->is_scalar()) {
  492.       _mesa_glsl_error(loc, state,
  493.                        "Operands to relational operators must be scalar and "
  494.                        "numeric");
  495.       return glsl_type::error_type;
  496.    }
  497.  
  498.    /*    "Either the operands' types must match, or the conversions from
  499.     *    Section 4.1.10 "Implicit Conversions" will be applied to the integer
  500.     *    operand, after which the types must match."
  501.     */
  502.    if (!apply_implicit_conversion(type_a, value_b, state)
  503.        && !apply_implicit_conversion(type_b, value_a, state)) {
  504.       _mesa_glsl_error(loc, state,
  505.                        "Could not implicitly convert operands to "
  506.                        "relational operator");
  507.       return glsl_type::error_type;
  508.    }
  509.    type_a = value_a->type;
  510.    type_b = value_b->type;
  511.  
  512.    if (type_a->base_type != type_b->base_type) {
  513.       _mesa_glsl_error(loc, state, "base type mismatch");
  514.       return glsl_type::error_type;
  515.    }
  516.  
  517.    /*    "The result is scalar Boolean."
  518.     */
  519.    return glsl_type::bool_type;
  520. }
  521.  
  522. /**
  523.  * \brief Return the result type of a bit-shift operation.
  524.  *
  525.  * If the given types to the bit-shift operator are invalid, return
  526.  * glsl_type::error_type.
  527.  *
  528.  * \param type_a Type of LHS of bit-shift op
  529.  * \param type_b Type of RHS of bit-shift op
  530.  */
  531. static const struct glsl_type *
  532. shift_result_type(const struct glsl_type *type_a,
  533.                   const struct glsl_type *type_b,
  534.                   ast_operators op,
  535.                   struct _mesa_glsl_parse_state *state, YYLTYPE *loc)
  536. {
  537.    if (state->language_version < 130) {
  538.       _mesa_glsl_error(loc, state, "bit operations require GLSL 1.30");
  539.       return glsl_type::error_type;
  540.    }
  541.  
  542.    /* From page 50 (page 56 of the PDF) of the GLSL 1.30 spec:
  543.     *
  544.     *     "The shift operators (<<) and (>>). For both operators, the operands
  545.     *     must be signed or unsigned integers or integer vectors. One operand
  546.     *     can be signed while the other is unsigned."
  547.     */
  548.    if (!type_a->is_integer()) {
  549.       _mesa_glsl_error(loc, state, "LHS of operator %s must be an integer or "
  550.               "integer vector", ast_expression::operator_string(op));
  551.      return glsl_type::error_type;
  552.  
  553.    }
  554.    if (!type_b->is_integer()) {
  555.       _mesa_glsl_error(loc, state, "RHS of operator %s must be an integer or "
  556.               "integer vector", ast_expression::operator_string(op));
  557.      return glsl_type::error_type;
  558.    }
  559.  
  560.    /*     "If the first operand is a scalar, the second operand has to be
  561.     *     a scalar as well."
  562.     */
  563.    if (type_a->is_scalar() && !type_b->is_scalar()) {
  564.       _mesa_glsl_error(loc, state, "If the first operand of %s is scalar, the "
  565.               "second must be scalar as well",
  566.               ast_expression::operator_string(op));
  567.      return glsl_type::error_type;
  568.    }
  569.  
  570.    /* If both operands are vectors, check that they have same number of
  571.     * elements.
  572.     */
  573.    if (type_a->is_vector() &&
  574.       type_b->is_vector() &&
  575.       type_a->vector_elements != type_b->vector_elements) {
  576.       _mesa_glsl_error(loc, state, "Vector operands to operator %s must "
  577.               "have same number of elements",
  578.               ast_expression::operator_string(op));
  579.      return glsl_type::error_type;
  580.    }
  581.  
  582.    /*     "In all cases, the resulting type will be the same type as the left
  583.     *     operand."
  584.     */
  585.    return type_a;
  586. }
  587.  
  588. /**
  589.  * Validates that a value can be assigned to a location with a specified type
  590.  *
  591.  * Validates that \c rhs can be assigned to some location.  If the types are
  592.  * not an exact match but an automatic conversion is possible, \c rhs will be
  593.  * converted.
  594.  *
  595.  * \return
  596.  * \c NULL if \c rhs cannot be assigned to a location with type \c lhs_type.
  597.  * Otherwise the actual RHS to be assigned will be returned.  This may be
  598.  * \c rhs, or it may be \c rhs after some type conversion.
  599.  *
  600.  * \note
  601.  * In addition to being used for assignments, this function is used to
  602.  * type-check return values.
  603.  */
  604. ir_rvalue *
  605. validate_assignment(struct _mesa_glsl_parse_state *state,
  606.                     const glsl_type *lhs_type, ir_rvalue *rhs)
  607. {
  608.    const glsl_type *rhs_type = rhs->type;
  609.  
  610.    /* If there is already some error in the RHS, just return it.  Anything
  611.     * else will lead to an avalanche of error message back to the user.
  612.     */
  613.    if (rhs_type->is_error())
  614.       return rhs;
  615.  
  616.    /* If the types are identical, the assignment can trivially proceed.
  617.     */
  618.    if (rhs_type == lhs_type)
  619.       return rhs;
  620.  
  621.    /* If the array element types are the same and the size of the LHS is zero,
  622.     * the assignment is okay.
  623.     *
  624.     * Note: Whole-array assignments are not permitted in GLSL 1.10, but this
  625.     * is handled by ir_dereference::is_lvalue.
  626.     */
  627.    if (lhs_type->is_array() && rhs->type->is_array()
  628.        && (lhs_type->element_type() == rhs->type->element_type())
  629.        && (lhs_type->array_size() == 0)) {
  630.       return rhs;
  631.    }
  632.  
  633.    /* Check for implicit conversion in GLSL 1.20 */
  634.    if (apply_implicit_conversion(lhs_type, rhs, state)) {
  635.       rhs_type = rhs->type;
  636.       if (rhs_type == lhs_type)
  637.          return rhs;
  638.    }
  639.  
  640.    return NULL;
  641. }
  642.  
  643. ir_rvalue *
  644. do_assignment(exec_list *instructions, struct _mesa_glsl_parse_state *state,
  645.               ir_rvalue *lhs, ir_rvalue *rhs,
  646.               YYLTYPE lhs_loc)
  647. {
  648.    void *ctx = state;
  649.    bool error_emitted = (lhs->type->is_error() || rhs->type->is_error());
  650.  
  651.    if (!error_emitted) {
  652.       if (!lhs->is_lvalue()) {
  653.          _mesa_glsl_error(& lhs_loc, state, "non-lvalue in assignment");
  654.          error_emitted = true;
  655.       }
  656.  
  657.       if (state->es_shader && lhs->type->is_array()) {
  658.          _mesa_glsl_error(&lhs_loc, state, "whole array assignment is not "
  659.                           "allowed in GLSL ES 1.00.");
  660.          error_emitted = true;
  661.       }
  662.    }
  663.  
  664.    ir_rvalue *new_rhs = validate_assignment(state, lhs->type, rhs);
  665.    if (new_rhs == NULL) {
  666.       _mesa_glsl_error(& lhs_loc, state, "type mismatch");
  667.    } else {
  668.       rhs = new_rhs;
  669.  
  670.       /* If the LHS array was not declared with a size, it takes it size from
  671.        * the RHS.  If the LHS is an l-value and a whole array, it must be a
  672.        * dereference of a variable.  Any other case would require that the LHS
  673.        * is either not an l-value or not a whole array.
  674.        */
  675.       if (lhs->type->array_size() == 0) {
  676.          ir_dereference *const d = lhs->as_dereference();
  677.  
  678.          assert(d != NULL);
  679.  
  680.          ir_variable *const var = d->variable_referenced();
  681.  
  682.          assert(var != NULL);
  683.  
  684.          if (var->max_array_access >= unsigned(rhs->type->array_size())) {
  685.             /* FINISHME: This should actually log the location of the RHS. */
  686.             _mesa_glsl_error(& lhs_loc, state, "array size must be > %u due to "
  687.                              "previous access",
  688.                              var->max_array_access);
  689.          }
  690.  
  691.          var->type = glsl_type::get_array_instance(lhs->type->element_type(),
  692.                                                    rhs->type->array_size());
  693.          d->type = var->type;
  694.       }
  695.    }
  696.  
  697.    /* Most callers of do_assignment (assign, add_assign, pre_inc/dec,
  698.     * but not post_inc) need the converted assigned value as an rvalue
  699.     * to handle things like:
  700.     *
  701.     * i = j += 1;
  702.     *
  703.     * So we always just store the computed value being assigned to a
  704.     * temporary and return a deref of that temporary.  If the rvalue
  705.     * ends up not being used, the temp will get copy-propagated out.
  706.     */
  707.    ir_variable *var = new(ctx) ir_variable(rhs->type, "assignment_tmp",
  708.                                            ir_var_temporary);
  709.    ir_dereference_variable *deref_var = new(ctx) ir_dereference_variable(var);
  710.    instructions->push_tail(var);
  711.    instructions->push_tail(new(ctx) ir_assignment(deref_var,
  712.                                                   rhs,
  713.                                                   NULL));
  714.    deref_var = new(ctx) ir_dereference_variable(var);
  715.  
  716.    if (!error_emitted)
  717.       instructions->push_tail(new(ctx) ir_assignment(lhs, deref_var, NULL));
  718.  
  719.    return new(ctx) ir_dereference_variable(var);
  720. }
  721.  
  722. static ir_rvalue *
  723. get_lvalue_copy(exec_list *instructions, ir_rvalue *lvalue)
  724. {
  725.    void *ctx = ralloc_parent(lvalue);
  726.    ir_variable *var;
  727.  
  728.    var = new(ctx) ir_variable(lvalue->type, "_post_incdec_tmp",
  729.                               ir_var_temporary);
  730.    instructions->push_tail(var);
  731.    var->mode = ir_var_auto;
  732.  
  733.    instructions->push_tail(new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
  734.                                                   lvalue, NULL));
  735.  
  736.    /* Once we've created this temporary, mark it read only so it's no
  737.     * longer considered an lvalue.
  738.     */
  739.    var->read_only = true;
  740.  
  741.    return new(ctx) ir_dereference_variable(var);
  742. }
  743.  
  744.  
  745. ir_rvalue *
  746. ast_node::hir(exec_list *instructions,
  747.               struct _mesa_glsl_parse_state *state)
  748. {
  749.    (void) instructions;
  750.    (void) state;
  751.  
  752.    return NULL;
  753. }
  754.  
  755. static void
  756. mark_whole_array_access(ir_rvalue *access)
  757. {
  758.    ir_dereference_variable *deref = access->as_dereference_variable();
  759.  
  760.    if (deref) {
  761.       deref->var->max_array_access = deref->type->length - 1;
  762.    }
  763. }
  764.  
  765. static ir_rvalue *
  766. do_comparison(void *mem_ctx, int operation, ir_rvalue *op0, ir_rvalue *op1)
  767. {
  768.    int join_op;
  769.    ir_rvalue *cmp = NULL;
  770.  
  771.    if (operation == ir_binop_all_equal)
  772.       join_op = ir_binop_logic_and;
  773.    else
  774.       join_op = ir_binop_logic_or;
  775.  
  776.    switch (op0->type->base_type) {
  777.    case GLSL_TYPE_FLOAT:
  778.    case GLSL_TYPE_UINT:
  779.    case GLSL_TYPE_INT:
  780.    case GLSL_TYPE_BOOL:
  781.       return new(mem_ctx) ir_expression(operation, op0, op1);
  782.  
  783.    case GLSL_TYPE_ARRAY: {
  784.       for (unsigned int i = 0; i < op0->type->length; i++) {
  785.          ir_rvalue *e0, *e1, *result;
  786.  
  787.          e0 = new(mem_ctx) ir_dereference_array(op0->clone(mem_ctx, NULL),
  788.                                                 new(mem_ctx) ir_constant(i));
  789.          e1 = new(mem_ctx) ir_dereference_array(op1->clone(mem_ctx, NULL),
  790.                                                 new(mem_ctx) ir_constant(i));
  791.          result = do_comparison(mem_ctx, operation, e0, e1);
  792.  
  793.          if (cmp) {
  794.             cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
  795.          } else {
  796.             cmp = result;
  797.          }
  798.       }
  799.  
  800.       mark_whole_array_access(op0);
  801.       mark_whole_array_access(op1);
  802.       break;
  803.    }
  804.  
  805.    case GLSL_TYPE_STRUCT: {
  806.       for (unsigned int i = 0; i < op0->type->length; i++) {
  807.          ir_rvalue *e0, *e1, *result;
  808.          const char *field_name = op0->type->fields.structure[i].name;
  809.  
  810.          e0 = new(mem_ctx) ir_dereference_record(op0->clone(mem_ctx, NULL),
  811.                                                  field_name);
  812.          e1 = new(mem_ctx) ir_dereference_record(op1->clone(mem_ctx, NULL),
  813.                                                  field_name);
  814.          result = do_comparison(mem_ctx, operation, e0, e1);
  815.  
  816.          if (cmp) {
  817.             cmp = new(mem_ctx) ir_expression(join_op, cmp, result);
  818.          } else {
  819.             cmp = result;
  820.          }
  821.       }
  822.       break;
  823.    }
  824.  
  825.    case GLSL_TYPE_ERROR:
  826.    case GLSL_TYPE_VOID:
  827.    case GLSL_TYPE_SAMPLER:
  828.       /* I assume a comparison of a struct containing a sampler just
  829.        * ignores the sampler present in the type.
  830.        */
  831.       break;
  832.  
  833.    default:
  834.       assert(!"Should not get here.");
  835.       break;
  836.    }
  837.  
  838.    if (cmp == NULL)
  839.       cmp = new(mem_ctx) ir_constant(true);
  840.  
  841.    return cmp;
  842. }
  843.  
  844. ir_rvalue *
  845. ast_expression::hir(exec_list *instructions,
  846.                     struct _mesa_glsl_parse_state *state)
  847. {
  848.    void *ctx = state;
  849.    static const int operations[AST_NUM_OPERATORS] = {
  850.       -1,               /* ast_assign doesn't convert to ir_expression. */
  851.       -1,               /* ast_plus doesn't convert to ir_expression. */
  852.       ir_unop_neg,
  853.       ir_binop_add,
  854.       ir_binop_sub,
  855.       ir_binop_mul,
  856.       ir_binop_div,
  857.       ir_binop_mod,
  858.       ir_binop_lshift,
  859.       ir_binop_rshift,
  860.       ir_binop_less,
  861.       ir_binop_greater,
  862.       ir_binop_lequal,
  863.       ir_binop_gequal,
  864.       ir_binop_all_equal,
  865.       ir_binop_any_nequal,
  866.       ir_binop_bit_and,
  867.       ir_binop_bit_xor,
  868.       ir_binop_bit_or,
  869.       ir_unop_bit_not,
  870.       ir_binop_logic_and,
  871.       ir_binop_logic_xor,
  872.       ir_binop_logic_or,
  873.       ir_unop_logic_not,
  874.  
  875.       /* Note: The following block of expression types actually convert
  876.        * to multiple IR instructions.
  877.        */
  878.       ir_binop_mul,     /* ast_mul_assign */
  879.       ir_binop_div,     /* ast_div_assign */
  880.       ir_binop_mod,     /* ast_mod_assign */
  881.       ir_binop_add,     /* ast_add_assign */
  882.       ir_binop_sub,     /* ast_sub_assign */
  883.       ir_binop_lshift,  /* ast_ls_assign */
  884.       ir_binop_rshift,  /* ast_rs_assign */
  885.       ir_binop_bit_and, /* ast_and_assign */
  886.       ir_binop_bit_xor, /* ast_xor_assign */
  887.       ir_binop_bit_or,  /* ast_or_assign */
  888.  
  889.       -1,               /* ast_conditional doesn't convert to ir_expression. */
  890.       ir_binop_add,     /* ast_pre_inc. */
  891.       ir_binop_sub,     /* ast_pre_dec. */
  892.       ir_binop_add,     /* ast_post_inc. */
  893.       ir_binop_sub,     /* ast_post_dec. */
  894.       -1,               /* ast_field_selection doesn't conv to ir_expression. */
  895.       -1,               /* ast_array_index doesn't convert to ir_expression. */
  896.       -1,               /* ast_function_call doesn't conv to ir_expression. */
  897.       -1,               /* ast_identifier doesn't convert to ir_expression. */
  898.       -1,               /* ast_int_constant doesn't convert to ir_expression. */
  899.       -1,               /* ast_uint_constant doesn't conv to ir_expression. */
  900.       -1,               /* ast_float_constant doesn't conv to ir_expression. */
  901.       -1,               /* ast_bool_constant doesn't conv to ir_expression. */
  902.       -1,               /* ast_sequence doesn't convert to ir_expression. */
  903.    };
  904.    ir_rvalue *result = NULL;
  905.    ir_rvalue *op[3];
  906.    const struct glsl_type *type = glsl_type::error_type;
  907.    bool error_emitted = false;
  908.    YYLTYPE loc;
  909.  
  910.    loc = this->get_location();
  911.  
  912.    switch (this->oper) {
  913.    case ast_assign: {
  914.       op[0] = this->subexpressions[0]->hir(instructions, state);
  915.       op[1] = this->subexpressions[1]->hir(instructions, state);
  916.  
  917.       result = do_assignment(instructions, state, op[0], op[1],
  918.                              this->subexpressions[0]->get_location());
  919.       error_emitted = result->type->is_error();
  920.       type = result->type;
  921.       break;
  922.    }
  923.  
  924.    case ast_plus:
  925.       op[0] = this->subexpressions[0]->hir(instructions, state);
  926.  
  927.       type = unary_arithmetic_result_type(op[0]->type, state, & loc);
  928.  
  929.       error_emitted = type->is_error();
  930.  
  931.       result = op[0];
  932.       break;
  933.  
  934.    case ast_neg:
  935.       op[0] = this->subexpressions[0]->hir(instructions, state);
  936.  
  937.       type = unary_arithmetic_result_type(op[0]->type, state, & loc);
  938.  
  939.       error_emitted = type->is_error();
  940.  
  941.       result = new(ctx) ir_expression(operations[this->oper], type,
  942.                                       op[0], NULL);
  943.       break;
  944.  
  945.    case ast_add:
  946.    case ast_sub:
  947.    case ast_mul:
  948.    case ast_div:
  949.       op[0] = this->subexpressions[0]->hir(instructions, state);
  950.       op[1] = this->subexpressions[1]->hir(instructions, state);
  951.  
  952.       type = arithmetic_result_type(op[0], op[1],
  953.                                     (this->oper == ast_mul),
  954.                                     state, & loc);
  955.       error_emitted = type->is_error();
  956.  
  957.       result = new(ctx) ir_expression(operations[this->oper], type,
  958.                                       op[0], op[1]);
  959.       break;
  960.  
  961.    case ast_mod:
  962.       op[0] = this->subexpressions[0]->hir(instructions, state);
  963.       op[1] = this->subexpressions[1]->hir(instructions, state);
  964.  
  965.       type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
  966.  
  967.       assert(operations[this->oper] == ir_binop_mod);
  968.  
  969.       result = new(ctx) ir_expression(operations[this->oper], type,
  970.                                       op[0], op[1]);
  971.       error_emitted = type->is_error();
  972.       break;
  973.  
  974.    case ast_lshift:
  975.    case ast_rshift:
  976.        if (state->language_version < 130) {
  977.           _mesa_glsl_error(&loc, state, "operator %s requires GLSL 1.30",
  978.               operator_string(this->oper));
  979.           error_emitted = true;
  980.        }
  981.  
  982.        op[0] = this->subexpressions[0]->hir(instructions, state);
  983.        op[1] = this->subexpressions[1]->hir(instructions, state);
  984.        type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
  985.                                 &loc);
  986.        result = new(ctx) ir_expression(operations[this->oper], type,
  987.                                        op[0], op[1]);
  988.        error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  989.        break;
  990.  
  991.    case ast_less:
  992.    case ast_greater:
  993.    case ast_lequal:
  994.    case ast_gequal:
  995.       op[0] = this->subexpressions[0]->hir(instructions, state);
  996.       op[1] = this->subexpressions[1]->hir(instructions, state);
  997.  
  998.       type = relational_result_type(op[0], op[1], state, & loc);
  999.  
  1000.       /* The relational operators must either generate an error or result
  1001.        * in a scalar boolean.  See page 57 of the GLSL 1.50 spec.
  1002.        */
  1003.       assert(type->is_error()
  1004.              || ((type->base_type == GLSL_TYPE_BOOL)
  1005.                  && type->is_scalar()));
  1006.  
  1007.       result = new(ctx) ir_expression(operations[this->oper], type,
  1008.                                       op[0], op[1]);
  1009.       error_emitted = type->is_error();
  1010.       break;
  1011.  
  1012.    case ast_nequal:
  1013.    case ast_equal:
  1014.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1015.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1016.  
  1017.       /* From page 58 (page 64 of the PDF) of the GLSL 1.50 spec:
  1018.        *
  1019.        *    "The equality operators equal (==), and not equal (!=)
  1020.        *    operate on all types. They result in a scalar Boolean. If
  1021.        *    the operand types do not match, then there must be a
  1022.        *    conversion from Section 4.1.10 "Implicit Conversions"
  1023.        *    applied to one operand that can make them match, in which
  1024.        *    case this conversion is done."
  1025.        */
  1026.       if ((!apply_implicit_conversion(op[0]->type, op[1], state)
  1027.            && !apply_implicit_conversion(op[1]->type, op[0], state))
  1028.           || (op[0]->type != op[1]->type)) {
  1029.          _mesa_glsl_error(& loc, state, "operands of `%s' must have the same "
  1030.                           "type", (this->oper == ast_equal) ? "==" : "!=");
  1031.          error_emitted = true;
  1032.       } else if ((state->language_version <= 110)
  1033.                  && (op[0]->type->is_array() || op[1]->type->is_array())) {
  1034.          _mesa_glsl_error(& loc, state, "array comparisons forbidden in "
  1035.                           "GLSL 1.10");
  1036.          error_emitted = true;
  1037.       }
  1038.  
  1039.       result = do_comparison(ctx, operations[this->oper], op[0], op[1]);
  1040.       type = glsl_type::bool_type;
  1041.  
  1042.       assert(error_emitted || (result->type == glsl_type::bool_type));
  1043.       break;
  1044.  
  1045.    case ast_bit_and:
  1046.    case ast_bit_xor:
  1047.    case ast_bit_or:
  1048.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1049.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1050.       type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
  1051.                                    state, &loc);
  1052.       result = new(ctx) ir_expression(operations[this->oper], type,
  1053.                                       op[0], op[1]);
  1054.       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1055.       break;
  1056.  
  1057.    case ast_bit_not:
  1058.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1059.  
  1060.       if (state->language_version < 130) {
  1061.          _mesa_glsl_error(&loc, state, "bit-wise operations require GLSL 1.30");
  1062.          error_emitted = true;
  1063.       }
  1064.  
  1065.       if (!op[0]->type->is_integer()) {
  1066.          _mesa_glsl_error(&loc, state, "operand of `~' must be an integer");
  1067.          error_emitted = true;
  1068.       }
  1069.  
  1070.       type = op[0]->type;
  1071.       result = new(ctx) ir_expression(ir_unop_bit_not, type, op[0], NULL);
  1072.       break;
  1073.  
  1074.    case ast_logic_and: {
  1075.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1076.  
  1077.       if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
  1078.          YYLTYPE loc = this->subexpressions[0]->get_location();
  1079.  
  1080.          _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
  1081.                           operator_string(this->oper));
  1082.          error_emitted = true;
  1083.       }
  1084.  
  1085.       ir_constant *op0_const = op[0]->constant_expression_value();
  1086.       if (op0_const) {
  1087.          if (op0_const->value.b[0]) {
  1088.             op[1] = this->subexpressions[1]->hir(instructions, state);
  1089.  
  1090.             if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
  1091.                YYLTYPE loc = this->subexpressions[1]->get_location();
  1092.  
  1093.                _mesa_glsl_error(& loc, state,
  1094.                                 "RHS of `%s' must be scalar boolean",
  1095.                                 operator_string(this->oper));
  1096.                error_emitted = true;
  1097.             }
  1098.             result = op[1];
  1099.          } else {
  1100.             result = op0_const;
  1101.          }
  1102.          type = glsl_type::bool_type;
  1103.       } else {
  1104.          ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
  1105.                                                        "and_tmp",
  1106.                                                        ir_var_temporary);
  1107.          instructions->push_tail(tmp);
  1108.  
  1109.          ir_if *const stmt = new(ctx) ir_if(op[0]);
  1110.          instructions->push_tail(stmt);
  1111.  
  1112.          op[1] = this->subexpressions[1]->hir(&stmt->then_instructions, state);
  1113.  
  1114.          if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
  1115.             YYLTYPE loc = this->subexpressions[1]->get_location();
  1116.  
  1117.             _mesa_glsl_error(& loc, state,
  1118.                              "RHS of `%s' must be scalar boolean",
  1119.                              operator_string(this->oper));
  1120.             error_emitted = true;
  1121.          }
  1122.  
  1123.          ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
  1124.          ir_assignment *const then_assign =
  1125.             new(ctx) ir_assignment(then_deref, op[1], NULL);
  1126.          stmt->then_instructions.push_tail(then_assign);
  1127.  
  1128.          ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
  1129.          ir_assignment *const else_assign =
  1130.             new(ctx) ir_assignment(else_deref, new(ctx) ir_constant(false), NULL);
  1131.          stmt->else_instructions.push_tail(else_assign);
  1132.  
  1133.          result = new(ctx) ir_dereference_variable(tmp);
  1134.          type = tmp->type;
  1135.       }
  1136.       break;
  1137.    }
  1138.  
  1139.    case ast_logic_or: {
  1140.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1141.  
  1142.       if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
  1143.          YYLTYPE loc = this->subexpressions[0]->get_location();
  1144.  
  1145.          _mesa_glsl_error(& loc, state, "LHS of `%s' must be scalar boolean",
  1146.                           operator_string(this->oper));
  1147.          error_emitted = true;
  1148.       }
  1149.  
  1150.       ir_constant *op0_const = op[0]->constant_expression_value();
  1151.       if (op0_const) {
  1152.          if (op0_const->value.b[0]) {
  1153.             result = op0_const;
  1154.          } else {
  1155.             op[1] = this->subexpressions[1]->hir(instructions, state);
  1156.  
  1157.             if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
  1158.                YYLTYPE loc = this->subexpressions[1]->get_location();
  1159.  
  1160.                _mesa_glsl_error(& loc, state,
  1161.                                 "RHS of `%s' must be scalar boolean",
  1162.                                 operator_string(this->oper));
  1163.                error_emitted = true;
  1164.             }
  1165.             result = op[1];
  1166.          }
  1167.          type = glsl_type::bool_type;
  1168.       } else {
  1169.          ir_variable *const tmp = new(ctx) ir_variable(glsl_type::bool_type,
  1170.                                                        "or_tmp",
  1171.                                                        ir_var_temporary);
  1172.          instructions->push_tail(tmp);
  1173.  
  1174.          ir_if *const stmt = new(ctx) ir_if(op[0]);
  1175.          instructions->push_tail(stmt);
  1176.  
  1177.          op[1] = this->subexpressions[1]->hir(&stmt->else_instructions, state);
  1178.  
  1179.          if (!op[1]->type->is_boolean() || !op[1]->type->is_scalar()) {
  1180.             YYLTYPE loc = this->subexpressions[1]->get_location();
  1181.  
  1182.             _mesa_glsl_error(& loc, state, "RHS of `%s' must be scalar boolean",
  1183.                              operator_string(this->oper));
  1184.             error_emitted = true;
  1185.          }
  1186.  
  1187.          ir_dereference *const then_deref = new(ctx) ir_dereference_variable(tmp);
  1188.          ir_assignment *const then_assign =
  1189.             new(ctx) ir_assignment(then_deref, new(ctx) ir_constant(true), NULL);
  1190.          stmt->then_instructions.push_tail(then_assign);
  1191.  
  1192.          ir_dereference *const else_deref = new(ctx) ir_dereference_variable(tmp);
  1193.          ir_assignment *const else_assign =
  1194.             new(ctx) ir_assignment(else_deref, op[1], NULL);
  1195.          stmt->else_instructions.push_tail(else_assign);
  1196.  
  1197.          result = new(ctx) ir_dereference_variable(tmp);
  1198.          type = tmp->type;
  1199.       }
  1200.       break;
  1201.    }
  1202.  
  1203.    case ast_logic_xor:
  1204.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1205.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1206.  
  1207.  
  1208.       result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
  1209.                                       op[0], op[1]);
  1210.       type = glsl_type::bool_type;
  1211.       break;
  1212.  
  1213.    case ast_logic_not:
  1214.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1215.  
  1216.       if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
  1217.          YYLTYPE loc = this->subexpressions[0]->get_location();
  1218.  
  1219.          _mesa_glsl_error(& loc, state,
  1220.                           "operand of `!' must be scalar boolean");
  1221.          error_emitted = true;
  1222.       }
  1223.  
  1224.       result = new(ctx) ir_expression(operations[this->oper], glsl_type::bool_type,
  1225.                                       op[0], NULL);
  1226.       type = glsl_type::bool_type;
  1227.       break;
  1228.  
  1229.    case ast_mul_assign:
  1230.    case ast_div_assign:
  1231.    case ast_add_assign:
  1232.    case ast_sub_assign: {
  1233.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1234.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1235.  
  1236.       type = arithmetic_result_type(op[0], op[1],
  1237.                                     (this->oper == ast_mul_assign),
  1238.                                     state, & loc);
  1239.  
  1240.       ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1241.                                                    op[0], op[1]);
  1242.  
  1243.       result = do_assignment(instructions, state,
  1244.                              op[0]->clone(ctx, NULL), temp_rhs,
  1245.                              this->subexpressions[0]->get_location());
  1246.       type = result->type;
  1247.       error_emitted = (op[0]->type->is_error());
  1248.  
  1249.       /* GLSL 1.10 does not allow array assignment.  However, we don't have to
  1250.        * explicitly test for this because none of the binary expression
  1251.        * operators allow array operands either.
  1252.        */
  1253.  
  1254.       break;
  1255.    }
  1256.  
  1257.    case ast_mod_assign: {
  1258.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1259.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1260.  
  1261.       type = modulus_result_type(op[0]->type, op[1]->type, state, & loc);
  1262.  
  1263.       assert(operations[this->oper] == ir_binop_mod);
  1264.  
  1265.       ir_rvalue *temp_rhs;
  1266.       temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1267.                                         op[0], op[1]);
  1268.  
  1269.       result = do_assignment(instructions, state,
  1270.                              op[0]->clone(ctx, NULL), temp_rhs,
  1271.                              this->subexpressions[0]->get_location());
  1272.       type = result->type;
  1273.       error_emitted = type->is_error();
  1274.       break;
  1275.    }
  1276.  
  1277.    case ast_ls_assign:
  1278.    case ast_rs_assign: {
  1279.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1280.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1281.       type = shift_result_type(op[0]->type, op[1]->type, this->oper, state,
  1282.                                &loc);
  1283.       ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
  1284.                                                    type, op[0], op[1]);
  1285.       result = do_assignment(instructions, state, op[0]->clone(ctx, NULL),
  1286.                              temp_rhs,
  1287.                              this->subexpressions[0]->get_location());
  1288.       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1289.       break;
  1290.    }
  1291.  
  1292.    case ast_and_assign:
  1293.    case ast_xor_assign:
  1294.    case ast_or_assign: {
  1295.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1296.       op[1] = this->subexpressions[1]->hir(instructions, state);
  1297.       type = bit_logic_result_type(op[0]->type, op[1]->type, this->oper,
  1298.                                    state, &loc);
  1299.       ir_rvalue *temp_rhs = new(ctx) ir_expression(operations[this->oper],
  1300.                                                    type, op[0], op[1]);
  1301.       result = do_assignment(instructions, state, op[0]->clone(ctx, NULL),
  1302.                              temp_rhs,
  1303.                              this->subexpressions[0]->get_location());
  1304.       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1305.       break;
  1306.    }
  1307.  
  1308.    case ast_conditional: {
  1309.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1310.  
  1311.       /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
  1312.        *
  1313.        *    "The ternary selection operator (?:). It operates on three
  1314.        *    expressions (exp1 ? exp2 : exp3). This operator evaluates the
  1315.        *    first expression, which must result in a scalar Boolean."
  1316.        */
  1317.       if (!op[0]->type->is_boolean() || !op[0]->type->is_scalar()) {
  1318.          YYLTYPE loc = this->subexpressions[0]->get_location();
  1319.  
  1320.          _mesa_glsl_error(& loc, state, "?: condition must be scalar boolean");
  1321.          error_emitted = true;
  1322.       }
  1323.  
  1324.       /* The :? operator is implemented by generating an anonymous temporary
  1325.        * followed by an if-statement.  The last instruction in each branch of
  1326.        * the if-statement assigns a value to the anonymous temporary.  This
  1327.        * temporary is the r-value of the expression.
  1328.        */
  1329.       exec_list then_instructions;
  1330.       exec_list else_instructions;
  1331.  
  1332.       op[1] = this->subexpressions[1]->hir(&then_instructions, state);
  1333.       op[2] = this->subexpressions[2]->hir(&else_instructions, state);
  1334.  
  1335.       /* From page 59 (page 65 of the PDF) of the GLSL 1.50 spec:
  1336.        *
  1337.        *     "The second and third expressions can be any type, as
  1338.        *     long their types match, or there is a conversion in
  1339.        *     Section 4.1.10 "Implicit Conversions" that can be applied
  1340.        *     to one of the expressions to make their types match. This
  1341.        *     resulting matching type is the type of the entire
  1342.        *     expression."
  1343.        */
  1344.       if ((!apply_implicit_conversion(op[1]->type, op[2], state)
  1345.            && !apply_implicit_conversion(op[2]->type, op[1], state))
  1346.           || (op[1]->type != op[2]->type)) {
  1347.          YYLTYPE loc = this->subexpressions[1]->get_location();
  1348.  
  1349.          _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
  1350.                           "operator must have matching types.");
  1351.          error_emitted = true;
  1352.          type = glsl_type::error_type;
  1353.       } else {
  1354.          type = op[1]->type;
  1355.       }
  1356.  
  1357.       /* From page 33 (page 39 of the PDF) of the GLSL 1.10 spec:
  1358.        *
  1359.        *    "The second and third expressions must be the same type, but can
  1360.        *    be of any type other than an array."
  1361.        */
  1362.       if ((state->language_version <= 110) && type->is_array()) {
  1363.          _mesa_glsl_error(& loc, state, "Second and third operands of ?: "
  1364.                           "operator must not be arrays.");
  1365.          error_emitted = true;
  1366.       }
  1367.  
  1368.       ir_constant *cond_val = op[0]->constant_expression_value();
  1369.       ir_constant *then_val = op[1]->constant_expression_value();
  1370.       ir_constant *else_val = op[2]->constant_expression_value();
  1371.  
  1372.       if (then_instructions.is_empty()
  1373.           && else_instructions.is_empty()
  1374.           && (cond_val != NULL) && (then_val != NULL) && (else_val != NULL)) {
  1375.          result = (cond_val->value.b[0]) ? then_val : else_val;
  1376.       } else {
  1377.          ir_variable *const tmp =
  1378.             new(ctx) ir_variable(type, "conditional_tmp", ir_var_temporary);
  1379.          instructions->push_tail(tmp);
  1380.  
  1381.          ir_if *const stmt = new(ctx) ir_if(op[0]);
  1382.          instructions->push_tail(stmt);
  1383.  
  1384.          then_instructions.move_nodes_to(& stmt->then_instructions);
  1385.          ir_dereference *const then_deref =
  1386.             new(ctx) ir_dereference_variable(tmp);
  1387.          ir_assignment *const then_assign =
  1388.             new(ctx) ir_assignment(then_deref, op[1], NULL);
  1389.          stmt->then_instructions.push_tail(then_assign);
  1390.  
  1391.          else_instructions.move_nodes_to(& stmt->else_instructions);
  1392.          ir_dereference *const else_deref =
  1393.             new(ctx) ir_dereference_variable(tmp);
  1394.          ir_assignment *const else_assign =
  1395.             new(ctx) ir_assignment(else_deref, op[2], NULL);
  1396.          stmt->else_instructions.push_tail(else_assign);
  1397.  
  1398.          result = new(ctx) ir_dereference_variable(tmp);
  1399.       }
  1400.       break;
  1401.    }
  1402.  
  1403.    case ast_pre_inc:
  1404.    case ast_pre_dec: {
  1405.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1406.       if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
  1407.          op[1] = new(ctx) ir_constant(1.0f);
  1408.       else
  1409.          op[1] = new(ctx) ir_constant(1);
  1410.  
  1411.       type = arithmetic_result_type(op[0], op[1], false, state, & loc);
  1412.  
  1413.       ir_rvalue *temp_rhs;
  1414.       temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1415.                                         op[0], op[1]);
  1416.  
  1417.       result = do_assignment(instructions, state,
  1418.                              op[0]->clone(ctx, NULL), temp_rhs,
  1419.                              this->subexpressions[0]->get_location());
  1420.       type = result->type;
  1421.       error_emitted = op[0]->type->is_error();
  1422.       break;
  1423.    }
  1424.  
  1425.    case ast_post_inc:
  1426.    case ast_post_dec: {
  1427.       op[0] = this->subexpressions[0]->hir(instructions, state);
  1428.       if (op[0]->type->base_type == GLSL_TYPE_FLOAT)
  1429.          op[1] = new(ctx) ir_constant(1.0f);
  1430.       else
  1431.          op[1] = new(ctx) ir_constant(1);
  1432.  
  1433.       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1434.  
  1435.       type = arithmetic_result_type(op[0], op[1], false, state, & loc);
  1436.  
  1437.       ir_rvalue *temp_rhs;
  1438.       temp_rhs = new(ctx) ir_expression(operations[this->oper], type,
  1439.                                         op[0], op[1]);
  1440.  
  1441.       /* Get a temporary of a copy of the lvalue before it's modified.
  1442.        * This may get thrown away later.
  1443.        */
  1444.       result = get_lvalue_copy(instructions, op[0]->clone(ctx, NULL));
  1445.  
  1446.       (void)do_assignment(instructions, state,
  1447.                           op[0]->clone(ctx, NULL), temp_rhs,
  1448.                           this->subexpressions[0]->get_location());
  1449.  
  1450.       type = result->type;
  1451.       error_emitted = op[0]->type->is_error();
  1452.       break;
  1453.    }
  1454.  
  1455.    case ast_field_selection:
  1456.       result = _mesa_ast_field_selection_to_hir(this, instructions, state);
  1457.       type = result->type;
  1458.       break;
  1459.  
  1460.    case ast_array_index: {
  1461.       YYLTYPE index_loc = subexpressions[1]->get_location();
  1462.  
  1463.       op[0] = subexpressions[0]->hir(instructions, state);
  1464.       op[1] = subexpressions[1]->hir(instructions, state);
  1465.  
  1466.       error_emitted = op[0]->type->is_error() || op[1]->type->is_error();
  1467.  
  1468.       ir_rvalue *const array = op[0];
  1469.  
  1470.       result = new(ctx) ir_dereference_array(op[0], op[1]);
  1471.  
  1472.       /* Do not use op[0] after this point.  Use array.
  1473.        */
  1474.       op[0] = NULL;
  1475.  
  1476.  
  1477.       if (error_emitted)
  1478.          break;
  1479.  
  1480.       if (!array->type->is_array()
  1481.           && !array->type->is_matrix()
  1482.           && !array->type->is_vector()) {
  1483.          _mesa_glsl_error(& index_loc, state,
  1484.                           "cannot dereference non-array / non-matrix / "
  1485.                           "non-vector");
  1486.          error_emitted = true;
  1487.       }
  1488.  
  1489.       if (!op[1]->type->is_integer()) {
  1490.          _mesa_glsl_error(& index_loc, state,
  1491.                           "array index must be integer type");
  1492.          error_emitted = true;
  1493.       } else if (!op[1]->type->is_scalar()) {
  1494.          _mesa_glsl_error(& index_loc, state,
  1495.                           "array index must be scalar");
  1496.          error_emitted = true;
  1497.       }
  1498.  
  1499.       /* If the array index is a constant expression and the array has a
  1500.        * declared size, ensure that the access is in-bounds.  If the array
  1501.        * index is not a constant expression, ensure that the array has a
  1502.        * declared size.
  1503.        */
  1504.       ir_constant *const const_index = op[1]->constant_expression_value();
  1505.       if (const_index != NULL) {
  1506.          const int idx = const_index->value.i[0];
  1507.          const char *type_name;
  1508.          unsigned bound = 0;
  1509.  
  1510.          if (array->type->is_matrix()) {
  1511.             type_name = "matrix";
  1512.          } else if (array->type->is_vector()) {
  1513.             type_name = "vector";
  1514.          } else {
  1515.             type_name = "array";
  1516.          }
  1517.  
  1518.          /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec:
  1519.           *
  1520.           *    "It is illegal to declare an array with a size, and then
  1521.           *    later (in the same shader) index the same array with an
  1522.           *    integral constant expression greater than or equal to the
  1523.           *    declared size. It is also illegal to index an array with a
  1524.           *    negative constant expression."
  1525.           */
  1526.          if (array->type->is_matrix()) {
  1527.             if (array->type->row_type()->vector_elements <= idx) {
  1528.                bound = array->type->row_type()->vector_elements;
  1529.             }
  1530.          } else if (array->type->is_vector()) {
  1531.             if (array->type->vector_elements <= idx) {
  1532.                bound = array->type->vector_elements;
  1533.             }
  1534.          } else {
  1535.             if ((array->type->array_size() > 0)
  1536.                 && (array->type->array_size() <= idx)) {
  1537.                bound = array->type->array_size();
  1538.             }
  1539.          }
  1540.  
  1541.          if (bound > 0) {
  1542.             _mesa_glsl_error(& loc, state, "%s index must be < %u",
  1543.                              type_name, bound);
  1544.             error_emitted = true;
  1545.          } else if (idx < 0) {
  1546.             _mesa_glsl_error(& loc, state, "%s index must be >= 0",
  1547.                              type_name);
  1548.             error_emitted = true;
  1549.          }
  1550.  
  1551.          if (array->type->is_array()) {
  1552.             /* If the array is a variable dereference, it dereferences the
  1553.              * whole array, by definition.  Use this to get the variable.
  1554.              *
  1555.              * FINISHME: Should some methods for getting / setting / testing
  1556.              * FINISHME: array access limits be added to ir_dereference?
  1557.              */
  1558.             ir_variable *const v = array->whole_variable_referenced();
  1559.             if ((v != NULL) && (unsigned(idx) > v->max_array_access))
  1560.                v->max_array_access = idx;
  1561.          }
  1562.       } else if (array->type->array_size() == 0) {
  1563.          _mesa_glsl_error(&loc, state, "unsized array index must be constant");
  1564.       } else {
  1565.          if (array->type->is_array()) {
  1566.             /* whole_variable_referenced can return NULL if the array is a
  1567.              * member of a structure.  In this case it is safe to not update
  1568.              * the max_array_access field because it is never used for fields
  1569.              * of structures.
  1570.              */
  1571.             ir_variable *v = array->whole_variable_referenced();
  1572.             if (v != NULL)
  1573.                v->max_array_access = array->type->array_size();
  1574.          }
  1575.       }
  1576.  
  1577.       /* From page 23 (29 of the PDF) of the GLSL 1.30 spec:
  1578.        *
  1579.        *    "Samplers aggregated into arrays within a shader (using square
  1580.        *    brackets [ ]) can only be indexed with integral constant
  1581.        *    expressions [...]."
  1582.        *
  1583.        * This restriction was added in GLSL 1.30.  Shaders using earlier version
  1584.        * of the language should not be rejected by the compiler front-end for
  1585.        * using this construct.  This allows useful things such as using a loop
  1586.        * counter as the index to an array of samplers.  If the loop in unrolled,
  1587.        * the code should compile correctly.  Instead, emit a warning.
  1588.        */
  1589.       if (array->type->is_array() &&
  1590.           array->type->element_type()->is_sampler() &&
  1591.           const_index == NULL) {
  1592.  
  1593.          if (state->language_version == 100) {
  1594.             _mesa_glsl_warning(&loc, state,
  1595.                                "sampler arrays indexed with non-constant "
  1596.                                "expressions is optional in GLSL ES 1.00");
  1597.          } else if (state->language_version < 130) {
  1598.             _mesa_glsl_warning(&loc, state,
  1599.                                "sampler arrays indexed with non-constant "
  1600.                                "expressions is forbidden in GLSL 1.30 and "
  1601.                                "later");
  1602.          } else {
  1603.             _mesa_glsl_error(&loc, state,
  1604.                              "sampler arrays indexed with non-constant "
  1605.                              "expressions is forbidden in GLSL 1.30 and "
  1606.                              "later");
  1607.             error_emitted = true;
  1608.          }
  1609.       }
  1610.  
  1611.       if (error_emitted)
  1612.          result->type = glsl_type::error_type;
  1613.  
  1614.       type = result->type;
  1615.       break;
  1616.    }
  1617.  
  1618.    case ast_function_call:
  1619.       /* Should *NEVER* get here.  ast_function_call should always be handled
  1620.        * by ast_function_expression::hir.
  1621.        */
  1622.       assert(0);
  1623.       break;
  1624.  
  1625.    case ast_identifier: {
  1626.       /* ast_identifier can appear several places in a full abstract syntax
  1627.        * tree.  This particular use must be at location specified in the grammar
  1628.        * as 'variable_identifier'.
  1629.        */
  1630.       ir_variable *var =
  1631.          state->symbols->get_variable(this->primary_expression.identifier);
  1632.  
  1633.       result = new(ctx) ir_dereference_variable(var);
  1634.  
  1635.       if (var != NULL) {
  1636.          var->used = true;
  1637.          type = result->type;
  1638.       } else {
  1639.          _mesa_glsl_error(& loc, state, "`%s' undeclared",
  1640.                           this->primary_expression.identifier);
  1641.  
  1642.          error_emitted = true;
  1643.       }
  1644.       break;
  1645.    }
  1646.  
  1647.    case ast_int_constant:
  1648.       type = glsl_type::int_type;
  1649.       result = new(ctx) ir_constant(this->primary_expression.int_constant);
  1650.       break;
  1651.  
  1652.    case ast_uint_constant:
  1653.       type = glsl_type::uint_type;
  1654.       result = new(ctx) ir_constant(this->primary_expression.uint_constant);
  1655.       break;
  1656.  
  1657.    case ast_float_constant:
  1658.       type = glsl_type::float_type;
  1659.       result = new(ctx) ir_constant(this->primary_expression.float_constant);
  1660.       break;
  1661.  
  1662.    case ast_bool_constant:
  1663.       type = glsl_type::bool_type;
  1664.       result = new(ctx) ir_constant(bool(this->primary_expression.bool_constant));
  1665.       break;
  1666.  
  1667.    case ast_sequence: {
  1668.       /* It should not be possible to generate a sequence in the AST without
  1669.        * any expressions in it.
  1670.        */
  1671.       assert(!this->expressions.is_empty());
  1672.  
  1673.       /* The r-value of a sequence is the last expression in the sequence.  If
  1674.        * the other expressions in the sequence do not have side-effects (and
  1675.        * therefore add instructions to the instruction list), they get dropped
  1676.        * on the floor.
  1677.        */
  1678.       foreach_list_typed (ast_node, ast, link, &this->expressions)
  1679.          result = ast->hir(instructions, state);
  1680.  
  1681.       type = result->type;
  1682.  
  1683.       /* Any errors should have already been emitted in the loop above.
  1684.        */
  1685.       error_emitted = true;
  1686.       break;
  1687.    }
  1688.    }
  1689.  
  1690.    if (type->is_error() && !error_emitted)
  1691.       _mesa_glsl_error(& loc, state, "type mismatch");
  1692.  
  1693.    return result;
  1694. }
  1695.  
  1696.  
  1697. ir_rvalue *
  1698. ast_expression_statement::hir(exec_list *instructions,
  1699.                               struct _mesa_glsl_parse_state *state)
  1700. {
  1701.    /* It is possible to have expression statements that don't have an
  1702.     * expression.  This is the solitary semicolon:
  1703.     *
  1704.     * for (i = 0; i < 5; i++)
  1705.     *     ;
  1706.     *
  1707.     * In this case the expression will be NULL.  Test for NULL and don't do
  1708.     * anything in that case.
  1709.     */
  1710.    if (expression != NULL)
  1711.       expression->hir(instructions, state);
  1712.  
  1713.    /* Statements do not have r-values.
  1714.     */
  1715.    return NULL;
  1716. }
  1717.  
  1718.  
  1719. ir_rvalue *
  1720. ast_compound_statement::hir(exec_list *instructions,
  1721.                             struct _mesa_glsl_parse_state *state)
  1722. {
  1723.    if (new_scope)
  1724.       state->symbols->push_scope();
  1725.  
  1726.    foreach_list_typed (ast_node, ast, link, &this->statements)
  1727.       ast->hir(instructions, state);
  1728.  
  1729.    if (new_scope)
  1730.       state->symbols->pop_scope();
  1731.  
  1732.    /* Compound statements do not have r-values.
  1733.     */
  1734.    return NULL;
  1735. }
  1736.  
  1737.  
  1738. static const glsl_type *
  1739. process_array_type(YYLTYPE *loc, const glsl_type *base, ast_node *array_size,
  1740.                    struct _mesa_glsl_parse_state *state)
  1741. {
  1742.    unsigned length = 0;
  1743.  
  1744.    /* FINISHME: Reject delcarations of multidimensional arrays. */
  1745.  
  1746.    if (array_size != NULL) {
  1747.       exec_list dummy_instructions;
  1748.       ir_rvalue *const ir = array_size->hir(& dummy_instructions, state);
  1749.       YYLTYPE loc = array_size->get_location();
  1750.  
  1751.       /* FINISHME: Verify that the grammar forbids side-effects in array
  1752.        * FINISHME: sizes.   i.e., 'vec4 [x = 12] data'
  1753.        */
  1754.       assert(dummy_instructions.is_empty());
  1755.  
  1756.       if (ir != NULL) {
  1757.          if (!ir->type->is_integer()) {
  1758.             _mesa_glsl_error(& loc, state, "array size must be integer type");
  1759.          } else if (!ir->type->is_scalar()) {
  1760.             _mesa_glsl_error(& loc, state, "array size must be scalar type");
  1761.          } else {
  1762.             ir_constant *const size = ir->constant_expression_value();
  1763.  
  1764.             if (size == NULL) {
  1765.                _mesa_glsl_error(& loc, state, "array size must be a "
  1766.                                 "constant valued expression");
  1767.             } else if (size->value.i[0] <= 0) {
  1768.                _mesa_glsl_error(& loc, state, "array size must be > 0");
  1769.             } else {
  1770.                assert(size->type == ir->type);
  1771.                length = size->value.u[0];
  1772.             }
  1773.          }
  1774.       }
  1775.    } else if (state->es_shader) {
  1776.       /* Section 10.17 of the GLSL ES 1.00 specification states that unsized
  1777.        * array declarations have been removed from the language.
  1778.        */
  1779.       _mesa_glsl_error(loc, state, "unsized array declarations are not "
  1780.                        "allowed in GLSL ES 1.00.");
  1781.    }
  1782.  
  1783.    return glsl_type::get_array_instance(base, length);
  1784. }
  1785.  
  1786.  
  1787. const glsl_type *
  1788. ast_type_specifier::glsl_type(const char **name,
  1789.                               struct _mesa_glsl_parse_state *state) const
  1790. {
  1791.    const struct glsl_type *type;
  1792.  
  1793.    type = state->symbols->get_type(this->type_name);
  1794.    *name = this->type_name;
  1795.  
  1796.    if (this->is_array) {
  1797.       YYLTYPE loc = this->get_location();
  1798.       type = process_array_type(&loc, type, this->array_size, state);
  1799.    }
  1800.  
  1801.    return type;
  1802. }
  1803.  
  1804.  
  1805. static void
  1806. apply_type_qualifier_to_variable(const struct ast_type_qualifier *qual,
  1807.                                  ir_variable *var,
  1808.                                  struct _mesa_glsl_parse_state *state,
  1809.                                  YYLTYPE *loc)
  1810. {
  1811.    if (qual->flags.q.invariant) {
  1812.       if (var->used) {
  1813.          _mesa_glsl_error(loc, state,
  1814.                           "variable `%s' may not be redeclared "
  1815.                           "`invariant' after being used",
  1816.                           var->name);
  1817.       } else {
  1818.          var->invariant = 1;
  1819.       }
  1820.    }
  1821.  
  1822.    if (qual->flags.q.constant || qual->flags.q.attribute
  1823.        || qual->flags.q.uniform
  1824.        || (qual->flags.q.varying && (state->target == fragment_shader)))
  1825.       var->read_only = 1;
  1826.  
  1827.    if (qual->flags.q.centroid)
  1828.       var->centroid = 1;
  1829.  
  1830.    if (qual->flags.q.attribute && state->target != vertex_shader) {
  1831.       var->type = glsl_type::error_type;
  1832.       _mesa_glsl_error(loc, state,
  1833.                        "`attribute' variables may not be declared in the "
  1834.                        "%s shader",
  1835.                        _mesa_glsl_shader_target_name(state->target));
  1836.    }
  1837.  
  1838.    /* From page 25 (page 31 of the PDF) of the GLSL 1.10 spec:
  1839.     *
  1840.     *     "The varying qualifier can be used only with the data types
  1841.     *     float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of
  1842.     *     these."
  1843.     */
  1844.    if (qual->flags.q.varying) {
  1845.       const glsl_type *non_array_type;
  1846.  
  1847.       if (var->type && var->type->is_array())
  1848.          non_array_type = var->type->fields.array;
  1849.       else
  1850.          non_array_type = var->type;
  1851.  
  1852.       if (non_array_type && non_array_type->base_type != GLSL_TYPE_FLOAT) {
  1853.          var->type = glsl_type::error_type;
  1854.          _mesa_glsl_error(loc, state,
  1855.                           "varying variables must be of base type float");
  1856.       }
  1857.    }
  1858.  
  1859.    /* If there is no qualifier that changes the mode of the variable, leave
  1860.     * the setting alone.
  1861.     */
  1862.    if (qual->flags.q.in && qual->flags.q.out)
  1863.       var->mode = ir_var_inout;
  1864.    else if (qual->flags.q.attribute || qual->flags.q.in
  1865.             || (qual->flags.q.varying && (state->target == fragment_shader)))
  1866.       var->mode = ir_var_in;
  1867.    else if (qual->flags.q.out
  1868.             || (qual->flags.q.varying && (state->target == vertex_shader)))
  1869.       var->mode = ir_var_out;
  1870.    else if (qual->flags.q.uniform)
  1871.       var->mode = ir_var_uniform;
  1872.  
  1873.    if (state->all_invariant && (state->current_function == NULL)) {
  1874.       switch (state->target) {
  1875.       case vertex_shader:
  1876.          if (var->mode == ir_var_out)
  1877.             var->invariant = true;
  1878.          break;
  1879.       case geometry_shader:
  1880.          if ((var->mode == ir_var_in) || (var->mode == ir_var_out))
  1881.             var->invariant = true;
  1882.          break;
  1883.       case fragment_shader:
  1884.          if (var->mode == ir_var_in)
  1885.             var->invariant = true;
  1886.          break;
  1887.       }
  1888.    }
  1889.  
  1890.    if (qual->flags.q.flat)
  1891.       var->interpolation = ir_var_flat;
  1892.    else if (qual->flags.q.noperspective)
  1893.       var->interpolation = ir_var_noperspective;
  1894.    else
  1895.       var->interpolation = ir_var_smooth;
  1896.  
  1897.    var->pixel_center_integer = qual->flags.q.pixel_center_integer;
  1898.    var->origin_upper_left = qual->flags.q.origin_upper_left;
  1899.    if ((qual->flags.q.origin_upper_left || qual->flags.q.pixel_center_integer)
  1900.        && (strcmp(var->name, "gl_FragCoord") != 0)) {
  1901.       const char *const qual_string = (qual->flags.q.origin_upper_left)
  1902.          ? "origin_upper_left" : "pixel_center_integer";
  1903.  
  1904.       _mesa_glsl_error(loc, state,
  1905.                        "layout qualifier `%s' can only be applied to "
  1906.                        "fragment shader input `gl_FragCoord'",
  1907.                        qual_string);
  1908.    }
  1909.  
  1910.    if (qual->flags.q.explicit_location) {
  1911.       const bool global_scope = (state->current_function == NULL);
  1912.       bool fail = false;
  1913.       const char *string = "";
  1914.  
  1915.       /* In the vertex shader only shader inputs can be given explicit
  1916.        * locations.
  1917.        *
  1918.        * In the fragment shader only shader outputs can be given explicit
  1919.        * locations.
  1920.        */
  1921.       switch (state->target) {
  1922.       case vertex_shader:
  1923.          if (!global_scope || (var->mode != ir_var_in)) {
  1924.             fail = true;
  1925.             string = "input";
  1926.          }
  1927.          break;
  1928.  
  1929.       case geometry_shader:
  1930.          _mesa_glsl_error(loc, state,
  1931.                           "geometry shader variables cannot be given "
  1932.                           "explicit locations\n");
  1933.          break;
  1934.  
  1935.       case fragment_shader:
  1936.          if (!global_scope || (var->mode != ir_var_in)) {
  1937.             fail = true;
  1938.             string = "output";
  1939.          }
  1940.          break;
  1941.       };
  1942.  
  1943.       if (fail) {
  1944.          _mesa_glsl_error(loc, state,
  1945.                           "only %s shader %s variables can be given an "
  1946.                           "explicit location\n",
  1947.                           _mesa_glsl_shader_target_name(state->target),
  1948.                           string);
  1949.       } else {
  1950.          var->explicit_location = true;
  1951.  
  1952.          /* This bit of silliness is needed because invalid explicit locations
  1953.           * are supposed to be flagged during linking.  Small negative values
  1954.           * biased by VERT_ATTRIB_GENERIC0 or FRAG_RESULT_DATA0 could alias
  1955.           * built-in values (e.g., -16+VERT_ATTRIB_GENERIC0 = VERT_ATTRIB_POS).
  1956.           * The linker needs to be able to differentiate these cases.  This
  1957.           * ensures that negative values stay negative.
  1958.           */
  1959.          if (qual->location >= 0) {
  1960.             var->location = (state->target == vertex_shader)
  1961.                ? (qual->location + VERT_ATTRIB_GENERIC0)
  1962.                : (qual->location + FRAG_RESULT_DATA0);
  1963.          } else {
  1964.             var->location = qual->location;
  1965.          }
  1966.       }
  1967.    }
  1968.  
  1969.    /* Does the declaration use the 'layout' keyword?
  1970.     */
  1971.    const bool uses_layout = qual->flags.q.pixel_center_integer
  1972.       || qual->flags.q.origin_upper_left
  1973.       || qual->flags.q.explicit_location;
  1974.  
  1975.    /* Does the declaration use the deprecated 'attribute' or 'varying'
  1976.     * keywords?
  1977.     */
  1978.    const bool uses_deprecated_qualifier = qual->flags.q.attribute
  1979.       || qual->flags.q.varying;
  1980.  
  1981.    /* Is the 'layout' keyword used with parameters that allow relaxed checking.
  1982.     * Many implementations of GL_ARB_fragment_coord_conventions_enable and some
  1983.     * implementations (only Mesa?) GL_ARB_explicit_attrib_location_enable
  1984.     * allowed the layout qualifier to be used with 'varying' and 'attribute'.
  1985.     * These extensions and all following extensions that add the 'layout'
  1986.     * keyword have been modified to require the use of 'in' or 'out'.
  1987.     *
  1988.     * The following extension do not allow the deprecated keywords:
  1989.     *
  1990.     *    GL_AMD_conservative_depth
  1991.     *    GL_ARB_gpu_shader5
  1992.     *    GL_ARB_separate_shader_objects
  1993.     *    GL_ARB_tesselation_shader
  1994.     *    GL_ARB_transform_feedback3
  1995.     *    GL_ARB_uniform_buffer_object
  1996.     *
  1997.     * It is unknown whether GL_EXT_shader_image_load_store or GL_NV_gpu_shader5
  1998.     * allow layout with the deprecated keywords.
  1999.     */
  2000.    const bool relaxed_layout_qualifier_checking =
  2001.       state->ARB_fragment_coord_conventions_enable;
  2002.  
  2003.    if (uses_layout && uses_deprecated_qualifier) {
  2004.       if (relaxed_layout_qualifier_checking) {
  2005.          _mesa_glsl_warning(loc, state,
  2006.                             "`layout' qualifier may not be used with "
  2007.                             "`attribute' or `varying'");
  2008.       } else {
  2009.          _mesa_glsl_error(loc, state,
  2010.                           "`layout' qualifier may not be used with "
  2011.                           "`attribute' or `varying'");
  2012.       }
  2013.    }
  2014.  
  2015.    if (var->type->is_array() && state->language_version != 110) {
  2016.       var->array_lvalue = true;
  2017.    }
  2018. }
  2019.  
  2020.  
  2021. ir_rvalue *
  2022. ast_declarator_list::hir(exec_list *instructions,
  2023.                          struct _mesa_glsl_parse_state *state)
  2024. {
  2025.    void *ctx = state;
  2026.    const struct glsl_type *decl_type;
  2027.    const char *type_name = NULL;
  2028.    ir_rvalue *result = NULL;
  2029.    YYLTYPE loc = this->get_location();
  2030.  
  2031.    /* From page 46 (page 52 of the PDF) of the GLSL 1.50 spec:
  2032.     *
  2033.     *     "To ensure that a particular output variable is invariant, it is
  2034.     *     necessary to use the invariant qualifier. It can either be used to
  2035.     *     qualify a previously declared variable as being invariant
  2036.     *
  2037.     *         invariant gl_Position; // make existing gl_Position be invariant"
  2038.     *
  2039.     * In these cases the parser will set the 'invariant' flag in the declarator
  2040.     * list, and the type will be NULL.
  2041.     */
  2042.    if (this->invariant) {
  2043.       assert(this->type == NULL);
  2044.  
  2045.       if (state->current_function != NULL) {
  2046.          _mesa_glsl_error(& loc, state,
  2047.                           "All uses of `invariant' keyword must be at global "
  2048.                           "scope\n");
  2049.       }
  2050.  
  2051.       foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
  2052.          assert(!decl->is_array);
  2053.          assert(decl->array_size == NULL);
  2054.          assert(decl->initializer == NULL);
  2055.  
  2056.          ir_variable *const earlier =
  2057.             state->symbols->get_variable(decl->identifier);
  2058.          if (earlier == NULL) {
  2059.             _mesa_glsl_error(& loc, state,
  2060.                              "Undeclared variable `%s' cannot be marked "
  2061.                              "invariant\n", decl->identifier);
  2062.          } else if ((state->target == vertex_shader)
  2063.                && (earlier->mode != ir_var_out)) {
  2064.             _mesa_glsl_error(& loc, state,
  2065.                              "`%s' cannot be marked invariant, vertex shader "
  2066.                              "outputs only\n", decl->identifier);
  2067.          } else if ((state->target == fragment_shader)
  2068.                && (earlier->mode != ir_var_in)) {
  2069.             _mesa_glsl_error(& loc, state,
  2070.                              "`%s' cannot be marked invariant, fragment shader "
  2071.                              "inputs only\n", decl->identifier);
  2072.          } else if (earlier->used) {
  2073.             _mesa_glsl_error(& loc, state,
  2074.                              "variable `%s' may not be redeclared "
  2075.                              "`invariant' after being used",
  2076.                              earlier->name);
  2077.          } else {
  2078.             earlier->invariant = true;
  2079.          }
  2080.       }
  2081.  
  2082.       /* Invariant redeclarations do not have r-values.
  2083.        */
  2084.       return NULL;
  2085.    }
  2086.  
  2087.    assert(this->type != NULL);
  2088.    assert(!this->invariant);
  2089.  
  2090.    /* The type specifier may contain a structure definition.  Process that
  2091.     * before any of the variable declarations.
  2092.     */
  2093.    (void) this->type->specifier->hir(instructions, state);
  2094.  
  2095.    decl_type = this->type->specifier->glsl_type(& type_name, state);
  2096.    if (this->declarations.is_empty()) {
  2097.       /* The only valid case where the declaration list can be empty is when
  2098.        * the declaration is setting the default precision of a built-in type
  2099.        * (e.g., 'precision highp vec4;').
  2100.        */
  2101.  
  2102.       if (decl_type != NULL) {
  2103.       } else {
  2104.             _mesa_glsl_error(& loc, state, "incomplete declaration");
  2105.       }
  2106.    }
  2107.  
  2108.    foreach_list_typed (ast_declaration, decl, link, &this->declarations) {
  2109.       const struct glsl_type *var_type;
  2110.       ir_variable *var;
  2111.  
  2112.       /* FINISHME: Emit a warning if a variable declaration shadows a
  2113.        * FINISHME: declaration at a higher scope.
  2114.        */
  2115.  
  2116.       if ((decl_type == NULL) || decl_type->is_void()) {
  2117.          if (type_name != NULL) {
  2118.             _mesa_glsl_error(& loc, state,
  2119.                              "invalid type `%s' in declaration of `%s'",
  2120.                              type_name, decl->identifier);
  2121.          } else {
  2122.             _mesa_glsl_error(& loc, state,
  2123.                              "invalid type in declaration of `%s'",
  2124.                              decl->identifier);
  2125.          }
  2126.          continue;
  2127.       }
  2128.  
  2129.       if (decl->is_array) {
  2130.          var_type = process_array_type(&loc, decl_type, decl->array_size,
  2131.                                        state);
  2132.       } else {
  2133.          var_type = decl_type;
  2134.       }
  2135.  
  2136.       var = new(ctx) ir_variable(var_type, decl->identifier, ir_var_auto);
  2137.  
  2138.       /* From page 22 (page 28 of the PDF) of the GLSL 1.10 specification;
  2139.        *
  2140.        *     "Global variables can only use the qualifiers const,
  2141.        *     attribute, uni form, or varying. Only one may be
  2142.        *     specified.
  2143.        *
  2144.        *     Local variables can only use the qualifier const."
  2145.        *
  2146.        * This is relaxed in GLSL 1.30.  It is also relaxed by any extension
  2147.        * that adds the 'layout' keyword.
  2148.        */
  2149.       if ((state->language_version < 130)
  2150.           && !state->ARB_explicit_attrib_location_enable
  2151.           && !state->ARB_fragment_coord_conventions_enable) {
  2152.          if (this->type->qualifier.flags.q.out) {
  2153.             _mesa_glsl_error(& loc, state,
  2154.                              "`out' qualifier in declaration of `%s' "
  2155.                              "only valid for function parameters in %s.",
  2156.                              decl->identifier, state->version_string);
  2157.          }
  2158.          if (this->type->qualifier.flags.q.in) {
  2159.             _mesa_glsl_error(& loc, state,
  2160.                              "`in' qualifier in declaration of `%s' "
  2161.                              "only valid for function parameters in %s.",
  2162.                              decl->identifier, state->version_string);
  2163.          }
  2164.          /* FINISHME: Test for other invalid qualifiers. */
  2165.       }
  2166.  
  2167.       apply_type_qualifier_to_variable(& this->type->qualifier, var, state,
  2168.                                        & loc);
  2169.  
  2170.       if (this->type->qualifier.flags.q.invariant) {
  2171.          if ((state->target == vertex_shader) && !(var->mode == ir_var_out ||
  2172.                                                    var->mode == ir_var_inout)) {
  2173.             /* FINISHME: Note that this doesn't work for invariant on
  2174.              * a function signature outval
  2175.              */
  2176.             _mesa_glsl_error(& loc, state,
  2177.                              "`%s' cannot be marked invariant, vertex shader "
  2178.                              "outputs only\n", var->name);
  2179.          } else if ((state->target == fragment_shader) &&
  2180.                     !(var->mode == ir_var_in || var->mode == ir_var_inout)) {
  2181.             /* FINISHME: Note that this doesn't work for invariant on
  2182.              * a function signature inval
  2183.              */
  2184.             _mesa_glsl_error(& loc, state,
  2185.                              "`%s' cannot be marked invariant, fragment shader "
  2186.                              "inputs only\n", var->name);
  2187.          }
  2188.       }
  2189.  
  2190.       if (state->current_function != NULL) {
  2191.          const char *mode = NULL;
  2192.          const char *extra = "";
  2193.  
  2194.          /* There is no need to check for 'inout' here because the parser will
  2195.           * only allow that in function parameter lists.
  2196.           */
  2197.          if (this->type->qualifier.flags.q.attribute) {
  2198.             mode = "attribute";
  2199.          } else if (this->type->qualifier.flags.q.uniform) {
  2200.             mode = "uniform";
  2201.          } else if (this->type->qualifier.flags.q.varying) {
  2202.             mode = "varying";
  2203.          } else if (this->type->qualifier.flags.q.in) {
  2204.             mode = "in";
  2205.             extra = " or in function parameter list";
  2206.          } else if (this->type->qualifier.flags.q.out) {
  2207.             mode = "out";
  2208.             extra = " or in function parameter list";
  2209.          }
  2210.  
  2211.          if (mode) {
  2212.             _mesa_glsl_error(& loc, state,
  2213.                              "%s variable `%s' must be declared at "
  2214.                              "global scope%s",
  2215.                              mode, var->name, extra);
  2216.          }
  2217.       } else if (var->mode == ir_var_in) {
  2218.          var->read_only = true;
  2219.  
  2220.          if (state->target == vertex_shader) {
  2221.             bool error_emitted = false;
  2222.  
  2223.             /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
  2224.              *
  2225.              *    "Vertex shader inputs can only be float, floating-point
  2226.              *    vectors, matrices, signed and unsigned integers and integer
  2227.              *    vectors. Vertex shader inputs can also form arrays of these
  2228.              *    types, but not structures."
  2229.              *
  2230.              * From page 31 (page 27 of the PDF) of the GLSL 1.30 spec:
  2231.              *
  2232.              *    "Vertex shader inputs can only be float, floating-point
  2233.              *    vectors, matrices, signed and unsigned integers and integer
  2234.              *    vectors. They cannot be arrays or structures."
  2235.              *
  2236.              * From page 23 (page 29 of the PDF) of the GLSL 1.20 spec:
  2237.              *
  2238.              *    "The attribute qualifier can be used only with float,
  2239.              *    floating-point vectors, and matrices. Attribute variables
  2240.              *    cannot be declared as arrays or structures."
  2241.              */
  2242.             const glsl_type *check_type = var->type->is_array()
  2243.                ? var->type->fields.array : var->type;
  2244.  
  2245.             switch (check_type->base_type) {
  2246.             case GLSL_TYPE_FLOAT:
  2247.                break;
  2248.             case GLSL_TYPE_UINT:
  2249.             case GLSL_TYPE_INT:
  2250.                if (state->language_version > 120)
  2251.                   break;
  2252.                /* FALLTHROUGH */
  2253.             default:
  2254.                _mesa_glsl_error(& loc, state,
  2255.                                 "vertex shader input / attribute cannot have "
  2256.                                 "type %s`%s'",
  2257.                                 var->type->is_array() ? "array of " : "",
  2258.                                 check_type->name);
  2259.                error_emitted = true;
  2260.             }
  2261.  
  2262.             if (!error_emitted && (state->language_version <= 130)
  2263.                 && var->type->is_array()) {
  2264.                _mesa_glsl_error(& loc, state,
  2265.                                 "vertex shader input / attribute cannot have "
  2266.                                 "array type");
  2267.                error_emitted = true;
  2268.             }
  2269.          }
  2270.       }
  2271.  
  2272.       /* Precision qualifiers exists only in GLSL versions 1.00 and >= 1.30.
  2273.        */
  2274.       if (this->type->specifier->precision != ast_precision_none
  2275.           && state->language_version != 100
  2276.           && state->language_version < 130) {
  2277.  
  2278.          _mesa_glsl_error(&loc, state,
  2279.                           "precision qualifiers are supported only in GLSL ES "
  2280.                           "1.00, and GLSL 1.30 and later");
  2281.       }
  2282.  
  2283.  
  2284.       /* Precision qualifiers only apply to floating point and integer types.
  2285.        *
  2286.        * From section 4.5.2 of the GLSL 1.30 spec:
  2287.        *    "Any floating point or any integer declaration can have the type
  2288.        *    preceded by one of these precision qualifiers [...] Literal
  2289.        *    constants do not have precision qualifiers. Neither do Boolean
  2290.        *    variables.
  2291.        */
  2292.       if (this->type->specifier->precision != ast_precision_none
  2293.           && !var->type->is_float()
  2294.           && !var->type->is_integer()
  2295.           && !(var->type->is_array()
  2296.                && (var->type->fields.array->is_float()
  2297.                    || var->type->fields.array->is_integer()))) {
  2298.  
  2299.          _mesa_glsl_error(&loc, state,
  2300.                           "precision qualifiers apply only to floating point "
  2301.                           "and integer types");
  2302.       }
  2303.  
  2304.       /* Process the initializer and add its instructions to a temporary
  2305.        * list.  This list will be added to the instruction stream (below) after
  2306.        * the declaration is added.  This is done because in some cases (such as
  2307.        * redeclarations) the declaration may not actually be added to the
  2308.        * instruction stream.
  2309.        */
  2310.       exec_list initializer_instructions;
  2311.       if (decl->initializer != NULL) {
  2312.          YYLTYPE initializer_loc = decl->initializer->get_location();
  2313.  
  2314.          /* From page 24 (page 30 of the PDF) of the GLSL 1.10 spec:
  2315.           *
  2316.           *    "All uniform variables are read-only and are initialized either
  2317.           *    directly by an application via API commands, or indirectly by
  2318.           *    OpenGL."
  2319.           */
  2320.          if ((state->language_version <= 110)
  2321.              && (var->mode == ir_var_uniform)) {
  2322.             _mesa_glsl_error(& initializer_loc, state,
  2323.                              "cannot initialize uniforms in GLSL 1.10");
  2324.          }
  2325.  
  2326.          if (var->type->is_sampler()) {
  2327.             _mesa_glsl_error(& initializer_loc, state,
  2328.                              "cannot initialize samplers");
  2329.          }
  2330.  
  2331.          if ((var->mode == ir_var_in) && (state->current_function == NULL)) {
  2332.             _mesa_glsl_error(& initializer_loc, state,
  2333.                              "cannot initialize %s shader input / %s",
  2334.                              _mesa_glsl_shader_target_name(state->target),
  2335.                              (state->target == vertex_shader)
  2336.                              ? "attribute" : "varying");
  2337.          }
  2338.  
  2339.          ir_dereference *const lhs = new(ctx) ir_dereference_variable(var);
  2340.          ir_rvalue *rhs = decl->initializer->hir(&initializer_instructions,
  2341.                                                  state);
  2342.  
  2343.          /* Calculate the constant value if this is a const or uniform
  2344.           * declaration.
  2345.           */
  2346.          if (this->type->qualifier.flags.q.constant
  2347.              || this->type->qualifier.flags.q.uniform) {
  2348.             ir_rvalue *new_rhs = validate_assignment(state, var->type, rhs);
  2349.             if (new_rhs != NULL) {
  2350.                rhs = new_rhs;
  2351.  
  2352.                ir_constant *constant_value = rhs->constant_expression_value();
  2353.                if (!constant_value) {
  2354.                   _mesa_glsl_error(& initializer_loc, state,
  2355.                                    "initializer of %s variable `%s' must be a "
  2356.                                    "constant expression",
  2357.                                    (this->type->qualifier.flags.q.constant)
  2358.                                    ? "const" : "uniform",
  2359.                                    decl->identifier);
  2360.                   if (var->type->is_numeric()) {
  2361.                      /* Reduce cascading errors. */
  2362.                      var->constant_value = ir_constant::zero(ctx, var->type);
  2363.                   }
  2364.                } else {
  2365.                   rhs = constant_value;
  2366.                   var->constant_value = constant_value;
  2367.                }
  2368.             } else {
  2369.                _mesa_glsl_error(&initializer_loc, state,
  2370.                                 "initializer of type %s cannot be assigned to "
  2371.                                 "variable of type %s",
  2372.                                 rhs->type->name, var->type->name);
  2373.                if (var->type->is_numeric()) {
  2374.                   /* Reduce cascading errors. */
  2375.                   var->constant_value = ir_constant::zero(ctx, var->type);
  2376.                }
  2377.             }
  2378.          }
  2379.  
  2380.          if (rhs && !rhs->type->is_error()) {
  2381.             bool temp = var->read_only;
  2382.             if (this->type->qualifier.flags.q.constant)
  2383.                var->read_only = false;
  2384.  
  2385.             /* Never emit code to initialize a uniform.
  2386.              */
  2387.             const glsl_type *initializer_type;
  2388.             if (!this->type->qualifier.flags.q.uniform) {
  2389.                result = do_assignment(&initializer_instructions, state,
  2390.                                       lhs, rhs,
  2391.                                       this->get_location());
  2392.                initializer_type = result->type;
  2393.             } else
  2394.                initializer_type = rhs->type;
  2395.  
  2396.             /* If the declared variable is an unsized array, it must inherrit
  2397.              * its full type from the initializer.  A declaration such as
  2398.              *
  2399.              *     uniform float a[] = float[](1.0, 2.0, 3.0, 3.0);
  2400.              *
  2401.              * becomes
  2402.              *
  2403.              *     uniform float a[4] = float[](1.0, 2.0, 3.0, 3.0);
  2404.              *
  2405.              * The assignment generated in the if-statement (below) will also
  2406.              * automatically handle this case for non-uniforms.
  2407.              *
  2408.              * If the declared variable is not an array, the types must
  2409.              * already match exactly.  As a result, the type assignment
  2410.              * here can be done unconditionally.  For non-uniforms the call
  2411.              * to do_assignment can change the type of the initializer (via
  2412.              * the implicit conversion rules).  For uniforms the initializer
  2413.              * must be a constant expression, and the type of that expression
  2414.              * was validated above.
  2415.              */
  2416.             var->type = initializer_type;
  2417.  
  2418.             var->read_only = temp;
  2419.          }
  2420.       }
  2421.  
  2422.       /* From page 23 (page 29 of the PDF) of the GLSL 1.10 spec:
  2423.        *
  2424.        *     "It is an error to write to a const variable outside of
  2425.        *      its declaration, so they must be initialized when
  2426.        *      declared."
  2427.        */
  2428.       if (this->type->qualifier.flags.q.constant && decl->initializer == NULL) {
  2429.          _mesa_glsl_error(& loc, state,
  2430.                           "const declaration of `%s' must be initialized",
  2431.                           decl->identifier);
  2432.       }
  2433.  
  2434.       /* Check if this declaration is actually a re-declaration, either to
  2435.        * resize an array or add qualifiers to an existing variable.
  2436.        *
  2437.        * This is allowed for variables in the current scope, or when at
  2438.        * global scope (for built-ins in the implicit outer scope).
  2439.        */
  2440.       ir_variable *earlier = state->symbols->get_variable(decl->identifier);
  2441.       if (earlier != NULL && (state->current_function == NULL ||
  2442.           state->symbols->name_declared_this_scope(decl->identifier))) {
  2443.  
  2444.          /* From page 24 (page 30 of the PDF) of the GLSL 1.50 spec,
  2445.           *
  2446.           * "It is legal to declare an array without a size and then
  2447.           *  later re-declare the same name as an array of the same
  2448.           *  type and specify a size."
  2449.           */
  2450.          if ((earlier->type->array_size() == 0)
  2451.              && var->type->is_array()
  2452.              && (var->type->element_type() == earlier->type->element_type())) {
  2453.             /* FINISHME: This doesn't match the qualifiers on the two
  2454.              * FINISHME: declarations.  It's not 100% clear whether this is
  2455.              * FINISHME: required or not.
  2456.              */
  2457.  
  2458.             /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
  2459.              *
  2460.              *     "The size [of gl_TexCoord] can be at most
  2461.              *     gl_MaxTextureCoords."
  2462.              */
  2463.             const unsigned size = unsigned(var->type->array_size());
  2464.             if ((strcmp("gl_TexCoord", var->name) == 0)
  2465.                 && (size > state->Const.MaxTextureCoords)) {
  2466.                YYLTYPE loc = this->get_location();
  2467.  
  2468.                _mesa_glsl_error(& loc, state, "`gl_TexCoord' array size cannot "
  2469.                                 "be larger than gl_MaxTextureCoords (%u)\n",
  2470.                                 state->Const.MaxTextureCoords);
  2471.             } else if ((size > 0) && (size <= earlier->max_array_access)) {
  2472.                YYLTYPE loc = this->get_location();
  2473.  
  2474.                _mesa_glsl_error(& loc, state, "array size must be > %u due to "
  2475.                                 "previous access",
  2476.                                 earlier->max_array_access);
  2477.             }
  2478.  
  2479.             earlier->type = var->type;
  2480.             delete var;
  2481.             var = NULL;
  2482.          } else if (state->ARB_fragment_coord_conventions_enable
  2483.                     && strcmp(var->name, "gl_FragCoord") == 0
  2484.                     && earlier->type == var->type
  2485.                     && earlier->mode == var->mode) {
  2486.             /* Allow redeclaration of gl_FragCoord for ARB_fcc layout
  2487.              * qualifiers.
  2488.              */
  2489.             earlier->origin_upper_left = var->origin_upper_left;
  2490.             earlier->pixel_center_integer = var->pixel_center_integer;
  2491.          } else {
  2492.             YYLTYPE loc = this->get_location();
  2493.             _mesa_glsl_error(&loc, state, "`%s' redeclared", decl->identifier);
  2494.          }
  2495.  
  2496.          continue;
  2497.       }
  2498.  
  2499.       /* By now, we know it's a new variable declaration (we didn't hit the
  2500.        * above "continue").
  2501.        *
  2502.        * From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
  2503.        *
  2504.        *   "Identifiers starting with "gl_" are reserved for use by
  2505.        *   OpenGL, and may not be declared in a shader as either a
  2506.        *   variable or a function."
  2507.        */
  2508.       if (strncmp(decl->identifier, "gl_", 3) == 0)
  2509.          _mesa_glsl_error(& loc, state,
  2510.                           "identifier `%s' uses reserved `gl_' prefix",
  2511.                           decl->identifier);
  2512.  
  2513.       /* Add the variable to the symbol table.  Note that the initializer's
  2514.        * IR was already processed earlier (though it hasn't been emitted yet),
  2515.        * without the variable in scope.
  2516.        *
  2517.        * This differs from most C-like languages, but it follows the GLSL
  2518.        * specification.  From page 28 (page 34 of the PDF) of the GLSL 1.50
  2519.        * spec:
  2520.        *
  2521.        *     "Within a declaration, the scope of a name starts immediately
  2522.        *     after the initializer if present or immediately after the name
  2523.        *     being declared if not."
  2524.        */
  2525.       if (!state->symbols->add_variable(var)) {
  2526.          YYLTYPE loc = this->get_location();
  2527.          _mesa_glsl_error(&loc, state, "name `%s' already taken in the "
  2528.                           "current scope", decl->identifier);
  2529.          continue;
  2530.       }
  2531.  
  2532.       /* Push the variable declaration to the top.  It means that all
  2533.        * the variable declarations will appear in a funny
  2534.        * last-to-first order, but otherwise we run into trouble if a
  2535.        * function is prototyped, a global var is decled, then the
  2536.        * function is defined with usage of the global var.  See
  2537.        * glslparsertest's CorrectModule.frag.
  2538.        */
  2539.       instructions->push_head(var);
  2540.       instructions->append_list(&initializer_instructions);
  2541.    }
  2542.  
  2543.  
  2544.    /* Generally, variable declarations do not have r-values.  However,
  2545.     * one is used for the declaration in
  2546.     *
  2547.     * while (bool b = some_condition()) {
  2548.     *   ...
  2549.     * }
  2550.     *
  2551.     * so we return the rvalue from the last seen declaration here.
  2552.     */
  2553.    return result;
  2554. }
  2555.  
  2556.  
  2557. ir_rvalue *
  2558. ast_parameter_declarator::hir(exec_list *instructions,
  2559.                               struct _mesa_glsl_parse_state *state)
  2560. {
  2561.    void *ctx = state;
  2562.    const struct glsl_type *type;
  2563.    const char *name = NULL;
  2564.    YYLTYPE loc = this->get_location();
  2565.  
  2566.    type = this->type->specifier->glsl_type(& name, state);
  2567.  
  2568.    if (type == NULL) {
  2569.       if (name != NULL) {
  2570.          _mesa_glsl_error(& loc, state,
  2571.                           "invalid type `%s' in declaration of `%s'",
  2572.                           name, this->identifier);
  2573.       } else {
  2574.          _mesa_glsl_error(& loc, state,
  2575.                           "invalid type in declaration of `%s'",
  2576.                           this->identifier);
  2577.       }
  2578.  
  2579.       type = glsl_type::error_type;
  2580.    }
  2581.  
  2582.    /* From page 62 (page 68 of the PDF) of the GLSL 1.50 spec:
  2583.     *
  2584.     *    "Functions that accept no input arguments need not use void in the
  2585.     *    argument list because prototypes (or definitions) are required and
  2586.     *    therefore there is no ambiguity when an empty argument list "( )" is
  2587.     *    declared. The idiom "(void)" as a parameter list is provided for
  2588.     *    convenience."
  2589.     *
  2590.     * Placing this check here prevents a void parameter being set up
  2591.     * for a function, which avoids tripping up checks for main taking
  2592.     * parameters and lookups of an unnamed symbol.
  2593.     */
  2594.    if (type->is_void()) {
  2595.       if (this->identifier != NULL)
  2596.          _mesa_glsl_error(& loc, state,
  2597.                           "named parameter cannot have type `void'");
  2598.  
  2599.       is_void = true;
  2600.       return NULL;
  2601.    }
  2602.  
  2603.    if (formal_parameter && (this->identifier == NULL)) {
  2604.       _mesa_glsl_error(& loc, state, "formal parameter lacks a name");
  2605.       return NULL;
  2606.    }
  2607.  
  2608.    /* This only handles "vec4 foo[..]".  The earlier specifier->glsl_type(...)
  2609.     * call already handled the "vec4[..] foo" case.
  2610.     */
  2611.    if (this->is_array) {
  2612.       type = process_array_type(&loc, type, this->array_size, state);
  2613.    }
  2614.  
  2615.    if (type->array_size() == 0) {
  2616.       _mesa_glsl_error(&loc, state, "arrays passed as parameters must have "
  2617.                        "a declared size.");
  2618.       type = glsl_type::error_type;
  2619.    }
  2620.  
  2621.    is_void = false;
  2622.    ir_variable *var = new(ctx) ir_variable(type, this->identifier, ir_var_in);
  2623.  
  2624.    /* Apply any specified qualifiers to the parameter declaration.  Note that
  2625.     * for function parameters the default mode is 'in'.
  2626.     */
  2627.    apply_type_qualifier_to_variable(& this->type->qualifier, var, state, & loc);
  2628.  
  2629.    instructions->push_tail(var);
  2630.  
  2631.    /* Parameter declarations do not have r-values.
  2632.     */
  2633.    return NULL;
  2634. }
  2635.  
  2636.  
  2637. void
  2638. ast_parameter_declarator::parameters_to_hir(exec_list *ast_parameters,
  2639.                                             bool formal,
  2640.                                             exec_list *ir_parameters,
  2641.                                             _mesa_glsl_parse_state *state)
  2642. {
  2643.    ast_parameter_declarator *void_param = NULL;
  2644.    unsigned count = 0;
  2645.  
  2646.    foreach_list_typed (ast_parameter_declarator, param, link, ast_parameters) {
  2647.       param->formal_parameter = formal;
  2648.       param->hir(ir_parameters, state);
  2649.  
  2650.       if (param->is_void)
  2651.          void_param = param;
  2652.  
  2653.       count++;
  2654.    }
  2655.  
  2656.    if ((void_param != NULL) && (count > 1)) {
  2657.       YYLTYPE loc = void_param->get_location();
  2658.  
  2659.       _mesa_glsl_error(& loc, state,
  2660.                        "`void' parameter must be only parameter");
  2661.    }
  2662. }
  2663.  
  2664.  
  2665. void
  2666. emit_function(_mesa_glsl_parse_state *state, exec_list *instructions,
  2667.               ir_function *f)
  2668. {
  2669.    /* Emit the new function header */
  2670.    if (state->current_function == NULL) {
  2671.       instructions->push_tail(f);
  2672.    } else {
  2673.       /* IR invariants disallow function declarations or definitions nested
  2674.        * within other function definitions.  Insert the new ir_function
  2675.        * block in the instruction sequence before the ir_function block
  2676.        * containing the current ir_function_signature.
  2677.        */
  2678.       ir_function *const curr =
  2679.          const_cast<ir_function *>(state->current_function->function());
  2680.  
  2681.       curr->insert_before(f);
  2682.    }
  2683. }
  2684.  
  2685.  
  2686. ir_rvalue *
  2687. ast_function::hir(exec_list *instructions,
  2688.                   struct _mesa_glsl_parse_state *state)
  2689. {
  2690.    void *ctx = state;
  2691.    ir_function *f = NULL;
  2692.    ir_function_signature *sig = NULL;
  2693.    exec_list hir_parameters;
  2694.  
  2695.    const char *const name = identifier;
  2696.  
  2697.    /* From page 21 (page 27 of the PDF) of the GLSL 1.20 spec,
  2698.     *
  2699.     *   "Function declarations (prototypes) cannot occur inside of functions;
  2700.     *   they must be at global scope, or for the built-in functions, outside
  2701.     *   the global scope."
  2702.     *
  2703.     * From page 27 (page 33 of the PDF) of the GLSL ES 1.00.16 spec,
  2704.     *
  2705.     *   "User defined functions may only be defined within the global scope."
  2706.     *
  2707.     * Note that this language does not appear in GLSL 1.10.
  2708.     */
  2709.    if ((state->current_function != NULL) && (state->language_version != 110)) {
  2710.       YYLTYPE loc = this->get_location();
  2711.       _mesa_glsl_error(&loc, state,
  2712.                        "declaration of function `%s' not allowed within "
  2713.                        "function body", name);
  2714.    }
  2715.  
  2716.    /* From page 15 (page 21 of the PDF) of the GLSL 1.10 spec,
  2717.     *
  2718.     *   "Identifiers starting with "gl_" are reserved for use by
  2719.     *   OpenGL, and may not be declared in a shader as either a
  2720.     *   variable or a function."
  2721.     */
  2722.    if (strncmp(name, "gl_", 3) == 0) {
  2723.       YYLTYPE loc = this->get_location();
  2724.       _mesa_glsl_error(&loc, state,
  2725.                        "identifier `%s' uses reserved `gl_' prefix", name);
  2726.    }
  2727.  
  2728.    /* Convert the list of function parameters to HIR now so that they can be
  2729.     * used below to compare this function's signature with previously seen
  2730.     * signatures for functions with the same name.
  2731.     */
  2732.    ast_parameter_declarator::parameters_to_hir(& this->parameters,
  2733.                                                is_definition,
  2734.                                                & hir_parameters, state);
  2735.  
  2736.    const char *return_type_name;
  2737.    const glsl_type *return_type =
  2738.       this->return_type->specifier->glsl_type(& return_type_name, state);
  2739.  
  2740.    if (!return_type) {
  2741.       YYLTYPE loc = this->get_location();
  2742.       _mesa_glsl_error(&loc, state,
  2743.                        "function `%s' has undeclared return type `%s'",
  2744.                        name, return_type_name);
  2745.       return_type = glsl_type::error_type;
  2746.    }
  2747.  
  2748.    /* From page 56 (page 62 of the PDF) of the GLSL 1.30 spec:
  2749.     * "No qualifier is allowed on the return type of a function."
  2750.     */
  2751.    if (this->return_type->has_qualifiers()) {
  2752.       YYLTYPE loc = this->get_location();
  2753.       _mesa_glsl_error(& loc, state,
  2754.                        "function `%s' return type has qualifiers", name);
  2755.    }
  2756.  
  2757.    /* Verify that this function's signature either doesn't match a previously
  2758.     * seen signature for a function with the same name, or, if a match is found,
  2759.     * that the previously seen signature does not have an associated definition.
  2760.     */
  2761.    f = state->symbols->get_function(name);
  2762.    if (f != NULL && (state->es_shader || f->has_user_signature())) {
  2763.       sig = f->exact_matching_signature(&hir_parameters);
  2764.       if (sig != NULL) {
  2765.          const char *badvar = sig->qualifiers_match(&hir_parameters);
  2766.          if (badvar != NULL) {
  2767.             YYLTYPE loc = this->get_location();
  2768.  
  2769.             _mesa_glsl_error(&loc, state, "function `%s' parameter `%s' "
  2770.                              "qualifiers don't match prototype", name, badvar);
  2771.          }
  2772.  
  2773.          if (sig->return_type != return_type) {
  2774.             YYLTYPE loc = this->get_location();
  2775.  
  2776.             _mesa_glsl_error(&loc, state, "function `%s' return type doesn't "
  2777.                              "match prototype", name);
  2778.          }
  2779.  
  2780.          if (is_definition && sig->is_defined) {
  2781.             YYLTYPE loc = this->get_location();
  2782.  
  2783.             _mesa_glsl_error(& loc, state, "function `%s' redefined", name);
  2784.          }
  2785.       }
  2786.    } else {
  2787.       f = new(ctx) ir_function(name);
  2788.       if (!state->symbols->add_function(f)) {
  2789.          /* This function name shadows a non-function use of the same name. */
  2790.          YYLTYPE loc = this->get_location();
  2791.  
  2792.          _mesa_glsl_error(&loc, state, "function name `%s' conflicts with "
  2793.                           "non-function", name);
  2794.          return NULL;
  2795.       }
  2796.  
  2797.       emit_function(state, instructions, f);
  2798.    }
  2799.  
  2800.    /* Verify the return type of main() */
  2801.    if (strcmp(name, "main") == 0) {
  2802.       if (! return_type->is_void()) {
  2803.          YYLTYPE loc = this->get_location();
  2804.  
  2805.          _mesa_glsl_error(& loc, state, "main() must return void");
  2806.       }
  2807.  
  2808.       if (!hir_parameters.is_empty()) {
  2809.          YYLTYPE loc = this->get_location();
  2810.  
  2811.          _mesa_glsl_error(& loc, state, "main() must not take any parameters");
  2812.       }
  2813.    }
  2814.  
  2815.    /* Finish storing the information about this new function in its signature.
  2816.     */
  2817.    if (sig == NULL) {
  2818.       sig = new(ctx) ir_function_signature(return_type);
  2819.       f->add_signature(sig);
  2820.    }
  2821.  
  2822.    sig->replace_parameters(&hir_parameters);
  2823.    signature = sig;
  2824.  
  2825.    /* Function declarations (prototypes) do not have r-values.
  2826.     */
  2827.    return NULL;
  2828. }
  2829.  
  2830.  
  2831. ir_rvalue *
  2832. ast_function_definition::hir(exec_list *instructions,
  2833.                              struct _mesa_glsl_parse_state *state)
  2834. {
  2835.    prototype->is_definition = true;
  2836.    prototype->hir(instructions, state);
  2837.  
  2838.    ir_function_signature *signature = prototype->signature;
  2839.    if (signature == NULL)
  2840.       return NULL;
  2841.  
  2842.    assert(state->current_function == NULL);
  2843.    state->current_function = signature;
  2844.    state->found_return = false;
  2845.  
  2846.    /* Duplicate parameters declared in the prototype as concrete variables.
  2847.     * Add these to the symbol table.
  2848.     */
  2849.    state->symbols->push_scope();
  2850.    foreach_iter(exec_list_iterator, iter, signature->parameters) {
  2851.       ir_variable *const var = ((ir_instruction *) iter.get())->as_variable();
  2852.  
  2853.       assert(var != NULL);
  2854.  
  2855.       /* The only way a parameter would "exist" is if two parameters have
  2856.        * the same name.
  2857.        */
  2858.       if (state->symbols->name_declared_this_scope(var->name)) {
  2859.          YYLTYPE loc = this->get_location();
  2860.  
  2861.          _mesa_glsl_error(& loc, state, "parameter `%s' redeclared", var->name);
  2862.       } else {
  2863.          state->symbols->add_variable(var);
  2864.       }
  2865.    }
  2866.  
  2867.    /* Convert the body of the function to HIR. */
  2868.    this->body->hir(&signature->body, state);
  2869.    signature->is_defined = true;
  2870.  
  2871.    state->symbols->pop_scope();
  2872.  
  2873.    assert(state->current_function == signature);
  2874.    state->current_function = NULL;
  2875.  
  2876.    if (!signature->return_type->is_void() && !state->found_return) {
  2877.       YYLTYPE loc = this->get_location();
  2878.       _mesa_glsl_error(& loc, state, "function `%s' has non-void return type "
  2879.                        "%s, but no return statement",
  2880.                        signature->function_name(),
  2881.                        signature->return_type->name);
  2882.    }
  2883.  
  2884.    /* Function definitions do not have r-values.
  2885.     */
  2886.    return NULL;
  2887. }
  2888.  
  2889.  
  2890. ir_rvalue *
  2891. ast_jump_statement::hir(exec_list *instructions,
  2892.                         struct _mesa_glsl_parse_state *state)
  2893. {
  2894.    void *ctx = state;
  2895.  
  2896.    switch (mode) {
  2897.    case ast_return: {
  2898.       ir_return *inst;
  2899.       assert(state->current_function);
  2900.  
  2901.       if (opt_return_value) {
  2902.          ir_rvalue *const ret = opt_return_value->hir(instructions, state);
  2903.  
  2904.          /* The value of the return type can be NULL if the shader says
  2905.           * 'return foo();' and foo() is a function that returns void.
  2906.           *
  2907.           * NOTE: The GLSL spec doesn't say that this is an error.  The type
  2908.           * of the return value is void.  If the return type of the function is
  2909.           * also void, then this should compile without error.  Seriously.
  2910.           */
  2911.          const glsl_type *const ret_type =
  2912.             (ret == NULL) ? glsl_type::void_type : ret->type;
  2913.  
  2914.          /* Implicit conversions are not allowed for return values. */
  2915.          if (state->current_function->return_type != ret_type) {
  2916.             YYLTYPE loc = this->get_location();
  2917.  
  2918.             _mesa_glsl_error(& loc, state,
  2919.                              "`return' with wrong type %s, in function `%s' "
  2920.                              "returning %s",
  2921.                              ret_type->name,
  2922.                              state->current_function->function_name(),
  2923.                              state->current_function->return_type->name);
  2924.          }
  2925.  
  2926.          inst = new(ctx) ir_return(ret);
  2927.       } else {
  2928.          if (state->current_function->return_type->base_type !=
  2929.              GLSL_TYPE_VOID) {
  2930.             YYLTYPE loc = this->get_location();
  2931.  
  2932.             _mesa_glsl_error(& loc, state,
  2933.                              "`return' with no value, in function %s returning "
  2934.                              "non-void",
  2935.                              state->current_function->function_name());
  2936.          }
  2937.          inst = new(ctx) ir_return;
  2938.       }
  2939.  
  2940.       state->found_return = true;
  2941.       instructions->push_tail(inst);
  2942.       break;
  2943.    }
  2944.  
  2945.    case ast_discard:
  2946.       if (state->target != fragment_shader) {
  2947.          YYLTYPE loc = this->get_location();
  2948.  
  2949.          _mesa_glsl_error(& loc, state,
  2950.                           "`discard' may only appear in a fragment shader");
  2951.       }
  2952.       instructions->push_tail(new(ctx) ir_discard);
  2953.       break;
  2954.  
  2955.    case ast_break:
  2956.    case ast_continue:
  2957.       /* FINISHME: Handle switch-statements.  They cannot contain 'continue',
  2958.        * FINISHME: and they use a different IR instruction for 'break'.
  2959.        */
  2960.       /* FINISHME: Correctly handle the nesting.  If a switch-statement is
  2961.        * FINISHME: inside a loop, a 'continue' is valid and will bind to the
  2962.        * FINISHME: loop.
  2963.        */
  2964.       if (state->loop_or_switch_nesting == NULL) {
  2965.          YYLTYPE loc = this->get_location();
  2966.  
  2967.          _mesa_glsl_error(& loc, state,
  2968.                           "`%s' may only appear in a loop",
  2969.                           (mode == ast_break) ? "break" : "continue");
  2970.       } else {
  2971.          ir_loop *const loop = state->loop_or_switch_nesting->as_loop();
  2972.  
  2973.          /* Inline the for loop expression again, since we don't know
  2974.           * where near the end of the loop body the normal copy of it
  2975.           * is going to be placed.
  2976.           */
  2977.          if (mode == ast_continue &&
  2978.              state->loop_or_switch_nesting_ast->rest_expression) {
  2979.             state->loop_or_switch_nesting_ast->rest_expression->hir(instructions,
  2980.                                                                     state);
  2981.          }
  2982.  
  2983.          if (loop != NULL) {
  2984.             ir_loop_jump *const jump =
  2985.                new(ctx) ir_loop_jump((mode == ast_break)
  2986.                                      ? ir_loop_jump::jump_break
  2987.                                      : ir_loop_jump::jump_continue);
  2988.             instructions->push_tail(jump);
  2989.          }
  2990.       }
  2991.  
  2992.       break;
  2993.    }
  2994.  
  2995.    /* Jump instructions do not have r-values.
  2996.     */
  2997.    return NULL;
  2998. }
  2999.  
  3000.  
  3001. ir_rvalue *
  3002. ast_selection_statement::hir(exec_list *instructions,
  3003.                              struct _mesa_glsl_parse_state *state)
  3004. {
  3005.    void *ctx = state;
  3006.  
  3007.    ir_rvalue *const condition = this->condition->hir(instructions, state);
  3008.  
  3009.    /* From page 66 (page 72 of the PDF) of the GLSL 1.50 spec:
  3010.     *
  3011.     *    "Any expression whose type evaluates to a Boolean can be used as the
  3012.     *    conditional expression bool-expression. Vector types are not accepted
  3013.     *    as the expression to if."
  3014.     *
  3015.     * The checks are separated so that higher quality diagnostics can be
  3016.     * generated for cases where both rules are violated.
  3017.     */
  3018.    if (!condition->type->is_boolean() || !condition->type->is_scalar()) {
  3019.       YYLTYPE loc = this->condition->get_location();
  3020.  
  3021.       _mesa_glsl_error(& loc, state, "if-statement condition must be scalar "
  3022.                        "boolean");
  3023.    }
  3024.  
  3025.    ir_if *const stmt = new(ctx) ir_if(condition);
  3026.  
  3027.    if (then_statement != NULL) {
  3028.       state->symbols->push_scope();
  3029.       then_statement->hir(& stmt->then_instructions, state);
  3030.       state->symbols->pop_scope();
  3031.    }
  3032.  
  3033.    if (else_statement != NULL) {
  3034.       state->symbols->push_scope();
  3035.       else_statement->hir(& stmt->else_instructions, state);
  3036.       state->symbols->pop_scope();
  3037.    }
  3038.  
  3039.    instructions->push_tail(stmt);
  3040.  
  3041.    /* if-statements do not have r-values.
  3042.     */
  3043.    return NULL;
  3044. }
  3045.  
  3046.  
  3047. void
  3048. ast_iteration_statement::condition_to_hir(ir_loop *stmt,
  3049.                                           struct _mesa_glsl_parse_state *state)
  3050. {
  3051.    void *ctx = state;
  3052.  
  3053.    if (condition != NULL) {
  3054.       ir_rvalue *const cond =
  3055.          condition->hir(& stmt->body_instructions, state);
  3056.  
  3057.       if ((cond == NULL)
  3058.           || !cond->type->is_boolean() || !cond->type->is_scalar()) {
  3059.          YYLTYPE loc = condition->get_location();
  3060.  
  3061.          _mesa_glsl_error(& loc, state,
  3062.                           "loop condition must be scalar boolean");
  3063.       } else {
  3064.          /* As the first code in the loop body, generate a block that looks
  3065.           * like 'if (!condition) break;' as the loop termination condition.
  3066.           */
  3067.          ir_rvalue *const not_cond =
  3068.             new(ctx) ir_expression(ir_unop_logic_not, glsl_type::bool_type, cond,
  3069.                                    NULL);
  3070.  
  3071.          ir_if *const if_stmt = new(ctx) ir_if(not_cond);
  3072.  
  3073.          ir_jump *const break_stmt =
  3074.             new(ctx) ir_loop_jump(ir_loop_jump::jump_break);
  3075.  
  3076.          if_stmt->then_instructions.push_tail(break_stmt);
  3077.          stmt->body_instructions.push_tail(if_stmt);
  3078.       }
  3079.    }
  3080. }
  3081.  
  3082.  
  3083. ir_rvalue *
  3084. ast_iteration_statement::hir(exec_list *instructions,
  3085.                              struct _mesa_glsl_parse_state *state)
  3086. {
  3087.    void *ctx = state;
  3088.  
  3089.    /* For-loops and while-loops start a new scope, but do-while loops do not.
  3090.     */
  3091.    if (mode != ast_do_while)
  3092.       state->symbols->push_scope();
  3093.  
  3094.    if (init_statement != NULL)
  3095.       init_statement->hir(instructions, state);
  3096.  
  3097.    ir_loop *const stmt = new(ctx) ir_loop();
  3098.    instructions->push_tail(stmt);
  3099.  
  3100.    /* Track the current loop and / or switch-statement nesting.
  3101.     */
  3102.    ir_instruction *const nesting = state->loop_or_switch_nesting;
  3103.    ast_iteration_statement *nesting_ast = state->loop_or_switch_nesting_ast;
  3104.  
  3105.    state->loop_or_switch_nesting = stmt;
  3106.    state->loop_or_switch_nesting_ast = this;
  3107.  
  3108.    if (mode != ast_do_while)
  3109.       condition_to_hir(stmt, state);
  3110.  
  3111.    if (body != NULL)
  3112.       body->hir(& stmt->body_instructions, state);
  3113.  
  3114.    if (rest_expression != NULL)
  3115.       rest_expression->hir(& stmt->body_instructions, state);
  3116.  
  3117.    if (mode == ast_do_while)
  3118.       condition_to_hir(stmt, state);
  3119.  
  3120.    if (mode != ast_do_while)
  3121.       state->symbols->pop_scope();
  3122.  
  3123.    /* Restore previous nesting before returning.
  3124.     */
  3125.    state->loop_or_switch_nesting = nesting;
  3126.    state->loop_or_switch_nesting_ast = nesting_ast;
  3127.  
  3128.    /* Loops do not have r-values.
  3129.     */
  3130.    return NULL;
  3131. }
  3132.  
  3133.  
  3134. ir_rvalue *
  3135. ast_type_specifier::hir(exec_list *instructions,
  3136.                           struct _mesa_glsl_parse_state *state)
  3137. {
  3138.    if (!this->is_precision_statement && this->structure == NULL)
  3139.       return NULL;
  3140.  
  3141.    YYLTYPE loc = this->get_location();
  3142.  
  3143.    if (this->precision != ast_precision_none
  3144.        && state->language_version != 100
  3145.        && state->language_version < 130) {
  3146.       _mesa_glsl_error(&loc, state,
  3147.                        "precision qualifiers exist only in "
  3148.                        "GLSL ES 1.00, and GLSL 1.30 and later");
  3149.       return NULL;
  3150.    }
  3151.    if (this->precision != ast_precision_none
  3152.        && this->structure != NULL) {
  3153.       _mesa_glsl_error(&loc, state,
  3154.                        "precision qualifiers do not apply to structures");
  3155.       return NULL;
  3156.    }
  3157.  
  3158.    /* If this is a precision statement, check that the type to which it is
  3159.     * applied is either float or int.
  3160.     *
  3161.     * From section 4.5.3 of the GLSL 1.30 spec:
  3162.     *    "The precision statement
  3163.     *       precision precision-qualifier type;
  3164.     *    can be used to establish a default precision qualifier. The type
  3165.     *    field can be either int or float [...].  Any other types or
  3166.     *    qualifiers will result in an error.
  3167.     */
  3168.    if (this->is_precision_statement) {
  3169.       assert(this->precision != ast_precision_none);
  3170.       assert(this->structure == NULL); /* The check for structures was
  3171.                                         * performed above. */
  3172.       if (this->is_array) {
  3173.          _mesa_glsl_error(&loc, state,
  3174.                           "default precision statements do not apply to "
  3175.                           "arrays");
  3176.          return NULL;
  3177.       }
  3178.       if (this->type_specifier != ast_float
  3179.           && this->type_specifier != ast_int) {
  3180.          _mesa_glsl_error(&loc, state,
  3181.                           "default precision statements apply only to types "
  3182.                           "float and int");
  3183.          return NULL;
  3184.       }
  3185.  
  3186.       /* FINISHME: Translate precision statements into IR. */
  3187.       return NULL;
  3188.    }
  3189.  
  3190.    if (this->structure != NULL)
  3191.       return this->structure->hir(instructions, state);
  3192.  
  3193.    return NULL;
  3194. }
  3195.  
  3196.  
  3197. ir_rvalue *
  3198. ast_struct_specifier::hir(exec_list *instructions,
  3199.                           struct _mesa_glsl_parse_state *state)
  3200. {
  3201.    unsigned decl_count = 0;
  3202.  
  3203.    /* Make an initial pass over the list of structure fields to determine how
  3204.     * many there are.  Each element in this list is an ast_declarator_list.
  3205.     * This means that we actually need to count the number of elements in the
  3206.     * 'declarations' list in each of the elements.
  3207.     */
  3208.    foreach_list_typed (ast_declarator_list, decl_list, link,
  3209.                        &this->declarations) {
  3210.       foreach_list_const (decl_ptr, & decl_list->declarations) {
  3211.          decl_count++;
  3212.       }
  3213.    }
  3214.  
  3215.    /* Allocate storage for the structure fields and process the field
  3216.     * declarations.  As the declarations are processed, try to also convert
  3217.     * the types to HIR.  This ensures that structure definitions embedded in
  3218.     * other structure definitions are processed.
  3219.     */
  3220.    glsl_struct_field *const fields = ralloc_array(state, glsl_struct_field,
  3221.                                                   decl_count);
  3222.  
  3223.    unsigned i = 0;
  3224.    foreach_list_typed (ast_declarator_list, decl_list, link,
  3225.                        &this->declarations) {
  3226.       const char *type_name;
  3227.  
  3228.       decl_list->type->specifier->hir(instructions, state);
  3229.  
  3230.       /* Section 10.9 of the GLSL ES 1.00 specification states that
  3231.        * embedded structure definitions have been removed from the language.
  3232.        */
  3233.       if (state->es_shader && decl_list->type->specifier->structure != NULL) {
  3234.          YYLTYPE loc = this->get_location();
  3235.          _mesa_glsl_error(&loc, state, "Embedded structure definitions are "
  3236.                           "not allowed in GLSL ES 1.00.");
  3237.       }
  3238.  
  3239.       const glsl_type *decl_type =
  3240.          decl_list->type->specifier->glsl_type(& type_name, state);
  3241.  
  3242.       foreach_list_typed (ast_declaration, decl, link,
  3243.                           &decl_list->declarations) {
  3244.          const struct glsl_type *field_type = decl_type;
  3245.          if (decl->is_array) {
  3246.             YYLTYPE loc = decl->get_location();
  3247.             field_type = process_array_type(&loc, decl_type, decl->array_size,
  3248.                                             state);
  3249.          }
  3250.          fields[i].type = (field_type != NULL)
  3251.             ? field_type : glsl_type::error_type;
  3252.          fields[i].name = decl->identifier;
  3253.          i++;
  3254.       }
  3255.    }
  3256.  
  3257.    assert(i == decl_count);
  3258.  
  3259.    const glsl_type *t =
  3260.       glsl_type::get_record_instance(fields, decl_count, this->name);
  3261.  
  3262.    YYLTYPE loc = this->get_location();
  3263.    if (!state->symbols->add_type(name, t)) {
  3264.       _mesa_glsl_error(& loc, state, "struct `%s' previously defined", name);
  3265.    } else {
  3266.       const glsl_type **s = reralloc(state, state->user_structures,
  3267.                                      const glsl_type *,
  3268.                                      state->num_user_structures + 1);
  3269.       if (s != NULL) {
  3270.          s[state->num_user_structures] = t;
  3271.          state->user_structures = s;
  3272.          state->num_user_structures++;
  3273.       }
  3274.    }
  3275.  
  3276.    /* Structure type definitions do not have r-values.
  3277.     */
  3278.    return NULL;
  3279. }
  3280.